xref: /petsc/src/dm/impls/plex/plexcreate.c (revision 6913077d92d45dc85e2cd558d8cf01c8c45125fc)
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>
5b7f5c055SJed Brown #include <petsc/private/kernels/blockmatmult.h>
6b7f5c055SJed Brown #include <petsc/private/kernels/blockinvert.h>
7552f7358SJed Brown 
8b09969d6SVaclav Hapla PetscLogEvent DMPLEX_CreateFromFile, DMPLEX_BuildFromCellList, DMPLEX_BuildCoordinatesFromCellList;
958cd63d5SVaclav Hapla 
109318fe57SMatthew G. Knepley /* External function declarations here */
119318fe57SMatthew G. Knepley static PetscErrorCode DMInitialize_Plex(DM dm);
129318fe57SMatthew G. Knepley 
13e600fa54SMatthew G. Knepley /* This copies internal things in the Plex structure that we generally want when making a new, related Plex */
14e600fa54SMatthew G. Knepley PetscErrorCode DMPlexCopy_Internal(DM dmin, PetscBool copyPeriodicity, DM dmout)
15e600fa54SMatthew G. Knepley {
16e600fa54SMatthew G. Knepley   const DMBoundaryType *bd;
17e600fa54SMatthew G. Knepley   const PetscReal      *maxCell, *L;
18e600fa54SMatthew G. Knepley   PetscBool             isper, dist;
19e600fa54SMatthew G. Knepley   PetscErrorCode        ierr;
20e600fa54SMatthew G. Knepley 
21e600fa54SMatthew G. Knepley   PetscFunctionBegin;
22e600fa54SMatthew G. Knepley   if (copyPeriodicity) {
23e600fa54SMatthew G. Knepley     ierr = DMGetPeriodicity(dmin, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
24e600fa54SMatthew G. Knepley     ierr = DMSetPeriodicity(dmout, isper,  maxCell,  L,  bd);CHKERRQ(ierr);
25e600fa54SMatthew G. Knepley   }
26e600fa54SMatthew G. Knepley   ierr = DMPlexDistributeGetDefault(dmin, &dist);CHKERRQ(ierr);
27e600fa54SMatthew G. Knepley   ierr = DMPlexDistributeSetDefault(dmout, dist);CHKERRQ(ierr);
28e600fa54SMatthew G. Knepley   ((DM_Plex *) dmout->data)->useHashLocation = ((DM_Plex *) dmin->data)->useHashLocation;
29e600fa54SMatthew G. Knepley   PetscFunctionReturn(0);
30e600fa54SMatthew G. Knepley }
31e600fa54SMatthew G. Knepley 
329318fe57SMatthew G. Knepley /* Replace dm with the contents of ndm, and then destroy ndm
339318fe57SMatthew G. Knepley    - Share the DM_Plex structure
349318fe57SMatthew G. Knepley    - Share the coordinates
359318fe57SMatthew G. Knepley    - Share the SF
369318fe57SMatthew G. Knepley */
379318fe57SMatthew G. Knepley static PetscErrorCode DMPlexReplace_Static(DM dm, DM *ndm)
389318fe57SMatthew G. Knepley {
399318fe57SMatthew G. Knepley   PetscSF               sf;
409318fe57SMatthew G. Knepley   DM                    dmNew = *ndm, coordDM, coarseDM;
419318fe57SMatthew G. Knepley   Vec                   coords;
429318fe57SMatthew G. Knepley   PetscBool             isper;
439318fe57SMatthew G. Knepley   const PetscReal      *maxCell, *L;
449318fe57SMatthew G. Knepley   const DMBoundaryType *bd;
459318fe57SMatthew G. Knepley   PetscInt              dim, cdim;
469318fe57SMatthew G. Knepley   PetscErrorCode        ierr;
479318fe57SMatthew G. Knepley 
489318fe57SMatthew G. Knepley   PetscFunctionBegin;
499318fe57SMatthew G. Knepley   if (dm == dmNew) {
509318fe57SMatthew G. Knepley     ierr = DMDestroy(ndm);CHKERRQ(ierr);
519318fe57SMatthew G. Knepley     PetscFunctionReturn(0);
529318fe57SMatthew G. Knepley   }
539318fe57SMatthew G. Knepley   dm->setupcalled = dmNew->setupcalled;
549318fe57SMatthew G. Knepley   ierr = DMGetDimension(dmNew, &dim);CHKERRQ(ierr);
559318fe57SMatthew G. Knepley   ierr = DMSetDimension(dm, dim);CHKERRQ(ierr);
569318fe57SMatthew G. Knepley   ierr = DMGetCoordinateDim(dmNew, &cdim);CHKERRQ(ierr);
579318fe57SMatthew G. Knepley   ierr = DMSetCoordinateDim(dm, cdim);CHKERRQ(ierr);
589318fe57SMatthew G. Knepley   ierr = DMGetPointSF(dmNew, &sf);CHKERRQ(ierr);
599318fe57SMatthew G. Knepley   ierr = DMSetPointSF(dm, sf);CHKERRQ(ierr);
609318fe57SMatthew G. Knepley   ierr = DMGetCoordinateDM(dmNew, &coordDM);CHKERRQ(ierr);
619318fe57SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dmNew, &coords);CHKERRQ(ierr);
629318fe57SMatthew G. Knepley   ierr = DMSetCoordinateDM(dm, coordDM);CHKERRQ(ierr);
639318fe57SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, coords);CHKERRQ(ierr);
649318fe57SMatthew G. Knepley   /* Do not want to create the coordinate field if it does not already exist, so do not call DMGetCoordinateField() */
659318fe57SMatthew G. Knepley   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
669318fe57SMatthew G. Knepley   dm->coordinateField = dmNew->coordinateField;
6761a622f3SMatthew G. Knepley   ((DM_Plex *) dmNew->data)->coordFunc = ((DM_Plex *) dm->data)->coordFunc;
689318fe57SMatthew G. Knepley   ierr = DMGetPeriodicity(dmNew, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
699318fe57SMatthew G. Knepley   ierr = DMSetPeriodicity(dm, isper, maxCell, L, bd);CHKERRQ(ierr);
709318fe57SMatthew G. Knepley   ierr = DMDestroy_Plex(dm);CHKERRQ(ierr);
719318fe57SMatthew G. Knepley   ierr = DMInitialize_Plex(dm);CHKERRQ(ierr);
729318fe57SMatthew G. Knepley   dm->data = dmNew->data;
739318fe57SMatthew G. Knepley   ((DM_Plex *) dmNew->data)->refct++;
749318fe57SMatthew G. Knepley   ierr = DMDestroyLabelLinkList_Internal(dm);CHKERRQ(ierr);
752cbb9b06SVaclav Hapla   ierr = DMCopyLabels(dmNew, dm, PETSC_OWN_POINTER, PETSC_TRUE, DM_COPY_LABELS_FAIL);CHKERRQ(ierr);
769318fe57SMatthew G. Knepley   ierr = DMGetCoarseDM(dmNew,&coarseDM);CHKERRQ(ierr);
779318fe57SMatthew G. Knepley   ierr = DMSetCoarseDM(dm,coarseDM);CHKERRQ(ierr);
789318fe57SMatthew G. Knepley   ierr = DMDestroy(ndm);CHKERRQ(ierr);
799318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
809318fe57SMatthew G. Knepley }
819318fe57SMatthew G. Knepley 
829318fe57SMatthew G. Knepley /* Swap dm with the contents of dmNew
839318fe57SMatthew G. Knepley    - Swap the DM_Plex structure
849318fe57SMatthew G. Knepley    - Swap the coordinates
859318fe57SMatthew G. Knepley    - Swap the point PetscSF
869318fe57SMatthew G. Knepley */
879318fe57SMatthew G. Knepley static PetscErrorCode DMPlexSwap_Static(DM dmA, DM dmB)
889318fe57SMatthew G. Knepley {
899318fe57SMatthew G. Knepley   DM              coordDMA, coordDMB;
909318fe57SMatthew G. Knepley   Vec             coordsA,  coordsB;
919318fe57SMatthew G. Knepley   PetscSF         sfA,      sfB;
929318fe57SMatthew G. Knepley   DMField         fieldTmp;
939318fe57SMatthew G. Knepley   void            *tmp;
949318fe57SMatthew G. Knepley   DMLabelLink     listTmp;
959318fe57SMatthew G. Knepley   DMLabel         depthTmp;
969318fe57SMatthew G. Knepley   PetscInt        tmpI;
979318fe57SMatthew G. Knepley   PetscErrorCode  ierr;
989318fe57SMatthew G. Knepley 
999318fe57SMatthew G. Knepley   PetscFunctionBegin;
1009318fe57SMatthew G. Knepley   if (dmA == dmB) PetscFunctionReturn(0);
1019318fe57SMatthew G. Knepley   ierr = DMGetPointSF(dmA, &sfA);CHKERRQ(ierr);
1029318fe57SMatthew G. Knepley   ierr = DMGetPointSF(dmB, &sfB);CHKERRQ(ierr);
1039318fe57SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) sfA);CHKERRQ(ierr);
1049318fe57SMatthew G. Knepley   ierr = DMSetPointSF(dmA, sfB);CHKERRQ(ierr);
1059318fe57SMatthew G. Knepley   ierr = DMSetPointSF(dmB, sfA);CHKERRQ(ierr);
1069318fe57SMatthew G. Knepley   ierr = PetscObjectDereference((PetscObject) sfA);CHKERRQ(ierr);
1079318fe57SMatthew G. Knepley 
1089318fe57SMatthew G. Knepley   ierr = DMGetCoordinateDM(dmA, &coordDMA);CHKERRQ(ierr);
1099318fe57SMatthew G. Knepley   ierr = DMGetCoordinateDM(dmB, &coordDMB);CHKERRQ(ierr);
1109318fe57SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) coordDMA);CHKERRQ(ierr);
1119318fe57SMatthew G. Knepley   ierr = DMSetCoordinateDM(dmA, coordDMB);CHKERRQ(ierr);
1129318fe57SMatthew G. Knepley   ierr = DMSetCoordinateDM(dmB, coordDMA);CHKERRQ(ierr);
1139318fe57SMatthew G. Knepley   ierr = PetscObjectDereference((PetscObject) coordDMA);CHKERRQ(ierr);
1149318fe57SMatthew G. Knepley 
1159318fe57SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dmA, &coordsA);CHKERRQ(ierr);
1169318fe57SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dmB, &coordsB);CHKERRQ(ierr);
1179318fe57SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) coordsA);CHKERRQ(ierr);
1189318fe57SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmA, coordsB);CHKERRQ(ierr);
1199318fe57SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dmB, coordsA);CHKERRQ(ierr);
1209318fe57SMatthew G. Knepley   ierr = PetscObjectDereference((PetscObject) coordsA);CHKERRQ(ierr);
1219318fe57SMatthew G. Knepley 
1229318fe57SMatthew G. Knepley   fieldTmp             = dmA->coordinateField;
1239318fe57SMatthew G. Knepley   dmA->coordinateField = dmB->coordinateField;
1249318fe57SMatthew G. Knepley   dmB->coordinateField = fieldTmp;
1259318fe57SMatthew G. Knepley   tmp       = dmA->data;
1269318fe57SMatthew G. Knepley   dmA->data = dmB->data;
1279318fe57SMatthew G. Knepley   dmB->data = tmp;
1289318fe57SMatthew G. Knepley   listTmp   = dmA->labels;
1299318fe57SMatthew G. Knepley   dmA->labels = dmB->labels;
1309318fe57SMatthew G. Knepley   dmB->labels = listTmp;
1319318fe57SMatthew G. Knepley   depthTmp  = dmA->depthLabel;
1329318fe57SMatthew G. Knepley   dmA->depthLabel = dmB->depthLabel;
1339318fe57SMatthew G. Knepley   dmB->depthLabel = depthTmp;
1349318fe57SMatthew G. Knepley   depthTmp  = dmA->celltypeLabel;
1359318fe57SMatthew G. Knepley   dmA->celltypeLabel = dmB->celltypeLabel;
1369318fe57SMatthew G. Knepley   dmB->celltypeLabel = depthTmp;
1379318fe57SMatthew G. Knepley   tmpI         = dmA->levelup;
1389318fe57SMatthew G. Knepley   dmA->levelup = dmB->levelup;
1399318fe57SMatthew G. Knepley   dmB->levelup = tmpI;
1409318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
1419318fe57SMatthew G. Knepley }
1429318fe57SMatthew G. Knepley 
1439318fe57SMatthew G. Knepley static PetscErrorCode DMPlexInterpolateInPlace_Internal(DM dm)
1449318fe57SMatthew G. Knepley {
1459318fe57SMatthew G. Knepley   DM             idm;
1469318fe57SMatthew G. Knepley   PetscErrorCode ierr;
1479318fe57SMatthew G. Knepley 
1489318fe57SMatthew G. Knepley   PetscFunctionBegin;
1499318fe57SMatthew G. Knepley   ierr = DMPlexInterpolate(dm, &idm);CHKERRQ(ierr);
1509318fe57SMatthew G. Knepley   ierr = DMPlexCopyCoordinates(dm, idm);CHKERRQ(ierr);
1519318fe57SMatthew G. Knepley   ierr = DMPlexReplace_Static(dm, &idm);CHKERRQ(ierr);
1529318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
1539318fe57SMatthew G. Knepley }
1549318fe57SMatthew G. Knepley 
1559318fe57SMatthew G. Knepley /*@C
1569318fe57SMatthew G. Knepley   DMPlexCreateCoordinateSpace - Creates a finite element space for the coordinates
1579318fe57SMatthew G. Knepley 
1589318fe57SMatthew G. Knepley   Collective
1599318fe57SMatthew G. Knepley 
1609318fe57SMatthew G. Knepley   Input Parameters:
1619318fe57SMatthew G. Knepley + DM        - The DM
1624f9ab2b4SJed Brown . degree    - The degree of the finite element or PETSC_DECIDE
1639318fe57SMatthew G. Knepley - coordFunc - An optional function to map new points from refinement to the surface
1649318fe57SMatthew G. Knepley 
1659318fe57SMatthew G. Knepley   Level: advanced
1669318fe57SMatthew G. Knepley 
1679318fe57SMatthew G. Knepley .seealso: PetscFECreateLagrange(), DMGetCoordinateDM()
1689318fe57SMatthew G. Knepley @*/
1699318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateCoordinateSpace(DM dm, PetscInt degree, PetscPointFunc coordFunc)
1709318fe57SMatthew G. Knepley {
1719318fe57SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
1729318fe57SMatthew G. Knepley   DM             cdm;
1739318fe57SMatthew G. Knepley   PetscDS        cds;
1749318fe57SMatthew G. Knepley   PetscFE        fe;
1759318fe57SMatthew G. Knepley   PetscClassId   id;
1769318fe57SMatthew G. Knepley   PetscErrorCode ierr;
1779318fe57SMatthew G. Knepley 
1789318fe57SMatthew G. Knepley   PetscFunctionBegin;
1799318fe57SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
1809318fe57SMatthew G. Knepley   ierr = DMGetDS(cdm, &cds);CHKERRQ(ierr);
1819318fe57SMatthew G. Knepley   ierr = PetscDSGetDiscretization(cds, 0, (PetscObject *) &fe);CHKERRQ(ierr);
1829318fe57SMatthew G. Knepley   ierr = PetscObjectGetClassId((PetscObject) fe, &id);CHKERRQ(ierr);
1839318fe57SMatthew G. Knepley   if (id != PETSCFE_CLASSID) {
1849318fe57SMatthew G. Knepley     PetscBool simplex;
1859318fe57SMatthew G. Knepley     PetscInt  dim, dE, qorder;
1869318fe57SMatthew G. Knepley 
1879318fe57SMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1889318fe57SMatthew G. Knepley     ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
1899318fe57SMatthew G. Knepley     ierr = DMPlexIsSimplex(dm, &simplex);CHKERRQ(ierr);
1909318fe57SMatthew G. Knepley     qorder = degree;
1919318fe57SMatthew G. Knepley     ierr = PetscObjectOptionsBegin((PetscObject) cdm);CHKERRQ(ierr);
1929318fe57SMatthew 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);
1939318fe57SMatthew G. Knepley     ierr = PetscOptionsEnd();CHKERRQ(ierr);
1944f9ab2b4SJed Brown     if (degree == PETSC_DECIDE) fe = NULL;
1954f9ab2b4SJed Brown     else {
1969318fe57SMatthew G. Knepley       ierr = PetscFECreateLagrange(PETSC_COMM_SELF, dim, dE, simplex, degree, qorder, &fe);CHKERRQ(ierr);
1979318fe57SMatthew G. Knepley       ierr = DMSetField(cdm, 0, NULL, (PetscObject) fe);CHKERRQ(ierr);
1989318fe57SMatthew G. Knepley       ierr = DMCreateDS(cdm);CHKERRQ(ierr);
1994f9ab2b4SJed Brown     }
2009318fe57SMatthew G. Knepley     ierr = DMProjectCoordinates(dm, fe);CHKERRQ(ierr);
2019318fe57SMatthew G. Knepley     ierr = PetscFEDestroy(&fe);CHKERRQ(ierr);
2029318fe57SMatthew G. Knepley   }
2039318fe57SMatthew G. Knepley   mesh->coordFunc = coordFunc;
2049318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
2059318fe57SMatthew G. Knepley }
2069318fe57SMatthew G. Knepley 
2071df5d5c5SMatthew G. Knepley /*@
2081df5d5c5SMatthew G. Knepley   DMPlexCreateDoublet - Creates a mesh of two cells of the specified type, optionally with later refinement.
2091df5d5c5SMatthew G. Knepley 
210d083f849SBarry Smith   Collective
2111df5d5c5SMatthew G. Knepley 
2121df5d5c5SMatthew G. Knepley   Input Parameters:
2131df5d5c5SMatthew G. Knepley + comm - The communicator for the DM object
2141df5d5c5SMatthew G. Knepley . dim - The spatial dimension
2151df5d5c5SMatthew G. Knepley . simplex - Flag for simplicial cells, otherwise they are tensor product cells
2161df5d5c5SMatthew G. Knepley . interpolate - Flag to create intermediate mesh pieces (edges, faces)
2171df5d5c5SMatthew G. Knepley - refinementLimit - A nonzero number indicates the largest admissible volume for a refined cell
2181df5d5c5SMatthew G. Knepley 
2191df5d5c5SMatthew G. Knepley   Output Parameter:
2201df5d5c5SMatthew G. Knepley . dm - The DM object
2211df5d5c5SMatthew G. Knepley 
2221df5d5c5SMatthew G. Knepley   Level: beginner
2231df5d5c5SMatthew G. Knepley 
2241df5d5c5SMatthew G. Knepley .seealso: DMSetType(), DMCreate()
2251df5d5c5SMatthew G. Knepley @*/
2260fdc7489SMatthew Knepley PetscErrorCode DMPlexCreateDoublet(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscBool interpolate, PetscReal refinementLimit, DM *newdm)
2271df5d5c5SMatthew G. Knepley {
2281df5d5c5SMatthew G. Knepley   DM             dm;
2291df5d5c5SMatthew G. Knepley   PetscMPIInt    rank;
2301df5d5c5SMatthew G. Knepley   PetscErrorCode ierr;
2311df5d5c5SMatthew G. Knepley 
2321df5d5c5SMatthew G. Knepley   PetscFunctionBegin;
2331df5d5c5SMatthew G. Knepley   ierr = DMCreate(comm, &dm);CHKERRQ(ierr);
2341df5d5c5SMatthew G. Knepley   ierr = DMSetType(dm, DMPLEX);CHKERRQ(ierr);
235c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(dm, dim);CHKERRQ(ierr);
236ffc4695bSBarry Smith   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
237ce78fa2fSMatthew G. Knepley   switch (dim) {
238ce78fa2fSMatthew G. Knepley   case 2:
239ce78fa2fSMatthew G. Knepley     if (simplex) {ierr = PetscObjectSetName((PetscObject) dm, "triangular");CHKERRQ(ierr);}
240ce78fa2fSMatthew G. Knepley     else         {ierr = PetscObjectSetName((PetscObject) dm, "quadrilateral");CHKERRQ(ierr);}
241ce78fa2fSMatthew G. Knepley     break;
242ce78fa2fSMatthew G. Knepley   case 3:
243ce78fa2fSMatthew G. Knepley     if (simplex) {ierr = PetscObjectSetName((PetscObject) dm, "tetrahedral");CHKERRQ(ierr);}
244ce78fa2fSMatthew G. Knepley     else         {ierr = PetscObjectSetName((PetscObject) dm, "hexahedral");CHKERRQ(ierr);}
245ce78fa2fSMatthew G. Knepley     break;
246ce78fa2fSMatthew G. Knepley   default:
24798921bdaSJacob Faibussowitsch     SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %D", dim);
248ce78fa2fSMatthew G. Knepley   }
2491df5d5c5SMatthew G. Knepley   if (rank) {
2501df5d5c5SMatthew G. Knepley     PetscInt numPoints[2] = {0, 0};
2511df5d5c5SMatthew G. Knepley     ierr = DMPlexCreateFromDAG(dm, 1, numPoints, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
2521df5d5c5SMatthew G. Knepley   } else {
2531df5d5c5SMatthew G. Knepley     switch (dim) {
2541df5d5c5SMatthew G. Knepley     case 2:
2551df5d5c5SMatthew G. Knepley       if (simplex) {
2561df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {4, 2};
2571df5d5c5SMatthew G. Knepley         PetscInt    coneSize[6]         = {3, 3, 0, 0, 0, 0};
2581df5d5c5SMatthew G. Knepley         PetscInt    cones[6]            = {2, 3, 4,  5, 4, 3};
2591df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[6] = {0, 0, 0,  0, 0, 0};
2601df5d5c5SMatthew G. Knepley         PetscScalar vertexCoords[8]     = {-0.5, 0.5, 0.0, 0.0, 0.0, 1.0, 0.5, 0.5};
2611df5d5c5SMatthew G. Knepley 
2621df5d5c5SMatthew G. Knepley         ierr = DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2631df5d5c5SMatthew G. Knepley       } else {
2641df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {6, 2};
2651df5d5c5SMatthew G. Knepley         PetscInt    coneSize[8]         = {4, 4, 0, 0, 0, 0, 0, 0};
2661df5d5c5SMatthew G. Knepley         PetscInt    cones[8]            = {2, 3, 4, 5,  3, 6, 7, 4};
2671df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[8] = {0, 0, 0, 0,  0, 0, 0, 0};
2681df5d5c5SMatthew 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};
2691df5d5c5SMatthew G. Knepley 
2701df5d5c5SMatthew G. Knepley         ierr = DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2711df5d5c5SMatthew G. Knepley       }
2721df5d5c5SMatthew G. Knepley       break;
2731df5d5c5SMatthew G. Knepley     case 3:
2741df5d5c5SMatthew G. Knepley       if (simplex) {
2751df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {5, 2};
2761df5d5c5SMatthew G. Knepley         PetscInt    coneSize[7]         = {4, 4, 0, 0, 0, 0, 0};
2771df5d5c5SMatthew G. Knepley         PetscInt    cones[8]            = {4, 3, 5, 2,  5, 3, 4, 6};
2781df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[8] = {0, 0, 0, 0,  0, 0, 0, 0};
2791df5d5c5SMatthew 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};
2801df5d5c5SMatthew G. Knepley 
2811df5d5c5SMatthew G. Knepley         ierr = DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2821df5d5c5SMatthew G. Knepley       } else {
2831df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]         = {12, 2};
2841df5d5c5SMatthew G. Knepley         PetscInt    coneSize[14]         = {8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
2851df5d5c5SMatthew G. Knepley         PetscInt    cones[16]            = {2, 3, 4, 5, 6, 7, 8, 9,  5, 4, 10, 11, 7, 12, 13, 8};
2861df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[16] = {0, 0, 0, 0, 0, 0, 0, 0,  0, 0,  0,  0, 0,  0,  0, 0};
2871df5d5c5SMatthew 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,
2881df5d5c5SMatthew 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,
2891df5d5c5SMatthew 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};
2901df5d5c5SMatthew G. Knepley 
2911df5d5c5SMatthew G. Knepley         ierr = DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2921df5d5c5SMatthew G. Knepley       }
2931df5d5c5SMatthew G. Knepley       break;
2941df5d5c5SMatthew G. Knepley     default:
29598921bdaSJacob Faibussowitsch       SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %D", dim);
2961df5d5c5SMatthew G. Knepley     }
2971df5d5c5SMatthew G. Knepley   }
2981df5d5c5SMatthew G. Knepley   *newdm = dm;
2991df5d5c5SMatthew G. Knepley   if (refinementLimit > 0.0) {
3001df5d5c5SMatthew G. Knepley     DM rdm;
3011df5d5c5SMatthew G. Knepley     const char *name;
3021df5d5c5SMatthew G. Knepley 
3031df5d5c5SMatthew G. Knepley     ierr = DMPlexSetRefinementUniform(*newdm, PETSC_FALSE);CHKERRQ(ierr);
3041df5d5c5SMatthew G. Knepley     ierr = DMPlexSetRefinementLimit(*newdm, refinementLimit);CHKERRQ(ierr);
3051df5d5c5SMatthew G. Knepley     ierr = DMRefine(*newdm, comm, &rdm);CHKERRQ(ierr);
3061df5d5c5SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) *newdm, &name);CHKERRQ(ierr);
3071df5d5c5SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject)    rdm,  name);CHKERRQ(ierr);
3081df5d5c5SMatthew G. Knepley     ierr = DMDestroy(newdm);CHKERRQ(ierr);
3091df5d5c5SMatthew G. Knepley     *newdm = rdm;
3101df5d5c5SMatthew G. Knepley   }
3111df5d5c5SMatthew G. Knepley   if (interpolate) {
3125fd9971aSMatthew G. Knepley     DM idm;
3131df5d5c5SMatthew G. Knepley 
3141df5d5c5SMatthew G. Knepley     ierr = DMPlexInterpolate(*newdm, &idm);CHKERRQ(ierr);
3151df5d5c5SMatthew G. Knepley     ierr = DMDestroy(newdm);CHKERRQ(ierr);
3161df5d5c5SMatthew G. Knepley     *newdm = idm;
3171df5d5c5SMatthew G. Knepley   }
3181df5d5c5SMatthew G. Knepley   PetscFunctionReturn(0);
3191df5d5c5SMatthew G. Knepley }
3201df5d5c5SMatthew G. Knepley 
3219318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
3229318fe57SMatthew G. Knepley {
3239318fe57SMatthew G. Knepley   const PetscInt numVertices    = 2;
3249318fe57SMatthew G. Knepley   PetscInt       markerRight    = 1;
3259318fe57SMatthew G. Knepley   PetscInt       markerLeft     = 1;
3269318fe57SMatthew G. Knepley   PetscBool      markerSeparate = PETSC_FALSE;
3279318fe57SMatthew G. Knepley   Vec            coordinates;
3289318fe57SMatthew G. Knepley   PetscSection   coordSection;
3299318fe57SMatthew G. Knepley   PetscScalar   *coords;
3309318fe57SMatthew G. Knepley   PetscInt       coordSize;
3319318fe57SMatthew G. Knepley   PetscMPIInt    rank;
3329318fe57SMatthew G. Knepley   PetscInt       cdim = 1, v;
3339318fe57SMatthew G. Knepley   PetscErrorCode ierr;
334552f7358SJed Brown 
3359318fe57SMatthew G. Knepley   PetscFunctionBegin;
3369318fe57SMatthew G. Knepley   ierr = PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL);CHKERRQ(ierr);
3379318fe57SMatthew G. Knepley   if (markerSeparate) {
3389318fe57SMatthew G. Knepley     markerRight  = 2;
3399318fe57SMatthew G. Knepley     markerLeft   = 1;
3409318fe57SMatthew G. Knepley   }
3419318fe57SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRMPI(ierr);
3429318fe57SMatthew G. Knepley   if (!rank) {
3439318fe57SMatthew G. Knepley     ierr = DMPlexSetChart(dm, 0, numVertices);CHKERRQ(ierr);
3449318fe57SMatthew G. Knepley     ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
3459318fe57SMatthew G. Knepley     ierr = DMSetLabelValue(dm, "marker", 0, markerLeft);CHKERRQ(ierr);
3469318fe57SMatthew G. Knepley     ierr = DMSetLabelValue(dm, "marker", 1, markerRight);CHKERRQ(ierr);
3479318fe57SMatthew G. Knepley   }
3489318fe57SMatthew G. Knepley   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
3499318fe57SMatthew G. Knepley   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
3509318fe57SMatthew G. Knepley   /* Build coordinates */
3519318fe57SMatthew G. Knepley   ierr = DMSetCoordinateDim(dm, cdim);CHKERRQ(ierr);
3529318fe57SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
3539318fe57SMatthew G. Knepley   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
3549318fe57SMatthew G. Knepley   ierr = PetscSectionSetChart(coordSection, 0, numVertices);CHKERRQ(ierr);
3559318fe57SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(coordSection, 0, cdim);CHKERRQ(ierr);
3569318fe57SMatthew G. Knepley   for (v = 0; v < numVertices; ++v) {
3579318fe57SMatthew G. Knepley     ierr = PetscSectionSetDof(coordSection, v, cdim);CHKERRQ(ierr);
3589318fe57SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(coordSection, v, 0, cdim);CHKERRQ(ierr);
3599318fe57SMatthew G. Knepley   }
3609318fe57SMatthew G. Knepley   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
3619318fe57SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
3629318fe57SMatthew G. Knepley   ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
3639318fe57SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
3649318fe57SMatthew G. Knepley   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
3659318fe57SMatthew G. Knepley   ierr = VecSetBlockSize(coordinates, cdim);CHKERRQ(ierr);
3669318fe57SMatthew G. Knepley   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
3679318fe57SMatthew G. Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
3689318fe57SMatthew G. Knepley   coords[0] = lower[0];
3699318fe57SMatthew G. Knepley   coords[1] = upper[0];
3709318fe57SMatthew G. Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
3719318fe57SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
3729318fe57SMatthew G. Knepley   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
3739318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
3749318fe57SMatthew G. Knepley }
37526492d91SMatthew G. Knepley 
3769318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
377552f7358SJed Brown {
3781df21d24SMatthew G. Knepley   const PetscInt numVertices    = (edges[0]+1)*(edges[1]+1);
3791df21d24SMatthew G. Knepley   const PetscInt numEdges       = edges[0]*(edges[1]+1) + (edges[0]+1)*edges[1];
380552f7358SJed Brown   PetscInt       markerTop      = 1;
381552f7358SJed Brown   PetscInt       markerBottom   = 1;
382552f7358SJed Brown   PetscInt       markerRight    = 1;
383552f7358SJed Brown   PetscInt       markerLeft     = 1;
384552f7358SJed Brown   PetscBool      markerSeparate = PETSC_FALSE;
385552f7358SJed Brown   Vec            coordinates;
386552f7358SJed Brown   PetscSection   coordSection;
387552f7358SJed Brown   PetscScalar    *coords;
388552f7358SJed Brown   PetscInt       coordSize;
389552f7358SJed Brown   PetscMPIInt    rank;
390552f7358SJed Brown   PetscInt       v, vx, vy;
391552f7358SJed Brown   PetscErrorCode ierr;
392552f7358SJed Brown 
393552f7358SJed Brown   PetscFunctionBegin;
394c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL);CHKERRQ(ierr);
395552f7358SJed Brown   if (markerSeparate) {
3961df21d24SMatthew G. Knepley     markerTop    = 3;
3971df21d24SMatthew G. Knepley     markerBottom = 1;
3981df21d24SMatthew G. Knepley     markerRight  = 2;
3991df21d24SMatthew G. Knepley     markerLeft   = 4;
400552f7358SJed Brown   }
401ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRMPI(ierr);
402dd400576SPatrick Sanan   if (rank == 0) {
403552f7358SJed Brown     PetscInt e, ex, ey;
404552f7358SJed Brown 
405552f7358SJed Brown     ierr = DMPlexSetChart(dm, 0, numEdges+numVertices);CHKERRQ(ierr);
406552f7358SJed Brown     for (e = 0; e < numEdges; ++e) {
407552f7358SJed Brown       ierr = DMPlexSetConeSize(dm, e, 2);CHKERRQ(ierr);
408552f7358SJed Brown     }
409552f7358SJed Brown     ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
410552f7358SJed Brown     for (vx = 0; vx <= edges[0]; vx++) {
411552f7358SJed Brown       for (ey = 0; ey < edges[1]; ey++) {
412552f7358SJed Brown         PetscInt edge   = vx*edges[1] + ey + edges[0]*(edges[1]+1);
413552f7358SJed Brown         PetscInt vertex = ey*(edges[0]+1) + vx + numEdges;
414da80777bSKarl Rupp         PetscInt cone[2];
415552f7358SJed Brown 
416da80777bSKarl Rupp         cone[0] = vertex; cone[1] = vertex+edges[0]+1;
417552f7358SJed Brown         ierr    = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr);
418552f7358SJed Brown         if (vx == edges[0]) {
419c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", edge,    markerRight);CHKERRQ(ierr);
420c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", cone[0], markerRight);CHKERRQ(ierr);
421552f7358SJed Brown           if (ey == edges[1]-1) {
422c58f1c22SToby Isaac             ierr = DMSetLabelValue(dm, "marker", cone[1], markerRight);CHKERRQ(ierr);
423c668006fSMatthew G. Knepley             ierr = DMSetLabelValue(dm, "Face Sets", cone[1], markerRight);CHKERRQ(ierr);
424552f7358SJed Brown           }
425552f7358SJed Brown         } else if (vx == 0) {
426c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", edge,    markerLeft);CHKERRQ(ierr);
427c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", cone[0], markerLeft);CHKERRQ(ierr);
428552f7358SJed Brown           if (ey == edges[1]-1) {
429c58f1c22SToby Isaac             ierr = DMSetLabelValue(dm, "marker", cone[1], markerLeft);CHKERRQ(ierr);
430c668006fSMatthew G. Knepley             ierr = DMSetLabelValue(dm, "Face Sets", cone[1], markerLeft);CHKERRQ(ierr);
431552f7358SJed Brown           }
432552f7358SJed Brown         }
433552f7358SJed Brown       }
434552f7358SJed Brown     }
435552f7358SJed Brown     for (vy = 0; vy <= edges[1]; vy++) {
436552f7358SJed Brown       for (ex = 0; ex < edges[0]; ex++) {
437552f7358SJed Brown         PetscInt edge   = vy*edges[0]     + ex;
438552f7358SJed Brown         PetscInt vertex = vy*(edges[0]+1) + ex + numEdges;
439da80777bSKarl Rupp         PetscInt cone[2];
440552f7358SJed Brown 
441da80777bSKarl Rupp         cone[0] = vertex; cone[1] = vertex+1;
442552f7358SJed Brown         ierr    = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr);
443552f7358SJed Brown         if (vy == edges[1]) {
444c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", edge,    markerTop);CHKERRQ(ierr);
445c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", cone[0], markerTop);CHKERRQ(ierr);
446552f7358SJed Brown           if (ex == edges[0]-1) {
447c58f1c22SToby Isaac             ierr = DMSetLabelValue(dm, "marker", cone[1], markerTop);CHKERRQ(ierr);
448c668006fSMatthew G. Knepley             ierr = DMSetLabelValue(dm, "Face Sets", cone[1], markerTop);CHKERRQ(ierr);
449552f7358SJed Brown           }
450552f7358SJed Brown         } else if (vy == 0) {
451c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", edge,    markerBottom);CHKERRQ(ierr);
452c58f1c22SToby Isaac           ierr = DMSetLabelValue(dm, "marker", cone[0], markerBottom);CHKERRQ(ierr);
453552f7358SJed Brown           if (ex == edges[0]-1) {
454c58f1c22SToby Isaac             ierr = DMSetLabelValue(dm, "marker", cone[1], markerBottom);CHKERRQ(ierr);
455c668006fSMatthew G. Knepley             ierr = DMSetLabelValue(dm, "Face Sets", cone[1], markerBottom);CHKERRQ(ierr);
456552f7358SJed Brown           }
457552f7358SJed Brown         }
458552f7358SJed Brown       }
459552f7358SJed Brown     }
460552f7358SJed Brown   }
461552f7358SJed Brown   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
462552f7358SJed Brown   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
463552f7358SJed Brown   /* Build coordinates */
4649596c6baSMatthew G. Knepley   ierr = DMSetCoordinateDim(dm, 2);CHKERRQ(ierr);
465c2166f76SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
466972bc18aSToby Isaac   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
467552f7358SJed Brown   ierr = PetscSectionSetChart(coordSection, numEdges, numEdges + numVertices);CHKERRQ(ierr);
468972bc18aSToby Isaac   ierr = PetscSectionSetFieldComponents(coordSection, 0, 2);CHKERRQ(ierr);
469552f7358SJed Brown   for (v = numEdges; v < numEdges+numVertices; ++v) {
470552f7358SJed Brown     ierr = PetscSectionSetDof(coordSection, v, 2);CHKERRQ(ierr);
471972bc18aSToby Isaac     ierr = PetscSectionSetFieldDof(coordSection, v, 0, 2);CHKERRQ(ierr);
472552f7358SJed Brown   }
473552f7358SJed Brown   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
474552f7358SJed Brown   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
4758b9ced59SLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
476da16285aSMichael Lange   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
477552f7358SJed Brown   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
4788b9ced59SLisandro Dalcin   ierr = VecSetBlockSize(coordinates, 2);CHKERRQ(ierr);
4792eb5907fSJed Brown   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
480552f7358SJed Brown   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
481552f7358SJed Brown   for (vy = 0; vy <= edges[1]; ++vy) {
482552f7358SJed Brown     for (vx = 0; vx <= edges[0]; ++vx) {
483552f7358SJed Brown       coords[(vy*(edges[0]+1)+vx)*2+0] = lower[0] + ((upper[0] - lower[0])/edges[0])*vx;
484552f7358SJed Brown       coords[(vy*(edges[0]+1)+vx)*2+1] = lower[1] + ((upper[1] - lower[1])/edges[1])*vy;
485552f7358SJed Brown     }
486552f7358SJed Brown   }
487552f7358SJed Brown   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
488552f7358SJed Brown   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
489552f7358SJed Brown   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
490552f7358SJed Brown   PetscFunctionReturn(0);
491552f7358SJed Brown }
492552f7358SJed Brown 
4939318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt faces[])
494552f7358SJed Brown {
4959e8abbc3SMichael Lange   PetscInt       vertices[3], numVertices;
4967b59f5a9SMichael Lange   PetscInt       numFaces    = 2*faces[0]*faces[1] + 2*faces[1]*faces[2] + 2*faces[0]*faces[2];
497552f7358SJed Brown   Vec            coordinates;
498552f7358SJed Brown   PetscSection   coordSection;
499552f7358SJed Brown   PetscScalar    *coords;
500552f7358SJed Brown   PetscInt       coordSize;
501552f7358SJed Brown   PetscMPIInt    rank;
502552f7358SJed Brown   PetscInt       v, vx, vy, vz;
5037b59f5a9SMichael Lange   PetscInt       voffset, iface=0, cone[4];
504552f7358SJed Brown   PetscErrorCode ierr;
505552f7358SJed Brown 
506552f7358SJed Brown   PetscFunctionBegin;
5072c71b3e2SJacob Faibussowitsch   PetscCheckFalse((faces[0] < 1) || (faces[1] < 1) || (faces[2] < 1),PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Must have at least 1 face per side");
508ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRMPI(ierr);
5099e8abbc3SMichael Lange   vertices[0] = faces[0]+1; vertices[1] = faces[1]+1; vertices[2] = faces[2]+1;
5109e8abbc3SMichael Lange   numVertices = vertices[0]*vertices[1]*vertices[2];
511dd400576SPatrick Sanan   if (rank == 0) {
512552f7358SJed Brown     PetscInt f;
513552f7358SJed Brown 
514552f7358SJed Brown     ierr = DMPlexSetChart(dm, 0, numFaces+numVertices);CHKERRQ(ierr);
515552f7358SJed Brown     for (f = 0; f < numFaces; ++f) {
516552f7358SJed Brown       ierr = DMPlexSetConeSize(dm, f, 4);CHKERRQ(ierr);
517552f7358SJed Brown     }
518552f7358SJed Brown     ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
5197b59f5a9SMichael Lange 
5207b59f5a9SMichael Lange     /* Side 0 (Top) */
5217b59f5a9SMichael Lange     for (vy = 0; vy < faces[1]; vy++) {
5227b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5237b59f5a9SMichael Lange         voffset = numFaces + vertices[0]*vertices[1]*(vertices[2]-1) + vy*vertices[0] + vx;
5247b59f5a9SMichael Lange         cone[0] = voffset; cone[1] = voffset+1; cone[2] = voffset+vertices[0]+1; cone[3] = voffset+vertices[0];
5257b59f5a9SMichael Lange         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
526c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", iface, 1);CHKERRQ(ierr);
527c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+0, 1);CHKERRQ(ierr);
528c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+1, 1);CHKERRQ(ierr);
529c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, 1);CHKERRQ(ierr);
530c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]+1, 1);CHKERRQ(ierr);
5317b59f5a9SMichael Lange         iface++;
532552f7358SJed Brown       }
533552f7358SJed Brown     }
5347b59f5a9SMichael Lange 
5357b59f5a9SMichael Lange     /* Side 1 (Bottom) */
5367b59f5a9SMichael Lange     for (vy = 0; vy < faces[1]; vy++) {
5377b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5387b59f5a9SMichael Lange         voffset = numFaces + vy*(faces[0]+1) + vx;
5397b59f5a9SMichael Lange         cone[0] = voffset+1; cone[1] = voffset; cone[2] = voffset+vertices[0]; cone[3] = voffset+vertices[0]+1;
5407b59f5a9SMichael Lange         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
541c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", iface, 1);CHKERRQ(ierr);
542c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+0, 1);CHKERRQ(ierr);
543c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+1, 1);CHKERRQ(ierr);
544c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, 1);CHKERRQ(ierr);
545c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]+1, 1);CHKERRQ(ierr);
5467b59f5a9SMichael Lange         iface++;
547552f7358SJed Brown       }
548552f7358SJed Brown     }
5497b59f5a9SMichael Lange 
5507b59f5a9SMichael Lange     /* Side 2 (Front) */
5517b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
5527b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5537b59f5a9SMichael Lange         voffset = numFaces + vz*vertices[0]*vertices[1] + vx;
5547b59f5a9SMichael Lange         cone[0] = voffset; cone[1] = voffset+1; cone[2] = voffset+vertices[0]*vertices[1]+1; cone[3] = voffset+vertices[0]*vertices[1];
5557b59f5a9SMichael Lange         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
556c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", iface, 1);CHKERRQ(ierr);
557c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+0, 1);CHKERRQ(ierr);
558c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+1, 1);CHKERRQ(ierr);
559c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+0, 1);CHKERRQ(ierr);
560c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+1, 1);CHKERRQ(ierr);
5617b59f5a9SMichael Lange         iface++;
562552f7358SJed Brown       }
5637b59f5a9SMichael Lange     }
5647b59f5a9SMichael Lange 
5657b59f5a9SMichael Lange     /* Side 3 (Back) */
5667b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
5677b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5687b59f5a9SMichael Lange         voffset = numFaces + vz*vertices[0]*vertices[1] + vertices[0]*(vertices[1]-1) + vx;
5697b59f5a9SMichael Lange         cone[0] = voffset+vertices[0]*vertices[1]; cone[1] = voffset+vertices[0]*vertices[1]+1;
5707b59f5a9SMichael Lange         cone[2] = voffset+1; cone[3] = voffset;
5717b59f5a9SMichael Lange         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
572c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", iface, 1);CHKERRQ(ierr);
573c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+0, 1);CHKERRQ(ierr);
574c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+1, 1);CHKERRQ(ierr);
575c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+0, 1);CHKERRQ(ierr);
576c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+1, 1);CHKERRQ(ierr);
5777b59f5a9SMichael Lange         iface++;
5787b59f5a9SMichael Lange       }
5797b59f5a9SMichael Lange     }
5807b59f5a9SMichael Lange 
5817b59f5a9SMichael Lange     /* Side 4 (Left) */
5827b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
5837b59f5a9SMichael Lange       for (vy = 0; vy < faces[1]; vy++) {
5847b59f5a9SMichael Lange         voffset = numFaces + vz*vertices[0]*vertices[1] + vy*vertices[0];
5857b59f5a9SMichael Lange         cone[0] = voffset; cone[1] = voffset+vertices[0]*vertices[1];
5867b59f5a9SMichael Lange         cone[2] = voffset+vertices[0]*vertices[1]+vertices[0]; cone[3] = voffset+vertices[0];
5877b59f5a9SMichael Lange         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
588c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", iface, 1);CHKERRQ(ierr);
589c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+0, 1);CHKERRQ(ierr);
590c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, 1);CHKERRQ(ierr);
591c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[1]+0, 1);CHKERRQ(ierr);
592c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+vertices[0], 1);CHKERRQ(ierr);
5937b59f5a9SMichael Lange         iface++;
5947b59f5a9SMichael Lange       }
5957b59f5a9SMichael Lange     }
5967b59f5a9SMichael Lange 
5977b59f5a9SMichael Lange     /* Side 5 (Right) */
5987b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
5997b59f5a9SMichael Lange       for (vy = 0; vy < faces[1]; vy++) {
600aab5bcd8SJed Brown         voffset = numFaces + vz*vertices[0]*vertices[1] + vy*vertices[0] + faces[0];
6017b59f5a9SMichael Lange         cone[0] = voffset+vertices[0]*vertices[1]; cone[1] = voffset;
6027b59f5a9SMichael Lange         cone[2] = voffset+vertices[0]; cone[3] = voffset+vertices[0]*vertices[1]+vertices[0];
6037b59f5a9SMichael Lange         ierr    = DMPlexSetCone(dm, iface, cone);CHKERRQ(ierr);
604c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", iface, 1);CHKERRQ(ierr);
605c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+0, 1);CHKERRQ(ierr);
606c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, 1);CHKERRQ(ierr);
607c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+0, 1);CHKERRQ(ierr);
608c9360375SMatthew G. Knepley         ierr    = DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+vertices[0], 1);CHKERRQ(ierr);
6097b59f5a9SMichael Lange         iface++;
6107b59f5a9SMichael Lange       }
611552f7358SJed Brown     }
612552f7358SJed Brown   }
613552f7358SJed Brown   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
614552f7358SJed Brown   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
615552f7358SJed Brown   /* Build coordinates */
6169596c6baSMatthew G. Knepley   ierr = DMSetCoordinateDim(dm, 3);CHKERRQ(ierr);
617c2166f76SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
6189318fe57SMatthew G. Knepley   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
619552f7358SJed Brown   ierr = PetscSectionSetChart(coordSection, numFaces, numFaces + numVertices);CHKERRQ(ierr);
6209318fe57SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(coordSection, 0, 3);CHKERRQ(ierr);
621552f7358SJed Brown   for (v = numFaces; v < numFaces+numVertices; ++v) {
622552f7358SJed Brown     ierr = PetscSectionSetDof(coordSection, v, 3);CHKERRQ(ierr);
6239318fe57SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(coordSection, v, 0, 3);CHKERRQ(ierr);
624552f7358SJed Brown   }
625552f7358SJed Brown   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
626552f7358SJed Brown   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
6278b9ced59SLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
628552f7358SJed Brown   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
629552f7358SJed Brown   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
6308b9ced59SLisandro Dalcin   ierr = VecSetBlockSize(coordinates, 3);CHKERRQ(ierr);
6312eb5907fSJed Brown   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
632552f7358SJed Brown   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
633552f7358SJed Brown   for (vz = 0; vz <= faces[2]; ++vz) {
634552f7358SJed Brown     for (vy = 0; vy <= faces[1]; ++vy) {
635552f7358SJed Brown       for (vx = 0; vx <= faces[0]; ++vx) {
636552f7358SJed Brown         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+0] = lower[0] + ((upper[0] - lower[0])/faces[0])*vx;
637552f7358SJed Brown         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+1] = lower[1] + ((upper[1] - lower[1])/faces[1])*vy;
638552f7358SJed Brown         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+2] = lower[2] + ((upper[2] - lower[2])/faces[2])*vz;
639552f7358SJed Brown       }
640552f7358SJed Brown     }
641552f7358SJed Brown   }
642552f7358SJed Brown   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
643552f7358SJed Brown   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
644552f7358SJed Brown   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
645552f7358SJed Brown   PetscFunctionReturn(0);
646552f7358SJed Brown }
647552f7358SJed Brown 
6489318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate)
6499318fe57SMatthew G. Knepley {
6509318fe57SMatthew G. Knepley   PetscErrorCode ierr;
6519318fe57SMatthew G. Knepley 
6529318fe57SMatthew G. Knepley   PetscFunctionBegin;
6539318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6549318fe57SMatthew G. Knepley   ierr = DMSetDimension(dm, dim-1);CHKERRQ(ierr);
6559318fe57SMatthew G. Knepley   ierr = DMSetCoordinateDim(dm, dim);CHKERRQ(ierr);
6569318fe57SMatthew G. Knepley   switch (dim) {
6579318fe57SMatthew G. Knepley     case 1: ierr = DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(dm, lower, upper, faces);CHKERRQ(ierr);break;
6589318fe57SMatthew G. Knepley     case 2: ierr = DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(dm, lower, upper, faces);CHKERRQ(ierr);break;
6599318fe57SMatthew G. Knepley     case 3: ierr = DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(dm, lower, upper, faces);CHKERRQ(ierr);break;
66098921bdaSJacob Faibussowitsch     default: SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Dimension not supported: %D", dim);
6619318fe57SMatthew G. Knepley   }
6629318fe57SMatthew G. Knepley   if (interpolate) {ierr = DMPlexInterpolateInPlace_Internal(dm);CHKERRQ(ierr);}
6639318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
6649318fe57SMatthew G. Knepley }
6659318fe57SMatthew G. Knepley 
6669318fe57SMatthew G. Knepley /*@C
6679318fe57SMatthew G. Knepley   DMPlexCreateBoxSurfaceMesh - Creates a mesh on the surface of the tensor product of unit intervals (box) using tensor cells (hexahedra).
6689318fe57SMatthew G. Knepley 
6699318fe57SMatthew G. Knepley   Collective
6709318fe57SMatthew G. Knepley 
6719318fe57SMatthew G. Knepley   Input Parameters:
6729318fe57SMatthew G. Knepley + comm        - The communicator for the DM object
6739318fe57SMatthew G. Knepley . dim         - The spatial dimension of the box, so the resulting mesh is has dimension dim-1
6749318fe57SMatthew 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
6759318fe57SMatthew G. Knepley . lower       - The lower left corner, or NULL for (0, 0, 0)
6769318fe57SMatthew G. Knepley . upper       - The upper right corner, or NULL for (1, 1, 1)
6779318fe57SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
6789318fe57SMatthew G. Knepley 
6799318fe57SMatthew G. Knepley   Output Parameter:
6809318fe57SMatthew G. Knepley . dm  - The DM object
6819318fe57SMatthew G. Knepley 
6829318fe57SMatthew G. Knepley   Level: beginner
6839318fe57SMatthew G. Knepley 
6849318fe57SMatthew G. Knepley .seealso: DMSetFromOptions(), DMPlexCreateBoxMesh(), DMPlexCreateFromFile(), DMSetType(), DMCreate()
6859318fe57SMatthew G. Knepley @*/
6869318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateBoxSurfaceMesh(MPI_Comm comm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate, DM *dm)
6879318fe57SMatthew G. Knepley {
6889318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
6899318fe57SMatthew G. Knepley   PetscReal      low[3] = {0, 0, 0};
6909318fe57SMatthew G. Knepley   PetscReal      upp[3] = {1, 1, 1};
6919318fe57SMatthew G. Knepley   PetscErrorCode ierr;
6929318fe57SMatthew G. Knepley 
6939318fe57SMatthew G. Knepley   PetscFunctionBegin;
6949318fe57SMatthew G. Knepley   ierr = DMCreate(comm,dm);CHKERRQ(ierr);
6959318fe57SMatthew G. Knepley   ierr = DMSetType(*dm,DMPLEX);CHKERRQ(ierr);
6969318fe57SMatthew G. Knepley   ierr = DMPlexCreateBoxSurfaceMesh_Internal(*dm, dim, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, interpolate);CHKERRQ(ierr);
6979318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
6989318fe57SMatthew G. Knepley }
6999318fe57SMatthew G. Knepley 
7009318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateLineMesh_Internal(DM dm,PetscInt segments,PetscReal lower,PetscReal upper,DMBoundaryType bd)
701fdbf62faSLisandro Dalcin {
702fdbf62faSLisandro Dalcin   PetscInt       i,fStart,fEnd,numCells = 0,numVerts = 0;
703fdbf62faSLisandro Dalcin   PetscInt       numPoints[2],*coneSize,*cones,*coneOrientations;
704fdbf62faSLisandro Dalcin   PetscScalar    *vertexCoords;
705fdbf62faSLisandro Dalcin   PetscReal      L,maxCell;
706fdbf62faSLisandro Dalcin   PetscBool      markerSeparate = PETSC_FALSE;
707fdbf62faSLisandro Dalcin   PetscInt       markerLeft  = 1, faceMarkerLeft  = 1;
708fdbf62faSLisandro Dalcin   PetscInt       markerRight = 1, faceMarkerRight = 2;
709fdbf62faSLisandro Dalcin   PetscBool      wrap = (bd == DM_BOUNDARY_PERIODIC || bd == DM_BOUNDARY_TWIST) ? PETSC_TRUE : PETSC_FALSE;
710fdbf62faSLisandro Dalcin   PetscMPIInt    rank;
711fdbf62faSLisandro Dalcin   PetscErrorCode ierr;
712fdbf62faSLisandro Dalcin 
713fdbf62faSLisandro Dalcin   PetscFunctionBegin;
7149318fe57SMatthew G. Knepley   PetscValidPointer(dm,1);
715fdbf62faSLisandro Dalcin 
7169318fe57SMatthew G. Knepley   ierr = DMSetDimension(dm,1);CHKERRQ(ierr);
7179318fe57SMatthew G. Knepley   ierr = DMCreateLabel(dm,"marker");CHKERRQ(ierr);
7189318fe57SMatthew G. Knepley   ierr = DMCreateLabel(dm,"Face Sets");CHKERRQ(ierr);
719fdbf62faSLisandro Dalcin 
7209318fe57SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm),&rank);CHKERRMPI(ierr);
721dd400576SPatrick Sanan   if (rank == 0) numCells = segments;
722dd400576SPatrick Sanan   if (rank == 0) numVerts = segments + (wrap ? 0 : 1);
723fdbf62faSLisandro Dalcin 
724fdbf62faSLisandro Dalcin   numPoints[0] = numVerts ; numPoints[1] = numCells;
725fdbf62faSLisandro Dalcin   ierr = PetscMalloc4(numCells+numVerts,&coneSize,numCells*2,&cones,numCells+numVerts,&coneOrientations,numVerts,&vertexCoords);CHKERRQ(ierr);
726580bdb30SBarry Smith   ierr = PetscArrayzero(coneOrientations,numCells+numVerts);CHKERRQ(ierr);
727fdbf62faSLisandro Dalcin   for (i = 0; i < numCells; ++i) { coneSize[i] = 2; }
728fdbf62faSLisandro Dalcin   for (i = 0; i < numVerts; ++i) { coneSize[numCells+i] = 0; }
729fdbf62faSLisandro Dalcin   for (i = 0; i < numCells; ++i) { cones[2*i] = numCells + i%numVerts; cones[2*i+1] = numCells + (i+1)%numVerts; }
730fdbf62faSLisandro Dalcin   for (i = 0; i < numVerts; ++i) { vertexCoords[i] = lower + (upper-lower)*((PetscReal)i/(PetscReal)numCells); }
7319318fe57SMatthew G. Knepley   ierr = DMPlexCreateFromDAG(dm,1,numPoints,coneSize,cones,coneOrientations,vertexCoords);CHKERRQ(ierr);
732fdbf62faSLisandro Dalcin   ierr = PetscFree4(coneSize,cones,coneOrientations,vertexCoords);CHKERRQ(ierr);
733fdbf62faSLisandro Dalcin 
7349318fe57SMatthew G. Knepley   ierr = PetscOptionsGetBool(((PetscObject)dm)->options,((PetscObject)dm)->prefix,"-dm_plex_separate_marker",&markerSeparate,NULL);CHKERRQ(ierr);
735fdbf62faSLisandro Dalcin   if (markerSeparate) { markerLeft = faceMarkerLeft; markerRight = faceMarkerRight;}
736dd400576SPatrick Sanan   if (!wrap && rank == 0) {
7379318fe57SMatthew G. Knepley     ierr = DMPlexGetHeightStratum(dm,1,&fStart,&fEnd);CHKERRQ(ierr);
7389318fe57SMatthew G. Knepley     ierr = DMSetLabelValue(dm,"marker",fStart,markerLeft);CHKERRQ(ierr);
7399318fe57SMatthew G. Knepley     ierr = DMSetLabelValue(dm,"marker",fEnd-1,markerRight);CHKERRQ(ierr);
7409318fe57SMatthew G. Knepley     ierr = DMSetLabelValue(dm,"Face Sets",fStart,faceMarkerLeft);CHKERRQ(ierr);
7419318fe57SMatthew G. Knepley     ierr = DMSetLabelValue(dm,"Face Sets",fEnd-1,faceMarkerRight);CHKERRQ(ierr);
742fdbf62faSLisandro Dalcin   }
743fdbf62faSLisandro Dalcin   if (wrap) {
744fdbf62faSLisandro Dalcin     L       = upper - lower;
745fdbf62faSLisandro Dalcin     maxCell = (PetscReal)1.1*(L/(PetscReal)PetscMax(1,segments));
7469318fe57SMatthew G. Knepley     ierr = DMSetPeriodicity(dm,PETSC_TRUE,&maxCell,&L,&bd);CHKERRQ(ierr);
747fdbf62faSLisandro Dalcin   }
7489318fe57SMatthew G. Knepley   ierr = DMPlexSetRefinementUniform(dm, PETSC_TRUE);CHKERRQ(ierr);
749fdbf62faSLisandro Dalcin   PetscFunctionReturn(0);
750fdbf62faSLisandro Dalcin }
751fdbf62faSLisandro Dalcin 
7529318fe57SMatthew 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)
753d6218766SMatthew G. Knepley {
7549318fe57SMatthew G. Knepley   DM             boundary, vol;
755768d5fceSMatthew G. Knepley   PetscInt       i;
756d6218766SMatthew G. Knepley   PetscErrorCode ierr;
757d6218766SMatthew G. Knepley 
758d6218766SMatthew G. Knepley   PetscFunctionBegin;
7599318fe57SMatthew G. Knepley   PetscValidPointer(dm, 1);
7602c71b3e2SJacob Faibussowitsch   for (i = 0; i < dim; ++i) PetscCheckFalse(periodicity[i] != DM_BOUNDARY_NONE,PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Periodicity is not supported for simplex meshes");
7619318fe57SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), &boundary);CHKERRQ(ierr);
762d6218766SMatthew G. Knepley   ierr = DMSetType(boundary, DMPLEX);CHKERRQ(ierr);
7639318fe57SMatthew G. Knepley   ierr = DMPlexCreateBoxSurfaceMesh_Internal(boundary, dim, faces, lower, upper, PETSC_FALSE);CHKERRQ(ierr);
7649318fe57SMatthew G. Knepley   ierr = DMPlexGenerate(boundary, NULL, interpolate, &vol);CHKERRQ(ierr);
765e600fa54SMatthew G. Knepley   ierr = DMPlexCopy_Internal(dm, PETSC_TRUE, vol);CHKERRQ(ierr);
7669318fe57SMatthew G. Knepley   ierr = DMPlexReplace_Static(dm, &vol);CHKERRQ(ierr);
767d6218766SMatthew G. Knepley   ierr = DMDestroy(&boundary);CHKERRQ(ierr);
768d6218766SMatthew G. Knepley   PetscFunctionReturn(0);
769d6218766SMatthew G. Knepley }
770d6218766SMatthew G. Knepley 
7713dfda0b1SToby Isaac static PetscErrorCode DMPlexCreateCubeMesh_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], DMBoundaryType bdX, DMBoundaryType bdY, DMBoundaryType bdZ)
7723dfda0b1SToby Isaac {
773ed0e4b50SMatthew G. Knepley   DMLabel        cutLabel = NULL;
774f4eb4c5dSMatthew G. Knepley   PetscInt       markerTop      = 1, faceMarkerTop      = 1;
775f4eb4c5dSMatthew G. Knepley   PetscInt       markerBottom   = 1, faceMarkerBottom   = 1;
776f4eb4c5dSMatthew G. Knepley   PetscInt       markerFront    = 1, faceMarkerFront    = 1;
777f4eb4c5dSMatthew G. Knepley   PetscInt       markerBack     = 1, faceMarkerBack     = 1;
778f4eb4c5dSMatthew G. Knepley   PetscInt       markerRight    = 1, faceMarkerRight    = 1;
779f4eb4c5dSMatthew G. Knepley   PetscInt       markerLeft     = 1, faceMarkerLeft     = 1;
7803dfda0b1SToby Isaac   PetscInt       dim;
781d8211ee3SMatthew G. Knepley   PetscBool      markerSeparate = PETSC_FALSE, cutMarker = PETSC_FALSE;
7823dfda0b1SToby Isaac   PetscMPIInt    rank;
7833dfda0b1SToby Isaac   PetscErrorCode ierr;
7843dfda0b1SToby Isaac 
7853dfda0b1SToby Isaac   PetscFunctionBegin;
786f0226e14SMatthew G. Knepley   ierr = DMGetDimension(dm,&dim);CHKERRQ(ierr);
787ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRMPI(ierr);
78850ae33c3SToby Isaac   ierr = DMCreateLabel(dm,"marker");CHKERRQ(ierr);
78950ae33c3SToby Isaac   ierr = DMCreateLabel(dm,"Face Sets");CHKERRQ(ierr);
7904c67ea77SStefano Zampini   ierr = PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL);CHKERRQ(ierr);
791d8211ee3SMatthew G. Knepley   if (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ||
792d8211ee3SMatthew G. Knepley       bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ||
793d8211ee3SMatthew G. Knepley       bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST) {
7944c67ea77SStefano Zampini 
795d1c88043SMatthew G. Knepley     if (cutMarker) {ierr = DMCreateLabel(dm, "periodic_cut");CHKERRQ(ierr); ierr = DMGetLabel(dm, "periodic_cut", &cutLabel);CHKERRQ(ierr);}
796d8211ee3SMatthew G. Knepley   }
7973dfda0b1SToby Isaac   switch (dim) {
7983dfda0b1SToby Isaac   case 2:
799f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 3;
800f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
801f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 2;
802f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 4;
8033dfda0b1SToby Isaac     break;
8043dfda0b1SToby Isaac   case 3:
805f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
806f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 2;
807f4eb4c5dSMatthew G. Knepley     faceMarkerFront  = 3;
808f4eb4c5dSMatthew G. Knepley     faceMarkerBack   = 4;
809f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 5;
810f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 6;
8113dfda0b1SToby Isaac     break;
8123dfda0b1SToby Isaac   default:
81398921bdaSJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Dimension %D not supported",dim);
8143dfda0b1SToby Isaac   }
815c5929fdfSBarry Smith   ierr = PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL);CHKERRQ(ierr);
816f4eb4c5dSMatthew G. Knepley   if (markerSeparate) {
817f4eb4c5dSMatthew G. Knepley     markerBottom = faceMarkerBottom;
818f4eb4c5dSMatthew G. Knepley     markerTop    = faceMarkerTop;
819f4eb4c5dSMatthew G. Knepley     markerFront  = faceMarkerFront;
820f4eb4c5dSMatthew G. Knepley     markerBack   = faceMarkerBack;
821f4eb4c5dSMatthew G. Knepley     markerRight  = faceMarkerRight;
822f4eb4c5dSMatthew G. Knepley     markerLeft   = faceMarkerLeft;
8233dfda0b1SToby Isaac   }
8243dfda0b1SToby Isaac   {
825dd400576SPatrick Sanan     const PetscInt numXEdges    = rank == 0 ? edges[0] : 0;
826dd400576SPatrick Sanan     const PetscInt numYEdges    = rank == 0 ? edges[1] : 0;
827dd400576SPatrick Sanan     const PetscInt numZEdges    = rank == 0 ? edges[2] : 0;
828dd400576SPatrick Sanan     const PetscInt numXVertices = rank == 0 ? (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ? edges[0] : edges[0]+1) : 0;
829dd400576SPatrick Sanan     const PetscInt numYVertices = rank == 0 ? (bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ? edges[1] : edges[1]+1) : 0;
830dd400576SPatrick Sanan     const PetscInt numZVertices = rank == 0 ? (bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST ? edges[2] : edges[2]+1) : 0;
8313dfda0b1SToby Isaac     const PetscInt numCells     = numXEdges*numYEdges*numZEdges;
8323dfda0b1SToby Isaac     const PetscInt numXFaces    = numYEdges*numZEdges;
8333dfda0b1SToby Isaac     const PetscInt numYFaces    = numXEdges*numZEdges;
8343dfda0b1SToby Isaac     const PetscInt numZFaces    = numXEdges*numYEdges;
8353dfda0b1SToby Isaac     const PetscInt numTotXFaces = numXVertices*numXFaces;
8363dfda0b1SToby Isaac     const PetscInt numTotYFaces = numYVertices*numYFaces;
8373dfda0b1SToby Isaac     const PetscInt numTotZFaces = numZVertices*numZFaces;
8383dfda0b1SToby Isaac     const PetscInt numFaces     = numTotXFaces + numTotYFaces + numTotZFaces;
8393dfda0b1SToby Isaac     const PetscInt numTotXEdges = numXEdges*numYVertices*numZVertices;
8403dfda0b1SToby Isaac     const PetscInt numTotYEdges = numYEdges*numXVertices*numZVertices;
8413dfda0b1SToby Isaac     const PetscInt numTotZEdges = numZEdges*numXVertices*numYVertices;
8423dfda0b1SToby Isaac     const PetscInt numVertices  = numXVertices*numYVertices*numZVertices;
8433dfda0b1SToby Isaac     const PetscInt numEdges     = numTotXEdges + numTotYEdges + numTotZEdges;
8443dfda0b1SToby Isaac     const PetscInt firstVertex  = (dim == 2) ? numFaces : numCells;
8453dfda0b1SToby Isaac     const PetscInt firstXFace   = (dim == 2) ? 0 : numCells + numVertices;
8463dfda0b1SToby Isaac     const PetscInt firstYFace   = firstXFace + numTotXFaces;
8473dfda0b1SToby Isaac     const PetscInt firstZFace   = firstYFace + numTotYFaces;
8483dfda0b1SToby Isaac     const PetscInt firstXEdge   = numCells + numFaces + numVertices;
8493dfda0b1SToby Isaac     const PetscInt firstYEdge   = firstXEdge + numTotXEdges;
8503dfda0b1SToby Isaac     const PetscInt firstZEdge   = firstYEdge + numTotYEdges;
8513dfda0b1SToby Isaac     Vec            coordinates;
8523dfda0b1SToby Isaac     PetscSection   coordSection;
8533dfda0b1SToby Isaac     PetscScalar   *coords;
8543dfda0b1SToby Isaac     PetscInt       coordSize;
8553dfda0b1SToby Isaac     PetscInt       v, vx, vy, vz;
8563dfda0b1SToby Isaac     PetscInt       c, f, fx, fy, fz, e, ex, ey, ez;
8573dfda0b1SToby Isaac 
8583dfda0b1SToby Isaac     ierr = DMPlexSetChart(dm, 0, numCells+numFaces+numEdges+numVertices);CHKERRQ(ierr);
8593dfda0b1SToby Isaac     for (c = 0; c < numCells; c++) {
8603dfda0b1SToby Isaac       ierr = DMPlexSetConeSize(dm, c, 6);CHKERRQ(ierr);
8613dfda0b1SToby Isaac     }
8623dfda0b1SToby Isaac     for (f = firstXFace; f < firstXFace+numFaces; ++f) {
8633dfda0b1SToby Isaac       ierr = DMPlexSetConeSize(dm, f, 4);CHKERRQ(ierr);
8643dfda0b1SToby Isaac     }
8653dfda0b1SToby Isaac     for (e = firstXEdge; e < firstXEdge+numEdges; ++e) {
8663dfda0b1SToby Isaac       ierr = DMPlexSetConeSize(dm, e, 2);CHKERRQ(ierr);
8673dfda0b1SToby Isaac     }
8683dfda0b1SToby Isaac     ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
8693dfda0b1SToby Isaac     /* Build cells */
8703dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
8713dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
8723dfda0b1SToby Isaac         for (fx = 0; fx < numXEdges; ++fx) {
8733dfda0b1SToby Isaac           PetscInt cell    = (fz*numYEdges + fy)*numXEdges + fx;
8743dfda0b1SToby Isaac           PetscInt faceB   = firstZFace + (fy*numXEdges+fx)*numZVertices +   fz;
8753dfda0b1SToby Isaac           PetscInt faceT   = firstZFace + (fy*numXEdges+fx)*numZVertices + ((fz+1)%numZVertices);
8763dfda0b1SToby Isaac           PetscInt faceF   = firstYFace + (fz*numXEdges+fx)*numYVertices +   fy;
8773dfda0b1SToby Isaac           PetscInt faceK   = firstYFace + (fz*numXEdges+fx)*numYVertices + ((fy+1)%numYVertices);
8783dfda0b1SToby Isaac           PetscInt faceL   = firstXFace + (fz*numYEdges+fy)*numXVertices +   fx;
8793dfda0b1SToby Isaac           PetscInt faceR   = firstXFace + (fz*numYEdges+fy)*numXVertices + ((fx+1)%numXVertices);
8803dfda0b1SToby Isaac                             /* B,  T,  F,  K,  R,  L */
881b5a892a1SMatthew G. Knepley           PetscInt ornt[6] = {-2,  0,  0, -3,  0, -2}; /* ??? */
88242206facSLisandro Dalcin           PetscInt cone[6];
8833dfda0b1SToby Isaac 
8843dfda0b1SToby Isaac           /* no boundary twisting in 3D */
8853dfda0b1SToby Isaac           cone[0] = faceB; cone[1] = faceT; cone[2] = faceF; cone[3] = faceK; cone[4] = faceR; cone[5] = faceL;
8863dfda0b1SToby Isaac           ierr    = DMPlexSetCone(dm, cell, cone);CHKERRQ(ierr);
8873dfda0b1SToby Isaac           ierr    = DMPlexSetConeOrientation(dm, cell, ornt);CHKERRQ(ierr);
8888a5b437dSMatthew G. Knepley           if (bdX != DM_BOUNDARY_NONE && fx == numXEdges-1 && cutLabel) {ierr = DMLabelSetValue(cutLabel, cell, 2);CHKERRQ(ierr);}
8898a5b437dSMatthew G. Knepley           if (bdY != DM_BOUNDARY_NONE && fy == numYEdges-1 && cutLabel) {ierr = DMLabelSetValue(cutLabel, cell, 2);CHKERRQ(ierr);}
8908a5b437dSMatthew G. Knepley           if (bdZ != DM_BOUNDARY_NONE && fz == numZEdges-1 && cutLabel) {ierr = DMLabelSetValue(cutLabel, cell, 2);CHKERRQ(ierr);}
8913dfda0b1SToby Isaac         }
8923dfda0b1SToby Isaac       }
8933dfda0b1SToby Isaac     }
8943dfda0b1SToby Isaac     /* Build x faces */
8953dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
8963dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
8973dfda0b1SToby Isaac         for (fx = 0; fx < numXVertices; ++fx) {
8983dfda0b1SToby Isaac           PetscInt face    = firstXFace + (fz*numYEdges+fy)     *numXVertices+fx;
8993dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy                   *numXVertices+fx)*numZEdges + fz;
9003dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (((fy+1)%numYVertices)*numXVertices+fx)*numZEdges + fz;
9013dfda0b1SToby Isaac           PetscInt edgeB   = firstYEdge + (fz                   *numXVertices+fx)*numYEdges + fy;
9023dfda0b1SToby Isaac           PetscInt edgeT   = firstYEdge + (((fz+1)%numZVertices)*numXVertices+fx)*numYEdges + fy;
903b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
9043dfda0b1SToby Isaac           PetscInt cone[4];
9053dfda0b1SToby Isaac 
9063dfda0b1SToby Isaac           if (dim == 3) {
9073dfda0b1SToby Isaac             /* markers */
9083dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
9093dfda0b1SToby Isaac               if (fx == numXVertices-1) {
910c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerRight);CHKERRQ(ierr);
911c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", face, markerRight);CHKERRQ(ierr);
9123dfda0b1SToby Isaac               }
9133dfda0b1SToby Isaac               else if (fx == 0) {
914c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerLeft);CHKERRQ(ierr);
915c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", face, markerLeft);CHKERRQ(ierr);
9163dfda0b1SToby Isaac               }
9173dfda0b1SToby Isaac             }
9183dfda0b1SToby Isaac           }
9193dfda0b1SToby Isaac           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
9203dfda0b1SToby Isaac           ierr    = DMPlexSetCone(dm, face, cone);CHKERRQ(ierr);
9213dfda0b1SToby Isaac           ierr    = DMPlexSetConeOrientation(dm, face, ornt);CHKERRQ(ierr);
9223dfda0b1SToby Isaac         }
9233dfda0b1SToby Isaac       }
9243dfda0b1SToby Isaac     }
9253dfda0b1SToby Isaac     /* Build y faces */
9263dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
92742206facSLisandro Dalcin       for (fx = 0; fx < numXEdges; ++fx) {
9283dfda0b1SToby Isaac         for (fy = 0; fy < numYVertices; ++fy) {
9293dfda0b1SToby Isaac           PetscInt face    = firstYFace + (fz*numXEdges+fx)*numYVertices + fy;
9303dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy*numXVertices+  fx)*numZEdges + fz;
9313dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (fy*numXVertices+((fx+1)%numXVertices))*numZEdges + fz;
9323dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz                   *numYVertices+fy)*numXEdges + fx;
9333dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (((fz+1)%numZVertices)*numYVertices+fy)*numXEdges + fx;
934b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
9353dfda0b1SToby Isaac           PetscInt cone[4];
9363dfda0b1SToby Isaac 
9373dfda0b1SToby Isaac           if (dim == 3) {
9383dfda0b1SToby Isaac             /* markers */
9393dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
9403dfda0b1SToby Isaac               if (fy == numYVertices-1) {
941c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerBack);CHKERRQ(ierr);
942c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", face, markerBack);CHKERRQ(ierr);
9433dfda0b1SToby Isaac               }
9443dfda0b1SToby Isaac               else if (fy == 0) {
945c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerFront);CHKERRQ(ierr);
946c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", face, markerFront);CHKERRQ(ierr);
9473dfda0b1SToby Isaac               }
9483dfda0b1SToby Isaac             }
9493dfda0b1SToby Isaac           }
9503dfda0b1SToby Isaac           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
9513dfda0b1SToby Isaac           ierr    = DMPlexSetCone(dm, face, cone);CHKERRQ(ierr);
9523dfda0b1SToby Isaac           ierr    = DMPlexSetConeOrientation(dm, face, ornt);CHKERRQ(ierr);
9533dfda0b1SToby Isaac         }
9543dfda0b1SToby Isaac       }
9553dfda0b1SToby Isaac     }
9563dfda0b1SToby Isaac     /* Build z faces */
9573dfda0b1SToby Isaac     for (fy = 0; fy < numYEdges; ++fy) {
9583dfda0b1SToby Isaac       for (fx = 0; fx < numXEdges; ++fx) {
9593dfda0b1SToby Isaac         for (fz = 0; fz < numZVertices; fz++) {
9603dfda0b1SToby Isaac           PetscInt face    = firstZFace + (fy*numXEdges+fx)*numZVertices + fz;
9613dfda0b1SToby Isaac           PetscInt edgeL   = firstYEdge + (fz*numXVertices+  fx)*numYEdges + fy;
9623dfda0b1SToby Isaac           PetscInt edgeR   = firstYEdge + (fz*numXVertices+((fx+1)%numXVertices))*numYEdges + fy;
9633dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz*numYVertices+  fy)*numXEdges + fx;
9643dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (fz*numYVertices+((fy+1)%numYVertices))*numXEdges + fx;
965b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
9663dfda0b1SToby Isaac           PetscInt cone[4];
9673dfda0b1SToby Isaac 
9683dfda0b1SToby Isaac           if (dim == 2) {
969b5a892a1SMatthew G. Knepley             if (bdX == DM_BOUNDARY_TWIST && fx == numXEdges-1) {edgeR += numYEdges-1-2*fy; ornt[1] = -1;}
9703dfda0b1SToby Isaac             if (bdY == DM_BOUNDARY_TWIST && fy == numYEdges-1) {edgeT += numXEdges-1-2*fx; ornt[2] =  0;}
9718a5b437dSMatthew G. Knepley             if (bdX != DM_BOUNDARY_NONE && fx == numXEdges-1 && cutLabel) {ierr = DMLabelSetValue(cutLabel, face, 2);CHKERRQ(ierr);}
9728a5b437dSMatthew G. Knepley             if (bdY != DM_BOUNDARY_NONE && fy == numYEdges-1 && cutLabel) {ierr = DMLabelSetValue(cutLabel, face, 2);CHKERRQ(ierr);}
973d1c88043SMatthew G. Knepley           } else {
9743dfda0b1SToby Isaac             /* markers */
9753dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
9763dfda0b1SToby Isaac               if (fz == numZVertices-1) {
977c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerTop);CHKERRQ(ierr);
978c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", face, markerTop);CHKERRQ(ierr);
9793dfda0b1SToby Isaac               }
9803dfda0b1SToby Isaac               else if (fz == 0) {
981c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", face, faceMarkerBottom);CHKERRQ(ierr);
982c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", face, markerBottom);CHKERRQ(ierr);
9833dfda0b1SToby Isaac               }
9843dfda0b1SToby Isaac             }
9853dfda0b1SToby Isaac           }
9863dfda0b1SToby Isaac           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
9873dfda0b1SToby Isaac           ierr    = DMPlexSetCone(dm, face, cone);CHKERRQ(ierr);
9883dfda0b1SToby Isaac           ierr    = DMPlexSetConeOrientation(dm, face, ornt);CHKERRQ(ierr);
9893dfda0b1SToby Isaac         }
9903dfda0b1SToby Isaac       }
9913dfda0b1SToby Isaac     }
9923dfda0b1SToby Isaac     /* Build Z edges*/
9933dfda0b1SToby Isaac     for (vy = 0; vy < numYVertices; vy++) {
9943dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
9953dfda0b1SToby Isaac         for (ez = 0; ez < numZEdges; ez++) {
9963dfda0b1SToby Isaac           const PetscInt edge    = firstZEdge  + (vy*numXVertices+vx)*numZEdges + ez;
9973dfda0b1SToby Isaac           const PetscInt vertexB = firstVertex + (ez                   *numYVertices+vy)*numXVertices + vx;
9983dfda0b1SToby Isaac           const PetscInt vertexT = firstVertex + (((ez+1)%numZVertices)*numYVertices+vy)*numXVertices + vx;
9993dfda0b1SToby Isaac           PetscInt       cone[2];
10003dfda0b1SToby Isaac 
10013dfda0b1SToby Isaac           if (dim == 3) {
10023dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
10033dfda0b1SToby Isaac               if (vx == numXVertices-1) {
1004c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerRight);CHKERRQ(ierr);
10053dfda0b1SToby Isaac               }
10063dfda0b1SToby Isaac               else if (vx == 0) {
1007c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerLeft);CHKERRQ(ierr);
10083dfda0b1SToby Isaac               }
10093dfda0b1SToby Isaac             }
10103dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
10113dfda0b1SToby Isaac               if (vy == numYVertices-1) {
1012c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerBack);CHKERRQ(ierr);
10133dfda0b1SToby Isaac               }
10143dfda0b1SToby Isaac               else if (vy == 0) {
1015c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerFront);CHKERRQ(ierr);
10163dfda0b1SToby Isaac               }
10173dfda0b1SToby Isaac             }
10183dfda0b1SToby Isaac           }
10193dfda0b1SToby Isaac           cone[0] = vertexB; cone[1] = vertexT;
10203dfda0b1SToby Isaac           ierr = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr);
10213dfda0b1SToby Isaac         }
10223dfda0b1SToby Isaac       }
10233dfda0b1SToby Isaac     }
10243dfda0b1SToby Isaac     /* Build Y edges*/
10253dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
10263dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
10273dfda0b1SToby Isaac         for (ey = 0; ey < numYEdges; ey++) {
10283dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdY == DM_BOUNDARY_TWIST && ey == numYEdges-1) ? (numXVertices-vx-1) : (vz*numYVertices+((ey+1)%numYVertices))*numXVertices + vx;
10293dfda0b1SToby Isaac           const PetscInt edge    = firstYEdge  + (vz*numXVertices+vx)*numYEdges + ey;
10303dfda0b1SToby Isaac           const PetscInt vertexF = firstVertex + (vz*numYVertices+ey)*numXVertices + vx;
10313dfda0b1SToby Isaac           const PetscInt vertexK = firstVertex + nextv;
10323dfda0b1SToby Isaac           PetscInt       cone[2];
10333dfda0b1SToby Isaac 
10343dfda0b1SToby Isaac           cone[0] = vertexF; cone[1] = vertexK;
10353dfda0b1SToby Isaac           ierr = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr);
10363dfda0b1SToby Isaac           if (dim == 2) {
10373dfda0b1SToby Isaac             if ((bdX != DM_BOUNDARY_PERIODIC) && (bdX != DM_BOUNDARY_TWIST)) {
10383dfda0b1SToby Isaac               if (vx == numXVertices-1) {
1039c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", edge, faceMarkerRight);CHKERRQ(ierr);
1040c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge,    markerRight);CHKERRQ(ierr);
1041c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", cone[0], markerRight);CHKERRQ(ierr);
10423dfda0b1SToby Isaac                 if (ey == numYEdges-1) {
1043c58f1c22SToby Isaac                   ierr = DMSetLabelValue(dm, "marker", cone[1], markerRight);CHKERRQ(ierr);
10443dfda0b1SToby Isaac                 }
1045d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
1046c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", edge, faceMarkerLeft);CHKERRQ(ierr);
1047c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge,    markerLeft);CHKERRQ(ierr);
1048c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", cone[0], markerLeft);CHKERRQ(ierr);
10493dfda0b1SToby Isaac                 if (ey == numYEdges-1) {
1050c58f1c22SToby Isaac                   ierr = DMSetLabelValue(dm, "marker", cone[1], markerLeft);CHKERRQ(ierr);
10513dfda0b1SToby Isaac                 }
10523dfda0b1SToby Isaac               }
1053d8211ee3SMatthew G. Knepley             } else {
10544c67ea77SStefano Zampini               if (vx == 0 && cutLabel) {
1055d1c88043SMatthew G. Knepley                 ierr = DMLabelSetValue(cutLabel, edge,    1);CHKERRQ(ierr);
1056d1c88043SMatthew G. Knepley                 ierr = DMLabelSetValue(cutLabel, cone[0], 1);CHKERRQ(ierr);
1057d8211ee3SMatthew G. Knepley                 if (ey == numYEdges-1) {
1058d1c88043SMatthew G. Knepley                   ierr = DMLabelSetValue(cutLabel, cone[1], 1);CHKERRQ(ierr);
10593dfda0b1SToby Isaac                 }
10603dfda0b1SToby Isaac               }
1061d8211ee3SMatthew G. Knepley             }
1062d8211ee3SMatthew G. Knepley           } else {
10633dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
10643dfda0b1SToby Isaac               if (vx == numXVertices-1) {
1065c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerRight);CHKERRQ(ierr);
1066d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
1067c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerLeft);CHKERRQ(ierr);
10683dfda0b1SToby Isaac               }
10693dfda0b1SToby Isaac             }
10703dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
10713dfda0b1SToby Isaac               if (vz == numZVertices-1) {
1072c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerTop);CHKERRQ(ierr);
1073d8211ee3SMatthew G. Knepley               } else if (vz == 0) {
1074c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerBottom);CHKERRQ(ierr);
10753dfda0b1SToby Isaac               }
10763dfda0b1SToby Isaac             }
10773dfda0b1SToby Isaac           }
10783dfda0b1SToby Isaac         }
10793dfda0b1SToby Isaac       }
10803dfda0b1SToby Isaac     }
10813dfda0b1SToby Isaac     /* Build X edges*/
10823dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
10833dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; vy++) {
10843dfda0b1SToby Isaac         for (ex = 0; ex < numXEdges; ex++) {
10853dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdX == DM_BOUNDARY_TWIST && ex == numXEdges-1) ? (numYVertices-vy-1)*numXVertices : (vz*numYVertices+vy)*numXVertices + (ex+1)%numXVertices;
10863dfda0b1SToby Isaac           const PetscInt edge    = firstXEdge  + (vz*numYVertices+vy)*numXEdges + ex;
10873dfda0b1SToby Isaac           const PetscInt vertexL = firstVertex + (vz*numYVertices+vy)*numXVertices + ex;
10883dfda0b1SToby Isaac           const PetscInt vertexR = firstVertex + nextv;
10893dfda0b1SToby Isaac           PetscInt       cone[2];
10903dfda0b1SToby Isaac 
10913dfda0b1SToby Isaac           cone[0] = vertexL; cone[1] = vertexR;
10923dfda0b1SToby Isaac           ierr = DMPlexSetCone(dm, edge, cone);CHKERRQ(ierr);
10933dfda0b1SToby Isaac           if (dim == 2) {
10943dfda0b1SToby Isaac             if ((bdY != DM_BOUNDARY_PERIODIC) && (bdY != DM_BOUNDARY_TWIST)) {
10953dfda0b1SToby Isaac               if (vy == numYVertices-1) {
1096c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", edge, faceMarkerTop);CHKERRQ(ierr);
1097c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge,    markerTop);CHKERRQ(ierr);
1098c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", cone[0], markerTop);CHKERRQ(ierr);
10993dfda0b1SToby Isaac                 if (ex == numXEdges-1) {
1100c58f1c22SToby Isaac                   ierr = DMSetLabelValue(dm, "marker", cone[1], markerTop);CHKERRQ(ierr);
11013dfda0b1SToby Isaac                 }
1102d8211ee3SMatthew G. Knepley               } else if (vy == 0) {
1103c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "Face Sets", edge, faceMarkerBottom);CHKERRQ(ierr);
1104c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge,    markerBottom);CHKERRQ(ierr);
1105c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", cone[0], markerBottom);CHKERRQ(ierr);
11063dfda0b1SToby Isaac                 if (ex == numXEdges-1) {
1107c58f1c22SToby Isaac                   ierr = DMSetLabelValue(dm, "marker", cone[1], markerBottom);CHKERRQ(ierr);
11083dfda0b1SToby Isaac                 }
11093dfda0b1SToby Isaac               }
1110d8211ee3SMatthew G. Knepley             } else {
11114c67ea77SStefano Zampini               if (vy == 0 && cutLabel) {
1112d1c88043SMatthew G. Knepley                 ierr = DMLabelSetValue(cutLabel, edge,    1);CHKERRQ(ierr);
1113d1c88043SMatthew G. Knepley                 ierr = DMLabelSetValue(cutLabel, cone[0], 1);CHKERRQ(ierr);
1114d8211ee3SMatthew G. Knepley                 if (ex == numXEdges-1) {
1115d1c88043SMatthew G. Knepley                   ierr = DMLabelSetValue(cutLabel, cone[1], 1);CHKERRQ(ierr);
11163dfda0b1SToby Isaac                 }
11173dfda0b1SToby Isaac               }
1118d8211ee3SMatthew G. Knepley             }
1119d8211ee3SMatthew G. Knepley           } else {
11203dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
11213dfda0b1SToby Isaac               if (vy == numYVertices-1) {
1122c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerBack);CHKERRQ(ierr);
11233dfda0b1SToby Isaac               }
11243dfda0b1SToby Isaac               else if (vy == 0) {
1125c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerFront);CHKERRQ(ierr);
11263dfda0b1SToby Isaac               }
11273dfda0b1SToby Isaac             }
11283dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
11293dfda0b1SToby Isaac               if (vz == numZVertices-1) {
1130c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerTop);CHKERRQ(ierr);
11313dfda0b1SToby Isaac               }
11323dfda0b1SToby Isaac               else if (vz == 0) {
1133c58f1c22SToby Isaac                 ierr = DMSetLabelValue(dm, "marker", edge, markerBottom);CHKERRQ(ierr);
11343dfda0b1SToby Isaac               }
11353dfda0b1SToby Isaac             }
11363dfda0b1SToby Isaac           }
11373dfda0b1SToby Isaac         }
11383dfda0b1SToby Isaac       }
11393dfda0b1SToby Isaac     }
11403dfda0b1SToby Isaac     ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
11413dfda0b1SToby Isaac     ierr = DMPlexStratify(dm);CHKERRQ(ierr);
11423dfda0b1SToby Isaac     /* Build coordinates */
11433dfda0b1SToby Isaac     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
11443dfda0b1SToby Isaac     ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
11453dfda0b1SToby Isaac     ierr = PetscSectionSetFieldComponents(coordSection, 0, dim);CHKERRQ(ierr);
11463dfda0b1SToby Isaac     ierr = PetscSectionSetChart(coordSection, firstVertex, firstVertex+numVertices);CHKERRQ(ierr);
11473dfda0b1SToby Isaac     for (v = firstVertex; v < firstVertex+numVertices; ++v) {
11483dfda0b1SToby Isaac       ierr = PetscSectionSetDof(coordSection, v, dim);CHKERRQ(ierr);
11493dfda0b1SToby Isaac       ierr = PetscSectionSetFieldDof(coordSection, v, 0, dim);CHKERRQ(ierr);
11503dfda0b1SToby Isaac     }
11513dfda0b1SToby Isaac     ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
11523dfda0b1SToby Isaac     ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
11538b9ced59SLisandro Dalcin     ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
1154da16285aSMichael Lange     ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
11553dfda0b1SToby Isaac     ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
11568b9ced59SLisandro Dalcin     ierr = VecSetBlockSize(coordinates, dim);CHKERRQ(ierr);
11573dfda0b1SToby Isaac     ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
11583dfda0b1SToby Isaac     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
11593dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; ++vz) {
11603dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; ++vy) {
11613dfda0b1SToby Isaac         for (vx = 0; vx < numXVertices; ++vx) {
11623dfda0b1SToby Isaac           coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+0] = lower[0] + ((upper[0] - lower[0])/numXEdges)*vx;
11633dfda0b1SToby Isaac           coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+1] = lower[1] + ((upper[1] - lower[1])/numYEdges)*vy;
11643dfda0b1SToby Isaac           if (dim == 3) {
11653dfda0b1SToby Isaac             coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+2] = lower[2] + ((upper[2] - lower[2])/numZEdges)*vz;
11663dfda0b1SToby Isaac           }
11673dfda0b1SToby Isaac         }
11683dfda0b1SToby Isaac       }
11693dfda0b1SToby Isaac     }
11703dfda0b1SToby Isaac     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
11713dfda0b1SToby Isaac     ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
11723dfda0b1SToby Isaac     ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
11733dfda0b1SToby Isaac   }
11743dfda0b1SToby Isaac   PetscFunctionReturn(0);
11753dfda0b1SToby Isaac }
11763dfda0b1SToby Isaac 
11779318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxMesh_Tensor_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
1178a6dfd86eSKarl Rupp {
11799318fe57SMatthew G. Knepley   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
11809318fe57SMatthew G. Knepley   PetscInt       fac[3] = {0, 0, 0}, d;
1181552f7358SJed Brown   PetscErrorCode ierr;
1182552f7358SJed Brown 
1183552f7358SJed Brown   PetscFunctionBegin;
11849318fe57SMatthew G. Knepley   PetscValidPointer(dm, 1);
11859318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
11869318fe57SMatthew G. Knepley   ierr = DMSetDimension(dm, dim);CHKERRQ(ierr);
11879318fe57SMatthew G. Knepley   for (d = 0; d < dim; ++d) {fac[d] = faces[d]; bdt[d] = periodicity[d];}
11889318fe57SMatthew G. Knepley   ierr = DMPlexCreateCubeMesh_Internal(dm, lower, upper, fac, bdt[0], bdt[1], bdt[2]);CHKERRQ(ierr);
1189768d5fceSMatthew G. Knepley   if (periodicity[0] == DM_BOUNDARY_PERIODIC || periodicity[0] == DM_BOUNDARY_TWIST ||
1190768d5fceSMatthew G. Knepley       periodicity[1] == DM_BOUNDARY_PERIODIC || periodicity[1] == DM_BOUNDARY_TWIST ||
1191768d5fceSMatthew G. Knepley       (dim > 2 && (periodicity[2] == DM_BOUNDARY_PERIODIC || periodicity[2] == DM_BOUNDARY_TWIST))) {
1192768d5fceSMatthew G. Knepley     PetscReal L[3];
1193768d5fceSMatthew G. Knepley     PetscReal maxCell[3];
1194552f7358SJed Brown 
11959318fe57SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
11969318fe57SMatthew G. Knepley       L[d]       = upper[d] - lower[d];
11979318fe57SMatthew G. Knepley       maxCell[d] = 1.1 * (L[d] / PetscMax(1, faces[d]));
1198768d5fceSMatthew G. Knepley     }
11999318fe57SMatthew G. Knepley     ierr = DMSetPeriodicity(dm, PETSC_TRUE, maxCell, L, periodicity);CHKERRQ(ierr);
1200768d5fceSMatthew G. Knepley   }
12019318fe57SMatthew G. Knepley   ierr = DMPlexSetRefinementUniform(dm, PETSC_TRUE);CHKERRQ(ierr);
12029318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
12039318fe57SMatthew G. Knepley }
12049318fe57SMatthew G. Knepley 
12059318fe57SMatthew 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)
12069318fe57SMatthew G. Knepley {
12079318fe57SMatthew G. Knepley   PetscErrorCode ierr;
12089318fe57SMatthew G. Knepley 
12099318fe57SMatthew G. Knepley   PetscFunctionBegin;
12109318fe57SMatthew G. Knepley   if (dim == 1)      {ierr = DMPlexCreateLineMesh_Internal(dm, faces[0], lower[0], upper[0], periodicity[0]);CHKERRQ(ierr);}
12119318fe57SMatthew G. Knepley   else if (simplex)  {ierr = DMPlexCreateBoxMesh_Simplex_Internal(dm, dim, faces, lower, upper, periodicity, interpolate);CHKERRQ(ierr);}
12129318fe57SMatthew G. Knepley   else               {ierr = DMPlexCreateBoxMesh_Tensor_Internal(dm, dim, faces, lower, upper, periodicity);CHKERRQ(ierr);}
12139318fe57SMatthew G. Knepley   if (!interpolate && dim > 1 && !simplex) {
1214768d5fceSMatthew G. Knepley     DM udm;
1215768d5fceSMatthew G. Knepley 
12169318fe57SMatthew G. Knepley     ierr = DMPlexUninterpolate(dm, &udm);CHKERRQ(ierr);
12179318fe57SMatthew G. Knepley     ierr = DMPlexCopyCoordinates(dm, udm);CHKERRQ(ierr);
12189318fe57SMatthew G. Knepley     ierr = DMPlexReplace_Static(dm, &udm);CHKERRQ(ierr);
1219768d5fceSMatthew G. Knepley   }
1220768d5fceSMatthew G. Knepley   PetscFunctionReturn(0);
1221c8c68bd8SToby Isaac }
1222c8c68bd8SToby Isaac 
1223768d5fceSMatthew G. Knepley /*@C
1224768d5fceSMatthew G. Knepley   DMPlexCreateBoxMesh - Creates a mesh on the tensor product of unit intervals (box) using simplices or tensor cells (hexahedra).
1225768d5fceSMatthew G. Knepley 
1226d083f849SBarry Smith   Collective
1227768d5fceSMatthew G. Knepley 
1228768d5fceSMatthew G. Knepley   Input Parameters:
1229768d5fceSMatthew G. Knepley + comm        - The communicator for the DM object
1230768d5fceSMatthew G. Knepley . dim         - The spatial dimension
1231768d5fceSMatthew G. Knepley . simplex     - PETSC_TRUE for simplices, PETSC_FALSE for tensor cells
1232fdbf62faSLisandro Dalcin . faces       - Number of faces per dimension, or NULL for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
1233768d5fceSMatthew G. Knepley . lower       - The lower left corner, or NULL for (0, 0, 0)
1234768d5fceSMatthew G. Knepley . upper       - The upper right corner, or NULL for (1, 1, 1)
1235fdbf62faSLisandro Dalcin . periodicity - The boundary type for the X,Y,Z direction, or NULL for DM_BOUNDARY_NONE
1236768d5fceSMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
1237768d5fceSMatthew G. Knepley 
1238768d5fceSMatthew G. Knepley   Output Parameter:
1239768d5fceSMatthew G. Knepley . dm  - The DM object
1240768d5fceSMatthew G. Knepley 
12419318fe57SMatthew G. Knepley   Note: If you want to customize this mesh using options, you just need to
12429318fe57SMatthew G. Knepley $  DMCreate(comm, &dm);
12439318fe57SMatthew G. Knepley $  DMSetType(dm, DMPLEX);
12449318fe57SMatthew G. Knepley $  DMSetFromOptions(dm);
12459318fe57SMatthew G. Knepley and use the options on the DMSetFromOptions() page.
12461367e252SJed Brown 
12471367e252SJed Brown   Here is the numbering returned for 2 faces in each direction for tensor cells:
1248768d5fceSMatthew G. Knepley $ 10---17---11---18----12
1249768d5fceSMatthew G. Knepley $  |         |         |
1250768d5fceSMatthew G. Knepley $  |         |         |
1251768d5fceSMatthew G. Knepley $ 20    2   22    3    24
1252768d5fceSMatthew G. Knepley $  |         |         |
1253768d5fceSMatthew G. Knepley $  |         |         |
1254768d5fceSMatthew G. Knepley $  7---15----8---16----9
1255768d5fceSMatthew G. Knepley $  |         |         |
1256768d5fceSMatthew G. Knepley $  |         |         |
1257768d5fceSMatthew G. Knepley $ 19    0   21    1   23
1258768d5fceSMatthew G. Knepley $  |         |         |
1259768d5fceSMatthew G. Knepley $  |         |         |
1260768d5fceSMatthew G. Knepley $  4---13----5---14----6
1261768d5fceSMatthew G. Knepley 
1262768d5fceSMatthew G. Knepley and for simplicial cells
1263768d5fceSMatthew G. Knepley 
1264768d5fceSMatthew G. Knepley $ 14----8---15----9----16
1265768d5fceSMatthew G. Knepley $  |\     5  |\      7 |
1266768d5fceSMatthew G. Knepley $  | \       | \       |
1267768d5fceSMatthew G. Knepley $ 13   2    14    3    15
1268768d5fceSMatthew G. Knepley $  | 4   \   | 6   \   |
1269768d5fceSMatthew G. Knepley $  |       \ |       \ |
1270768d5fceSMatthew G. Knepley $ 11----6---12----7----13
1271768d5fceSMatthew G. Knepley $  |\        |\        |
1272768d5fceSMatthew G. Knepley $  | \    1  | \     3 |
1273768d5fceSMatthew G. Knepley $ 10   0    11    1    12
1274768d5fceSMatthew G. Knepley $  | 0   \   | 2   \   |
1275768d5fceSMatthew G. Knepley $  |       \ |       \ |
1276768d5fceSMatthew G. Knepley $  8----4----9----5----10
1277768d5fceSMatthew G. Knepley 
1278768d5fceSMatthew G. Knepley   Level: beginner
1279768d5fceSMatthew G. Knepley 
12809318fe57SMatthew G. Knepley .seealso: DMSetFromOptions(), DMPlexCreateFromFile(), DMPlexCreateHexCylinderMesh(), DMSetType(), DMCreate()
1281768d5fceSMatthew G. Knepley @*/
1282768d5fceSMatthew 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)
1283552f7358SJed Brown {
12849318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
1285fdbf62faSLisandro Dalcin   PetscReal      low[3] = {0, 0, 0};
1286fdbf62faSLisandro Dalcin   PetscReal      upp[3] = {1, 1, 1};
1287fdbf62faSLisandro Dalcin   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
1288768d5fceSMatthew G. Knepley   PetscErrorCode ierr;
1289552f7358SJed Brown 
1290768d5fceSMatthew G. Knepley   PetscFunctionBegin;
12919318fe57SMatthew G. Knepley   ierr = DMCreate(comm,dm);CHKERRQ(ierr);
12929318fe57SMatthew G. Knepley   ierr = DMSetType(*dm,DMPLEX);CHKERRQ(ierr);
12939318fe57SMatthew G. Knepley   ierr = DMPlexCreateBoxMesh_Internal(*dm, dim, simplex, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt, interpolate);CHKERRQ(ierr);
12949318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
12959318fe57SMatthew G. Knepley }
1296fdbf62faSLisandro Dalcin 
1297d410b0cfSMatthew G. Knepley static PetscErrorCode DMPlexCreateWedgeBoxMesh_Internal(DM dm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
12989318fe57SMatthew G. Knepley {
12999318fe57SMatthew G. Knepley   DM             bdm, vol;
13009318fe57SMatthew G. Knepley   PetscInt       i;
13019318fe57SMatthew G. Knepley   PetscErrorCode ierr;
13029318fe57SMatthew G. Knepley 
13039318fe57SMatthew G. Knepley   PetscFunctionBegin;
13042c71b3e2SJacob Faibussowitsch   for (i = 0; i < 3; ++i) PetscCheckFalse(periodicity[i] != DM_BOUNDARY_NONE,PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Periodicity not yet supported");
13059318fe57SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), &bdm);CHKERRQ(ierr);
13069318fe57SMatthew G. Knepley   ierr = DMSetType(bdm, DMPLEX);CHKERRQ(ierr);
13079318fe57SMatthew G. Knepley   ierr = DMSetDimension(bdm, 2);CHKERRQ(ierr);
1308d410b0cfSMatthew G. Knepley   ierr = DMPlexCreateBoxMesh_Simplex_Internal(bdm, 2, faces, lower, upper, periodicity, PETSC_TRUE);CHKERRQ(ierr);
1309d410b0cfSMatthew G. Knepley   ierr = DMPlexExtrude(bdm, faces[2], upper[2] - lower[2], PETSC_TRUE, PETSC_FALSE, NULL, NULL, &vol);CHKERRQ(ierr);
13109318fe57SMatthew G. Knepley   ierr = DMDestroy(&bdm);CHKERRQ(ierr);
13119318fe57SMatthew G. Knepley   ierr = DMPlexReplace_Static(dm, &vol);CHKERRQ(ierr);
13129318fe57SMatthew G. Knepley   if (lower[2] != 0.0) {
13139318fe57SMatthew G. Knepley     Vec          v;
13149318fe57SMatthew G. Knepley     PetscScalar *x;
13159318fe57SMatthew G. Knepley     PetscInt     cDim, n;
13169318fe57SMatthew G. Knepley 
13179318fe57SMatthew G. Knepley     ierr = DMGetCoordinatesLocal(dm, &v);CHKERRQ(ierr);
13189318fe57SMatthew G. Knepley     ierr = VecGetBlockSize(v, &cDim);CHKERRQ(ierr);
13199318fe57SMatthew G. Knepley     ierr = VecGetLocalSize(v, &n);CHKERRQ(ierr);
13209318fe57SMatthew G. Knepley     ierr = VecGetArray(v, &x);CHKERRQ(ierr);
13219318fe57SMatthew G. Knepley     x   += cDim;
13229318fe57SMatthew G. Knepley     for (i = 0; i < n; i += cDim) x[i] += lower[2];
13239318fe57SMatthew G. Knepley     ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
13249318fe57SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(dm, v);CHKERRQ(ierr);
13259318fe57SMatthew G. Knepley   }
1326552f7358SJed Brown   PetscFunctionReturn(0);
1327552f7358SJed Brown }
1328552f7358SJed Brown 
132900dabe28SStefano Zampini /*@
133000dabe28SStefano Zampini   DMPlexCreateWedgeBoxMesh - Creates a 3-D mesh tesselating the (x,y) plane and extruding in the third direction using wedge cells.
133100dabe28SStefano Zampini 
1332d083f849SBarry Smith   Collective
133300dabe28SStefano Zampini 
133400dabe28SStefano Zampini   Input Parameters:
133500dabe28SStefano Zampini + comm        - The communicator for the DM object
133600dabe28SStefano Zampini . faces       - Number of faces per dimension, or NULL for (1, 1, 1)
133700dabe28SStefano Zampini . lower       - The lower left corner, or NULL for (0, 0, 0)
133800dabe28SStefano Zampini . upper       - The upper right corner, or NULL for (1, 1, 1)
133900dabe28SStefano Zampini . periodicity - The boundary type for the X,Y,Z direction, or NULL for DM_BOUNDARY_NONE
1340d0fcb9c2SMatthew G. Knepley . orderHeight - If PETSC_TRUE, orders the extruded cells in the height first. Otherwise, orders the cell on the layers first
134100dabe28SStefano Zampini - interpolate - Flag to create intermediate mesh pieces (edges, faces)
134200dabe28SStefano Zampini 
134300dabe28SStefano Zampini   Output Parameter:
134400dabe28SStefano Zampini . dm  - The DM object
134500dabe28SStefano Zampini 
134600dabe28SStefano Zampini   Level: beginner
134700dabe28SStefano Zampini 
1348d410b0cfSMatthew G. Knepley .seealso: DMPlexCreateHexCylinderMesh(), DMPlexCreateWedgeCylinderMesh(), DMExtrude(), DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
134900dabe28SStefano Zampini @*/
1350d0fcb9c2SMatthew 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)
135100dabe28SStefano Zampini {
13529318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
135300dabe28SStefano Zampini   PetscReal      low[3] = {0, 0, 0};
135400dabe28SStefano Zampini   PetscReal      upp[3] = {1, 1, 1};
135500dabe28SStefano Zampini   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
135600dabe28SStefano Zampini   PetscErrorCode ierr;
135700dabe28SStefano Zampini 
135800dabe28SStefano Zampini   PetscFunctionBegin;
13599318fe57SMatthew G. Knepley   ierr = DMCreate(comm,dm);CHKERRQ(ierr);
13609318fe57SMatthew G. Knepley   ierr = DMSetType(*dm,DMPLEX);CHKERRQ(ierr);
1361d410b0cfSMatthew G. Knepley   ierr = DMPlexCreateWedgeBoxMesh_Internal(*dm, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt);CHKERRQ(ierr);
1362d410b0cfSMatthew G. Knepley   if (!interpolate) {
1363d410b0cfSMatthew G. Knepley     DM udm;
136400dabe28SStefano Zampini 
1365d410b0cfSMatthew G. Knepley     ierr = DMPlexUninterpolate(*dm, &udm);CHKERRQ(ierr);
1366d410b0cfSMatthew G. Knepley     ierr = DMPlexReplace_Static(*dm, &udm);CHKERRQ(ierr);
136700dabe28SStefano Zampini   }
136800dabe28SStefano Zampini   PetscFunctionReturn(0);
136900dabe28SStefano Zampini }
137000dabe28SStefano Zampini 
1371a9074c1eSMatthew G. Knepley /*@C
1372a9074c1eSMatthew G. Knepley   DMPlexSetOptionsPrefix - Sets the prefix used for searching for all DM options in the database.
1373a9074c1eSMatthew G. Knepley 
1374d083f849SBarry Smith   Logically Collective on dm
1375a9074c1eSMatthew G. Knepley 
1376a9074c1eSMatthew G. Knepley   Input Parameters:
1377a9074c1eSMatthew G. Knepley + dm - the DM context
1378a9074c1eSMatthew G. Knepley - prefix - the prefix to prepend to all option names
1379a9074c1eSMatthew G. Knepley 
1380a9074c1eSMatthew G. Knepley   Notes:
1381a9074c1eSMatthew G. Knepley   A hyphen (-) must NOT be given at the beginning of the prefix name.
1382a9074c1eSMatthew G. Knepley   The first character of all runtime options is AUTOMATICALLY the hyphen.
1383a9074c1eSMatthew G. Knepley 
1384a9074c1eSMatthew G. Knepley   Level: advanced
1385a9074c1eSMatthew G. Knepley 
1386a9074c1eSMatthew G. Knepley .seealso: SNESSetFromOptions()
1387a9074c1eSMatthew G. Knepley @*/
1388a9074c1eSMatthew G. Knepley PetscErrorCode DMPlexSetOptionsPrefix(DM dm, const char prefix[])
1389a9074c1eSMatthew G. Knepley {
1390a9074c1eSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
1391a9074c1eSMatthew G. Knepley   PetscErrorCode ierr;
1392a9074c1eSMatthew G. Knepley 
1393a9074c1eSMatthew G. Knepley   PetscFunctionBegin;
1394a9074c1eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1395a9074c1eSMatthew G. Knepley   ierr = PetscObjectSetOptionsPrefix((PetscObject) dm, prefix);CHKERRQ(ierr);
1396a9074c1eSMatthew G. Knepley   ierr = PetscObjectSetOptionsPrefix((PetscObject) mesh->partitioner, prefix);CHKERRQ(ierr);
1397a9074c1eSMatthew G. Knepley   PetscFunctionReturn(0);
1398a9074c1eSMatthew G. Knepley }
1399a9074c1eSMatthew G. Knepley 
14009318fe57SMatthew G. Knepley /* Remap geometry to cylinder
140161a622f3SMatthew G. Knepley    TODO: This only works for a single refinement, then it is broken
140261a622f3SMatthew G. Knepley 
14039318fe57SMatthew G. Knepley      Interior square: Linear interpolation is correct
14049318fe57SMatthew G. Knepley      The other cells all have vertices on rays from the origin. We want to uniformly expand the spacing
14059318fe57SMatthew G. Knepley      such that the last vertex is on the unit circle. So the closest and farthest vertices are at distance
14060510c589SMatthew G. Knepley 
14079318fe57SMatthew G. Knepley        phi     = arctan(y/x)
14089318fe57SMatthew G. Knepley        d_close = sqrt(1/8 + 1/4 sin^2(phi))
14099318fe57SMatthew G. Knepley        d_far   = sqrt(1/2 + sin^2(phi))
14100510c589SMatthew G. Knepley 
14119318fe57SMatthew G. Knepley      so we remap them using
14120510c589SMatthew G. Knepley 
14139318fe57SMatthew G. Knepley        x_new = x_close + (x - x_close) (1 - d_close) / (d_far - d_close)
14149318fe57SMatthew G. Knepley        y_new = y_close + (y - y_close) (1 - d_close) / (d_far - d_close)
14150510c589SMatthew G. Knepley 
14169318fe57SMatthew G. Knepley      If pi/4 < phi < 3pi/4 or -3pi/4 < phi < -pi/4, then we switch x and y.
14179318fe57SMatthew G. Knepley */
14189318fe57SMatthew G. Knepley static void snapToCylinder(PetscInt dim, PetscInt Nf, PetscInt NfAux,
14199318fe57SMatthew G. Knepley                            const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
14209318fe57SMatthew G. Knepley                            const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
14219318fe57SMatthew G. Knepley                            PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
14229318fe57SMatthew G. Knepley {
14239318fe57SMatthew G. Knepley   const PetscReal dis = 1.0/PetscSqrtReal(2.0);
14249318fe57SMatthew G. Knepley   const PetscReal ds2 = 0.5*dis;
142522cc497dSMatthew G. Knepley 
14269318fe57SMatthew G. Knepley   if ((PetscAbsScalar(u[0]) <= ds2) && (PetscAbsScalar(u[1]) <= ds2)) {
14279318fe57SMatthew G. Knepley     f0[0] = u[0];
14289318fe57SMatthew G. Knepley     f0[1] = u[1];
14299318fe57SMatthew G. Knepley   } else {
14309318fe57SMatthew G. Knepley     PetscReal phi, sinp, cosp, dc, df, x, y, xc, yc;
14310510c589SMatthew G. Knepley 
14329318fe57SMatthew G. Knepley     x    = PetscRealPart(u[0]);
14339318fe57SMatthew G. Knepley     y    = PetscRealPart(u[1]);
14349318fe57SMatthew G. Knepley     phi  = PetscAtan2Real(y, x);
14359318fe57SMatthew G. Knepley     sinp = PetscSinReal(phi);
14369318fe57SMatthew G. Knepley     cosp = PetscCosReal(phi);
14379318fe57SMatthew G. Knepley     if ((PetscAbsReal(phi) > PETSC_PI/4.0) && (PetscAbsReal(phi) < 3.0*PETSC_PI/4.0)) {
14389318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2/sinp);
14399318fe57SMatthew G. Knepley       df = PetscAbsReal(dis/sinp);
14409318fe57SMatthew G. Knepley       xc = ds2*x/PetscAbsReal(y);
14419318fe57SMatthew G. Knepley       yc = ds2*PetscSignReal(y);
14429318fe57SMatthew G. Knepley     } else {
14439318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2/cosp);
14449318fe57SMatthew G. Knepley       df = PetscAbsReal(dis/cosp);
14459318fe57SMatthew G. Knepley       xc = ds2*PetscSignReal(x);
14469318fe57SMatthew G. Knepley       yc = ds2*y/PetscAbsReal(x);
14479318fe57SMatthew G. Knepley     }
14489318fe57SMatthew G. Knepley     f0[0] = xc + (u[0] - xc)*(1.0 - dc)/(df - dc);
14499318fe57SMatthew G. Knepley     f0[1] = yc + (u[1] - yc)*(1.0 - dc)/(df - dc);
14509318fe57SMatthew G. Knepley   }
14519318fe57SMatthew G. Knepley   f0[2] = u[2];
14529318fe57SMatthew G. Knepley }
14530510c589SMatthew G. Knepley 
14549318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateHexCylinderMesh_Internal(DM dm, DMBoundaryType periodicZ)
14550510c589SMatthew G. Knepley {
14560510c589SMatthew G. Knepley   const PetscInt dim = 3;
14579318fe57SMatthew G. Knepley   PetscInt       numCells, numVertices;
1458d8c47e87SMatthew G. Knepley   PetscMPIInt    rank;
14590510c589SMatthew G. Knepley   PetscErrorCode ierr;
14600510c589SMatthew G. Knepley 
14610510c589SMatthew G. Knepley   PetscFunctionBegin;
14629318fe57SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr);
14639318fe57SMatthew G. Knepley   ierr = DMSetDimension(dm, dim);CHKERRQ(ierr);
14640510c589SMatthew G. Knepley   /* Create topology */
14650510c589SMatthew G. Knepley   {
14660510c589SMatthew G. Knepley     PetscInt cone[8], c;
14670510c589SMatthew G. Knepley 
1468dd400576SPatrick Sanan     numCells    = rank == 0 ?  5 : 0;
1469dd400576SPatrick Sanan     numVertices = rank == 0 ? 16 : 0;
1470006a8963SMatthew G. Knepley     if (periodicZ == DM_BOUNDARY_PERIODIC) {
1471ae8bcbbbSMatthew G. Knepley       numCells   *= 3;
1472dd400576SPatrick Sanan       numVertices = rank == 0 ? 24 : 0;
1473006a8963SMatthew G. Knepley     }
14749318fe57SMatthew G. Knepley     ierr = DMPlexSetChart(dm, 0, numCells+numVertices);CHKERRQ(ierr);
14759318fe57SMatthew G. Knepley     for (c = 0; c < numCells; c++) {ierr = DMPlexSetConeSize(dm, c, 8);CHKERRQ(ierr);}
14769318fe57SMatthew G. Knepley     ierr = DMSetUp(dm);CHKERRQ(ierr);
1477dd400576SPatrick Sanan     if (rank == 0) {
1478006a8963SMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
1479ae8bcbbbSMatthew G. Knepley         cone[0] = 15; cone[1] = 18; cone[2] = 17; cone[3] = 16;
1480ae8bcbbbSMatthew G. Knepley         cone[4] = 31; cone[5] = 32; cone[6] = 33; cone[7] = 34;
14819318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 0, cone);CHKERRQ(ierr);
1482ae8bcbbbSMatthew G. Knepley         cone[0] = 16; cone[1] = 17; cone[2] = 24; cone[3] = 23;
1483ae8bcbbbSMatthew G. Knepley         cone[4] = 32; cone[5] = 36; cone[6] = 37; cone[7] = 33; /* 22 25 26 21 */
14849318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 1, cone);CHKERRQ(ierr);
1485ae8bcbbbSMatthew G. Knepley         cone[0] = 18; cone[1] = 27; cone[2] = 24; cone[3] = 17;
1486ae8bcbbbSMatthew G. Knepley         cone[4] = 34; cone[5] = 33; cone[6] = 37; cone[7] = 38;
14879318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 2, cone);CHKERRQ(ierr);
1488ae8bcbbbSMatthew G. Knepley         cone[0] = 29; cone[1] = 27; cone[2] = 18; cone[3] = 15;
1489ae8bcbbbSMatthew G. Knepley         cone[4] = 35; cone[5] = 31; cone[6] = 34; cone[7] = 38;
14909318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 3, cone);CHKERRQ(ierr);
1491ae8bcbbbSMatthew G. Knepley         cone[0] = 29; cone[1] = 15; cone[2] = 16; cone[3] = 23;
1492ae8bcbbbSMatthew G. Knepley         cone[4] = 35; cone[5] = 36; cone[6] = 32; cone[7] = 31;
14939318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 4, cone);CHKERRQ(ierr);
1494006a8963SMatthew G. Knepley 
1495ae8bcbbbSMatthew G. Knepley         cone[0] = 31; cone[1] = 34; cone[2] = 33; cone[3] = 32;
1496ae8bcbbbSMatthew G. Knepley         cone[4] = 19; cone[5] = 22; cone[6] = 21; cone[7] = 20;
14979318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 5, cone);CHKERRQ(ierr);
1498ae8bcbbbSMatthew G. Knepley         cone[0] = 32; cone[1] = 33; cone[2] = 37; cone[3] = 36;
1499ae8bcbbbSMatthew G. Knepley         cone[4] = 22; cone[5] = 25; cone[6] = 26; cone[7] = 21;
15009318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 6, cone);CHKERRQ(ierr);
1501ae8bcbbbSMatthew G. Knepley         cone[0] = 34; cone[1] = 38; cone[2] = 37; cone[3] = 33;
1502ae8bcbbbSMatthew G. Knepley         cone[4] = 20; cone[5] = 21; cone[6] = 26; cone[7] = 28;
15039318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 7, cone);CHKERRQ(ierr);
1504ae8bcbbbSMatthew G. Knepley         cone[0] = 35; cone[1] = 38; cone[2] = 34; cone[3] = 31;
1505ae8bcbbbSMatthew G. Knepley         cone[4] = 30; cone[5] = 19; cone[6] = 20; cone[7] = 28;
15069318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 8, cone);CHKERRQ(ierr);
1507ae8bcbbbSMatthew G. Knepley         cone[0] = 35; cone[1] = 31; cone[2] = 32; cone[3] = 36;
1508ae8bcbbbSMatthew G. Knepley         cone[4] = 30; cone[5] = 25; cone[6] = 22; cone[7] = 19;
15099318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 9, cone);CHKERRQ(ierr);
1510ae8bcbbbSMatthew G. Knepley 
1511ae8bcbbbSMatthew G. Knepley         cone[0] = 19; cone[1] = 20; cone[2] = 21; cone[3] = 22;
1512ae8bcbbbSMatthew G. Knepley         cone[4] = 15; cone[5] = 16; cone[6] = 17; cone[7] = 18;
15139318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 10, cone);CHKERRQ(ierr);
1514ae8bcbbbSMatthew G. Knepley         cone[0] = 22; cone[1] = 21; cone[2] = 26; cone[3] = 25;
1515ae8bcbbbSMatthew G. Knepley         cone[4] = 16; cone[5] = 23; cone[6] = 24; cone[7] = 17;
15169318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 11, cone);CHKERRQ(ierr);
1517ae8bcbbbSMatthew G. Knepley         cone[0] = 20; cone[1] = 28; cone[2] = 26; cone[3] = 21;
1518ae8bcbbbSMatthew G. Knepley         cone[4] = 18; cone[5] = 17; cone[6] = 24; cone[7] = 27;
15199318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 12, cone);CHKERRQ(ierr);
1520ae8bcbbbSMatthew G. Knepley         cone[0] = 30; cone[1] = 28; cone[2] = 20; cone[3] = 19;
1521ae8bcbbbSMatthew G. Knepley         cone[4] = 29; cone[5] = 15; cone[6] = 18; cone[7] = 27;
15229318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 13, cone);CHKERRQ(ierr);
1523ae8bcbbbSMatthew G. Knepley         cone[0] = 30; cone[1] = 19; cone[2] = 22; cone[3] = 25;
1524ae8bcbbbSMatthew G. Knepley         cone[4] = 29; cone[5] = 23; cone[6] = 16; cone[7] = 15;
15259318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 14, cone);CHKERRQ(ierr);
1526006a8963SMatthew G. Knepley       } else {
152710c6f908SMatthew G. Knepley         cone[0] =  5; cone[1] =  8; cone[2] =  7; cone[3] =  6;
152810c6f908SMatthew G. Knepley         cone[4] =  9; cone[5] = 12; cone[6] = 11; cone[7] = 10;
15299318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 0, cone);CHKERRQ(ierr);
153010c6f908SMatthew G. Knepley         cone[0] =  6; cone[1] =  7; cone[2] = 14; cone[3] = 13;
153110c6f908SMatthew G. Knepley         cone[4] = 12; cone[5] = 15; cone[6] = 16; cone[7] = 11;
15329318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 1, cone);CHKERRQ(ierr);
153310c6f908SMatthew G. Knepley         cone[0] =  8; cone[1] = 17; cone[2] = 14; cone[3] =  7;
153410c6f908SMatthew G. Knepley         cone[4] = 10; cone[5] = 11; cone[6] = 16; cone[7] = 18;
15359318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 2, cone);CHKERRQ(ierr);
153610c6f908SMatthew G. Knepley         cone[0] = 19; cone[1] = 17; cone[2] =  8; cone[3] =  5;
153710c6f908SMatthew G. Knepley         cone[4] = 20; cone[5] =  9; cone[6] = 10; cone[7] = 18;
15389318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 3, cone);CHKERRQ(ierr);
153910c6f908SMatthew G. Knepley         cone[0] = 19; cone[1] =  5; cone[2] =  6; cone[3] = 13;
154010c6f908SMatthew G. Knepley         cone[4] = 20; cone[5] = 15; cone[6] = 12; cone[7] =  9;
15419318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 4, cone);CHKERRQ(ierr);
1542006a8963SMatthew G. Knepley       }
1543d8c47e87SMatthew G. Knepley     }
15449318fe57SMatthew G. Knepley     ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
15459318fe57SMatthew G. Knepley     ierr = DMPlexStratify(dm);CHKERRQ(ierr);
15460510c589SMatthew G. Knepley   }
1547dbc1dc17SMatthew G. Knepley   /* Create cube geometry */
15480510c589SMatthew G. Knepley   {
15490510c589SMatthew G. Knepley     Vec             coordinates;
15500510c589SMatthew G. Knepley     PetscSection    coordSection;
15510510c589SMatthew G. Knepley     PetscScalar    *coords;
15520510c589SMatthew G. Knepley     PetscInt        coordSize, v;
15530510c589SMatthew G. Knepley     const PetscReal dis = 1.0/PetscSqrtReal(2.0);
15540510c589SMatthew G. Knepley     const PetscReal ds2 = dis/2.0;
15550510c589SMatthew G. Knepley 
15560510c589SMatthew G. Knepley     /* Build coordinates */
15579318fe57SMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
15580510c589SMatthew G. Knepley     ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
15590510c589SMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(coordSection, 0, dim);CHKERRQ(ierr);
15600510c589SMatthew G. Knepley     ierr = PetscSectionSetChart(coordSection, numCells, numCells+numVertices);CHKERRQ(ierr);
15610510c589SMatthew G. Knepley     for (v = numCells; v < numCells+numVertices; ++v) {
15620510c589SMatthew G. Knepley       ierr = PetscSectionSetDof(coordSection, v, dim);CHKERRQ(ierr);
15630510c589SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(coordSection, v, 0, dim);CHKERRQ(ierr);
15640510c589SMatthew G. Knepley     }
15650510c589SMatthew G. Knepley     ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
15660510c589SMatthew G. Knepley     ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
15670510c589SMatthew G. Knepley     ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
15680510c589SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
15690510c589SMatthew G. Knepley     ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
15700510c589SMatthew G. Knepley     ierr = VecSetBlockSize(coordinates, dim);CHKERRQ(ierr);
15710510c589SMatthew G. Knepley     ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
15720510c589SMatthew G. Knepley     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
1573dd400576SPatrick Sanan     if (rank == 0) {
15740510c589SMatthew G. Knepley       coords[0*dim+0] = -ds2; coords[0*dim+1] = -ds2; coords[0*dim+2] = 0.0;
15750510c589SMatthew G. Knepley       coords[1*dim+0] =  ds2; coords[1*dim+1] = -ds2; coords[1*dim+2] = 0.0;
15760510c589SMatthew G. Knepley       coords[2*dim+0] =  ds2; coords[2*dim+1] =  ds2; coords[2*dim+2] = 0.0;
15770510c589SMatthew G. Knepley       coords[3*dim+0] = -ds2; coords[3*dim+1] =  ds2; coords[3*dim+2] = 0.0;
15780510c589SMatthew G. Knepley       coords[4*dim+0] = -ds2; coords[4*dim+1] = -ds2; coords[4*dim+2] = 1.0;
15790510c589SMatthew G. Knepley       coords[5*dim+0] = -ds2; coords[5*dim+1] =  ds2; coords[5*dim+2] = 1.0;
15800510c589SMatthew G. Knepley       coords[6*dim+0] =  ds2; coords[6*dim+1] =  ds2; coords[6*dim+2] = 1.0;
15810510c589SMatthew G. Knepley       coords[7*dim+0] =  ds2; coords[7*dim+1] = -ds2; coords[7*dim+2] = 1.0;
15820510c589SMatthew G. Knepley       coords[ 8*dim+0] =  dis; coords[ 8*dim+1] = -dis; coords[ 8*dim+2] = 0.0;
15830510c589SMatthew G. Knepley       coords[ 9*dim+0] =  dis; coords[ 9*dim+1] =  dis; coords[ 9*dim+2] = 0.0;
15840510c589SMatthew G. Knepley       coords[10*dim+0] =  dis; coords[10*dim+1] = -dis; coords[10*dim+2] = 1.0;
15850510c589SMatthew G. Knepley       coords[11*dim+0] =  dis; coords[11*dim+1] =  dis; coords[11*dim+2] = 1.0;
15860510c589SMatthew G. Knepley       coords[12*dim+0] = -dis; coords[12*dim+1] =  dis; coords[12*dim+2] = 0.0;
15870510c589SMatthew G. Knepley       coords[13*dim+0] = -dis; coords[13*dim+1] =  dis; coords[13*dim+2] = 1.0;
15880510c589SMatthew G. Knepley       coords[14*dim+0] = -dis; coords[14*dim+1] = -dis; coords[14*dim+2] = 0.0;
15890510c589SMatthew G. Knepley       coords[15*dim+0] = -dis; coords[15*dim+1] = -dis; coords[15*dim+2] = 1.0;
1590ae8bcbbbSMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
1591ae8bcbbbSMatthew G. Knepley         /* 15 31 19 */ coords[16*dim+0] = -ds2; coords[16*dim+1] = -ds2; coords[16*dim+2] = 0.5;
1592ae8bcbbbSMatthew G. Knepley         /* 16 32 22 */ coords[17*dim+0] =  ds2; coords[17*dim+1] = -ds2; coords[17*dim+2] = 0.5;
1593ae8bcbbbSMatthew G. Knepley         /* 17 33 21 */ coords[18*dim+0] =  ds2; coords[18*dim+1] =  ds2; coords[18*dim+2] = 0.5;
1594ae8bcbbbSMatthew G. Knepley         /* 18 34 20 */ coords[19*dim+0] = -ds2; coords[19*dim+1] =  ds2; coords[19*dim+2] = 0.5;
1595ae8bcbbbSMatthew G. Knepley         /* 29 35 30 */ coords[20*dim+0] = -dis; coords[20*dim+1] = -dis; coords[20*dim+2] = 0.5;
1596ae8bcbbbSMatthew G. Knepley         /* 23 36 25 */ coords[21*dim+0] =  dis; coords[21*dim+1] = -dis; coords[21*dim+2] = 0.5;
1597ae8bcbbbSMatthew G. Knepley         /* 24 37 26 */ coords[22*dim+0] =  dis; coords[22*dim+1] =  dis; coords[22*dim+2] = 0.5;
1598ae8bcbbbSMatthew G. Knepley         /* 27 38 28 */ coords[23*dim+0] = -dis; coords[23*dim+1] =  dis; coords[23*dim+2] = 0.5;
1599ae8bcbbbSMatthew G. Knepley       }
1600d8c47e87SMatthew G. Knepley     }
16010510c589SMatthew G. Knepley     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
16029318fe57SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
16030510c589SMatthew G. Knepley     ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
16040510c589SMatthew G. Knepley   }
1605006a8963SMatthew G. Knepley   /* Create periodicity */
1606006a8963SMatthew G. Knepley   if (periodicZ == DM_BOUNDARY_PERIODIC || periodicZ == DM_BOUNDARY_TWIST) {
1607006a8963SMatthew G. Knepley     PetscReal      L[3];
1608006a8963SMatthew G. Knepley     PetscReal      maxCell[3];
1609006a8963SMatthew G. Knepley     DMBoundaryType bdType[3];
1610006a8963SMatthew G. Knepley     PetscReal      lower[3] = {0.0, 0.0, 0.0};
1611ae8bcbbbSMatthew G. Knepley     PetscReal      upper[3] = {1.0, 1.0, 1.5};
1612ae8bcbbbSMatthew G. Knepley     PetscInt       i, numZCells = 3;
1613006a8963SMatthew G. Knepley 
1614006a8963SMatthew G. Knepley     bdType[0] = DM_BOUNDARY_NONE;
1615006a8963SMatthew G. Knepley     bdType[1] = DM_BOUNDARY_NONE;
1616006a8963SMatthew G. Knepley     bdType[2] = periodicZ;
1617006a8963SMatthew G. Knepley     for (i = 0; i < dim; i++) {
1618006a8963SMatthew G. Knepley       L[i]       = upper[i] - lower[i];
1619006a8963SMatthew G. Knepley       maxCell[i] = 1.1 * (L[i] / numZCells);
1620006a8963SMatthew G. Knepley     }
16219318fe57SMatthew G. Knepley     ierr = DMSetPeriodicity(dm, PETSC_TRUE, maxCell, L, bdType);CHKERRQ(ierr);
1622006a8963SMatthew G. Knepley   }
1623dbc1dc17SMatthew G. Knepley   {
16249318fe57SMatthew G. Knepley     DM          cdm;
16259318fe57SMatthew G. Knepley     PetscDS     cds;
16269318fe57SMatthew G. Knepley     PetscScalar c[2] = {1.0, 1.0};
1627dbc1dc17SMatthew G. Knepley 
16289318fe57SMatthew G. Knepley     ierr = DMPlexCreateCoordinateSpace(dm, 1, snapToCylinder);CHKERRQ(ierr);
16299318fe57SMatthew G. Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
16309318fe57SMatthew G. Knepley     ierr = DMGetDS(cdm, &cds);CHKERRQ(ierr);
16319318fe57SMatthew G. Knepley     ierr = PetscDSSetConstants(cds, 2, c);CHKERRQ(ierr);
1632dbc1dc17SMatthew G. Knepley   }
16339318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
16349318fe57SMatthew G. Knepley   ierr = DMPlexInterpolateInPlace_Internal(dm);CHKERRQ(ierr);
16350510c589SMatthew G. Knepley   PetscFunctionReturn(0);
16360510c589SMatthew G. Knepley }
16370510c589SMatthew G. Knepley 
163824119c2aSMatthew G. Knepley /*@
16399318fe57SMatthew G. Knepley   DMPlexCreateHexCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using hexahedra.
164024119c2aSMatthew G. Knepley 
1641d083f849SBarry Smith   Collective
164224119c2aSMatthew G. Knepley 
164324119c2aSMatthew G. Knepley   Input Parameters:
164424119c2aSMatthew G. Knepley + comm      - The communicator for the DM object
16459318fe57SMatthew G. Knepley - periodicZ - The boundary type for the Z direction
164624119c2aSMatthew G. Knepley 
164724119c2aSMatthew G. Knepley   Output Parameter:
164824119c2aSMatthew G. Knepley . dm  - The DM object
164924119c2aSMatthew G. Knepley 
16509318fe57SMatthew G. Knepley   Note:
16519318fe57SMatthew G. Knepley   Here is the output numbering looking from the bottom of the cylinder:
16529318fe57SMatthew G. Knepley $       17-----14
16539318fe57SMatthew G. Knepley $        |     |
16549318fe57SMatthew G. Knepley $        |  2  |
16559318fe57SMatthew G. Knepley $        |     |
16569318fe57SMatthew G. Knepley $ 17-----8-----7-----14
16579318fe57SMatthew G. Knepley $  |     |     |     |
16589318fe57SMatthew G. Knepley $  |  3  |  0  |  1  |
16599318fe57SMatthew G. Knepley $  |     |     |     |
16609318fe57SMatthew G. Knepley $ 19-----5-----6-----13
16619318fe57SMatthew G. Knepley $        |     |
16629318fe57SMatthew G. Knepley $        |  4  |
16639318fe57SMatthew G. Knepley $        |     |
16649318fe57SMatthew G. Knepley $       19-----13
16659318fe57SMatthew G. Knepley $
16669318fe57SMatthew G. Knepley $ and up through the top
16679318fe57SMatthew G. Knepley $
16689318fe57SMatthew G. Knepley $       18-----16
16699318fe57SMatthew G. Knepley $        |     |
16709318fe57SMatthew G. Knepley $        |  2  |
16719318fe57SMatthew G. Knepley $        |     |
16729318fe57SMatthew G. Knepley $ 18----10----11-----16
16739318fe57SMatthew G. Knepley $  |     |     |     |
16749318fe57SMatthew G. Knepley $  |  3  |  0  |  1  |
16759318fe57SMatthew G. Knepley $  |     |     |     |
16769318fe57SMatthew G. Knepley $ 20-----9----12-----15
16779318fe57SMatthew G. Knepley $        |     |
16789318fe57SMatthew G. Knepley $        |  4  |
16799318fe57SMatthew G. Knepley $        |     |
16809318fe57SMatthew G. Knepley $       20-----15
16819318fe57SMatthew G. Knepley 
168224119c2aSMatthew G. Knepley   Level: beginner
168324119c2aSMatthew G. Knepley 
16849318fe57SMatthew G. Knepley .seealso: DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
168524119c2aSMatthew G. Knepley @*/
16869318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateHexCylinderMesh(MPI_Comm comm, DMBoundaryType periodicZ, DM *dm)
16879318fe57SMatthew G. Knepley {
16889318fe57SMatthew G. Knepley   PetscErrorCode ierr;
16899318fe57SMatthew G. Knepley 
16909318fe57SMatthew G. Knepley   PetscFunctionBegin;
16919318fe57SMatthew G. Knepley   PetscValidPointer(dm, 3);
16929318fe57SMatthew G. Knepley   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
16939318fe57SMatthew G. Knepley   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
16949318fe57SMatthew G. Knepley   ierr = DMPlexCreateHexCylinderMesh_Internal(*dm, periodicZ);CHKERRQ(ierr);
16959318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
16969318fe57SMatthew G. Knepley }
16979318fe57SMatthew G. Knepley 
16989318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateWedgeCylinderMesh_Internal(DM dm, PetscInt n, PetscBool interpolate)
169924119c2aSMatthew G. Knepley {
170024119c2aSMatthew G. Knepley   const PetscInt dim = 3;
1701412e9a14SMatthew G. Knepley   PetscInt       numCells, numVertices, v;
17029fe9f049SMatthew G. Knepley   PetscMPIInt    rank;
170324119c2aSMatthew G. Knepley   PetscErrorCode ierr;
170424119c2aSMatthew G. Knepley 
170524119c2aSMatthew G. Knepley   PetscFunctionBegin;
17062c71b3e2SJacob Faibussowitsch   PetscCheckFalse(n < 0,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of wedges %D cannot be negative", n);
17079318fe57SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr);
17089318fe57SMatthew G. Knepley   ierr = DMSetDimension(dm, dim);CHKERRQ(ierr);
1709412e9a14SMatthew G. Knepley   /* Must create the celltype label here so that we do not automatically try to compute the types */
17109318fe57SMatthew G. Knepley   ierr = DMCreateLabel(dm, "celltype");CHKERRQ(ierr);
171124119c2aSMatthew G. Knepley   /* Create topology */
171224119c2aSMatthew G. Knepley   {
171324119c2aSMatthew G. Knepley     PetscInt cone[6], c;
171424119c2aSMatthew G. Knepley 
1715dd400576SPatrick Sanan     numCells    = rank == 0 ?        n : 0;
1716dd400576SPatrick Sanan     numVertices = rank == 0 ?  2*(n+1) : 0;
17179318fe57SMatthew G. Knepley     ierr = DMPlexSetChart(dm, 0, numCells+numVertices);CHKERRQ(ierr);
17189318fe57SMatthew G. Knepley     for (c = 0; c < numCells; c++) {ierr = DMPlexSetConeSize(dm, c, 6);CHKERRQ(ierr);}
17199318fe57SMatthew G. Knepley     ierr = DMSetUp(dm);CHKERRQ(ierr);
172024119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
172124119c2aSMatthew G. Knepley       cone[0] =  c+n*1; cone[1] = (c+1)%n+n*1; cone[2] = 0+3*n;
172224119c2aSMatthew G. Knepley       cone[3] =  c+n*2; cone[4] = (c+1)%n+n*2; cone[5] = 1+3*n;
17239318fe57SMatthew G. Knepley       ierr = DMPlexSetCone(dm, c, cone);CHKERRQ(ierr);
17249318fe57SMatthew G. Knepley       ierr = DMPlexSetCellType(dm, c, DM_POLYTOPE_TRI_PRISM_TENSOR);CHKERRQ(ierr);
172524119c2aSMatthew G. Knepley     }
17269318fe57SMatthew G. Knepley     ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
17279318fe57SMatthew G. Knepley     ierr = DMPlexStratify(dm);CHKERRQ(ierr);
172824119c2aSMatthew G. Knepley   }
1729412e9a14SMatthew G. Knepley   for (v = numCells; v < numCells+numVertices; ++v) {
17309318fe57SMatthew G. Knepley     ierr = DMPlexSetCellType(dm, v, DM_POLYTOPE_POINT);CHKERRQ(ierr);
173124119c2aSMatthew G. Knepley   }
173224119c2aSMatthew G. Knepley   /* Create cylinder geometry */
173324119c2aSMatthew G. Knepley   {
173424119c2aSMatthew G. Knepley     Vec          coordinates;
173524119c2aSMatthew G. Knepley     PetscSection coordSection;
173624119c2aSMatthew G. Knepley     PetscScalar *coords;
1737412e9a14SMatthew G. Knepley     PetscInt     coordSize, c;
173824119c2aSMatthew G. Knepley 
173924119c2aSMatthew G. Knepley     /* Build coordinates */
17409318fe57SMatthew G. Knepley     ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
174124119c2aSMatthew G. Knepley     ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
174224119c2aSMatthew G. Knepley     ierr = PetscSectionSetFieldComponents(coordSection, 0, dim);CHKERRQ(ierr);
174324119c2aSMatthew G. Knepley     ierr = PetscSectionSetChart(coordSection, numCells, numCells+numVertices);CHKERRQ(ierr);
174424119c2aSMatthew G. Knepley     for (v = numCells; v < numCells+numVertices; ++v) {
174524119c2aSMatthew G. Knepley       ierr = PetscSectionSetDof(coordSection, v, dim);CHKERRQ(ierr);
174624119c2aSMatthew G. Knepley       ierr = PetscSectionSetFieldDof(coordSection, v, 0, dim);CHKERRQ(ierr);
174724119c2aSMatthew G. Knepley     }
174824119c2aSMatthew G. Knepley     ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
174924119c2aSMatthew G. Knepley     ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
175024119c2aSMatthew G. Knepley     ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
175124119c2aSMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
175224119c2aSMatthew G. Knepley     ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
175324119c2aSMatthew G. Knepley     ierr = VecSetBlockSize(coordinates, dim);CHKERRQ(ierr);
175424119c2aSMatthew G. Knepley     ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
175524119c2aSMatthew G. Knepley     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
175624119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
175724119c2aSMatthew 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;
175824119c2aSMatthew 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;
175924119c2aSMatthew G. Knepley     }
1760dd400576SPatrick Sanan     if (rank == 0) {
176124119c2aSMatthew 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;
176224119c2aSMatthew 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;
17639fe9f049SMatthew G. Knepley     }
176424119c2aSMatthew G. Knepley     ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
17659318fe57SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
176624119c2aSMatthew G. Knepley     ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
176724119c2aSMatthew G. Knepley   }
17689318fe57SMatthew G. Knepley   /* Interpolate */
17699318fe57SMatthew G. Knepley   if (interpolate) {ierr = DMPlexInterpolateInPlace_Internal(dm);CHKERRQ(ierr);}
17709318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
17719318fe57SMatthew G. Knepley }
17729318fe57SMatthew G. Knepley 
17739318fe57SMatthew G. Knepley /*@
17749318fe57SMatthew G. Knepley   DMPlexCreateWedgeCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using wedges.
17759318fe57SMatthew G. Knepley 
17769318fe57SMatthew G. Knepley   Collective
17779318fe57SMatthew G. Knepley 
17789318fe57SMatthew G. Knepley   Input Parameters:
17799318fe57SMatthew G. Knepley + comm - The communicator for the DM object
17809318fe57SMatthew G. Knepley . n    - The number of wedges around the origin
17819318fe57SMatthew G. Knepley - interpolate - Create edges and faces
17829318fe57SMatthew G. Knepley 
17839318fe57SMatthew G. Knepley   Output Parameter:
17849318fe57SMatthew G. Knepley . dm  - The DM object
17859318fe57SMatthew G. Knepley 
17869318fe57SMatthew G. Knepley   Level: beginner
17879318fe57SMatthew G. Knepley 
17889318fe57SMatthew G. Knepley .seealso: DMPlexCreateHexCylinderMesh(), DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
17899318fe57SMatthew G. Knepley @*/
17909318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateWedgeCylinderMesh(MPI_Comm comm, PetscInt n, PetscBool interpolate, DM *dm)
17919318fe57SMatthew G. Knepley {
17929318fe57SMatthew G. Knepley   PetscErrorCode ierr;
17939318fe57SMatthew G. Knepley 
17949318fe57SMatthew G. Knepley   PetscFunctionBegin;
17959318fe57SMatthew G. Knepley   PetscValidPointer(dm, 4);
17969318fe57SMatthew G. Knepley   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
17979318fe57SMatthew G. Knepley   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
17989318fe57SMatthew G. Knepley   ierr = DMPlexCreateWedgeCylinderMesh_Internal(*dm, n, interpolate);CHKERRQ(ierr);
179924119c2aSMatthew G. Knepley   PetscFunctionReturn(0);
180024119c2aSMatthew G. Knepley }
180124119c2aSMatthew G. Knepley 
18029fbee547SJacob Faibussowitsch static inline PetscReal DiffNormReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
180365a81367SMatthew G. Knepley {
180465a81367SMatthew G. Knepley   PetscReal prod = 0.0;
180565a81367SMatthew G. Knepley   PetscInt  i;
180665a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += PetscSqr(x[i] - y[i]);
180765a81367SMatthew G. Knepley   return PetscSqrtReal(prod);
180865a81367SMatthew G. Knepley }
18099fbee547SJacob Faibussowitsch static inline PetscReal DotReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
181065a81367SMatthew G. Knepley {
181165a81367SMatthew G. Knepley   PetscReal prod = 0.0;
181265a81367SMatthew G. Knepley   PetscInt  i;
181365a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += x[i]*y[i];
181465a81367SMatthew G. Knepley   return prod;
181565a81367SMatthew G. Knepley }
181665a81367SMatthew G. Knepley 
181751a74b61SMatthew G. Knepley /* The first constant is the sphere radius */
181851a74b61SMatthew G. Knepley static void snapToSphere(PetscInt dim, PetscInt Nf, PetscInt NfAux,
181951a74b61SMatthew G. Knepley                          const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
182051a74b61SMatthew G. Knepley                          const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
182151a74b61SMatthew G. Knepley                          PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
182251a74b61SMatthew G. Knepley {
182351a74b61SMatthew G. Knepley   PetscReal r = PetscRealPart(constants[0]);
182451a74b61SMatthew G. Knepley   PetscReal norm2 = 0.0, fac;
182551a74b61SMatthew G. Knepley   PetscInt  n = uOff[1] - uOff[0], d;
182651a74b61SMatthew G. Knepley 
182751a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) norm2 += PetscSqr(PetscRealPart(u[d]));
182851a74b61SMatthew G. Knepley   fac = r/PetscSqrtReal(norm2);
182951a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) f0[d] = u[d]*fac;
183051a74b61SMatthew G. Knepley }
183151a74b61SMatthew G. Knepley 
18329318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateSphereMesh_Internal(DM dm, PetscInt dim, PetscBool simplex, PetscReal R)
18332829fed8SMatthew G. Knepley {
183465a81367SMatthew G. Knepley   const PetscInt  embedDim = dim+1;
183565a81367SMatthew G. Knepley   PetscSection    coordSection;
183665a81367SMatthew G. Knepley   Vec             coordinates;
183765a81367SMatthew G. Knepley   PetscScalar    *coords;
183865a81367SMatthew G. Knepley   PetscReal      *coordsIn;
183965a81367SMatthew G. Knepley   PetscInt        numCells, numEdges, numVerts, firstVertex, v, firstEdge, coordSize, d, c, e;
184065a81367SMatthew G. Knepley   PetscMPIInt     rank;
184165a81367SMatthew G. Knepley   PetscErrorCode  ierr;
184265a81367SMatthew G. Knepley 
184365a81367SMatthew G. Knepley   PetscFunctionBegin;
18449318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveBool(dm, simplex, 3);
18459318fe57SMatthew G. Knepley   ierr = DMSetDimension(dm, dim);CHKERRQ(ierr);
18469318fe57SMatthew G. Knepley   ierr = DMSetCoordinateDim(dm, dim+1);CHKERRQ(ierr);
18479318fe57SMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRMPI(ierr);
184865a81367SMatthew G. Knepley   switch (dim) {
184965a81367SMatthew G. Knepley   case 2:
185065a81367SMatthew G. Knepley     if (simplex) {
185151a74b61SMatthew G. Knepley       const PetscReal radius    = PetscSqrtReal(1 + PETSC_PHI*PETSC_PHI)/(1.0 + PETSC_PHI);
185251a74b61SMatthew G. Knepley       const PetscReal edgeLen   = 2.0/(1.0 + PETSC_PHI) * (R/radius);
185365a81367SMatthew G. Knepley       const PetscInt  degree    = 5;
185451a74b61SMatthew G. Knepley       PetscReal       vertex[3] = {0.0, 1.0/(1.0 + PETSC_PHI), PETSC_PHI/(1.0 + PETSC_PHI)};
185565a81367SMatthew G. Knepley       PetscInt        s[3]      = {1, 1, 1};
185665a81367SMatthew G. Knepley       PetscInt        cone[3];
185765a81367SMatthew G. Knepley       PetscInt       *graph, p, i, j, k;
185865a81367SMatthew G. Knepley 
185951a74b61SMatthew G. Knepley       vertex[0] *= R/radius; vertex[1] *= R/radius; vertex[2] *= R/radius;
1860dd400576SPatrick Sanan       numCells    = rank == 0 ? 20 : 0;
1861dd400576SPatrick Sanan       numVerts    = rank == 0 ? 12 : 0;
186265a81367SMatthew G. Knepley       firstVertex = numCells;
186351a74b61SMatthew G. Knepley       /* Use icosahedron, which for a R-sphere has coordinates which are all cyclic permutations of
186465a81367SMatthew G. Knepley 
186565a81367SMatthew G. Knepley            (0, \pm 1/\phi+1, \pm \phi/\phi+1)
186665a81367SMatthew G. Knepley 
186765a81367SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
186851a74b61SMatthew G. Knepley          length is then given by 2/(1+\phi) = 2 * 0.38197 = 0.76393.
186965a81367SMatthew G. Knepley       */
187065a81367SMatthew G. Knepley       /* Construct vertices */
187165a81367SMatthew G. Knepley       ierr = PetscCalloc1(numVerts * embedDim, &coordsIn);CHKERRQ(ierr);
1872dd400576SPatrick Sanan       if (rank == 0) {
187365a81367SMatthew G. Knepley         for (p = 0, i = 0; p < embedDim; ++p) {
187465a81367SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
187565a81367SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
187665a81367SMatthew G. Knepley               for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[(d+p)%embedDim]*vertex[(d+p)%embedDim];
187765a81367SMatthew G. Knepley               ++i;
187865a81367SMatthew G. Knepley             }
187965a81367SMatthew G. Knepley           }
188065a81367SMatthew G. Knepley         }
188145da822fSValeria Barra       }
188265a81367SMatthew G. Knepley       /* Construct graph */
188365a81367SMatthew G. Knepley       ierr = PetscCalloc1(numVerts * numVerts, &graph);CHKERRQ(ierr);
188465a81367SMatthew G. Knepley       for (i = 0; i < numVerts; ++i) {
188565a81367SMatthew G. Knepley         for (j = 0, k = 0; j < numVerts; ++j) {
188665a81367SMatthew G. Knepley           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i*embedDim], &coordsIn[j*embedDim]) - edgeLen) < PETSC_SMALL) {graph[i*numVerts+j] = 1; ++k;}
188765a81367SMatthew G. Knepley         }
18882c71b3e2SJacob Faibussowitsch         PetscCheckFalse(k != degree,PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid icosahedron, vertex %D degree %D != %D", i, k, degree);
188965a81367SMatthew G. Knepley       }
189065a81367SMatthew G. Knepley       /* Build Topology */
18919318fe57SMatthew G. Knepley       ierr = DMPlexSetChart(dm, 0, numCells+numVerts);CHKERRQ(ierr);
189265a81367SMatthew G. Knepley       for (c = 0; c < numCells; c++) {
18939318fe57SMatthew G. Knepley         ierr = DMPlexSetConeSize(dm, c, embedDim);CHKERRQ(ierr);
189465a81367SMatthew G. Knepley       }
18959318fe57SMatthew G. Knepley       ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
189665a81367SMatthew G. Knepley       /* Cells */
189765a81367SMatthew G. Knepley       for (i = 0, c = 0; i < numVerts; ++i) {
189865a81367SMatthew G. Knepley         for (j = 0; j < i; ++j) {
189965a81367SMatthew G. Knepley           for (k = 0; k < j; ++k) {
190065a81367SMatthew G. Knepley             if (graph[i*numVerts+j] && graph[j*numVerts+k] && graph[k*numVerts+i]) {
190165a81367SMatthew G. Knepley               cone[0] = firstVertex+i; cone[1] = firstVertex+j; cone[2] = firstVertex+k;
190265a81367SMatthew G. Knepley               /* Check orientation */
190365a81367SMatthew G. Knepley               {
190465a81367SMatthew 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}}};
190565a81367SMatthew G. Knepley                 PetscReal normal[3];
190665a81367SMatthew G. Knepley                 PetscInt  e, f;
190765a81367SMatthew G. Knepley 
190865a81367SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) {
190965a81367SMatthew G. Knepley                   normal[d] = 0.0;
191065a81367SMatthew G. Knepley                   for (e = 0; e < embedDim; ++e) {
191165a81367SMatthew G. Knepley                     for (f = 0; f < embedDim; ++f) {
191265a81367SMatthew G. Knepley                       normal[d] += epsilon[d][e][f]*(coordsIn[j*embedDim+e] - coordsIn[i*embedDim+e])*(coordsIn[k*embedDim+f] - coordsIn[i*embedDim+f]);
191365a81367SMatthew G. Knepley                     }
191465a81367SMatthew G. Knepley                   }
191565a81367SMatthew G. Knepley                 }
191665a81367SMatthew G. Knepley                 if (DotReal(embedDim, normal, &coordsIn[i*embedDim]) < 0) {PetscInt tmp = cone[1]; cone[1] = cone[2]; cone[2] = tmp;}
191765a81367SMatthew G. Knepley               }
19189318fe57SMatthew G. Knepley               ierr = DMPlexSetCone(dm, c++, cone);CHKERRQ(ierr);
191965a81367SMatthew G. Knepley             }
192065a81367SMatthew G. Knepley           }
192165a81367SMatthew G. Knepley         }
192265a81367SMatthew G. Knepley       }
19239318fe57SMatthew G. Knepley       ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
19249318fe57SMatthew G. Knepley       ierr = DMPlexStratify(dm);CHKERRQ(ierr);
192565a81367SMatthew G. Knepley       ierr = PetscFree(graph);CHKERRQ(ierr);
192665a81367SMatthew G. Knepley     } else {
19272829fed8SMatthew G. Knepley       /*
19282829fed8SMatthew G. Knepley         12-21--13
19292829fed8SMatthew G. Knepley          |     |
19302829fed8SMatthew G. Knepley         25  4  24
19312829fed8SMatthew G. Knepley          |     |
19322829fed8SMatthew G. Knepley   12-25--9-16--8-24--13
19332829fed8SMatthew G. Knepley    |     |     |     |
19342829fed8SMatthew G. Knepley   23  5 17  0 15  3  22
19352829fed8SMatthew G. Knepley    |     |     |     |
19362829fed8SMatthew G. Knepley   10-20--6-14--7-19--11
19372829fed8SMatthew G. Knepley          |     |
19382829fed8SMatthew G. Knepley         20  1  19
19392829fed8SMatthew G. Knepley          |     |
19402829fed8SMatthew G. Knepley         10-18--11
19412829fed8SMatthew G. Knepley          |     |
19422829fed8SMatthew G. Knepley         23  2  22
19432829fed8SMatthew G. Knepley          |     |
19442829fed8SMatthew G. Knepley         12-21--13
19452829fed8SMatthew G. Knepley        */
19462829fed8SMatthew G. Knepley       PetscInt cone[4], ornt[4];
19472829fed8SMatthew G. Knepley 
1948dd400576SPatrick Sanan       numCells    = rank == 0 ?  6 : 0;
1949dd400576SPatrick Sanan       numEdges    = rank == 0 ? 12 : 0;
1950dd400576SPatrick Sanan       numVerts    = rank == 0 ?  8 : 0;
195165a81367SMatthew G. Knepley       firstVertex = numCells;
195265a81367SMatthew G. Knepley       firstEdge   = numCells + numVerts;
19532829fed8SMatthew G. Knepley       /* Build Topology */
19549318fe57SMatthew G. Knepley       ierr = DMPlexSetChart(dm, 0, numCells+numEdges+numVerts);CHKERRQ(ierr);
19552829fed8SMatthew G. Knepley       for (c = 0; c < numCells; c++) {
19569318fe57SMatthew G. Knepley         ierr = DMPlexSetConeSize(dm, c, 4);CHKERRQ(ierr);
19572829fed8SMatthew G. Knepley       }
19582829fed8SMatthew G. Knepley       for (e = firstEdge; e < firstEdge+numEdges; ++e) {
19599318fe57SMatthew G. Knepley         ierr = DMPlexSetConeSize(dm, e, 2);CHKERRQ(ierr);
19602829fed8SMatthew G. Knepley       }
19619318fe57SMatthew G. Knepley       ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
1962dd400576SPatrick Sanan       if (rank == 0) {
19632829fed8SMatthew G. Knepley         /* Cell 0 */
19642829fed8SMatthew G. Knepley         cone[0] = 14; cone[1] = 15; cone[2] = 16; cone[3] = 17;
19659318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 0, cone);CHKERRQ(ierr);
19662829fed8SMatthew G. Knepley         ornt[0] = 0; ornt[1] = 0; ornt[2] = 0; ornt[3] = 0;
19679318fe57SMatthew G. Knepley         ierr = DMPlexSetConeOrientation(dm, 0, ornt);CHKERRQ(ierr);
19682829fed8SMatthew G. Knepley         /* Cell 1 */
19692829fed8SMatthew G. Knepley         cone[0] = 18; cone[1] = 19; cone[2] = 14; cone[3] = 20;
19709318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 1, cone);CHKERRQ(ierr);
1971b5a892a1SMatthew G. Knepley         ornt[0] = 0; ornt[1] = 0; ornt[2] = -1; ornt[3] = 0;
19729318fe57SMatthew G. Knepley         ierr = DMPlexSetConeOrientation(dm, 1, ornt);CHKERRQ(ierr);
19732829fed8SMatthew G. Knepley         /* Cell 2 */
19742829fed8SMatthew G. Knepley         cone[0] = 21; cone[1] = 22; cone[2] = 18; cone[3] = 23;
19759318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 2, cone);CHKERRQ(ierr);
1976b5a892a1SMatthew G. Knepley         ornt[0] = 0; ornt[1] = 0; ornt[2] = -1; ornt[3] = 0;
19779318fe57SMatthew G. Knepley         ierr = DMPlexSetConeOrientation(dm, 2, ornt);CHKERRQ(ierr);
19782829fed8SMatthew G. Knepley         /* Cell 3 */
19792829fed8SMatthew G. Knepley         cone[0] = 19; cone[1] = 22; cone[2] = 24; cone[3] = 15;
19809318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 3, cone);CHKERRQ(ierr);
1981b5a892a1SMatthew G. Knepley         ornt[0] = -1; ornt[1] = -1; ornt[2] = 0; ornt[3] = -1;
19829318fe57SMatthew G. Knepley         ierr = DMPlexSetConeOrientation(dm, 3, ornt);CHKERRQ(ierr);
19832829fed8SMatthew G. Knepley         /* Cell 4 */
19842829fed8SMatthew G. Knepley         cone[0] = 16; cone[1] = 24; cone[2] = 21; cone[3] = 25;
19859318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 4, cone);CHKERRQ(ierr);
1986b5a892a1SMatthew G. Knepley         ornt[0] = -1; ornt[1] = -1; ornt[2] = -1; ornt[3] = 0;
19879318fe57SMatthew G. Knepley         ierr = DMPlexSetConeOrientation(dm, 4, ornt);CHKERRQ(ierr);
19882829fed8SMatthew G. Knepley         /* Cell 5 */
19892829fed8SMatthew G. Knepley         cone[0] = 20; cone[1] = 17; cone[2] = 25; cone[3] = 23;
19909318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 5, cone);CHKERRQ(ierr);
1991b5a892a1SMatthew G. Knepley         ornt[0] = -1; ornt[1] = -1; ornt[2] = -1; ornt[3] = -1;
19929318fe57SMatthew G. Knepley         ierr = DMPlexSetConeOrientation(dm, 5, ornt);CHKERRQ(ierr);
19932829fed8SMatthew G. Knepley         /* Edges */
19942829fed8SMatthew G. Knepley         cone[0] =  6; cone[1] =  7;
19959318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 14, cone);CHKERRQ(ierr);
19962829fed8SMatthew G. Knepley         cone[0] =  7; cone[1] =  8;
19979318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 15, cone);CHKERRQ(ierr);
19982829fed8SMatthew G. Knepley         cone[0] =  8; cone[1] =  9;
19999318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 16, cone);CHKERRQ(ierr);
20002829fed8SMatthew G. Knepley         cone[0] =  9; cone[1] =  6;
20019318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 17, cone);CHKERRQ(ierr);
20022829fed8SMatthew G. Knepley         cone[0] = 10; cone[1] = 11;
20039318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 18, cone);CHKERRQ(ierr);
20042829fed8SMatthew G. Knepley         cone[0] = 11; cone[1] =  7;
20059318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 19, cone);CHKERRQ(ierr);
20062829fed8SMatthew G. Knepley         cone[0] =  6; cone[1] = 10;
20079318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 20, cone);CHKERRQ(ierr);
20082829fed8SMatthew G. Knepley         cone[0] = 12; cone[1] = 13;
20099318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 21, cone);CHKERRQ(ierr);
20102829fed8SMatthew G. Knepley         cone[0] = 13; cone[1] = 11;
20119318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 22, cone);CHKERRQ(ierr);
20122829fed8SMatthew G. Knepley         cone[0] = 10; cone[1] = 12;
20139318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 23, cone);CHKERRQ(ierr);
20142829fed8SMatthew G. Knepley         cone[0] = 13; cone[1] =  8;
20159318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 24, cone);CHKERRQ(ierr);
20162829fed8SMatthew G. Knepley         cone[0] = 12; cone[1] =  9;
20179318fe57SMatthew G. Knepley         ierr = DMPlexSetCone(dm, 25, cone);CHKERRQ(ierr);
201845da822fSValeria Barra       }
20199318fe57SMatthew G. Knepley       ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
20209318fe57SMatthew G. Knepley       ierr = DMPlexStratify(dm);CHKERRQ(ierr);
20212829fed8SMatthew G. Knepley       /* Build coordinates */
202265a81367SMatthew G. Knepley       ierr = PetscCalloc1(numVerts * embedDim, &coordsIn);CHKERRQ(ierr);
2023dd400576SPatrick Sanan       if (rank == 0) {
202451a74b61SMatthew G. Knepley         coordsIn[0*embedDim+0] = -R; coordsIn[0*embedDim+1] =  R; coordsIn[0*embedDim+2] = -R;
202551a74b61SMatthew G. Knepley         coordsIn[1*embedDim+0] =  R; coordsIn[1*embedDim+1] =  R; coordsIn[1*embedDim+2] = -R;
202651a74b61SMatthew G. Knepley         coordsIn[2*embedDim+0] =  R; coordsIn[2*embedDim+1] = -R; coordsIn[2*embedDim+2] = -R;
202751a74b61SMatthew G. Knepley         coordsIn[3*embedDim+0] = -R; coordsIn[3*embedDim+1] = -R; coordsIn[3*embedDim+2] = -R;
202851a74b61SMatthew G. Knepley         coordsIn[4*embedDim+0] = -R; coordsIn[4*embedDim+1] =  R; coordsIn[4*embedDim+2] =  R;
202951a74b61SMatthew G. Knepley         coordsIn[5*embedDim+0] =  R; coordsIn[5*embedDim+1] =  R; coordsIn[5*embedDim+2] =  R;
203051a74b61SMatthew G. Knepley         coordsIn[6*embedDim+0] = -R; coordsIn[6*embedDim+1] = -R; coordsIn[6*embedDim+2] =  R;
203151a74b61SMatthew G. Knepley         coordsIn[7*embedDim+0] =  R; coordsIn[7*embedDim+1] = -R; coordsIn[7*embedDim+2] =  R;
203265a81367SMatthew G. Knepley       }
203345da822fSValeria Barra     }
203465a81367SMatthew G. Knepley     break;
203565a81367SMatthew G. Knepley   case 3:
2036116ded15SMatthew G. Knepley     if (simplex) {
2037116ded15SMatthew G. Knepley       const PetscReal edgeLen         = 1.0/PETSC_PHI;
203851a74b61SMatthew G. Knepley       PetscReal       vertexA[4]      = {0.5, 0.5, 0.5, 0.5};
203951a74b61SMatthew G. Knepley       PetscReal       vertexB[4]      = {1.0, 0.0, 0.0, 0.0};
204051a74b61SMatthew G. Knepley       PetscReal       vertexC[4]      = {0.5, 0.5*PETSC_PHI, 0.5/PETSC_PHI, 0.0};
2041116ded15SMatthew G. Knepley       const PetscInt  degree          = 12;
2042116ded15SMatthew G. Knepley       PetscInt        s[4]            = {1, 1, 1};
2043116ded15SMatthew 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},
2044116ded15SMatthew 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}};
2045116ded15SMatthew G. Knepley       PetscInt        cone[4];
2046116ded15SMatthew G. Knepley       PetscInt       *graph, p, i, j, k, l;
2047116ded15SMatthew G. Knepley 
204851a74b61SMatthew G. Knepley       vertexA[0] *= R; vertexA[1] *= R; vertexA[2] *= R; vertexA[3] *= R;
204951a74b61SMatthew G. Knepley       vertexB[0] *= R; vertexB[1] *= R; vertexB[2] *= R; vertexB[3] *= R;
205051a74b61SMatthew G. Knepley       vertexC[0] *= R; vertexC[1] *= R; vertexC[2] *= R; vertexC[3] *= R;
2051dd400576SPatrick Sanan       numCells    = rank == 0 ? 600 : 0;
2052dd400576SPatrick Sanan       numVerts    = rank == 0 ? 120 : 0;
2053116ded15SMatthew G. Knepley       firstVertex = numCells;
2054116ded15SMatthew G. Knepley       /* Use the 600-cell, which for a unit sphere has coordinates which are
2055116ded15SMatthew G. Knepley 
2056116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm 1,    \pm 1, \pm 1)                          16
2057116ded15SMatthew G. Knepley                (\pm 1,    0,       0,      0)  all cyclic permutations   8
2058116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm phi, \pm 1/phi, 0)  all even permutations    96
2059116ded15SMatthew G. Knepley 
2060116ded15SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
20616333ae4fSvaleriabarra          length is then given by 1/\phi = 0.61803.
2062116ded15SMatthew G. Knepley 
2063116ded15SMatthew G. Knepley          http://buzzard.pugetsound.edu/sage-practice/ch03s03.html
2064116ded15SMatthew G. Knepley          http://mathworld.wolfram.com/600-Cell.html
2065116ded15SMatthew G. Knepley       */
2066116ded15SMatthew G. Knepley       /* Construct vertices */
2067116ded15SMatthew G. Knepley       ierr = PetscCalloc1(numVerts * embedDim, &coordsIn);CHKERRQ(ierr);
2068116ded15SMatthew G. Knepley       i    = 0;
2069dd400576SPatrick Sanan       if (rank == 0) {
2070116ded15SMatthew G. Knepley         for (s[0] = -1; s[0] < 2; s[0] += 2) {
2071116ded15SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
2072116ded15SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
2073116ded15SMatthew G. Knepley               for (s[3] = -1; s[3] < 2; s[3] += 2) {
2074116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[d]*vertexA[d];
2075116ded15SMatthew G. Knepley                 ++i;
2076116ded15SMatthew G. Knepley               }
2077116ded15SMatthew G. Knepley             }
2078116ded15SMatthew G. Knepley           }
2079116ded15SMatthew G. Knepley         }
2080116ded15SMatthew G. Knepley         for (p = 0; p < embedDim; ++p) {
2081116ded15SMatthew G. Knepley           s[1] = s[2] = s[3] = 1;
2082116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
2083116ded15SMatthew G. Knepley             for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[(d+p)%embedDim]*vertexB[(d+p)%embedDim];
2084116ded15SMatthew G. Knepley             ++i;
2085116ded15SMatthew G. Knepley           }
2086116ded15SMatthew G. Knepley         }
2087116ded15SMatthew G. Knepley         for (p = 0; p < 12; ++p) {
2088116ded15SMatthew G. Knepley           s[3] = 1;
2089116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
2090116ded15SMatthew G. Knepley             for (s[1] = -1; s[1] < 2; s[1] += 2) {
2091116ded15SMatthew G. Knepley               for (s[2] = -1; s[2] < 2; s[2] += 2) {
2092116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[evenPerm[p][d]]*vertexC[evenPerm[p][d]];
2093116ded15SMatthew G. Knepley                 ++i;
2094116ded15SMatthew G. Knepley               }
2095116ded15SMatthew G. Knepley             }
2096116ded15SMatthew G. Knepley           }
2097116ded15SMatthew G. Knepley         }
209845da822fSValeria Barra       }
20992c71b3e2SJacob Faibussowitsch       PetscCheckFalse(i != numVerts,PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertices %D != %D", i, numVerts);
2100116ded15SMatthew G. Knepley       /* Construct graph */
2101116ded15SMatthew G. Knepley       ierr = PetscCalloc1(numVerts * numVerts, &graph);CHKERRQ(ierr);
2102116ded15SMatthew G. Knepley       for (i = 0; i < numVerts; ++i) {
2103116ded15SMatthew G. Knepley         for (j = 0, k = 0; j < numVerts; ++j) {
2104116ded15SMatthew G. Knepley           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i*embedDim], &coordsIn[j*embedDim]) - edgeLen) < PETSC_SMALL) {graph[i*numVerts+j] = 1; ++k;}
2105116ded15SMatthew G. Knepley         }
21062c71b3e2SJacob Faibussowitsch         PetscCheckFalse(k != degree,PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertex %D degree %D != %D", i, k, degree);
2107116ded15SMatthew G. Knepley       }
2108116ded15SMatthew G. Knepley       /* Build Topology */
21099318fe57SMatthew G. Knepley       ierr = DMPlexSetChart(dm, 0, numCells+numVerts);CHKERRQ(ierr);
2110116ded15SMatthew G. Knepley       for (c = 0; c < numCells; c++) {
21119318fe57SMatthew G. Knepley         ierr = DMPlexSetConeSize(dm, c, embedDim);CHKERRQ(ierr);
2112116ded15SMatthew G. Knepley       }
21139318fe57SMatthew G. Knepley       ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
2114116ded15SMatthew G. Knepley       /* Cells */
2115dd400576SPatrick Sanan       if (rank == 0) {
2116116ded15SMatthew G. Knepley         for (i = 0, c = 0; i < numVerts; ++i) {
2117116ded15SMatthew G. Knepley           for (j = 0; j < i; ++j) {
2118116ded15SMatthew G. Knepley             for (k = 0; k < j; ++k) {
2119116ded15SMatthew G. Knepley               for (l = 0; l < k; ++l) {
2120116ded15SMatthew G. Knepley                 if (graph[i*numVerts+j] && graph[j*numVerts+k] && graph[k*numVerts+i] &&
2121116ded15SMatthew G. Knepley                     graph[l*numVerts+i] && graph[l*numVerts+j] && graph[l*numVerts+k]) {
2122116ded15SMatthew G. Knepley                   cone[0] = firstVertex+i; cone[1] = firstVertex+j; cone[2] = firstVertex+k; cone[3] = firstVertex+l;
2123116ded15SMatthew G. Knepley                   /* Check orientation: https://ef.gy/linear-algebra:normal-vectors-in-higher-dimensional-spaces */
2124116ded15SMatthew G. Knepley                   {
2125116ded15SMatthew 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}},
2126116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  1}, { 0,  0, -1, 0}},
2127116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  0, -1}, { 0,  0, 0,  0}, { 0,  1,  0, 0}},
2128116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  1,  0}, { 0, -1, 0,  0}, { 0,  0,  0, 0}}},
2129116ded15SMatthew G. Knepley 
2130116ded15SMatthew G. Knepley                                                           {{{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0, -1}, { 0,  0,  1, 0}},
2131116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
2132116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  1}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, {-1,  0,  0, 0}},
2133116ded15SMatthew G. Knepley                                                            {{0,  0, -1,  0}, { 0, 0,  0,  0}, { 1,  0, 0,  0}, { 0,  0,  0, 0}}},
2134116ded15SMatthew G. Knepley 
2135116ded15SMatthew G. Knepley                                                           {{{0,  0,  0,  0}, { 0, 0,  0,  1}, { 0,  0, 0,  0}, { 0, -1,  0, 0}},
2136116ded15SMatthew G. Knepley                                                            {{0,  0,  0, -1}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 1,  0,  0, 0}},
2137116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
2138116ded15SMatthew G. Knepley                                                            {{0,  1,  0,  0}, {-1, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}}},
2139116ded15SMatthew G. Knepley 
2140116ded15SMatthew G. Knepley                                                           {{{0,  0,  0,  0}, { 0, 0, -1,  0}, { 0,  1, 0,  0}, { 0,  0,  0, 0}},
2141116ded15SMatthew G. Knepley                                                            {{0,  0,  1,  0}, { 0, 0,  0,  0}, {-1,  0, 0,  0}, { 0,  0,  0, 0}},
2142116ded15SMatthew G. Knepley                                                            {{0, -1,  0,  0}, { 1, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
2143116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}}}};
2144116ded15SMatthew G. Knepley                     PetscReal normal[4];
2145116ded15SMatthew G. Knepley                     PetscInt  e, f, g;
2146116ded15SMatthew G. Knepley 
2147116ded15SMatthew G. Knepley                     for (d = 0; d < embedDim; ++d) {
2148116ded15SMatthew G. Knepley                       normal[d] = 0.0;
2149116ded15SMatthew G. Knepley                       for (e = 0; e < embedDim; ++e) {
2150116ded15SMatthew G. Knepley                         for (f = 0; f < embedDim; ++f) {
2151116ded15SMatthew G. Knepley                           for (g = 0; g < embedDim; ++g) {
2152116ded15SMatthew 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]);
2153116ded15SMatthew G. Knepley                           }
2154116ded15SMatthew G. Knepley                         }
2155116ded15SMatthew G. Knepley                       }
2156116ded15SMatthew G. Knepley                     }
2157116ded15SMatthew G. Knepley                     if (DotReal(embedDim, normal, &coordsIn[i*embedDim]) < 0) {PetscInt tmp = cone[1]; cone[1] = cone[2]; cone[2] = tmp;}
2158116ded15SMatthew G. Knepley                   }
21599318fe57SMatthew G. Knepley                   ierr = DMPlexSetCone(dm, c++, cone);CHKERRQ(ierr);
2160116ded15SMatthew G. Knepley                 }
2161116ded15SMatthew G. Knepley               }
2162116ded15SMatthew G. Knepley             }
2163116ded15SMatthew G. Knepley           }
2164116ded15SMatthew G. Knepley         }
216545da822fSValeria Barra       }
21669318fe57SMatthew G. Knepley       ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
21679318fe57SMatthew G. Knepley       ierr = DMPlexStratify(dm);CHKERRQ(ierr);
2168116ded15SMatthew G. Knepley       ierr = PetscFree(graph);CHKERRQ(ierr);
2169116ded15SMatthew G. Knepley       break;
2170116ded15SMatthew G. Knepley     }
217198921bdaSJacob Faibussowitsch   default: SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Unsupported dimension for sphere: %D", dim);
217265a81367SMatthew G. Knepley   }
217365a81367SMatthew G. Knepley   /* Create coordinates */
21749318fe57SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
21752829fed8SMatthew G. Knepley   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
21762829fed8SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(coordSection, 0, embedDim);CHKERRQ(ierr);
21772829fed8SMatthew G. Knepley   ierr = PetscSectionSetChart(coordSection, firstVertex, firstVertex+numVerts);CHKERRQ(ierr);
21782829fed8SMatthew G. Knepley   for (v = firstVertex; v < firstVertex+numVerts; ++v) {
21792829fed8SMatthew G. Knepley     ierr = PetscSectionSetDof(coordSection, v, embedDim);CHKERRQ(ierr);
21802829fed8SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(coordSection, v, 0, embedDim);CHKERRQ(ierr);
21812829fed8SMatthew G. Knepley   }
21822829fed8SMatthew G. Knepley   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
21832829fed8SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
21842829fed8SMatthew G. Knepley   ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
21852829fed8SMatthew G. Knepley   ierr = VecSetBlockSize(coordinates, embedDim);CHKERRQ(ierr);
21862829fed8SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
21872829fed8SMatthew G. Knepley   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
21882829fed8SMatthew G. Knepley   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
21892829fed8SMatthew G. Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
219065a81367SMatthew G. Knepley   for (v = 0; v < numVerts; ++v) for (d = 0; d < embedDim; ++d) {coords[v*embedDim+d] = coordsIn[v*embedDim+d];}
21912829fed8SMatthew G. Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
21929318fe57SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
21932829fed8SMatthew G. Knepley   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
219465a81367SMatthew G. Knepley   ierr = PetscFree(coordsIn);CHKERRQ(ierr);
219551a74b61SMatthew G. Knepley   {
219651a74b61SMatthew G. Knepley     DM          cdm;
219751a74b61SMatthew G. Knepley     PetscDS     cds;
21989318fe57SMatthew G. Knepley     PetscScalar c = R;
219951a74b61SMatthew G. Knepley 
22009318fe57SMatthew G. Knepley     ierr = DMPlexCreateCoordinateSpace(dm, 1, snapToSphere);CHKERRQ(ierr);
22019318fe57SMatthew G. Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
220251a74b61SMatthew G. Knepley     ierr = DMGetDS(cdm, &cds);CHKERRQ(ierr);
22039318fe57SMatthew G. Knepley     ierr = PetscDSSetConstants(cds, 1, &c);CHKERRQ(ierr);
220451a74b61SMatthew G. Knepley   }
22059318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
22069318fe57SMatthew G. Knepley   if (simplex) {ierr = DMPlexInterpolateInPlace_Internal(dm);CHKERRQ(ierr);}
22079318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
22089318fe57SMatthew G. Knepley }
22099318fe57SMatthew G. Knepley 
2210b7f5c055SJed Brown typedef void (*TPSEvaluateFunc)(const PetscReal[], PetscReal*, PetscReal[], PetscReal(*)[3]);
2211b7f5c055SJed Brown 
2212b7f5c055SJed Brown /*
2213b7f5c055SJed Brown  The Schwarz P implicit surface is
2214b7f5c055SJed Brown 
2215b7f5c055SJed Brown      f(x) = cos(x0) + cos(x1) + cos(x2) = 0
2216b7f5c055SJed Brown */
2217b7f5c055SJed Brown static void TPSEvaluate_SchwarzP(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3])
2218b7f5c055SJed Brown {
2219b7f5c055SJed Brown   PetscReal c[3] = {PetscCosReal(y[0] * PETSC_PI), PetscCosReal(y[1] * PETSC_PI), PetscCosReal(y[2] * PETSC_PI)};
2220b7f5c055SJed Brown   PetscReal g[3] = {-PetscSinReal(y[0] * PETSC_PI), -PetscSinReal(y[1] * PETSC_PI), -PetscSinReal(y[2] * PETSC_PI)};
2221b7f5c055SJed Brown   f[0] = c[0] + c[1] + c[2];
2222b7f5c055SJed Brown   for (PetscInt i=0; i<3; i++) {
2223b7f5c055SJed Brown     grad[i] = PETSC_PI * g[i];
2224b7f5c055SJed Brown     for (PetscInt j=0; j<3; j++) {
2225b7f5c055SJed Brown       hess[i][j] = (i == j) ? -PetscSqr(PETSC_PI) * c[i] : 0.;
2226b7f5c055SJed Brown     }
2227b7f5c055SJed Brown   }
2228b7f5c055SJed Brown }
2229b7f5c055SJed Brown 
2230b7f5c055SJed Brown /*
2231b7f5c055SJed Brown  The Gyroid implicit surface is
2232b7f5c055SJed Brown 
2233b7f5c055SJed Brown  f(x,y,z) = sin(pi * x) * cos (pi * (y + 1/2))  + sin(pi * (y + 1/2)) * cos(pi * (z + 1/4)) + sin(pi * (z + 1/4)) * cos(pi * x)
2234b7f5c055SJed Brown 
2235b7f5c055SJed Brown */
2236b7f5c055SJed Brown static void TPSEvaluate_Gyroid(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3])
2237b7f5c055SJed Brown {
2238b7f5c055SJed Brown   PetscReal s[3] = {PetscSinReal(PETSC_PI * y[0]), PetscSinReal(PETSC_PI * (y[1] + .5)), PetscSinReal(PETSC_PI * (y[2] + .25))};
2239b7f5c055SJed Brown   PetscReal c[3] = {PetscCosReal(PETSC_PI * y[0]), PetscCosReal(PETSC_PI * (y[1] + .5)), PetscCosReal(PETSC_PI * (y[2] + .25))};
2240b7f5c055SJed Brown   f[0] = s[0] * c[1] + s[1] * c[2] + s[2] * c[0];
2241b7f5c055SJed Brown   grad[0] = PETSC_PI * (c[0] * c[1] - s[2] * s[0]);
2242b7f5c055SJed Brown   grad[1] = PETSC_PI * (c[1] * c[2] - s[0] * s[1]);
2243b7f5c055SJed Brown   grad[2] = PETSC_PI * (c[2] * c[0] - s[1] * s[2]);
2244b7f5c055SJed Brown   hess[0][0] = -PetscSqr(PETSC_PI) * (s[0] * c[1] + s[2] * c[0]);
2245b7f5c055SJed Brown   hess[0][1] = -PetscSqr(PETSC_PI) * (c[0] * s[1]);
2246b7f5c055SJed Brown   hess[0][2] = -PetscSqr(PETSC_PI) * (c[2] * s[0]);
2247b7f5c055SJed Brown   hess[1][0] = -PetscSqr(PETSC_PI) * (s[1] * c[2] + s[0] * c[1]);
2248b7f5c055SJed Brown   hess[1][1] = -PetscSqr(PETSC_PI) * (c[1] * s[2]);
2249b7f5c055SJed Brown   hess[2][2] = -PetscSqr(PETSC_PI) * (c[0] * s[1]);
2250b7f5c055SJed Brown   hess[2][0] = -PetscSqr(PETSC_PI) * (s[2] * c[0] + s[1] * c[2]);
2251b7f5c055SJed Brown   hess[2][1] = -PetscSqr(PETSC_PI) * (c[2] * s[0]);
2252b7f5c055SJed Brown   hess[2][2] = -PetscSqr(PETSC_PI) * (c[1] * s[2]);
2253b7f5c055SJed Brown }
2254b7f5c055SJed Brown 
2255b7f5c055SJed Brown /*
2256b7f5c055SJed Brown    We wish to solve
2257b7f5c055SJed Brown 
2258b7f5c055SJed Brown          min_y || y - x ||^2  subject to f(y) = 0
2259b7f5c055SJed Brown 
2260b7f5c055SJed Brown    Let g(y) = grad(f).  The minimization problem is equivalent to asking to satisfy
2261b7f5c055SJed Brown    f(y) = 0 and (y-x) is parallel to g(y).  We do this by using Householder QR to obtain a basis for the
2262b7f5c055SJed Brown    tangent space and ask for both components in the tangent space to be zero.
2263b7f5c055SJed Brown 
2264b7f5c055SJed Brown    Take g to be a column vector and compute the "full QR" factorization Q R = g,
2265b7f5c055SJed Brown    where Q = I - 2 n n^T is a symmetric orthogonal matrix.
2266b7f5c055SJed Brown    The first column of Q is parallel to g so the remaining two columns span the null space.
2267b7f5c055SJed Brown    Let Qn = Q[:,1:] be those remaining columns.  Then Qn Qn^T is an orthogonal projector into the tangent space.
2268b7f5c055SJed Brown    Since Q is symmetric, this is equivalent to multipyling by Q and taking the last two entries.
2269b7f5c055SJed Brown    In total, we have a system of 3 equations in 3 unknowns:
2270b7f5c055SJed Brown 
2271b7f5c055SJed Brown      f(y) = 0                       1 equation
2272b7f5c055SJed Brown      Qn^T (y - x) = 0               2 equations
2273b7f5c055SJed Brown 
2274b7f5c055SJed Brown    Here, we compute the residual and Jacobian of this system.
2275b7f5c055SJed Brown */
2276b7f5c055SJed Brown static void TPSNearestPointResJac(TPSEvaluateFunc feval, const PetscScalar x[], const PetscScalar y[], PetscScalar res[], PetscScalar J[])
2277b7f5c055SJed Brown {
2278b7f5c055SJed Brown   PetscReal yreal[3] = {PetscRealPart(y[0]), PetscRealPart(y[1]), PetscRealPart(y[2])};
2279b7f5c055SJed Brown   PetscReal d[3] = {PetscRealPart(y[0] - x[0]), PetscRealPart(y[1] - x[1]), PetscRealPart(y[2] - x[2])};
2280b7f5c055SJed Brown   PetscReal f, grad[3], n[3], n_y[3][3], norm, norm_y[3], nd, nd_y[3], sign;
2281b7f5c055SJed Brown 
2282b7f5c055SJed Brown   feval(yreal, &f, grad, n_y);
2283b7f5c055SJed Brown 
2284b7f5c055SJed Brown   for (PetscInt i=0; i<3; i++) n[i] = grad[i];
2285b7f5c055SJed Brown   norm = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2]));
2286b7f5c055SJed Brown   for (PetscInt i=0; i<3; i++) {
2287b7f5c055SJed Brown     norm_y[i] = 1. / norm * n[i] * n_y[i][i];
2288b7f5c055SJed Brown   }
2289b7f5c055SJed Brown 
2290b7f5c055SJed Brown   // Define the Householder reflector
2291b7f5c055SJed Brown   sign = n[0] >= 0 ? 1. : -1.;
2292b7f5c055SJed Brown   n[0] += norm * sign;
2293b7f5c055SJed Brown   for (PetscInt i=0; i<3; i++) n_y[0][i] += norm_y[i] * sign;
2294b7f5c055SJed Brown 
2295b7f5c055SJed Brown   norm = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2]));
2296b7f5c055SJed Brown   norm_y[0] = 1. / norm * (n[0] * n_y[0][0]);
2297b7f5c055SJed Brown   norm_y[1] = 1. / norm * (n[0] * n_y[0][1] + n[1] * n_y[1][1]);
2298b7f5c055SJed Brown   norm_y[2] = 1. / norm * (n[0] * n_y[0][2] + n[2] * n_y[2][2]);
2299b7f5c055SJed Brown 
2300b7f5c055SJed Brown   for (PetscInt i=0; i<3; i++) {
2301b7f5c055SJed Brown     n[i] /= norm;
2302b7f5c055SJed Brown     for (PetscInt j=0; j<3; j++) {
2303b7f5c055SJed Brown       // note that n[i] is n_old[i]/norm when executing the code below
2304b7f5c055SJed Brown       n_y[i][j] = n_y[i][j] / norm - n[i] / norm * norm_y[j];
2305b7f5c055SJed Brown     }
2306b7f5c055SJed Brown   }
2307b7f5c055SJed Brown 
2308b7f5c055SJed Brown   nd = n[0] * d[0] + n[1] * d[1] + n[2] * d[2];
2309b7f5c055SJed Brown   for (PetscInt i=0; i<3; i++) nd_y[i] = n[i] + n_y[0][i] * d[0] + n_y[1][i] * d[1] + n_y[2][i] * d[2];
2310b7f5c055SJed Brown 
2311b7f5c055SJed Brown   res[0] = f;
2312b7f5c055SJed Brown   res[1] = d[1] - 2 * n[1] * nd;
2313b7f5c055SJed Brown   res[2] = d[2] - 2 * n[2] * nd;
2314b7f5c055SJed Brown   // J[j][i] is J_{ij} (column major)
2315b7f5c055SJed Brown   for (PetscInt j=0; j<3; j++) {
2316b7f5c055SJed Brown     J[0 + j*3] = grad[j];
2317b7f5c055SJed Brown     J[1 + j*3] = (j == 1)*1. - 2 * (n_y[1][j] * nd + n[1] * nd_y[j]);
2318b7f5c055SJed Brown     J[2 + j*3] = (j == 2)*1. - 2 * (n_y[2][j] * nd + n[2] * nd_y[j]);
2319b7f5c055SJed Brown   }
2320b7f5c055SJed Brown }
2321b7f5c055SJed Brown 
2322b7f5c055SJed Brown /*
2323b7f5c055SJed Brown    Project x to the nearest point on the implicit surface using Newton's method.
2324b7f5c055SJed Brown */
2325b7f5c055SJed Brown static PetscErrorCode TPSNearestPoint(TPSEvaluateFunc feval, PetscScalar x[])
2326b7f5c055SJed Brown {
2327b7f5c055SJed Brown   PetscScalar y[3] = {x[0], x[1], x[2]}; // Initial guess
2328b7f5c055SJed Brown   PetscErrorCode ierr;
2329b7f5c055SJed Brown 
2330b7f5c055SJed Brown   PetscFunctionBegin;
2331b7f5c055SJed Brown   for (PetscInt iter=0; iter<10; iter++) {
2332b7f5c055SJed Brown     PetscScalar res[3], J[9];
2333b7f5c055SJed Brown     PetscReal resnorm;
2334b7f5c055SJed Brown     TPSNearestPointResJac(feval, x, y, res, J);
2335b7f5c055SJed Brown     resnorm = PetscSqrtReal(PetscSqr(PetscRealPart(res[0])) + PetscSqr(PetscRealPart(res[1])) + PetscSqr(PetscRealPart(res[2])));
2336b7f5c055SJed Brown     if (0) { // Turn on this monitor if you need to confirm quadratic convergence
2337b7f5c055SJed Brown       ierr = PetscPrintf(PETSC_COMM_SELF, "[%D] res [%g %g %g]\n", iter, PetscRealPart(res[0]), PetscRealPart(res[1]), PetscRealPart(res[2]));CHKERRQ(ierr);
2338b7f5c055SJed Brown     }
2339b7f5c055SJed Brown     if (resnorm < PETSC_SMALL) break;
2340b7f5c055SJed Brown 
2341b7f5c055SJed Brown     // Take the Newton step
2342b7f5c055SJed Brown     ierr = PetscKernel_A_gets_inverse_A_3(J, 0., PETSC_FALSE, NULL);CHKERRQ(ierr);
2343b7f5c055SJed Brown     PetscKernel_v_gets_v_minus_A_times_w_3(y, J, res);
2344b7f5c055SJed Brown   }
2345b7f5c055SJed Brown   for (PetscInt i=0; i<3; i++) x[i] = y[i];
2346b7f5c055SJed Brown   PetscFunctionReturn(0);
2347b7f5c055SJed Brown }
2348b7f5c055SJed Brown 
2349b7f5c055SJed Brown const char *const DMPlexTPSTypes[] = {"SCHWARZ_P", "GYROID", "DMPlexTPSType", "DMPLEX_TPS_", NULL};
2350b7f5c055SJed Brown 
23511436d7faSJed Brown static PetscErrorCode DMPlexCreateTPSMesh_Internal(DM dm, DMPlexTPSType tpstype, const PetscInt extent[], const DMBoundaryType periodic[], PetscBool tps_distribute, PetscInt refinements, PetscInt layers, PetscReal thickness)
2352b7f5c055SJed Brown {
2353b7f5c055SJed Brown   PetscErrorCode ierr;
2354b7f5c055SJed Brown   PetscMPIInt rank;
2355b7f5c055SJed Brown   PetscInt topoDim = 2, spaceDim = 3, numFaces = 0, numVertices = 0, numEdges = 0;
2356b7f5c055SJed Brown   PetscInt (*edges)[2] = NULL, *edgeSets = NULL;
2357b7f5c055SJed Brown   PetscInt *cells_flat = NULL;
2358b7f5c055SJed Brown   PetscReal *vtxCoords = NULL;
2359b7f5c055SJed Brown   TPSEvaluateFunc evalFunc = NULL;
2360b7f5c055SJed Brown   DMLabel label;
2361b7f5c055SJed Brown 
2362b7f5c055SJed Brown   PetscFunctionBegin;
2363b7f5c055SJed Brown   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank);CHKERRMPI(ierr);
2364b7f5c055SJed Brown   PetscCheck((layers != 0) ^ (thickness == 0.), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_INCOMP, "Layers %D must be nonzero iff thickness %g is nonzero", layers, (double)thickness);
2365b7f5c055SJed Brown   switch (tpstype) {
2366b7f5c055SJed Brown   case DMPLEX_TPS_SCHWARZ_P:
2367b7f5c055SJed Brown     PetscCheck(!periodic || (periodic[0] == DM_BOUNDARY_NONE && periodic[1] == DM_BOUNDARY_NONE && periodic[2] == DM_BOUNDARY_NONE), PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Schwarz P does not support periodic meshes");
2368b7f5c055SJed Brown     if (!rank) {
2369b7f5c055SJed Brown       PetscInt (*cells)[6][4][4] = NULL; // [junction, junction-face, cell, conn]
2370b7f5c055SJed Brown       PetscInt Njunctions = 0, Ncuts = 0, Npipes[3], vcount;
2371b7f5c055SJed Brown       PetscReal L = 1;
2372b7f5c055SJed Brown 
2373b7f5c055SJed Brown       Npipes[0] = (extent[0] + 1) * extent[1] * extent[2];
2374b7f5c055SJed Brown       Npipes[1] = extent[0] * (extent[1] + 1) * extent[2];
2375b7f5c055SJed Brown       Npipes[2] = extent[0] * extent[1] * (extent[2] + 1);
2376b7f5c055SJed Brown       Njunctions = extent[0] * extent[1] * extent[2];
2377b7f5c055SJed Brown       Ncuts = 2 * (extent[0] * extent[1] + extent[1] * extent[2] + extent[2] * extent[0]);
2378b7f5c055SJed Brown       numVertices = 4 * (Npipes[0] + Npipes[1] + Npipes[2]) + 8 * Njunctions;
2379b7f5c055SJed Brown       ierr = PetscMalloc1(3*numVertices, &vtxCoords);CHKERRQ(ierr);
2380b7f5c055SJed Brown       ierr = PetscMalloc1(Njunctions, &cells);CHKERRQ(ierr);
2381b7f5c055SJed Brown       ierr = PetscMalloc1(Ncuts*4, &edges);CHKERRQ(ierr);
2382b7f5c055SJed Brown       ierr = PetscMalloc1(Ncuts*4, &edgeSets);CHKERRQ(ierr);
2383b7f5c055SJed Brown       // x-normal pipes
2384b7f5c055SJed Brown       vcount = 0;
2385b7f5c055SJed Brown       for (PetscInt i=0; i<extent[0]+1; i++) {
2386b7f5c055SJed Brown         for (PetscInt j=0; j<extent[1]; j++) {
2387b7f5c055SJed Brown           for (PetscInt k=0; k<extent[2]; k++) {
2388b7f5c055SJed Brown             for (PetscInt l=0; l<4; l++) {
2389b7f5c055SJed Brown               vtxCoords[vcount++] = (2*i - 1) * L;
2390b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * j * L + PetscCosReal((2*l + 1) * PETSC_PI / 4) * L / 2;
2391b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * k * L + PetscSinReal((2*l + 1) * PETSC_PI / 4) * L / 2;
2392b7f5c055SJed Brown             }
2393b7f5c055SJed Brown           }
2394b7f5c055SJed Brown         }
2395b7f5c055SJed Brown       }
2396b7f5c055SJed Brown       // y-normal pipes
2397b7f5c055SJed Brown       for (PetscInt i=0; i<extent[0]; i++) {
2398b7f5c055SJed Brown         for (PetscInt j=0; j<extent[1]+1; j++) {
2399b7f5c055SJed Brown           for (PetscInt k=0; k<extent[2]; k++) {
2400b7f5c055SJed Brown             for (PetscInt l=0; l<4; l++) {
2401b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * i * L + PetscSinReal((2*l + 1) * PETSC_PI / 4) * L / 2;
2402b7f5c055SJed Brown               vtxCoords[vcount++] = (2*j - 1) * L;
2403b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * k * L + PetscCosReal((2*l + 1) * PETSC_PI / 4) * L / 2;
2404b7f5c055SJed Brown             }
2405b7f5c055SJed Brown           }
2406b7f5c055SJed Brown         }
2407b7f5c055SJed Brown       }
2408b7f5c055SJed Brown       // z-normal pipes
2409b7f5c055SJed Brown       for (PetscInt i=0; i<extent[0]; i++) {
2410b7f5c055SJed Brown         for (PetscInt j=0; j<extent[1]; j++) {
2411b7f5c055SJed Brown           for (PetscInt k=0; k<extent[2]+1; k++) {
2412b7f5c055SJed Brown             for (PetscInt l=0; l<4; l++) {
2413b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * i * L + PetscCosReal((2*l + 1) * PETSC_PI / 4) * L / 2;
2414b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * j * L + PetscSinReal((2*l + 1) * PETSC_PI / 4) * L / 2;
2415b7f5c055SJed Brown               vtxCoords[vcount++] = (2*k - 1) * L;
2416b7f5c055SJed Brown             }
2417b7f5c055SJed Brown           }
2418b7f5c055SJed Brown         }
2419b7f5c055SJed Brown       }
2420b7f5c055SJed Brown       // junctions
2421b7f5c055SJed Brown       for (PetscInt i=0; i<extent[0]; i++) {
2422b7f5c055SJed Brown         for (PetscInt j=0; j<extent[1]; j++) {
2423b7f5c055SJed Brown           for (PetscInt k=0; k<extent[2]; k++) {
2424b7f5c055SJed Brown             const PetscInt J = (i*extent[1] + j)*extent[2] + k, Jvoff = (Npipes[0] + Npipes[1] + Npipes[2])*4 + J*8;
2425b7f5c055SJed Brown             PetscCheck(vcount / 3 == Jvoff, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unexpected vertex count");
2426b7f5c055SJed Brown             for (PetscInt ii=0; ii<2; ii++) {
2427b7f5c055SJed Brown               for (PetscInt jj=0; jj<2; jj++) {
2428b7f5c055SJed Brown                 for (PetscInt kk=0; kk<2; kk++) {
2429b7f5c055SJed Brown                   double Ls = (1 - sqrt(2) / 4) * L;
2430b7f5c055SJed Brown                   vtxCoords[vcount++] = 2*i*L + (2*ii-1) * Ls;
2431b7f5c055SJed Brown                   vtxCoords[vcount++] = 2*j*L + (2*jj-1) * Ls;
2432b7f5c055SJed Brown                   vtxCoords[vcount++] = 2*k*L + (2*kk-1) * Ls;
2433b7f5c055SJed Brown                 }
2434b7f5c055SJed Brown               }
2435b7f5c055SJed Brown             }
2436b7f5c055SJed Brown             const PetscInt jfaces[3][2][4] = {
2437b7f5c055SJed Brown               {{3,1,0,2}, {7,5,4,6}}, // x-aligned
2438b7f5c055SJed Brown               {{5,4,0,1}, {7,6,2,3}}, // y-aligned
2439b7f5c055SJed Brown               {{6,2,0,4}, {7,3,1,5}}  // z-aligned
2440b7f5c055SJed Brown             };
2441b7f5c055SJed Brown             const PetscInt pipe_lo[3] = { // vertex numbers of pipes
2442b7f5c055SJed Brown               ((i * extent[1] + j) * extent[2] + k)*4,
2443b7f5c055SJed Brown               ((i * (extent[1] + 1) + j) * extent[2] + k + Npipes[0])*4,
2444b7f5c055SJed Brown               ((i * extent[1] + j) * (extent[2]+1) + k + Npipes[0] + Npipes[1])*4
2445b7f5c055SJed Brown             };
2446b7f5c055SJed Brown             const PetscInt pipe_hi[3] = { // vertex numbers of pipes
2447b7f5c055SJed Brown               (((i + 1) * extent[1] + j) * extent[2] + k)*4,
2448b7f5c055SJed Brown               ((i * (extent[1] + 1) + j + 1) * extent[2] + k + Npipes[0])*4,
2449b7f5c055SJed Brown               ((i * extent[1] + j) * (extent[2]+1) + k + 1 + Npipes[0] + Npipes[1])*4
2450b7f5c055SJed Brown             };
2451b7f5c055SJed Brown             for (PetscInt dir=0; dir<3; dir++) { // x,y,z
2452b7f5c055SJed Brown               const PetscInt ijk[3] = {i, j, k};
2453b7f5c055SJed Brown               for (PetscInt l=0; l<4; l++) { // rotations
2454b7f5c055SJed Brown                 cells[J][dir*2+0][l][0] = pipe_lo[dir] + l;
2455b7f5c055SJed Brown                 cells[J][dir*2+0][l][1] = Jvoff + jfaces[dir][0][l];
2456b7f5c055SJed Brown                 cells[J][dir*2+0][l][2] = Jvoff + jfaces[dir][0][(l-1+4)%4];
2457b7f5c055SJed Brown                 cells[J][dir*2+0][l][3] = pipe_lo[dir] + (l-1+4)%4;
2458b7f5c055SJed Brown                 cells[J][dir*2+1][l][0] = Jvoff + jfaces[dir][1][l];
2459b7f5c055SJed Brown                 cells[J][dir*2+1][l][1] = pipe_hi[dir] + l;
2460b7f5c055SJed Brown                 cells[J][dir*2+1][l][2] = pipe_hi[dir] + (l-1+4)%4;
2461b7f5c055SJed Brown                 cells[J][dir*2+1][l][3] = Jvoff + jfaces[dir][1][(l-1+4)%4];
2462b7f5c055SJed Brown                 if (ijk[dir] == 0) {
2463b7f5c055SJed Brown                   edges[numEdges][0] = pipe_lo[dir] + l;
2464b7f5c055SJed Brown                   edges[numEdges][1] = pipe_lo[dir] + (l+1) % 4;
2465b7f5c055SJed Brown                   edgeSets[numEdges] = dir*2 + 1;
2466b7f5c055SJed Brown                   numEdges++;
2467b7f5c055SJed Brown                 }
2468b7f5c055SJed Brown                 if (ijk[dir] + 1 == extent[dir]) {
2469b7f5c055SJed Brown                   edges[numEdges][0] = pipe_hi[dir] + l;
2470b7f5c055SJed Brown                   edges[numEdges][1] = pipe_hi[dir] + (l+1) % 4;
2471b7f5c055SJed Brown                   edgeSets[numEdges] = dir*2 + 2;
2472b7f5c055SJed Brown                   numEdges++;
2473b7f5c055SJed Brown                 }
2474b7f5c055SJed Brown               }
2475b7f5c055SJed Brown             }
2476b7f5c055SJed Brown           }
2477b7f5c055SJed Brown         }
2478b7f5c055SJed Brown       }
2479b7f5c055SJed Brown       PetscCheck(numEdges == Ncuts * 4, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Edge count %D incompatible with number of cuts %D", numEdges, Ncuts);
2480b7f5c055SJed Brown       numFaces = 24 * Njunctions;
2481b7f5c055SJed Brown       cells_flat = cells[0][0][0];
2482b7f5c055SJed Brown     }
2483b7f5c055SJed Brown     evalFunc = TPSEvaluate_SchwarzP;
2484b7f5c055SJed Brown     break;
2485b7f5c055SJed Brown   case DMPLEX_TPS_GYROID:
2486b7f5c055SJed Brown     if (!rank) {
2487b7f5c055SJed Brown       // This is a coarse mesh approximation of the gyroid shifted to being the zero of the level set
2488b7f5c055SJed Brown       //
2489b7f5c055SJed Brown       //     sin(pi*x)*cos(pi*(y+1/2)) + sin(pi*(y+1/2))*cos(pi*(z+1/4)) + sin(pi*(z+1/4))*cos(x)
2490b7f5c055SJed Brown       //
2491b7f5c055SJed Brown       // on the cell [0,2]^3.
2492b7f5c055SJed Brown       //
2493b7f5c055SJed Brown       // Think about dividing that cell into four columns, and focus on the column [0,1]x[0,1]x[0,2].
2494b7f5c055SJed Brown       // If you looked at the gyroid in that column at different slices of z you would see that it kind of spins
2495b7f5c055SJed Brown       // like a boomerang:
2496b7f5c055SJed Brown       //
2497b7f5c055SJed Brown       //     z = 0          z = 1/4        z = 1/2        z = 3/4     //
2498b7f5c055SJed Brown       //     -----          -------        -------        -------     //
2499b7f5c055SJed Brown       //                                                              //
2500b7f5c055SJed Brown       //     +       +      +       +      +       +      +   \   +   //
2501b7f5c055SJed Brown       //      \                                   /            \      //
2502b7f5c055SJed Brown       //       \            `-_   _-'            /              }     //
2503b7f5c055SJed Brown       //        *-_            `-'            _-'              /      //
2504b7f5c055SJed Brown       //     +     `-+      +       +      +-'     +      +   /   +   //
2505b7f5c055SJed Brown       //                                                              //
2506b7f5c055SJed Brown       //                                                              //
2507b7f5c055SJed Brown       //     z = 1          z = 5/4        z = 3/2        z = 7/4     //
2508b7f5c055SJed Brown       //     -----          -------        -------        -------     //
2509b7f5c055SJed Brown       //                                                              //
2510b7f5c055SJed Brown       //     +-_     +      +       +      +     _-+      +   /   +   //
2511b7f5c055SJed Brown       //        `-_            _-_            _-`            /        //
2512b7f5c055SJed Brown       //           \        _-'   `-_        /              {         //
2513b7f5c055SJed Brown       //            \                       /                \        //
2514b7f5c055SJed Brown       //     +       +      +       +      +       +      +   \   +   //
2515b7f5c055SJed Brown       //
2516b7f5c055SJed Brown       //
2517b7f5c055SJed Brown       // This course mesh approximates each of these slices by two line segments,
2518b7f5c055SJed Brown       // and then connects the segments in consecutive layers with quadrilateral faces.
2519b7f5c055SJed Brown       // All of the end points of the segments are multiples of 1/4 except for the
2520b7f5c055SJed Brown       // point * in the picture for z = 0 above and the similar points in other layers.
2521b7f5c055SJed Brown       // That point is at (gamma, gamma, 0), where gamma is calculated below.
2522b7f5c055SJed Brown       //
2523b7f5c055SJed Brown       // The column  [1,2]x[1,2]x[0,2] looks the same as this column;
2524b7f5c055SJed Brown       // The columns [1,2]x[0,1]x[0,2] and [0,1]x[1,2]x[0,2] are mirror images.
2525b7f5c055SJed Brown       //
2526b7f5c055SJed Brown       // As for how this method turned into the names given to the vertices:
2527b7f5c055SJed Brown       // that was not systematic, it was just the way it worked out in my handwritten notes.
2528b7f5c055SJed Brown 
2529b7f5c055SJed Brown       PetscInt facesPerBlock = 64;
2530b7f5c055SJed Brown       PetscInt vertsPerBlock = 56;
2531b7f5c055SJed Brown       PetscInt extentPlus[3];
2532b7f5c055SJed Brown       PetscInt numBlocks, numBlocksPlus;
2533b7f5c055SJed Brown       const PetscInt A =  0,   B =  1,   C =  2,   D =  3,   E =  4,   F =  5,   G =  6,   H =  7,
2534b7f5c055SJed Brown         II =  8,   J =  9,   K = 10,   L = 11,   M = 12,   N = 13,   O = 14,   P = 15,
2535b7f5c055SJed Brown         Q = 16,   R = 17,   S = 18,   T = 19,   U = 20,   V = 21,   W = 22,   X = 23,
2536b7f5c055SJed Brown         Y = 24,   Z = 25,  Ap = 26,  Bp = 27,  Cp = 28,  Dp = 29,  Ep = 30,  Fp = 31,
2537b7f5c055SJed Brown         Gp = 32,  Hp = 33,  Ip = 34,  Jp = 35,  Kp = 36,  Lp = 37,  Mp = 38,  Np = 39,
2538b7f5c055SJed Brown         Op = 40,  Pp = 41,  Qp = 42,  Rp = 43,  Sp = 44,  Tp = 45,  Up = 46,  Vp = 47,
2539b7f5c055SJed Brown         Wp = 48,  Xp = 49,  Yp = 50,  Zp = 51,  Aq = 52,  Bq = 53,  Cq = 54,  Dq = 55;
2540b7f5c055SJed Brown       const PetscInt pattern[64][4] =
2541b7f5c055SJed Brown         { /* face to vertex within the coarse discretization of a single gyroid block */
2542b7f5c055SJed Brown           /* layer 0 */
2543b7f5c055SJed Brown           {A,C,K,G},{C,B,II,K},{D,A,H,L},{B+56*1,D,L,J},{E,B+56*1,J,N},{A+56*2,E,N,H+56*2},{F,A+56*2,G+56*2,M},{B,F,M,II},
2544b7f5c055SJed Brown           /* layer 1 */
2545b7f5c055SJed Brown           {G,K,Q,O},{K,II,P,Q},{L,H,O+56*1,R},{J,L,R,P},{N,J,P,S},{H+56*2,N,S,O+56*3},{M,G+56*2,O+56*2,T},{II,M,T,P},
2546b7f5c055SJed Brown           /* layer 2 */
2547b7f5c055SJed Brown           {O,Q,Y,U},{Q,P,W,Y},{R,O+56*1,U+56*1,Ap},{P,R,Ap,W},{S,P,X,Bp},{O+56*3,S,Bp,V+56*1},{T,O+56*2,V,Z},{P,T,Z,X},
2548b7f5c055SJed Brown           /* layer 3 */
2549b7f5c055SJed Brown           {U,Y,Ep,Dp},{Y,W,Cp,Ep},{Ap,U+56*1,Dp+56*1,Gp},{W,Ap,Gp,Cp},{Bp,X,Cp+56*2,Fp},{V+56*1,Bp,Fp,Dp+56*1},{Z,V,Dp,Hp},{X,Z,Hp,Cp+56*2},
2550b7f5c055SJed Brown           /* layer 4 */
2551b7f5c055SJed Brown           {Dp,Ep,Mp,Kp},{Ep,Cp,Ip,Mp},{Gp,Dp+56*1,Lp,Np},{Cp,Gp,Np,Jp},{Fp,Cp+56*2,Jp+56*2,Pp},{Dp+56*1,Fp,Pp,Lp},{Hp,Dp,Kp,Op},{Cp+56*2,Hp,Op,Ip+56*2},
2552b7f5c055SJed Brown           /* layer 5 */
2553b7f5c055SJed Brown           {Kp,Mp,Sp,Rp},{Mp,Ip,Qp,Sp},{Np,Lp,Rp,Tp},{Jp,Np,Tp,Qp+56*1},{Pp,Jp+56*2,Qp+56*3,Up},{Lp,Pp,Up,Rp},{Op,Kp,Rp,Vp},{Ip+56*2,Op,Vp,Qp+56*2},
2554b7f5c055SJed Brown           /* layer 6 */
2555b7f5c055SJed Brown           {Rp,Sp,Aq,Yp},{Sp,Qp,Wp,Aq},{Tp,Rp,Yp,Cq},{Qp+56*1,Tp,Cq,Wp+56*1},{Up,Qp+56*3,Xp+56*1,Dq},{Rp,Up,Dq,Zp},{Vp,Rp,Zp,Bq},{Qp+56*2,Vp,Bq,Xp},
2556b7f5c055SJed Brown           /* layer 7 (the top is the periodic image of the bottom of layer 0) */
2557b7f5c055SJed Brown           {Yp,Aq,C+56*4,A+56*4},{Aq,Wp,B+56*4,C+56*4},{Cq,Yp,A+56*4,D+56*4},{Wp+56*1,Cq,D+56*4,B+56*5},{Dq,Xp+56*1,B+56*5,E+56*4},{Zp,Dq,E+56*4,A+56*6},{Bq,Zp,A+56*6,F+56*4},{Xp,Bq,F+56*4,B+56*4}
2558b7f5c055SJed Brown         };
2559b7f5c055SJed Brown       const PetscReal gamma = PetscAcosReal((PetscSqrtReal(3.)-1.) / PetscSqrtReal(2.)) / PETSC_PI;
2560b7f5c055SJed Brown       const PetscReal patternCoords[56][3] =
2561b7f5c055SJed Brown         {
2562b7f5c055SJed Brown           /* A  */ {1.,0.,0.},
2563b7f5c055SJed Brown           /* B  */ {0.,1.,0.},
2564b7f5c055SJed Brown           /* C  */ {gamma,gamma,0.},
2565b7f5c055SJed Brown           /* D  */ {1+gamma,1-gamma,0.},
2566b7f5c055SJed Brown           /* E  */ {2-gamma,2-gamma,0.},
2567b7f5c055SJed Brown           /* F  */ {1-gamma,1+gamma,0.},
2568b7f5c055SJed Brown 
2569b7f5c055SJed Brown           /* G  */ {.5,0,.25},
2570b7f5c055SJed Brown           /* H  */ {1.5,0.,.25},
2571b7f5c055SJed Brown           /* II */ {.5,1.,.25},
2572b7f5c055SJed Brown           /* J  */ {1.5,1.,.25},
2573b7f5c055SJed Brown           /* K  */ {.25,.5,.25},
2574b7f5c055SJed Brown           /* L  */ {1.25,.5,.25},
2575b7f5c055SJed Brown           /* M  */ {.75,1.5,.25},
2576b7f5c055SJed Brown           /* N  */ {1.75,1.5,.25},
2577b7f5c055SJed Brown 
2578b7f5c055SJed Brown           /* O  */ {0.,0.,.5},
2579b7f5c055SJed Brown           /* P  */ {1.,1.,.5},
2580b7f5c055SJed Brown           /* Q  */ {gamma,1-gamma,.5},
2581b7f5c055SJed Brown           /* R  */ {1+gamma,gamma,.5},
2582b7f5c055SJed Brown           /* S  */ {2-gamma,1+gamma,.5},
2583b7f5c055SJed Brown           /* T  */ {1-gamma,2-gamma,.5},
2584b7f5c055SJed Brown 
2585b7f5c055SJed Brown           /* U  */ {0.,.5,.75},
2586b7f5c055SJed Brown           /* V  */ {0.,1.5,.75},
2587b7f5c055SJed Brown           /* W  */ {1.,.5,.75},
2588b7f5c055SJed Brown           /* X  */ {1.,1.5,.75},
2589b7f5c055SJed Brown           /* Y  */ {.5,.75,.75},
2590b7f5c055SJed Brown           /* Z  */ {.5,1.75,.75},
2591b7f5c055SJed Brown           /* Ap */ {1.5,.25,.75},
2592b7f5c055SJed Brown           /* Bp */ {1.5,1.25,.75},
2593b7f5c055SJed Brown 
2594b7f5c055SJed Brown           /* Cp */ {1.,0.,1.},
2595b7f5c055SJed Brown           /* Dp */ {0.,1.,1.},
2596b7f5c055SJed Brown           /* Ep */ {1-gamma,1-gamma,1.},
2597b7f5c055SJed Brown           /* Fp */ {1+gamma,1+gamma,1.},
2598b7f5c055SJed Brown           /* Gp */ {2-gamma,gamma,1.},
2599b7f5c055SJed Brown           /* Hp */ {gamma,2-gamma,1.},
2600b7f5c055SJed Brown 
2601b7f5c055SJed Brown           /* Ip */ {.5,0.,1.25},
2602b7f5c055SJed Brown           /* Jp */ {1.5,0.,1.25},
2603b7f5c055SJed Brown           /* Kp */ {.5,1.,1.25},
2604b7f5c055SJed Brown           /* Lp */ {1.5,1.,1.25},
2605b7f5c055SJed Brown           /* Mp */ {.75,.5,1.25},
2606b7f5c055SJed Brown           /* Np */ {1.75,.5,1.25},
2607b7f5c055SJed Brown           /* Op */ {.25,1.5,1.25},
2608b7f5c055SJed Brown           /* Pp */ {1.25,1.5,1.25},
2609b7f5c055SJed Brown 
2610b7f5c055SJed Brown           /* Qp */ {0.,0.,1.5},
2611b7f5c055SJed Brown           /* Rp */ {1.,1.,1.5},
2612b7f5c055SJed Brown           /* Sp */ {1-gamma,gamma,1.5},
2613b7f5c055SJed Brown           /* Tp */ {2-gamma,1-gamma,1.5},
2614b7f5c055SJed Brown           /* Up */ {1+gamma,2-gamma,1.5},
2615b7f5c055SJed Brown           /* Vp */ {gamma,1+gamma,1.5},
2616b7f5c055SJed Brown 
2617b7f5c055SJed Brown           /* Wp */ {0.,.5,1.75},
2618b7f5c055SJed Brown           /* Xp */ {0.,1.5,1.75},
2619b7f5c055SJed Brown           /* Yp */ {1.,.5,1.75},
2620b7f5c055SJed Brown           /* Zp */ {1.,1.5,1.75},
2621b7f5c055SJed Brown           /* Aq */ {.5,.25,1.75},
2622b7f5c055SJed Brown           /* Bq */ {.5,1.25,1.75},
2623b7f5c055SJed Brown           /* Cq */ {1.5,.75,1.75},
2624b7f5c055SJed Brown           /* Dq */ {1.5,1.75,1.75},
2625b7f5c055SJed Brown         };
2626b7f5c055SJed Brown       PetscInt  (*cells)[64][4] = NULL;
2627b7f5c055SJed Brown       PetscBool *seen;
2628b7f5c055SJed Brown       PetscInt  *vertToTrueVert;
2629b7f5c055SJed Brown       PetscInt  count;
2630b7f5c055SJed Brown 
2631b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) extentPlus[i]  = extent[i] + 1;
2632b7f5c055SJed Brown       numBlocks = 1;
2633b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++)     numBlocks *= extent[i];
2634b7f5c055SJed Brown       numBlocksPlus = 1;
2635b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) numBlocksPlus *= extentPlus[i];
2636b7f5c055SJed Brown       numFaces = numBlocks * facesPerBlock;
2637b7f5c055SJed Brown       ierr = PetscMalloc1(numBlocks, &cells);CHKERRQ(ierr);
2638b7f5c055SJed Brown       ierr = PetscCalloc1(numBlocksPlus * vertsPerBlock,&seen);CHKERRQ(ierr);
2639b7f5c055SJed Brown       for (PetscInt k = 0; k < extent[2]; k++) {
2640b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
2641b7f5c055SJed Brown           for (PetscInt i = 0; i < extent[0]; i++) {
2642b7f5c055SJed Brown             for (PetscInt f = 0; f < facesPerBlock; f++) {
2643b7f5c055SJed Brown               for (PetscInt v = 0; v < 4; v++) {
2644b7f5c055SJed Brown                 PetscInt vertRaw = pattern[f][v];
2645b7f5c055SJed Brown                 PetscInt blockidx = vertRaw / 56;
2646b7f5c055SJed Brown                 PetscInt patternvert = vertRaw % 56;
2647b7f5c055SJed Brown                 PetscInt xplus = (blockidx & 1);
2648b7f5c055SJed Brown                 PetscInt yplus = (blockidx & 2) >> 1;
2649b7f5c055SJed Brown                 PetscInt zplus = (blockidx & 4) >> 2;
2650b7f5c055SJed Brown                 PetscInt zcoord = (periodic && periodic[2] == DM_BOUNDARY_PERIODIC) ? ((k + zplus) % extent[2]) : (k + zplus);
2651b7f5c055SJed Brown                 PetscInt ycoord = (periodic && periodic[1] == DM_BOUNDARY_PERIODIC) ? ((j + yplus) % extent[1]) : (j + yplus);
2652b7f5c055SJed Brown                 PetscInt xcoord = (periodic && periodic[0] == DM_BOUNDARY_PERIODIC) ? ((i + xplus) % extent[0]) : (i + xplus);
2653b7f5c055SJed Brown                 PetscInt vert = ((zcoord * extentPlus[1] + ycoord) * extentPlus[0] + xcoord) * 56 + patternvert;
2654b7f5c055SJed Brown 
2655b7f5c055SJed Brown                 cells[(k * extent[1] + j) * extent[0] + i][f][v] = vert;
2656b7f5c055SJed Brown                 seen[vert] = PETSC_TRUE;
2657b7f5c055SJed Brown               }
2658b7f5c055SJed Brown             }
2659b7f5c055SJed Brown           }
2660b7f5c055SJed Brown         }
2661b7f5c055SJed Brown       }
2662b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++) if (seen[i]) numVertices++;
2663b7f5c055SJed Brown       count = 0;
2664b7f5c055SJed Brown       ierr = PetscMalloc1(numBlocksPlus * vertsPerBlock, &vertToTrueVert);CHKERRQ(ierr);
2665b7f5c055SJed Brown       ierr = PetscMalloc1(numVertices * 3, &vtxCoords);CHKERRQ(ierr);
2666b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++) vertToTrueVert[i] = -1;
2667b7f5c055SJed Brown       for (PetscInt k = 0; k < extentPlus[2]; k++) {
2668b7f5c055SJed Brown         for (PetscInt j = 0; j < extentPlus[1]; j++) {
2669b7f5c055SJed Brown           for (PetscInt i = 0; i < extentPlus[0]; i++) {
2670b7f5c055SJed Brown             for (PetscInt v = 0; v < vertsPerBlock; v++) {
2671b7f5c055SJed Brown               PetscInt vIdx = ((k * extentPlus[1] + j) * extentPlus[0] + i) * vertsPerBlock + v;
2672b7f5c055SJed Brown 
2673b7f5c055SJed Brown               if (seen[vIdx]) {
2674b7f5c055SJed Brown                 PetscInt thisVert;
2675b7f5c055SJed Brown 
2676b7f5c055SJed Brown                 vertToTrueVert[vIdx] = thisVert = count++;
2677b7f5c055SJed Brown 
2678b7f5c055SJed Brown                 for (PetscInt d = 0; d < 3; d++) vtxCoords[3 * thisVert + d] = patternCoords[v][d];
2679b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 0] += i * 2;
2680b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 1] += j * 2;
2681b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 2] += k * 2;
2682b7f5c055SJed Brown               }
2683b7f5c055SJed Brown             }
2684b7f5c055SJed Brown           }
2685b7f5c055SJed Brown         }
2686b7f5c055SJed Brown       }
2687b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocks; i++) {
2688b7f5c055SJed Brown         for (PetscInt f = 0; f < facesPerBlock; f++) {
2689b7f5c055SJed Brown           for (PetscInt v = 0; v < 4; v++) {
2690b7f5c055SJed Brown             cells[i][f][v] = vertToTrueVert[cells[i][f][v]];
2691b7f5c055SJed Brown           }
2692b7f5c055SJed Brown         }
2693b7f5c055SJed Brown       }
2694b7f5c055SJed Brown       ierr = PetscFree(vertToTrueVert);CHKERRQ(ierr);
2695b7f5c055SJed Brown       ierr = PetscFree(seen);CHKERRQ(ierr);
2696b7f5c055SJed Brown       cells_flat = cells[0][0];
2697b7f5c055SJed Brown       numEdges = 0;
2698b7f5c055SJed Brown       for (PetscInt i = 0; i < numFaces; i++) {
2699b7f5c055SJed Brown         for (PetscInt e = 0; e < 4; e++) {
2700b7f5c055SJed Brown           PetscInt ev[] = {cells_flat[i*4 + e], cells_flat[i*4 + ((e+1)%4)]};
2701b7f5c055SJed Brown           const PetscReal *evCoords[] = {&vtxCoords[3*ev[0]], &vtxCoords[3*ev[1]]};
2702b7f5c055SJed Brown 
2703b7f5c055SJed Brown           for (PetscInt d = 0; d < 3; d++) {
2704b7f5c055SJed Brown             if (!periodic || periodic[0] != DM_BOUNDARY_PERIODIC) {
2705b7f5c055SJed Brown               if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) numEdges++;
2706b7f5c055SJed Brown               if (evCoords[0][d] == 2.*extent[d] && evCoords[1][d] == 2.*extent[d]) numEdges++;
2707b7f5c055SJed Brown             }
2708b7f5c055SJed Brown           }
2709b7f5c055SJed Brown         }
2710b7f5c055SJed Brown       }
2711b7f5c055SJed Brown       ierr = PetscMalloc1(numEdges, &edges);CHKERRQ(ierr);
2712b7f5c055SJed Brown       ierr = PetscMalloc1(numEdges, &edgeSets);CHKERRQ(ierr);
2713b7f5c055SJed Brown       for (PetscInt edge = 0, i = 0; i < numFaces; i++) {
2714b7f5c055SJed Brown         for (PetscInt e = 0; e < 4; e++) {
2715b7f5c055SJed Brown           PetscInt ev[] = {cells_flat[i*4 + e], cells_flat[i*4 + ((e+1)%4)]};
2716b7f5c055SJed Brown           const PetscReal *evCoords[] = {&vtxCoords[3*ev[0]], &vtxCoords[3*ev[1]]};
2717b7f5c055SJed Brown 
2718b7f5c055SJed Brown           for (PetscInt d = 0; d < 3; d++) {
2719b7f5c055SJed Brown             if (!periodic || periodic[d] != DM_BOUNDARY_PERIODIC) {
2720b7f5c055SJed Brown               if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) {
2721b7f5c055SJed Brown                 edges[edge][0] = ev[0];
2722b7f5c055SJed Brown                 edges[edge][1] = ev[1];
2723b7f5c055SJed Brown                 edgeSets[edge++] = 2 * d;
2724b7f5c055SJed Brown               }
2725b7f5c055SJed Brown               if (evCoords[0][d] == 2.*extent[d] && evCoords[1][d] == 2.*extent[d]) {
2726b7f5c055SJed Brown                 edges[edge][0] = ev[0];
2727b7f5c055SJed Brown                 edges[edge][1] = ev[1];
2728b7f5c055SJed Brown                 edgeSets[edge++] = 2 * d + 1;
2729b7f5c055SJed Brown               }
2730b7f5c055SJed Brown             }
2731b7f5c055SJed Brown           }
2732b7f5c055SJed Brown         }
2733b7f5c055SJed Brown       }
2734b7f5c055SJed Brown     }
2735b7f5c055SJed Brown     evalFunc = TPSEvaluate_Gyroid;
2736b7f5c055SJed Brown     break;
2737b7f5c055SJed Brown   }
2738b7f5c055SJed Brown 
2739b7f5c055SJed Brown   ierr = DMSetDimension(dm, topoDim);CHKERRQ(ierr);
2740b7f5c055SJed Brown   if (!rank) {ierr = DMPlexBuildFromCellList(dm, numFaces, numVertices, 4, cells_flat);CHKERRQ(ierr);}
2741b7f5c055SJed Brown   else       {ierr = DMPlexBuildFromCellList(dm, 0, 0, 0, NULL);CHKERRQ(ierr);}
2742b7f5c055SJed Brown   ierr = PetscFree(cells_flat);CHKERRQ(ierr);
2743b7f5c055SJed Brown   {
2744b7f5c055SJed Brown     DM idm;
2745b7f5c055SJed Brown     ierr = DMPlexInterpolate(dm, &idm);CHKERRQ(ierr);
2746b7f5c055SJed Brown     ierr = DMPlexReplace_Static(dm, &idm);CHKERRQ(ierr);
2747b7f5c055SJed Brown   }
2748b7f5c055SJed Brown   if (!rank) {ierr = DMPlexBuildCoordinatesFromCellList(dm, spaceDim, vtxCoords);CHKERRQ(ierr);}
2749b7f5c055SJed Brown   else       {ierr = DMPlexBuildCoordinatesFromCellList(dm, spaceDim, NULL);CHKERRQ(ierr);}
2750b7f5c055SJed Brown   ierr = PetscFree(vtxCoords);CHKERRQ(ierr);
2751b7f5c055SJed Brown 
2752b7f5c055SJed Brown   ierr = DMCreateLabel(dm, "Face Sets");CHKERRQ(ierr);
2753b7f5c055SJed Brown   ierr = DMGetLabel(dm, "Face Sets", &label);CHKERRQ(ierr);
2754b7f5c055SJed Brown   for (PetscInt e=0; e<numEdges; e++) {
2755b7f5c055SJed Brown     PetscInt njoin;
2756b7f5c055SJed Brown     const PetscInt *join, verts[] = {numFaces + edges[e][0], numFaces + edges[e][1]};
2757b7f5c055SJed Brown     ierr = DMPlexGetJoin(dm, 2, verts, &njoin, &join);CHKERRQ(ierr);
2758b7f5c055SJed Brown     PetscCheck(njoin == 1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Expected unique join of vertices %D and %D", edges[e][0], edges[e][1]);
2759b7f5c055SJed Brown     ierr = DMLabelSetValue(label, join[0], edgeSets[e]);CHKERRQ(ierr);
2760b7f5c055SJed Brown     ierr = DMPlexRestoreJoin(dm, 2, verts, &njoin, &join);CHKERRQ(ierr);
2761b7f5c055SJed Brown   }
2762b7f5c055SJed Brown   ierr = PetscFree(edges);CHKERRQ(ierr);
2763b7f5c055SJed Brown   ierr = PetscFree(edgeSets);CHKERRQ(ierr);
27641436d7faSJed Brown   if (tps_distribute) {
27651436d7faSJed Brown     DM               pdm = NULL;
27661436d7faSJed Brown     PetscPartitioner part;
27671436d7faSJed Brown 
27681436d7faSJed Brown     ierr = DMPlexGetPartitioner(dm, &part);CHKERRQ(ierr);
27691436d7faSJed Brown     ierr = PetscPartitionerSetFromOptions(part);CHKERRQ(ierr);
27701436d7faSJed Brown     ierr = DMPlexDistribute(dm, 0, NULL, &pdm);CHKERRQ(ierr);
27711436d7faSJed Brown     if (pdm) {
27721436d7faSJed Brown       ierr = DMPlexReplace_Static(dm, &pdm);CHKERRQ(ierr);
27731436d7faSJed Brown     }
27741436d7faSJed Brown     // Do not auto-distribute again
27751436d7faSJed Brown     ierr = DMPlexDistributeSetDefault(dm, PETSC_FALSE);CHKERRQ(ierr);
27761436d7faSJed Brown   }
2777b7f5c055SJed Brown 
2778b7f5c055SJed Brown   ierr = DMPlexSetRefinementUniform(dm, PETSC_TRUE);CHKERRQ(ierr);
2779b7f5c055SJed Brown   for (PetscInt refine=0; refine<refinements; refine++) {
2780b7f5c055SJed Brown     PetscInt m;
2781b7f5c055SJed Brown     DM dmf;
2782b7f5c055SJed Brown     Vec X;
2783b7f5c055SJed Brown     PetscScalar *x;
2784b7f5c055SJed Brown     ierr = DMRefine(dm, MPI_COMM_NULL, &dmf);CHKERRQ(ierr);
2785b7f5c055SJed Brown     ierr = DMPlexReplace_Static(dm, &dmf);CHKERRQ(ierr);
2786b7f5c055SJed Brown 
2787b7f5c055SJed Brown     ierr = DMGetCoordinatesLocal(dm, &X);CHKERRQ(ierr);
2788b7f5c055SJed Brown     ierr = VecGetLocalSize(X, &m);CHKERRQ(ierr);
2789b7f5c055SJed Brown     ierr = VecGetArray(X, &x);CHKERRQ(ierr);
2790b7f5c055SJed Brown     for (PetscInt i=0; i<m; i+=3) {
2791b7f5c055SJed Brown       ierr = TPSNearestPoint(evalFunc, &x[i]);CHKERRQ(ierr);
2792b7f5c055SJed Brown     }
2793b7f5c055SJed Brown     ierr = VecRestoreArray(X, &x);CHKERRQ(ierr);
2794b7f5c055SJed Brown   }
2795b7f5c055SJed Brown 
2796b7f5c055SJed Brown   // Face Sets has already been propagated to new vertices during refinement; this propagates to the initial vertices.
2797b7f5c055SJed Brown   ierr = DMGetLabel(dm, "Face Sets", &label);CHKERRQ(ierr);
2798b7f5c055SJed Brown   ierr = DMPlexLabelComplete(dm, label);CHKERRQ(ierr);
2799b7f5c055SJed Brown 
2800b7f5c055SJed Brown   if (thickness > 0) {
2801b7f5c055SJed Brown     DM dm3;
2802b7f5c055SJed Brown     ierr = DMPlexExtrude(dm, layers, thickness, PETSC_FALSE, PETSC_TRUE, NULL, NULL, &dm3);CHKERRQ(ierr);
2803b7f5c055SJed Brown     ierr = DMPlexReplace_Static(dm, &dm3);CHKERRQ(ierr);
2804b7f5c055SJed Brown   }
2805b7f5c055SJed Brown   PetscFunctionReturn(0);
2806b7f5c055SJed Brown }
2807b7f5c055SJed Brown 
2808b7f5c055SJed Brown /*@
2809b7f5c055SJed Brown   DMPlexCreateTPSMesh - Create a distributed, interpolated mesh of a triply-periodic surface
2810b7f5c055SJed Brown 
2811b7f5c055SJed Brown   Collective
2812b7f5c055SJed Brown 
2813b7f5c055SJed Brown   Input Parameters:
2814b7f5c055SJed Brown + comm   - The communicator for the DM object
2815b7f5c055SJed Brown . tpstype - Type of triply-periodic surface
2816b7f5c055SJed Brown . extent - Array of length 3 containing number of periods in each direction
2817b7f5c055SJed Brown . periodic - array of length 3 with periodicity, or NULL for non-periodic
28181436d7faSJed Brown . tps_distribute - Distribute 2D manifold mesh prior to refinement and extrusion (more scalable)
2819817da375SSatish Balay . refinements - Number of factor-of-2 refinements of 2D manifold mesh
28201436d7faSJed Brown . layers - Number of cell layers extruded in normal direction
2821817da375SSatish Balay - thickness - Thickness in normal direction
2822b7f5c055SJed Brown 
2823b7f5c055SJed Brown   Output Parameter:
2824b7f5c055SJed Brown . dm  - The DM object
2825b7f5c055SJed Brown 
2826b7f5c055SJed Brown   Notes:
2827b7f5c055SJed Brown   This meshes the surface of the Schwarz P or Gyroid surfaces.  Schwarz P is is the simplest member of the triply-periodic minimal surfaces.
2828b7f5c055SJed Brown   https://en.wikipedia.org/wiki/Schwarz_minimal_surface#Schwarz_P_(%22Primitive%22) and can be cut with "clean" boundaries.
2829b7f5c055SJed Brown   The Gyroid (https://en.wikipedia.org/wiki/Gyroid) is another triply-periodic minimal surface with applications in additive manufacturing; it is much more difficult to "cut" since there are no planes of symmetry.
2830b7f5c055SJed Brown   Our implementation creates a very coarse mesh of the surface and refines (by 4-way splitting) as many times as requested.
2831b7f5c055SJed Brown   On each refinement, all vertices are projected to their nearest point on the surface.
2832b7f5c055SJed Brown   This projection could readily be extended to related surfaces.
2833b7f5c055SJed Brown 
2834b7f5c055SJed Brown   The face (edge) sets for the Schwarz P surface are numbered 1(-x), 2(+x), 3(-y), 4(+y), 5(-z), 6(+z).
2835b7f5c055SJed Brown   When the mesh is refined, "Face Sets" contain the new vertices (created during refinement).  Use DMPlexLabelComplete() to propagate to coarse-level vertices.
2836b7f5c055SJed Brown 
2837b7f5c055SJed Brown   References:
2838606c0280SSatish Balay . * - Maskery et al, Insights into the mechanical properties of several triply periodic minimal surface lattice structures made by polymer additive manufacturing, 2017. https://doi.org/10.1016/j.polymer.2017.11.049
2839b7f5c055SJed Brown 
2840b7f5c055SJed Brown   Developer Notes:
2841b7f5c055SJed Brown   The Gyroid mesh does not currently mark boundary sets.
2842b7f5c055SJed Brown 
2843b7f5c055SJed Brown   Level: beginner
2844b7f5c055SJed Brown 
2845b7f5c055SJed Brown .seealso: DMPlexCreateSphereMesh(), DMSetType(), DMCreate()
2846b7f5c055SJed Brown @*/
28471436d7faSJed Brown PetscErrorCode DMPlexCreateTPSMesh(MPI_Comm comm, DMPlexTPSType tpstype, const PetscInt extent[], const DMBoundaryType periodic[], PetscBool tps_distribute, PetscInt refinements, PetscInt layers, PetscReal thickness, DM *dm)
2848b7f5c055SJed Brown {
2849b7f5c055SJed Brown   PetscErrorCode ierr;
2850b7f5c055SJed Brown 
2851b7f5c055SJed Brown   PetscFunctionBegin;
2852b7f5c055SJed Brown   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
2853b7f5c055SJed Brown   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
28541436d7faSJed Brown   ierr = DMPlexCreateTPSMesh_Internal(*dm, tpstype, extent, periodic, tps_distribute, refinements, layers, thickness);CHKERRQ(ierr);
2855b7f5c055SJed Brown   PetscFunctionReturn(0);
2856b7f5c055SJed Brown }
2857b7f5c055SJed Brown 
28589318fe57SMatthew G. Knepley /*@
28599318fe57SMatthew G. Knepley   DMPlexCreateSphereMesh - Creates a mesh on the d-dimensional sphere, S^d.
28609318fe57SMatthew G. Knepley 
28619318fe57SMatthew G. Knepley   Collective
28629318fe57SMatthew G. Knepley 
28639318fe57SMatthew G. Knepley   Input Parameters:
28649318fe57SMatthew G. Knepley + comm    - The communicator for the DM object
28659318fe57SMatthew G. Knepley . dim     - The dimension
28669318fe57SMatthew G. Knepley . simplex - Use simplices, or tensor product cells
28679318fe57SMatthew G. Knepley - R       - The radius
28689318fe57SMatthew G. Knepley 
28699318fe57SMatthew G. Knepley   Output Parameter:
28709318fe57SMatthew G. Knepley . dm  - The DM object
28719318fe57SMatthew G. Knepley 
28729318fe57SMatthew G. Knepley   Level: beginner
28739318fe57SMatthew G. Knepley 
28749318fe57SMatthew G. Knepley .seealso: DMPlexCreateBallMesh(), DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
28759318fe57SMatthew G. Knepley @*/
28769318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateSphereMesh(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscReal R, DM *dm)
28779318fe57SMatthew G. Knepley {
28789318fe57SMatthew G. Knepley   PetscErrorCode ierr;
28799318fe57SMatthew G. Knepley 
28809318fe57SMatthew G. Knepley   PetscFunctionBegin;
28819318fe57SMatthew G. Knepley   PetscValidPointer(dm, 5);
28829318fe57SMatthew G. Knepley   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
28839318fe57SMatthew G. Knepley   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
28849318fe57SMatthew G. Knepley   ierr = DMPlexCreateSphereMesh_Internal(*dm, dim, simplex, R);CHKERRQ(ierr);
28859318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
28869318fe57SMatthew G. Knepley }
28879318fe57SMatthew G. Knepley 
28889318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBallMesh_Internal(DM dm, PetscInt dim, PetscReal R)
28899318fe57SMatthew G. Knepley {
28909318fe57SMatthew G. Knepley   DM             sdm, vol;
28919318fe57SMatthew G. Knepley   DMLabel        bdlabel;
28929318fe57SMatthew G. Knepley   PetscErrorCode ierr;
28939318fe57SMatthew G. Knepley 
28949318fe57SMatthew G. Knepley   PetscFunctionBegin;
28959318fe57SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), &sdm);CHKERRQ(ierr);
28969318fe57SMatthew G. Knepley   ierr = DMSetType(sdm, DMPLEX);CHKERRQ(ierr);
28979318fe57SMatthew G. Knepley   ierr = PetscObjectSetOptionsPrefix((PetscObject) sdm, "bd_");CHKERRQ(ierr);
28989318fe57SMatthew G. Knepley   ierr = DMPlexCreateSphereMesh_Internal(sdm, dim-1, PETSC_TRUE, R);CHKERRQ(ierr);
28999318fe57SMatthew G. Knepley   ierr = DMSetFromOptions(sdm);CHKERRQ(ierr);
29009318fe57SMatthew G. Knepley   ierr = DMViewFromOptions(sdm, NULL, "-dm_view");CHKERRQ(ierr);
29019318fe57SMatthew G. Knepley   ierr = DMPlexGenerate(sdm, NULL, PETSC_TRUE, &vol);CHKERRQ(ierr);
29029318fe57SMatthew G. Knepley   ierr = DMDestroy(&sdm);CHKERRQ(ierr);
29039318fe57SMatthew G. Knepley   ierr = DMPlexReplace_Static(dm, &vol);CHKERRQ(ierr);
29049318fe57SMatthew G. Knepley   ierr = DMCreateLabel(dm, "marker");CHKERRQ(ierr);
29059318fe57SMatthew G. Knepley   ierr = DMGetLabel(dm, "marker", &bdlabel);CHKERRQ(ierr);
29069318fe57SMatthew G. Knepley   ierr = DMPlexMarkBoundaryFaces(dm, PETSC_DETERMINE, bdlabel);CHKERRQ(ierr);
29079318fe57SMatthew G. Knepley   ierr = DMPlexLabelComplete(dm, bdlabel);CHKERRQ(ierr);
290851a74b61SMatthew G. Knepley   PetscFunctionReturn(0);
290951a74b61SMatthew G. Knepley }
291051a74b61SMatthew G. Knepley 
291151a74b61SMatthew G. Knepley /*@
291251a74b61SMatthew G. Knepley   DMPlexCreateBallMesh - Creates a simplex mesh on the d-dimensional ball, B^d.
291351a74b61SMatthew G. Knepley 
291451a74b61SMatthew G. Knepley   Collective
291551a74b61SMatthew G. Knepley 
291651a74b61SMatthew G. Knepley   Input Parameters:
291751a74b61SMatthew G. Knepley + comm  - The communicator for the DM object
291851a74b61SMatthew G. Knepley . dim   - The dimension
291951a74b61SMatthew G. Knepley - R     - The radius
292051a74b61SMatthew G. Knepley 
292151a74b61SMatthew G. Knepley   Output Parameter:
292251a74b61SMatthew G. Knepley . dm  - The DM object
292351a74b61SMatthew G. Knepley 
292451a74b61SMatthew G. Knepley   Options Database Keys:
292551a74b61SMatthew G. Knepley - bd_dm_refine - This will refine the surface mesh preserving the sphere geometry
292651a74b61SMatthew G. Knepley 
292751a74b61SMatthew G. Knepley   Level: beginner
292851a74b61SMatthew G. Knepley 
292951a74b61SMatthew G. Knepley .seealso: DMPlexCreateSphereMesh(), DMPlexCreateBoxMesh(), DMSetType(), DMCreate()
293051a74b61SMatthew G. Knepley @*/
293151a74b61SMatthew G. Knepley PetscErrorCode DMPlexCreateBallMesh(MPI_Comm comm, PetscInt dim, PetscReal R, DM *dm)
293251a74b61SMatthew G. Knepley {
293351a74b61SMatthew G. Knepley   PetscErrorCode ierr;
293451a74b61SMatthew G. Knepley 
293551a74b61SMatthew G. Knepley   PetscFunctionBegin;
29369318fe57SMatthew G. Knepley   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
29379318fe57SMatthew G. Knepley   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
29389318fe57SMatthew G. Knepley   ierr = DMPlexCreateBallMesh_Internal(*dm, dim, R);CHKERRQ(ierr);
29392829fed8SMatthew G. Knepley   PetscFunctionReturn(0);
29402829fed8SMatthew G. Knepley }
29412829fed8SMatthew G. Knepley 
29429318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateReferenceCell_Internal(DM rdm, DMPolytopeType ct)
29430a6ba040SMatthew G. Knepley {
29440a6ba040SMatthew G. Knepley   PetscErrorCode ierr;
29450a6ba040SMatthew G. Knepley 
29460a6ba040SMatthew G. Knepley   PetscFunctionBegin;
29479318fe57SMatthew G. Knepley   switch (ct) {
29489318fe57SMatthew G. Knepley     case DM_POLYTOPE_POINT:
29499318fe57SMatthew G. Knepley     {
29509318fe57SMatthew G. Knepley       PetscInt    numPoints[1]        = {1};
29519318fe57SMatthew G. Knepley       PetscInt    coneSize[1]         = {0};
29529318fe57SMatthew G. Knepley       PetscInt    cones[1]            = {0};
29539318fe57SMatthew G. Knepley       PetscInt    coneOrientations[1] = {0};
29549318fe57SMatthew G. Knepley       PetscScalar vertexCoords[1]     = {0.0};
29559318fe57SMatthew G. Knepley 
29569318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 0);CHKERRQ(ierr);
29579318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 0, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
29589318fe57SMatthew G. Knepley     }
29599318fe57SMatthew G. Knepley     break;
29609318fe57SMatthew G. Knepley     case DM_POLYTOPE_SEGMENT:
29619318fe57SMatthew G. Knepley     {
29629318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {2, 1};
29639318fe57SMatthew G. Knepley       PetscInt    coneSize[3]         = {2, 0, 0};
29649318fe57SMatthew G. Knepley       PetscInt    cones[2]            = {1, 2};
29659318fe57SMatthew G. Knepley       PetscInt    coneOrientations[2] = {0, 0};
29669318fe57SMatthew G. Knepley       PetscScalar vertexCoords[2]     = {-1.0,  1.0};
29679318fe57SMatthew G. Knepley 
29689318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 1);CHKERRQ(ierr);
29699318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
29709318fe57SMatthew G. Knepley     }
29719318fe57SMatthew G. Knepley     break;
2972b5a892a1SMatthew G. Knepley     case DM_POLYTOPE_POINT_PRISM_TENSOR:
2973b5a892a1SMatthew G. Knepley     {
2974b5a892a1SMatthew G. Knepley       PetscInt    numPoints[2]        = {2, 1};
2975b5a892a1SMatthew G. Knepley       PetscInt    coneSize[3]         = {2, 0, 0};
2976b5a892a1SMatthew G. Knepley       PetscInt    cones[2]            = {1, 2};
2977b5a892a1SMatthew G. Knepley       PetscInt    coneOrientations[2] = {0, 0};
2978b5a892a1SMatthew G. Knepley       PetscScalar vertexCoords[2]     = {-1.0,  1.0};
2979b5a892a1SMatthew G. Knepley 
2980b5a892a1SMatthew G. Knepley       ierr = DMSetDimension(rdm, 1);CHKERRQ(ierr);
2981b5a892a1SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
2982b5a892a1SMatthew G. Knepley     }
2983b5a892a1SMatthew G. Knepley     break;
29849318fe57SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
29859318fe57SMatthew G. Knepley     {
29869318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {3, 1};
29879318fe57SMatthew G. Knepley       PetscInt    coneSize[4]         = {3, 0, 0, 0};
29889318fe57SMatthew G. Knepley       PetscInt    cones[3]            = {1, 2, 3};
29899318fe57SMatthew G. Knepley       PetscInt    coneOrientations[3] = {0, 0, 0};
29909318fe57SMatthew G. Knepley       PetscScalar vertexCoords[6]     = {-1.0, -1.0,  1.0, -1.0,  -1.0, 1.0};
29919318fe57SMatthew G. Knepley 
29929318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 2);CHKERRQ(ierr);
29939318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
29949318fe57SMatthew G. Knepley     }
29959318fe57SMatthew G. Knepley     break;
29969318fe57SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
29979318fe57SMatthew G. Knepley     {
29989318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {4, 1};
29999318fe57SMatthew G. Knepley       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
30009318fe57SMatthew G. Knepley       PetscInt    cones[4]            = {1, 2, 3, 4};
30019318fe57SMatthew G. Knepley       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
30029318fe57SMatthew G. Knepley       PetscScalar vertexCoords[8]     = {-1.0, -1.0,  1.0, -1.0,  1.0, 1.0,  -1.0, 1.0};
30039318fe57SMatthew G. Knepley 
30049318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 2);CHKERRQ(ierr);
30059318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
30069318fe57SMatthew G. Knepley     }
30079318fe57SMatthew G. Knepley     break;
30089318fe57SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
30099318fe57SMatthew G. Knepley     {
30109318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {4, 1};
30119318fe57SMatthew G. Knepley       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
30129318fe57SMatthew G. Knepley       PetscInt    cones[4]            = {1, 2, 3, 4};
30139318fe57SMatthew G. Knepley       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
30149318fe57SMatthew G. Knepley       PetscScalar vertexCoords[8]     = {-1.0, -1.0,  1.0, -1.0,  -1.0, 1.0,  1.0, 1.0};
30159318fe57SMatthew G. Knepley 
30169318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 2);CHKERRQ(ierr);
30179318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
30189318fe57SMatthew G. Knepley     }
30199318fe57SMatthew G. Knepley     break;
30209318fe57SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
30219318fe57SMatthew G. Knepley     {
30229318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {4, 1};
30239318fe57SMatthew G. Knepley       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
3024f0edb160SMatthew G. Knepley       PetscInt    cones[4]            = {1, 2, 3, 4};
30259318fe57SMatthew G. Knepley       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
3026f0edb160SMatthew 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};
30279318fe57SMatthew G. Knepley 
30289318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 3);CHKERRQ(ierr);
30299318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
30309318fe57SMatthew G. Knepley     }
30319318fe57SMatthew G. Knepley     break;
30329318fe57SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
30339318fe57SMatthew G. Knepley     {
30349318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {8, 1};
30359318fe57SMatthew G. Knepley       PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
3036f0edb160SMatthew G. Knepley       PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
30379318fe57SMatthew G. Knepley       PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
3038f0edb160SMatthew 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,
30399318fe57SMatthew 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};
30409318fe57SMatthew G. Knepley 
30419318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 3);CHKERRQ(ierr);
30429318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
30439318fe57SMatthew G. Knepley     }
30449318fe57SMatthew G. Knepley     break;
30459318fe57SMatthew G. Knepley     case DM_POLYTOPE_TRI_PRISM:
30469318fe57SMatthew G. Knepley     {
30479318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {6, 1};
30489318fe57SMatthew G. Knepley       PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
3049f0edb160SMatthew G. Knepley       PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
30509318fe57SMatthew G. Knepley       PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
3051f0edb160SMatthew G. Knepley       PetscScalar vertexCoords[18]    = {-1.0, -1.0, -1.0, -1.0,  1.0, -1.0,   1.0, -1.0, -1.0,
30529318fe57SMatthew G. Knepley                                          -1.0, -1.0,  1.0,  1.0, -1.0,  1.0,  -1.0,  1.0,  1.0};
30539318fe57SMatthew G. Knepley 
30549318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 3);CHKERRQ(ierr);
30559318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
30569318fe57SMatthew G. Knepley     }
30579318fe57SMatthew G. Knepley     break;
30589318fe57SMatthew G. Knepley     case DM_POLYTOPE_TRI_PRISM_TENSOR:
30599318fe57SMatthew G. Knepley     {
30609318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {6, 1};
30619318fe57SMatthew G. Knepley       PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
30629318fe57SMatthew G. Knepley       PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
30639318fe57SMatthew G. Knepley       PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
30649318fe57SMatthew G. Knepley       PetscScalar vertexCoords[18]    = {-1.0, -1.0, -1.0,  1.0, -1.0, -1.0,  -1.0, 1.0, -1.0,
30659318fe57SMatthew G. Knepley                                          -1.0, -1.0,  1.0,  1.0, -1.0,  1.0,  -1.0, 1.0,  1.0};
30669318fe57SMatthew G. Knepley 
30679318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 3);CHKERRQ(ierr);
30689318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
30699318fe57SMatthew G. Knepley     }
30709318fe57SMatthew G. Knepley     break;
30719318fe57SMatthew G. Knepley     case DM_POLYTOPE_QUAD_PRISM_TENSOR:
30729318fe57SMatthew G. Knepley     {
30739318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {8, 1};
30749318fe57SMatthew G. Knepley       PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
30759318fe57SMatthew G. Knepley       PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
30769318fe57SMatthew G. Knepley       PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
30779318fe57SMatthew 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,
30789318fe57SMatthew 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};
30799318fe57SMatthew G. Knepley 
30809318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 3);CHKERRQ(ierr);
30819318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
30829318fe57SMatthew G. Knepley     }
30839318fe57SMatthew G. Knepley     break;
30849318fe57SMatthew G. Knepley     case DM_POLYTOPE_PYRAMID:
30859318fe57SMatthew G. Knepley     {
30869318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {5, 1};
30879318fe57SMatthew G. Knepley       PetscInt    coneSize[6]         = {5, 0, 0, 0, 0, 0};
3088f0edb160SMatthew G. Knepley       PetscInt    cones[5]            = {1, 2, 3, 4, 5};
30899318fe57SMatthew G. Knepley       PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
3090f0edb160SMatthew 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,
30919318fe57SMatthew G. Knepley                                           0.0,  0.0,  1.0};
30929318fe57SMatthew G. Knepley 
30939318fe57SMatthew G. Knepley       ierr = DMSetDimension(rdm, 3);CHKERRQ(ierr);
30949318fe57SMatthew G. Knepley       ierr = DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr);
30959318fe57SMatthew G. Knepley     }
30969318fe57SMatthew G. Knepley     break;
309798921bdaSJacob Faibussowitsch     default: SETERRQ(PetscObjectComm((PetscObject) rdm), PETSC_ERR_ARG_WRONG, "Cannot create reference cell for cell type %s", DMPolytopeTypes[ct]);
30989318fe57SMatthew G. Knepley   }
30999318fe57SMatthew G. Knepley   {
31009318fe57SMatthew G. Knepley     PetscInt Nv, v;
31019318fe57SMatthew G. Knepley 
31029318fe57SMatthew G. Knepley     /* Must create the celltype label here so that we do not automatically try to compute the types */
31039318fe57SMatthew G. Knepley     ierr = DMCreateLabel(rdm, "celltype");CHKERRQ(ierr);
31049318fe57SMatthew G. Knepley     ierr = DMPlexSetCellType(rdm, 0, ct);CHKERRQ(ierr);
31059318fe57SMatthew G. Knepley     ierr = DMPlexGetChart(rdm, NULL, &Nv);CHKERRQ(ierr);
31069318fe57SMatthew G. Knepley     for (v = 1; v < Nv; ++v) {ierr = DMPlexSetCellType(rdm, v, DM_POLYTOPE_POINT);CHKERRQ(ierr);}
31079318fe57SMatthew G. Knepley   }
31089318fe57SMatthew G. Knepley   ierr = DMPlexInterpolateInPlace_Internal(rdm);CHKERRQ(ierr);
3109b7f6ffafSMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) rdm, DMPolytopeTypes[ct]);CHKERRQ(ierr);
31100a6ba040SMatthew G. Knepley   PetscFunctionReturn(0);
31110a6ba040SMatthew G. Knepley }
31120a6ba040SMatthew G. Knepley 
31139318fe57SMatthew G. Knepley /*@
31149318fe57SMatthew G. Knepley   DMPlexCreateReferenceCell - Create a DMPLEX with the appropriate FEM reference cell
31159318fe57SMatthew G. Knepley 
31169318fe57SMatthew G. Knepley   Collective
31179318fe57SMatthew G. Knepley 
31189318fe57SMatthew G. Knepley   Input Parameters:
31199318fe57SMatthew G. Knepley + comm - The communicator
31209318fe57SMatthew G. Knepley - ct   - The cell type of the reference cell
31219318fe57SMatthew G. Knepley 
31229318fe57SMatthew G. Knepley   Output Parameter:
31239318fe57SMatthew G. Knepley . refdm - The reference cell
31249318fe57SMatthew G. Knepley 
31259318fe57SMatthew G. Knepley   Level: intermediate
31269318fe57SMatthew G. Knepley 
31279318fe57SMatthew G. Knepley .seealso: DMPlexCreateReferenceCell(), DMPlexCreateBoxMesh()
31289318fe57SMatthew G. Knepley @*/
31299318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateReferenceCell(MPI_Comm comm, DMPolytopeType ct, DM *refdm)
31300a6ba040SMatthew G. Knepley {
31310a6ba040SMatthew G. Knepley   PetscErrorCode ierr;
31320a6ba040SMatthew G. Knepley 
31330a6ba040SMatthew G. Knepley   PetscFunctionBegin;
31349318fe57SMatthew G. Knepley   ierr = DMCreate(comm, refdm);CHKERRQ(ierr);
31359318fe57SMatthew G. Knepley   ierr = DMSetType(*refdm, DMPLEX);CHKERRQ(ierr);
31369318fe57SMatthew G. Knepley   ierr = DMPlexCreateReferenceCell_Internal(*refdm, ct);CHKERRQ(ierr);
31379318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
31389318fe57SMatthew G. Knepley }
313979a015ccSMatthew G. Knepley 
31409318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoundaryLabel_Private(DM dm, const char name[])
31419318fe57SMatthew G. Knepley {
31429318fe57SMatthew G. Knepley   DM             plex;
31439318fe57SMatthew G. Knepley   DMLabel        label;
31449318fe57SMatthew G. Knepley   PetscBool      hasLabel;
31459318fe57SMatthew G. Knepley   PetscErrorCode ierr;
31460a6ba040SMatthew G. Knepley 
31479318fe57SMatthew G. Knepley   PetscFunctionBeginUser;
31489318fe57SMatthew G. Knepley   ierr = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
31499318fe57SMatthew G. Knepley   if (hasLabel) PetscFunctionReturn(0);
31509318fe57SMatthew G. Knepley   ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
31519318fe57SMatthew G. Knepley   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
31529318fe57SMatthew G. Knepley   ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
31539318fe57SMatthew G. Knepley   ierr = DMPlexMarkBoundaryFaces(plex, 1, label);CHKERRQ(ierr);
31549318fe57SMatthew G. Knepley   ierr = DMDestroy(&plex);CHKERRQ(ierr);
31559318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
31569318fe57SMatthew G. Knepley }
3157acdc6f61SToby Isaac 
3158b7f5c055SJed Brown const char * const DMPlexShapes[] = {"box", "box_surface", "ball", "sphere", "cylinder", "schwarz_p", "gyroid", "unknown", "DMPlexShape", "DM_SHAPE_", NULL};
31599318fe57SMatthew G. Knepley 
316061a622f3SMatthew G. Knepley static PetscErrorCode DMPlexCreateFromOptions_Internal(PetscOptionItems *PetscOptionsObject, PetscBool *useCoordSpace, DM dm)
31619318fe57SMatthew G. Knepley {
31629318fe57SMatthew G. Knepley   DMPlexShape    shape = DM_SHAPE_BOX;
31639318fe57SMatthew G. Knepley   DMPolytopeType cell  = DM_POLYTOPE_TRIANGLE;
31649318fe57SMatthew G. Knepley   PetscInt       dim   = 2;
31659318fe57SMatthew G. Knepley   PetscBool      simplex = PETSC_TRUE, interpolate = PETSC_TRUE, adjCone = PETSC_FALSE, adjClosure = PETSC_TRUE, refDomain = PETSC_FALSE;
3166cd7e8a5eSksagiyam   PetscBool      flg, flg2, fflg, bdfflg, nameflg;
31679318fe57SMatthew G. Knepley   MPI_Comm       comm;
3168ed5e4e85SVaclav Hapla   char           filename[PETSC_MAX_PATH_LEN]   = "<unspecified>";
3169ed5e4e85SVaclav Hapla   char           bdFilename[PETSC_MAX_PATH_LEN] = "<unspecified>";
3170ed5e4e85SVaclav Hapla   char           plexname[PETSC_MAX_PATH_LEN]   = "";
31719318fe57SMatthew G. Knepley   PetscErrorCode ierr;
31729318fe57SMatthew G. Knepley 
31739318fe57SMatthew G. Knepley   PetscFunctionBegin;
31749318fe57SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
31759318fe57SMatthew G. Knepley   /* TODO Turn this into a registration interface */
31769318fe57SMatthew G. Knepley   ierr = PetscOptionsString("-dm_plex_filename", "File containing a mesh", "DMPlexCreateFromFile", filename, filename, sizeof(filename), &fflg);CHKERRQ(ierr);
31779318fe57SMatthew G. Knepley   ierr = PetscOptionsString("-dm_plex_boundary_filename", "File containing a mesh boundary", "DMPlexCreateFromFile", bdFilename, bdFilename, sizeof(bdFilename), &bdfflg);CHKERRQ(ierr);
3178cd7e8a5eSksagiyam   ierr = PetscOptionsString("-dm_plex_name", "Name of the mesh in the file", "DMPlexCreateFromFile", plexname, plexname, sizeof(plexname), &nameflg);CHKERRQ(ierr);
31799318fe57SMatthew G. Knepley   ierr = PetscOptionsEnum("-dm_plex_cell", "Cell shape", "", DMPolytopeTypes, (PetscEnum) cell, (PetscEnum *) &cell, NULL);CHKERRQ(ierr);
31809318fe57SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_reference_cell_domain", "Use a reference cell domain", "", refDomain, &refDomain, NULL);CHKERRQ(ierr);
31819318fe57SMatthew G. Knepley   ierr = PetscOptionsEnum("-dm_plex_shape", "Shape for built-in mesh", "", DMPlexShapes, (PetscEnum) shape, (PetscEnum *) &shape, &flg);CHKERRQ(ierr);
31829318fe57SMatthew G. Knepley   ierr = PetscOptionsBoundedInt("-dm_plex_dim", "Topological dimension of the mesh", "DMGetDimension", dim, &dim, &flg, 0);CHKERRQ(ierr);
31832c71b3e2SJacob Faibussowitsch   PetscCheckFalse((dim < 0) || (dim > 3),comm, PETSC_ERR_ARG_OUTOFRANGE, "Dimension %D should be in [1, 3]", dim);
31849318fe57SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_simplex", "Mesh cell shape", "", simplex,  &simplex, &flg);CHKERRQ(ierr);
31859318fe57SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_interpolate", "Flag to create edges and faces automatically", "", interpolate, &interpolate, &flg);CHKERRQ(ierr);
31869318fe57SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_adj_cone", "Set adjacency direction", "DMSetBasicAdjacency", adjCone,  &adjCone, &flg);CHKERRQ(ierr);
31879318fe57SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_adj_closure", "Set adjacency size", "DMSetBasicAdjacency", adjClosure,  &adjClosure, &flg2);CHKERRQ(ierr);
31889318fe57SMatthew G. Knepley   if (flg || flg2) {ierr = DMSetBasicAdjacency(dm, adjCone, adjClosure);CHKERRQ(ierr);}
31899318fe57SMatthew G. Knepley 
319061a622f3SMatthew G. Knepley   switch (cell) {
319161a622f3SMatthew G. Knepley     case DM_POLYTOPE_POINT:
319261a622f3SMatthew G. Knepley     case DM_POLYTOPE_SEGMENT:
319361a622f3SMatthew G. Knepley     case DM_POLYTOPE_POINT_PRISM_TENSOR:
319461a622f3SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
319561a622f3SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
319661a622f3SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
319761a622f3SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
319861a622f3SMatthew G. Knepley       *useCoordSpace = PETSC_TRUE;break;
319961a622f3SMatthew G. Knepley     default: *useCoordSpace = PETSC_FALSE;break;
320061a622f3SMatthew G. Knepley   }
320161a622f3SMatthew G. Knepley 
32029318fe57SMatthew G. Knepley   if (fflg) {
32039318fe57SMatthew G. Knepley     DM dmnew;
32049318fe57SMatthew G. Knepley 
3205cd7e8a5eSksagiyam     ierr = DMPlexCreateFromFile(PetscObjectComm((PetscObject) dm), filename, plexname, interpolate, &dmnew);CHKERRQ(ierr);
3206e600fa54SMatthew G. Knepley     ierr = DMPlexCopy_Internal(dm, PETSC_FALSE, dmnew);CHKERRQ(ierr);
32079318fe57SMatthew G. Knepley     ierr = DMPlexReplace_Static(dm, &dmnew);CHKERRQ(ierr);
32089318fe57SMatthew G. Knepley   } else if (refDomain) {
32099318fe57SMatthew G. Knepley     ierr = DMPlexCreateReferenceCell_Internal(dm, cell);CHKERRQ(ierr);
32109318fe57SMatthew G. Knepley   } else if (bdfflg) {
32119318fe57SMatthew G. Knepley     DM bdm, dmnew;
32129318fe57SMatthew G. Knepley 
3213cd7e8a5eSksagiyam     ierr = DMPlexCreateFromFile(PetscObjectComm((PetscObject) dm), bdFilename, plexname, interpolate, &bdm);CHKERRQ(ierr);
32149318fe57SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) bdm, "bd_");CHKERRQ(ierr);
32159318fe57SMatthew G. Knepley     ierr = DMSetFromOptions(bdm);CHKERRQ(ierr);
32169318fe57SMatthew G. Knepley     ierr = DMPlexGenerate(bdm, NULL, interpolate, &dmnew);CHKERRQ(ierr);
32179318fe57SMatthew G. Knepley     ierr = DMDestroy(&bdm);CHKERRQ(ierr);
3218e600fa54SMatthew G. Knepley     ierr = DMPlexCopy_Internal(dm, PETSC_FALSE, dmnew);CHKERRQ(ierr);
32199318fe57SMatthew G. Knepley     ierr = DMPlexReplace_Static(dm, &dmnew);CHKERRQ(ierr);
32209318fe57SMatthew G. Knepley   } else {
32219318fe57SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) dm, DMPlexShapes[shape]);CHKERRQ(ierr);
32229318fe57SMatthew G. Knepley     switch (shape) {
32239318fe57SMatthew G. Knepley       case DM_SHAPE_BOX:
32249318fe57SMatthew G. Knepley       {
32259318fe57SMatthew G. Knepley         PetscInt       faces[3] = {0, 0, 0};
32269318fe57SMatthew G. Knepley         PetscReal      lower[3] = {0, 0, 0};
32279318fe57SMatthew G. Knepley         PetscReal      upper[3] = {1, 1, 1};
32289318fe57SMatthew G. Knepley         DMBoundaryType bdt[3]   = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
32299318fe57SMatthew G. Knepley         PetscInt       i, n;
32309318fe57SMatthew G. Knepley 
32319318fe57SMatthew G. Knepley         n    = dim;
32329318fe57SMatthew G. Knepley         for (i = 0; i < dim; ++i) faces[i] = (dim == 1 ? 1 : 4-dim);
32339318fe57SMatthew G. Knepley         ierr = PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg);CHKERRQ(ierr);
32349318fe57SMatthew G. Knepley         n    = 3;
32359318fe57SMatthew G. Knepley         ierr = PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg);CHKERRQ(ierr);
32362c71b3e2SJacob Faibussowitsch         PetscCheckFalse(flg && (n != dim),comm, PETSC_ERR_ARG_SIZ, "Lower box point had %D values, should have been %D", n, dim);
32379318fe57SMatthew G. Knepley         n    = 3;
32389318fe57SMatthew G. Knepley         ierr = PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg);CHKERRQ(ierr);
32392c71b3e2SJacob Faibussowitsch         PetscCheckFalse(flg && (n != dim),comm, PETSC_ERR_ARG_SIZ, "Upper box point had %D values, should have been %D", n, dim);
32409318fe57SMatthew G. Knepley         n    = 3;
32419318fe57SMatthew G. Knepley         ierr = PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *) bdt, &n, &flg);CHKERRQ(ierr);
32422c71b3e2SJacob Faibussowitsch         PetscCheckFalse(flg && (n != dim),comm, PETSC_ERR_ARG_SIZ, "Box boundary types had %D values, should have been %D", n, dim);
32439318fe57SMatthew G. Knepley         switch (cell) {
324461a622f3SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
3245d410b0cfSMatthew G. Knepley             ierr = DMPlexCreateWedgeBoxMesh_Internal(dm, faces, lower, upper, bdt);CHKERRQ(ierr);
3246d410b0cfSMatthew G. Knepley             if (!interpolate) {
3247d410b0cfSMatthew G. Knepley               DM udm;
3248d410b0cfSMatthew G. Knepley 
3249d410b0cfSMatthew G. Knepley               ierr = DMPlexUninterpolate(dm, &udm);CHKERRQ(ierr);
3250d410b0cfSMatthew G. Knepley               ierr = DMPlexReplace_Static(dm, &udm);CHKERRQ(ierr);
3251d410b0cfSMatthew G. Knepley             }
32529318fe57SMatthew G. Knepley             break;
32539318fe57SMatthew G. Knepley           default:
32549318fe57SMatthew G. Knepley             ierr = DMPlexCreateBoxMesh_Internal(dm, dim, simplex, faces, lower, upper, bdt, interpolate);CHKERRQ(ierr);
32559318fe57SMatthew G. Knepley             break;
32569318fe57SMatthew G. Knepley         }
32579318fe57SMatthew G. Knepley       }
32589318fe57SMatthew G. Knepley       break;
32599318fe57SMatthew G. Knepley       case DM_SHAPE_BOX_SURFACE:
32609318fe57SMatthew G. Knepley       {
32619318fe57SMatthew G. Knepley         PetscInt  faces[3] = {0, 0, 0};
32629318fe57SMatthew G. Knepley         PetscReal lower[3] = {0, 0, 0};
32639318fe57SMatthew G. Knepley         PetscReal upper[3] = {1, 1, 1};
32649318fe57SMatthew G. Knepley         PetscInt  i, n;
32659318fe57SMatthew G. Knepley 
32669318fe57SMatthew G. Knepley         n    = dim+1;
32679318fe57SMatthew G. Knepley         for (i = 0; i < dim+1; ++i) faces[i] = (dim+1 == 1 ? 1 : 4-(dim+1));
32689318fe57SMatthew G. Knepley         ierr = PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg);CHKERRQ(ierr);
32699318fe57SMatthew G. Knepley         n    = 3;
32709318fe57SMatthew G. Knepley         ierr = PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg);CHKERRQ(ierr);
32712c71b3e2SJacob Faibussowitsch         PetscCheckFalse(flg && (n != dim+1),comm, PETSC_ERR_ARG_SIZ, "Lower box point had %D values, should have been %D", n, dim+1);
32729318fe57SMatthew G. Knepley         n    = 3;
32739318fe57SMatthew G. Knepley         ierr = PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg);CHKERRQ(ierr);
32742c71b3e2SJacob Faibussowitsch         PetscCheckFalse(flg && (n != dim+1),comm, PETSC_ERR_ARG_SIZ, "Upper box point had %D values, should have been %D", n, dim+1);
32759318fe57SMatthew G. Knepley         ierr = DMPlexCreateBoxSurfaceMesh_Internal(dm, dim+1, faces, lower, upper, interpolate);CHKERRQ(ierr);
32769318fe57SMatthew G. Knepley       }
32779318fe57SMatthew G. Knepley       break;
32789318fe57SMatthew G. Knepley       case DM_SHAPE_SPHERE:
32799318fe57SMatthew G. Knepley       {
32809318fe57SMatthew G. Knepley         PetscReal R = 1.0;
32819318fe57SMatthew G. Knepley 
32829318fe57SMatthew G. Knepley         ierr = PetscOptionsReal("-dm_plex_sphere_radius", "Radius of the sphere", "", R,  &R, &flg);CHKERRQ(ierr);
32839318fe57SMatthew G. Knepley         ierr = DMPlexCreateSphereMesh_Internal(dm, dim, simplex, R);CHKERRQ(ierr);
32849318fe57SMatthew G. Knepley       }
32859318fe57SMatthew G. Knepley       break;
32869318fe57SMatthew G. Knepley       case DM_SHAPE_BALL:
32879318fe57SMatthew G. Knepley       {
32889318fe57SMatthew G. Knepley         PetscReal R = 1.0;
32899318fe57SMatthew G. Knepley 
32909318fe57SMatthew G. Knepley         ierr = PetscOptionsReal("-dm_plex_ball_radius", "Radius of the ball", "", R,  &R, &flg);CHKERRQ(ierr);
32919318fe57SMatthew G. Knepley         ierr = DMPlexCreateBallMesh_Internal(dm, dim, R);CHKERRQ(ierr);
32929318fe57SMatthew G. Knepley       }
32939318fe57SMatthew G. Knepley       break;
32949318fe57SMatthew G. Knepley       case DM_SHAPE_CYLINDER:
32959318fe57SMatthew G. Knepley       {
32969318fe57SMatthew G. Knepley         DMBoundaryType bdt = DM_BOUNDARY_NONE;
32979318fe57SMatthew G. Knepley         PetscInt       Nw  = 6;
32989318fe57SMatthew G. Knepley 
32999318fe57SMatthew G. Knepley         ierr = PetscOptionsEnum("-dm_plex_cylinder_bd", "Boundary type in the z direction", "", DMBoundaryTypes, (PetscEnum) bdt, (PetscEnum *) &bdt, NULL);CHKERRQ(ierr);
330061a622f3SMatthew G. Knepley         ierr = PetscOptionsInt("-dm_plex_cylinder_num_wedges", "Number of wedges around the cylinder", "", Nw, &Nw, NULL);CHKERRQ(ierr);
33019318fe57SMatthew G. Knepley         switch (cell) {
330261a622f3SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
33039318fe57SMatthew G. Knepley             ierr = DMPlexCreateWedgeCylinderMesh_Internal(dm, Nw, interpolate);CHKERRQ(ierr);
33049318fe57SMatthew G. Knepley             break;
33059318fe57SMatthew G. Knepley           default:
33069318fe57SMatthew G. Knepley             ierr = DMPlexCreateHexCylinderMesh_Internal(dm, bdt);CHKERRQ(ierr);
33079318fe57SMatthew G. Knepley             break;
33089318fe57SMatthew G. Knepley         }
33099318fe57SMatthew G. Knepley       }
33109318fe57SMatthew G. Knepley       break;
3311b7f5c055SJed Brown       case DM_SHAPE_SCHWARZ_P: // fallthrough
3312b7f5c055SJed Brown       case DM_SHAPE_GYROID:
3313b7f5c055SJed Brown       {
3314b7f5c055SJed Brown         PetscInt       extent[3] = {1,1,1}, refine = 0, layers = 0, three;
3315b7f5c055SJed Brown         PetscReal      thickness = 0.;
3316b7f5c055SJed Brown         DMBoundaryType periodic[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
3317b7f5c055SJed Brown         DMPlexTPSType  tps_type = shape == DM_SHAPE_SCHWARZ_P ? DMPLEX_TPS_SCHWARZ_P : DMPLEX_TPS_GYROID;
33181436d7faSJed Brown         PetscBool      tps_distribute;
3319b7f5c055SJed Brown         ierr = PetscOptionsIntArray("-dm_plex_tps_extent", "Number of replicas for each of three dimensions", NULL, extent, (three=3, &three), NULL);CHKERRQ(ierr);
3320b7f5c055SJed Brown         ierr = PetscOptionsInt("-dm_plex_tps_refine", "Number of refinements", NULL, refine, &refine, NULL);CHKERRQ(ierr);
3321b7f5c055SJed Brown         ierr = PetscOptionsEnumArray("-dm_plex_tps_periodic", "Periodicity in each of three dimensions", NULL, DMBoundaryTypes, (PetscEnum*)periodic, (three=3, &three), NULL);CHKERRQ(ierr);
3322b7f5c055SJed Brown         ierr = PetscOptionsInt("-dm_plex_tps_layers", "Number of layers in volumetric extrusion (or zero to not extrude)", NULL, layers, &layers, NULL);CHKERRQ(ierr);
3323b7f5c055SJed Brown         ierr = PetscOptionsReal("-dm_plex_tps_thickness", "Thickness of volumetric extrusion", NULL, thickness, &thickness, NULL);CHKERRQ(ierr);
33241436d7faSJed Brown         ierr = DMPlexDistributeGetDefault(dm, &tps_distribute);CHKERRQ(ierr);
33251436d7faSJed Brown         ierr = PetscOptionsBool("-dm_plex_tps_distribute", "Distribute the 2D mesh prior to refinement and extrusion", NULL, tps_distribute, &tps_distribute, NULL);CHKERRQ(ierr);
33261436d7faSJed Brown         ierr = DMPlexCreateTPSMesh_Internal(dm, tps_type, extent, periodic, tps_distribute, refine, layers, thickness);CHKERRQ(ierr);
3327b7f5c055SJed Brown       }
3328b7f5c055SJed Brown       break;
332998921bdaSJacob Faibussowitsch       default: SETERRQ(comm, PETSC_ERR_SUP, "Domain shape %s is unsupported", DMPlexShapes[shape]);
33309318fe57SMatthew G. Knepley     }
33319318fe57SMatthew G. Knepley   }
33329318fe57SMatthew G. Knepley   ierr = DMPlexSetRefinementUniform(dm, PETSC_TRUE);CHKERRQ(ierr);
3333ed5e4e85SVaclav Hapla   if (!((PetscObject)dm)->name && nameflg) {
3334ed5e4e85SVaclav Hapla     ierr = PetscObjectSetName((PetscObject)dm, plexname);CHKERRQ(ierr);
3335ed5e4e85SVaclav Hapla   }
33360a6ba040SMatthew G. Knepley   PetscFunctionReturn(0);
33370a6ba040SMatthew G. Knepley }
33380a6ba040SMatthew G. Knepley 
333947920aaeSMatthew G. Knepley PetscErrorCode DMSetFromOptions_NonRefinement_Plex(PetscOptionItems *PetscOptionsObject, DM dm)
33400a6ba040SMatthew G. Knepley {
33410a6ba040SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
3342c0f0dcc3SMatthew G. Knepley   PetscBool      flg;
33439318fe57SMatthew G. Knepley   char           bdLabel[PETSC_MAX_PATH_LEN];
33440a6ba040SMatthew G. Knepley   PetscErrorCode ierr;
33450a6ba040SMatthew G. Knepley 
33460a6ba040SMatthew G. Knepley   PetscFunctionBegin;
33470a6ba040SMatthew G. Knepley   /* Handle viewing */
33480c77aedcSMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_print_set_values", "Output all set values info", "DMPlexMatSetClosure", PETSC_FALSE, &mesh->printSetValues, NULL);CHKERRQ(ierr);
33495a856986SBarry Smith   ierr = PetscOptionsBoundedInt("-dm_plex_print_fem", "Debug output level all fem computations", "DMPlexSNESComputeResidualFEM", 0, &mesh->printFEM, NULL,0);CHKERRQ(ierr);
33500c77aedcSMatthew G. Knepley   ierr = PetscOptionsReal("-dm_plex_print_tol", "Tolerance for FEM output", "DMPlexSNESComputeResidualFEM", mesh->printTol, &mesh->printTol, NULL);CHKERRQ(ierr);
33515a856986SBarry Smith   ierr = PetscOptionsBoundedInt("-dm_plex_print_l2", "Debug output level all L2 diff computations", "DMComputeL2Diff", 0, &mesh->printL2, NULL,0);CHKERRQ(ierr);
3352c0f0dcc3SMatthew G. Knepley   ierr = DMMonitorSetFromOptions(dm, "-dm_plex_monitor_throughput", "Monitor the simulation throughput", "DMPlexMonitorThroughput", DMPlexMonitorThroughput, NULL, &flg);CHKERRQ(ierr);
3353c0f0dcc3SMatthew G. Knepley   if (flg) {ierr = PetscLogDefaultBegin();CHKERRQ(ierr);}
33549318fe57SMatthew G. Knepley   /* Labeling */
33559318fe57SMatthew G. Knepley   ierr = PetscOptionsString("-dm_plex_boundary_label", "Label to mark the mesh boundary", "", bdLabel, bdLabel, sizeof(bdLabel), &flg);CHKERRQ(ierr);
33569318fe57SMatthew G. Knepley   if (flg) {ierr = DMPlexCreateBoundaryLabel_Private(dm, bdLabel);CHKERRQ(ierr);}
3357953fc75cSMatthew G. Knepley   /* Point Location */
33580c77aedcSMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_hash_location", "Use grid hashing for point location", "DMInterpolate", PETSC_FALSE, &mesh->useHashLocation, NULL);CHKERRQ(ierr);
33590848f4b5SMatthew G. Knepley   /* Partitioning and distribution */
336098ba2d7fSLawrence 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);
33612e62ab5aSMatthew G. Knepley   /* Generation and remeshing */
33620c77aedcSMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_remesh_bd", "Allow changes to the boundary on remeshing", "DMAdapt", PETSC_FALSE, &mesh->remeshBd, NULL);CHKERRQ(ierr);
3363b29cfa1cSToby Isaac   /* Projection behavior */
33645a856986SBarry Smith   ierr = PetscOptionsBoundedInt("-dm_plex_max_projection_height", "Maxmimum mesh point height used to project locally", "DMPlexSetMaxProjectionHeight", 0, &mesh->maxProjectionHeight, NULL,0);CHKERRQ(ierr);
336564141f95SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_regular_refinement", "Use special nested projection algorithm for regular refinement", "DMPlexSetRegularRefinement", mesh->regularRefinement, &mesh->regularRefinement, NULL);CHKERRQ(ierr);
3366f12cf164SMatthew G. Knepley   /* Checking structure */
3367f12cf164SMatthew G. Knepley   {
3368e902f1eaSVaclav Hapla     PetscBool   flg = PETSC_FALSE, flg2 = PETSC_FALSE, all = PETSC_FALSE;
3369f12cf164SMatthew G. Knepley 
3370e902f1eaSVaclav Hapla     ierr = PetscOptionsBool("-dm_plex_check_all", "Perform all checks", NULL, PETSC_FALSE, &all, &flg2);CHKERRQ(ierr);
3371f12cf164SMatthew 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);
3372e902f1eaSVaclav Hapla     if (all || (flg && flg2)) {ierr = DMPlexCheckSymmetry(dm);CHKERRQ(ierr);}
337325c50c26SVaclav 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);
3374e902f1eaSVaclav Hapla     if (all || (flg && flg2)) {ierr = DMPlexCheckSkeleton(dm, 0);CHKERRQ(ierr);}
337525c50c26SVaclav 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);
3376e902f1eaSVaclav Hapla     if (all || (flg && flg2)) {ierr = DMPlexCheckFaces(dm, 0);CHKERRQ(ierr);}
3377f12cf164SMatthew G. Knepley     ierr = PetscOptionsBool("-dm_plex_check_geometry", "Check that cells have positive volume", "DMPlexCheckGeometry", PETSC_FALSE, &flg, &flg2);CHKERRQ(ierr);
3378e902f1eaSVaclav Hapla     if (all || (flg && flg2)) {ierr = DMPlexCheckGeometry(dm);CHKERRQ(ierr);}
337975ebff7aSVaclav Hapla     ierr = PetscOptionsBool("-dm_plex_check_pointsf", "Check some necessary conditions for PointSF", "DMPlexCheckPointSF", PETSC_FALSE, &flg, &flg2);CHKERRQ(ierr);
3380e902f1eaSVaclav Hapla     if (all || (flg && flg2)) {ierr = DMPlexCheckPointSF(dm);CHKERRQ(ierr);}
338175ebff7aSVaclav 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);
3382e902f1eaSVaclav Hapla     if (all || (flg && flg2)) {ierr = DMPlexCheckInterfaceCones(dm);CHKERRQ(ierr);}
3383412e9a14SMatthew G. Knepley     ierr = PetscOptionsBool("-dm_plex_check_cell_shape", "Check cell shape", "DMPlexCheckCellShape", PETSC_FALSE, &flg, &flg2);CHKERRQ(ierr);
3384412e9a14SMatthew G. Knepley     if (flg && flg2) {ierr = DMPlexCheckCellShape(dm, PETSC_TRUE, PETSC_DETERMINE);CHKERRQ(ierr);}
3385f12cf164SMatthew G. Knepley   }
33869318fe57SMatthew G. Knepley   {
33879318fe57SMatthew G. Knepley     PetscReal scale = 1.0;
33884f3833eaSMatthew G. Knepley 
33899318fe57SMatthew G. Knepley     ierr = PetscOptionsReal("-dm_plex_scale", "Scale factor for mesh coordinates", "DMPlexScale", scale, &scale, &flg);CHKERRQ(ierr);
33909318fe57SMatthew G. Knepley     if (flg) {
33919318fe57SMatthew G. Knepley       Vec coordinates, coordinatesLocal;
33929318fe57SMatthew G. Knepley 
33939318fe57SMatthew G. Knepley       ierr = DMGetCoordinates(dm, &coordinates);CHKERRQ(ierr);
33949318fe57SMatthew G. Knepley       ierr = DMGetCoordinatesLocal(dm, &coordinatesLocal);CHKERRQ(ierr);
33959318fe57SMatthew G. Knepley       ierr = VecScale(coordinates, scale);CHKERRQ(ierr);
33969318fe57SMatthew G. Knepley       ierr = VecScale(coordinatesLocal, scale);CHKERRQ(ierr);
33979318fe57SMatthew G. Knepley     }
33989318fe57SMatthew G. Knepley   }
33994f3833eaSMatthew G. Knepley   ierr = PetscPartitionerSetFromOptions(mesh->partitioner);CHKERRQ(ierr);
340068d4fef7SMatthew G. Knepley   PetscFunctionReturn(0);
340168d4fef7SMatthew G. Knepley }
340268d4fef7SMatthew G. Knepley 
340346fa42a0SMatthew G. Knepley static PetscErrorCode DMSetFromOptions_Plex(PetscOptionItems *PetscOptionsObject,DM dm)
340468d4fef7SMatthew G. Knepley {
3405bdf63967SMatthew G. Knepley   PetscFunctionList ordlist;
3406bdf63967SMatthew G. Knepley   char              oname[256];
3407d410b0cfSMatthew G. Knepley   PetscReal         volume = -1.0;
34089318fe57SMatthew G. Knepley   PetscInt          prerefine = 0, refine = 0, r, coarsen = 0, overlap = 0, extLayers = 0, dim;
3409e600fa54SMatthew G. Knepley   PetscBool         uniformOrig, created = PETSC_FALSE, uniform = PETSC_TRUE, distribute, interpolate = PETSC_TRUE, coordSpace = PETSC_TRUE, remap = PETSC_TRUE, ghostCells = PETSC_FALSE, isHierarchy, ignoreModel = PETSC_FALSE, flg;
341068d4fef7SMatthew G. Knepley   PetscErrorCode ierr;
341168d4fef7SMatthew G. Knepley 
341268d4fef7SMatthew G. Knepley   PetscFunctionBegin;
3413064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
34141a1499c8SBarry Smith   ierr = PetscOptionsHead(PetscOptionsObject,"DMPlex Options");CHKERRQ(ierr);
34159318fe57SMatthew G. Knepley   /* Handle automatic creation */
34169318fe57SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
341761a622f3SMatthew G. Knepley   if (dim < 0) {ierr = DMPlexCreateFromOptions_Internal(PetscOptionsObject, &coordSpace, dm);CHKERRQ(ierr);created = PETSC_TRUE;}
3418d89e6e46SMatthew G. Knepley   /* Handle interpolation before distribution */
3419d89e6e46SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_plex_interpolate_pre", "Flag to interpolate mesh before distribution", "", interpolate, &interpolate, &flg);CHKERRQ(ierr);
3420d89e6e46SMatthew G. Knepley   if (flg) {
3421d89e6e46SMatthew G. Knepley     DMPlexInterpolatedFlag interpolated;
3422d89e6e46SMatthew G. Knepley 
3423d89e6e46SMatthew G. Knepley     ierr = DMPlexIsInterpolated(dm, &interpolated);CHKERRQ(ierr);
3424d89e6e46SMatthew G. Knepley     if (interpolated == DMPLEX_INTERPOLATED_FULL && !interpolate) {
3425d89e6e46SMatthew G. Knepley       DM udm;
3426d89e6e46SMatthew G. Knepley 
3427d89e6e46SMatthew G. Knepley       ierr = DMPlexUninterpolate(dm, &udm);CHKERRQ(ierr);
3428d89e6e46SMatthew G. Knepley       ierr = DMPlexReplace_Static(dm, &udm);CHKERRQ(ierr);
3429d89e6e46SMatthew G. Knepley     } else if (interpolated != DMPLEX_INTERPOLATED_FULL && interpolate) {
3430d89e6e46SMatthew G. Knepley       DM idm;
3431d89e6e46SMatthew G. Knepley 
3432d89e6e46SMatthew G. Knepley       ierr = DMPlexInterpolate(dm, &idm);CHKERRQ(ierr);
3433d89e6e46SMatthew G. Knepley       ierr = DMPlexReplace_Static(dm, &idm);CHKERRQ(ierr);
3434d89e6e46SMatthew G. Knepley     }
3435d89e6e46SMatthew G. Knepley   }
34369b44eab4SMatthew G. Knepley   /* Handle DMPlex refinement before distribution */
3437c1cad2e7SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_refine_ignore_model", "Flag to ignore the geometry model when refining", "DMCreate", ignoreModel, &ignoreModel, &flg);CHKERRQ(ierr);
3438c1cad2e7SMatthew G. Knepley   if (flg) {((DM_Plex *) dm->data)->ignoreModel = ignoreModel;}
3439250712c9SMatthew G. Knepley   ierr = DMPlexGetRefinementUniform(dm, &uniformOrig);CHKERRQ(ierr);
34409318fe57SMatthew G. Knepley   ierr = PetscOptionsBoundedInt("-dm_refine_pre", "The number of refinements before distribution", "DMCreate", prerefine, &prerefine, NULL,0);CHKERRQ(ierr);
344161a622f3SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_refine_remap_pre", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL);CHKERRQ(ierr);
3442250712c9SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_refine_uniform_pre", "Flag for uniform refinement before distribution", "DMCreate", uniform, &uniform, &flg);CHKERRQ(ierr);
3443250712c9SMatthew G. Knepley   if (flg) {ierr = DMPlexSetRefinementUniform(dm, uniform);CHKERRQ(ierr);}
3444250712c9SMatthew G. Knepley   ierr = PetscOptionsReal("-dm_refine_volume_limit_pre", "The maximum cell volume after refinement before distribution", "DMCreate", volume, &volume, &flg);CHKERRQ(ierr);
34459318fe57SMatthew G. Knepley   if (flg) {
34469318fe57SMatthew G. Knepley     ierr = DMPlexSetRefinementUniform(dm, PETSC_FALSE);CHKERRQ(ierr);
34479318fe57SMatthew G. Knepley     ierr = DMPlexSetRefinementLimit(dm, volume);CHKERRQ(ierr);
34489318fe57SMatthew G. Knepley     prerefine = PetscMax(prerefine, 1);
34499318fe57SMatthew G. Knepley   }
34509b44eab4SMatthew G. Knepley   for (r = 0; r < prerefine; ++r) {
34519b44eab4SMatthew G. Knepley     DM             rdm;
34529b44eab4SMatthew G. Knepley     PetscPointFunc coordFunc = ((DM_Plex*) dm->data)->coordFunc;
34539b44eab4SMatthew G. Knepley 
34549b44eab4SMatthew G. Knepley     ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
34559b44eab4SMatthew G. Knepley     ierr = DMRefine(dm, PetscObjectComm((PetscObject) dm), &rdm);CHKERRQ(ierr);
34569318fe57SMatthew G. Knepley     ierr = DMPlexReplace_Static(dm, &rdm);CHKERRQ(ierr);
34579b44eab4SMatthew G. Knepley     ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
345861a622f3SMatthew G. Knepley     if (coordFunc && remap) {
34599b44eab4SMatthew G. Knepley       ierr = DMPlexRemapGeometry(dm, 0.0, coordFunc);CHKERRQ(ierr);
34609b44eab4SMatthew G. Knepley       ((DM_Plex*) dm->data)->coordFunc = coordFunc;
34619b44eab4SMatthew G. Knepley     }
34629b44eab4SMatthew G. Knepley   }
3463250712c9SMatthew G. Knepley   ierr = DMPlexSetRefinementUniform(dm, uniformOrig);CHKERRQ(ierr);
34649318fe57SMatthew G. Knepley   /* Handle DMPlex extrusion before distribution */
3465d410b0cfSMatthew G. Knepley   ierr = PetscOptionsBoundedInt("-dm_extrude", "The number of layers to extrude", "", extLayers, &extLayers, NULL, 0);CHKERRQ(ierr);
34669318fe57SMatthew G. Knepley   if (extLayers) {
34679318fe57SMatthew G. Knepley     DM edm;
34689318fe57SMatthew G. Knepley 
3469d410b0cfSMatthew G. Knepley     ierr = DMExtrude(dm, extLayers, &edm);CHKERRQ(ierr);
34709318fe57SMatthew G. Knepley     ierr = DMPlexReplace_Static(dm, &edm);CHKERRQ(ierr);
347148d16a33SMatthew G. Knepley     ((DM_Plex *) dm->data)->coordFunc = NULL;
3472d410b0cfSMatthew G. Knepley     ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
3473d410b0cfSMatthew G. Knepley     extLayers = 0;
34749318fe57SMatthew G. Knepley   }
3475bdf63967SMatthew G. Knepley   /* Handle DMPlex reordering before distribution */
3476bdf63967SMatthew G. Knepley   ierr = MatGetOrderingList(&ordlist);CHKERRQ(ierr);
3477bdf63967SMatthew G. Knepley   ierr = PetscOptionsFList("-dm_plex_reorder", "Set mesh reordering type", "DMPlexGetOrdering", ordlist, MATORDERINGNATURAL, oname, sizeof(oname), &flg);CHKERRQ(ierr);
3478bdf63967SMatthew G. Knepley   if (flg) {
3479bdf63967SMatthew G. Knepley     DM pdm;
3480bdf63967SMatthew G. Knepley     IS perm;
3481bdf63967SMatthew G. Knepley 
3482bdf63967SMatthew G. Knepley     ierr = DMPlexGetOrdering(dm, oname, NULL, &perm);CHKERRQ(ierr);
3483bdf63967SMatthew G. Knepley     ierr = DMPlexPermute(dm, perm, &pdm);CHKERRQ(ierr);
3484bdf63967SMatthew G. Knepley     ierr = ISDestroy(&perm);CHKERRQ(ierr);
3485bdf63967SMatthew G. Knepley     ierr = DMPlexReplace_Static(dm, &pdm);CHKERRQ(ierr);
3486bdf63967SMatthew G. Knepley     ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
3487bdf63967SMatthew G. Knepley   }
34889b44eab4SMatthew G. Knepley   /* Handle DMPlex distribution */
3489e600fa54SMatthew G. Knepley   ierr = DMPlexDistributeGetDefault(dm, &distribute);CHKERRQ(ierr);
34909b44eab4SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_distribute", "Flag to redistribute a mesh among processes", "DMCreate", distribute, &distribute, NULL);CHKERRQ(ierr);
34919b44eab4SMatthew G. Knepley   ierr = PetscOptionsBoundedInt("-dm_distribute_overlap", "The size of the overlap halo", "DMCreate", overlap, &overlap, NULL, 0);CHKERRQ(ierr);
34929b44eab4SMatthew G. Knepley   if (distribute) {
34939b44eab4SMatthew G. Knepley     DM               pdm = NULL;
34949b44eab4SMatthew G. Knepley     PetscPartitioner part;
34959b44eab4SMatthew G. Knepley 
34969b44eab4SMatthew G. Knepley     ierr = DMPlexGetPartitioner(dm, &part);CHKERRQ(ierr);
34979b44eab4SMatthew G. Knepley     ierr = PetscPartitionerSetFromOptions(part);CHKERRQ(ierr);
34989b44eab4SMatthew G. Knepley     ierr = DMPlexDistribute(dm, overlap, NULL, &pdm);CHKERRQ(ierr);
34999b44eab4SMatthew G. Knepley     if (pdm) {
35009318fe57SMatthew G. Knepley       ierr = DMPlexReplace_Static(dm, &pdm);CHKERRQ(ierr);
35019b44eab4SMatthew G. Knepley     }
35029b44eab4SMatthew G. Knepley   }
35039318fe57SMatthew G. Knepley   /* Create coordinate space */
35049318fe57SMatthew G. Knepley   if (created) {
350561a622f3SMatthew G. Knepley     DM_Plex  *mesh = (DM_Plex *) dm->data;
35069318fe57SMatthew G. Knepley     PetscInt  degree = 1;
350761a622f3SMatthew G. Knepley     PetscBool periodic, flg;
35089318fe57SMatthew G. Knepley 
350961a622f3SMatthew G. Knepley     ierr = PetscOptionsBool("-dm_coord_space", "Use an FEM space for coordinates", "", coordSpace, &coordSpace, &flg);CHKERRQ(ierr);
35109318fe57SMatthew G. Knepley     ierr = PetscOptionsInt("-dm_coord_petscspace_degree", "FEM degree for coordinate space", "", degree, &degree, NULL);CHKERRQ(ierr);
351161a622f3SMatthew G. Knepley     if (coordSpace) {ierr = DMPlexCreateCoordinateSpace(dm, degree, mesh->coordFunc);CHKERRQ(ierr);}
351261a622f3SMatthew G. Knepley     if (flg && !coordSpace) {
351361a622f3SMatthew G. Knepley       DM           cdm;
351461a622f3SMatthew G. Knepley       PetscDS      cds;
351561a622f3SMatthew G. Knepley       PetscObject  obj;
351661a622f3SMatthew G. Knepley       PetscClassId id;
351761a622f3SMatthew G. Knepley 
351861a622f3SMatthew G. Knepley       ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
351961a622f3SMatthew G. Knepley       ierr = DMGetDS(cdm, &cds);CHKERRQ(ierr);
352061a622f3SMatthew G. Knepley       ierr = PetscDSGetDiscretization(cds, 0, &obj);CHKERRQ(ierr);
352161a622f3SMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
352261a622f3SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
352361a622f3SMatthew G. Knepley         PetscContainer dummy;
352461a622f3SMatthew G. Knepley 
352561a622f3SMatthew G. Knepley         ierr = PetscContainerCreate(PETSC_COMM_SELF, &dummy);CHKERRQ(ierr);
352661a622f3SMatthew G. Knepley         ierr = PetscObjectSetName((PetscObject) dummy, "coordinates");CHKERRQ(ierr);
352761a622f3SMatthew G. Knepley         ierr = DMSetField(cdm, 0, NULL, (PetscObject) dummy);CHKERRQ(ierr);
352861a622f3SMatthew G. Knepley         ierr = PetscContainerDestroy(&dummy);CHKERRQ(ierr);
352961a622f3SMatthew G. Knepley         ierr = DMClearDS(cdm);CHKERRQ(ierr);
353061a622f3SMatthew G. Knepley       }
353161a622f3SMatthew G. Knepley       mesh->coordFunc = NULL;
353261a622f3SMatthew G. Knepley     }
35339318fe57SMatthew G. Knepley     ierr = DMLocalizeCoordinates(dm);CHKERRQ(ierr);
353461a622f3SMatthew G. Knepley     ierr = DMGetPeriodicity(dm, &periodic, NULL, NULL, NULL);CHKERRQ(ierr);
353561a622f3SMatthew G. Knepley     if (periodic) {ierr = DMSetPeriodicity(dm, PETSC_TRUE, NULL, NULL, NULL);CHKERRQ(ierr);}
35369318fe57SMatthew G. Knepley   }
353768d4fef7SMatthew G. Knepley   /* Handle DMPlex refinement */
353861a622f3SMatthew G. Knepley   remap = PETSC_TRUE;
35395a856986SBarry Smith   ierr = PetscOptionsBoundedInt("-dm_refine", "The number of uniform refinements", "DMCreate", refine, &refine, NULL,0);CHKERRQ(ierr);
354061a622f3SMatthew G. Knepley   ierr = PetscOptionsBool("-dm_refine_remap", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL);CHKERRQ(ierr);
35415a856986SBarry Smith   ierr = PetscOptionsBoundedInt("-dm_refine_hierarchy", "The number of uniform refinements", "DMCreate", refine, &refine, &isHierarchy,0);CHKERRQ(ierr);
3542b6a0289aSMatthew G. Knepley   if (refine) {ierr = DMPlexSetRefinementUniform(dm, PETSC_TRUE);CHKERRQ(ierr);}
354368d4fef7SMatthew G. Knepley   if (refine && isHierarchy) {
3544acdc6f61SToby Isaac     DM *dms, coarseDM;
354568d4fef7SMatthew G. Knepley 
3546acdc6f61SToby Isaac     ierr = DMGetCoarseDM(dm, &coarseDM);CHKERRQ(ierr);
3547acdc6f61SToby Isaac     ierr = PetscObjectReference((PetscObject)coarseDM);CHKERRQ(ierr);
354868d4fef7SMatthew G. Knepley     ierr = PetscMalloc1(refine,&dms);CHKERRQ(ierr);
354968d4fef7SMatthew G. Knepley     ierr = DMRefineHierarchy(dm, refine, dms);CHKERRQ(ierr);
355068d4fef7SMatthew G. Knepley     /* Total hack since we do not pass in a pointer */
355168d4fef7SMatthew G. Knepley     ierr = DMPlexSwap_Static(dm, dms[refine-1]);CHKERRQ(ierr);
355268d4fef7SMatthew G. Knepley     if (refine == 1) {
3553a8fb8f29SToby Isaac       ierr = DMSetCoarseDM(dm, dms[0]);CHKERRQ(ierr);
35540aef6b92SMatthew G. Knepley       ierr = DMPlexSetRegularRefinement(dm, PETSC_TRUE);CHKERRQ(ierr);
355568d4fef7SMatthew G. Knepley     } else {
3556a8fb8f29SToby Isaac       ierr = DMSetCoarseDM(dm, dms[refine-2]);CHKERRQ(ierr);
35570aef6b92SMatthew G. Knepley       ierr = DMPlexSetRegularRefinement(dm, PETSC_TRUE);CHKERRQ(ierr);
3558a8fb8f29SToby Isaac       ierr = DMSetCoarseDM(dms[0], dms[refine-1]);CHKERRQ(ierr);
35590aef6b92SMatthew G. Knepley       ierr = DMPlexSetRegularRefinement(dms[0], PETSC_TRUE);CHKERRQ(ierr);
356068d4fef7SMatthew G. Knepley     }
3561acdc6f61SToby Isaac     ierr = DMSetCoarseDM(dms[refine-1], coarseDM);CHKERRQ(ierr);
3562acdc6f61SToby Isaac     ierr = PetscObjectDereference((PetscObject)coarseDM);CHKERRQ(ierr);
356368d4fef7SMatthew G. Knepley     /* Free DMs */
356468d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
3565547f7119SMatthew G. Knepley       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]);CHKERRQ(ierr);
356668d4fef7SMatthew G. Knepley       ierr = DMDestroy(&dms[r]);CHKERRQ(ierr);
356768d4fef7SMatthew G. Knepley     }
356868d4fef7SMatthew G. Knepley     ierr = PetscFree(dms);CHKERRQ(ierr);
356968d4fef7SMatthew G. Knepley   } else {
357068d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
35719318fe57SMatthew G. Knepley       DM             rdm;
357251a74b61SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex*) dm->data)->coordFunc;
357368d4fef7SMatthew G. Knepley 
35741a1499c8SBarry Smith       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
35759318fe57SMatthew G. Knepley       ierr = DMRefine(dm, PetscObjectComm((PetscObject) dm), &rdm);CHKERRQ(ierr);
357668d4fef7SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
35779318fe57SMatthew G. Knepley       ierr = DMPlexReplace_Static(dm, &rdm);CHKERRQ(ierr);
3578547f7119SMatthew G. Knepley       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
357961a622f3SMatthew G. Knepley       if (coordFunc && remap) {
358051a74b61SMatthew G. Knepley         ierr = DMPlexRemapGeometry(dm, 0.0, coordFunc);CHKERRQ(ierr);
358151a74b61SMatthew G. Knepley         ((DM_Plex*) dm->data)->coordFunc = coordFunc;
358251a74b61SMatthew G. Knepley       }
358368d4fef7SMatthew G. Knepley     }
358468d4fef7SMatthew G. Knepley   }
35853cf6fe12SMatthew G. Knepley   /* Handle DMPlex coarsening */
35865a856986SBarry Smith   ierr = PetscOptionsBoundedInt("-dm_coarsen", "Coarsen the mesh", "DMCreate", coarsen, &coarsen, NULL,0);CHKERRQ(ierr);
35875a856986SBarry Smith   ierr = PetscOptionsBoundedInt("-dm_coarsen_hierarchy", "The number of coarsenings", "DMCreate", coarsen, &coarsen, &isHierarchy,0);CHKERRQ(ierr);
3588b653a561SMatthew G. Knepley   if (coarsen && isHierarchy) {
3589b653a561SMatthew G. Knepley     DM *dms;
3590b653a561SMatthew G. Knepley 
3591b653a561SMatthew G. Knepley     ierr = PetscMalloc1(coarsen, &dms);CHKERRQ(ierr);
3592b653a561SMatthew G. Knepley     ierr = DMCoarsenHierarchy(dm, coarsen, dms);CHKERRQ(ierr);
3593b653a561SMatthew G. Knepley     /* Free DMs */
3594b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
3595547f7119SMatthew G. Knepley       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]);CHKERRQ(ierr);
3596b653a561SMatthew G. Knepley       ierr = DMDestroy(&dms[r]);CHKERRQ(ierr);
3597b653a561SMatthew G. Knepley     }
3598b653a561SMatthew G. Knepley     ierr = PetscFree(dms);CHKERRQ(ierr);
3599b653a561SMatthew G. Knepley   } else {
3600b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
36019318fe57SMatthew G. Knepley       DM             cdm;
36029318fe57SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex*) dm->data)->coordFunc;
36033cf6fe12SMatthew G. Knepley 
36043cf6fe12SMatthew G. Knepley       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
36059318fe57SMatthew G. Knepley       ierr = DMCoarsen(dm, PetscObjectComm((PetscObject) dm), &cdm);CHKERRQ(ierr);
36063cf6fe12SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
36079318fe57SMatthew G. Knepley       ierr = DMPlexReplace_Static(dm, &cdm);CHKERRQ(ierr);
3608547f7119SMatthew G. Knepley       ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
36099318fe57SMatthew G. Knepley       if (coordFunc) {
36109318fe57SMatthew G. Knepley         ierr = DMPlexRemapGeometry(dm, 0.0, coordFunc);CHKERRQ(ierr);
36119318fe57SMatthew G. Knepley         ((DM_Plex*) dm->data)->coordFunc = coordFunc;
36129318fe57SMatthew G. Knepley       }
36133cf6fe12SMatthew G. Knepley     }
3614b653a561SMatthew G. Knepley   }
3615909dfd52SMatthew G. Knepley   /* Handle ghost cells */
3616909dfd52SMatthew 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);
3617909dfd52SMatthew G. Knepley   if (ghostCells) {
3618909dfd52SMatthew G. Knepley     DM   gdm;
3619909dfd52SMatthew G. Knepley     char lname[PETSC_MAX_PATH_LEN];
3620909dfd52SMatthew G. Knepley 
3621909dfd52SMatthew G. Knepley     lname[0] = '\0';
3622909dfd52SMatthew G. Knepley     ierr = PetscOptionsString("-dm_plex_fv_ghost_cells_label", "Label name for ghost cells boundary", "DMCreate", lname, lname, sizeof(lname), &flg);CHKERRQ(ierr);
3623909dfd52SMatthew G. Knepley     ierr = DMPlexConstructGhostCells(dm, flg ? lname : NULL, NULL, &gdm);CHKERRQ(ierr);
3624909dfd52SMatthew G. Knepley     ierr = DMPlexReplace_Static(dm, &gdm);CHKERRQ(ierr);
3625909dfd52SMatthew G. Knepley   }
3626*6913077dSMatthew G. Knepley   /* Handle 1D order */
3627*6913077dSMatthew G. Knepley   {
3628*6913077dSMatthew G. Knepley     DM           cdm, rdm;
3629*6913077dSMatthew G. Knepley     PetscDS      cds;
3630*6913077dSMatthew G. Knepley     PetscObject  obj;
3631*6913077dSMatthew G. Knepley     PetscClassId id = PETSC_OBJECT_CLASSID;
3632*6913077dSMatthew G. Knepley     IS           perm;
3633*6913077dSMatthew G. Knepley     PetscInt     dim, Nf;
3634*6913077dSMatthew G. Knepley     PetscBool    distributed;
3635*6913077dSMatthew G. Knepley 
3636*6913077dSMatthew G. Knepley     ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
3637*6913077dSMatthew G. Knepley     ierr = DMPlexIsDistributed(dm, &distributed);CHKERRQ(ierr);
3638*6913077dSMatthew G. Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
3639*6913077dSMatthew G. Knepley     ierr = DMGetDS(cdm, &cds);CHKERRQ(ierr);
3640*6913077dSMatthew G. Knepley     ierr = PetscDSGetNumFields(cds, &Nf);CHKERRQ(ierr);
3641*6913077dSMatthew G. Knepley     if (Nf) {
3642*6913077dSMatthew G. Knepley       ierr = PetscDSGetDiscretization(cds, 0, &obj);CHKERRQ(ierr);
3643*6913077dSMatthew G. Knepley       ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
3644*6913077dSMatthew G. Knepley     }
3645*6913077dSMatthew G. Knepley     if (dim == 1 && !distributed && id != PETSCFE_CLASSID) {
3646*6913077dSMatthew G. Knepley       ierr = DMPlexGetOrdering1D(dm, &perm);CHKERRQ(ierr);
3647*6913077dSMatthew G. Knepley       ierr = DMPlexPermute(dm, perm, &rdm);CHKERRQ(ierr);
3648*6913077dSMatthew G. Knepley       ierr = DMPlexReplace_Static(dm, &rdm);CHKERRQ(ierr);
3649*6913077dSMatthew G. Knepley       ierr = ISDestroy(&perm);CHKERRQ(ierr);
3650*6913077dSMatthew G. Knepley     }
3651*6913077dSMatthew G. Knepley   }
36523cf6fe12SMatthew G. Knepley   /* Handle */
36531a1499c8SBarry Smith   ierr = DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm);CHKERRQ(ierr);
36540a6ba040SMatthew G. Knepley   ierr = PetscOptionsTail();CHKERRQ(ierr);
36550a6ba040SMatthew G. Knepley   PetscFunctionReturn(0);
36560a6ba040SMatthew G. Knepley }
36570a6ba040SMatthew G. Knepley 
3658552f7358SJed Brown static PetscErrorCode DMCreateGlobalVector_Plex(DM dm,Vec *vec)
3659552f7358SJed Brown {
3660552f7358SJed Brown   PetscErrorCode ierr;
3661552f7358SJed Brown 
3662552f7358SJed Brown   PetscFunctionBegin;
3663552f7358SJed Brown   ierr = DMCreateGlobalVector_Section_Private(dm,vec);CHKERRQ(ierr);
3664552f7358SJed Brown   /* ierr = VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM);CHKERRQ(ierr); */
3665552f7358SJed Brown   ierr = VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex);CHKERRQ(ierr);
3666d930f514SMatthew G. Knepley   ierr = VecSetOperation(*vec, VECOP_VIEWNATIVE, (void (*)(void)) VecView_Plex_Native);CHKERRQ(ierr);
36672c40f234SMatthew G. Knepley   ierr = VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex);CHKERRQ(ierr);
3668d930f514SMatthew G. Knepley   ierr = VecSetOperation(*vec, VECOP_LOADNATIVE, (void (*)(void)) VecLoad_Plex_Native);CHKERRQ(ierr);
3669552f7358SJed Brown   PetscFunctionReturn(0);
3670552f7358SJed Brown }
3671552f7358SJed Brown 
3672552f7358SJed Brown static PetscErrorCode DMCreateLocalVector_Plex(DM dm,Vec *vec)
3673552f7358SJed Brown {
3674552f7358SJed Brown   PetscErrorCode ierr;
3675552f7358SJed Brown 
3676552f7358SJed Brown   PetscFunctionBegin;
3677552f7358SJed Brown   ierr = DMCreateLocalVector_Section_Private(dm,vec);CHKERRQ(ierr);
3678552f7358SJed Brown   ierr = VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex_Local);CHKERRQ(ierr);
36792c40f234SMatthew G. Knepley   ierr = VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex_Local);CHKERRQ(ierr);
3680552f7358SJed Brown   PetscFunctionReturn(0);
3681552f7358SJed Brown }
3682552f7358SJed Brown 
3683793f3fe5SMatthew G. Knepley static PetscErrorCode DMGetDimPoints_Plex(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
3684793f3fe5SMatthew G. Knepley {
3685793f3fe5SMatthew G. Knepley   PetscInt       depth, d;
3686793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
3687793f3fe5SMatthew G. Knepley 
3688793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
3689793f3fe5SMatthew G. Knepley   ierr = DMPlexGetDepth(dm, &depth);CHKERRQ(ierr);
3690793f3fe5SMatthew G. Knepley   if (depth == 1) {
3691793f3fe5SMatthew G. Knepley     ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
3692793f3fe5SMatthew G. Knepley     if (dim == 0)      {ierr = DMPlexGetDepthStratum(dm, dim, pStart, pEnd);CHKERRQ(ierr);}
3693793f3fe5SMatthew G. Knepley     else if (dim == d) {ierr = DMPlexGetDepthStratum(dm, 1, pStart, pEnd);CHKERRQ(ierr);}
3694793f3fe5SMatthew G. Knepley     else               {*pStart = 0; *pEnd = 0;}
3695793f3fe5SMatthew G. Knepley   } else {
3696793f3fe5SMatthew G. Knepley     ierr = DMPlexGetDepthStratum(dm, dim, pStart, pEnd);CHKERRQ(ierr);
3697793f3fe5SMatthew G. Knepley   }
3698793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
3699793f3fe5SMatthew G. Knepley }
3700793f3fe5SMatthew G. Knepley 
370128d58a37SPierre Jolivet static PetscErrorCode DMGetNeighbors_Plex(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
3702502a2867SDave May {
3703502a2867SDave May   PetscSF           sf;
37040a19bb7dSprj-   PetscInt          niranks, njranks, n;
37050a19bb7dSprj-   const PetscMPIInt *iranks, *jranks;
37060a19bb7dSprj-   DM_Plex           *data = (DM_Plex*) dm->data;
37072f356facSMatthew G. Knepley   PetscErrorCode    ierr;
3708502a2867SDave May 
37092f356facSMatthew G. Knepley   PetscFunctionBegin;
3710502a2867SDave May   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
37110a19bb7dSprj-   if (!data->neighbors) {
371207a779feSPierre Jolivet     ierr = PetscSFSetUp(sf);CHKERRQ(ierr);
37130a19bb7dSprj-     ierr = PetscSFGetRootRanks(sf, &njranks, &jranks, NULL, NULL, NULL);CHKERRQ(ierr);
37140a19bb7dSprj-     ierr = PetscSFGetLeafRanks(sf, &niranks, &iranks, NULL, NULL);CHKERRQ(ierr);
37150a19bb7dSprj-     ierr = PetscMalloc1(njranks + niranks + 1, &data->neighbors);CHKERRQ(ierr);
37160a19bb7dSprj-     ierr = PetscArraycpy(data->neighbors + 1, jranks, njranks);CHKERRQ(ierr);
37170a19bb7dSprj-     ierr = PetscArraycpy(data->neighbors + njranks + 1, iranks, niranks);CHKERRQ(ierr);
37180a19bb7dSprj-     n = njranks + niranks;
37190a19bb7dSprj-     ierr = PetscSortRemoveDupsMPIInt(&n, data->neighbors + 1);CHKERRQ(ierr);
37200a19bb7dSprj-     /* The following cast should never fail: can't have more neighbors than PETSC_MPI_INT_MAX */
37210a19bb7dSprj-     ierr = PetscMPIIntCast(n, data->neighbors);CHKERRQ(ierr);
37220a19bb7dSprj-   }
37230a19bb7dSprj-   if (nranks) *nranks = data->neighbors[0];
37240a19bb7dSprj-   if (ranks) {
37250a19bb7dSprj-     if (data->neighbors[0]) *ranks = data->neighbors + 1;
37260a19bb7dSprj-     else                    *ranks = NULL;
37270a19bb7dSprj-   }
3728502a2867SDave May   PetscFunctionReturn(0);
3729502a2867SDave May }
3730502a2867SDave May 
37311eb70e55SToby Isaac PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM, DM, Mat, Vec, Vec);
37321eb70e55SToby Isaac 
373346fa42a0SMatthew G. Knepley static PetscErrorCode DMInitialize_Plex(DM dm)
3734552f7358SJed Brown {
3735713918a9SToby Isaac   PetscErrorCode ierr;
3736713918a9SToby Isaac 
3737552f7358SJed Brown   PetscFunctionBegin;
3738552f7358SJed Brown   dm->ops->view                            = DMView_Plex;
37392c40f234SMatthew G. Knepley   dm->ops->load                            = DMLoad_Plex;
3740552f7358SJed Brown   dm->ops->setfromoptions                  = DMSetFromOptions_Plex;
374138221697SMatthew G. Knepley   dm->ops->clone                           = DMClone_Plex;
3742552f7358SJed Brown   dm->ops->setup                           = DMSetUp_Plex;
37431bb6d2a8SBarry Smith   dm->ops->createlocalsection              = DMCreateLocalSection_Plex;
374466ad2231SToby Isaac   dm->ops->createdefaultconstraints        = DMCreateDefaultConstraints_Plex;
3745552f7358SJed Brown   dm->ops->createglobalvector              = DMCreateGlobalVector_Plex;
3746552f7358SJed Brown   dm->ops->createlocalvector               = DMCreateLocalVector_Plex;
3747184d77edSJed Brown   dm->ops->getlocaltoglobalmapping         = NULL;
37480298fd71SBarry Smith   dm->ops->createfieldis                   = NULL;
3749552f7358SJed Brown   dm->ops->createcoordinatedm              = DMCreateCoordinateDM_Plex;
3750f19dbd58SToby Isaac   dm->ops->createcoordinatefield           = DMCreateCoordinateField_Plex;
37510a6ba040SMatthew G. Knepley   dm->ops->getcoloring                     = NULL;
3752552f7358SJed Brown   dm->ops->creatematrix                    = DMCreateMatrix_Plex;
3753bceba477SMatthew G. Knepley   dm->ops->createinterpolation             = DMCreateInterpolation_Plex;
3754bd041c0cSMatthew G. Knepley   dm->ops->createmassmatrix                = DMCreateMassMatrix_Plex;
3755b4937a87SMatthew G. Knepley   dm->ops->createmassmatrixlumped          = DMCreateMassMatrixLumped_Plex;
37565a84ad33SLisandro Dalcin   dm->ops->createinjection                 = DMCreateInjection_Plex;
3757552f7358SJed Brown   dm->ops->refine                          = DMRefine_Plex;
37580a6ba040SMatthew G. Knepley   dm->ops->coarsen                         = DMCoarsen_Plex;
37590a6ba040SMatthew G. Knepley   dm->ops->refinehierarchy                 = DMRefineHierarchy_Plex;
3760b653a561SMatthew G. Knepley   dm->ops->coarsenhierarchy                = DMCoarsenHierarchy_Plex;
3761d410b0cfSMatthew G. Knepley   dm->ops->extrude                         = DMExtrude_Plex;
37620298fd71SBarry Smith   dm->ops->globaltolocalbegin              = NULL;
37630298fd71SBarry Smith   dm->ops->globaltolocalend                = NULL;
37640298fd71SBarry Smith   dm->ops->localtoglobalbegin              = NULL;
37650298fd71SBarry Smith   dm->ops->localtoglobalend                = NULL;
3766552f7358SJed Brown   dm->ops->destroy                         = DMDestroy_Plex;
3767552f7358SJed Brown   dm->ops->createsubdm                     = DMCreateSubDM_Plex;
37682adcc780SMatthew G. Knepley   dm->ops->createsuperdm                   = DMCreateSuperDM_Plex;
3769793f3fe5SMatthew G. Knepley   dm->ops->getdimpoints                    = DMGetDimPoints_Plex;
3770552f7358SJed Brown   dm->ops->locatepoints                    = DMLocatePoints_Plex;
37710709b2feSToby Isaac   dm->ops->projectfunctionlocal            = DMProjectFunctionLocal_Plex;
37720709b2feSToby Isaac   dm->ops->projectfunctionlabellocal       = DMProjectFunctionLabelLocal_Plex;
3773bfc4295aSToby Isaac   dm->ops->projectfieldlocal               = DMProjectFieldLocal_Plex;
37748c6c5593SMatthew G. Knepley   dm->ops->projectfieldlabellocal          = DMProjectFieldLabelLocal_Plex;
3775ece3a9fcSMatthew G. Knepley   dm->ops->projectbdfieldlabellocal        = DMProjectBdFieldLabelLocal_Plex;
37760709b2feSToby Isaac   dm->ops->computel2diff                   = DMComputeL2Diff_Plex;
3777b698f381SToby Isaac   dm->ops->computel2gradientdiff           = DMComputeL2GradientDiff_Plex;
37782a16baeaSToby Isaac   dm->ops->computel2fielddiff              = DMComputeL2FieldDiff_Plex;
377928d58a37SPierre Jolivet   dm->ops->getneighbors                    = DMGetNeighbors_Plex;
3780f1d73a7aSMatthew G. Knepley   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C",DMPlexInsertBoundaryValues_Plex);CHKERRQ(ierr);
378156cf3b9cSMatthew G. Knepley   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertTimeDerviativeBoundaryValues_C",DMPlexInsertTimeDerivativeBoundaryValues_Plex);CHKERRQ(ierr);
37828135c375SStefano Zampini   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",DMSetUpGLVisViewer_Plex);CHKERRQ(ierr);
378328d58a37SPierre Jolivet   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMCreateNeumannOverlap_C",DMCreateNeumannOverlap_Plex);CHKERRQ(ierr);
3784cb54e036SVaclav Hapla   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexGetOverlap_C",DMPlexGetOverlap_Plex);CHKERRQ(ierr);
3785e600fa54SMatthew G. Knepley   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexDistributeGetDefault_C",DMPlexDistributeGetDefault_Plex);CHKERRQ(ierr);
3786e600fa54SMatthew G. Knepley   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMPlexDistributeSetDefault_C",DMPlexDistributeSetDefault_Plex);CHKERRQ(ierr);
37871eb70e55SToby Isaac   ierr = PetscObjectComposeFunction((PetscObject)dm,"DMInterpolateSolution_C",DMInterpolateSolution_Plex);CHKERRQ(ierr);
3788552f7358SJed Brown   PetscFunctionReturn(0);
3789552f7358SJed Brown }
3790552f7358SJed Brown 
379146fa42a0SMatthew G. Knepley PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm)
379263a16f15SMatthew G. Knepley {
379363a16f15SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex *) dm->data;
379463a16f15SMatthew G. Knepley   PetscErrorCode ierr;
379563a16f15SMatthew G. Knepley 
379663a16f15SMatthew G. Knepley   PetscFunctionBegin;
379763a16f15SMatthew G. Knepley   mesh->refct++;
379863a16f15SMatthew G. Knepley   (*newdm)->data = mesh;
379963a16f15SMatthew G. Knepley   ierr = PetscObjectChangeTypeName((PetscObject) *newdm, DMPLEX);CHKERRQ(ierr);
380063a16f15SMatthew G. Knepley   ierr = DMInitialize_Plex(*newdm);CHKERRQ(ierr);
380163a16f15SMatthew G. Knepley   PetscFunctionReturn(0);
380263a16f15SMatthew G. Knepley }
380363a16f15SMatthew G. Knepley 
38048818961aSMatthew G Knepley /*MC
38058818961aSMatthew G Knepley   DMPLEX = "plex" - A DM object that encapsulates an unstructured mesh, or CW Complex, which can be expressed using a Hasse Diagram.
38068818961aSMatthew G Knepley                     In the local representation, Vecs contain all unknowns in the interior and shared boundary. This is
38078818961aSMatthew G Knepley                     specified by a PetscSection object. Ownership in the global representation is determined by
38088818961aSMatthew G Knepley                     ownership of the underlying DMPlex points. This is specified by another PetscSection object.
38098818961aSMatthew G Knepley 
3810e5893cccSMatthew G. Knepley   Options Database Keys:
3811250712c9SMatthew G. Knepley + -dm_refine_pre                     - Refine mesh before distribution
3812250712c9SMatthew G. Knepley + -dm_refine_uniform_pre             - Choose uniform or generator-based refinement
3813250712c9SMatthew G. Knepley + -dm_refine_volume_limit_pre        - Cell volume limit after pre-refinement using generator
3814250712c9SMatthew G. Knepley . -dm_distribute                     - Distribute mesh across processes
3815250712c9SMatthew G. Knepley . -dm_distribute_overlap             - Number of cells to overlap for distribution
3816250712c9SMatthew G. Knepley . -dm_refine                         - Refine mesh after distribution
3817250712c9SMatthew G. Knepley . -dm_plex_hash_location             - Use grid hashing for point location
3818ddce0771SMatthew G. Knepley . -dm_plex_hash_box_faces <n,m,p>    - The number of divisions in each direction of the grid hash
3819f12cf164SMatthew G. Knepley . -dm_plex_partition_balance         - Attempt to evenly divide points on partition boundary between processes
3820f12cf164SMatthew G. Knepley . -dm_plex_remesh_bd                 - Allow changes to the boundary on remeshing
3821f12cf164SMatthew G. Knepley . -dm_plex_max_projection_height     - Maxmimum mesh point height used to project locally
3822f12cf164SMatthew G. Knepley . -dm_plex_regular_refinement        - Use special nested projection algorithm for regular refinement
3823250712c9SMatthew G. Knepley . -dm_plex_check_all                 - Perform all shecks below
3824f12cf164SMatthew G. Knepley . -dm_plex_check_symmetry            - Check that the adjacency information in the mesh is symmetric
3825f12cf164SMatthew G. Knepley . -dm_plex_check_skeleton <celltype> - Check that each cell has the correct number of vertices
3826f12cf164SMatthew 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
3827f12cf164SMatthew G. Knepley . -dm_plex_check_geometry            - Check that cells have positive volume
3828f12cf164SMatthew G. Knepley . -dm_view :mesh.tex:ascii_latex     - View the mesh in LaTeX/TikZ
3829e5893cccSMatthew G. Knepley . -dm_plex_view_scale <num>          - Scale the TikZ
3830e5893cccSMatthew G. Knepley - -dm_plex_print_fem <num>           - View FEM assembly information, such as element vectors and matrices
3831e5893cccSMatthew G. Knepley 
38328818961aSMatthew G Knepley   Level: intermediate
38338818961aSMatthew G Knepley 
38348818961aSMatthew G Knepley .seealso: DMType, DMPlexCreate(), DMCreate(), DMSetType()
38358818961aSMatthew G Knepley M*/
38368818961aSMatthew G Knepley 
38378cc058d9SJed Brown PETSC_EXTERN PetscErrorCode DMCreate_Plex(DM dm)
3838552f7358SJed Brown {
3839552f7358SJed Brown   DM_Plex       *mesh;
3840412e9a14SMatthew G. Knepley   PetscInt       unit;
3841552f7358SJed Brown   PetscErrorCode ierr;
3842552f7358SJed Brown 
3843552f7358SJed Brown   PetscFunctionBegin;
3844552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3845b00a9115SJed Brown   ierr     = PetscNewLog(dm,&mesh);CHKERRQ(ierr);
3846552f7358SJed Brown   dm->data = mesh;
3847552f7358SJed Brown 
3848552f7358SJed Brown   mesh->refct             = 1;
384982f516ccSBarry Smith   ierr                    = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->coneSection);CHKERRQ(ierr);
3850552f7358SJed Brown   mesh->maxConeSize       = 0;
38510298fd71SBarry Smith   mesh->cones             = NULL;
38520298fd71SBarry Smith   mesh->coneOrientations  = NULL;
385382f516ccSBarry Smith   ierr                    = PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->supportSection);CHKERRQ(ierr);
3854552f7358SJed Brown   mesh->maxSupportSize    = 0;
38550298fd71SBarry Smith   mesh->supports          = NULL;
3856552f7358SJed Brown   mesh->refinementUniform = PETSC_TRUE;
3857552f7358SJed Brown   mesh->refinementLimit   = -1.0;
3858e600fa54SMatthew G. Knepley   mesh->distDefault       = PETSC_TRUE;
38597d0f5628SVaclav Hapla   mesh->interpolated      = DMPLEX_INTERPOLATED_INVALID;
38607d0f5628SVaclav Hapla   mesh->interpolatedCollective = DMPLEX_INTERPOLATED_INVALID;
3861552f7358SJed Brown 
38620298fd71SBarry Smith   mesh->facesTmp = NULL;
3863552f7358SJed Brown 
3864d9deefdfSMatthew G. Knepley   mesh->tetgenOpts   = NULL;
3865d9deefdfSMatthew G. Knepley   mesh->triangleOpts = NULL;
386677623264SMatthew G. Knepley   ierr = PetscPartitionerCreate(PetscObjectComm((PetscObject)dm), &mesh->partitioner);CHKERRQ(ierr);
38672e62ab5aSMatthew G. Knepley   mesh->remeshBd     = PETSC_FALSE;
3868d9deefdfSMatthew G. Knepley 
38690298fd71SBarry Smith   mesh->subpointMap = NULL;
3870552f7358SJed Brown 
38718865f1eaSKarl Rupp   for (unit = 0; unit < NUM_PETSC_UNITS; ++unit) mesh->scale[unit] = 1.0;
3872552f7358SJed Brown 
38730aef6b92SMatthew G. Knepley   mesh->regularRefinement   = PETSC_FALSE;
3874df0420ecSMatthew G. Knepley   mesh->depthState          = -1;
3875ba2698f1SMatthew G. Knepley   mesh->celltypeState       = -1;
38760298fd71SBarry Smith   mesh->globalVertexNumbers = NULL;
38770298fd71SBarry Smith   mesh->globalCellNumbers   = NULL;
3878a68b90caSToby Isaac   mesh->anchorSection       = NULL;
3879a68b90caSToby Isaac   mesh->anchorIS            = NULL;
388041e6d900SToby Isaac   mesh->createanchors       = NULL;
3881fa73a4e1SToby Isaac   mesh->computeanchormatrix = NULL;
3882d961a43aSToby Isaac   mesh->parentSection       = NULL;
3883d961a43aSToby Isaac   mesh->parents             = NULL;
3884d961a43aSToby Isaac   mesh->childIDs            = NULL;
3885d961a43aSToby Isaac   mesh->childSection        = NULL;
3886d961a43aSToby Isaac   mesh->children            = NULL;
3887d6a7ad0dSToby Isaac   mesh->referenceTree       = NULL;
3888dcbd3bf7SToby Isaac   mesh->getchildsymmetry    = NULL;
3889552f7358SJed Brown   mesh->vtkCellHeight       = 0;
3890e228b242SToby Isaac   mesh->useAnchors          = PETSC_FALSE;
3891552f7358SJed Brown 
3892b29cfa1cSToby Isaac   mesh->maxProjectionHeight = 0;
3893b29cfa1cSToby Isaac 
38940a19bb7dSprj-   mesh->neighbors           = NULL;
38950a19bb7dSprj- 
3896552f7358SJed Brown   mesh->printSetValues = PETSC_FALSE;
3897552f7358SJed Brown   mesh->printFEM       = 0;
38986113b454SMatthew G. Knepley   mesh->printTol       = 1.0e-10;
3899552f7358SJed Brown 
3900552f7358SJed Brown   ierr = DMInitialize_Plex(dm);CHKERRQ(ierr);
3901552f7358SJed Brown   PetscFunctionReturn(0);
3902552f7358SJed Brown }
3903552f7358SJed Brown 
3904552f7358SJed Brown /*@
3905552f7358SJed Brown   DMPlexCreate - Creates a DMPlex object, which encapsulates an unstructured mesh, or CW complex, which can be expressed using a Hasse Diagram.
3906552f7358SJed Brown 
3907d083f849SBarry Smith   Collective
3908552f7358SJed Brown 
3909552f7358SJed Brown   Input Parameter:
3910552f7358SJed Brown . comm - The communicator for the DMPlex object
3911552f7358SJed Brown 
3912552f7358SJed Brown   Output Parameter:
3913552f7358SJed Brown . mesh  - The DMPlex object
3914552f7358SJed Brown 
3915552f7358SJed Brown   Level: beginner
3916552f7358SJed Brown 
3917552f7358SJed Brown @*/
3918552f7358SJed Brown PetscErrorCode DMPlexCreate(MPI_Comm comm, DM *mesh)
3919552f7358SJed Brown {
3920552f7358SJed Brown   PetscErrorCode ierr;
3921552f7358SJed Brown 
3922552f7358SJed Brown   PetscFunctionBegin;
3923552f7358SJed Brown   PetscValidPointer(mesh,2);
3924552f7358SJed Brown   ierr = DMCreate(comm, mesh);CHKERRQ(ierr);
3925552f7358SJed Brown   ierr = DMSetType(*mesh, DMPLEX);CHKERRQ(ierr);
3926552f7358SJed Brown   PetscFunctionReturn(0);
3927552f7358SJed Brown }
3928552f7358SJed Brown 
3929b09969d6SVaclav Hapla /*@C
3930b09969d6SVaclav Hapla   DMPlexBuildFromCellListParallel - Build distributed DMPLEX topology from a list of vertices for each cell (common mesh generator output)
3931b09969d6SVaclav Hapla 
3932b09969d6SVaclav Hapla   Input Parameters:
3933b09969d6SVaclav Hapla + dm - The DM
3934b09969d6SVaclav Hapla . numCells - The number of cells owned by this process
3935325d53feSBarry Smith . numVertices - The number of vertices to be owned by this process, or PETSC_DECIDE
3936325d53feSBarry Smith . NVertices - The global number of vertices, or PETSC_DETERMINE
3937b09969d6SVaclav Hapla . numCorners - The number of vertices for each cell
39385e488331SVaclav Hapla - cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
3939b09969d6SVaclav Hapla 
3940be8c289dSNicolas Barral   Output Parameters:
3941be8c289dSNicolas Barral + vertexSF - (Optional) SF describing complete vertex ownership
3942be8c289dSNicolas Barral - verticesAdjSaved - (Optional) vertex adjacency array
3943b09969d6SVaclav Hapla 
3944b09969d6SVaclav Hapla   Notes:
3945b09969d6SVaclav Hapla   Two triangles sharing a face
3946b09969d6SVaclav Hapla $
3947b09969d6SVaclav Hapla $        2
3948b09969d6SVaclav Hapla $      / | \
3949b09969d6SVaclav Hapla $     /  |  \
3950b09969d6SVaclav Hapla $    /   |   \
3951b09969d6SVaclav Hapla $   0  0 | 1  3
3952b09969d6SVaclav Hapla $    \   |   /
3953b09969d6SVaclav Hapla $     \  |  /
3954b09969d6SVaclav Hapla $      \ | /
3955b09969d6SVaclav Hapla $        1
3956b09969d6SVaclav Hapla would have input
3957b09969d6SVaclav Hapla $  numCells = 2, numVertices = 4
3958b09969d6SVaclav Hapla $  cells = [0 1 2  1 3 2]
3959b09969d6SVaclav Hapla $
3960b09969d6SVaclav Hapla which would result in the DMPlex
3961b09969d6SVaclav Hapla $
3962b09969d6SVaclav Hapla $        4
3963b09969d6SVaclav Hapla $      / | \
3964b09969d6SVaclav Hapla $     /  |  \
3965b09969d6SVaclav Hapla $    /   |   \
3966b09969d6SVaclav Hapla $   2  0 | 1  5
3967b09969d6SVaclav Hapla $    \   |   /
3968b09969d6SVaclav Hapla $     \  |  /
3969b09969d6SVaclav Hapla $      \ | /
3970b09969d6SVaclav Hapla $        3
3971b09969d6SVaclav Hapla 
397225b6865aSVaclav Hapla   Vertices are implicitly numbered consecutively 0,...,NVertices.
397325b6865aSVaclav Hapla   Each rank owns a chunk of numVertices consecutive vertices.
397425b6865aSVaclav Hapla   If numVertices is PETSC_DECIDE, PETSc will distribute them as evenly as possible using PetscLayout.
3975325d53feSBarry Smith   If NVertices is PETSC_DETERMINE and numVertices is PETSC_DECIDE, NVertices is computed by PETSc as the maximum vertex index in cells + 1.
3976325d53feSBarry Smith   If only NVertices is PETSC_DETERMINE, it is computed as the sum of numVertices over all ranks.
397725b6865aSVaclav Hapla 
3978b09969d6SVaclav Hapla   The cell distribution is arbitrary non-overlapping, independent of the vertex distribution.
3979b09969d6SVaclav Hapla 
3980b09969d6SVaclav Hapla   Not currently supported in Fortran.
3981b09969d6SVaclav Hapla 
3982b09969d6SVaclav Hapla   Level: advanced
3983b09969d6SVaclav Hapla 
3984b09969d6SVaclav Hapla .seealso: DMPlexBuildFromCellList(), DMPlexCreateFromCellListParallelPetsc(), DMPlexBuildCoordinatesFromCellListParallel()
3985b09969d6SVaclav Hapla @*/
3986be8c289dSNicolas Barral PetscErrorCode DMPlexBuildFromCellListParallel(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt NVertices, PetscInt numCorners, const PetscInt cells[], PetscSF *vertexSF, PetscInt **verticesAdjSaved)
3987a47d0d45SMatthew G. Knepley {
39882464107aSksagiyam   PetscSF         sfPoint;
39892464107aSksagiyam   PetscLayout     layout;
399082fb893eSVaclav Hapla   PetscInt        numVerticesAdj, *verticesAdj, *cones, c, p;
3991a47d0d45SMatthew G. Knepley   PetscErrorCode  ierr;
3992a47d0d45SMatthew G. Knepley 
3993a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
399425b6865aSVaclav Hapla   PetscValidLogicalCollectiveInt(dm,NVertices,4);
3995b09969d6SVaclav Hapla   ierr = PetscLogEventBegin(DMPLEX_BuildFromCellList,dm,0,0,0);CHKERRQ(ierr);
399625b6865aSVaclav Hapla   /* Get/check global number of vertices */
399725b6865aSVaclav Hapla   {
399825b6865aSVaclav Hapla     PetscInt NVerticesInCells, i;
399925b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
400025b6865aSVaclav Hapla 
400125b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
400225b6865aSVaclav Hapla     NVerticesInCells = PETSC_MIN_INT;
400325b6865aSVaclav Hapla     for (i=0; i<len; i++) if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
400425b6865aSVaclav Hapla     ++NVerticesInCells;
4005ffc4695bSBarry Smith     ierr = MPI_Allreduce(MPI_IN_PLACE, &NVerticesInCells, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm));CHKERRMPI(ierr);
400625b6865aSVaclav Hapla 
400725b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE && NVertices == PETSC_DECIDE) NVertices = NVerticesInCells;
40082c71b3e2SJacob Faibussowitsch     else PetscCheckFalse(NVertices != PETSC_DECIDE && NVertices < NVerticesInCells,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);
400925b6865aSVaclav Hapla   }
40109079aca8SVaclav Hapla   /* Count locally unique vertices */
40119079aca8SVaclav Hapla   {
40129079aca8SVaclav Hapla     PetscHSetI vhash;
40139079aca8SVaclav Hapla     PetscInt off = 0;
40149079aca8SVaclav Hapla 
4015e8f14785SLisandro Dalcin     ierr = PetscHSetICreate(&vhash);CHKERRQ(ierr);
4016a47d0d45SMatthew G. Knepley     for (c = 0; c < numCells; ++c) {
4017a47d0d45SMatthew G. Knepley       for (p = 0; p < numCorners; ++p) {
4018e8f14785SLisandro Dalcin         ierr = PetscHSetIAdd(vhash, cells[c*numCorners+p]);CHKERRQ(ierr);
4019a47d0d45SMatthew G. Knepley       }
4020a47d0d45SMatthew G. Knepley     }
4021e8f14785SLisandro Dalcin     ierr = PetscHSetIGetSize(vhash, &numVerticesAdj);CHKERRQ(ierr);
4022be8c289dSNicolas Barral     if (!verticesAdjSaved) { ierr = PetscMalloc1(numVerticesAdj, &verticesAdj);CHKERRQ(ierr); }
4023be8c289dSNicolas Barral     else { verticesAdj = *verticesAdjSaved; }
4024e8f14785SLisandro Dalcin     ierr = PetscHSetIGetElems(vhash, &off, verticesAdj);CHKERRQ(ierr);
4025e8f14785SLisandro Dalcin     ierr = PetscHSetIDestroy(&vhash);CHKERRQ(ierr);
40262c71b3e2SJacob Faibussowitsch     PetscCheckFalse(off != numVerticesAdj,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid number of local vertices %D should be %D", off, numVerticesAdj);
4027a47d0d45SMatthew G. Knepley   }
40289079aca8SVaclav Hapla   ierr = PetscSortInt(numVerticesAdj, verticesAdj);CHKERRQ(ierr);
4029a47d0d45SMatthew G. Knepley   /* Create cones */
4030a47d0d45SMatthew G. Knepley   ierr = DMPlexSetChart(dm, 0, numCells+numVerticesAdj);CHKERRQ(ierr);
4031a47d0d45SMatthew G. Knepley   for (c = 0; c < numCells; ++c) {ierr = DMPlexSetConeSize(dm, c, numCorners);CHKERRQ(ierr);}
4032a47d0d45SMatthew G. Knepley   ierr = DMSetUp(dm);CHKERRQ(ierr);
4033961cfab0SVaclav Hapla   ierr = DMPlexGetCones(dm,&cones);CHKERRQ(ierr);
4034a47d0d45SMatthew G. Knepley   for (c = 0; c < numCells; ++c) {
4035a47d0d45SMatthew G. Knepley     for (p = 0; p < numCorners; ++p) {
4036a47d0d45SMatthew G. Knepley       const PetscInt gv = cells[c*numCorners+p];
4037a47d0d45SMatthew G. Knepley       PetscInt       lv;
4038a47d0d45SMatthew G. Knepley 
40399079aca8SVaclav Hapla       /* Positions within verticesAdj form 0-based local vertex numbering;
40409079aca8SVaclav Hapla          we need to shift it by numCells to get correct DAG points (cells go first) */
4041a47d0d45SMatthew G. Knepley       ierr = PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv);CHKERRQ(ierr);
40422c71b3e2SJacob Faibussowitsch       PetscCheckFalse(lv < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not find global vertex %D in local connectivity", gv);
4043961cfab0SVaclav Hapla       cones[c*numCorners+p] = lv+numCells;
4044a47d0d45SMatthew G. Knepley     }
4045a47d0d45SMatthew G. Knepley   }
40462464107aSksagiyam   /* Build point sf */
40472464107aSksagiyam   ierr = PetscLayoutCreate(PetscObjectComm((PetscObject)dm), &layout);CHKERRQ(ierr);
40482464107aSksagiyam   ierr = PetscLayoutSetSize(layout, NVertices);CHKERRQ(ierr);
40492464107aSksagiyam   ierr = PetscLayoutSetLocalSize(layout, numVertices);CHKERRQ(ierr);
40502464107aSksagiyam   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
40512464107aSksagiyam   ierr = PetscSFCreateByMatchingIndices(layout, numVerticesAdj, verticesAdj, NULL, numCells, numVerticesAdj, verticesAdj, NULL, numCells, vertexSF, &sfPoint);CHKERRQ(ierr);
40522464107aSksagiyam   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
4053be8c289dSNicolas Barral   if (!verticesAdjSaved) { ierr = PetscFree(verticesAdj);CHKERRQ(ierr); }
4054a47d0d45SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) sfPoint, "point SF");CHKERRQ(ierr);
40552464107aSksagiyam   if (dm->sf) {
40562464107aSksagiyam     const char *prefix;
40572464107aSksagiyam 
40582464107aSksagiyam     ierr = PetscObjectGetOptionsPrefix((PetscObject)dm->sf, &prefix);CHKERRQ(ierr);
40592464107aSksagiyam     ierr = PetscObjectSetOptionsPrefix((PetscObject)sfPoint, prefix);CHKERRQ(ierr);
40602464107aSksagiyam   }
40612464107aSksagiyam   ierr = DMSetPointSF(dm, sfPoint);CHKERRQ(ierr);
40622464107aSksagiyam   ierr = PetscSFDestroy(&sfPoint);CHKERRQ(ierr);
40632464107aSksagiyam   if (vertexSF) {ierr = PetscObjectSetName((PetscObject)(*vertexSF), "Vertex Ownership SF");CHKERRQ(ierr);}
4064a47d0d45SMatthew G. Knepley   /* Fill in the rest of the topology structure */
4065a47d0d45SMatthew G. Knepley   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
4066a47d0d45SMatthew G. Knepley   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
4067b09969d6SVaclav Hapla   ierr = PetscLogEventEnd(DMPLEX_BuildFromCellList,dm,0,0,0);CHKERRQ(ierr);
4068a47d0d45SMatthew G. Knepley   PetscFunctionReturn(0);
4069a47d0d45SMatthew G. Knepley }
4070a47d0d45SMatthew G. Knepley 
4071b09969d6SVaclav Hapla /*@C
4072b09969d6SVaclav Hapla   DMPlexBuildCoordinatesFromCellListParallel - Build DM coordinates from a list of coordinates for each owned vertex (common mesh generator output)
4073b09969d6SVaclav Hapla 
4074b09969d6SVaclav Hapla   Input Parameters:
4075b09969d6SVaclav Hapla + dm - The DM
4076b09969d6SVaclav Hapla . spaceDim - The spatial dimension used for coordinates
4077b09969d6SVaclav Hapla . sfVert - SF describing complete vertex ownership
4078b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
4079b09969d6SVaclav Hapla 
4080b09969d6SVaclav Hapla   Level: advanced
4081b09969d6SVaclav Hapla 
4082b09969d6SVaclav Hapla   Notes:
4083b09969d6SVaclav Hapla   Not currently supported in Fortran.
4084b09969d6SVaclav Hapla 
4085b09969d6SVaclav Hapla .seealso: DMPlexBuildCoordinatesFromCellList(), DMPlexCreateFromCellListParallelPetsc(), DMPlexBuildFromCellListParallel()
4086b09969d6SVaclav Hapla @*/
40871edcf0b2SVaclav Hapla PetscErrorCode DMPlexBuildCoordinatesFromCellListParallel(DM dm, PetscInt spaceDim, PetscSF sfVert, const PetscReal vertexCoords[])
4088a47d0d45SMatthew G. Knepley {
4089a47d0d45SMatthew G. Knepley   PetscSection   coordSection;
4090a47d0d45SMatthew G. Knepley   Vec            coordinates;
4091a47d0d45SMatthew G. Knepley   PetscScalar   *coords;
40921edcf0b2SVaclav Hapla   PetscInt       numVertices, numVerticesAdj, coordSize, v, vStart, vEnd;
4093a47d0d45SMatthew G. Knepley   PetscErrorCode ierr;
4094a47d0d45SMatthew G. Knepley 
4095a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
4096b09969d6SVaclav Hapla   ierr = PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0);CHKERRQ(ierr);
40971edcf0b2SVaclav Hapla   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
40982c71b3e2SJacob Faibussowitsch   PetscCheckFalse(vStart < 0 || vEnd < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
40999596c6baSMatthew G. Knepley   ierr = DMSetCoordinateDim(dm, spaceDim);CHKERRQ(ierr);
4100a47d0d45SMatthew G. Knepley   ierr = PetscSFGetGraph(sfVert, &numVertices, &numVerticesAdj, NULL, NULL);CHKERRQ(ierr);
41012c71b3e2SJacob Faibussowitsch   PetscCheckFalse(vEnd - vStart != numVerticesAdj,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Supplied sfVert has wrong number of leaves = %D != %D = vEnd - vStart",numVerticesAdj,vEnd - vStart);
4102a47d0d45SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
4103a47d0d45SMatthew G. Knepley   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
4104a47d0d45SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(coordSection, 0, spaceDim);CHKERRQ(ierr);
41051edcf0b2SVaclav Hapla   ierr = PetscSectionSetChart(coordSection, vStart, vEnd);CHKERRQ(ierr);
41061edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
4107a47d0d45SMatthew G. Knepley     ierr = PetscSectionSetDof(coordSection, v, spaceDim);CHKERRQ(ierr);
4108a47d0d45SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(coordSection, v, 0, spaceDim);CHKERRQ(ierr);
4109a47d0d45SMatthew G. Knepley   }
4110a47d0d45SMatthew G. Knepley   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
4111a47d0d45SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
4112a47d0d45SMatthew G. Knepley   ierr = VecCreate(PetscObjectComm((PetscObject)dm), &coordinates);CHKERRQ(ierr);
4113a47d0d45SMatthew G. Knepley   ierr = VecSetBlockSize(coordinates, spaceDim);CHKERRQ(ierr);
4114a47d0d45SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
4115a47d0d45SMatthew G. Knepley   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
4116a47d0d45SMatthew G. Knepley   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
4117a47d0d45SMatthew G. Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
4118a47d0d45SMatthew G. Knepley   {
4119a47d0d45SMatthew G. Knepley     MPI_Datatype coordtype;
4120a47d0d45SMatthew G. Knepley 
4121a47d0d45SMatthew G. Knepley     /* Need a temp buffer for coords if we have complex/single */
4122ffc4695bSBarry Smith     ierr = MPI_Type_contiguous(spaceDim, MPIU_SCALAR, &coordtype);CHKERRMPI(ierr);
4123ffc4695bSBarry Smith     ierr = MPI_Type_commit(&coordtype);CHKERRMPI(ierr);
412421016a8bSBarry Smith #if defined(PETSC_USE_COMPLEX)
412521016a8bSBarry Smith     {
412621016a8bSBarry Smith     PetscScalar *svertexCoords;
412721016a8bSBarry Smith     PetscInt    i;
41283612f820SVaclav Hapla     ierr = PetscMalloc1(numVertices*spaceDim,&svertexCoords);CHKERRQ(ierr);
41293612f820SVaclav Hapla     for (i=0; i<numVertices*spaceDim; i++) svertexCoords[i] = vertexCoords[i];
4130ad227feaSJunchao Zhang     ierr = PetscSFBcastBegin(sfVert, coordtype, svertexCoords, coords,MPI_REPLACE);CHKERRQ(ierr);
4131ad227feaSJunchao Zhang     ierr = PetscSFBcastEnd(sfVert, coordtype, svertexCoords, coords,MPI_REPLACE);CHKERRQ(ierr);
413221016a8bSBarry Smith     ierr = PetscFree(svertexCoords);CHKERRQ(ierr);
413321016a8bSBarry Smith     }
413421016a8bSBarry Smith #else
4135ad227feaSJunchao Zhang     ierr = PetscSFBcastBegin(sfVert, coordtype, vertexCoords, coords,MPI_REPLACE);CHKERRQ(ierr);
4136ad227feaSJunchao Zhang     ierr = PetscSFBcastEnd(sfVert, coordtype, vertexCoords, coords,MPI_REPLACE);CHKERRQ(ierr);
413721016a8bSBarry Smith #endif
4138ffc4695bSBarry Smith     ierr = MPI_Type_free(&coordtype);CHKERRMPI(ierr);
4139a47d0d45SMatthew G. Knepley   }
4140a47d0d45SMatthew G. Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
4141a47d0d45SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
4142a47d0d45SMatthew G. Knepley   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
4143b09969d6SVaclav Hapla   ierr = PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0);CHKERRQ(ierr);
4144a47d0d45SMatthew G. Knepley   PetscFunctionReturn(0);
4145a47d0d45SMatthew G. Knepley }
4146a47d0d45SMatthew G. Knepley 
4147c3edce3dSSatish Balay /*@
4148b09969d6SVaclav Hapla   DMPlexCreateFromCellListParallelPetsc - Create distributed DMPLEX from a list of vertices for each cell (common mesh generator output)
4149a47d0d45SMatthew G. Knepley 
4150a47d0d45SMatthew G. Knepley   Input Parameters:
4151a47d0d45SMatthew G. Knepley + comm - The communicator
4152a47d0d45SMatthew G. Knepley . dim - The topological dimension of the mesh
4153a47d0d45SMatthew G. Knepley . numCells - The number of cells owned by this process
415425b6865aSVaclav Hapla . numVertices - The number of vertices owned by this process, or PETSC_DECIDE
415525b6865aSVaclav Hapla . NVertices - The global number of vertices, or PETSC_DECIDE
4156a47d0d45SMatthew G. Knepley . numCorners - The number of vertices for each cell
4157a47d0d45SMatthew G. Knepley . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
4158a47d0d45SMatthew G. Knepley . cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
4159a47d0d45SMatthew G. Knepley . spaceDim - The spatial dimension used for coordinates
4160a47d0d45SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
4161a47d0d45SMatthew G. Knepley 
4162d8d19677SJose E. Roman   Output Parameters:
416318d54ad4SMichael Lange + dm - The DM
4164be8c289dSNicolas Barral . vertexSF - (Optional) SF describing complete vertex ownership
4165be8c289dSNicolas Barral - verticesAdjSaved - (Optional) vertex adjacency array
4166a47d0d45SMatthew G. Knepley 
4167b09969d6SVaclav Hapla   Notes:
4168b09969d6SVaclav Hapla   This function is just a convenient sequence of DMCreate(), DMSetType(), DMSetDimension(),
4169b09969d6SVaclav Hapla   DMPlexBuildFromCellListParallel(), DMPlexInterpolate(), DMPlexBuildCoordinatesFromCellListParallel()
4170a47d0d45SMatthew G. Knepley 
417125b6865aSVaclav Hapla   See DMPlexBuildFromCellListParallel() for an example and details about the topology-related parameters.
417225b6865aSVaclav Hapla   See DMPlexBuildCoordinatesFromCellListParallel() for details about the geometry-related parameters.
417325b6865aSVaclav Hapla 
4174b09969d6SVaclav Hapla   Level: intermediate
4175a47d0d45SMatthew G. Knepley 
4176b09969d6SVaclav Hapla .seealso: DMPlexCreateFromCellListPetsc(), DMPlexBuildFromCellListParallel(), DMPlexBuildCoordinatesFromCellListParallel(), DMPlexCreateFromDAG(), DMPlexCreate()
4177a47d0d45SMatthew G. Knepley @*/
4178be8c289dSNicolas 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)
4179a47d0d45SMatthew G. Knepley {
4180a47d0d45SMatthew G. Knepley   PetscSF        sfVert;
4181a47d0d45SMatthew G. Knepley   PetscErrorCode ierr;
4182a47d0d45SMatthew G. Knepley 
4183a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
4184a47d0d45SMatthew G. Knepley   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
4185a47d0d45SMatthew G. Knepley   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
4186a47d0d45SMatthew G. Knepley   PetscValidLogicalCollectiveInt(*dm, dim, 2);
4187064a246eSJacob Faibussowitsch   PetscValidLogicalCollectiveInt(*dm, spaceDim, 9);
4188a47d0d45SMatthew G. Knepley   ierr = DMSetDimension(*dm, dim);CHKERRQ(ierr);
4189be8c289dSNicolas Barral   ierr = DMPlexBuildFromCellListParallel(*dm, numCells, numVertices, NVertices, numCorners, cells, &sfVert, verticesAdj);CHKERRQ(ierr);
4190a47d0d45SMatthew G. Knepley   if (interpolate) {
41915fd9971aSMatthew G. Knepley     DM idm;
4192a47d0d45SMatthew G. Knepley 
4193a47d0d45SMatthew G. Knepley     ierr = DMPlexInterpolate(*dm, &idm);CHKERRQ(ierr);
4194a47d0d45SMatthew G. Knepley     ierr = DMDestroy(dm);CHKERRQ(ierr);
4195a47d0d45SMatthew G. Knepley     *dm  = idm;
4196a47d0d45SMatthew G. Knepley   }
41971edcf0b2SVaclav Hapla   ierr = DMPlexBuildCoordinatesFromCellListParallel(*dm, spaceDim, sfVert, vertexCoords);CHKERRQ(ierr);
419818d54ad4SMichael Lange   if (vertexSF) *vertexSF = sfVert;
4199fba955ccSBarry Smith   else {ierr = PetscSFDestroy(&sfVert);CHKERRQ(ierr);}
4200a47d0d45SMatthew G. Knepley   PetscFunctionReturn(0);
4201a47d0d45SMatthew G. Knepley }
4202a47d0d45SMatthew G. Knepley 
4203b09969d6SVaclav Hapla /*@C
4204b09969d6SVaclav Hapla   DMPlexBuildFromCellList - Build DMPLEX topology from a list of vertices for each cell (common mesh generator output)
42059298eaa6SMatthew G Knepley 
42069298eaa6SMatthew G Knepley   Input Parameters:
4207b09969d6SVaclav Hapla + dm - The DM
4208b09969d6SVaclav Hapla . numCells - The number of cells owned by this process
4209325d53feSBarry Smith . numVertices - The number of vertices owned by this process, or PETSC_DETERMINE
42109298eaa6SMatthew G Knepley . numCorners - The number of vertices for each cell
42115e488331SVaclav Hapla - cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
42129298eaa6SMatthew G Knepley 
4213b09969d6SVaclav Hapla   Level: advanced
42149298eaa6SMatthew G Knepley 
4215b09969d6SVaclav Hapla   Notes:
4216b09969d6SVaclav Hapla   Two triangles sharing a face
42179298eaa6SMatthew G Knepley $
42189298eaa6SMatthew G Knepley $        2
42199298eaa6SMatthew G Knepley $      / | \
42209298eaa6SMatthew G Knepley $     /  |  \
42219298eaa6SMatthew G Knepley $    /   |   \
42229298eaa6SMatthew G Knepley $   0  0 | 1  3
42239298eaa6SMatthew G Knepley $    \   |   /
42249298eaa6SMatthew G Knepley $     \  |  /
42259298eaa6SMatthew G Knepley $      \ | /
42269298eaa6SMatthew G Knepley $        1
42279298eaa6SMatthew G Knepley would have input
42289298eaa6SMatthew G Knepley $  numCells = 2, numVertices = 4
42299298eaa6SMatthew G Knepley $  cells = [0 1 2  1 3 2]
42309298eaa6SMatthew G Knepley $
42319298eaa6SMatthew G Knepley which would result in the DMPlex
42329298eaa6SMatthew G Knepley $
42339298eaa6SMatthew G Knepley $        4
42349298eaa6SMatthew G Knepley $      / | \
42359298eaa6SMatthew G Knepley $     /  |  \
42369298eaa6SMatthew G Knepley $    /   |   \
42379298eaa6SMatthew G Knepley $   2  0 | 1  5
42389298eaa6SMatthew G Knepley $    \   |   /
42399298eaa6SMatthew G Knepley $     \  |  /
42409298eaa6SMatthew G Knepley $      \ | /
42419298eaa6SMatthew G Knepley $        3
42429298eaa6SMatthew G Knepley 
4243325d53feSBarry Smith   If numVertices is PETSC_DETERMINE, it is computed by PETSc as the maximum vertex index in cells + 1.
424425b6865aSVaclav Hapla 
4245b09969d6SVaclav Hapla   Not currently supported in Fortran.
42469298eaa6SMatthew G Knepley 
4247b09969d6SVaclav Hapla .seealso: DMPlexBuildFromCellListParallel(), DMPlexBuildCoordinatesFromCellList(), DMPlexCreateFromCellListPetsc()
4248b09969d6SVaclav Hapla @*/
42495e488331SVaclav Hapla PetscErrorCode DMPlexBuildFromCellList(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const PetscInt cells[])
4250b09969d6SVaclav Hapla {
4251961cfab0SVaclav Hapla   PetscInt      *cones, c, p, dim;
4252b09969d6SVaclav Hapla   PetscErrorCode ierr;
4253b09969d6SVaclav Hapla 
4254b09969d6SVaclav Hapla   PetscFunctionBegin;
4255b09969d6SVaclav Hapla   ierr = PetscLogEventBegin(DMPLEX_BuildFromCellList,dm,0,0,0);CHKERRQ(ierr);
4256b09969d6SVaclav Hapla   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
425725b6865aSVaclav Hapla   /* Get/check global number of vertices */
425825b6865aSVaclav Hapla   {
425925b6865aSVaclav Hapla     PetscInt NVerticesInCells, i;
426025b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
426125b6865aSVaclav Hapla 
426225b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
426325b6865aSVaclav Hapla     NVerticesInCells = PETSC_MIN_INT;
426425b6865aSVaclav Hapla     for (i=0; i<len; i++) if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
426525b6865aSVaclav Hapla     ++NVerticesInCells;
426625b6865aSVaclav Hapla 
426725b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE) numVertices = NVerticesInCells;
42682c71b3e2SJacob Faibussowitsch     else PetscCheckFalse(numVertices < NVerticesInCells,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);
426925b6865aSVaclav Hapla   }
4270b09969d6SVaclav Hapla   ierr = DMPlexSetChart(dm, 0, numCells+numVertices);CHKERRQ(ierr);
4271b09969d6SVaclav Hapla   for (c = 0; c < numCells; ++c) {
4272b09969d6SVaclav Hapla     ierr = DMPlexSetConeSize(dm, c, numCorners);CHKERRQ(ierr);
4273b09969d6SVaclav Hapla   }
4274b09969d6SVaclav Hapla   ierr = DMSetUp(dm);CHKERRQ(ierr);
4275961cfab0SVaclav Hapla   ierr = DMPlexGetCones(dm,&cones);CHKERRQ(ierr);
4276b09969d6SVaclav Hapla   for (c = 0; c < numCells; ++c) {
4277b09969d6SVaclav Hapla     for (p = 0; p < numCorners; ++p) {
4278961cfab0SVaclav Hapla       cones[c*numCorners+p] = cells[c*numCorners+p]+numCells;
4279b09969d6SVaclav Hapla     }
4280b09969d6SVaclav Hapla   }
4281b09969d6SVaclav Hapla   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
4282b09969d6SVaclav Hapla   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
4283b09969d6SVaclav Hapla   ierr = PetscLogEventEnd(DMPLEX_BuildFromCellList,dm,0,0,0);CHKERRQ(ierr);
4284b09969d6SVaclav Hapla   PetscFunctionReturn(0);
4285b09969d6SVaclav Hapla }
4286b09969d6SVaclav Hapla 
4287b09969d6SVaclav Hapla /*@C
4288b09969d6SVaclav Hapla   DMPlexBuildCoordinatesFromCellList - Build DM coordinates from a list of coordinates for each owned vertex (common mesh generator output)
4289b09969d6SVaclav Hapla 
4290b09969d6SVaclav Hapla   Input Parameters:
4291b09969d6SVaclav Hapla + dm - The DM
4292b09969d6SVaclav Hapla . spaceDim - The spatial dimension used for coordinates
4293b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
4294b09969d6SVaclav Hapla 
4295b09969d6SVaclav Hapla   Level: advanced
4296b09969d6SVaclav Hapla 
4297b09969d6SVaclav Hapla   Notes:
4298b09969d6SVaclav Hapla   Not currently supported in Fortran.
4299b09969d6SVaclav Hapla 
4300b09969d6SVaclav Hapla .seealso: DMPlexBuildCoordinatesFromCellListParallel(), DMPlexCreateFromCellListPetsc(), DMPlexBuildFromCellList()
4301b09969d6SVaclav Hapla @*/
43021edcf0b2SVaclav Hapla PetscErrorCode DMPlexBuildCoordinatesFromCellList(DM dm, PetscInt spaceDim, const PetscReal vertexCoords[])
4303b09969d6SVaclav Hapla {
4304b09969d6SVaclav Hapla   PetscSection   coordSection;
4305b09969d6SVaclav Hapla   Vec            coordinates;
4306b09969d6SVaclav Hapla   DM             cdm;
4307b09969d6SVaclav Hapla   PetscScalar   *coords;
43081edcf0b2SVaclav Hapla   PetscInt       v, vStart, vEnd, d;
4309b09969d6SVaclav Hapla   PetscErrorCode ierr;
4310b09969d6SVaclav Hapla 
4311b09969d6SVaclav Hapla   PetscFunctionBegin;
4312b09969d6SVaclav Hapla   ierr = PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0);CHKERRQ(ierr);
43131edcf0b2SVaclav Hapla   ierr = DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
43142c71b3e2SJacob Faibussowitsch   PetscCheckFalse(vStart < 0 || vEnd < 0,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
4315b09969d6SVaclav Hapla   ierr = DMSetCoordinateDim(dm, spaceDim);CHKERRQ(ierr);
4316b09969d6SVaclav Hapla   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
4317b09969d6SVaclav Hapla   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
4318b09969d6SVaclav Hapla   ierr = PetscSectionSetFieldComponents(coordSection, 0, spaceDim);CHKERRQ(ierr);
43191edcf0b2SVaclav Hapla   ierr = PetscSectionSetChart(coordSection, vStart, vEnd);CHKERRQ(ierr);
43201edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
4321b09969d6SVaclav Hapla     ierr = PetscSectionSetDof(coordSection, v, spaceDim);CHKERRQ(ierr);
4322b09969d6SVaclav Hapla     ierr = PetscSectionSetFieldDof(coordSection, v, 0, spaceDim);CHKERRQ(ierr);
4323b09969d6SVaclav Hapla   }
4324b09969d6SVaclav Hapla   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
4325b09969d6SVaclav Hapla 
4326b09969d6SVaclav Hapla   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4327b09969d6SVaclav Hapla   ierr = DMCreateLocalVector(cdm, &coordinates);CHKERRQ(ierr);
4328b09969d6SVaclav Hapla   ierr = VecSetBlockSize(coordinates, spaceDim);CHKERRQ(ierr);
4329b09969d6SVaclav Hapla   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
433099946890SBarry Smith   ierr = VecGetArrayWrite(coordinates, &coords);CHKERRQ(ierr);
43311edcf0b2SVaclav Hapla   for (v = 0; v < vEnd-vStart; ++v) {
4332b09969d6SVaclav Hapla     for (d = 0; d < spaceDim; ++d) {
4333b09969d6SVaclav Hapla       coords[v*spaceDim+d] = vertexCoords[v*spaceDim+d];
4334b09969d6SVaclav Hapla     }
4335b09969d6SVaclav Hapla   }
433699946890SBarry Smith   ierr = VecRestoreArrayWrite(coordinates, &coords);CHKERRQ(ierr);
4337b09969d6SVaclav Hapla   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
4338b09969d6SVaclav Hapla   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
4339b09969d6SVaclav Hapla   ierr = PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0);CHKERRQ(ierr);
4340b09969d6SVaclav Hapla   PetscFunctionReturn(0);
4341b09969d6SVaclav Hapla }
4342b09969d6SVaclav Hapla 
4343b09969d6SVaclav Hapla /*@
43443df08285SMatthew 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
43453df08285SMatthew G. Knepley 
43463df08285SMatthew G. Knepley   Collective on comm
4347b09969d6SVaclav Hapla 
4348b09969d6SVaclav Hapla   Input Parameters:
4349b09969d6SVaclav Hapla + comm - The communicator
4350b09969d6SVaclav Hapla . dim - The topological dimension of the mesh
43513df08285SMatthew G. Knepley . numCells - The number of cells, only on process 0
43523df08285SMatthew G. Knepley . numVertices - The number of vertices owned by this process, or PETSC_DECIDE, only on process 0
43533df08285SMatthew G. Knepley . numCorners - The number of vertices for each cell, only on process 0
4354b09969d6SVaclav Hapla . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
43553df08285SMatthew G. Knepley . cells - An array of numCells*numCorners numbers, the vertices for each cell, only on process 0
4356b09969d6SVaclav Hapla . spaceDim - The spatial dimension used for coordinates
43573df08285SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex, only on process 0
4358b09969d6SVaclav Hapla 
4359b09969d6SVaclav Hapla   Output Parameter:
43603df08285SMatthew G. Knepley . dm - The DM, which only has points on process 0
4361b09969d6SVaclav Hapla 
4362b09969d6SVaclav Hapla   Notes:
4363b09969d6SVaclav Hapla   This function is just a convenient sequence of DMCreate(), DMSetType(), DMSetDimension(), DMPlexBuildFromCellList(),
4364b09969d6SVaclav Hapla   DMPlexInterpolate(), DMPlexBuildCoordinatesFromCellList()
4365b09969d6SVaclav Hapla 
436625b6865aSVaclav Hapla   See DMPlexBuildFromCellList() for an example and details about the topology-related parameters.
436725b6865aSVaclav Hapla   See DMPlexBuildCoordinatesFromCellList() for details about the geometry-related parameters.
43683df08285SMatthew G. Knepley   See DMPlexCreateFromCellListParallelPetsc() for parallel input
436925b6865aSVaclav Hapla 
4370b09969d6SVaclav Hapla   Level: intermediate
4371b09969d6SVaclav Hapla 
4372b09969d6SVaclav Hapla .seealso: DMPlexCreateFromCellListParallelPetsc(), DMPlexBuildFromCellList(), DMPlexBuildCoordinatesFromCellList(), DMPlexCreateFromDAG(), DMPlexCreate()
43739298eaa6SMatthew G Knepley @*/
4374a4a685f2SJacob 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)
43759298eaa6SMatthew G Knepley {
43763df08285SMatthew G. Knepley   PetscMPIInt    rank;
43779298eaa6SMatthew G Knepley   PetscErrorCode ierr;
43789298eaa6SMatthew G Knepley 
43799298eaa6SMatthew G Knepley   PetscFunctionBegin;
43802c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!dim,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.");
43813df08285SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
43829298eaa6SMatthew G Knepley   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
43839298eaa6SMatthew G Knepley   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
4384c73cfb54SMatthew G. Knepley   ierr = DMSetDimension(*dm, dim);CHKERRQ(ierr);
43853df08285SMatthew G. Knepley   if (!rank) {ierr = DMPlexBuildFromCellList(*dm, numCells, numVertices, numCorners, cells);CHKERRQ(ierr);}
43863df08285SMatthew G. Knepley   else       {ierr = DMPlexBuildFromCellList(*dm, 0, 0, 0, NULL);CHKERRQ(ierr);}
43879298eaa6SMatthew G Knepley   if (interpolate) {
43885fd9971aSMatthew G. Knepley     DM idm;
43899298eaa6SMatthew G Knepley 
43909298eaa6SMatthew G Knepley     ierr = DMPlexInterpolate(*dm, &idm);CHKERRQ(ierr);
43919298eaa6SMatthew G Knepley     ierr = DMDestroy(dm);CHKERRQ(ierr);
43929298eaa6SMatthew G Knepley     *dm  = idm;
43939298eaa6SMatthew G Knepley   }
43943df08285SMatthew G. Knepley   if (!rank) {ierr = DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, vertexCoords);CHKERRQ(ierr);}
43953df08285SMatthew G. Knepley   else       {ierr = DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, NULL);CHKERRQ(ierr);}
43969298eaa6SMatthew G Knepley   PetscFunctionReturn(0);
43979298eaa6SMatthew G Knepley }
43989298eaa6SMatthew G Knepley 
4399939f6067SMatthew G. Knepley /*@
4400939f6067SMatthew 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
4401939f6067SMatthew G. Knepley 
4402939f6067SMatthew G. Knepley   Input Parameters:
4403c73cfb54SMatthew G. Knepley + dm - The empty DM object, usually from DMCreate() and DMSetDimension()
4404939f6067SMatthew G. Knepley . depth - The depth of the DAG
4405367003a6SStefano Zampini . numPoints - Array of size depth + 1 containing the number of points at each depth
4406939f6067SMatthew G. Knepley . coneSize - The cone size of each point
4407939f6067SMatthew G. Knepley . cones - The concatenation of the cone points for each point, the cone list must be oriented correctly for each point
4408939f6067SMatthew G. Knepley . coneOrientations - The orientation of each cone point
4409367003a6SStefano Zampini - vertexCoords - An array of numPoints[0]*spacedim numbers representing the coordinates of each vertex, with spacedim the value set via DMSetCoordinateDim()
4410939f6067SMatthew G. Knepley 
4411939f6067SMatthew G. Knepley   Output Parameter:
4412939f6067SMatthew G. Knepley . dm - The DM
4413939f6067SMatthew G. Knepley 
4414939f6067SMatthew G. Knepley   Note: Two triangles sharing a face would have input
4415939f6067SMatthew G. Knepley $  depth = 1, numPoints = [4 2], coneSize = [3 3 0 0 0 0]
4416939f6067SMatthew G. Knepley $  cones = [2 3 4  3 5 4], coneOrientations = [0 0 0  0 0 0]
4417939f6067SMatthew G. Knepley $ vertexCoords = [-1.0 0.0  0.0 -1.0  0.0 1.0  1.0 0.0]
4418939f6067SMatthew G. Knepley $
4419939f6067SMatthew G. Knepley which would result in the DMPlex
4420939f6067SMatthew G. Knepley $
4421939f6067SMatthew G. Knepley $        4
4422939f6067SMatthew G. Knepley $      / | \
4423939f6067SMatthew G. Knepley $     /  |  \
4424939f6067SMatthew G. Knepley $    /   |   \
4425939f6067SMatthew G. Knepley $   2  0 | 1  5
4426939f6067SMatthew G. Knepley $    \   |   /
4427939f6067SMatthew G. Knepley $     \  |  /
4428939f6067SMatthew G. Knepley $      \ | /
4429939f6067SMatthew G. Knepley $        3
4430939f6067SMatthew G. Knepley $
4431a4a685f2SJacob Faibussowitsch $ Notice that all points are numbered consecutively, unlike DMPlexCreateFromCellListPetsc()
4432939f6067SMatthew G. Knepley 
4433939f6067SMatthew G. Knepley   Level: advanced
4434939f6067SMatthew G. Knepley 
4435a4a685f2SJacob Faibussowitsch .seealso: DMPlexCreateFromCellListPetsc(), DMPlexCreate()
4436939f6067SMatthew G. Knepley @*/
44379298eaa6SMatthew G Knepley PetscErrorCode DMPlexCreateFromDAG(DM dm, PetscInt depth, const PetscInt numPoints[], const PetscInt coneSize[], const PetscInt cones[], const PetscInt coneOrientations[], const PetscScalar vertexCoords[])
44389298eaa6SMatthew G Knepley {
44399298eaa6SMatthew G Knepley   Vec            coordinates;
44409298eaa6SMatthew G Knepley   PetscSection   coordSection;
44419298eaa6SMatthew G Knepley   PetscScalar    *coords;
4442811e8653SToby Isaac   PetscInt       coordSize, firstVertex = -1, pStart = 0, pEnd = 0, p, v, dim, dimEmbed, d, off;
44439298eaa6SMatthew G Knepley   PetscErrorCode ierr;
44449298eaa6SMatthew G Knepley 
44459298eaa6SMatthew G Knepley   PetscFunctionBegin;
4446c73cfb54SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
4447811e8653SToby Isaac   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
44482c71b3e2SJacob Faibussowitsch   PetscCheckFalse(dimEmbed < dim,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Embedding dimension %D cannot be less than intrinsic dimension %d",dimEmbed,dim);
44499298eaa6SMatthew G Knepley   for (d = 0; d <= depth; ++d) pEnd += numPoints[d];
44509298eaa6SMatthew G Knepley   ierr = DMPlexSetChart(dm, pStart, pEnd);CHKERRQ(ierr);
44519298eaa6SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
44529298eaa6SMatthew G Knepley     ierr = DMPlexSetConeSize(dm, p, coneSize[p-pStart]);CHKERRQ(ierr);
445397e052ccSToby Isaac     if (firstVertex < 0 && !coneSize[p - pStart]) {
445497e052ccSToby Isaac       firstVertex = p - pStart;
44559298eaa6SMatthew G Knepley     }
445697e052ccSToby Isaac   }
44572c71b3e2SJacob Faibussowitsch   PetscCheckFalse(firstVertex < 0 && numPoints[0],PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected %D vertices but could not find any", numPoints[0]);
44589298eaa6SMatthew G Knepley   ierr = DMSetUp(dm);CHKERRQ(ierr); /* Allocate space for cones */
44599298eaa6SMatthew G Knepley   for (p = pStart, off = 0; p < pEnd; off += coneSize[p-pStart], ++p) {
44609298eaa6SMatthew G Knepley     ierr = DMPlexSetCone(dm, p, &cones[off]);CHKERRQ(ierr);
44619298eaa6SMatthew G Knepley     ierr = DMPlexSetConeOrientation(dm, p, &coneOrientations[off]);CHKERRQ(ierr);
44629298eaa6SMatthew G Knepley   }
44639298eaa6SMatthew G Knepley   ierr = DMPlexSymmetrize(dm);CHKERRQ(ierr);
44649298eaa6SMatthew G Knepley   ierr = DMPlexStratify(dm);CHKERRQ(ierr);
44659298eaa6SMatthew G Knepley   /* Build coordinates */
4466c2166f76SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
44679298eaa6SMatthew G Knepley   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
4468811e8653SToby Isaac   ierr = PetscSectionSetFieldComponents(coordSection, 0, dimEmbed);CHKERRQ(ierr);
44699298eaa6SMatthew G Knepley   ierr = PetscSectionSetChart(coordSection, firstVertex, firstVertex+numPoints[0]);CHKERRQ(ierr);
44709298eaa6SMatthew G Knepley   for (v = firstVertex; v < firstVertex+numPoints[0]; ++v) {
4471811e8653SToby Isaac     ierr = PetscSectionSetDof(coordSection, v, dimEmbed);CHKERRQ(ierr);
4472811e8653SToby Isaac     ierr = PetscSectionSetFieldDof(coordSection, v, 0, dimEmbed);CHKERRQ(ierr);
44739298eaa6SMatthew G Knepley   }
44749298eaa6SMatthew G Knepley   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
44759298eaa6SMatthew G Knepley   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
44768b9ced59SLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
44776f8cbbeeSMatthew G Knepley   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
44789298eaa6SMatthew G Knepley   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
44798b9ced59SLisandro Dalcin   ierr = VecSetBlockSize(coordinates, dimEmbed);CHKERRQ(ierr);
44802eb5907fSJed Brown   ierr = VecSetType(coordinates,VECSTANDARD);CHKERRQ(ierr);
44819318fe57SMatthew G. Knepley   if (vertexCoords) {
44829298eaa6SMatthew G Knepley     ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
44839298eaa6SMatthew G Knepley     for (v = 0; v < numPoints[0]; ++v) {
44849298eaa6SMatthew G Knepley       PetscInt off;
44859298eaa6SMatthew G Knepley 
44869298eaa6SMatthew G Knepley       ierr = PetscSectionGetOffset(coordSection, v+firstVertex, &off);CHKERRQ(ierr);
4487811e8653SToby Isaac       for (d = 0; d < dimEmbed; ++d) {
4488811e8653SToby Isaac         coords[off+d] = vertexCoords[v*dimEmbed+d];
44899298eaa6SMatthew G Knepley       }
44909298eaa6SMatthew G Knepley     }
44919318fe57SMatthew G. Knepley   }
44929298eaa6SMatthew G Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
44939298eaa6SMatthew G Knepley   ierr = DMSetCoordinatesLocal(dm, coordinates);CHKERRQ(ierr);
44949298eaa6SMatthew G Knepley   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
44959298eaa6SMatthew G Knepley   PetscFunctionReturn(0);
44969298eaa6SMatthew G Knepley }
44978415267dSToby Isaac 
4498ca522641SMatthew G. Knepley /*@C
44998ca92349SMatthew G. Knepley   DMPlexCreateCellVertexFromFile - Create a DMPlex mesh from a simple cell-vertex file.
45008ca92349SMatthew G. Knepley 
45018ca92349SMatthew G. Knepley + comm        - The MPI communicator
45028ca92349SMatthew G. Knepley . filename    - Name of the .dat file
45038ca92349SMatthew G. Knepley - interpolate - Create faces and edges in the mesh
45048ca92349SMatthew G. Knepley 
45058ca92349SMatthew G. Knepley   Output Parameter:
45068ca92349SMatthew G. Knepley . dm  - The DM object representing the mesh
45078ca92349SMatthew G. Knepley 
45088ca92349SMatthew G. Knepley   Note: The format is the simplest possible:
45098ca92349SMatthew G. Knepley $ Ne
45108ca92349SMatthew G. Knepley $ v0 v1 ... vk
45118ca92349SMatthew G. Knepley $ Nv
45128ca92349SMatthew G. Knepley $ x y z marker
45138ca92349SMatthew G. Knepley 
45148ca92349SMatthew G. Knepley   Level: beginner
45158ca92349SMatthew G. Knepley 
45168ca92349SMatthew G. Knepley .seealso: DMPlexCreateFromFile(), DMPlexCreateMedFromFile(), DMPlexCreateGmsh(), DMPlexCreate()
45178ca92349SMatthew G. Knepley @*/
45188ca92349SMatthew G. Knepley PetscErrorCode DMPlexCreateCellVertexFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
45198ca92349SMatthew G. Knepley {
45208ca92349SMatthew G. Knepley   DMLabel         marker;
45218ca92349SMatthew G. Knepley   PetscViewer     viewer;
45228ca92349SMatthew G. Knepley   Vec             coordinates;
45238ca92349SMatthew G. Knepley   PetscSection    coordSection;
45248ca92349SMatthew G. Knepley   PetscScalar    *coords;
45258ca92349SMatthew G. Knepley   char            line[PETSC_MAX_PATH_LEN];
45268ca92349SMatthew G. Knepley   PetscInt        dim = 3, cdim = 3, coordSize, v, c, d;
45278ca92349SMatthew G. Knepley   PetscMPIInt     rank;
4528f8d5e320SMatthew G. Knepley   int             snum, Nv, Nc, Ncn, Nl;
45298ca92349SMatthew G. Knepley   PetscErrorCode  ierr;
45308ca92349SMatthew G. Knepley 
45318ca92349SMatthew G. Knepley   PetscFunctionBegin;
4532ffc4695bSBarry Smith   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
45338ca92349SMatthew G. Knepley   ierr = PetscViewerCreate(comm, &viewer);CHKERRQ(ierr);
45348ca92349SMatthew G. Knepley   ierr = PetscViewerSetType(viewer, PETSCVIEWERASCII);CHKERRQ(ierr);
45358ca92349SMatthew G. Knepley   ierr = PetscViewerFileSetMode(viewer, FILE_MODE_READ);CHKERRQ(ierr);
45368ca92349SMatthew G. Knepley   ierr = PetscViewerFileSetName(viewer, filename);CHKERRQ(ierr);
4537dd400576SPatrick Sanan   if (rank == 0) {
4538f8d5e320SMatthew G. Knepley     ierr = PetscViewerRead(viewer, line, 4, NULL, PETSC_STRING);CHKERRQ(ierr);
4539f8d5e320SMatthew G. Knepley     snum = sscanf(line, "%d %d %d %d", &Nc, &Nv, &Ncn, &Nl);
45402c71b3e2SJacob Faibussowitsch     PetscCheckFalse(snum != 4,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
454125ce1634SJed Brown   } else {
4542f8d5e320SMatthew G. Knepley     Nc = Nv = Ncn = Nl = 0;
45438ca92349SMatthew G. Knepley   }
45448ca92349SMatthew G. Knepley   ierr = DMCreate(comm, dm);CHKERRQ(ierr);
45458ca92349SMatthew G. Knepley   ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
45468ca92349SMatthew G. Knepley   ierr = DMPlexSetChart(*dm, 0, Nc+Nv);CHKERRQ(ierr);
45478ca92349SMatthew G. Knepley   ierr = DMSetDimension(*dm, dim);CHKERRQ(ierr);
45488ca92349SMatthew G. Knepley   ierr = DMSetCoordinateDim(*dm, cdim);CHKERRQ(ierr);
45498ca92349SMatthew G. Knepley   /* Read topology */
4550dd400576SPatrick Sanan   if (rank == 0) {
4551f8d5e320SMatthew G. Knepley     char     format[PETSC_MAX_PATH_LEN];
4552f8d5e320SMatthew G. Knepley     PetscInt cone[8];
45538ca92349SMatthew G. Knepley     int      vbuf[8], v;
45548ca92349SMatthew G. Knepley 
4555f8d5e320SMatthew G. Knepley     for (c = 0; c < Ncn; ++c) {format[c*3+0] = '%'; format[c*3+1] = 'd'; format[c*3+2] = ' ';}
4556f8d5e320SMatthew G. Knepley     format[Ncn*3-1] = '\0';
4557f8d5e320SMatthew G. Knepley     for (c = 0; c < Nc; ++c) {ierr = DMPlexSetConeSize(*dm, c, Ncn);CHKERRQ(ierr);}
45588ca92349SMatthew G. Knepley     ierr = DMSetUp(*dm);CHKERRQ(ierr);
45598ca92349SMatthew G. Knepley     for (c = 0; c < Nc; ++c) {
4560f8d5e320SMatthew G. Knepley       ierr = PetscViewerRead(viewer, line, Ncn, NULL, PETSC_STRING);CHKERRQ(ierr);
4561f8d5e320SMatthew G. Knepley       switch (Ncn) {
4562f8d5e320SMatthew G. Knepley         case 2: snum = sscanf(line, format, &vbuf[0], &vbuf[1]);break;
4563f8d5e320SMatthew G. Knepley         case 3: snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2]);break;
4564f8d5e320SMatthew G. Knepley         case 4: snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3]);break;
4565f8d5e320SMatthew G. Knepley         case 6: snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5]);break;
4566f8d5e320SMatthew 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;
456798921bdaSJacob Faibussowitsch         default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No cell shape with %D vertices", Ncn);
4568f8d5e320SMatthew G. Knepley       }
45692c71b3e2SJacob Faibussowitsch       PetscCheckFalse(snum != Ncn,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
4570f8d5e320SMatthew G. Knepley       for (v = 0; v < Ncn; ++v) cone[v] = vbuf[v] + Nc;
45718ca92349SMatthew G. Knepley       /* Hexahedra are inverted */
4572f8d5e320SMatthew G. Knepley       if (Ncn == 8) {
45738ca92349SMatthew G. Knepley         PetscInt tmp = cone[1];
45748ca92349SMatthew G. Knepley         cone[1] = cone[3];
45758ca92349SMatthew G. Knepley         cone[3] = tmp;
45768ca92349SMatthew G. Knepley       }
45778ca92349SMatthew G. Knepley       ierr = DMPlexSetCone(*dm, c, cone);CHKERRQ(ierr);
45788ca92349SMatthew G. Knepley     }
45798ca92349SMatthew G. Knepley   }
45808ca92349SMatthew G. Knepley   ierr = DMPlexSymmetrize(*dm);CHKERRQ(ierr);
45818ca92349SMatthew G. Knepley   ierr = DMPlexStratify(*dm);CHKERRQ(ierr);
45828ca92349SMatthew G. Knepley   /* Read coordinates */
45838ca92349SMatthew G. Knepley   ierr = DMGetCoordinateSection(*dm, &coordSection);CHKERRQ(ierr);
45848ca92349SMatthew G. Knepley   ierr = PetscSectionSetNumFields(coordSection, 1);CHKERRQ(ierr);
45858ca92349SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(coordSection, 0, cdim);CHKERRQ(ierr);
45868ca92349SMatthew G. Knepley   ierr = PetscSectionSetChart(coordSection, Nc, Nc + Nv);CHKERRQ(ierr);
45878ca92349SMatthew G. Knepley   for (v = Nc; v < Nc+Nv; ++v) {
45888ca92349SMatthew G. Knepley     ierr = PetscSectionSetDof(coordSection, v, cdim);CHKERRQ(ierr);
45898ca92349SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(coordSection, v, 0, cdim);CHKERRQ(ierr);
45908ca92349SMatthew G. Knepley   }
45918ca92349SMatthew G. Knepley   ierr = PetscSectionSetUp(coordSection);CHKERRQ(ierr);
45928ca92349SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(coordSection, &coordSize);CHKERRQ(ierr);
45938ca92349SMatthew G. Knepley   ierr = VecCreate(PETSC_COMM_SELF, &coordinates);CHKERRQ(ierr);
45948ca92349SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject) coordinates, "coordinates");CHKERRQ(ierr);
45958ca92349SMatthew G. Knepley   ierr = VecSetSizes(coordinates, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
45968ca92349SMatthew G. Knepley   ierr = VecSetBlockSize(coordinates, cdim);CHKERRQ(ierr);
45978ca92349SMatthew G. Knepley   ierr = VecSetType(coordinates, VECSTANDARD);CHKERRQ(ierr);
45988ca92349SMatthew G. Knepley   ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr);
4599dd400576SPatrick Sanan   if (rank == 0) {
4600f8d5e320SMatthew G. Knepley     char   format[PETSC_MAX_PATH_LEN];
46018ca92349SMatthew G. Knepley     double x[3];
4602f8d5e320SMatthew G. Knepley     int    l, val[3];
46038ca92349SMatthew G. Knepley 
4604f8d5e320SMatthew G. Knepley     if (Nl) {
4605f8d5e320SMatthew G. Knepley       for (l = 0; l < Nl; ++l) {format[l*3+0] = '%'; format[l*3+1] = 'd'; format[l*3+2] = ' ';}
4606f8d5e320SMatthew G. Knepley       format[Nl*3-1] = '\0';
46078ca92349SMatthew G. Knepley       ierr = DMCreateLabel(*dm, "marker");CHKERRQ(ierr);
46088ca92349SMatthew G. Knepley       ierr = DMGetLabel(*dm, "marker", &marker);CHKERRQ(ierr);
4609f8d5e320SMatthew G. Knepley     }
46108ca92349SMatthew G. Knepley     for (v = 0; v < Nv; ++v) {
4611f8d5e320SMatthew G. Knepley       ierr = PetscViewerRead(viewer, line, 3+Nl, NULL, PETSC_STRING);CHKERRQ(ierr);
4612f8d5e320SMatthew G. Knepley       snum = sscanf(line, "%lg %lg %lg", &x[0], &x[1], &x[2]);
46132c71b3e2SJacob Faibussowitsch       PetscCheckFalse(snum != 3,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
4614f8d5e320SMatthew G. Knepley       switch (Nl) {
4615f8d5e320SMatthew G. Knepley         case 0: snum = 0;break;
4616f8d5e320SMatthew G. Knepley         case 1: snum = sscanf(line, format, &val[0]);break;
4617f8d5e320SMatthew G. Knepley         case 2: snum = sscanf(line, format, &val[0], &val[1]);break;
4618f8d5e320SMatthew G. Knepley         case 3: snum = sscanf(line, format, &val[0], &val[1], &val[2]);break;
461998921bdaSJacob Faibussowitsch         default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Request support for %D labels", Nl);
4620f8d5e320SMatthew G. Knepley       }
46212c71b3e2SJacob Faibussowitsch       PetscCheckFalse(snum != Nl,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
46228ca92349SMatthew G. Knepley       for (d = 0; d < cdim; ++d) coords[v*cdim+d] = x[d];
4623f8d5e320SMatthew G. Knepley       for (l = 0; l < Nl; ++l) {ierr = DMLabelSetValue(marker, v+Nc, val[l]);CHKERRQ(ierr);}
46248ca92349SMatthew G. Knepley     }
46258ca92349SMatthew G. Knepley   }
46268ca92349SMatthew G. Knepley   ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr);
46278ca92349SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(*dm, coordinates);CHKERRQ(ierr);
46288ca92349SMatthew G. Knepley   ierr = VecDestroy(&coordinates);CHKERRQ(ierr);
46298ca92349SMatthew G. Knepley   ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
46308ca92349SMatthew G. Knepley   if (interpolate) {
46318ca92349SMatthew G. Knepley     DM      idm;
46328ca92349SMatthew G. Knepley     DMLabel bdlabel;
46338ca92349SMatthew G. Knepley 
46348ca92349SMatthew G. Knepley     ierr = DMPlexInterpolate(*dm, &idm);CHKERRQ(ierr);
46358ca92349SMatthew G. Knepley     ierr = DMDestroy(dm);CHKERRQ(ierr);
46368ca92349SMatthew G. Knepley     *dm  = idm;
46378ca92349SMatthew G. Knepley 
4638f8d5e320SMatthew G. Knepley     if (!Nl) {
4639f8d5e320SMatthew G. Knepley       ierr = DMCreateLabel(*dm, "marker");CHKERRQ(ierr);
46408ca92349SMatthew G. Knepley       ierr = DMGetLabel(*dm, "marker", &bdlabel);CHKERRQ(ierr);
46418ca92349SMatthew G. Knepley       ierr = DMPlexMarkBoundaryFaces(*dm, PETSC_DETERMINE, bdlabel);CHKERRQ(ierr);
46428ca92349SMatthew G. Knepley       ierr = DMPlexLabelComplete(*dm, bdlabel);CHKERRQ(ierr);
46438ca92349SMatthew G. Knepley     }
4644f8d5e320SMatthew G. Knepley   }
46458ca92349SMatthew G. Knepley   PetscFunctionReturn(0);
46468ca92349SMatthew G. Knepley }
46478ca92349SMatthew G. Knepley 
46488ca92349SMatthew G. Knepley /*@C
4649ca522641SMatthew G. Knepley   DMPlexCreateFromFile - This takes a filename and produces a DM
4650ca522641SMatthew G. Knepley 
4651ca522641SMatthew G. Knepley   Input Parameters:
4652ca522641SMatthew G. Knepley + comm - The communicator
4653ca522641SMatthew G. Knepley . filename - A file name
4654cd7e8a5eSksagiyam . plexname - The object name of the resulting DM, also used for intra-datafile lookup by some formats
4655ca522641SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
4656ca522641SMatthew G. Knepley 
4657ca522641SMatthew G. Knepley   Output Parameter:
4658ca522641SMatthew G. Knepley . dm - The DM
4659ca522641SMatthew G. Knepley 
466002ef0d99SVaclav Hapla   Options Database Keys:
466102ef0d99SVaclav Hapla . -dm_plex_create_from_hdf5_xdmf - use the PETSC_VIEWER_HDF5_XDMF format for reading HDF5
466202ef0d99SVaclav Hapla 
4663bca97951SVaclav Hapla   Use -dm_plex_create_ prefix to pass options to the internal PetscViewer, e.g.
4664bca97951SVaclav Hapla $ -dm_plex_create_viewer_hdf5_collective
4665bca97951SVaclav Hapla 
4666cd7e8a5eSksagiyam   Notes:
4667cd7e8a5eSksagiyam   Using PETSCVIEWERHDF5 type with PETSC_VIEWER_HDF5_PETSC format, one can save multiple DMPlex
4668cd7e8a5eSksagiyam   meshes in a single HDF5 file. This in turn requires one to name the DMPlex object with PetscObjectSetName()
4669cd7e8a5eSksagiyam   before saving it with DMView() and before loading it with DMLoad() for identification of the mesh object.
4670cd7e8a5eSksagiyam   The input parameter name is thus used to name the DMPlex object when DMPlexCreateFromFile() internally
4671cd7e8a5eSksagiyam   calls DMLoad(). Currently, name is ignored for other viewer types and/or formats.
4672cd7e8a5eSksagiyam 
4673ca522641SMatthew G. Knepley   Level: beginner
4674ca522641SMatthew G. Knepley 
4675cd7e8a5eSksagiyam .seealso: DMPlexCreateFromDAG(), DMPlexCreateFromCellListPetsc(), DMPlexCreate(), PetscObjectSetName(), DMView(), DMLoad()
4676ca522641SMatthew G. Knepley @*/
4677cd7e8a5eSksagiyam PetscErrorCode DMPlexCreateFromFile(MPI_Comm comm, const char filename[], const char plexname[], PetscBool interpolate, DM *dm)
4678ca522641SMatthew G. Knepley {
4679ca522641SMatthew G. Knepley   const char    *extGmsh      = ".msh";
4680de78e4feSLisandro Dalcin   const char    *extGmsh2     = ".msh2";
4681de78e4feSLisandro Dalcin   const char    *extGmsh4     = ".msh4";
4682ca522641SMatthew G. Knepley   const char    *extCGNS      = ".cgns";
4683ca522641SMatthew G. Knepley   const char    *extExodus    = ".exo";
468486a0adb1SJed Brown   const char    *extExodus_e  = ".e";
468590c68965SMatthew G. Knepley   const char    *extGenesis   = ".gen";
46862f0bd6dcSMichael Lange   const char    *extFluent    = ".cas";
4687cc2f8f65SMatthew G. Knepley   const char    *extHDF5      = ".h5";
4688707dd687SMichael Lange   const char    *extMed       = ".med";
4689f2801cd6SMatthew G. Knepley   const char    *extPLY       = ".ply";
4690c1cad2e7SMatthew G. Knepley   const char    *extEGADSLite = ".egadslite";
4691c1cad2e7SMatthew G. Knepley   const char    *extEGADS     = ".egads";
4692c1cad2e7SMatthew G. Knepley   const char    *extIGES      = ".igs";
4693c1cad2e7SMatthew G. Knepley   const char    *extSTEP      = ".stp";
46948ca92349SMatthew G. Knepley   const char    *extCV        = ".dat";
4695ca522641SMatthew G. Knepley   size_t         len;
4696c1cad2e7SMatthew G. Knepley   PetscBool      isGmsh, isGmsh2, isGmsh4, isCGNS, isExodus, isGenesis, isFluent, isHDF5, isMed, isPLY, isEGADSLite, isEGADS, isIGES, isSTEP, isCV;
4697ca522641SMatthew G. Knepley   PetscMPIInt    rank;
4698ca522641SMatthew G. Knepley   PetscErrorCode ierr;
4699ca522641SMatthew G. Knepley 
4700ca522641SMatthew G. Knepley   PetscFunctionBegin;
47015d80c0bfSVaclav Hapla   PetscValidCharPointer(filename, 2);
47020d862eaeSPierre Jolivet   if (plexname) PetscValidCharPointer(plexname, 3);
4703cd7e8a5eSksagiyam   PetscValidPointer(dm, 5);
4704f1f45c63SVaclav Hapla   ierr = DMInitializePackage();CHKERRQ(ierr);
4705f1f45c63SVaclav Hapla   ierr = PetscLogEventBegin(DMPLEX_CreateFromFile,0,0,0,0);CHKERRQ(ierr);
4706ffc4695bSBarry Smith   ierr = MPI_Comm_rank(comm, &rank);CHKERRMPI(ierr);
4707ca522641SMatthew G. Knepley   ierr = PetscStrlen(filename, &len);CHKERRQ(ierr);
47082c71b3e2SJacob Faibussowitsch   PetscCheckFalse(!len,comm, PETSC_ERR_ARG_WRONG, "Filename must be a valid path");
4709ca522641SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)],  extGmsh,      4, &isGmsh);CHKERRQ(ierr);
4710de78e4feSLisandro Dalcin   ierr = PetscStrncmp(&filename[PetscMax(0,len-5)],  extGmsh2,     5, &isGmsh2);CHKERRQ(ierr);
4711de78e4feSLisandro Dalcin   ierr = PetscStrncmp(&filename[PetscMax(0,len-5)],  extGmsh4,     5, &isGmsh4);CHKERRQ(ierr);
4712ca522641SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-5)],  extCGNS,      5, &isCGNS);CHKERRQ(ierr);
4713ca522641SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)],  extExodus,    4, &isExodus);CHKERRQ(ierr);
471486a0adb1SJed Brown   if (!isExodus) {
471586a0adb1SJed Brown     ierr = PetscStrncmp(&filename[PetscMax(0,len-2)],  extExodus_e,    2, &isExodus);CHKERRQ(ierr);
471686a0adb1SJed Brown   }
471790c68965SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)],  extGenesis,   4, &isGenesis);CHKERRQ(ierr);
47182f0bd6dcSMichael Lange   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)],  extFluent,    4, &isFluent);CHKERRQ(ierr);
4719cc2f8f65SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-3)],  extHDF5,      3, &isHDF5);CHKERRQ(ierr);
4720707dd687SMichael Lange   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)],  extMed,       4, &isMed);CHKERRQ(ierr);
4721f2801cd6SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)],  extPLY,       4, &isPLY);CHKERRQ(ierr);
4722c1cad2e7SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-10)], extEGADSLite, 10, &isEGADSLite);CHKERRQ(ierr);
4723c1cad2e7SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-6)],  extEGADS,     6, &isEGADS);CHKERRQ(ierr);
4724c1cad2e7SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)],  extIGES,      4, &isIGES);CHKERRQ(ierr);
4725c1cad2e7SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)],  extSTEP,      4, &isSTEP);CHKERRQ(ierr);
47268ca92349SMatthew G. Knepley   ierr = PetscStrncmp(&filename[PetscMax(0,len-4)],  extCV,        4, &isCV);CHKERRQ(ierr);
4727de78e4feSLisandro Dalcin   if (isGmsh || isGmsh2 || isGmsh4) {
47287d282ae0SMichael Lange     ierr = DMPlexCreateGmshFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
4729ca522641SMatthew G. Knepley   } else if (isCGNS) {
4730ca522641SMatthew G. Knepley     ierr = DMPlexCreateCGNSFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
473190c68965SMatthew G. Knepley   } else if (isExodus || isGenesis) {
4732ca522641SMatthew G. Knepley     ierr = DMPlexCreateExodusFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
47332f0bd6dcSMichael Lange   } else if (isFluent) {
47342f0bd6dcSMichael Lange     ierr = DMPlexCreateFluentFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
4735cc2f8f65SMatthew G. Knepley   } else if (isHDF5) {
47369c48423bSVaclav Hapla     PetscBool      load_hdf5_xdmf = PETSC_FALSE;
4737cc2f8f65SMatthew G. Knepley     PetscViewer viewer;
4738cc2f8f65SMatthew G. Knepley 
473943b242b4SVaclav 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 */
474043b242b4SVaclav Hapla     ierr = PetscStrncmp(&filename[PetscMax(0,len-8)], ".xdmf",  5, &load_hdf5_xdmf);CHKERRQ(ierr);
47419c48423bSVaclav Hapla     ierr = PetscOptionsGetBool(NULL, NULL, "-dm_plex_create_from_hdf5_xdmf", &load_hdf5_xdmf, NULL);CHKERRQ(ierr);
4742cc2f8f65SMatthew G. Knepley     ierr = PetscViewerCreate(comm, &viewer);CHKERRQ(ierr);
4743cc2f8f65SMatthew G. Knepley     ierr = PetscViewerSetType(viewer, PETSCVIEWERHDF5);CHKERRQ(ierr);
4744bca97951SVaclav Hapla     ierr = PetscViewerSetOptionsPrefix(viewer, "dm_plex_create_");CHKERRQ(ierr);
4745bca97951SVaclav Hapla     ierr = PetscViewerSetFromOptions(viewer);CHKERRQ(ierr);
4746cc2f8f65SMatthew G. Knepley     ierr = PetscViewerFileSetMode(viewer, FILE_MODE_READ);CHKERRQ(ierr);
4747cc2f8f65SMatthew G. Knepley     ierr = PetscViewerFileSetName(viewer, filename);CHKERRQ(ierr);
4748cd7e8a5eSksagiyam 
4749cc2f8f65SMatthew G. Knepley     ierr = DMCreate(comm, dm);CHKERRQ(ierr);
4750cd7e8a5eSksagiyam     ierr = PetscObjectSetName((PetscObject)(*dm), plexname);CHKERRQ(ierr);
4751cc2f8f65SMatthew G. Knepley     ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr);
47529c48423bSVaclav Hapla     if (load_hdf5_xdmf) {ierr = PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_XDMF);CHKERRQ(ierr);}
4753cc2f8f65SMatthew G. Knepley     ierr = DMLoad(*dm, viewer);CHKERRQ(ierr);
47549c48423bSVaclav Hapla     if (load_hdf5_xdmf) {ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr);}
4755cc2f8f65SMatthew G. Knepley     ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);
47565fd9971aSMatthew G. Knepley 
47575fd9971aSMatthew G. Knepley     if (interpolate) {
47585fd9971aSMatthew G. Knepley       DM idm;
47595fd9971aSMatthew G. Knepley 
47605fd9971aSMatthew G. Knepley       ierr = DMPlexInterpolate(*dm, &idm);CHKERRQ(ierr);
47615fd9971aSMatthew G. Knepley       ierr = DMDestroy(dm);CHKERRQ(ierr);
47625fd9971aSMatthew G. Knepley       *dm  = idm;
47635fd9971aSMatthew G. Knepley     }
4764707dd687SMichael Lange   } else if (isMed) {
4765707dd687SMichael Lange     ierr = DMPlexCreateMedFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
4766f2801cd6SMatthew G. Knepley   } else if (isPLY) {
4767f2801cd6SMatthew G. Knepley     ierr = DMPlexCreatePLYFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
4768c1cad2e7SMatthew G. Knepley   } else if (isEGADSLite || isEGADS || isIGES || isSTEP) {
4769c1cad2e7SMatthew G. Knepley     if (isEGADSLite) {ierr = DMPlexCreateEGADSLiteFromFile(comm, filename, dm);CHKERRQ(ierr);}
4770c1cad2e7SMatthew G. Knepley     else             {ierr = DMPlexCreateEGADSFromFile(comm, filename, dm);CHKERRQ(ierr);}
47717bee2925SMatthew Knepley     if (!interpolate) {
47727bee2925SMatthew Knepley       DM udm;
47737bee2925SMatthew Knepley 
47747bee2925SMatthew Knepley       ierr = DMPlexUninterpolate(*dm, &udm);CHKERRQ(ierr);
47757bee2925SMatthew Knepley       ierr = DMDestroy(dm);CHKERRQ(ierr);
47767bee2925SMatthew Knepley       *dm  = udm;
47777bee2925SMatthew Knepley     }
47788ca92349SMatthew G. Knepley   } else if (isCV) {
47798ca92349SMatthew G. Knepley     ierr = DMPlexCreateCellVertexFromFile(comm, filename, interpolate, dm);CHKERRQ(ierr);
478098921bdaSJacob Faibussowitsch   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot load file %s: unrecognized extension", filename);
4781ed5e4e85SVaclav Hapla   ierr = PetscStrlen(plexname, &len);CHKERRQ(ierr);
4782ed5e4e85SVaclav Hapla   if (len) {ierr = PetscObjectSetName((PetscObject)(*dm), plexname);CHKERRQ(ierr);}
4783f1f45c63SVaclav Hapla   ierr = PetscLogEventEnd(DMPLEX_CreateFromFile,0,0,0,0);CHKERRQ(ierr);
4784ca522641SMatthew G. Knepley   PetscFunctionReturn(0);
4785ca522641SMatthew G. Knepley }
4786