xref: /petsc/src/dm/impls/plex/plexcreate.c (revision 1baa6e3354bfe224b33a0c158f482508792a8a6e)
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>
54663dae6SJed Brown #include <petscdmplextransform.h>
6b7f5c055SJed Brown #include <petsc/private/kernels/blockmatmult.h>
7b7f5c055SJed Brown #include <petsc/private/kernels/blockinvert.h>
8552f7358SJed Brown 
9b09969d6SVaclav Hapla PetscLogEvent DMPLEX_CreateFromFile, DMPLEX_BuildFromCellList, DMPLEX_BuildCoordinatesFromCellList;
1058cd63d5SVaclav Hapla 
119318fe57SMatthew G. Knepley /* External function declarations here */
129318fe57SMatthew G. Knepley static PetscErrorCode DMInitialize_Plex(DM dm);
139318fe57SMatthew G. Knepley 
14e600fa54SMatthew G. Knepley /* This copies internal things in the Plex structure that we generally want when making a new, related Plex */
155de52c6dSVaclav Hapla PetscErrorCode DMPlexCopy_Internal(DM dmin, PetscBool copyPeriodicity, PetscBool copyOverlap, DM dmout)
16e600fa54SMatthew G. Knepley {
17e600fa54SMatthew G. Knepley   const DMBoundaryType     *bd;
18e600fa54SMatthew G. Knepley   const PetscReal          *maxCell, *L;
19e600fa54SMatthew G. Knepley   PetscBool                 isper, dist;
206bc1bd01Sksagiyam   DMPlexReorderDefaultFlag  reorder;
21e600fa54SMatthew G. Knepley 
22e600fa54SMatthew G. Knepley   PetscFunctionBegin;
23e600fa54SMatthew G. Knepley   if (copyPeriodicity) {
249566063dSJacob Faibussowitsch     PetscCall(DMGetPeriodicity(dmin, &isper, &maxCell, &L, &bd));
259566063dSJacob Faibussowitsch     PetscCall(DMSetPeriodicity(dmout, isper,  maxCell,  L,  bd));
26e600fa54SMatthew G. Knepley   }
279566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeGetDefault(dmin, &dist));
289566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeSetDefault(dmout, dist));
296bc1bd01Sksagiyam   PetscCall(DMPlexReorderGetDefault(dmin, &reorder));
306bc1bd01Sksagiyam   PetscCall(DMPlexReorderSetDefault(dmout, reorder));
31e600fa54SMatthew G. Knepley   ((DM_Plex *) dmout->data)->useHashLocation  = ((DM_Plex *) dmin->data)->useHashLocation;
32*1baa6e33SBarry Smith   if (copyOverlap) PetscCall(DMPlexSetOverlap_Plex(dmout, dmin, 0));
33e600fa54SMatthew G. Knepley   PetscFunctionReturn(0);
34e600fa54SMatthew G. Knepley }
35e600fa54SMatthew G. Knepley 
369318fe57SMatthew G. Knepley /* Replace dm with the contents of ndm, and then destroy ndm
379318fe57SMatthew G. Knepley    - Share the DM_Plex structure
389318fe57SMatthew G. Knepley    - Share the coordinates
399318fe57SMatthew G. Knepley    - Share the SF
409318fe57SMatthew G. Knepley */
419318fe57SMatthew G. Knepley static PetscErrorCode DMPlexReplace_Static(DM dm, DM *ndm)
429318fe57SMatthew G. Knepley {
439318fe57SMatthew G. Knepley   PetscSF               sf;
449318fe57SMatthew G. Knepley   DM                    dmNew = *ndm, coordDM, coarseDM;
459318fe57SMatthew G. Knepley   Vec                   coords;
469318fe57SMatthew G. Knepley   PetscBool             isper;
479318fe57SMatthew G. Knepley   const PetscReal      *maxCell, *L;
489318fe57SMatthew G. Knepley   const DMBoundaryType *bd;
499318fe57SMatthew G. Knepley   PetscInt              dim, cdim;
509318fe57SMatthew G. Knepley 
519318fe57SMatthew G. Knepley   PetscFunctionBegin;
529318fe57SMatthew G. Knepley   if (dm == dmNew) {
539566063dSJacob Faibussowitsch     PetscCall(DMDestroy(ndm));
549318fe57SMatthew G. Knepley     PetscFunctionReturn(0);
559318fe57SMatthew G. Knepley   }
569318fe57SMatthew G. Knepley   dm->setupcalled = dmNew->setupcalled;
579566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dmNew, &dim));
589566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
599566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dmNew, &cdim));
609566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, cdim));
619566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmNew, &sf));
629566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dm, sf));
639566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmNew, &coordDM));
649566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmNew, &coords));
659566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dm, coordDM));
669566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coords));
679318fe57SMatthew G. Knepley   /* Do not want to create the coordinate field if it does not already exist, so do not call DMGetCoordinateField() */
689566063dSJacob Faibussowitsch   PetscCall(DMFieldDestroy(&dm->coordinateField));
699318fe57SMatthew G. Knepley   dm->coordinateField = dmNew->coordinateField;
7061a622f3SMatthew G. Knepley   ((DM_Plex *) dmNew->data)->coordFunc = ((DM_Plex *) dm->data)->coordFunc;
719566063dSJacob Faibussowitsch   PetscCall(DMGetPeriodicity(dmNew, &isper, &maxCell, &L, &bd));
729566063dSJacob Faibussowitsch   PetscCall(DMSetPeriodicity(dm, isper, maxCell, L, bd));
739566063dSJacob Faibussowitsch   PetscCall(DMDestroy_Plex(dm));
749566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(dm));
759318fe57SMatthew G. Knepley   dm->data = dmNew->data;
769318fe57SMatthew G. Knepley   ((DM_Plex *) dmNew->data)->refct++;
779566063dSJacob Faibussowitsch   PetscCall(DMDestroyLabelLinkList_Internal(dm));
789566063dSJacob Faibussowitsch   PetscCall(DMCopyLabels(dmNew, dm, PETSC_OWN_POINTER, PETSC_TRUE, DM_COPY_LABELS_FAIL));
799566063dSJacob Faibussowitsch   PetscCall(DMGetCoarseDM(dmNew,&coarseDM));
809566063dSJacob Faibussowitsch   PetscCall(DMSetCoarseDM(dm,coarseDM));
819566063dSJacob Faibussowitsch   PetscCall(DMDestroy(ndm));
829318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
839318fe57SMatthew G. Knepley }
849318fe57SMatthew G. Knepley 
859318fe57SMatthew G. Knepley /* Swap dm with the contents of dmNew
869318fe57SMatthew G. Knepley    - Swap the DM_Plex structure
879318fe57SMatthew G. Knepley    - Swap the coordinates
889318fe57SMatthew G. Knepley    - Swap the point PetscSF
899318fe57SMatthew G. Knepley */
909318fe57SMatthew G. Knepley static PetscErrorCode DMPlexSwap_Static(DM dmA, DM dmB)
919318fe57SMatthew G. Knepley {
929318fe57SMatthew G. Knepley   DM              coordDMA, coordDMB;
939318fe57SMatthew G. Knepley   Vec             coordsA,  coordsB;
949318fe57SMatthew G. Knepley   PetscSF         sfA,      sfB;
959318fe57SMatthew G. Knepley   DMField         fieldTmp;
969318fe57SMatthew G. Knepley   void            *tmp;
979318fe57SMatthew G. Knepley   DMLabelLink     listTmp;
989318fe57SMatthew G. Knepley   DMLabel         depthTmp;
999318fe57SMatthew G. Knepley   PetscInt        tmpI;
1009318fe57SMatthew G. Knepley 
1019318fe57SMatthew G. Knepley   PetscFunctionBegin;
1029318fe57SMatthew G. Knepley   if (dmA == dmB) PetscFunctionReturn(0);
1039566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmA, &sfA));
1049566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmB, &sfB));
1059566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) sfA));
1069566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dmA, sfB));
1079566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dmB, sfA));
1089566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject) sfA));
1099318fe57SMatthew G. Knepley 
1109566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmA, &coordDMA));
1119566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmB, &coordDMB));
1129566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) coordDMA));
1139566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dmA, coordDMB));
1149566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dmB, coordDMA));
1159566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject) coordDMA));
1169318fe57SMatthew G. Knepley 
1179566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmA, &coordsA));
1189566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmB, &coordsB));
1199566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) coordsA));
1209566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmA, coordsB));
1219566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmB, coordsA));
1229566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject) coordsA));
1239318fe57SMatthew G. Knepley 
1249318fe57SMatthew G. Knepley   fieldTmp             = dmA->coordinateField;
1259318fe57SMatthew G. Knepley   dmA->coordinateField = dmB->coordinateField;
1269318fe57SMatthew G. Knepley   dmB->coordinateField = fieldTmp;
1279318fe57SMatthew G. Knepley   tmp       = dmA->data;
1289318fe57SMatthew G. Knepley   dmA->data = dmB->data;
1299318fe57SMatthew G. Knepley   dmB->data = tmp;
1309318fe57SMatthew G. Knepley   listTmp   = dmA->labels;
1319318fe57SMatthew G. Knepley   dmA->labels = dmB->labels;
1329318fe57SMatthew G. Knepley   dmB->labels = listTmp;
1339318fe57SMatthew G. Knepley   depthTmp  = dmA->depthLabel;
1349318fe57SMatthew G. Knepley   dmA->depthLabel = dmB->depthLabel;
1359318fe57SMatthew G. Knepley   dmB->depthLabel = depthTmp;
1369318fe57SMatthew G. Knepley   depthTmp  = dmA->celltypeLabel;
1379318fe57SMatthew G. Knepley   dmA->celltypeLabel = dmB->celltypeLabel;
1389318fe57SMatthew G. Knepley   dmB->celltypeLabel = depthTmp;
1399318fe57SMatthew G. Knepley   tmpI         = dmA->levelup;
1409318fe57SMatthew G. Knepley   dmA->levelup = dmB->levelup;
1419318fe57SMatthew G. Knepley   dmB->levelup = tmpI;
1429318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
1439318fe57SMatthew G. Knepley }
1449318fe57SMatthew G. Knepley 
1459318fe57SMatthew G. Knepley static PetscErrorCode DMPlexInterpolateInPlace_Internal(DM dm)
1469318fe57SMatthew G. Knepley {
1479318fe57SMatthew G. Knepley   DM             idm;
1489318fe57SMatthew G. Knepley 
1499318fe57SMatthew G. Knepley   PetscFunctionBegin;
1509566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolate(dm, &idm));
1519566063dSJacob Faibussowitsch   PetscCall(DMPlexCopyCoordinates(dm, idm));
1529566063dSJacob Faibussowitsch   PetscCall(DMPlexReplace_Static(dm, &idm));
1539318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
1549318fe57SMatthew G. Knepley }
1559318fe57SMatthew G. Knepley 
1569318fe57SMatthew G. Knepley /*@C
1579318fe57SMatthew G. Knepley   DMPlexCreateCoordinateSpace - Creates a finite element space for the coordinates
1589318fe57SMatthew G. Knepley 
1599318fe57SMatthew G. Knepley   Collective
1609318fe57SMatthew G. Knepley 
1619318fe57SMatthew G. Knepley   Input Parameters:
1629318fe57SMatthew G. Knepley + DM        - The DM
1634f9ab2b4SJed Brown . degree    - The degree of the finite element or PETSC_DECIDE
1649318fe57SMatthew G. Knepley - coordFunc - An optional function to map new points from refinement to the surface
1659318fe57SMatthew G. Knepley 
1669318fe57SMatthew G. Knepley   Level: advanced
1679318fe57SMatthew G. Knepley 
168db781477SPatrick Sanan .seealso: `PetscFECreateLagrange()`, `DMGetCoordinateDM()`
1699318fe57SMatthew G. Knepley @*/
1709318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateCoordinateSpace(DM dm, PetscInt degree, PetscPointFunc coordFunc)
1719318fe57SMatthew G. Knepley {
1729318fe57SMatthew G. Knepley   DM_Plex      *mesh = (DM_Plex *) dm->data;
1739318fe57SMatthew G. Knepley   DM            cdm;
1749318fe57SMatthew G. Knepley   PetscDS       cds;
1759318fe57SMatthew G. Knepley   PetscFE       fe;
1769318fe57SMatthew G. Knepley   PetscClassId  id;
1779318fe57SMatthew G. Knepley 
1789318fe57SMatthew G. Knepley   PetscFunctionBegin;
1799566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
1809566063dSJacob Faibussowitsch   PetscCall(DMGetDS(cdm, &cds));
1819566063dSJacob Faibussowitsch   PetscCall(PetscDSGetDiscretization(cds, 0, (PetscObject *) &fe));
1829566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId((PetscObject) fe, &id));
1839318fe57SMatthew G. Knepley   if (id != PETSCFE_CLASSID) {
1849318fe57SMatthew G. Knepley     PetscBool      simplex;
1859318fe57SMatthew G. Knepley     PetscInt       dim, dE, qorder;
1869318fe57SMatthew G. Knepley 
1879566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(dm, &dim));
1889566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDim(dm, &dE));
1899318fe57SMatthew G. Knepley     qorder = degree;
190d0609cedSBarry Smith     PetscObjectOptionsBegin((PetscObject) cdm);
1919566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBoundedInt("-coord_dm_default_quadrature_order", "Quadrature order is one less than quadrature points per edge", "DMPlexCreateCoordinateSpace", qorder, &qorder, NULL, 0));
192d0609cedSBarry Smith     PetscOptionsEnd();
1934f9ab2b4SJed Brown     if (degree == PETSC_DECIDE) fe = NULL;
1944f9ab2b4SJed Brown     else {
1958c4475acSStefano Zampini       PetscCall(DMPlexIsSimplex(dm, &simplex));
1969566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, dE, simplex, degree, qorder, &fe));
1974f9ab2b4SJed Brown     }
1989566063dSJacob Faibussowitsch     PetscCall(DMProjectCoordinates(dm, fe));
1999566063dSJacob Faibussowitsch     PetscCall(PetscFEDestroy(&fe));
2009318fe57SMatthew G. Knepley   }
2019318fe57SMatthew G. Knepley   mesh->coordFunc = coordFunc;
2029318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
2039318fe57SMatthew G. Knepley }
2049318fe57SMatthew G. Knepley 
2051df5d5c5SMatthew G. Knepley /*@
2061df5d5c5SMatthew G. Knepley   DMPlexCreateDoublet - Creates a mesh of two cells of the specified type, optionally with later refinement.
2071df5d5c5SMatthew G. Knepley 
208d083f849SBarry Smith   Collective
2091df5d5c5SMatthew G. Knepley 
2101df5d5c5SMatthew G. Knepley   Input Parameters:
2111df5d5c5SMatthew G. Knepley + comm - The communicator for the DM object
2121df5d5c5SMatthew G. Knepley . dim - The spatial dimension
2131df5d5c5SMatthew G. Knepley . simplex - Flag for simplicial cells, otherwise they are tensor product cells
2141df5d5c5SMatthew G. Knepley . interpolate - Flag to create intermediate mesh pieces (edges, faces)
2151df5d5c5SMatthew G. Knepley - refinementLimit - A nonzero number indicates the largest admissible volume for a refined cell
2161df5d5c5SMatthew G. Knepley 
2171df5d5c5SMatthew G. Knepley   Output Parameter:
2181df5d5c5SMatthew G. Knepley . dm - The DM object
2191df5d5c5SMatthew G. Knepley 
2201df5d5c5SMatthew G. Knepley   Level: beginner
2211df5d5c5SMatthew G. Knepley 
222db781477SPatrick Sanan .seealso: `DMSetType()`, `DMCreate()`
2231df5d5c5SMatthew G. Knepley @*/
2240fdc7489SMatthew Knepley PetscErrorCode DMPlexCreateDoublet(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscBool interpolate, PetscReal refinementLimit, DM *newdm)
2251df5d5c5SMatthew G. Knepley {
2261df5d5c5SMatthew G. Knepley   DM             dm;
2271df5d5c5SMatthew G. Knepley   PetscMPIInt    rank;
2281df5d5c5SMatthew G. Knepley 
2291df5d5c5SMatthew G. Knepley   PetscFunctionBegin;
2309566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, &dm));
2319566063dSJacob Faibussowitsch   PetscCall(DMSetType(dm, DMPLEX));
2329566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
2339566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
234ce78fa2fSMatthew G. Knepley   switch (dim) {
235ce78fa2fSMatthew G. Knepley   case 2:
2369566063dSJacob Faibussowitsch     if (simplex) PetscCall(PetscObjectSetName((PetscObject) dm, "triangular"));
2379566063dSJacob Faibussowitsch     else         PetscCall(PetscObjectSetName((PetscObject) dm, "quadrilateral"));
238ce78fa2fSMatthew G. Knepley     break;
239ce78fa2fSMatthew G. Knepley   case 3:
2409566063dSJacob Faibussowitsch     if (simplex) PetscCall(PetscObjectSetName((PetscObject) dm, "tetrahedral"));
2419566063dSJacob Faibussowitsch     else         PetscCall(PetscObjectSetName((PetscObject) dm, "hexahedral"));
242ce78fa2fSMatthew G. Knepley     break;
243ce78fa2fSMatthew G. Knepley   default:
24463a3b9bcSJacob Faibussowitsch     SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %" PetscInt_FMT, dim);
245ce78fa2fSMatthew G. Knepley   }
2461df5d5c5SMatthew G. Knepley   if (rank) {
2471df5d5c5SMatthew G. Knepley     PetscInt numPoints[2] = {0, 0};
2489566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, NULL, NULL, NULL, NULL));
2491df5d5c5SMatthew G. Knepley   } else {
2501df5d5c5SMatthew G. Knepley     switch (dim) {
2511df5d5c5SMatthew G. Knepley     case 2:
2521df5d5c5SMatthew G. Knepley       if (simplex) {
2531df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {4, 2};
2541df5d5c5SMatthew G. Knepley         PetscInt    coneSize[6]         = {3, 3, 0, 0, 0, 0};
2551df5d5c5SMatthew G. Knepley         PetscInt    cones[6]            = {2, 3, 4,  5, 4, 3};
2561df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[6] = {0, 0, 0,  0, 0, 0};
2571df5d5c5SMatthew G. Knepley         PetscScalar vertexCoords[8]     = {-0.5, 0.5, 0.0, 0.0, 0.0, 1.0, 0.5, 0.5};
2581df5d5c5SMatthew G. Knepley 
2599566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
2601df5d5c5SMatthew G. Knepley       } else {
2611df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {6, 2};
2621df5d5c5SMatthew G. Knepley         PetscInt    coneSize[8]         = {4, 4, 0, 0, 0, 0, 0, 0};
2631df5d5c5SMatthew G. Knepley         PetscInt    cones[8]            = {2, 3, 4, 5,  3, 6, 7, 4};
2641df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[8] = {0, 0, 0, 0,  0, 0, 0, 0};
2651df5d5c5SMatthew 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};
2661df5d5c5SMatthew G. Knepley 
2679566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
2681df5d5c5SMatthew G. Knepley       }
2691df5d5c5SMatthew G. Knepley       break;
2701df5d5c5SMatthew G. Knepley     case 3:
2711df5d5c5SMatthew G. Knepley       if (simplex) {
2721df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {5, 2};
2731df5d5c5SMatthew G. Knepley         PetscInt    coneSize[7]         = {4, 4, 0, 0, 0, 0, 0};
2741df5d5c5SMatthew G. Knepley         PetscInt    cones[8]            = {4, 3, 5, 2,  5, 3, 4, 6};
2751df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[8] = {0, 0, 0, 0,  0, 0, 0, 0};
2761df5d5c5SMatthew 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};
2771df5d5c5SMatthew G. Knepley 
2789566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
2791df5d5c5SMatthew G. Knepley       } else {
2801df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]         = {12, 2};
2811df5d5c5SMatthew G. Knepley         PetscInt    coneSize[14]         = {8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
2821df5d5c5SMatthew G. Knepley         PetscInt    cones[16]            = {2, 3, 4, 5, 6, 7, 8, 9,  5, 4, 10, 11, 7, 12, 13, 8};
2831df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[16] = {0, 0, 0, 0, 0, 0, 0, 0,  0, 0,  0,  0, 0,  0,  0, 0};
2841df5d5c5SMatthew 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,
2851df5d5c5SMatthew 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,
2861df5d5c5SMatthew 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};
2871df5d5c5SMatthew G. Knepley 
2889566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
2891df5d5c5SMatthew G. Knepley       }
2901df5d5c5SMatthew G. Knepley       break;
2911df5d5c5SMatthew G. Knepley     default:
29263a3b9bcSJacob Faibussowitsch       SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %" PetscInt_FMT, dim);
2931df5d5c5SMatthew G. Knepley     }
2941df5d5c5SMatthew G. Knepley   }
2951df5d5c5SMatthew G. Knepley   *newdm = dm;
2961df5d5c5SMatthew G. Knepley   if (refinementLimit > 0.0) {
2971df5d5c5SMatthew G. Knepley     DM rdm;
2981df5d5c5SMatthew G. Knepley     const char *name;
2991df5d5c5SMatthew G. Knepley 
3009566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementUniform(*newdm, PETSC_FALSE));
3019566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementLimit(*newdm, refinementLimit));
3029566063dSJacob Faibussowitsch     PetscCall(DMRefine(*newdm, comm, &rdm));
3039566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) *newdm, &name));
3049566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)    rdm,  name));
3059566063dSJacob Faibussowitsch     PetscCall(DMDestroy(newdm));
3061df5d5c5SMatthew G. Knepley     *newdm = rdm;
3071df5d5c5SMatthew G. Knepley   }
3081df5d5c5SMatthew G. Knepley   if (interpolate) {
3095fd9971aSMatthew G. Knepley     DM idm;
3101df5d5c5SMatthew G. Knepley 
3119566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*newdm, &idm));
3129566063dSJacob Faibussowitsch     PetscCall(DMDestroy(newdm));
3131df5d5c5SMatthew G. Knepley     *newdm = idm;
3141df5d5c5SMatthew G. Knepley   }
3151df5d5c5SMatthew G. Knepley   PetscFunctionReturn(0);
3161df5d5c5SMatthew G. Knepley }
3171df5d5c5SMatthew G. Knepley 
3189318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
3199318fe57SMatthew G. Knepley {
3209318fe57SMatthew G. Knepley   const PetscInt numVertices    = 2;
3219318fe57SMatthew G. Knepley   PetscInt       markerRight    = 1;
3229318fe57SMatthew G. Knepley   PetscInt       markerLeft     = 1;
3239318fe57SMatthew G. Knepley   PetscBool      markerSeparate = PETSC_FALSE;
3249318fe57SMatthew G. Knepley   Vec            coordinates;
3259318fe57SMatthew G. Knepley   PetscSection   coordSection;
3269318fe57SMatthew G. Knepley   PetscScalar   *coords;
3279318fe57SMatthew G. Knepley   PetscInt       coordSize;
3289318fe57SMatthew G. Knepley   PetscMPIInt    rank;
3299318fe57SMatthew G. Knepley   PetscInt       cdim = 1, v;
330552f7358SJed Brown 
3319318fe57SMatthew G. Knepley   PetscFunctionBegin;
3329566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
3339318fe57SMatthew G. Knepley   if (markerSeparate) {
3349318fe57SMatthew G. Knepley     markerRight  = 2;
3359318fe57SMatthew G. Knepley     markerLeft   = 1;
3369318fe57SMatthew G. Knepley   }
3379566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
338c5853193SPierre Jolivet   if (rank == 0) {
3399566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numVertices));
3409566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
3419566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", 0, markerLeft));
3429566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", 1, markerRight));
3439318fe57SMatthew G. Knepley   }
3449566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
3459566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
3469318fe57SMatthew G. Knepley   /* Build coordinates */
3479566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, cdim));
3489566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
3499566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
3509566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, 0, numVertices));
3519566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, cdim));
3529318fe57SMatthew G. Knepley   for (v = 0; v < numVertices; ++v) {
3539566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, cdim));
3549566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, cdim));
3559318fe57SMatthew G. Knepley   }
3569566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
3579566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
3589566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
3599566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
3609566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
3619566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, cdim));
3629566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates,VECSTANDARD));
3639566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
3649318fe57SMatthew G. Knepley   coords[0] = lower[0];
3659318fe57SMatthew G. Knepley   coords[1] = upper[0];
3669566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
3679566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
3689566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
3699318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
3709318fe57SMatthew G. Knepley }
37126492d91SMatthew G. Knepley 
3729318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
373552f7358SJed Brown {
3741df21d24SMatthew G. Knepley   const PetscInt numVertices    = (edges[0]+1)*(edges[1]+1);
3751df21d24SMatthew G. Knepley   const PetscInt numEdges       = edges[0]*(edges[1]+1) + (edges[0]+1)*edges[1];
376552f7358SJed Brown   PetscInt       markerTop      = 1;
377552f7358SJed Brown   PetscInt       markerBottom   = 1;
378552f7358SJed Brown   PetscInt       markerRight    = 1;
379552f7358SJed Brown   PetscInt       markerLeft     = 1;
380552f7358SJed Brown   PetscBool      markerSeparate = PETSC_FALSE;
381552f7358SJed Brown   Vec            coordinates;
382552f7358SJed Brown   PetscSection   coordSection;
383552f7358SJed Brown   PetscScalar    *coords;
384552f7358SJed Brown   PetscInt       coordSize;
385552f7358SJed Brown   PetscMPIInt    rank;
386552f7358SJed Brown   PetscInt       v, vx, vy;
387552f7358SJed Brown 
388552f7358SJed Brown   PetscFunctionBegin;
3899566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
390552f7358SJed Brown   if (markerSeparate) {
3911df21d24SMatthew G. Knepley     markerTop    = 3;
3921df21d24SMatthew G. Knepley     markerBottom = 1;
3931df21d24SMatthew G. Knepley     markerRight  = 2;
3941df21d24SMatthew G. Knepley     markerLeft   = 4;
395552f7358SJed Brown   }
3969566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
397dd400576SPatrick Sanan   if (rank == 0) {
398552f7358SJed Brown     PetscInt e, ex, ey;
399552f7358SJed Brown 
4009566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numEdges+numVertices));
401552f7358SJed Brown     for (e = 0; e < numEdges; ++e) {
4029566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(dm, e, 2));
403552f7358SJed Brown     }
4049566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
405552f7358SJed Brown     for (vx = 0; vx <= edges[0]; vx++) {
406552f7358SJed Brown       for (ey = 0; ey < edges[1]; ey++) {
407552f7358SJed Brown         PetscInt edge   = vx*edges[1] + ey + edges[0]*(edges[1]+1);
408552f7358SJed Brown         PetscInt vertex = ey*(edges[0]+1) + vx + numEdges;
409da80777bSKarl Rupp         PetscInt cone[2];
410552f7358SJed Brown 
411da80777bSKarl Rupp         cone[0] = vertex; cone[1] = vertex+edges[0]+1;
4129566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, edge, cone));
413552f7358SJed Brown         if (vx == edges[0]) {
4149566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge,    markerRight));
4159566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
416552f7358SJed Brown           if (ey == edges[1]-1) {
4179566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
4189566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerRight));
419552f7358SJed Brown           }
420552f7358SJed Brown         } else if (vx == 0) {
4219566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge,    markerLeft));
4229566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
423552f7358SJed Brown           if (ey == edges[1]-1) {
4249566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
4259566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerLeft));
426552f7358SJed Brown           }
427552f7358SJed Brown         }
428552f7358SJed Brown       }
429552f7358SJed Brown     }
430552f7358SJed Brown     for (vy = 0; vy <= edges[1]; vy++) {
431552f7358SJed Brown       for (ex = 0; ex < edges[0]; ex++) {
432552f7358SJed Brown         PetscInt edge   = vy*edges[0]     + ex;
433552f7358SJed Brown         PetscInt vertex = vy*(edges[0]+1) + ex + numEdges;
434da80777bSKarl Rupp         PetscInt cone[2];
435552f7358SJed Brown 
436da80777bSKarl Rupp         cone[0] = vertex; cone[1] = vertex+1;
4379566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, edge, cone));
438552f7358SJed Brown         if (vy == edges[1]) {
4399566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge,    markerTop));
4409566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
441552f7358SJed Brown           if (ex == edges[0]-1) {
4429566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
4439566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerTop));
444552f7358SJed Brown           }
445552f7358SJed Brown         } else if (vy == 0) {
4469566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge,    markerBottom));
4479566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
448552f7358SJed Brown           if (ex == edges[0]-1) {
4499566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
4509566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerBottom));
451552f7358SJed Brown           }
452552f7358SJed Brown         }
453552f7358SJed Brown       }
454552f7358SJed Brown     }
455552f7358SJed Brown   }
4569566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
4579566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
458552f7358SJed Brown   /* Build coordinates */
4599566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, 2));
4609566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
4619566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
4629566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, numEdges, numEdges + numVertices));
4639566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, 2));
464552f7358SJed Brown   for (v = numEdges; v < numEdges+numVertices; ++v) {
4659566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, 2));
4669566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, 2));
467552f7358SJed Brown   }
4689566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
4699566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
4709566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
4719566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
4729566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
4739566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, 2));
4749566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates,VECSTANDARD));
4759566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
476552f7358SJed Brown   for (vy = 0; vy <= edges[1]; ++vy) {
477552f7358SJed Brown     for (vx = 0; vx <= edges[0]; ++vx) {
478552f7358SJed Brown       coords[(vy*(edges[0]+1)+vx)*2+0] = lower[0] + ((upper[0] - lower[0])/edges[0])*vx;
479552f7358SJed Brown       coords[(vy*(edges[0]+1)+vx)*2+1] = lower[1] + ((upper[1] - lower[1])/edges[1])*vy;
480552f7358SJed Brown     }
481552f7358SJed Brown   }
4829566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
4839566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
4849566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
485552f7358SJed Brown   PetscFunctionReturn(0);
486552f7358SJed Brown }
487552f7358SJed Brown 
4889318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt faces[])
489552f7358SJed Brown {
4909e8abbc3SMichael Lange   PetscInt       vertices[3], numVertices;
4917b59f5a9SMichael Lange   PetscInt       numFaces    = 2*faces[0]*faces[1] + 2*faces[1]*faces[2] + 2*faces[0]*faces[2];
492552f7358SJed Brown   Vec            coordinates;
493552f7358SJed Brown   PetscSection   coordSection;
494552f7358SJed Brown   PetscScalar    *coords;
495552f7358SJed Brown   PetscInt       coordSize;
496552f7358SJed Brown   PetscMPIInt    rank;
497552f7358SJed Brown   PetscInt       v, vx, vy, vz;
4987b59f5a9SMichael Lange   PetscInt       voffset, iface=0, cone[4];
499552f7358SJed Brown 
500552f7358SJed Brown   PetscFunctionBegin;
5011dca8a05SBarry Smith   PetscCheck(faces[0] >= 1 && faces[1] >= 1 && faces[2] >= 1,PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Must have at least 1 face per side");
5029566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
5039e8abbc3SMichael Lange   vertices[0] = faces[0]+1; vertices[1] = faces[1]+1; vertices[2] = faces[2]+1;
5049e8abbc3SMichael Lange   numVertices = vertices[0]*vertices[1]*vertices[2];
505dd400576SPatrick Sanan   if (rank == 0) {
506552f7358SJed Brown     PetscInt f;
507552f7358SJed Brown 
5089566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numFaces+numVertices));
509552f7358SJed Brown     for (f = 0; f < numFaces; ++f) {
5109566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(dm, f, 4));
511552f7358SJed Brown     }
5129566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
5137b59f5a9SMichael Lange 
5147b59f5a9SMichael Lange     /* Side 0 (Top) */
5157b59f5a9SMichael Lange     for (vy = 0; vy < faces[1]; vy++) {
5167b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5177b59f5a9SMichael Lange         voffset = numFaces + vertices[0]*vertices[1]*(vertices[2]-1) + vy*vertices[0] + vx;
5187b59f5a9SMichael Lange         cone[0] = voffset; cone[1] = voffset+1; cone[2] = voffset+vertices[0]+1; cone[3] = voffset+vertices[0];
5199566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
5209566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", iface, 1));
5219566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+0, 1));
5229566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+1, 1));
5239566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, 1));
5249566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]+1, 1));
5257b59f5a9SMichael Lange         iface++;
526552f7358SJed Brown       }
527552f7358SJed Brown     }
5287b59f5a9SMichael Lange 
5297b59f5a9SMichael Lange     /* Side 1 (Bottom) */
5307b59f5a9SMichael Lange     for (vy = 0; vy < faces[1]; vy++) {
5317b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5327b59f5a9SMichael Lange         voffset = numFaces + vy*(faces[0]+1) + vx;
5337b59f5a9SMichael Lange         cone[0] = voffset+1; cone[1] = voffset; cone[2] = voffset+vertices[0]; cone[3] = voffset+vertices[0]+1;
5349566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
5359566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", iface, 1));
5369566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+0, 1));
5379566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+1, 1));
5389566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, 1));
5399566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]+1, 1));
5407b59f5a9SMichael Lange         iface++;
541552f7358SJed Brown       }
542552f7358SJed Brown     }
5437b59f5a9SMichael Lange 
5447b59f5a9SMichael Lange     /* Side 2 (Front) */
5457b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
5467b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5477b59f5a9SMichael Lange         voffset = numFaces + vz*vertices[0]*vertices[1] + vx;
5487b59f5a9SMichael Lange         cone[0] = voffset; cone[1] = voffset+1; cone[2] = voffset+vertices[0]*vertices[1]+1; cone[3] = voffset+vertices[0]*vertices[1];
5499566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
5509566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", iface, 1));
5519566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+0, 1));
5529566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+1, 1));
5539566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+0, 1));
5549566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+1, 1));
5557b59f5a9SMichael Lange         iface++;
556552f7358SJed Brown       }
5577b59f5a9SMichael Lange     }
5587b59f5a9SMichael Lange 
5597b59f5a9SMichael Lange     /* Side 3 (Back) */
5607b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
5617b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5627b59f5a9SMichael Lange         voffset = numFaces + vz*vertices[0]*vertices[1] + vertices[0]*(vertices[1]-1) + vx;
5637b59f5a9SMichael Lange         cone[0] = voffset+vertices[0]*vertices[1]; cone[1] = voffset+vertices[0]*vertices[1]+1;
5647b59f5a9SMichael Lange         cone[2] = voffset+1; cone[3] = voffset;
5659566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
5669566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", iface, 1));
5679566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+0, 1));
5689566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+1, 1));
5699566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+0, 1));
5709566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+1, 1));
5717b59f5a9SMichael Lange         iface++;
5727b59f5a9SMichael Lange       }
5737b59f5a9SMichael Lange     }
5747b59f5a9SMichael Lange 
5757b59f5a9SMichael Lange     /* Side 4 (Left) */
5767b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
5777b59f5a9SMichael Lange       for (vy = 0; vy < faces[1]; vy++) {
5787b59f5a9SMichael Lange         voffset = numFaces + vz*vertices[0]*vertices[1] + vy*vertices[0];
5797b59f5a9SMichael Lange         cone[0] = voffset; cone[1] = voffset+vertices[0]*vertices[1];
5807b59f5a9SMichael Lange         cone[2] = voffset+vertices[0]*vertices[1]+vertices[0]; cone[3] = voffset+vertices[0];
5819566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
5829566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", iface, 1));
5839566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+0, 1));
5849566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, 1));
5859566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[1]+0, 1));
5869566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+vertices[0], 1));
5877b59f5a9SMichael Lange         iface++;
5887b59f5a9SMichael Lange       }
5897b59f5a9SMichael Lange     }
5907b59f5a9SMichael Lange 
5917b59f5a9SMichael Lange     /* Side 5 (Right) */
5927b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
5937b59f5a9SMichael Lange       for (vy = 0; vy < faces[1]; vy++) {
594aab5bcd8SJed Brown         voffset = numFaces + vz*vertices[0]*vertices[1] + vy*vertices[0] + faces[0];
5957b59f5a9SMichael Lange         cone[0] = voffset+vertices[0]*vertices[1]; cone[1] = voffset;
5967b59f5a9SMichael Lange         cone[2] = voffset+vertices[0]; cone[3] = voffset+vertices[0]*vertices[1]+vertices[0];
5979566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
5989566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", iface, 1));
5999566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+0, 1));
6009566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, 1));
6019566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+0, 1));
6029566063dSJacob Faibussowitsch         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+vertices[0], 1));
6037b59f5a9SMichael Lange         iface++;
6047b59f5a9SMichael Lange       }
605552f7358SJed Brown     }
606552f7358SJed Brown   }
6079566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
6089566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
609552f7358SJed Brown   /* Build coordinates */
6109566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, 3));
6119566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
6129566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
6139566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, numFaces, numFaces + numVertices));
6149566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, 3));
615552f7358SJed Brown   for (v = numFaces; v < numFaces+numVertices; ++v) {
6169566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, 3));
6179566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, 3));
618552f7358SJed Brown   }
6199566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
6209566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
6219566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
6229566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
6239566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
6249566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, 3));
6259566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates,VECSTANDARD));
6269566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
627552f7358SJed Brown   for (vz = 0; vz <= faces[2]; ++vz) {
628552f7358SJed Brown     for (vy = 0; vy <= faces[1]; ++vy) {
629552f7358SJed Brown       for (vx = 0; vx <= faces[0]; ++vx) {
630552f7358SJed Brown         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+0] = lower[0] + ((upper[0] - lower[0])/faces[0])*vx;
631552f7358SJed Brown         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+1] = lower[1] + ((upper[1] - lower[1])/faces[1])*vy;
632552f7358SJed Brown         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+2] = lower[2] + ((upper[2] - lower[2])/faces[2])*vz;
633552f7358SJed Brown       }
634552f7358SJed Brown     }
635552f7358SJed Brown   }
6369566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
6379566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
6389566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
639552f7358SJed Brown   PetscFunctionReturn(0);
640552f7358SJed Brown }
641552f7358SJed Brown 
6429318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate)
6439318fe57SMatthew G. Knepley {
6449318fe57SMatthew G. Knepley   PetscFunctionBegin;
6459318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6469566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim-1));
6479566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, dim));
6489318fe57SMatthew G. Knepley   switch (dim) {
6499566063dSJacob Faibussowitsch     case 1: PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(dm, lower, upper, faces));break;
6509566063dSJacob Faibussowitsch     case 2: PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(dm, lower, upper, faces));break;
6519566063dSJacob Faibussowitsch     case 3: PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(dm, lower, upper, faces));break;
65263a3b9bcSJacob Faibussowitsch     default: SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Dimension not supported: %" PetscInt_FMT, dim);
6539318fe57SMatthew G. Knepley   }
6549566063dSJacob Faibussowitsch   if (interpolate) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
6559318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
6569318fe57SMatthew G. Knepley }
6579318fe57SMatthew G. Knepley 
6589318fe57SMatthew G. Knepley /*@C
6599318fe57SMatthew G. Knepley   DMPlexCreateBoxSurfaceMesh - Creates a mesh on the surface of the tensor product of unit intervals (box) using tensor cells (hexahedra).
6609318fe57SMatthew G. Knepley 
6619318fe57SMatthew G. Knepley   Collective
6629318fe57SMatthew G. Knepley 
6639318fe57SMatthew G. Knepley   Input Parameters:
6649318fe57SMatthew G. Knepley + comm        - The communicator for the DM object
6659318fe57SMatthew G. Knepley . dim         - The spatial dimension of the box, so the resulting mesh is has dimension dim-1
6669318fe57SMatthew 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
6679318fe57SMatthew G. Knepley . lower       - The lower left corner, or NULL for (0, 0, 0)
6689318fe57SMatthew G. Knepley . upper       - The upper right corner, or NULL for (1, 1, 1)
6699318fe57SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
6709318fe57SMatthew G. Knepley 
6719318fe57SMatthew G. Knepley   Output Parameter:
6729318fe57SMatthew G. Knepley . dm  - The DM object
6739318fe57SMatthew G. Knepley 
6749318fe57SMatthew G. Knepley   Level: beginner
6759318fe57SMatthew G. Knepley 
676db781477SPatrick Sanan .seealso: `DMSetFromOptions()`, `DMPlexCreateBoxMesh()`, `DMPlexCreateFromFile()`, `DMSetType()`, `DMCreate()`
6779318fe57SMatthew G. Knepley @*/
6789318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateBoxSurfaceMesh(MPI_Comm comm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate, DM *dm)
6799318fe57SMatthew G. Knepley {
6809318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
6819318fe57SMatthew G. Knepley   PetscReal      low[3] = {0, 0, 0};
6829318fe57SMatthew G. Knepley   PetscReal      upp[3] = {1, 1, 1};
6839318fe57SMatthew G. Knepley 
6849318fe57SMatthew G. Knepley   PetscFunctionBegin;
6859566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm,dm));
6869566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm,DMPLEX));
6879566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(*dm, dim, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, interpolate));
6889318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
6899318fe57SMatthew G. Knepley }
6909318fe57SMatthew G. Knepley 
6919318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateLineMesh_Internal(DM dm,PetscInt segments,PetscReal lower,PetscReal upper,DMBoundaryType bd)
692fdbf62faSLisandro Dalcin {
693fdbf62faSLisandro Dalcin   PetscInt       i,fStart,fEnd,numCells = 0,numVerts = 0;
694fdbf62faSLisandro Dalcin   PetscInt       numPoints[2],*coneSize,*cones,*coneOrientations;
695fdbf62faSLisandro Dalcin   PetscScalar    *vertexCoords;
696fdbf62faSLisandro Dalcin   PetscReal      L,maxCell;
697fdbf62faSLisandro Dalcin   PetscBool      markerSeparate = PETSC_FALSE;
698fdbf62faSLisandro Dalcin   PetscInt       markerLeft  = 1, faceMarkerLeft  = 1;
699fdbf62faSLisandro Dalcin   PetscInt       markerRight = 1, faceMarkerRight = 2;
700fdbf62faSLisandro Dalcin   PetscBool      wrap = (bd == DM_BOUNDARY_PERIODIC || bd == DM_BOUNDARY_TWIST) ? PETSC_TRUE : PETSC_FALSE;
701fdbf62faSLisandro Dalcin   PetscMPIInt    rank;
702fdbf62faSLisandro Dalcin 
703fdbf62faSLisandro Dalcin   PetscFunctionBegin;
7049318fe57SMatthew G. Knepley   PetscValidPointer(dm,1);
705fdbf62faSLisandro Dalcin 
7069566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm,1));
7079566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm,"marker"));
7089566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm,"Face Sets"));
709fdbf62faSLisandro Dalcin 
7109566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject) dm),&rank));
711dd400576SPatrick Sanan   if (rank == 0) numCells = segments;
712dd400576SPatrick Sanan   if (rank == 0) numVerts = segments + (wrap ? 0 : 1);
713fdbf62faSLisandro Dalcin 
714fdbf62faSLisandro Dalcin   numPoints[0] = numVerts ; numPoints[1] = numCells;
7159566063dSJacob Faibussowitsch   PetscCall(PetscMalloc4(numCells+numVerts,&coneSize,numCells*2,&cones,numCells+numVerts,&coneOrientations,numVerts,&vertexCoords));
7169566063dSJacob Faibussowitsch   PetscCall(PetscArrayzero(coneOrientations,numCells+numVerts));
717fdbf62faSLisandro Dalcin   for (i = 0; i < numCells; ++i) { coneSize[i] = 2; }
718fdbf62faSLisandro Dalcin   for (i = 0; i < numVerts; ++i) { coneSize[numCells+i] = 0; }
719fdbf62faSLisandro Dalcin   for (i = 0; i < numCells; ++i) { cones[2*i] = numCells + i%numVerts; cones[2*i+1] = numCells + (i+1)%numVerts; }
720fdbf62faSLisandro Dalcin   for (i = 0; i < numVerts; ++i) { vertexCoords[i] = lower + (upper-lower)*((PetscReal)i/(PetscReal)numCells); }
7219566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateFromDAG(dm,1,numPoints,coneSize,cones,coneOrientations,vertexCoords));
7229566063dSJacob Faibussowitsch   PetscCall(PetscFree4(coneSize,cones,coneOrientations,vertexCoords));
723fdbf62faSLisandro Dalcin 
7249566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options,((PetscObject)dm)->prefix,"-dm_plex_separate_marker",&markerSeparate,NULL));
725fdbf62faSLisandro Dalcin   if (markerSeparate) { markerLeft = faceMarkerLeft; markerRight = faceMarkerRight;}
726dd400576SPatrick Sanan   if (!wrap && rank == 0) {
7279566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm,1,&fStart,&fEnd));
7289566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm,"marker",fStart,markerLeft));
7299566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm,"marker",fEnd-1,markerRight));
7309566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm,"Face Sets",fStart,faceMarkerLeft));
7319566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm,"Face Sets",fEnd-1,faceMarkerRight));
732fdbf62faSLisandro Dalcin   }
733fdbf62faSLisandro Dalcin   if (wrap) {
734fdbf62faSLisandro Dalcin     L       = upper - lower;
735fdbf62faSLisandro Dalcin     maxCell = (PetscReal)1.1*(L/(PetscReal)PetscMax(1,segments));
7369566063dSJacob Faibussowitsch     PetscCall(DMSetPeriodicity(dm,PETSC_TRUE,&maxCell,&L,&bd));
737fdbf62faSLisandro Dalcin   }
7389566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
739fdbf62faSLisandro Dalcin   PetscFunctionReturn(0);
740fdbf62faSLisandro Dalcin }
741fdbf62faSLisandro Dalcin 
7429318fe57SMatthew 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)
743d6218766SMatthew G. Knepley {
7449318fe57SMatthew G. Knepley   DM             boundary, vol;
745768d5fceSMatthew G. Knepley   PetscInt       i;
746d6218766SMatthew G. Knepley 
747d6218766SMatthew G. Knepley   PetscFunctionBegin;
7489318fe57SMatthew G. Knepley   PetscValidPointer(dm, 1);
74908401ef6SPierre Jolivet   for (i = 0; i < dim; ++i) PetscCheck(periodicity[i] == DM_BOUNDARY_NONE,PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Periodicity is not supported for simplex meshes");
7509566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject) dm), &boundary));
7519566063dSJacob Faibussowitsch   PetscCall(DMSetType(boundary, DMPLEX));
7529566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(boundary, dim, faces, lower, upper, PETSC_FALSE));
7539566063dSJacob Faibussowitsch   PetscCall(DMPlexGenerate(boundary, NULL, interpolate, &vol));
7545de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_FALSE, vol));
7559566063dSJacob Faibussowitsch   PetscCall(DMPlexReplace_Static(dm, &vol));
7569566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&boundary));
757d6218766SMatthew G. Knepley   PetscFunctionReturn(0);
758d6218766SMatthew G. Knepley }
759d6218766SMatthew G. Knepley 
7603dfda0b1SToby Isaac static PetscErrorCode DMPlexCreateCubeMesh_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], DMBoundaryType bdX, DMBoundaryType bdY, DMBoundaryType bdZ)
7613dfda0b1SToby Isaac {
762ed0e4b50SMatthew G. Knepley   DMLabel        cutLabel = NULL;
763f4eb4c5dSMatthew G. Knepley   PetscInt       markerTop      = 1, faceMarkerTop      = 1;
764f4eb4c5dSMatthew G. Knepley   PetscInt       markerBottom   = 1, faceMarkerBottom   = 1;
765f4eb4c5dSMatthew G. Knepley   PetscInt       markerFront    = 1, faceMarkerFront    = 1;
766f4eb4c5dSMatthew G. Knepley   PetscInt       markerBack     = 1, faceMarkerBack     = 1;
767f4eb4c5dSMatthew G. Knepley   PetscInt       markerRight    = 1, faceMarkerRight    = 1;
768f4eb4c5dSMatthew G. Knepley   PetscInt       markerLeft     = 1, faceMarkerLeft     = 1;
7693dfda0b1SToby Isaac   PetscInt       dim;
770d8211ee3SMatthew G. Knepley   PetscBool      markerSeparate = PETSC_FALSE, cutMarker = PETSC_FALSE;
7713dfda0b1SToby Isaac   PetscMPIInt    rank;
7723dfda0b1SToby Isaac 
7733dfda0b1SToby Isaac   PetscFunctionBegin;
7749566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm,&dim));
7759566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
7769566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm,"marker"));
7779566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm,"Face Sets"));
7789566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL));
779d8211ee3SMatthew G. Knepley   if (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ||
780d8211ee3SMatthew G. Knepley       bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ||
781d8211ee3SMatthew G. Knepley       bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST) {
7824c67ea77SStefano Zampini 
7839566063dSJacob Faibussowitsch     if (cutMarker) {PetscCall(DMCreateLabel(dm, "periodic_cut")); PetscCall(DMGetLabel(dm, "periodic_cut", &cutLabel));}
784d8211ee3SMatthew G. Knepley   }
7853dfda0b1SToby Isaac   switch (dim) {
7863dfda0b1SToby Isaac   case 2:
787f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 3;
788f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
789f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 2;
790f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 4;
7913dfda0b1SToby Isaac     break;
7923dfda0b1SToby Isaac   case 3:
793f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
794f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 2;
795f4eb4c5dSMatthew G. Knepley     faceMarkerFront  = 3;
796f4eb4c5dSMatthew G. Knepley     faceMarkerBack   = 4;
797f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 5;
798f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 6;
7993dfda0b1SToby Isaac     break;
8003dfda0b1SToby Isaac   default:
80163a3b9bcSJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Dimension %" PetscInt_FMT " not supported",dim);
8023dfda0b1SToby Isaac   }
8039566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
804f4eb4c5dSMatthew G. Knepley   if (markerSeparate) {
805f4eb4c5dSMatthew G. Knepley     markerBottom = faceMarkerBottom;
806f4eb4c5dSMatthew G. Knepley     markerTop    = faceMarkerTop;
807f4eb4c5dSMatthew G. Knepley     markerFront  = faceMarkerFront;
808f4eb4c5dSMatthew G. Knepley     markerBack   = faceMarkerBack;
809f4eb4c5dSMatthew G. Knepley     markerRight  = faceMarkerRight;
810f4eb4c5dSMatthew G. Knepley     markerLeft   = faceMarkerLeft;
8113dfda0b1SToby Isaac   }
8123dfda0b1SToby Isaac   {
813dd400576SPatrick Sanan     const PetscInt numXEdges    = rank == 0 ? edges[0] : 0;
814dd400576SPatrick Sanan     const PetscInt numYEdges    = rank == 0 ? edges[1] : 0;
815dd400576SPatrick Sanan     const PetscInt numZEdges    = rank == 0 ? edges[2] : 0;
816dd400576SPatrick Sanan     const PetscInt numXVertices = rank == 0 ? (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ? edges[0] : edges[0]+1) : 0;
817dd400576SPatrick Sanan     const PetscInt numYVertices = rank == 0 ? (bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ? edges[1] : edges[1]+1) : 0;
818dd400576SPatrick Sanan     const PetscInt numZVertices = rank == 0 ? (bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST ? edges[2] : edges[2]+1) : 0;
8193dfda0b1SToby Isaac     const PetscInt numCells     = numXEdges*numYEdges*numZEdges;
8203dfda0b1SToby Isaac     const PetscInt numXFaces    = numYEdges*numZEdges;
8213dfda0b1SToby Isaac     const PetscInt numYFaces    = numXEdges*numZEdges;
8223dfda0b1SToby Isaac     const PetscInt numZFaces    = numXEdges*numYEdges;
8233dfda0b1SToby Isaac     const PetscInt numTotXFaces = numXVertices*numXFaces;
8243dfda0b1SToby Isaac     const PetscInt numTotYFaces = numYVertices*numYFaces;
8253dfda0b1SToby Isaac     const PetscInt numTotZFaces = numZVertices*numZFaces;
8263dfda0b1SToby Isaac     const PetscInt numFaces     = numTotXFaces + numTotYFaces + numTotZFaces;
8273dfda0b1SToby Isaac     const PetscInt numTotXEdges = numXEdges*numYVertices*numZVertices;
8283dfda0b1SToby Isaac     const PetscInt numTotYEdges = numYEdges*numXVertices*numZVertices;
8293dfda0b1SToby Isaac     const PetscInt numTotZEdges = numZEdges*numXVertices*numYVertices;
8303dfda0b1SToby Isaac     const PetscInt numVertices  = numXVertices*numYVertices*numZVertices;
8313dfda0b1SToby Isaac     const PetscInt numEdges     = numTotXEdges + numTotYEdges + numTotZEdges;
8323dfda0b1SToby Isaac     const PetscInt firstVertex  = (dim == 2) ? numFaces : numCells;
8333dfda0b1SToby Isaac     const PetscInt firstXFace   = (dim == 2) ? 0 : numCells + numVertices;
8343dfda0b1SToby Isaac     const PetscInt firstYFace   = firstXFace + numTotXFaces;
8353dfda0b1SToby Isaac     const PetscInt firstZFace   = firstYFace + numTotYFaces;
8363dfda0b1SToby Isaac     const PetscInt firstXEdge   = numCells + numFaces + numVertices;
8373dfda0b1SToby Isaac     const PetscInt firstYEdge   = firstXEdge + numTotXEdges;
8383dfda0b1SToby Isaac     const PetscInt firstZEdge   = firstYEdge + numTotYEdges;
8393dfda0b1SToby Isaac     Vec            coordinates;
8403dfda0b1SToby Isaac     PetscSection   coordSection;
8413dfda0b1SToby Isaac     PetscScalar   *coords;
8423dfda0b1SToby Isaac     PetscInt       coordSize;
8433dfda0b1SToby Isaac     PetscInt       v, vx, vy, vz;
8443dfda0b1SToby Isaac     PetscInt       c, f, fx, fy, fz, e, ex, ey, ez;
8453dfda0b1SToby Isaac 
8469566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells+numFaces+numEdges+numVertices));
8473dfda0b1SToby Isaac     for (c = 0; c < numCells; c++) {
8489566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(dm, c, 6));
8493dfda0b1SToby Isaac     }
8503dfda0b1SToby Isaac     for (f = firstXFace; f < firstXFace+numFaces; ++f) {
8519566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(dm, f, 4));
8523dfda0b1SToby Isaac     }
8533dfda0b1SToby Isaac     for (e = firstXEdge; e < firstXEdge+numEdges; ++e) {
8549566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(dm, e, 2));
8553dfda0b1SToby Isaac     }
8569566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
8573dfda0b1SToby Isaac     /* Build cells */
8583dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
8593dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
8603dfda0b1SToby Isaac         for (fx = 0; fx < numXEdges; ++fx) {
8613dfda0b1SToby Isaac           PetscInt cell    = (fz*numYEdges + fy)*numXEdges + fx;
8623dfda0b1SToby Isaac           PetscInt faceB   = firstZFace + (fy*numXEdges+fx)*numZVertices +   fz;
8633dfda0b1SToby Isaac           PetscInt faceT   = firstZFace + (fy*numXEdges+fx)*numZVertices + ((fz+1)%numZVertices);
8643dfda0b1SToby Isaac           PetscInt faceF   = firstYFace + (fz*numXEdges+fx)*numYVertices +   fy;
8653dfda0b1SToby Isaac           PetscInt faceK   = firstYFace + (fz*numXEdges+fx)*numYVertices + ((fy+1)%numYVertices);
8663dfda0b1SToby Isaac           PetscInt faceL   = firstXFace + (fz*numYEdges+fy)*numXVertices +   fx;
8673dfda0b1SToby Isaac           PetscInt faceR   = firstXFace + (fz*numYEdges+fy)*numXVertices + ((fx+1)%numXVertices);
8683dfda0b1SToby Isaac                             /* B,  T,  F,  K,  R,  L */
869b5a892a1SMatthew G. Knepley           PetscInt ornt[6] = {-2,  0,  0, -3,  0, -2}; /* ??? */
87042206facSLisandro Dalcin           PetscInt cone[6];
8713dfda0b1SToby Isaac 
8723dfda0b1SToby Isaac           /* no boundary twisting in 3D */
8733dfda0b1SToby Isaac           cone[0] = faceB; cone[1] = faceT; cone[2] = faceF; cone[3] = faceK; cone[4] = faceR; cone[5] = faceL;
8749566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, cell, cone));
8759566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, cell, ornt));
8769566063dSJacob Faibussowitsch           if (bdX != DM_BOUNDARY_NONE && fx == numXEdges-1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
8779566063dSJacob Faibussowitsch           if (bdY != DM_BOUNDARY_NONE && fy == numYEdges-1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
8789566063dSJacob Faibussowitsch           if (bdZ != DM_BOUNDARY_NONE && fz == numZEdges-1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
8793dfda0b1SToby Isaac         }
8803dfda0b1SToby Isaac       }
8813dfda0b1SToby Isaac     }
8823dfda0b1SToby Isaac     /* Build x faces */
8833dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
8843dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
8853dfda0b1SToby Isaac         for (fx = 0; fx < numXVertices; ++fx) {
8863dfda0b1SToby Isaac           PetscInt face    = firstXFace + (fz*numYEdges+fy)     *numXVertices+fx;
8873dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy                   *numXVertices+fx)*numZEdges + fz;
8883dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (((fy+1)%numYVertices)*numXVertices+fx)*numZEdges + fz;
8893dfda0b1SToby Isaac           PetscInt edgeB   = firstYEdge + (fz                   *numXVertices+fx)*numYEdges + fy;
8903dfda0b1SToby Isaac           PetscInt edgeT   = firstYEdge + (((fz+1)%numZVertices)*numXVertices+fx)*numYEdges + fy;
891b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
8923dfda0b1SToby Isaac           PetscInt cone[4];
8933dfda0b1SToby Isaac 
8943dfda0b1SToby Isaac           if (dim == 3) {
8953dfda0b1SToby Isaac             /* markers */
8963dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
8973dfda0b1SToby Isaac               if (fx == numXVertices-1) {
8989566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerRight));
8999566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerRight));
9003dfda0b1SToby Isaac               }
9013dfda0b1SToby Isaac               else if (fx == 0) {
9029566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerLeft));
9039566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerLeft));
9043dfda0b1SToby Isaac               }
9053dfda0b1SToby Isaac             }
9063dfda0b1SToby Isaac           }
9073dfda0b1SToby Isaac           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
9089566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
9099566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
9103dfda0b1SToby Isaac         }
9113dfda0b1SToby Isaac       }
9123dfda0b1SToby Isaac     }
9133dfda0b1SToby Isaac     /* Build y faces */
9143dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
91542206facSLisandro Dalcin       for (fx = 0; fx < numXEdges; ++fx) {
9163dfda0b1SToby Isaac         for (fy = 0; fy < numYVertices; ++fy) {
9173dfda0b1SToby Isaac           PetscInt face    = firstYFace + (fz*numXEdges+fx)*numYVertices + fy;
9183dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy*numXVertices+  fx)*numZEdges + fz;
9193dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (fy*numXVertices+((fx+1)%numXVertices))*numZEdges + fz;
9203dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz                   *numYVertices+fy)*numXEdges + fx;
9213dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (((fz+1)%numZVertices)*numYVertices+fy)*numXEdges + fx;
922b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
9233dfda0b1SToby Isaac           PetscInt cone[4];
9243dfda0b1SToby Isaac 
9253dfda0b1SToby Isaac           if (dim == 3) {
9263dfda0b1SToby Isaac             /* markers */
9273dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
9283dfda0b1SToby Isaac               if (fy == numYVertices-1) {
9299566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerBack));
9309566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerBack));
9313dfda0b1SToby Isaac               }
9323dfda0b1SToby Isaac               else if (fy == 0) {
9339566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerFront));
9349566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerFront));
9353dfda0b1SToby Isaac               }
9363dfda0b1SToby Isaac             }
9373dfda0b1SToby Isaac           }
9383dfda0b1SToby Isaac           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
9399566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
9409566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
9413dfda0b1SToby Isaac         }
9423dfda0b1SToby Isaac       }
9433dfda0b1SToby Isaac     }
9443dfda0b1SToby Isaac     /* Build z faces */
9453dfda0b1SToby Isaac     for (fy = 0; fy < numYEdges; ++fy) {
9463dfda0b1SToby Isaac       for (fx = 0; fx < numXEdges; ++fx) {
9473dfda0b1SToby Isaac         for (fz = 0; fz < numZVertices; fz++) {
9483dfda0b1SToby Isaac           PetscInt face    = firstZFace + (fy*numXEdges+fx)*numZVertices + fz;
9493dfda0b1SToby Isaac           PetscInt edgeL   = firstYEdge + (fz*numXVertices+  fx)*numYEdges + fy;
9503dfda0b1SToby Isaac           PetscInt edgeR   = firstYEdge + (fz*numXVertices+((fx+1)%numXVertices))*numYEdges + fy;
9513dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz*numYVertices+  fy)*numXEdges + fx;
9523dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (fz*numYVertices+((fy+1)%numYVertices))*numXEdges + fx;
953b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
9543dfda0b1SToby Isaac           PetscInt cone[4];
9553dfda0b1SToby Isaac 
9563dfda0b1SToby Isaac           if (dim == 2) {
957b5a892a1SMatthew G. Knepley             if (bdX == DM_BOUNDARY_TWIST && fx == numXEdges-1) {edgeR += numYEdges-1-2*fy; ornt[1] = -1;}
9583dfda0b1SToby Isaac             if (bdY == DM_BOUNDARY_TWIST && fy == numYEdges-1) {edgeT += numXEdges-1-2*fx; ornt[2] =  0;}
9599566063dSJacob Faibussowitsch             if (bdX != DM_BOUNDARY_NONE && fx == numXEdges-1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, face, 2));
9609566063dSJacob Faibussowitsch             if (bdY != DM_BOUNDARY_NONE && fy == numYEdges-1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, face, 2));
961d1c88043SMatthew G. Knepley           } else {
9623dfda0b1SToby Isaac             /* markers */
9633dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
9643dfda0b1SToby Isaac               if (fz == numZVertices-1) {
9659566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerTop));
9669566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerTop));
9673dfda0b1SToby Isaac               }
9683dfda0b1SToby Isaac               else if (fz == 0) {
9699566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerBottom));
9709566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerBottom));
9713dfda0b1SToby Isaac               }
9723dfda0b1SToby Isaac             }
9733dfda0b1SToby Isaac           }
9743dfda0b1SToby Isaac           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
9759566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
9769566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
9773dfda0b1SToby Isaac         }
9783dfda0b1SToby Isaac       }
9793dfda0b1SToby Isaac     }
9803dfda0b1SToby Isaac     /* Build Z edges*/
9813dfda0b1SToby Isaac     for (vy = 0; vy < numYVertices; vy++) {
9823dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
9833dfda0b1SToby Isaac         for (ez = 0; ez < numZEdges; ez++) {
9843dfda0b1SToby Isaac           const PetscInt edge    = firstZEdge  + (vy*numXVertices+vx)*numZEdges + ez;
9853dfda0b1SToby Isaac           const PetscInt vertexB = firstVertex + (ez                   *numYVertices+vy)*numXVertices + vx;
9863dfda0b1SToby Isaac           const PetscInt vertexT = firstVertex + (((ez+1)%numZVertices)*numYVertices+vy)*numXVertices + vx;
9873dfda0b1SToby Isaac           PetscInt       cone[2];
9883dfda0b1SToby Isaac 
9893dfda0b1SToby Isaac           if (dim == 3) {
9903dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
9913dfda0b1SToby Isaac               if (vx == numXVertices-1) {
9929566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
9933dfda0b1SToby Isaac               }
9943dfda0b1SToby Isaac               else if (vx == 0) {
9959566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
9963dfda0b1SToby Isaac               }
9973dfda0b1SToby Isaac             }
9983dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
9993dfda0b1SToby Isaac               if (vy == numYVertices-1) {
10009566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBack));
10013dfda0b1SToby Isaac               }
10023dfda0b1SToby Isaac               else if (vy == 0) {
10039566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerFront));
10043dfda0b1SToby Isaac               }
10053dfda0b1SToby Isaac             }
10063dfda0b1SToby Isaac           }
10073dfda0b1SToby Isaac           cone[0] = vertexB; cone[1] = vertexT;
10089566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, edge, cone));
10093dfda0b1SToby Isaac         }
10103dfda0b1SToby Isaac       }
10113dfda0b1SToby Isaac     }
10123dfda0b1SToby Isaac     /* Build Y edges*/
10133dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
10143dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
10153dfda0b1SToby Isaac         for (ey = 0; ey < numYEdges; ey++) {
10163dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdY == DM_BOUNDARY_TWIST && ey == numYEdges-1) ? (numXVertices-vx-1) : (vz*numYVertices+((ey+1)%numYVertices))*numXVertices + vx;
10173dfda0b1SToby Isaac           const PetscInt edge    = firstYEdge  + (vz*numXVertices+vx)*numYEdges + ey;
10183dfda0b1SToby Isaac           const PetscInt vertexF = firstVertex + (vz*numYVertices+ey)*numXVertices + vx;
10193dfda0b1SToby Isaac           const PetscInt vertexK = firstVertex + nextv;
10203dfda0b1SToby Isaac           PetscInt       cone[2];
10213dfda0b1SToby Isaac 
10223dfda0b1SToby Isaac           cone[0] = vertexF; cone[1] = vertexK;
10239566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, edge, cone));
10243dfda0b1SToby Isaac           if (dim == 2) {
10253dfda0b1SToby Isaac             if ((bdX != DM_BOUNDARY_PERIODIC) && (bdX != DM_BOUNDARY_TWIST)) {
10263dfda0b1SToby Isaac               if (vx == numXVertices-1) {
10279566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerRight));
10289566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge,    markerRight));
10299566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
10303dfda0b1SToby Isaac                 if (ey == numYEdges-1) {
10319566063dSJacob Faibussowitsch                   PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
10323dfda0b1SToby Isaac                 }
1033d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
10349566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerLeft));
10359566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge,    markerLeft));
10369566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
10373dfda0b1SToby Isaac                 if (ey == numYEdges-1) {
10389566063dSJacob Faibussowitsch                   PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
10393dfda0b1SToby Isaac                 }
10403dfda0b1SToby Isaac               }
1041d8211ee3SMatthew G. Knepley             } else {
10424c67ea77SStefano Zampini               if (vx == 0 && cutLabel) {
10439566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, edge,    1));
10449566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, cone[0], 1));
1045d8211ee3SMatthew G. Knepley                 if (ey == numYEdges-1) {
10469566063dSJacob Faibussowitsch                   PetscCall(DMLabelSetValue(cutLabel, cone[1], 1));
10473dfda0b1SToby Isaac                 }
10483dfda0b1SToby Isaac               }
1049d8211ee3SMatthew G. Knepley             }
1050d8211ee3SMatthew G. Knepley           } else {
10513dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
10523dfda0b1SToby Isaac               if (vx == numXVertices-1) {
10539566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
1054d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
10559566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
10563dfda0b1SToby Isaac               }
10573dfda0b1SToby Isaac             }
10583dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
10593dfda0b1SToby Isaac               if (vz == numZVertices-1) {
10609566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
1061d8211ee3SMatthew G. Knepley               } else if (vz == 0) {
10629566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
10633dfda0b1SToby Isaac               }
10643dfda0b1SToby Isaac             }
10653dfda0b1SToby Isaac           }
10663dfda0b1SToby Isaac         }
10673dfda0b1SToby Isaac       }
10683dfda0b1SToby Isaac     }
10693dfda0b1SToby Isaac     /* Build X edges*/
10703dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
10713dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; vy++) {
10723dfda0b1SToby Isaac         for (ex = 0; ex < numXEdges; ex++) {
10733dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdX == DM_BOUNDARY_TWIST && ex == numXEdges-1) ? (numYVertices-vy-1)*numXVertices : (vz*numYVertices+vy)*numXVertices + (ex+1)%numXVertices;
10743dfda0b1SToby Isaac           const PetscInt edge    = firstXEdge  + (vz*numYVertices+vy)*numXEdges + ex;
10753dfda0b1SToby Isaac           const PetscInt vertexL = firstVertex + (vz*numYVertices+vy)*numXVertices + ex;
10763dfda0b1SToby Isaac           const PetscInt vertexR = firstVertex + nextv;
10773dfda0b1SToby Isaac           PetscInt       cone[2];
10783dfda0b1SToby Isaac 
10793dfda0b1SToby Isaac           cone[0] = vertexL; cone[1] = vertexR;
10809566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, edge, cone));
10813dfda0b1SToby Isaac           if (dim == 2) {
10823dfda0b1SToby Isaac             if ((bdY != DM_BOUNDARY_PERIODIC) && (bdY != DM_BOUNDARY_TWIST)) {
10833dfda0b1SToby Isaac               if (vy == numYVertices-1) {
10849566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerTop));
10859566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge,    markerTop));
10869566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
10873dfda0b1SToby Isaac                 if (ex == numXEdges-1) {
10889566063dSJacob Faibussowitsch                   PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
10893dfda0b1SToby Isaac                 }
1090d8211ee3SMatthew G. Knepley               } else if (vy == 0) {
10919566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerBottom));
10929566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge,    markerBottom));
10939566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
10943dfda0b1SToby Isaac                 if (ex == numXEdges-1) {
10959566063dSJacob Faibussowitsch                   PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
10963dfda0b1SToby Isaac                 }
10973dfda0b1SToby Isaac               }
1098d8211ee3SMatthew G. Knepley             } else {
10994c67ea77SStefano Zampini               if (vy == 0 && cutLabel) {
11009566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, edge,    1));
11019566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, cone[0], 1));
1102d8211ee3SMatthew G. Knepley                 if (ex == numXEdges-1) {
11039566063dSJacob Faibussowitsch                   PetscCall(DMLabelSetValue(cutLabel, cone[1], 1));
11043dfda0b1SToby Isaac                 }
11053dfda0b1SToby Isaac               }
1106d8211ee3SMatthew G. Knepley             }
1107d8211ee3SMatthew G. Knepley           } else {
11083dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
11093dfda0b1SToby Isaac               if (vy == numYVertices-1) {
11109566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBack));
11113dfda0b1SToby Isaac               }
11123dfda0b1SToby Isaac               else if (vy == 0) {
11139566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerFront));
11143dfda0b1SToby Isaac               }
11153dfda0b1SToby Isaac             }
11163dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
11173dfda0b1SToby Isaac               if (vz == numZVertices-1) {
11189566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
11193dfda0b1SToby Isaac               }
11203dfda0b1SToby Isaac               else if (vz == 0) {
11219566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
11223dfda0b1SToby Isaac               }
11233dfda0b1SToby Isaac             }
11243dfda0b1SToby Isaac           }
11253dfda0b1SToby Isaac         }
11263dfda0b1SToby Isaac       }
11273dfda0b1SToby Isaac     }
11289566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
11299566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
11303dfda0b1SToby Isaac     /* Build coordinates */
11319566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
11329566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
11339566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
11349566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex+numVertices));
11353dfda0b1SToby Isaac     for (v = firstVertex; v < firstVertex+numVertices; ++v) {
11369566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
11379566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
11383dfda0b1SToby Isaac     }
11399566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
11409566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
11419566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
11429566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
11439566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
11449566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
11459566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates,VECSTANDARD));
11469566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
11473dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; ++vz) {
11483dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; ++vy) {
11493dfda0b1SToby Isaac         for (vx = 0; vx < numXVertices; ++vx) {
11503dfda0b1SToby Isaac           coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+0] = lower[0] + ((upper[0] - lower[0])/numXEdges)*vx;
11513dfda0b1SToby Isaac           coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+1] = lower[1] + ((upper[1] - lower[1])/numYEdges)*vy;
11523dfda0b1SToby Isaac           if (dim == 3) {
11533dfda0b1SToby Isaac             coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+2] = lower[2] + ((upper[2] - lower[2])/numZEdges)*vz;
11543dfda0b1SToby Isaac           }
11553dfda0b1SToby Isaac         }
11563dfda0b1SToby Isaac       }
11573dfda0b1SToby Isaac     }
11589566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
11599566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
11609566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
11613dfda0b1SToby Isaac   }
11623dfda0b1SToby Isaac   PetscFunctionReturn(0);
11633dfda0b1SToby Isaac }
11643dfda0b1SToby Isaac 
11659318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxMesh_Tensor_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
1166a6dfd86eSKarl Rupp {
11679318fe57SMatthew G. Knepley   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
11689318fe57SMatthew G. Knepley   PetscInt       fac[3] = {0, 0, 0}, d;
1169552f7358SJed Brown 
1170552f7358SJed Brown   PetscFunctionBegin;
11719318fe57SMatthew G. Knepley   PetscValidPointer(dm, 1);
11729318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
11739566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
11749318fe57SMatthew G. Knepley   for (d = 0; d < dim; ++d) {fac[d] = faces[d]; bdt[d] = periodicity[d];}
11759566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateCubeMesh_Internal(dm, lower, upper, fac, bdt[0], bdt[1], bdt[2]));
1176768d5fceSMatthew G. Knepley   if (periodicity[0] == DM_BOUNDARY_PERIODIC || periodicity[0] == DM_BOUNDARY_TWIST ||
1177768d5fceSMatthew G. Knepley       periodicity[1] == DM_BOUNDARY_PERIODIC || periodicity[1] == DM_BOUNDARY_TWIST ||
1178768d5fceSMatthew G. Knepley       (dim > 2 && (periodicity[2] == DM_BOUNDARY_PERIODIC || periodicity[2] == DM_BOUNDARY_TWIST))) {
1179768d5fceSMatthew G. Knepley     PetscReal L[3];
1180768d5fceSMatthew G. Knepley     PetscReal maxCell[3];
1181552f7358SJed Brown 
11829318fe57SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
11839318fe57SMatthew G. Knepley       L[d]       = upper[d] - lower[d];
11849318fe57SMatthew G. Knepley       maxCell[d] = 1.1 * (L[d] / PetscMax(1, faces[d]));
1185768d5fceSMatthew G. Knepley     }
11869566063dSJacob Faibussowitsch     PetscCall(DMSetPeriodicity(dm, PETSC_TRUE, maxCell, L, periodicity));
1187768d5fceSMatthew G. Knepley   }
11889566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
11899318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
11909318fe57SMatthew G. Knepley }
11919318fe57SMatthew G. Knepley 
11929318fe57SMatthew 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)
11939318fe57SMatthew G. Knepley {
11949318fe57SMatthew G. Knepley   PetscFunctionBegin;
11959566063dSJacob Faibussowitsch   if (dim == 1)      PetscCall(DMPlexCreateLineMesh_Internal(dm, faces[0], lower[0], upper[0], periodicity[0]));
11969566063dSJacob Faibussowitsch   else if (simplex)  PetscCall(DMPlexCreateBoxMesh_Simplex_Internal(dm, dim, faces, lower, upper, periodicity, interpolate));
11979566063dSJacob Faibussowitsch   else               PetscCall(DMPlexCreateBoxMesh_Tensor_Internal(dm, dim, faces, lower, upper, periodicity));
11989318fe57SMatthew G. Knepley   if (!interpolate && dim > 1 && !simplex) {
1199768d5fceSMatthew G. Knepley     DM udm;
1200768d5fceSMatthew G. Knepley 
12019566063dSJacob Faibussowitsch     PetscCall(DMPlexUninterpolate(dm, &udm));
12029566063dSJacob Faibussowitsch     PetscCall(DMPlexCopyCoordinates(dm, udm));
12039566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(dm, &udm));
1204768d5fceSMatthew G. Knepley   }
1205768d5fceSMatthew G. Knepley   PetscFunctionReturn(0);
1206c8c68bd8SToby Isaac }
1207c8c68bd8SToby Isaac 
1208768d5fceSMatthew G. Knepley /*@C
1209768d5fceSMatthew G. Knepley   DMPlexCreateBoxMesh - Creates a mesh on the tensor product of unit intervals (box) using simplices or tensor cells (hexahedra).
1210768d5fceSMatthew G. Knepley 
1211d083f849SBarry Smith   Collective
1212768d5fceSMatthew G. Knepley 
1213768d5fceSMatthew G. Knepley   Input Parameters:
1214768d5fceSMatthew G. Knepley + comm        - The communicator for the DM object
1215768d5fceSMatthew G. Knepley . dim         - The spatial dimension
1216768d5fceSMatthew G. Knepley . simplex     - PETSC_TRUE for simplices, PETSC_FALSE for tensor cells
1217fdbf62faSLisandro Dalcin . faces       - Number of faces per dimension, or NULL for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
1218768d5fceSMatthew G. Knepley . lower       - The lower left corner, or NULL for (0, 0, 0)
1219768d5fceSMatthew G. Knepley . upper       - The upper right corner, or NULL for (1, 1, 1)
1220fdbf62faSLisandro Dalcin . periodicity - The boundary type for the X,Y,Z direction, or NULL for DM_BOUNDARY_NONE
1221768d5fceSMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
1222768d5fceSMatthew G. Knepley 
1223768d5fceSMatthew G. Knepley   Output Parameter:
1224768d5fceSMatthew G. Knepley . dm  - The DM object
1225768d5fceSMatthew G. Knepley 
12269318fe57SMatthew G. Knepley   Note: If you want to customize this mesh using options, you just need to
12279318fe57SMatthew G. Knepley $  DMCreate(comm, &dm);
12289318fe57SMatthew G. Knepley $  DMSetType(dm, DMPLEX);
12299318fe57SMatthew G. Knepley $  DMSetFromOptions(dm);
12309318fe57SMatthew G. Knepley and use the options on the DMSetFromOptions() page.
12311367e252SJed Brown 
12321367e252SJed Brown   Here is the numbering returned for 2 faces in each direction for tensor cells:
1233768d5fceSMatthew G. Knepley $ 10---17---11---18----12
1234768d5fceSMatthew G. Knepley $  |         |         |
1235768d5fceSMatthew G. Knepley $  |         |         |
1236768d5fceSMatthew G. Knepley $ 20    2   22    3    24
1237768d5fceSMatthew G. Knepley $  |         |         |
1238768d5fceSMatthew G. Knepley $  |         |         |
1239768d5fceSMatthew G. Knepley $  7---15----8---16----9
1240768d5fceSMatthew G. Knepley $  |         |         |
1241768d5fceSMatthew G. Knepley $  |         |         |
1242768d5fceSMatthew G. Knepley $ 19    0   21    1   23
1243768d5fceSMatthew G. Knepley $  |         |         |
1244768d5fceSMatthew G. Knepley $  |         |         |
1245768d5fceSMatthew G. Knepley $  4---13----5---14----6
1246768d5fceSMatthew G. Knepley 
1247768d5fceSMatthew G. Knepley and for simplicial cells
1248768d5fceSMatthew G. Knepley 
1249768d5fceSMatthew G. Knepley $ 14----8---15----9----16
1250768d5fceSMatthew G. Knepley $  |\     5  |\      7 |
1251768d5fceSMatthew G. Knepley $  | \       | \       |
1252768d5fceSMatthew G. Knepley $ 13   2    14    3    15
1253768d5fceSMatthew G. Knepley $  | 4   \   | 6   \   |
1254768d5fceSMatthew G. Knepley $  |       \ |       \ |
1255768d5fceSMatthew G. Knepley $ 11----6---12----7----13
1256768d5fceSMatthew G. Knepley $  |\        |\        |
1257768d5fceSMatthew G. Knepley $  | \    1  | \     3 |
1258768d5fceSMatthew G. Knepley $ 10   0    11    1    12
1259768d5fceSMatthew G. Knepley $  | 0   \   | 2   \   |
1260768d5fceSMatthew G. Knepley $  |       \ |       \ |
1261768d5fceSMatthew G. Knepley $  8----4----9----5----10
1262768d5fceSMatthew G. Knepley 
1263768d5fceSMatthew G. Knepley   Level: beginner
1264768d5fceSMatthew G. Knepley 
1265db781477SPatrick Sanan .seealso: `DMSetFromOptions()`, `DMPlexCreateFromFile()`, `DMPlexCreateHexCylinderMesh()`, `DMSetType()`, `DMCreate()`
1266768d5fceSMatthew G. Knepley @*/
1267768d5fceSMatthew 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)
1268552f7358SJed Brown {
12699318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
1270fdbf62faSLisandro Dalcin   PetscReal      low[3] = {0, 0, 0};
1271fdbf62faSLisandro Dalcin   PetscReal      upp[3] = {1, 1, 1};
1272fdbf62faSLisandro Dalcin   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
1273552f7358SJed Brown 
1274768d5fceSMatthew G. Knepley   PetscFunctionBegin;
12759566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm,dm));
12769566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm,DMPLEX));
12779566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxMesh_Internal(*dm, dim, simplex, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt, interpolate));
12789318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
12799318fe57SMatthew G. Knepley }
1280fdbf62faSLisandro Dalcin 
1281d410b0cfSMatthew G. Knepley static PetscErrorCode DMPlexCreateWedgeBoxMesh_Internal(DM dm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
12829318fe57SMatthew G. Knepley {
12839318fe57SMatthew G. Knepley   DM             bdm, vol;
12849318fe57SMatthew G. Knepley   PetscInt       i;
12859318fe57SMatthew G. Knepley 
12869318fe57SMatthew G. Knepley   PetscFunctionBegin;
128708401ef6SPierre Jolivet   for (i = 0; i < 3; ++i) PetscCheck(periodicity[i] == DM_BOUNDARY_NONE,PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Periodicity not yet supported");
12889566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject) dm), &bdm));
12899566063dSJacob Faibussowitsch   PetscCall(DMSetType(bdm, DMPLEX));
12909566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(bdm, 2));
12919566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxMesh_Simplex_Internal(bdm, 2, faces, lower, upper, periodicity, PETSC_TRUE));
12929566063dSJacob Faibussowitsch   PetscCall(DMPlexExtrude(bdm, faces[2], upper[2] - lower[2], PETSC_TRUE, PETSC_FALSE, NULL, NULL, &vol));
12939566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&bdm));
12949566063dSJacob Faibussowitsch   PetscCall(DMPlexReplace_Static(dm, &vol));
12959318fe57SMatthew G. Knepley   if (lower[2] != 0.0) {
12969318fe57SMatthew G. Knepley     Vec          v;
12979318fe57SMatthew G. Knepley     PetscScalar *x;
12989318fe57SMatthew G. Knepley     PetscInt     cDim, n;
12999318fe57SMatthew G. Knepley 
13009566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &v));
13019566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(v, &cDim));
13029566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(v, &n));
13039566063dSJacob Faibussowitsch     PetscCall(VecGetArray(v, &x));
13049318fe57SMatthew G. Knepley     x   += cDim;
13059318fe57SMatthew G. Knepley     for (i = 0; i < n; i += cDim) x[i] += lower[2];
13069566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(v,&x));
13079566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, v));
13089318fe57SMatthew G. Knepley   }
1309552f7358SJed Brown   PetscFunctionReturn(0);
1310552f7358SJed Brown }
1311552f7358SJed Brown 
131200dabe28SStefano Zampini /*@
131300dabe28SStefano Zampini   DMPlexCreateWedgeBoxMesh - Creates a 3-D mesh tesselating the (x,y) plane and extruding in the third direction using wedge cells.
131400dabe28SStefano Zampini 
1315d083f849SBarry Smith   Collective
131600dabe28SStefano Zampini 
131700dabe28SStefano Zampini   Input Parameters:
131800dabe28SStefano Zampini + comm        - The communicator for the DM object
131900dabe28SStefano Zampini . faces       - Number of faces per dimension, or NULL for (1, 1, 1)
132000dabe28SStefano Zampini . lower       - The lower left corner, or NULL for (0, 0, 0)
132100dabe28SStefano Zampini . upper       - The upper right corner, or NULL for (1, 1, 1)
132200dabe28SStefano Zampini . periodicity - The boundary type for the X,Y,Z direction, or NULL for DM_BOUNDARY_NONE
1323d0fcb9c2SMatthew G. Knepley . orderHeight - If PETSC_TRUE, orders the extruded cells in the height first. Otherwise, orders the cell on the layers first
132400dabe28SStefano Zampini - interpolate - Flag to create intermediate mesh pieces (edges, faces)
132500dabe28SStefano Zampini 
132600dabe28SStefano Zampini   Output Parameter:
132700dabe28SStefano Zampini . dm  - The DM object
132800dabe28SStefano Zampini 
132900dabe28SStefano Zampini   Level: beginner
133000dabe28SStefano Zampini 
1331db781477SPatrick Sanan .seealso: `DMPlexCreateHexCylinderMesh()`, `DMPlexCreateWedgeCylinderMesh()`, `DMExtrude()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
133200dabe28SStefano Zampini @*/
1333d0fcb9c2SMatthew 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)
133400dabe28SStefano Zampini {
13359318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
133600dabe28SStefano Zampini   PetscReal      low[3] = {0, 0, 0};
133700dabe28SStefano Zampini   PetscReal      upp[3] = {1, 1, 1};
133800dabe28SStefano Zampini   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
133900dabe28SStefano Zampini 
134000dabe28SStefano Zampini   PetscFunctionBegin;
13419566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm,dm));
13429566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm,DMPLEX));
13439566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateWedgeBoxMesh_Internal(*dm, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt));
1344d410b0cfSMatthew G. Knepley   if (!interpolate) {
1345d410b0cfSMatthew G. Knepley     DM udm;
134600dabe28SStefano Zampini 
13479566063dSJacob Faibussowitsch     PetscCall(DMPlexUninterpolate(*dm, &udm));
13489566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(*dm, &udm));
134900dabe28SStefano Zampini   }
135000dabe28SStefano Zampini   PetscFunctionReturn(0);
135100dabe28SStefano Zampini }
135200dabe28SStefano Zampini 
1353a9074c1eSMatthew G. Knepley /*@C
1354a9074c1eSMatthew G. Knepley   DMPlexSetOptionsPrefix - Sets the prefix used for searching for all DM options in the database.
1355a9074c1eSMatthew G. Knepley 
1356d083f849SBarry Smith   Logically Collective on dm
1357a9074c1eSMatthew G. Knepley 
1358a9074c1eSMatthew G. Knepley   Input Parameters:
1359a9074c1eSMatthew G. Knepley + dm - the DM context
1360a9074c1eSMatthew G. Knepley - prefix - the prefix to prepend to all option names
1361a9074c1eSMatthew G. Knepley 
1362a9074c1eSMatthew G. Knepley   Notes:
1363a9074c1eSMatthew G. Knepley   A hyphen (-) must NOT be given at the beginning of the prefix name.
1364a9074c1eSMatthew G. Knepley   The first character of all runtime options is AUTOMATICALLY the hyphen.
1365a9074c1eSMatthew G. Knepley 
1366a9074c1eSMatthew G. Knepley   Level: advanced
1367a9074c1eSMatthew G. Knepley 
1368db781477SPatrick Sanan .seealso: `SNESSetFromOptions()`
1369a9074c1eSMatthew G. Knepley @*/
1370a9074c1eSMatthew G. Knepley PetscErrorCode DMPlexSetOptionsPrefix(DM dm, const char prefix[])
1371a9074c1eSMatthew G. Knepley {
1372a9074c1eSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
1373a9074c1eSMatthew G. Knepley 
1374a9074c1eSMatthew G. Knepley   PetscFunctionBegin;
1375a9074c1eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
13769566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject) dm, prefix));
13779566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject) mesh->partitioner, prefix));
1378a9074c1eSMatthew G. Knepley   PetscFunctionReturn(0);
1379a9074c1eSMatthew G. Knepley }
1380a9074c1eSMatthew G. Knepley 
13819318fe57SMatthew G. Knepley /* Remap geometry to cylinder
138261a622f3SMatthew G. Knepley    TODO: This only works for a single refinement, then it is broken
138361a622f3SMatthew G. Knepley 
13849318fe57SMatthew G. Knepley      Interior square: Linear interpolation is correct
13859318fe57SMatthew G. Knepley      The other cells all have vertices on rays from the origin. We want to uniformly expand the spacing
13869318fe57SMatthew G. Knepley      such that the last vertex is on the unit circle. So the closest and farthest vertices are at distance
13870510c589SMatthew G. Knepley 
13889318fe57SMatthew G. Knepley        phi     = arctan(y/x)
13899318fe57SMatthew G. Knepley        d_close = sqrt(1/8 + 1/4 sin^2(phi))
13909318fe57SMatthew G. Knepley        d_far   = sqrt(1/2 + sin^2(phi))
13910510c589SMatthew G. Knepley 
13929318fe57SMatthew G. Knepley      so we remap them using
13930510c589SMatthew G. Knepley 
13949318fe57SMatthew G. Knepley        x_new = x_close + (x - x_close) (1 - d_close) / (d_far - d_close)
13959318fe57SMatthew G. Knepley        y_new = y_close + (y - y_close) (1 - d_close) / (d_far - d_close)
13960510c589SMatthew G. Knepley 
13979318fe57SMatthew G. Knepley      If pi/4 < phi < 3pi/4 or -3pi/4 < phi < -pi/4, then we switch x and y.
13989318fe57SMatthew G. Knepley */
13999318fe57SMatthew G. Knepley static void snapToCylinder(PetscInt dim, PetscInt Nf, PetscInt NfAux,
14009318fe57SMatthew G. Knepley                            const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
14019318fe57SMatthew G. Knepley                            const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
14029318fe57SMatthew G. Knepley                            PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
14039318fe57SMatthew G. Knepley {
14049318fe57SMatthew G. Knepley   const PetscReal dis = 1.0/PetscSqrtReal(2.0);
14059318fe57SMatthew G. Knepley   const PetscReal ds2 = 0.5*dis;
140622cc497dSMatthew G. Knepley 
14079318fe57SMatthew G. Knepley   if ((PetscAbsScalar(u[0]) <= ds2) && (PetscAbsScalar(u[1]) <= ds2)) {
14089318fe57SMatthew G. Knepley     f0[0] = u[0];
14099318fe57SMatthew G. Knepley     f0[1] = u[1];
14109318fe57SMatthew G. Knepley   } else {
14119318fe57SMatthew G. Knepley     PetscReal phi, sinp, cosp, dc, df, x, y, xc, yc;
14120510c589SMatthew G. Knepley 
14139318fe57SMatthew G. Knepley     x    = PetscRealPart(u[0]);
14149318fe57SMatthew G. Knepley     y    = PetscRealPart(u[1]);
14159318fe57SMatthew G. Knepley     phi  = PetscAtan2Real(y, x);
14169318fe57SMatthew G. Knepley     sinp = PetscSinReal(phi);
14179318fe57SMatthew G. Knepley     cosp = PetscCosReal(phi);
14189318fe57SMatthew G. Knepley     if ((PetscAbsReal(phi) > PETSC_PI/4.0) && (PetscAbsReal(phi) < 3.0*PETSC_PI/4.0)) {
14199318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2/sinp);
14209318fe57SMatthew G. Knepley       df = PetscAbsReal(dis/sinp);
14219318fe57SMatthew G. Knepley       xc = ds2*x/PetscAbsReal(y);
14229318fe57SMatthew G. Knepley       yc = ds2*PetscSignReal(y);
14239318fe57SMatthew G. Knepley     } else {
14249318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2/cosp);
14259318fe57SMatthew G. Knepley       df = PetscAbsReal(dis/cosp);
14269318fe57SMatthew G. Knepley       xc = ds2*PetscSignReal(x);
14279318fe57SMatthew G. Knepley       yc = ds2*y/PetscAbsReal(x);
14289318fe57SMatthew G. Knepley     }
14299318fe57SMatthew G. Knepley     f0[0] = xc + (u[0] - xc)*(1.0 - dc)/(df - dc);
14309318fe57SMatthew G. Knepley     f0[1] = yc + (u[1] - yc)*(1.0 - dc)/(df - dc);
14319318fe57SMatthew G. Knepley   }
14329318fe57SMatthew G. Knepley   f0[2] = u[2];
14339318fe57SMatthew G. Knepley }
14340510c589SMatthew G. Knepley 
14359318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateHexCylinderMesh_Internal(DM dm, DMBoundaryType periodicZ)
14360510c589SMatthew G. Knepley {
14370510c589SMatthew G. Knepley   const PetscInt dim = 3;
14389318fe57SMatthew G. Knepley   PetscInt       numCells, numVertices;
1439d8c47e87SMatthew G. Knepley   PetscMPIInt    rank;
14400510c589SMatthew G. Knepley 
14410510c589SMatthew G. Knepley   PetscFunctionBegin;
14429566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank));
14439566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
14440510c589SMatthew G. Knepley   /* Create topology */
14450510c589SMatthew G. Knepley   {
14460510c589SMatthew G. Knepley     PetscInt cone[8], c;
14470510c589SMatthew G. Knepley 
1448dd400576SPatrick Sanan     numCells    = rank == 0 ?  5 : 0;
1449dd400576SPatrick Sanan     numVertices = rank == 0 ? 16 : 0;
1450006a8963SMatthew G. Knepley     if (periodicZ == DM_BOUNDARY_PERIODIC) {
1451ae8bcbbbSMatthew G. Knepley       numCells   *= 3;
1452dd400576SPatrick Sanan       numVertices = rank == 0 ? 24 : 0;
1453006a8963SMatthew G. Knepley     }
14549566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells+numVertices));
14559566063dSJacob Faibussowitsch     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 8));
14569566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm));
1457dd400576SPatrick Sanan     if (rank == 0) {
1458006a8963SMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
1459ae8bcbbbSMatthew G. Knepley         cone[0] = 15; cone[1] = 18; cone[2] = 17; cone[3] = 16;
1460ae8bcbbbSMatthew G. Knepley         cone[4] = 31; cone[5] = 32; cone[6] = 33; cone[7] = 34;
14619566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
1462ae8bcbbbSMatthew G. Knepley         cone[0] = 16; cone[1] = 17; cone[2] = 24; cone[3] = 23;
1463ae8bcbbbSMatthew G. Knepley         cone[4] = 32; cone[5] = 36; cone[6] = 37; cone[7] = 33; /* 22 25 26 21 */
14649566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
1465ae8bcbbbSMatthew G. Knepley         cone[0] = 18; cone[1] = 27; cone[2] = 24; cone[3] = 17;
1466ae8bcbbbSMatthew G. Knepley         cone[4] = 34; cone[5] = 33; cone[6] = 37; cone[7] = 38;
14679566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
1468ae8bcbbbSMatthew G. Knepley         cone[0] = 29; cone[1] = 27; cone[2] = 18; cone[3] = 15;
1469ae8bcbbbSMatthew G. Knepley         cone[4] = 35; cone[5] = 31; cone[6] = 34; cone[7] = 38;
14709566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
1471ae8bcbbbSMatthew G. Knepley         cone[0] = 29; cone[1] = 15; cone[2] = 16; cone[3] = 23;
1472ae8bcbbbSMatthew G. Knepley         cone[4] = 35; cone[5] = 36; cone[6] = 32; cone[7] = 31;
14739566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
1474006a8963SMatthew G. Knepley 
1475ae8bcbbbSMatthew G. Knepley         cone[0] = 31; cone[1] = 34; cone[2] = 33; cone[3] = 32;
1476ae8bcbbbSMatthew G. Knepley         cone[4] = 19; cone[5] = 22; cone[6] = 21; cone[7] = 20;
14779566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 5, cone));
1478ae8bcbbbSMatthew G. Knepley         cone[0] = 32; cone[1] = 33; cone[2] = 37; cone[3] = 36;
1479ae8bcbbbSMatthew G. Knepley         cone[4] = 22; cone[5] = 25; cone[6] = 26; cone[7] = 21;
14809566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 6, cone));
1481ae8bcbbbSMatthew G. Knepley         cone[0] = 34; cone[1] = 38; cone[2] = 37; cone[3] = 33;
1482ae8bcbbbSMatthew G. Knepley         cone[4] = 20; cone[5] = 21; cone[6] = 26; cone[7] = 28;
14839566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 7, cone));
1484ae8bcbbbSMatthew G. Knepley         cone[0] = 35; cone[1] = 38; cone[2] = 34; cone[3] = 31;
1485ae8bcbbbSMatthew G. Knepley         cone[4] = 30; cone[5] = 19; cone[6] = 20; cone[7] = 28;
14869566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 8, cone));
1487ae8bcbbbSMatthew G. Knepley         cone[0] = 35; cone[1] = 31; cone[2] = 32; cone[3] = 36;
1488ae8bcbbbSMatthew G. Knepley         cone[4] = 30; cone[5] = 25; cone[6] = 22; cone[7] = 19;
14899566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 9, cone));
1490ae8bcbbbSMatthew G. Knepley 
1491ae8bcbbbSMatthew G. Knepley         cone[0] = 19; cone[1] = 20; cone[2] = 21; cone[3] = 22;
1492ae8bcbbbSMatthew G. Knepley         cone[4] = 15; cone[5] = 16; cone[6] = 17; cone[7] = 18;
14939566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 10, cone));
1494ae8bcbbbSMatthew G. Knepley         cone[0] = 22; cone[1] = 21; cone[2] = 26; cone[3] = 25;
1495ae8bcbbbSMatthew G. Knepley         cone[4] = 16; cone[5] = 23; cone[6] = 24; cone[7] = 17;
14969566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 11, cone));
1497ae8bcbbbSMatthew G. Knepley         cone[0] = 20; cone[1] = 28; cone[2] = 26; cone[3] = 21;
1498ae8bcbbbSMatthew G. Knepley         cone[4] = 18; cone[5] = 17; cone[6] = 24; cone[7] = 27;
14999566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 12, cone));
1500ae8bcbbbSMatthew G. Knepley         cone[0] = 30; cone[1] = 28; cone[2] = 20; cone[3] = 19;
1501ae8bcbbbSMatthew G. Knepley         cone[4] = 29; cone[5] = 15; cone[6] = 18; cone[7] = 27;
15029566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 13, cone));
1503ae8bcbbbSMatthew G. Knepley         cone[0] = 30; cone[1] = 19; cone[2] = 22; cone[3] = 25;
1504ae8bcbbbSMatthew G. Knepley         cone[4] = 29; cone[5] = 23; cone[6] = 16; cone[7] = 15;
15059566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 14, cone));
1506006a8963SMatthew G. Knepley       } else {
150710c6f908SMatthew G. Knepley         cone[0] =  5; cone[1] =  8; cone[2] =  7; cone[3] =  6;
150810c6f908SMatthew G. Knepley         cone[4] =  9; cone[5] = 12; cone[6] = 11; cone[7] = 10;
15099566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
151010c6f908SMatthew G. Knepley         cone[0] =  6; cone[1] =  7; cone[2] = 14; cone[3] = 13;
151110c6f908SMatthew G. Knepley         cone[4] = 12; cone[5] = 15; cone[6] = 16; cone[7] = 11;
15129566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
151310c6f908SMatthew G. Knepley         cone[0] =  8; cone[1] = 17; cone[2] = 14; cone[3] =  7;
151410c6f908SMatthew G. Knepley         cone[4] = 10; cone[5] = 11; cone[6] = 16; cone[7] = 18;
15159566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
151610c6f908SMatthew G. Knepley         cone[0] = 19; cone[1] = 17; cone[2] =  8; cone[3] =  5;
151710c6f908SMatthew G. Knepley         cone[4] = 20; cone[5] =  9; cone[6] = 10; cone[7] = 18;
15189566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
151910c6f908SMatthew G. Knepley         cone[0] = 19; cone[1] =  5; cone[2] =  6; cone[3] = 13;
152010c6f908SMatthew G. Knepley         cone[4] = 20; cone[5] = 15; cone[6] = 12; cone[7] =  9;
15219566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
1522006a8963SMatthew G. Knepley       }
1523d8c47e87SMatthew G. Knepley     }
15249566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
15259566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
15260510c589SMatthew G. Knepley   }
1527dbc1dc17SMatthew G. Knepley   /* Create cube geometry */
15280510c589SMatthew G. Knepley   {
15290510c589SMatthew G. Knepley     Vec             coordinates;
15300510c589SMatthew G. Knepley     PetscSection    coordSection;
15310510c589SMatthew G. Knepley     PetscScalar    *coords;
15320510c589SMatthew G. Knepley     PetscInt        coordSize, v;
15330510c589SMatthew G. Knepley     const PetscReal dis = 1.0/PetscSqrtReal(2.0);
15340510c589SMatthew G. Knepley     const PetscReal ds2 = dis/2.0;
15350510c589SMatthew G. Knepley 
15360510c589SMatthew G. Knepley     /* Build coordinates */
15379566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
15389566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
15399566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
15409566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, numCells, numCells+numVertices));
15410510c589SMatthew G. Knepley     for (v = numCells; v < numCells+numVertices; ++v) {
15429566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
15439566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
15440510c589SMatthew G. Knepley     }
15459566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
15469566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
15479566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
15489566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
15499566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
15509566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
15519566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates,VECSTANDARD));
15529566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
1553dd400576SPatrick Sanan     if (rank == 0) {
15540510c589SMatthew G. Knepley       coords[0*dim+0] = -ds2; coords[0*dim+1] = -ds2; coords[0*dim+2] = 0.0;
15550510c589SMatthew G. Knepley       coords[1*dim+0] =  ds2; coords[1*dim+1] = -ds2; coords[1*dim+2] = 0.0;
15560510c589SMatthew G. Knepley       coords[2*dim+0] =  ds2; coords[2*dim+1] =  ds2; coords[2*dim+2] = 0.0;
15570510c589SMatthew G. Knepley       coords[3*dim+0] = -ds2; coords[3*dim+1] =  ds2; coords[3*dim+2] = 0.0;
15580510c589SMatthew G. Knepley       coords[4*dim+0] = -ds2; coords[4*dim+1] = -ds2; coords[4*dim+2] = 1.0;
15590510c589SMatthew G. Knepley       coords[5*dim+0] = -ds2; coords[5*dim+1] =  ds2; coords[5*dim+2] = 1.0;
15600510c589SMatthew G. Knepley       coords[6*dim+0] =  ds2; coords[6*dim+1] =  ds2; coords[6*dim+2] = 1.0;
15610510c589SMatthew G. Knepley       coords[7*dim+0] =  ds2; coords[7*dim+1] = -ds2; coords[7*dim+2] = 1.0;
15620510c589SMatthew G. Knepley       coords[ 8*dim+0] =  dis; coords[ 8*dim+1] = -dis; coords[ 8*dim+2] = 0.0;
15630510c589SMatthew G. Knepley       coords[ 9*dim+0] =  dis; coords[ 9*dim+1] =  dis; coords[ 9*dim+2] = 0.0;
15640510c589SMatthew G. Knepley       coords[10*dim+0] =  dis; coords[10*dim+1] = -dis; coords[10*dim+2] = 1.0;
15650510c589SMatthew G. Knepley       coords[11*dim+0] =  dis; coords[11*dim+1] =  dis; coords[11*dim+2] = 1.0;
15660510c589SMatthew G. Knepley       coords[12*dim+0] = -dis; coords[12*dim+1] =  dis; coords[12*dim+2] = 0.0;
15670510c589SMatthew G. Knepley       coords[13*dim+0] = -dis; coords[13*dim+1] =  dis; coords[13*dim+2] = 1.0;
15680510c589SMatthew G. Knepley       coords[14*dim+0] = -dis; coords[14*dim+1] = -dis; coords[14*dim+2] = 0.0;
15690510c589SMatthew G. Knepley       coords[15*dim+0] = -dis; coords[15*dim+1] = -dis; coords[15*dim+2] = 1.0;
1570ae8bcbbbSMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
1571ae8bcbbbSMatthew G. Knepley         /* 15 31 19 */ coords[16*dim+0] = -ds2; coords[16*dim+1] = -ds2; coords[16*dim+2] = 0.5;
1572ae8bcbbbSMatthew G. Knepley         /* 16 32 22 */ coords[17*dim+0] =  ds2; coords[17*dim+1] = -ds2; coords[17*dim+2] = 0.5;
1573ae8bcbbbSMatthew G. Knepley         /* 17 33 21 */ coords[18*dim+0] =  ds2; coords[18*dim+1] =  ds2; coords[18*dim+2] = 0.5;
1574ae8bcbbbSMatthew G. Knepley         /* 18 34 20 */ coords[19*dim+0] = -ds2; coords[19*dim+1] =  ds2; coords[19*dim+2] = 0.5;
1575ae8bcbbbSMatthew G. Knepley         /* 29 35 30 */ coords[20*dim+0] = -dis; coords[20*dim+1] = -dis; coords[20*dim+2] = 0.5;
1576ae8bcbbbSMatthew G. Knepley         /* 23 36 25 */ coords[21*dim+0] =  dis; coords[21*dim+1] = -dis; coords[21*dim+2] = 0.5;
1577ae8bcbbbSMatthew G. Knepley         /* 24 37 26 */ coords[22*dim+0] =  dis; coords[22*dim+1] =  dis; coords[22*dim+2] = 0.5;
1578ae8bcbbbSMatthew G. Knepley         /* 27 38 28 */ coords[23*dim+0] = -dis; coords[23*dim+1] =  dis; coords[23*dim+2] = 0.5;
1579ae8bcbbbSMatthew G. Knepley       }
1580d8c47e87SMatthew G. Knepley     }
15819566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
15829566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
15839566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
15840510c589SMatthew G. Knepley   }
1585006a8963SMatthew G. Knepley   /* Create periodicity */
1586006a8963SMatthew G. Knepley   if (periodicZ == DM_BOUNDARY_PERIODIC || periodicZ == DM_BOUNDARY_TWIST) {
1587006a8963SMatthew G. Knepley     PetscReal      L[3];
1588006a8963SMatthew G. Knepley     PetscReal      maxCell[3];
1589006a8963SMatthew G. Knepley     DMBoundaryType bdType[3];
1590006a8963SMatthew G. Knepley     PetscReal      lower[3] = {0.0, 0.0, 0.0};
1591ae8bcbbbSMatthew G. Knepley     PetscReal      upper[3] = {1.0, 1.0, 1.5};
1592ae8bcbbbSMatthew G. Knepley     PetscInt       i, numZCells = 3;
1593006a8963SMatthew G. Knepley 
1594006a8963SMatthew G. Knepley     bdType[0] = DM_BOUNDARY_NONE;
1595006a8963SMatthew G. Knepley     bdType[1] = DM_BOUNDARY_NONE;
1596006a8963SMatthew G. Knepley     bdType[2] = periodicZ;
1597006a8963SMatthew G. Knepley     for (i = 0; i < dim; i++) {
1598006a8963SMatthew G. Knepley       L[i]       = upper[i] - lower[i];
1599006a8963SMatthew G. Knepley       maxCell[i] = 1.1 * (L[i] / numZCells);
1600006a8963SMatthew G. Knepley     }
16019566063dSJacob Faibussowitsch     PetscCall(DMSetPeriodicity(dm, PETSC_TRUE, maxCell, L, bdType));
1602006a8963SMatthew G. Knepley   }
1603dbc1dc17SMatthew G. Knepley   {
16049318fe57SMatthew G. Knepley     DM          cdm;
16059318fe57SMatthew G. Knepley     PetscDS     cds;
16069318fe57SMatthew G. Knepley     PetscScalar c[2] = {1.0, 1.0};
1607dbc1dc17SMatthew G. Knepley 
16089566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCoordinateSpace(dm, 1, snapToCylinder));
16099566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
16109566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
16119566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, 2, c));
1612dbc1dc17SMatthew G. Knepley   }
16139318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
16149566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolateInPlace_Internal(dm));
16150510c589SMatthew G. Knepley   PetscFunctionReturn(0);
16160510c589SMatthew G. Knepley }
16170510c589SMatthew G. Knepley 
161824119c2aSMatthew G. Knepley /*@
16199318fe57SMatthew G. Knepley   DMPlexCreateHexCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using hexahedra.
162024119c2aSMatthew G. Knepley 
1621d083f849SBarry Smith   Collective
162224119c2aSMatthew G. Knepley 
162324119c2aSMatthew G. Knepley   Input Parameters:
162424119c2aSMatthew G. Knepley + comm      - The communicator for the DM object
16259318fe57SMatthew G. Knepley - periodicZ - The boundary type for the Z direction
162624119c2aSMatthew G. Knepley 
162724119c2aSMatthew G. Knepley   Output Parameter:
162824119c2aSMatthew G. Knepley . dm  - The DM object
162924119c2aSMatthew G. Knepley 
16309318fe57SMatthew G. Knepley   Note:
16319318fe57SMatthew G. Knepley   Here is the output numbering looking from the bottom of the cylinder:
16329318fe57SMatthew G. Knepley $       17-----14
16339318fe57SMatthew G. Knepley $        |     |
16349318fe57SMatthew G. Knepley $        |  2  |
16359318fe57SMatthew G. Knepley $        |     |
16369318fe57SMatthew G. Knepley $ 17-----8-----7-----14
16379318fe57SMatthew G. Knepley $  |     |     |     |
16389318fe57SMatthew G. Knepley $  |  3  |  0  |  1  |
16399318fe57SMatthew G. Knepley $  |     |     |     |
16409318fe57SMatthew G. Knepley $ 19-----5-----6-----13
16419318fe57SMatthew G. Knepley $        |     |
16429318fe57SMatthew G. Knepley $        |  4  |
16439318fe57SMatthew G. Knepley $        |     |
16449318fe57SMatthew G. Knepley $       19-----13
16459318fe57SMatthew G. Knepley $
16469318fe57SMatthew G. Knepley $ and up through the top
16479318fe57SMatthew G. Knepley $
16489318fe57SMatthew G. Knepley $       18-----16
16499318fe57SMatthew G. Knepley $        |     |
16509318fe57SMatthew G. Knepley $        |  2  |
16519318fe57SMatthew G. Knepley $        |     |
16529318fe57SMatthew G. Knepley $ 18----10----11-----16
16539318fe57SMatthew G. Knepley $  |     |     |     |
16549318fe57SMatthew G. Knepley $  |  3  |  0  |  1  |
16559318fe57SMatthew G. Knepley $  |     |     |     |
16569318fe57SMatthew G. Knepley $ 20-----9----12-----15
16579318fe57SMatthew G. Knepley $        |     |
16589318fe57SMatthew G. Knepley $        |  4  |
16599318fe57SMatthew G. Knepley $        |     |
16609318fe57SMatthew G. Knepley $       20-----15
16619318fe57SMatthew G. Knepley 
166224119c2aSMatthew G. Knepley   Level: beginner
166324119c2aSMatthew G. Knepley 
1664db781477SPatrick Sanan .seealso: `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
166524119c2aSMatthew G. Knepley @*/
16669318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateHexCylinderMesh(MPI_Comm comm, DMBoundaryType periodicZ, DM *dm)
16679318fe57SMatthew G. Knepley {
16689318fe57SMatthew G. Knepley   PetscFunctionBegin;
16699318fe57SMatthew G. Knepley   PetscValidPointer(dm, 3);
16709566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
16719566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
16729566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateHexCylinderMesh_Internal(*dm, periodicZ));
16739318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
16749318fe57SMatthew G. Knepley }
16759318fe57SMatthew G. Knepley 
16769318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateWedgeCylinderMesh_Internal(DM dm, PetscInt n, PetscBool interpolate)
167724119c2aSMatthew G. Knepley {
167824119c2aSMatthew G. Knepley   const PetscInt dim = 3;
1679412e9a14SMatthew G. Knepley   PetscInt       numCells, numVertices, v;
16809fe9f049SMatthew G. Knepley   PetscMPIInt    rank;
168124119c2aSMatthew G. Knepley 
168224119c2aSMatthew G. Knepley   PetscFunctionBegin;
168363a3b9bcSJacob Faibussowitsch   PetscCheck(n >= 0,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of wedges %" PetscInt_FMT " cannot be negative", n);
16849566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank));
16859566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
1686412e9a14SMatthew G. Knepley   /* Must create the celltype label here so that we do not automatically try to compute the types */
16879566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "celltype"));
168824119c2aSMatthew G. Knepley   /* Create topology */
168924119c2aSMatthew G. Knepley   {
169024119c2aSMatthew G. Knepley     PetscInt cone[6], c;
169124119c2aSMatthew G. Knepley 
1692dd400576SPatrick Sanan     numCells    = rank == 0 ?        n : 0;
1693dd400576SPatrick Sanan     numVertices = rank == 0 ?  2*(n+1) : 0;
16949566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells+numVertices));
16959566063dSJacob Faibussowitsch     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 6));
16969566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm));
169724119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
169824119c2aSMatthew G. Knepley       cone[0] =  c+n*1; cone[1] = (c+1)%n+n*1; cone[2] = 0+3*n;
169924119c2aSMatthew G. Knepley       cone[3] =  c+n*2; cone[4] = (c+1)%n+n*2; cone[5] = 1+3*n;
17009566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(dm, c, cone));
17019566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(dm, c, DM_POLYTOPE_TRI_PRISM_TENSOR));
170224119c2aSMatthew G. Knepley     }
17039566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
17049566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
170524119c2aSMatthew G. Knepley   }
1706412e9a14SMatthew G. Knepley   for (v = numCells; v < numCells+numVertices; ++v) {
17079566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCellType(dm, v, DM_POLYTOPE_POINT));
170824119c2aSMatthew G. Knepley   }
170924119c2aSMatthew G. Knepley   /* Create cylinder geometry */
171024119c2aSMatthew G. Knepley   {
171124119c2aSMatthew G. Knepley     Vec          coordinates;
171224119c2aSMatthew G. Knepley     PetscSection coordSection;
171324119c2aSMatthew G. Knepley     PetscScalar *coords;
1714412e9a14SMatthew G. Knepley     PetscInt     coordSize, c;
171524119c2aSMatthew G. Knepley 
171624119c2aSMatthew G. Knepley     /* Build coordinates */
17179566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
17189566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
17199566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
17209566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, numCells, numCells+numVertices));
172124119c2aSMatthew G. Knepley     for (v = numCells; v < numCells+numVertices; ++v) {
17229566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
17239566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
172424119c2aSMatthew G. Knepley     }
17259566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
17269566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
17279566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
17289566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
17299566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
17309566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
17319566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates,VECSTANDARD));
17329566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
173324119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
173424119c2aSMatthew 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;
173524119c2aSMatthew 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;
173624119c2aSMatthew G. Knepley     }
1737dd400576SPatrick Sanan     if (rank == 0) {
173824119c2aSMatthew 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;
173924119c2aSMatthew 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;
17409fe9f049SMatthew G. Knepley     }
17419566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
17429566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
17439566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
174424119c2aSMatthew G. Knepley   }
17459318fe57SMatthew G. Knepley   /* Interpolate */
17469566063dSJacob Faibussowitsch   if (interpolate) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
17479318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
17489318fe57SMatthew G. Knepley }
17499318fe57SMatthew G. Knepley 
17509318fe57SMatthew G. Knepley /*@
17519318fe57SMatthew G. Knepley   DMPlexCreateWedgeCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using wedges.
17529318fe57SMatthew G. Knepley 
17539318fe57SMatthew G. Knepley   Collective
17549318fe57SMatthew G. Knepley 
17559318fe57SMatthew G. Knepley   Input Parameters:
17569318fe57SMatthew G. Knepley + comm - The communicator for the DM object
17579318fe57SMatthew G. Knepley . n    - The number of wedges around the origin
17589318fe57SMatthew G. Knepley - interpolate - Create edges and faces
17599318fe57SMatthew G. Knepley 
17609318fe57SMatthew G. Knepley   Output Parameter:
17619318fe57SMatthew G. Knepley . dm  - The DM object
17629318fe57SMatthew G. Knepley 
17639318fe57SMatthew G. Knepley   Level: beginner
17649318fe57SMatthew G. Knepley 
1765db781477SPatrick Sanan .seealso: `DMPlexCreateHexCylinderMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
17669318fe57SMatthew G. Knepley @*/
17679318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateWedgeCylinderMesh(MPI_Comm comm, PetscInt n, PetscBool interpolate, DM *dm)
17689318fe57SMatthew G. Knepley {
17699318fe57SMatthew G. Knepley   PetscFunctionBegin;
17709318fe57SMatthew G. Knepley   PetscValidPointer(dm, 4);
17719566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
17729566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
17739566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateWedgeCylinderMesh_Internal(*dm, n, interpolate));
177424119c2aSMatthew G. Knepley   PetscFunctionReturn(0);
177524119c2aSMatthew G. Knepley }
177624119c2aSMatthew G. Knepley 
17779fbee547SJacob Faibussowitsch static inline PetscReal DiffNormReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
177865a81367SMatthew G. Knepley {
177965a81367SMatthew G. Knepley   PetscReal prod = 0.0;
178065a81367SMatthew G. Knepley   PetscInt  i;
178165a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += PetscSqr(x[i] - y[i]);
178265a81367SMatthew G. Knepley   return PetscSqrtReal(prod);
178365a81367SMatthew G. Knepley }
17849fbee547SJacob Faibussowitsch static inline PetscReal DotReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
178565a81367SMatthew G. Knepley {
178665a81367SMatthew G. Knepley   PetscReal prod = 0.0;
178765a81367SMatthew G. Knepley   PetscInt  i;
178865a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += x[i]*y[i];
178965a81367SMatthew G. Knepley   return prod;
179065a81367SMatthew G. Knepley }
179165a81367SMatthew G. Knepley 
179251a74b61SMatthew G. Knepley /* The first constant is the sphere radius */
179351a74b61SMatthew G. Knepley static void snapToSphere(PetscInt dim, PetscInt Nf, PetscInt NfAux,
179451a74b61SMatthew G. Knepley                          const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
179551a74b61SMatthew G. Knepley                          const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
179651a74b61SMatthew G. Knepley                          PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
179751a74b61SMatthew G. Knepley {
179851a74b61SMatthew G. Knepley   PetscReal r = PetscRealPart(constants[0]);
179951a74b61SMatthew G. Knepley   PetscReal norm2 = 0.0, fac;
180051a74b61SMatthew G. Knepley   PetscInt  n = uOff[1] - uOff[0], d;
180151a74b61SMatthew G. Knepley 
180251a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) norm2 += PetscSqr(PetscRealPart(u[d]));
180351a74b61SMatthew G. Knepley   fac = r/PetscSqrtReal(norm2);
180451a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) f0[d] = u[d]*fac;
180551a74b61SMatthew G. Knepley }
180651a74b61SMatthew G. Knepley 
18079318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateSphereMesh_Internal(DM dm, PetscInt dim, PetscBool simplex, PetscReal R)
18082829fed8SMatthew G. Knepley {
180965a81367SMatthew G. Knepley   const PetscInt  embedDim = dim+1;
181065a81367SMatthew G. Knepley   PetscSection    coordSection;
181165a81367SMatthew G. Knepley   Vec             coordinates;
181265a81367SMatthew G. Knepley   PetscScalar    *coords;
181365a81367SMatthew G. Knepley   PetscReal      *coordsIn;
181465a81367SMatthew G. Knepley   PetscInt        numCells, numEdges, numVerts, firstVertex, v, firstEdge, coordSize, d, c, e;
181565a81367SMatthew G. Knepley   PetscMPIInt     rank;
181665a81367SMatthew G. Knepley 
181765a81367SMatthew G. Knepley   PetscFunctionBegin;
18189318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveBool(dm, simplex, 3);
18199566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
18209566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, dim+1));
18219566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank));
182265a81367SMatthew G. Knepley   switch (dim) {
182365a81367SMatthew G. Knepley   case 2:
182465a81367SMatthew G. Knepley     if (simplex) {
182551a74b61SMatthew G. Knepley       const PetscReal radius    = PetscSqrtReal(1 + PETSC_PHI*PETSC_PHI)/(1.0 + PETSC_PHI);
182651a74b61SMatthew G. Knepley       const PetscReal edgeLen   = 2.0/(1.0 + PETSC_PHI) * (R/radius);
182765a81367SMatthew G. Knepley       const PetscInt  degree    = 5;
182851a74b61SMatthew G. Knepley       PetscReal       vertex[3] = {0.0, 1.0/(1.0 + PETSC_PHI), PETSC_PHI/(1.0 + PETSC_PHI)};
182965a81367SMatthew G. Knepley       PetscInt        s[3]      = {1, 1, 1};
183065a81367SMatthew G. Knepley       PetscInt        cone[3];
183165a81367SMatthew G. Knepley       PetscInt       *graph, p, i, j, k;
183265a81367SMatthew G. Knepley 
183351a74b61SMatthew G. Knepley       vertex[0] *= R/radius; vertex[1] *= R/radius; vertex[2] *= R/radius;
1834dd400576SPatrick Sanan       numCells    = rank == 0 ? 20 : 0;
1835dd400576SPatrick Sanan       numVerts    = rank == 0 ? 12 : 0;
183665a81367SMatthew G. Knepley       firstVertex = numCells;
183751a74b61SMatthew G. Knepley       /* Use icosahedron, which for a R-sphere has coordinates which are all cyclic permutations of
183865a81367SMatthew G. Knepley 
183965a81367SMatthew G. Knepley            (0, \pm 1/\phi+1, \pm \phi/\phi+1)
184065a81367SMatthew G. Knepley 
184165a81367SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
184251a74b61SMatthew G. Knepley          length is then given by 2/(1+\phi) = 2 * 0.38197 = 0.76393.
184365a81367SMatthew G. Knepley       */
184465a81367SMatthew G. Knepley       /* Construct vertices */
18459566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
1846dd400576SPatrick Sanan       if (rank == 0) {
184765a81367SMatthew G. Knepley         for (p = 0, i = 0; p < embedDim; ++p) {
184865a81367SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
184965a81367SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
185065a81367SMatthew G. Knepley               for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[(d+p)%embedDim]*vertex[(d+p)%embedDim];
185165a81367SMatthew G. Knepley               ++i;
185265a81367SMatthew G. Knepley             }
185365a81367SMatthew G. Knepley           }
185465a81367SMatthew G. Knepley         }
185545da822fSValeria Barra       }
185665a81367SMatthew G. Knepley       /* Construct graph */
18579566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * numVerts, &graph));
185865a81367SMatthew G. Knepley       for (i = 0; i < numVerts; ++i) {
185965a81367SMatthew G. Knepley         for (j = 0, k = 0; j < numVerts; ++j) {
186065a81367SMatthew G. Knepley           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i*embedDim], &coordsIn[j*embedDim]) - edgeLen) < PETSC_SMALL) {graph[i*numVerts+j] = 1; ++k;}
186165a81367SMatthew G. Knepley         }
186263a3b9bcSJacob Faibussowitsch         PetscCheck(k == degree,PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid icosahedron, vertex %" PetscInt_FMT " degree %" PetscInt_FMT " != %" PetscInt_FMT, i, k, degree);
186365a81367SMatthew G. Knepley       }
186465a81367SMatthew G. Knepley       /* Build Topology */
18659566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells+numVerts));
186665a81367SMatthew G. Knepley       for (c = 0; c < numCells; c++) {
18679566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(dm, c, embedDim));
186865a81367SMatthew G. Knepley       }
18699566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
187065a81367SMatthew G. Knepley       /* Cells */
187165a81367SMatthew G. Knepley       for (i = 0, c = 0; i < numVerts; ++i) {
187265a81367SMatthew G. Knepley         for (j = 0; j < i; ++j) {
187365a81367SMatthew G. Knepley           for (k = 0; k < j; ++k) {
187465a81367SMatthew G. Knepley             if (graph[i*numVerts+j] && graph[j*numVerts+k] && graph[k*numVerts+i]) {
187565a81367SMatthew G. Knepley               cone[0] = firstVertex+i; cone[1] = firstVertex+j; cone[2] = firstVertex+k;
187665a81367SMatthew G. Knepley               /* Check orientation */
187765a81367SMatthew G. Knepley               {
187865a81367SMatthew 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}}};
187965a81367SMatthew G. Knepley                 PetscReal normal[3];
188065a81367SMatthew G. Knepley                 PetscInt  e, f;
188165a81367SMatthew G. Knepley 
188265a81367SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) {
188365a81367SMatthew G. Knepley                   normal[d] = 0.0;
188465a81367SMatthew G. Knepley                   for (e = 0; e < embedDim; ++e) {
188565a81367SMatthew G. Knepley                     for (f = 0; f < embedDim; ++f) {
188665a81367SMatthew G. Knepley                       normal[d] += epsilon[d][e][f]*(coordsIn[j*embedDim+e] - coordsIn[i*embedDim+e])*(coordsIn[k*embedDim+f] - coordsIn[i*embedDim+f]);
188765a81367SMatthew G. Knepley                     }
188865a81367SMatthew G. Knepley                   }
188965a81367SMatthew G. Knepley                 }
189065a81367SMatthew G. Knepley                 if (DotReal(embedDim, normal, &coordsIn[i*embedDim]) < 0) {PetscInt tmp = cone[1]; cone[1] = cone[2]; cone[2] = tmp;}
189165a81367SMatthew G. Knepley               }
18929566063dSJacob Faibussowitsch               PetscCall(DMPlexSetCone(dm, c++, cone));
189365a81367SMatthew G. Knepley             }
189465a81367SMatthew G. Knepley           }
189565a81367SMatthew G. Knepley         }
189665a81367SMatthew G. Knepley       }
18979566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
18989566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
18999566063dSJacob Faibussowitsch       PetscCall(PetscFree(graph));
190065a81367SMatthew G. Knepley     } else {
19012829fed8SMatthew G. Knepley       /*
19022829fed8SMatthew G. Knepley         12-21--13
19032829fed8SMatthew G. Knepley          |     |
19042829fed8SMatthew G. Knepley         25  4  24
19052829fed8SMatthew G. Knepley          |     |
19062829fed8SMatthew G. Knepley   12-25--9-16--8-24--13
19072829fed8SMatthew G. Knepley    |     |     |     |
19082829fed8SMatthew G. Knepley   23  5 17  0 15  3  22
19092829fed8SMatthew G. Knepley    |     |     |     |
19102829fed8SMatthew G. Knepley   10-20--6-14--7-19--11
19112829fed8SMatthew G. Knepley          |     |
19122829fed8SMatthew G. Knepley         20  1  19
19132829fed8SMatthew G. Knepley          |     |
19142829fed8SMatthew G. Knepley         10-18--11
19152829fed8SMatthew G. Knepley          |     |
19162829fed8SMatthew G. Knepley         23  2  22
19172829fed8SMatthew G. Knepley          |     |
19182829fed8SMatthew G. Knepley         12-21--13
19192829fed8SMatthew G. Knepley        */
19202829fed8SMatthew G. Knepley       PetscInt cone[4], ornt[4];
19212829fed8SMatthew G. Knepley 
1922dd400576SPatrick Sanan       numCells    = rank == 0 ?  6 : 0;
1923dd400576SPatrick Sanan       numEdges    = rank == 0 ? 12 : 0;
1924dd400576SPatrick Sanan       numVerts    = rank == 0 ?  8 : 0;
192565a81367SMatthew G. Knepley       firstVertex = numCells;
192665a81367SMatthew G. Knepley       firstEdge   = numCells + numVerts;
19272829fed8SMatthew G. Knepley       /* Build Topology */
19289566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells+numEdges+numVerts));
19292829fed8SMatthew G. Knepley       for (c = 0; c < numCells; c++) {
19309566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(dm, c, 4));
19312829fed8SMatthew G. Knepley       }
19322829fed8SMatthew G. Knepley       for (e = firstEdge; e < firstEdge+numEdges; ++e) {
19339566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(dm, e, 2));
19342829fed8SMatthew G. Knepley       }
19359566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
1936dd400576SPatrick Sanan       if (rank == 0) {
19372829fed8SMatthew G. Knepley         /* Cell 0 */
19382829fed8SMatthew G. Knepley         cone[0] = 14; cone[1] = 15; cone[2] = 16; cone[3] = 17;
19399566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
19402829fed8SMatthew G. Knepley         ornt[0] = 0; ornt[1] = 0; ornt[2] = 0; ornt[3] = 0;
19419566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 0, ornt));
19422829fed8SMatthew G. Knepley         /* Cell 1 */
19432829fed8SMatthew G. Knepley         cone[0] = 18; cone[1] = 19; cone[2] = 14; cone[3] = 20;
19449566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
1945b5a892a1SMatthew G. Knepley         ornt[0] = 0; ornt[1] = 0; ornt[2] = -1; ornt[3] = 0;
19469566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 1, ornt));
19472829fed8SMatthew G. Knepley         /* Cell 2 */
19482829fed8SMatthew G. Knepley         cone[0] = 21; cone[1] = 22; cone[2] = 18; cone[3] = 23;
19499566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
1950b5a892a1SMatthew G. Knepley         ornt[0] = 0; ornt[1] = 0; ornt[2] = -1; ornt[3] = 0;
19519566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 2, ornt));
19522829fed8SMatthew G. Knepley         /* Cell 3 */
19532829fed8SMatthew G. Knepley         cone[0] = 19; cone[1] = 22; cone[2] = 24; cone[3] = 15;
19549566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
1955b5a892a1SMatthew G. Knepley         ornt[0] = -1; ornt[1] = -1; ornt[2] = 0; ornt[3] = -1;
19569566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 3, ornt));
19572829fed8SMatthew G. Knepley         /* Cell 4 */
19582829fed8SMatthew G. Knepley         cone[0] = 16; cone[1] = 24; cone[2] = 21; cone[3] = 25;
19599566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
1960b5a892a1SMatthew G. Knepley         ornt[0] = -1; ornt[1] = -1; ornt[2] = -1; ornt[3] = 0;
19619566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 4, ornt));
19622829fed8SMatthew G. Knepley         /* Cell 5 */
19632829fed8SMatthew G. Knepley         cone[0] = 20; cone[1] = 17; cone[2] = 25; cone[3] = 23;
19649566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 5, cone));
1965b5a892a1SMatthew G. Knepley         ornt[0] = -1; ornt[1] = -1; ornt[2] = -1; ornt[3] = -1;
19669566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 5, ornt));
19672829fed8SMatthew G. Knepley         /* Edges */
19682829fed8SMatthew G. Knepley         cone[0] =  6; cone[1] =  7;
19699566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 14, cone));
19702829fed8SMatthew G. Knepley         cone[0] =  7; cone[1] =  8;
19719566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 15, cone));
19722829fed8SMatthew G. Knepley         cone[0] =  8; cone[1] =  9;
19739566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 16, cone));
19742829fed8SMatthew G. Knepley         cone[0] =  9; cone[1] =  6;
19759566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 17, cone));
19762829fed8SMatthew G. Knepley         cone[0] = 10; cone[1] = 11;
19779566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 18, cone));
19782829fed8SMatthew G. Knepley         cone[0] = 11; cone[1] =  7;
19799566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 19, cone));
19802829fed8SMatthew G. Knepley         cone[0] =  6; cone[1] = 10;
19819566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 20, cone));
19822829fed8SMatthew G. Knepley         cone[0] = 12; cone[1] = 13;
19839566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 21, cone));
19842829fed8SMatthew G. Knepley         cone[0] = 13; cone[1] = 11;
19859566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 22, cone));
19862829fed8SMatthew G. Knepley         cone[0] = 10; cone[1] = 12;
19879566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 23, cone));
19882829fed8SMatthew G. Knepley         cone[0] = 13; cone[1] =  8;
19899566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 24, cone));
19902829fed8SMatthew G. Knepley         cone[0] = 12; cone[1] =  9;
19919566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 25, cone));
199245da822fSValeria Barra       }
19939566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
19949566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
19952829fed8SMatthew G. Knepley       /* Build coordinates */
19969566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
1997dd400576SPatrick Sanan       if (rank == 0) {
199851a74b61SMatthew G. Knepley         coordsIn[0*embedDim+0] = -R; coordsIn[0*embedDim+1] =  R; coordsIn[0*embedDim+2] = -R;
199951a74b61SMatthew G. Knepley         coordsIn[1*embedDim+0] =  R; coordsIn[1*embedDim+1] =  R; coordsIn[1*embedDim+2] = -R;
200051a74b61SMatthew G. Knepley         coordsIn[2*embedDim+0] =  R; coordsIn[2*embedDim+1] = -R; coordsIn[2*embedDim+2] = -R;
200151a74b61SMatthew G. Knepley         coordsIn[3*embedDim+0] = -R; coordsIn[3*embedDim+1] = -R; coordsIn[3*embedDim+2] = -R;
200251a74b61SMatthew G. Knepley         coordsIn[4*embedDim+0] = -R; coordsIn[4*embedDim+1] =  R; coordsIn[4*embedDim+2] =  R;
200351a74b61SMatthew G. Knepley         coordsIn[5*embedDim+0] =  R; coordsIn[5*embedDim+1] =  R; coordsIn[5*embedDim+2] =  R;
200451a74b61SMatthew G. Knepley         coordsIn[6*embedDim+0] = -R; coordsIn[6*embedDim+1] = -R; coordsIn[6*embedDim+2] =  R;
200551a74b61SMatthew G. Knepley         coordsIn[7*embedDim+0] =  R; coordsIn[7*embedDim+1] = -R; coordsIn[7*embedDim+2] =  R;
200665a81367SMatthew G. Knepley       }
200745da822fSValeria Barra     }
200865a81367SMatthew G. Knepley     break;
200965a81367SMatthew G. Knepley   case 3:
2010116ded15SMatthew G. Knepley     if (simplex) {
2011116ded15SMatthew G. Knepley       const PetscReal edgeLen         = 1.0/PETSC_PHI;
201251a74b61SMatthew G. Knepley       PetscReal       vertexA[4]      = {0.5, 0.5, 0.5, 0.5};
201351a74b61SMatthew G. Knepley       PetscReal       vertexB[4]      = {1.0, 0.0, 0.0, 0.0};
201451a74b61SMatthew G. Knepley       PetscReal       vertexC[4]      = {0.5, 0.5*PETSC_PHI, 0.5/PETSC_PHI, 0.0};
2015116ded15SMatthew G. Knepley       const PetscInt  degree          = 12;
2016116ded15SMatthew G. Knepley       PetscInt        s[4]            = {1, 1, 1};
2017116ded15SMatthew 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},
2018116ded15SMatthew 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}};
2019116ded15SMatthew G. Knepley       PetscInt        cone[4];
2020116ded15SMatthew G. Knepley       PetscInt       *graph, p, i, j, k, l;
2021116ded15SMatthew G. Knepley 
202251a74b61SMatthew G. Knepley       vertexA[0] *= R; vertexA[1] *= R; vertexA[2] *= R; vertexA[3] *= R;
202351a74b61SMatthew G. Knepley       vertexB[0] *= R; vertexB[1] *= R; vertexB[2] *= R; vertexB[3] *= R;
202451a74b61SMatthew G. Knepley       vertexC[0] *= R; vertexC[1] *= R; vertexC[2] *= R; vertexC[3] *= R;
2025dd400576SPatrick Sanan       numCells    = rank == 0 ? 600 : 0;
2026dd400576SPatrick Sanan       numVerts    = rank == 0 ? 120 : 0;
2027116ded15SMatthew G. Knepley       firstVertex = numCells;
2028116ded15SMatthew G. Knepley       /* Use the 600-cell, which for a unit sphere has coordinates which are
2029116ded15SMatthew G. Knepley 
2030116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm 1,    \pm 1, \pm 1)                          16
2031116ded15SMatthew G. Knepley                (\pm 1,    0,       0,      0)  all cyclic permutations   8
2032116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm phi, \pm 1/phi, 0)  all even permutations    96
2033116ded15SMatthew G. Knepley 
2034116ded15SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
20356333ae4fSvaleriabarra          length is then given by 1/\phi = 0.61803.
2036116ded15SMatthew G. Knepley 
2037116ded15SMatthew G. Knepley          http://buzzard.pugetsound.edu/sage-practice/ch03s03.html
2038116ded15SMatthew G. Knepley          http://mathworld.wolfram.com/600-Cell.html
2039116ded15SMatthew G. Knepley       */
2040116ded15SMatthew G. Knepley       /* Construct vertices */
20419566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
2042116ded15SMatthew G. Knepley       i    = 0;
2043dd400576SPatrick Sanan       if (rank == 0) {
2044116ded15SMatthew G. Knepley         for (s[0] = -1; s[0] < 2; s[0] += 2) {
2045116ded15SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
2046116ded15SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
2047116ded15SMatthew G. Knepley               for (s[3] = -1; s[3] < 2; s[3] += 2) {
2048116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[d]*vertexA[d];
2049116ded15SMatthew G. Knepley                 ++i;
2050116ded15SMatthew G. Knepley               }
2051116ded15SMatthew G. Knepley             }
2052116ded15SMatthew G. Knepley           }
2053116ded15SMatthew G. Knepley         }
2054116ded15SMatthew G. Knepley         for (p = 0; p < embedDim; ++p) {
2055116ded15SMatthew G. Knepley           s[1] = s[2] = s[3] = 1;
2056116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
2057116ded15SMatthew G. Knepley             for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[(d+p)%embedDim]*vertexB[(d+p)%embedDim];
2058116ded15SMatthew G. Knepley             ++i;
2059116ded15SMatthew G. Knepley           }
2060116ded15SMatthew G. Knepley         }
2061116ded15SMatthew G. Knepley         for (p = 0; p < 12; ++p) {
2062116ded15SMatthew G. Knepley           s[3] = 1;
2063116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
2064116ded15SMatthew G. Knepley             for (s[1] = -1; s[1] < 2; s[1] += 2) {
2065116ded15SMatthew G. Knepley               for (s[2] = -1; s[2] < 2; s[2] += 2) {
2066116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[evenPerm[p][d]]*vertexC[evenPerm[p][d]];
2067116ded15SMatthew G. Knepley                 ++i;
2068116ded15SMatthew G. Knepley               }
2069116ded15SMatthew G. Knepley             }
2070116ded15SMatthew G. Knepley           }
2071116ded15SMatthew G. Knepley         }
207245da822fSValeria Barra       }
207363a3b9bcSJacob Faibussowitsch       PetscCheck(i == numVerts,PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertices %" PetscInt_FMT " != %" PetscInt_FMT, i, numVerts);
2074116ded15SMatthew G. Knepley       /* Construct graph */
20759566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * numVerts, &graph));
2076116ded15SMatthew G. Knepley       for (i = 0; i < numVerts; ++i) {
2077116ded15SMatthew G. Knepley         for (j = 0, k = 0; j < numVerts; ++j) {
2078116ded15SMatthew G. Knepley           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i*embedDim], &coordsIn[j*embedDim]) - edgeLen) < PETSC_SMALL) {graph[i*numVerts+j] = 1; ++k;}
2079116ded15SMatthew G. Knepley         }
208063a3b9bcSJacob Faibussowitsch         PetscCheck(k == degree,PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertex %" PetscInt_FMT " degree %" PetscInt_FMT " != %" PetscInt_FMT, i, k, degree);
2081116ded15SMatthew G. Knepley       }
2082116ded15SMatthew G. Knepley       /* Build Topology */
20839566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells+numVerts));
2084116ded15SMatthew G. Knepley       for (c = 0; c < numCells; c++) {
20859566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(dm, c, embedDim));
2086116ded15SMatthew G. Knepley       }
20879566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
2088116ded15SMatthew G. Knepley       /* Cells */
2089dd400576SPatrick Sanan       if (rank == 0) {
2090116ded15SMatthew G. Knepley         for (i = 0, c = 0; i < numVerts; ++i) {
2091116ded15SMatthew G. Knepley           for (j = 0; j < i; ++j) {
2092116ded15SMatthew G. Knepley             for (k = 0; k < j; ++k) {
2093116ded15SMatthew G. Knepley               for (l = 0; l < k; ++l) {
2094116ded15SMatthew G. Knepley                 if (graph[i*numVerts+j] && graph[j*numVerts+k] && graph[k*numVerts+i] &&
2095116ded15SMatthew G. Knepley                     graph[l*numVerts+i] && graph[l*numVerts+j] && graph[l*numVerts+k]) {
2096116ded15SMatthew G. Knepley                   cone[0] = firstVertex+i; cone[1] = firstVertex+j; cone[2] = firstVertex+k; cone[3] = firstVertex+l;
2097116ded15SMatthew G. Knepley                   /* Check orientation: https://ef.gy/linear-algebra:normal-vectors-in-higher-dimensional-spaces */
2098116ded15SMatthew G. Knepley                   {
2099116ded15SMatthew 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}},
2100116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  1}, { 0,  0, -1, 0}},
2101116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  0, -1}, { 0,  0, 0,  0}, { 0,  1,  0, 0}},
2102116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  1,  0}, { 0, -1, 0,  0}, { 0,  0,  0, 0}}},
2103116ded15SMatthew G. Knepley 
2104116ded15SMatthew G. Knepley                                                           {{{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0, -1}, { 0,  0,  1, 0}},
2105116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
2106116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  1}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, {-1,  0,  0, 0}},
2107116ded15SMatthew G. Knepley                                                            {{0,  0, -1,  0}, { 0, 0,  0,  0}, { 1,  0, 0,  0}, { 0,  0,  0, 0}}},
2108116ded15SMatthew G. Knepley 
2109116ded15SMatthew G. Knepley                                                           {{{0,  0,  0,  0}, { 0, 0,  0,  1}, { 0,  0, 0,  0}, { 0, -1,  0, 0}},
2110116ded15SMatthew G. Knepley                                                            {{0,  0,  0, -1}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 1,  0,  0, 0}},
2111116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
2112116ded15SMatthew G. Knepley                                                            {{0,  1,  0,  0}, {-1, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}}},
2113116ded15SMatthew G. Knepley 
2114116ded15SMatthew G. Knepley                                                           {{{0,  0,  0,  0}, { 0, 0, -1,  0}, { 0,  1, 0,  0}, { 0,  0,  0, 0}},
2115116ded15SMatthew G. Knepley                                                            {{0,  0,  1,  0}, { 0, 0,  0,  0}, {-1,  0, 0,  0}, { 0,  0,  0, 0}},
2116116ded15SMatthew G. Knepley                                                            {{0, -1,  0,  0}, { 1, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
2117116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}}}};
2118116ded15SMatthew G. Knepley                     PetscReal normal[4];
2119116ded15SMatthew G. Knepley                     PetscInt  e, f, g;
2120116ded15SMatthew G. Knepley 
2121116ded15SMatthew G. Knepley                     for (d = 0; d < embedDim; ++d) {
2122116ded15SMatthew G. Knepley                       normal[d] = 0.0;
2123116ded15SMatthew G. Knepley                       for (e = 0; e < embedDim; ++e) {
2124116ded15SMatthew G. Knepley                         for (f = 0; f < embedDim; ++f) {
2125116ded15SMatthew G. Knepley                           for (g = 0; g < embedDim; ++g) {
2126116ded15SMatthew 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]);
2127116ded15SMatthew G. Knepley                           }
2128116ded15SMatthew G. Knepley                         }
2129116ded15SMatthew G. Knepley                       }
2130116ded15SMatthew G. Knepley                     }
2131116ded15SMatthew G. Knepley                     if (DotReal(embedDim, normal, &coordsIn[i*embedDim]) < 0) {PetscInt tmp = cone[1]; cone[1] = cone[2]; cone[2] = tmp;}
2132116ded15SMatthew G. Knepley                   }
21339566063dSJacob Faibussowitsch                   PetscCall(DMPlexSetCone(dm, c++, cone));
2134116ded15SMatthew G. Knepley                 }
2135116ded15SMatthew G. Knepley               }
2136116ded15SMatthew G. Knepley             }
2137116ded15SMatthew G. Knepley           }
2138116ded15SMatthew G. Knepley         }
213945da822fSValeria Barra       }
21409566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
21419566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
21429566063dSJacob Faibussowitsch       PetscCall(PetscFree(graph));
2143116ded15SMatthew G. Knepley       break;
2144116ded15SMatthew G. Knepley     }
214563a3b9bcSJacob Faibussowitsch   default: SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Unsupported dimension for sphere: %" PetscInt_FMT, dim);
214665a81367SMatthew G. Knepley   }
214765a81367SMatthew G. Knepley   /* Create coordinates */
21489566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
21499566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
21509566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, embedDim));
21519566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex+numVerts));
21522829fed8SMatthew G. Knepley   for (v = firstVertex; v < firstVertex+numVerts; ++v) {
21539566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, embedDim));
21549566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, embedDim));
21552829fed8SMatthew G. Knepley   }
21569566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
21579566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
21589566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
21599566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, embedDim));
21609566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
21619566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
21629566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates,VECSTANDARD));
21639566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
216465a81367SMatthew G. Knepley   for (v = 0; v < numVerts; ++v) for (d = 0; d < embedDim; ++d) {coords[v*embedDim+d] = coordsIn[v*embedDim+d];}
21659566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
21669566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
21679566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
21689566063dSJacob Faibussowitsch   PetscCall(PetscFree(coordsIn));
216951a74b61SMatthew G. Knepley   {
217051a74b61SMatthew G. Knepley     DM          cdm;
217151a74b61SMatthew G. Knepley     PetscDS     cds;
21729318fe57SMatthew G. Knepley     PetscScalar c = R;
217351a74b61SMatthew G. Knepley 
21749566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCoordinateSpace(dm, 1, snapToSphere));
21759566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
21769566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
21779566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, 1, &c));
217851a74b61SMatthew G. Knepley   }
21799318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
21809566063dSJacob Faibussowitsch   if (simplex) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
21819318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
21829318fe57SMatthew G. Knepley }
21839318fe57SMatthew G. Knepley 
2184b7f5c055SJed Brown typedef void (*TPSEvaluateFunc)(const PetscReal[], PetscReal*, PetscReal[], PetscReal(*)[3]);
2185b7f5c055SJed Brown 
2186b7f5c055SJed Brown /*
2187b7f5c055SJed Brown  The Schwarz P implicit surface is
2188b7f5c055SJed Brown 
2189b7f5c055SJed Brown      f(x) = cos(x0) + cos(x1) + cos(x2) = 0
2190b7f5c055SJed Brown */
2191b7f5c055SJed Brown static void TPSEvaluate_SchwarzP(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3])
2192b7f5c055SJed Brown {
2193b7f5c055SJed Brown   PetscReal c[3] = {PetscCosReal(y[0] * PETSC_PI), PetscCosReal(y[1] * PETSC_PI), PetscCosReal(y[2] * PETSC_PI)};
2194b7f5c055SJed Brown   PetscReal g[3] = {-PetscSinReal(y[0] * PETSC_PI), -PetscSinReal(y[1] * PETSC_PI), -PetscSinReal(y[2] * PETSC_PI)};
2195b7f5c055SJed Brown   f[0] = c[0] + c[1] + c[2];
2196b7f5c055SJed Brown   for (PetscInt i=0; i<3; i++) {
2197b7f5c055SJed Brown     grad[i] = PETSC_PI * g[i];
2198b7f5c055SJed Brown     for (PetscInt j=0; j<3; j++) {
2199b7f5c055SJed Brown       hess[i][j] = (i == j) ? -PetscSqr(PETSC_PI) * c[i] : 0.;
2200b7f5c055SJed Brown     }
2201b7f5c055SJed Brown   }
2202b7f5c055SJed Brown }
2203b7f5c055SJed Brown 
22044663dae6SJed Brown // u[] is a tentative normal on input. Replace with the implicit function gradient in the same direction
22054663dae6SJed Brown static PetscErrorCode TPSExtrudeNormalFunc_SchwarzP(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt r, PetscScalar u[], void *ctx) {
22064663dae6SJed Brown   for (PetscInt i=0; i<3; i++) {
22074663dae6SJed Brown     u[i] = -PETSC_PI * PetscSinReal(x[i] * PETSC_PI);
22084663dae6SJed Brown   }
22094663dae6SJed Brown   return 0;
22104663dae6SJed Brown }
22114663dae6SJed Brown 
2212b7f5c055SJed Brown /*
2213b7f5c055SJed Brown  The Gyroid implicit surface is
2214b7f5c055SJed Brown 
2215b7f5c055SJed 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)
2216b7f5c055SJed Brown 
2217b7f5c055SJed Brown */
2218b7f5c055SJed Brown static void TPSEvaluate_Gyroid(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3])
2219b7f5c055SJed Brown {
2220b7f5c055SJed Brown   PetscReal s[3] = {PetscSinReal(PETSC_PI * y[0]), PetscSinReal(PETSC_PI * (y[1] + .5)), PetscSinReal(PETSC_PI * (y[2] + .25))};
2221b7f5c055SJed Brown   PetscReal c[3] = {PetscCosReal(PETSC_PI * y[0]), PetscCosReal(PETSC_PI * (y[1] + .5)), PetscCosReal(PETSC_PI * (y[2] + .25))};
2222b7f5c055SJed Brown   f[0] = s[0] * c[1] + s[1] * c[2] + s[2] * c[0];
2223b7f5c055SJed Brown   grad[0] = PETSC_PI * (c[0] * c[1] - s[2] * s[0]);
2224b7f5c055SJed Brown   grad[1] = PETSC_PI * (c[1] * c[2] - s[0] * s[1]);
2225b7f5c055SJed Brown   grad[2] = PETSC_PI * (c[2] * c[0] - s[1] * s[2]);
2226b7f5c055SJed Brown   hess[0][0] = -PetscSqr(PETSC_PI) * (s[0] * c[1] + s[2] * c[0]);
2227b7f5c055SJed Brown   hess[0][1] = -PetscSqr(PETSC_PI) * (c[0] * s[1]);
2228b7f5c055SJed Brown   hess[0][2] = -PetscSqr(PETSC_PI) * (c[2] * s[0]);
2229b7f5c055SJed Brown   hess[1][0] = -PetscSqr(PETSC_PI) * (s[1] * c[2] + s[0] * c[1]);
2230b7f5c055SJed Brown   hess[1][1] = -PetscSqr(PETSC_PI) * (c[1] * s[2]);
2231b7f5c055SJed Brown   hess[2][2] = -PetscSqr(PETSC_PI) * (c[0] * s[1]);
2232b7f5c055SJed Brown   hess[2][0] = -PetscSqr(PETSC_PI) * (s[2] * c[0] + s[1] * c[2]);
2233b7f5c055SJed Brown   hess[2][1] = -PetscSqr(PETSC_PI) * (c[2] * s[0]);
2234b7f5c055SJed Brown   hess[2][2] = -PetscSqr(PETSC_PI) * (c[1] * s[2]);
2235b7f5c055SJed Brown }
2236b7f5c055SJed Brown 
22374663dae6SJed Brown // u[] is a tentative normal on input. Replace with the implicit function gradient in the same direction
22384663dae6SJed Brown static PetscErrorCode TPSExtrudeNormalFunc_Gyroid(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt r, PetscScalar u[], void *ctx) {
22394663dae6SJed Brown   PetscReal s[3] = {PetscSinReal(PETSC_PI * x[0]), PetscSinReal(PETSC_PI * (x[1] + .5)), PetscSinReal(PETSC_PI * (x[2] + .25))};
22404663dae6SJed Brown   PetscReal c[3] = {PetscCosReal(PETSC_PI * x[0]), PetscCosReal(PETSC_PI * (x[1] + .5)), PetscCosReal(PETSC_PI * (x[2] + .25))};
22414663dae6SJed Brown   u[0] = PETSC_PI * (c[0] * c[1] - s[2] * s[0]);
22424663dae6SJed Brown   u[1] = PETSC_PI * (c[1] * c[2] - s[0] * s[1]);
22434663dae6SJed Brown   u[2] = PETSC_PI * (c[2] * c[0] - s[1] * s[2]);
22444663dae6SJed Brown   return 0;
22454663dae6SJed Brown }
22464663dae6SJed Brown 
2247b7f5c055SJed Brown /*
2248b7f5c055SJed Brown    We wish to solve
2249b7f5c055SJed Brown 
2250b7f5c055SJed Brown          min_y || y - x ||^2  subject to f(y) = 0
2251b7f5c055SJed Brown 
2252b7f5c055SJed Brown    Let g(y) = grad(f).  The minimization problem is equivalent to asking to satisfy
2253b7f5c055SJed 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
2254b7f5c055SJed Brown    tangent space and ask for both components in the tangent space to be zero.
2255b7f5c055SJed Brown 
2256b7f5c055SJed Brown    Take g to be a column vector and compute the "full QR" factorization Q R = g,
2257b7f5c055SJed Brown    where Q = I - 2 n n^T is a symmetric orthogonal matrix.
2258b7f5c055SJed Brown    The first column of Q is parallel to g so the remaining two columns span the null space.
2259b7f5c055SJed Brown    Let Qn = Q[:,1:] be those remaining columns.  Then Qn Qn^T is an orthogonal projector into the tangent space.
2260b7f5c055SJed Brown    Since Q is symmetric, this is equivalent to multipyling by Q and taking the last two entries.
2261b7f5c055SJed Brown    In total, we have a system of 3 equations in 3 unknowns:
2262b7f5c055SJed Brown 
2263b7f5c055SJed Brown      f(y) = 0                       1 equation
2264b7f5c055SJed Brown      Qn^T (y - x) = 0               2 equations
2265b7f5c055SJed Brown 
2266b7f5c055SJed Brown    Here, we compute the residual and Jacobian of this system.
2267b7f5c055SJed Brown */
2268b7f5c055SJed Brown static void TPSNearestPointResJac(TPSEvaluateFunc feval, const PetscScalar x[], const PetscScalar y[], PetscScalar res[], PetscScalar J[])
2269b7f5c055SJed Brown {
2270b7f5c055SJed Brown   PetscReal yreal[3] = {PetscRealPart(y[0]), PetscRealPart(y[1]), PetscRealPart(y[2])};
2271b7f5c055SJed Brown   PetscReal d[3] = {PetscRealPart(y[0] - x[0]), PetscRealPart(y[1] - x[1]), PetscRealPart(y[2] - x[2])};
22722f0490c0SSatish Balay   PetscReal f, grad[3], n[3], norm, norm_y[3], nd, nd_y[3], sign;
22732f0490c0SSatish Balay   PetscReal n_y[3][3] = {{0,0,0},{0,0,0},{0,0,0}};
2274b7f5c055SJed Brown 
2275b7f5c055SJed Brown   feval(yreal, &f, grad, n_y);
2276b7f5c055SJed Brown 
2277b7f5c055SJed Brown   for (PetscInt i=0; i<3; i++) n[i] = grad[i];
2278b7f5c055SJed Brown   norm = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2]));
2279b7f5c055SJed Brown   for (PetscInt i=0; i<3; i++) {
2280b7f5c055SJed Brown     norm_y[i] = 1. / norm * n[i] * n_y[i][i];
2281b7f5c055SJed Brown   }
2282b7f5c055SJed Brown 
2283b7f5c055SJed Brown   // Define the Householder reflector
2284b7f5c055SJed Brown   sign = n[0] >= 0 ? 1. : -1.;
2285b7f5c055SJed Brown   n[0] += norm * sign;
2286b7f5c055SJed Brown   for (PetscInt i=0; i<3; i++) n_y[0][i] += norm_y[i] * sign;
2287b7f5c055SJed Brown 
2288b7f5c055SJed Brown   norm = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2]));
2289b7f5c055SJed Brown   norm_y[0] = 1. / norm * (n[0] * n_y[0][0]);
2290b7f5c055SJed Brown   norm_y[1] = 1. / norm * (n[0] * n_y[0][1] + n[1] * n_y[1][1]);
2291b7f5c055SJed Brown   norm_y[2] = 1. / norm * (n[0] * n_y[0][2] + n[2] * n_y[2][2]);
2292b7f5c055SJed Brown 
2293b7f5c055SJed Brown   for (PetscInt i=0; i<3; i++) {
2294b7f5c055SJed Brown     n[i] /= norm;
2295b7f5c055SJed Brown     for (PetscInt j=0; j<3; j++) {
2296b7f5c055SJed Brown       // note that n[i] is n_old[i]/norm when executing the code below
2297b7f5c055SJed Brown       n_y[i][j] = n_y[i][j] / norm - n[i] / norm * norm_y[j];
2298b7f5c055SJed Brown     }
2299b7f5c055SJed Brown   }
2300b7f5c055SJed Brown 
2301b7f5c055SJed Brown   nd = n[0] * d[0] + n[1] * d[1] + n[2] * d[2];
2302b7f5c055SJed 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];
2303b7f5c055SJed Brown 
2304b7f5c055SJed Brown   res[0] = f;
2305b7f5c055SJed Brown   res[1] = d[1] - 2 * n[1] * nd;
2306b7f5c055SJed Brown   res[2] = d[2] - 2 * n[2] * nd;
2307b7f5c055SJed Brown   // J[j][i] is J_{ij} (column major)
2308b7f5c055SJed Brown   for (PetscInt j=0; j<3; j++) {
2309b7f5c055SJed Brown     J[0 + j*3] = grad[j];
2310b7f5c055SJed Brown     J[1 + j*3] = (j == 1)*1. - 2 * (n_y[1][j] * nd + n[1] * nd_y[j]);
2311b7f5c055SJed Brown     J[2 + j*3] = (j == 2)*1. - 2 * (n_y[2][j] * nd + n[2] * nd_y[j]);
2312b7f5c055SJed Brown   }
2313b7f5c055SJed Brown }
2314b7f5c055SJed Brown 
2315b7f5c055SJed Brown /*
2316b7f5c055SJed Brown    Project x to the nearest point on the implicit surface using Newton's method.
2317b7f5c055SJed Brown */
2318b7f5c055SJed Brown static PetscErrorCode TPSNearestPoint(TPSEvaluateFunc feval, PetscScalar x[])
2319b7f5c055SJed Brown {
2320b7f5c055SJed Brown   PetscScalar y[3] = {x[0], x[1], x[2]}; // Initial guess
2321b7f5c055SJed Brown 
2322b7f5c055SJed Brown   PetscFunctionBegin;
2323b7f5c055SJed Brown   for (PetscInt iter=0; iter<10; iter++) {
2324b7f5c055SJed Brown     PetscScalar res[3], J[9];
2325b7f5c055SJed Brown     PetscReal resnorm;
2326b7f5c055SJed Brown     TPSNearestPointResJac(feval, x, y, res, J);
2327b7f5c055SJed Brown     resnorm = PetscSqrtReal(PetscSqr(PetscRealPart(res[0])) + PetscSqr(PetscRealPart(res[1])) + PetscSqr(PetscRealPart(res[2])));
2328b7f5c055SJed Brown     if (0) { // Turn on this monitor if you need to confirm quadratic convergence
232963a3b9bcSJacob Faibussowitsch       PetscCall(PetscPrintf(PETSC_COMM_SELF, "[%" PetscInt_FMT "] res [%g %g %g]\n", iter, (double)PetscRealPart(res[0]), (double)PetscRealPart(res[1]), (double)PetscRealPart(res[2])));
2330b7f5c055SJed Brown     }
2331b7f5c055SJed Brown     if (resnorm < PETSC_SMALL) break;
2332b7f5c055SJed Brown 
2333b7f5c055SJed Brown     // Take the Newton step
23349566063dSJacob Faibussowitsch     PetscCall(PetscKernel_A_gets_inverse_A_3(J, 0., PETSC_FALSE, NULL));
2335b7f5c055SJed Brown     PetscKernel_v_gets_v_minus_A_times_w_3(y, J, res);
2336b7f5c055SJed Brown   }
2337b7f5c055SJed Brown   for (PetscInt i=0; i<3; i++) x[i] = y[i];
2338b7f5c055SJed Brown   PetscFunctionReturn(0);
2339b7f5c055SJed Brown }
2340b7f5c055SJed Brown 
2341b7f5c055SJed Brown const char *const DMPlexTPSTypes[] = {"SCHWARZ_P", "GYROID", "DMPlexTPSType", "DMPLEX_TPS_", NULL};
2342b7f5c055SJed Brown 
23431436d7faSJed Brown static PetscErrorCode DMPlexCreateTPSMesh_Internal(DM dm, DMPlexTPSType tpstype, const PetscInt extent[], const DMBoundaryType periodic[], PetscBool tps_distribute, PetscInt refinements, PetscInt layers, PetscReal thickness)
2344b7f5c055SJed Brown {
2345b7f5c055SJed Brown   PetscMPIInt rank;
2346b7f5c055SJed Brown   PetscInt topoDim = 2, spaceDim = 3, numFaces = 0, numVertices = 0, numEdges = 0;
2347b7f5c055SJed Brown   PetscInt (*edges)[2] = NULL, *edgeSets = NULL;
2348b7f5c055SJed Brown   PetscInt *cells_flat = NULL;
2349b7f5c055SJed Brown   PetscReal *vtxCoords = NULL;
2350b7f5c055SJed Brown   TPSEvaluateFunc evalFunc = NULL;
23514663dae6SJed Brown   PetscSimplePointFunc normalFunc = NULL;
2352b7f5c055SJed Brown   DMLabel label;
2353b7f5c055SJed Brown 
2354b7f5c055SJed Brown   PetscFunctionBegin;
23559566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
235663a3b9bcSJacob Faibussowitsch   PetscCheck((layers != 0) ^ (thickness == 0.), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_INCOMP, "Layers %" PetscInt_FMT " must be nonzero iff thickness %g is nonzero", layers, (double)thickness);
2357b7f5c055SJed Brown   switch (tpstype) {
2358b7f5c055SJed Brown   case DMPLEX_TPS_SCHWARZ_P:
2359b7f5c055SJed 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");
2360c5853193SPierre Jolivet     if (rank == 0) {
2361b7f5c055SJed Brown       PetscInt (*cells)[6][4][4] = NULL; // [junction, junction-face, cell, conn]
2362b7f5c055SJed Brown       PetscInt Njunctions = 0, Ncuts = 0, Npipes[3], vcount;
2363b7f5c055SJed Brown       PetscReal L = 1;
2364b7f5c055SJed Brown 
2365b7f5c055SJed Brown       Npipes[0] = (extent[0] + 1) * extent[1] * extent[2];
2366b7f5c055SJed Brown       Npipes[1] = extent[0] * (extent[1] + 1) * extent[2];
2367b7f5c055SJed Brown       Npipes[2] = extent[0] * extent[1] * (extent[2] + 1);
2368b7f5c055SJed Brown       Njunctions = extent[0] * extent[1] * extent[2];
2369b7f5c055SJed Brown       Ncuts = 2 * (extent[0] * extent[1] + extent[1] * extent[2] + extent[2] * extent[0]);
2370b7f5c055SJed Brown       numVertices = 4 * (Npipes[0] + Npipes[1] + Npipes[2]) + 8 * Njunctions;
23719566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(3*numVertices, &vtxCoords));
23729566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Njunctions, &cells));
23739566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Ncuts*4, &edges));
23749566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Ncuts*4, &edgeSets));
2375b7f5c055SJed Brown       // x-normal pipes
2376b7f5c055SJed Brown       vcount = 0;
2377b7f5c055SJed Brown       for (PetscInt i=0; i<extent[0]+1; i++) {
2378b7f5c055SJed Brown         for (PetscInt j=0; j<extent[1]; j++) {
2379b7f5c055SJed Brown           for (PetscInt k=0; k<extent[2]; k++) {
2380b7f5c055SJed Brown             for (PetscInt l=0; l<4; l++) {
2381b7f5c055SJed Brown               vtxCoords[vcount++] = (2*i - 1) * L;
2382b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * j * L + PetscCosReal((2*l + 1) * PETSC_PI / 4) * L / 2;
2383b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * k * L + PetscSinReal((2*l + 1) * PETSC_PI / 4) * L / 2;
2384b7f5c055SJed Brown             }
2385b7f5c055SJed Brown           }
2386b7f5c055SJed Brown         }
2387b7f5c055SJed Brown       }
2388b7f5c055SJed Brown       // y-normal pipes
2389b7f5c055SJed Brown       for (PetscInt i=0; i<extent[0]; i++) {
2390b7f5c055SJed Brown         for (PetscInt j=0; j<extent[1]+1; j++) {
2391b7f5c055SJed Brown           for (PetscInt k=0; k<extent[2]; k++) {
2392b7f5c055SJed Brown             for (PetscInt l=0; l<4; l++) {
2393b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * i * L + PetscSinReal((2*l + 1) * PETSC_PI / 4) * L / 2;
2394b7f5c055SJed Brown               vtxCoords[vcount++] = (2*j - 1) * L;
2395b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * k * L + PetscCosReal((2*l + 1) * PETSC_PI / 4) * L / 2;
2396b7f5c055SJed Brown             }
2397b7f5c055SJed Brown           }
2398b7f5c055SJed Brown         }
2399b7f5c055SJed Brown       }
2400b7f5c055SJed Brown       // z-normal pipes
2401b7f5c055SJed Brown       for (PetscInt i=0; i<extent[0]; i++) {
2402b7f5c055SJed Brown         for (PetscInt j=0; j<extent[1]; j++) {
2403b7f5c055SJed Brown           for (PetscInt k=0; k<extent[2]+1; k++) {
2404b7f5c055SJed Brown             for (PetscInt l=0; l<4; l++) {
2405b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * i * L + PetscCosReal((2*l + 1) * PETSC_PI / 4) * L / 2;
2406b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * j * L + PetscSinReal((2*l + 1) * PETSC_PI / 4) * L / 2;
2407b7f5c055SJed Brown               vtxCoords[vcount++] = (2*k - 1) * L;
2408b7f5c055SJed Brown             }
2409b7f5c055SJed Brown           }
2410b7f5c055SJed Brown         }
2411b7f5c055SJed Brown       }
2412b7f5c055SJed Brown       // junctions
2413b7f5c055SJed Brown       for (PetscInt i=0; i<extent[0]; i++) {
2414b7f5c055SJed Brown         for (PetscInt j=0; j<extent[1]; j++) {
2415b7f5c055SJed Brown           for (PetscInt k=0; k<extent[2]; k++) {
2416b7f5c055SJed Brown             const PetscInt J = (i*extent[1] + j)*extent[2] + k, Jvoff = (Npipes[0] + Npipes[1] + Npipes[2])*4 + J*8;
2417b7f5c055SJed Brown             PetscCheck(vcount / 3 == Jvoff, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unexpected vertex count");
2418b7f5c055SJed Brown             for (PetscInt ii=0; ii<2; ii++) {
2419b7f5c055SJed Brown               for (PetscInt jj=0; jj<2; jj++) {
2420b7f5c055SJed Brown                 for (PetscInt kk=0; kk<2; kk++) {
2421b7f5c055SJed Brown                   double Ls = (1 - sqrt(2) / 4) * L;
2422b7f5c055SJed Brown                   vtxCoords[vcount++] = 2*i*L + (2*ii-1) * Ls;
2423b7f5c055SJed Brown                   vtxCoords[vcount++] = 2*j*L + (2*jj-1) * Ls;
2424b7f5c055SJed Brown                   vtxCoords[vcount++] = 2*k*L + (2*kk-1) * Ls;
2425b7f5c055SJed Brown                 }
2426b7f5c055SJed Brown               }
2427b7f5c055SJed Brown             }
2428b7f5c055SJed Brown             const PetscInt jfaces[3][2][4] = {
2429b7f5c055SJed Brown               {{3,1,0,2}, {7,5,4,6}}, // x-aligned
2430b7f5c055SJed Brown               {{5,4,0,1}, {7,6,2,3}}, // y-aligned
2431b7f5c055SJed Brown               {{6,2,0,4}, {7,3,1,5}}  // z-aligned
2432b7f5c055SJed Brown             };
2433b7f5c055SJed Brown             const PetscInt pipe_lo[3] = { // vertex numbers of pipes
2434b7f5c055SJed Brown               ((i * extent[1] + j) * extent[2] + k)*4,
2435b7f5c055SJed Brown               ((i * (extent[1] + 1) + j) * extent[2] + k + Npipes[0])*4,
2436b7f5c055SJed Brown               ((i * extent[1] + j) * (extent[2]+1) + k + Npipes[0] + Npipes[1])*4
2437b7f5c055SJed Brown             };
2438b7f5c055SJed Brown             const PetscInt pipe_hi[3] = { // vertex numbers of pipes
2439b7f5c055SJed Brown               (((i + 1) * extent[1] + j) * extent[2] + k)*4,
2440b7f5c055SJed Brown               ((i * (extent[1] + 1) + j + 1) * extent[2] + k + Npipes[0])*4,
2441b7f5c055SJed Brown               ((i * extent[1] + j) * (extent[2]+1) + k + 1 + Npipes[0] + Npipes[1])*4
2442b7f5c055SJed Brown             };
2443b7f5c055SJed Brown             for (PetscInt dir=0; dir<3; dir++) { // x,y,z
2444b7f5c055SJed Brown               const PetscInt ijk[3] = {i, j, k};
2445b7f5c055SJed Brown               for (PetscInt l=0; l<4; l++) { // rotations
2446b7f5c055SJed Brown                 cells[J][dir*2+0][l][0] = pipe_lo[dir] + l;
2447b7f5c055SJed Brown                 cells[J][dir*2+0][l][1] = Jvoff + jfaces[dir][0][l];
2448b7f5c055SJed Brown                 cells[J][dir*2+0][l][2] = Jvoff + jfaces[dir][0][(l-1+4)%4];
2449b7f5c055SJed Brown                 cells[J][dir*2+0][l][3] = pipe_lo[dir] + (l-1+4)%4;
2450b7f5c055SJed Brown                 cells[J][dir*2+1][l][0] = Jvoff + jfaces[dir][1][l];
2451b7f5c055SJed Brown                 cells[J][dir*2+1][l][1] = pipe_hi[dir] + l;
2452b7f5c055SJed Brown                 cells[J][dir*2+1][l][2] = pipe_hi[dir] + (l-1+4)%4;
2453b7f5c055SJed Brown                 cells[J][dir*2+1][l][3] = Jvoff + jfaces[dir][1][(l-1+4)%4];
2454b7f5c055SJed Brown                 if (ijk[dir] == 0) {
2455b7f5c055SJed Brown                   edges[numEdges][0] = pipe_lo[dir] + l;
2456b7f5c055SJed Brown                   edges[numEdges][1] = pipe_lo[dir] + (l+1) % 4;
2457b7f5c055SJed Brown                   edgeSets[numEdges] = dir*2 + 1;
2458b7f5c055SJed Brown                   numEdges++;
2459b7f5c055SJed Brown                 }
2460b7f5c055SJed Brown                 if (ijk[dir] + 1 == extent[dir]) {
2461b7f5c055SJed Brown                   edges[numEdges][0] = pipe_hi[dir] + l;
2462b7f5c055SJed Brown                   edges[numEdges][1] = pipe_hi[dir] + (l+1) % 4;
2463b7f5c055SJed Brown                   edgeSets[numEdges] = dir*2 + 2;
2464b7f5c055SJed Brown                   numEdges++;
2465b7f5c055SJed Brown                 }
2466b7f5c055SJed Brown               }
2467b7f5c055SJed Brown             }
2468b7f5c055SJed Brown           }
2469b7f5c055SJed Brown         }
2470b7f5c055SJed Brown       }
247163a3b9bcSJacob Faibussowitsch       PetscCheck(numEdges == Ncuts * 4, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Edge count %" PetscInt_FMT " incompatible with number of cuts %" PetscInt_FMT, numEdges, Ncuts);
2472b7f5c055SJed Brown       numFaces = 24 * Njunctions;
2473b7f5c055SJed Brown       cells_flat = cells[0][0][0];
2474b7f5c055SJed Brown     }
2475b7f5c055SJed Brown     evalFunc = TPSEvaluate_SchwarzP;
24764663dae6SJed Brown     normalFunc = TPSExtrudeNormalFunc_SchwarzP;
2477b7f5c055SJed Brown     break;
2478b7f5c055SJed Brown   case DMPLEX_TPS_GYROID:
2479c5853193SPierre Jolivet     if (rank == 0) {
2480b7f5c055SJed Brown       // This is a coarse mesh approximation of the gyroid shifted to being the zero of the level set
2481b7f5c055SJed Brown       //
2482b7f5c055SJed 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)
2483b7f5c055SJed Brown       //
2484b7f5c055SJed Brown       // on the cell [0,2]^3.
2485b7f5c055SJed Brown       //
2486b7f5c055SJed Brown       // Think about dividing that cell into four columns, and focus on the column [0,1]x[0,1]x[0,2].
2487b7f5c055SJed Brown       // If you looked at the gyroid in that column at different slices of z you would see that it kind of spins
2488b7f5c055SJed Brown       // like a boomerang:
2489b7f5c055SJed Brown       //
2490b7f5c055SJed Brown       //     z = 0          z = 1/4        z = 1/2        z = 3/4     //
2491b7f5c055SJed Brown       //     -----          -------        -------        -------     //
2492b7f5c055SJed Brown       //                                                              //
2493b7f5c055SJed Brown       //     +       +      +       +      +       +      +   \   +   //
2494b7f5c055SJed Brown       //      \                                   /            \      //
2495b7f5c055SJed Brown       //       \            `-_   _-'            /              }     //
2496b7f5c055SJed Brown       //        *-_            `-'            _-'              /      //
2497b7f5c055SJed Brown       //     +     `-+      +       +      +-'     +      +   /   +   //
2498b7f5c055SJed Brown       //                                                              //
2499b7f5c055SJed Brown       //                                                              //
2500b7f5c055SJed Brown       //     z = 1          z = 5/4        z = 3/2        z = 7/4     //
2501b7f5c055SJed Brown       //     -----          -------        -------        -------     //
2502b7f5c055SJed Brown       //                                                              //
2503b7f5c055SJed Brown       //     +-_     +      +       +      +     _-+      +   /   +   //
2504b7f5c055SJed Brown       //        `-_            _-_            _-`            /        //
2505b7f5c055SJed Brown       //           \        _-'   `-_        /              {         //
2506b7f5c055SJed Brown       //            \                       /                \        //
2507b7f5c055SJed Brown       //     +       +      +       +      +       +      +   \   +   //
2508b7f5c055SJed Brown       //
2509b7f5c055SJed Brown       //
2510b7f5c055SJed Brown       // This course mesh approximates each of these slices by two line segments,
2511b7f5c055SJed Brown       // and then connects the segments in consecutive layers with quadrilateral faces.
2512b7f5c055SJed Brown       // All of the end points of the segments are multiples of 1/4 except for the
2513b7f5c055SJed Brown       // point * in the picture for z = 0 above and the similar points in other layers.
2514b7f5c055SJed Brown       // That point is at (gamma, gamma, 0), where gamma is calculated below.
2515b7f5c055SJed Brown       //
2516b7f5c055SJed Brown       // The column  [1,2]x[1,2]x[0,2] looks the same as this column;
2517b7f5c055SJed Brown       // The columns [1,2]x[0,1]x[0,2] and [0,1]x[1,2]x[0,2] are mirror images.
2518b7f5c055SJed Brown       //
2519b7f5c055SJed Brown       // As for how this method turned into the names given to the vertices:
2520b7f5c055SJed Brown       // that was not systematic, it was just the way it worked out in my handwritten notes.
2521b7f5c055SJed Brown 
2522b7f5c055SJed Brown       PetscInt facesPerBlock = 64;
2523b7f5c055SJed Brown       PetscInt vertsPerBlock = 56;
2524b7f5c055SJed Brown       PetscInt extentPlus[3];
2525b7f5c055SJed Brown       PetscInt numBlocks, numBlocksPlus;
2526b7f5c055SJed Brown       const PetscInt A =  0,   B =  1,   C =  2,   D =  3,   E =  4,   F =  5,   G =  6,   H =  7,
2527b7f5c055SJed Brown         II =  8,   J =  9,   K = 10,   L = 11,   M = 12,   N = 13,   O = 14,   P = 15,
2528b7f5c055SJed Brown         Q = 16,   R = 17,   S = 18,   T = 19,   U = 20,   V = 21,   W = 22,   X = 23,
2529b7f5c055SJed Brown         Y = 24,   Z = 25,  Ap = 26,  Bp = 27,  Cp = 28,  Dp = 29,  Ep = 30,  Fp = 31,
2530b7f5c055SJed Brown         Gp = 32,  Hp = 33,  Ip = 34,  Jp = 35,  Kp = 36,  Lp = 37,  Mp = 38,  Np = 39,
2531b7f5c055SJed Brown         Op = 40,  Pp = 41,  Qp = 42,  Rp = 43,  Sp = 44,  Tp = 45,  Up = 46,  Vp = 47,
2532b7f5c055SJed Brown         Wp = 48,  Xp = 49,  Yp = 50,  Zp = 51,  Aq = 52,  Bq = 53,  Cq = 54,  Dq = 55;
2533b7f5c055SJed Brown       const PetscInt pattern[64][4] =
2534b7f5c055SJed Brown         { /* face to vertex within the coarse discretization of a single gyroid block */
2535b7f5c055SJed Brown           /* layer 0 */
2536b7f5c055SJed 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},
2537b7f5c055SJed Brown           /* layer 1 */
2538b7f5c055SJed 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},
2539b7f5c055SJed Brown           /* layer 2 */
2540b7f5c055SJed 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},
2541b7f5c055SJed Brown           /* layer 3 */
2542b7f5c055SJed 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},
2543b7f5c055SJed Brown           /* layer 4 */
2544b7f5c055SJed 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},
2545b7f5c055SJed Brown           /* layer 5 */
2546b7f5c055SJed 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},
2547b7f5c055SJed Brown           /* layer 6 */
2548b7f5c055SJed 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},
2549b7f5c055SJed Brown           /* layer 7 (the top is the periodic image of the bottom of layer 0) */
2550b7f5c055SJed 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}
2551b7f5c055SJed Brown         };
2552b7f5c055SJed Brown       const PetscReal gamma = PetscAcosReal((PetscSqrtReal(3.)-1.) / PetscSqrtReal(2.)) / PETSC_PI;
2553b7f5c055SJed Brown       const PetscReal patternCoords[56][3] =
2554b7f5c055SJed Brown         {
2555b7f5c055SJed Brown           /* A  */ {1.,0.,0.},
2556b7f5c055SJed Brown           /* B  */ {0.,1.,0.},
2557b7f5c055SJed Brown           /* C  */ {gamma,gamma,0.},
2558b7f5c055SJed Brown           /* D  */ {1+gamma,1-gamma,0.},
2559b7f5c055SJed Brown           /* E  */ {2-gamma,2-gamma,0.},
2560b7f5c055SJed Brown           /* F  */ {1-gamma,1+gamma,0.},
2561b7f5c055SJed Brown 
2562b7f5c055SJed Brown           /* G  */ {.5,0,.25},
2563b7f5c055SJed Brown           /* H  */ {1.5,0.,.25},
2564b7f5c055SJed Brown           /* II */ {.5,1.,.25},
2565b7f5c055SJed Brown           /* J  */ {1.5,1.,.25},
2566b7f5c055SJed Brown           /* K  */ {.25,.5,.25},
2567b7f5c055SJed Brown           /* L  */ {1.25,.5,.25},
2568b7f5c055SJed Brown           /* M  */ {.75,1.5,.25},
2569b7f5c055SJed Brown           /* N  */ {1.75,1.5,.25},
2570b7f5c055SJed Brown 
2571b7f5c055SJed Brown           /* O  */ {0.,0.,.5},
2572b7f5c055SJed Brown           /* P  */ {1.,1.,.5},
2573b7f5c055SJed Brown           /* Q  */ {gamma,1-gamma,.5},
2574b7f5c055SJed Brown           /* R  */ {1+gamma,gamma,.5},
2575b7f5c055SJed Brown           /* S  */ {2-gamma,1+gamma,.5},
2576b7f5c055SJed Brown           /* T  */ {1-gamma,2-gamma,.5},
2577b7f5c055SJed Brown 
2578b7f5c055SJed Brown           /* U  */ {0.,.5,.75},
2579b7f5c055SJed Brown           /* V  */ {0.,1.5,.75},
2580b7f5c055SJed Brown           /* W  */ {1.,.5,.75},
2581b7f5c055SJed Brown           /* X  */ {1.,1.5,.75},
2582b7f5c055SJed Brown           /* Y  */ {.5,.75,.75},
2583b7f5c055SJed Brown           /* Z  */ {.5,1.75,.75},
2584b7f5c055SJed Brown           /* Ap */ {1.5,.25,.75},
2585b7f5c055SJed Brown           /* Bp */ {1.5,1.25,.75},
2586b7f5c055SJed Brown 
2587b7f5c055SJed Brown           /* Cp */ {1.,0.,1.},
2588b7f5c055SJed Brown           /* Dp */ {0.,1.,1.},
2589b7f5c055SJed Brown           /* Ep */ {1-gamma,1-gamma,1.},
2590b7f5c055SJed Brown           /* Fp */ {1+gamma,1+gamma,1.},
2591b7f5c055SJed Brown           /* Gp */ {2-gamma,gamma,1.},
2592b7f5c055SJed Brown           /* Hp */ {gamma,2-gamma,1.},
2593b7f5c055SJed Brown 
2594b7f5c055SJed Brown           /* Ip */ {.5,0.,1.25},
2595b7f5c055SJed Brown           /* Jp */ {1.5,0.,1.25},
2596b7f5c055SJed Brown           /* Kp */ {.5,1.,1.25},
2597b7f5c055SJed Brown           /* Lp */ {1.5,1.,1.25},
2598b7f5c055SJed Brown           /* Mp */ {.75,.5,1.25},
2599b7f5c055SJed Brown           /* Np */ {1.75,.5,1.25},
2600b7f5c055SJed Brown           /* Op */ {.25,1.5,1.25},
2601b7f5c055SJed Brown           /* Pp */ {1.25,1.5,1.25},
2602b7f5c055SJed Brown 
2603b7f5c055SJed Brown           /* Qp */ {0.,0.,1.5},
2604b7f5c055SJed Brown           /* Rp */ {1.,1.,1.5},
2605b7f5c055SJed Brown           /* Sp */ {1-gamma,gamma,1.5},
2606b7f5c055SJed Brown           /* Tp */ {2-gamma,1-gamma,1.5},
2607b7f5c055SJed Brown           /* Up */ {1+gamma,2-gamma,1.5},
2608b7f5c055SJed Brown           /* Vp */ {gamma,1+gamma,1.5},
2609b7f5c055SJed Brown 
2610b7f5c055SJed Brown           /* Wp */ {0.,.5,1.75},
2611b7f5c055SJed Brown           /* Xp */ {0.,1.5,1.75},
2612b7f5c055SJed Brown           /* Yp */ {1.,.5,1.75},
2613b7f5c055SJed Brown           /* Zp */ {1.,1.5,1.75},
2614b7f5c055SJed Brown           /* Aq */ {.5,.25,1.75},
2615b7f5c055SJed Brown           /* Bq */ {.5,1.25,1.75},
2616b7f5c055SJed Brown           /* Cq */ {1.5,.75,1.75},
2617b7f5c055SJed Brown           /* Dq */ {1.5,1.75,1.75},
2618b7f5c055SJed Brown         };
2619b7f5c055SJed Brown       PetscInt  (*cells)[64][4] = NULL;
2620b7f5c055SJed Brown       PetscBool *seen;
2621b7f5c055SJed Brown       PetscInt  *vertToTrueVert;
2622b7f5c055SJed Brown       PetscInt  count;
2623b7f5c055SJed Brown 
2624b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) extentPlus[i]  = extent[i] + 1;
2625b7f5c055SJed Brown       numBlocks = 1;
2626b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++)     numBlocks *= extent[i];
2627b7f5c055SJed Brown       numBlocksPlus = 1;
2628b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) numBlocksPlus *= extentPlus[i];
2629b7f5c055SJed Brown       numFaces = numBlocks * facesPerBlock;
26309566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numBlocks, &cells));
26319566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numBlocksPlus * vertsPerBlock,&seen));
2632b7f5c055SJed Brown       for (PetscInt k = 0; k < extent[2]; k++) {
2633b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
2634b7f5c055SJed Brown           for (PetscInt i = 0; i < extent[0]; i++) {
2635b7f5c055SJed Brown             for (PetscInt f = 0; f < facesPerBlock; f++) {
2636b7f5c055SJed Brown               for (PetscInt v = 0; v < 4; v++) {
2637b7f5c055SJed Brown                 PetscInt vertRaw = pattern[f][v];
2638b7f5c055SJed Brown                 PetscInt blockidx = vertRaw / 56;
2639b7f5c055SJed Brown                 PetscInt patternvert = vertRaw % 56;
2640b7f5c055SJed Brown                 PetscInt xplus = (blockidx & 1);
2641b7f5c055SJed Brown                 PetscInt yplus = (blockidx & 2) >> 1;
2642b7f5c055SJed Brown                 PetscInt zplus = (blockidx & 4) >> 2;
2643b7f5c055SJed Brown                 PetscInt zcoord = (periodic && periodic[2] == DM_BOUNDARY_PERIODIC) ? ((k + zplus) % extent[2]) : (k + zplus);
2644b7f5c055SJed Brown                 PetscInt ycoord = (periodic && periodic[1] == DM_BOUNDARY_PERIODIC) ? ((j + yplus) % extent[1]) : (j + yplus);
2645b7f5c055SJed Brown                 PetscInt xcoord = (periodic && periodic[0] == DM_BOUNDARY_PERIODIC) ? ((i + xplus) % extent[0]) : (i + xplus);
2646b7f5c055SJed Brown                 PetscInt vert = ((zcoord * extentPlus[1] + ycoord) * extentPlus[0] + xcoord) * 56 + patternvert;
2647b7f5c055SJed Brown 
2648b7f5c055SJed Brown                 cells[(k * extent[1] + j) * extent[0] + i][f][v] = vert;
2649b7f5c055SJed Brown                 seen[vert] = PETSC_TRUE;
2650b7f5c055SJed Brown               }
2651b7f5c055SJed Brown             }
2652b7f5c055SJed Brown           }
2653b7f5c055SJed Brown         }
2654b7f5c055SJed Brown       }
2655b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++) if (seen[i]) numVertices++;
2656b7f5c055SJed Brown       count = 0;
26579566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numBlocksPlus * vertsPerBlock, &vertToTrueVert));
26589566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numVertices * 3, &vtxCoords));
2659b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++) vertToTrueVert[i] = -1;
2660b7f5c055SJed Brown       for (PetscInt k = 0; k < extentPlus[2]; k++) {
2661b7f5c055SJed Brown         for (PetscInt j = 0; j < extentPlus[1]; j++) {
2662b7f5c055SJed Brown           for (PetscInt i = 0; i < extentPlus[0]; i++) {
2663b7f5c055SJed Brown             for (PetscInt v = 0; v < vertsPerBlock; v++) {
2664b7f5c055SJed Brown               PetscInt vIdx = ((k * extentPlus[1] + j) * extentPlus[0] + i) * vertsPerBlock + v;
2665b7f5c055SJed Brown 
2666b7f5c055SJed Brown               if (seen[vIdx]) {
2667b7f5c055SJed Brown                 PetscInt thisVert;
2668b7f5c055SJed Brown 
2669b7f5c055SJed Brown                 vertToTrueVert[vIdx] = thisVert = count++;
2670b7f5c055SJed Brown 
2671b7f5c055SJed Brown                 for (PetscInt d = 0; d < 3; d++) vtxCoords[3 * thisVert + d] = patternCoords[v][d];
2672b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 0] += i * 2;
2673b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 1] += j * 2;
2674b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 2] += k * 2;
2675b7f5c055SJed Brown               }
2676b7f5c055SJed Brown             }
2677b7f5c055SJed Brown           }
2678b7f5c055SJed Brown         }
2679b7f5c055SJed Brown       }
2680b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocks; i++) {
2681b7f5c055SJed Brown         for (PetscInt f = 0; f < facesPerBlock; f++) {
2682b7f5c055SJed Brown           for (PetscInt v = 0; v < 4; v++) {
2683b7f5c055SJed Brown             cells[i][f][v] = vertToTrueVert[cells[i][f][v]];
2684b7f5c055SJed Brown           }
2685b7f5c055SJed Brown         }
2686b7f5c055SJed Brown       }
26879566063dSJacob Faibussowitsch       PetscCall(PetscFree(vertToTrueVert));
26889566063dSJacob Faibussowitsch       PetscCall(PetscFree(seen));
2689b7f5c055SJed Brown       cells_flat = cells[0][0];
2690b7f5c055SJed Brown       numEdges = 0;
2691b7f5c055SJed Brown       for (PetscInt i = 0; i < numFaces; i++) {
2692b7f5c055SJed Brown         for (PetscInt e = 0; e < 4; e++) {
2693b7f5c055SJed Brown           PetscInt ev[] = {cells_flat[i*4 + e], cells_flat[i*4 + ((e+1)%4)]};
2694b7f5c055SJed Brown           const PetscReal *evCoords[] = {&vtxCoords[3*ev[0]], &vtxCoords[3*ev[1]]};
2695b7f5c055SJed Brown 
2696b7f5c055SJed Brown           for (PetscInt d = 0; d < 3; d++) {
2697b7f5c055SJed Brown             if (!periodic || periodic[0] != DM_BOUNDARY_PERIODIC) {
2698b7f5c055SJed Brown               if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) numEdges++;
2699b7f5c055SJed Brown               if (evCoords[0][d] == 2.*extent[d] && evCoords[1][d] == 2.*extent[d]) numEdges++;
2700b7f5c055SJed Brown             }
2701b7f5c055SJed Brown           }
2702b7f5c055SJed Brown         }
2703b7f5c055SJed Brown       }
27049566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numEdges, &edges));
27059566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numEdges, &edgeSets));
2706b7f5c055SJed Brown       for (PetscInt edge = 0, i = 0; i < numFaces; i++) {
2707b7f5c055SJed Brown         for (PetscInt e = 0; e < 4; e++) {
2708b7f5c055SJed Brown           PetscInt ev[] = {cells_flat[i*4 + e], cells_flat[i*4 + ((e+1)%4)]};
2709b7f5c055SJed Brown           const PetscReal *evCoords[] = {&vtxCoords[3*ev[0]], &vtxCoords[3*ev[1]]};
2710b7f5c055SJed Brown 
2711b7f5c055SJed Brown           for (PetscInt d = 0; d < 3; d++) {
2712b7f5c055SJed Brown             if (!periodic || periodic[d] != DM_BOUNDARY_PERIODIC) {
2713b7f5c055SJed Brown               if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) {
2714b7f5c055SJed Brown                 edges[edge][0] = ev[0];
2715b7f5c055SJed Brown                 edges[edge][1] = ev[1];
2716b7f5c055SJed Brown                 edgeSets[edge++] = 2 * d;
2717b7f5c055SJed Brown               }
2718b7f5c055SJed Brown               if (evCoords[0][d] == 2.*extent[d] && evCoords[1][d] == 2.*extent[d]) {
2719b7f5c055SJed Brown                 edges[edge][0] = ev[0];
2720b7f5c055SJed Brown                 edges[edge][1] = ev[1];
2721b7f5c055SJed Brown                 edgeSets[edge++] = 2 * d + 1;
2722b7f5c055SJed Brown               }
2723b7f5c055SJed Brown             }
2724b7f5c055SJed Brown           }
2725b7f5c055SJed Brown         }
2726b7f5c055SJed Brown       }
2727b7f5c055SJed Brown     }
2728b7f5c055SJed Brown     evalFunc = TPSEvaluate_Gyroid;
27294663dae6SJed Brown     normalFunc = TPSExtrudeNormalFunc_Gyroid;
2730b7f5c055SJed Brown     break;
2731b7f5c055SJed Brown   }
2732b7f5c055SJed Brown 
27339566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, topoDim));
2734c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildFromCellList(dm, numFaces, numVertices, 4, cells_flat));
27359566063dSJacob Faibussowitsch   else           PetscCall(DMPlexBuildFromCellList(dm, 0, 0, 0, NULL));
27369566063dSJacob Faibussowitsch   PetscCall(PetscFree(cells_flat));
2737b7f5c055SJed Brown   {
2738b7f5c055SJed Brown     DM idm;
27399566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(dm, &idm));
27409566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(dm, &idm));
2741b7f5c055SJed Brown   }
2742c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildCoordinatesFromCellList(dm, spaceDim, vtxCoords));
27439566063dSJacob Faibussowitsch   else           PetscCall(DMPlexBuildCoordinatesFromCellList(dm, spaceDim, NULL));
27449566063dSJacob Faibussowitsch   PetscCall(PetscFree(vtxCoords));
2745b7f5c055SJed Brown 
27469566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
27479566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "Face Sets", &label));
2748b7f5c055SJed Brown   for (PetscInt e=0; e<numEdges; e++) {
2749b7f5c055SJed Brown     PetscInt njoin;
2750b7f5c055SJed Brown     const PetscInt *join, verts[] = {numFaces + edges[e][0], numFaces + edges[e][1]};
27519566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, 2, verts, &njoin, &join));
275263a3b9bcSJacob Faibussowitsch     PetscCheck(njoin == 1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Expected unique join of vertices %" PetscInt_FMT " and %" PetscInt_FMT, edges[e][0], edges[e][1]);
27539566063dSJacob Faibussowitsch     PetscCall(DMLabelSetValue(label, join[0], edgeSets[e]));
27549566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, 2, verts, &njoin, &join));
2755b7f5c055SJed Brown   }
27569566063dSJacob Faibussowitsch   PetscCall(PetscFree(edges));
27579566063dSJacob Faibussowitsch   PetscCall(PetscFree(edgeSets));
27581436d7faSJed Brown   if (tps_distribute) {
27591436d7faSJed Brown     DM               pdm = NULL;
27601436d7faSJed Brown     PetscPartitioner part;
27611436d7faSJed Brown 
27629566063dSJacob Faibussowitsch     PetscCall(DMPlexGetPartitioner(dm, &part));
27639566063dSJacob Faibussowitsch     PetscCall(PetscPartitionerSetFromOptions(part));
27649566063dSJacob Faibussowitsch     PetscCall(DMPlexDistribute(dm, 0, NULL, &pdm));
27651436d7faSJed Brown     if (pdm) {
27669566063dSJacob Faibussowitsch       PetscCall(DMPlexReplace_Static(dm, &pdm));
27671436d7faSJed Brown     }
27681436d7faSJed Brown     // Do not auto-distribute again
27699566063dSJacob Faibussowitsch     PetscCall(DMPlexDistributeSetDefault(dm, PETSC_FALSE));
27701436d7faSJed Brown   }
2771b7f5c055SJed Brown 
27729566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
2773b7f5c055SJed Brown   for (PetscInt refine=0; refine<refinements; refine++) {
2774b7f5c055SJed Brown     PetscInt m;
2775b7f5c055SJed Brown     DM dmf;
2776b7f5c055SJed Brown     Vec X;
2777b7f5c055SJed Brown     PetscScalar *x;
27789566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, MPI_COMM_NULL, &dmf));
27799566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(dm, &dmf));
2780b7f5c055SJed Brown 
27819566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &X));
27829566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(X, &m));
27839566063dSJacob Faibussowitsch     PetscCall(VecGetArray(X, &x));
2784b7f5c055SJed Brown     for (PetscInt i=0; i<m; i+=3) {
27859566063dSJacob Faibussowitsch       PetscCall(TPSNearestPoint(evalFunc, &x[i]));
2786b7f5c055SJed Brown     }
27879566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(X, &x));
2788b7f5c055SJed Brown   }
2789b7f5c055SJed Brown 
2790b7f5c055SJed Brown   // Face Sets has already been propagated to new vertices during refinement; this propagates to the initial vertices.
27919566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "Face Sets", &label));
27929566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete(dm, label));
2793b7f5c055SJed Brown 
2794b7f5c055SJed Brown   if (thickness > 0) {
27954663dae6SJed Brown     DM edm,cdm,ecdm;
27964663dae6SJed Brown     DMPlexTransform tr;
27974663dae6SJed Brown     const char *prefix;
27984663dae6SJed Brown     PetscOptions options;
27994663dae6SJed Brown     // Code from DMPlexExtrude
28004663dae6SJed Brown     PetscCall(DMPlexTransformCreate(PetscObjectComm((PetscObject)dm), &tr));
28014663dae6SJed Brown     PetscCall(DMPlexTransformSetDM(tr, dm));
28024663dae6SJed Brown     PetscCall(DMPlexTransformSetType(tr, DMPLEXEXTRUDE));
28034663dae6SJed Brown     PetscCall(PetscObjectGetOptionsPrefix((PetscObject) dm, &prefix));
28044663dae6SJed Brown     PetscCall(PetscObjectSetOptionsPrefix((PetscObject) tr,  prefix));
28054663dae6SJed Brown     PetscCall(PetscObjectGetOptions((PetscObject) dm, &options));
28064663dae6SJed Brown     PetscCall(PetscObjectSetOptions((PetscObject) tr, options));
28074663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetLayers(tr, layers));
28084663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetThickness(tr, thickness));
28094663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetTensor(tr, PETSC_FALSE));
28104663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetSymmetric(tr, PETSC_TRUE));
28114663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetNormalFunction(tr, normalFunc));
28124663dae6SJed Brown     PetscCall(DMPlexTransformSetFromOptions(tr));
28134663dae6SJed Brown     PetscCall(PetscObjectSetOptions((PetscObject) tr, NULL));
28144663dae6SJed Brown     PetscCall(DMPlexTransformSetUp(tr));
28154663dae6SJed Brown     PetscCall(PetscObjectViewFromOptions((PetscObject) tr, NULL, "-dm_plex_tps_transform_view"));
28164663dae6SJed Brown     PetscCall(DMPlexTransformApply(tr, dm, &edm));
28174663dae6SJed Brown     PetscCall(DMCopyDisc(dm, edm));
28184663dae6SJed Brown     PetscCall(DMGetCoordinateDM(dm, &cdm));
28194663dae6SJed Brown     PetscCall(DMGetCoordinateDM(edm, &ecdm));
28204663dae6SJed Brown     PetscCall(DMCopyDisc(cdm, ecdm));
28214663dae6SJed Brown     PetscCall(DMPlexTransformCreateDiscLabels(tr, edm));
28224663dae6SJed Brown     PetscCall(DMPlexTransformDestroy(&tr));
28234663dae6SJed Brown     if (edm) {
28244663dae6SJed Brown       ((DM_Plex *)edm->data)->printFEM = ((DM_Plex *)dm->data)->printFEM;
28254663dae6SJed Brown       ((DM_Plex *)edm->data)->printL2  = ((DM_Plex *)dm->data)->printL2;
28264663dae6SJed Brown     }
28274663dae6SJed Brown     PetscCall(DMPlexReplace_Static(dm, &edm));
2828b7f5c055SJed Brown   }
2829b7f5c055SJed Brown   PetscFunctionReturn(0);
2830b7f5c055SJed Brown }
2831b7f5c055SJed Brown 
2832b7f5c055SJed Brown /*@
2833b7f5c055SJed Brown   DMPlexCreateTPSMesh - Create a distributed, interpolated mesh of a triply-periodic surface
2834b7f5c055SJed Brown 
2835b7f5c055SJed Brown   Collective
2836b7f5c055SJed Brown 
2837b7f5c055SJed Brown   Input Parameters:
2838b7f5c055SJed Brown + comm   - The communicator for the DM object
2839b7f5c055SJed Brown . tpstype - Type of triply-periodic surface
2840b7f5c055SJed Brown . extent - Array of length 3 containing number of periods in each direction
2841b7f5c055SJed Brown . periodic - array of length 3 with periodicity, or NULL for non-periodic
28421436d7faSJed Brown . tps_distribute - Distribute 2D manifold mesh prior to refinement and extrusion (more scalable)
2843817da375SSatish Balay . refinements - Number of factor-of-2 refinements of 2D manifold mesh
28441436d7faSJed Brown . layers - Number of cell layers extruded in normal direction
2845817da375SSatish Balay - thickness - Thickness in normal direction
2846b7f5c055SJed Brown 
2847b7f5c055SJed Brown   Output Parameter:
2848b7f5c055SJed Brown . dm  - The DM object
2849b7f5c055SJed Brown 
2850b7f5c055SJed Brown   Notes:
2851b7f5c055SJed 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.
2852b7f5c055SJed Brown   https://en.wikipedia.org/wiki/Schwarz_minimal_surface#Schwarz_P_(%22Primitive%22) and can be cut with "clean" boundaries.
2853b7f5c055SJed 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.
2854b7f5c055SJed Brown   Our implementation creates a very coarse mesh of the surface and refines (by 4-way splitting) as many times as requested.
2855b7f5c055SJed Brown   On each refinement, all vertices are projected to their nearest point on the surface.
2856b7f5c055SJed Brown   This projection could readily be extended to related surfaces.
2857b7f5c055SJed Brown 
2858b7f5c055SJed Brown   The face (edge) sets for the Schwarz P surface are numbered 1(-x), 2(+x), 3(-y), 4(+y), 5(-z), 6(+z).
2859b7f5c055SJed Brown   When the mesh is refined, "Face Sets" contain the new vertices (created during refinement).  Use DMPlexLabelComplete() to propagate to coarse-level vertices.
2860b7f5c055SJed Brown 
2861b7f5c055SJed Brown   References:
2862606c0280SSatish 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
2863b7f5c055SJed Brown 
2864b7f5c055SJed Brown   Developer Notes:
2865b7f5c055SJed Brown   The Gyroid mesh does not currently mark boundary sets.
2866b7f5c055SJed Brown 
2867b7f5c055SJed Brown   Level: beginner
2868b7f5c055SJed Brown 
2869db781477SPatrick Sanan .seealso: `DMPlexCreateSphereMesh()`, `DMSetType()`, `DMCreate()`
2870b7f5c055SJed Brown @*/
28711436d7faSJed 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)
2872b7f5c055SJed Brown {
2873b7f5c055SJed Brown   PetscFunctionBegin;
28749566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
28759566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
28769566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateTPSMesh_Internal(*dm, tpstype, extent, periodic, tps_distribute, refinements, layers, thickness));
2877b7f5c055SJed Brown   PetscFunctionReturn(0);
2878b7f5c055SJed Brown }
2879b7f5c055SJed Brown 
28809318fe57SMatthew G. Knepley /*@
28819318fe57SMatthew G. Knepley   DMPlexCreateSphereMesh - Creates a mesh on the d-dimensional sphere, S^d.
28829318fe57SMatthew G. Knepley 
28839318fe57SMatthew G. Knepley   Collective
28849318fe57SMatthew G. Knepley 
28859318fe57SMatthew G. Knepley   Input Parameters:
28869318fe57SMatthew G. Knepley + comm    - The communicator for the DM object
28879318fe57SMatthew G. Knepley . dim     - The dimension
28889318fe57SMatthew G. Knepley . simplex - Use simplices, or tensor product cells
28899318fe57SMatthew G. Knepley - R       - The radius
28909318fe57SMatthew G. Knepley 
28919318fe57SMatthew G. Knepley   Output Parameter:
28929318fe57SMatthew G. Knepley . dm  - The DM object
28939318fe57SMatthew G. Knepley 
28949318fe57SMatthew G. Knepley   Level: beginner
28959318fe57SMatthew G. Knepley 
2896db781477SPatrick Sanan .seealso: `DMPlexCreateBallMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
28979318fe57SMatthew G. Knepley @*/
28989318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateSphereMesh(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscReal R, DM *dm)
28999318fe57SMatthew G. Knepley {
29009318fe57SMatthew G. Knepley   PetscFunctionBegin;
29019318fe57SMatthew G. Knepley   PetscValidPointer(dm, 5);
29029566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
29039566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
29049566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSphereMesh_Internal(*dm, dim, simplex, R));
29059318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
29069318fe57SMatthew G. Knepley }
29079318fe57SMatthew G. Knepley 
29089318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBallMesh_Internal(DM dm, PetscInt dim, PetscReal R)
29099318fe57SMatthew G. Knepley {
29109318fe57SMatthew G. Knepley   DM             sdm, vol;
29119318fe57SMatthew G. Knepley   DMLabel        bdlabel;
29129318fe57SMatthew G. Knepley 
29139318fe57SMatthew G. Knepley   PetscFunctionBegin;
29149566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject) dm), &sdm));
29159566063dSJacob Faibussowitsch   PetscCall(DMSetType(sdm, DMPLEX));
29169566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject) sdm, "bd_"));
29179566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSphereMesh_Internal(sdm, dim-1, PETSC_TRUE, R));
29189566063dSJacob Faibussowitsch   PetscCall(DMSetFromOptions(sdm));
29199566063dSJacob Faibussowitsch   PetscCall(DMViewFromOptions(sdm, NULL, "-dm_view"));
29209566063dSJacob Faibussowitsch   PetscCall(DMPlexGenerate(sdm, NULL, PETSC_TRUE, &vol));
29219566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&sdm));
29229566063dSJacob Faibussowitsch   PetscCall(DMPlexReplace_Static(dm, &vol));
29239566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
29249566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "marker", &bdlabel));
29259566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkBoundaryFaces(dm, PETSC_DETERMINE, bdlabel));
29269566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete(dm, bdlabel));
292751a74b61SMatthew G. Knepley   PetscFunctionReturn(0);
292851a74b61SMatthew G. Knepley }
292951a74b61SMatthew G. Knepley 
293051a74b61SMatthew G. Knepley /*@
293151a74b61SMatthew G. Knepley   DMPlexCreateBallMesh - Creates a simplex mesh on the d-dimensional ball, B^d.
293251a74b61SMatthew G. Knepley 
293351a74b61SMatthew G. Knepley   Collective
293451a74b61SMatthew G. Knepley 
293551a74b61SMatthew G. Knepley   Input Parameters:
293651a74b61SMatthew G. Knepley + comm  - The communicator for the DM object
293751a74b61SMatthew G. Knepley . dim   - The dimension
293851a74b61SMatthew G. Knepley - R     - The radius
293951a74b61SMatthew G. Knepley 
294051a74b61SMatthew G. Knepley   Output Parameter:
294151a74b61SMatthew G. Knepley . dm  - The DM object
294251a74b61SMatthew G. Knepley 
294351a74b61SMatthew G. Knepley   Options Database Keys:
294451a74b61SMatthew G. Knepley - bd_dm_refine - This will refine the surface mesh preserving the sphere geometry
294551a74b61SMatthew G. Knepley 
294651a74b61SMatthew G. Knepley   Level: beginner
294751a74b61SMatthew G. Knepley 
2948db781477SPatrick Sanan .seealso: `DMPlexCreateSphereMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
294951a74b61SMatthew G. Knepley @*/
295051a74b61SMatthew G. Knepley PetscErrorCode DMPlexCreateBallMesh(MPI_Comm comm, PetscInt dim, PetscReal R, DM *dm)
295151a74b61SMatthew G. Knepley {
295251a74b61SMatthew G. Knepley   PetscFunctionBegin;
29539566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
29549566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
29559566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBallMesh_Internal(*dm, dim, R));
29562829fed8SMatthew G. Knepley   PetscFunctionReturn(0);
29572829fed8SMatthew G. Knepley }
29582829fed8SMatthew G. Knepley 
29599318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateReferenceCell_Internal(DM rdm, DMPolytopeType ct)
29600a6ba040SMatthew G. Knepley {
29610a6ba040SMatthew G. Knepley   PetscFunctionBegin;
29629318fe57SMatthew G. Knepley   switch (ct) {
29639318fe57SMatthew G. Knepley     case DM_POLYTOPE_POINT:
29649318fe57SMatthew G. Knepley     {
29659318fe57SMatthew G. Knepley       PetscInt    numPoints[1]        = {1};
29669318fe57SMatthew G. Knepley       PetscInt    coneSize[1]         = {0};
29679318fe57SMatthew G. Knepley       PetscInt    cones[1]            = {0};
29689318fe57SMatthew G. Knepley       PetscInt    coneOrientations[1] = {0};
29699318fe57SMatthew G. Knepley       PetscScalar vertexCoords[1]     = {0.0};
29709318fe57SMatthew G. Knepley 
29719566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 0));
29729566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 0, numPoints, coneSize, cones, coneOrientations, vertexCoords));
29739318fe57SMatthew G. Knepley     }
29749318fe57SMatthew G. Knepley     break;
29759318fe57SMatthew G. Knepley     case DM_POLYTOPE_SEGMENT:
29769318fe57SMatthew G. Knepley     {
29779318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {2, 1};
29789318fe57SMatthew G. Knepley       PetscInt    coneSize[3]         = {2, 0, 0};
29799318fe57SMatthew G. Knepley       PetscInt    cones[2]            = {1, 2};
29809318fe57SMatthew G. Knepley       PetscInt    coneOrientations[2] = {0, 0};
29819318fe57SMatthew G. Knepley       PetscScalar vertexCoords[2]     = {-1.0,  1.0};
29829318fe57SMatthew G. Knepley 
29839566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 1));
29849566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
29859318fe57SMatthew G. Knepley     }
29869318fe57SMatthew G. Knepley     break;
2987b5a892a1SMatthew G. Knepley     case DM_POLYTOPE_POINT_PRISM_TENSOR:
2988b5a892a1SMatthew G. Knepley     {
2989b5a892a1SMatthew G. Knepley       PetscInt    numPoints[2]        = {2, 1};
2990b5a892a1SMatthew G. Knepley       PetscInt    coneSize[3]         = {2, 0, 0};
2991b5a892a1SMatthew G. Knepley       PetscInt    cones[2]            = {1, 2};
2992b5a892a1SMatthew G. Knepley       PetscInt    coneOrientations[2] = {0, 0};
2993b5a892a1SMatthew G. Knepley       PetscScalar vertexCoords[2]     = {-1.0,  1.0};
2994b5a892a1SMatthew G. Knepley 
29959566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 1));
29969566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
2997b5a892a1SMatthew G. Knepley     }
2998b5a892a1SMatthew G. Knepley     break;
29999318fe57SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
30009318fe57SMatthew G. Knepley     {
30019318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {3, 1};
30029318fe57SMatthew G. Knepley       PetscInt    coneSize[4]         = {3, 0, 0, 0};
30039318fe57SMatthew G. Knepley       PetscInt    cones[3]            = {1, 2, 3};
30049318fe57SMatthew G. Knepley       PetscInt    coneOrientations[3] = {0, 0, 0};
30059318fe57SMatthew G. Knepley       PetscScalar vertexCoords[6]     = {-1.0, -1.0,  1.0, -1.0,  -1.0, 1.0};
30069318fe57SMatthew G. Knepley 
30079566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 2));
30089566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
30099318fe57SMatthew G. Knepley     }
30109318fe57SMatthew G. Knepley     break;
30119318fe57SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
30129318fe57SMatthew G. Knepley     {
30139318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {4, 1};
30149318fe57SMatthew G. Knepley       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
30159318fe57SMatthew G. Knepley       PetscInt    cones[4]            = {1, 2, 3, 4};
30169318fe57SMatthew G. Knepley       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
30179318fe57SMatthew G. Knepley       PetscScalar vertexCoords[8]     = {-1.0, -1.0,  1.0, -1.0,  1.0, 1.0,  -1.0, 1.0};
30189318fe57SMatthew G. Knepley 
30199566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 2));
30209566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
30219318fe57SMatthew G. Knepley     }
30229318fe57SMatthew G. Knepley     break;
30239318fe57SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
30249318fe57SMatthew G. Knepley     {
30259318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {4, 1};
30269318fe57SMatthew G. Knepley       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
30279318fe57SMatthew G. Knepley       PetscInt    cones[4]            = {1, 2, 3, 4};
30289318fe57SMatthew G. Knepley       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
30299318fe57SMatthew G. Knepley       PetscScalar vertexCoords[8]     = {-1.0, -1.0,  1.0, -1.0,  -1.0, 1.0,  1.0, 1.0};
30309318fe57SMatthew G. Knepley 
30319566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 2));
30329566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
30339318fe57SMatthew G. Knepley     }
30349318fe57SMatthew G. Knepley     break;
30359318fe57SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
30369318fe57SMatthew G. Knepley     {
30379318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {4, 1};
30389318fe57SMatthew G. Knepley       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
3039f0edb160SMatthew G. Knepley       PetscInt    cones[4]            = {1, 2, 3, 4};
30409318fe57SMatthew G. Knepley       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
3041f0edb160SMatthew 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};
30429318fe57SMatthew G. Knepley 
30439566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 3));
30449566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
30459318fe57SMatthew G. Knepley     }
30469318fe57SMatthew G. Knepley     break;
30479318fe57SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
30489318fe57SMatthew G. Knepley     {
30499318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {8, 1};
30509318fe57SMatthew G. Knepley       PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
3051f0edb160SMatthew G. Knepley       PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
30529318fe57SMatthew G. Knepley       PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
3053f0edb160SMatthew 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,
30549318fe57SMatthew 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};
30559318fe57SMatthew G. Knepley 
30569566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 3));
30579566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
30589318fe57SMatthew G. Knepley     }
30599318fe57SMatthew G. Knepley     break;
30609318fe57SMatthew G. Knepley     case DM_POLYTOPE_TRI_PRISM:
30619318fe57SMatthew G. Knepley     {
30629318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {6, 1};
30639318fe57SMatthew G. Knepley       PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
3064f0edb160SMatthew G. Knepley       PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
30659318fe57SMatthew G. Knepley       PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
3066f0edb160SMatthew G. Knepley       PetscScalar vertexCoords[18]    = {-1.0, -1.0, -1.0, -1.0,  1.0, -1.0,   1.0, -1.0, -1.0,
30679318fe57SMatthew G. Knepley                                          -1.0, -1.0,  1.0,  1.0, -1.0,  1.0,  -1.0,  1.0,  1.0};
30689318fe57SMatthew G. Knepley 
30699566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 3));
30709566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
30719318fe57SMatthew G. Knepley     }
30729318fe57SMatthew G. Knepley     break;
30739318fe57SMatthew G. Knepley     case DM_POLYTOPE_TRI_PRISM_TENSOR:
30749318fe57SMatthew G. Knepley     {
30759318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {6, 1};
30769318fe57SMatthew G. Knepley       PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
30779318fe57SMatthew G. Knepley       PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
30789318fe57SMatthew G. Knepley       PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
30799318fe57SMatthew G. Knepley       PetscScalar vertexCoords[18]    = {-1.0, -1.0, -1.0,  1.0, -1.0, -1.0,  -1.0, 1.0, -1.0,
30809318fe57SMatthew G. Knepley                                          -1.0, -1.0,  1.0,  1.0, -1.0,  1.0,  -1.0, 1.0,  1.0};
30819318fe57SMatthew G. Knepley 
30829566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 3));
30839566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
30849318fe57SMatthew G. Knepley     }
30859318fe57SMatthew G. Knepley     break;
30869318fe57SMatthew G. Knepley     case DM_POLYTOPE_QUAD_PRISM_TENSOR:
30879318fe57SMatthew G. Knepley     {
30889318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {8, 1};
30899318fe57SMatthew G. Knepley       PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
30909318fe57SMatthew G. Knepley       PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
30919318fe57SMatthew G. Knepley       PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
30929318fe57SMatthew 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,
30939318fe57SMatthew 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};
30949318fe57SMatthew G. Knepley 
30959566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 3));
30969566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
30979318fe57SMatthew G. Knepley     }
30989318fe57SMatthew G. Knepley     break;
30999318fe57SMatthew G. Knepley     case DM_POLYTOPE_PYRAMID:
31009318fe57SMatthew G. Knepley     {
31019318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {5, 1};
31029318fe57SMatthew G. Knepley       PetscInt    coneSize[6]         = {5, 0, 0, 0, 0, 0};
3103f0edb160SMatthew G. Knepley       PetscInt    cones[5]            = {1, 2, 3, 4, 5};
31049318fe57SMatthew G. Knepley       PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
3105f0edb160SMatthew 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,
31069318fe57SMatthew G. Knepley                                           0.0,  0.0,  1.0};
31079318fe57SMatthew G. Knepley 
31089566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 3));
31099566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
31109318fe57SMatthew G. Knepley     }
31119318fe57SMatthew G. Knepley     break;
311298921bdaSJacob Faibussowitsch     default: SETERRQ(PetscObjectComm((PetscObject) rdm), PETSC_ERR_ARG_WRONG, "Cannot create reference cell for cell type %s", DMPolytopeTypes[ct]);
31139318fe57SMatthew G. Knepley   }
31149318fe57SMatthew G. Knepley   {
31159318fe57SMatthew G. Knepley     PetscInt Nv, v;
31169318fe57SMatthew G. Knepley 
31179318fe57SMatthew G. Knepley     /* Must create the celltype label here so that we do not automatically try to compute the types */
31189566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(rdm, "celltype"));
31199566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCellType(rdm, 0, ct));
31209566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(rdm, NULL, &Nv));
31219566063dSJacob Faibussowitsch     for (v = 1; v < Nv; ++v) PetscCall(DMPlexSetCellType(rdm, v, DM_POLYTOPE_POINT));
31229318fe57SMatthew G. Knepley   }
31239566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolateInPlace_Internal(rdm));
31249566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject) rdm, DMPolytopeTypes[ct]));
31250a6ba040SMatthew G. Knepley   PetscFunctionReturn(0);
31260a6ba040SMatthew G. Knepley }
31270a6ba040SMatthew G. Knepley 
31289318fe57SMatthew G. Knepley /*@
31299318fe57SMatthew G. Knepley   DMPlexCreateReferenceCell - Create a DMPLEX with the appropriate FEM reference cell
31309318fe57SMatthew G. Knepley 
31319318fe57SMatthew G. Knepley   Collective
31329318fe57SMatthew G. Knepley 
31339318fe57SMatthew G. Knepley   Input Parameters:
31349318fe57SMatthew G. Knepley + comm - The communicator
31359318fe57SMatthew G. Knepley - ct   - The cell type of the reference cell
31369318fe57SMatthew G. Knepley 
31379318fe57SMatthew G. Knepley   Output Parameter:
31389318fe57SMatthew G. Knepley . refdm - The reference cell
31399318fe57SMatthew G. Knepley 
31409318fe57SMatthew G. Knepley   Level: intermediate
31419318fe57SMatthew G. Knepley 
3142db781477SPatrick Sanan .seealso: `DMPlexCreateReferenceCell()`, `DMPlexCreateBoxMesh()`
31439318fe57SMatthew G. Knepley @*/
31449318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateReferenceCell(MPI_Comm comm, DMPolytopeType ct, DM *refdm)
31450a6ba040SMatthew G. Knepley {
31460a6ba040SMatthew G. Knepley   PetscFunctionBegin;
31479566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, refdm));
31489566063dSJacob Faibussowitsch   PetscCall(DMSetType(*refdm, DMPLEX));
31499566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateReferenceCell_Internal(*refdm, ct));
31509318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
31519318fe57SMatthew G. Knepley }
315279a015ccSMatthew G. Knepley 
31539318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoundaryLabel_Private(DM dm, const char name[])
31549318fe57SMatthew G. Knepley {
31559318fe57SMatthew G. Knepley   DM             plex;
31569318fe57SMatthew G. Knepley   DMLabel        label;
31579318fe57SMatthew G. Knepley   PetscBool      hasLabel;
31580a6ba040SMatthew G. Knepley 
31599318fe57SMatthew G. Knepley   PetscFunctionBeginUser;
31609566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &hasLabel));
31619318fe57SMatthew G. Knepley   if (hasLabel) PetscFunctionReturn(0);
31629566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, name));
31639566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
31649566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
31659566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkBoundaryFaces(plex, 1, label));
31669566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plex));
31679318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
31689318fe57SMatthew G. Knepley }
3169acdc6f61SToby Isaac 
317005bd46c0SStefano Zampini const char * const DMPlexShapes[] = {"box", "box_surface", "ball", "sphere", "cylinder", "schwarz_p", "gyroid", "doublet", "unknown", "DMPlexShape", "DM_SHAPE_", NULL};
31719318fe57SMatthew G. Knepley 
317261a622f3SMatthew G. Knepley static PetscErrorCode DMPlexCreateFromOptions_Internal(PetscOptionItems *PetscOptionsObject, PetscBool *useCoordSpace, DM dm)
31739318fe57SMatthew G. Knepley {
31749318fe57SMatthew G. Knepley   DMPlexShape    shape = DM_SHAPE_BOX;
31759318fe57SMatthew G. Knepley   DMPolytopeType cell  = DM_POLYTOPE_TRIANGLE;
31769318fe57SMatthew G. Knepley   PetscInt       dim   = 2;
31779318fe57SMatthew G. Knepley   PetscBool      simplex = PETSC_TRUE, interpolate = PETSC_TRUE, adjCone = PETSC_FALSE, adjClosure = PETSC_TRUE, refDomain = PETSC_FALSE;
3178cd7e8a5eSksagiyam   PetscBool      flg, flg2, fflg, bdfflg, nameflg;
31799318fe57SMatthew G. Knepley   MPI_Comm       comm;
3180ed5e4e85SVaclav Hapla   char           filename[PETSC_MAX_PATH_LEN]   = "<unspecified>";
3181ed5e4e85SVaclav Hapla   char           bdFilename[PETSC_MAX_PATH_LEN] = "<unspecified>";
3182ed5e4e85SVaclav Hapla   char           plexname[PETSC_MAX_PATH_LEN]   = "";
31839318fe57SMatthew G. Knepley 
31849318fe57SMatthew G. Knepley   PetscFunctionBegin;
31859566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject) dm, &comm));
31869318fe57SMatthew G. Knepley   /* TODO Turn this into a registration interface */
31879566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_filename", "File containing a mesh", "DMPlexCreateFromFile", filename, filename, sizeof(filename), &fflg));
31889566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_boundary_filename", "File containing a mesh boundary", "DMPlexCreateFromFile", bdFilename, bdFilename, sizeof(bdFilename), &bdfflg));
31899566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_name", "Name of the mesh in the file", "DMPlexCreateFromFile", plexname, plexname, sizeof(plexname), &nameflg));
31909566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_plex_cell", "Cell shape", "", DMPolytopeTypes, (PetscEnum) cell, (PetscEnum *) &cell, NULL));
31919566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_reference_cell_domain", "Use a reference cell domain", "", refDomain, &refDomain, NULL));
31929566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_plex_shape", "Shape for built-in mesh", "", DMPlexShapes, (PetscEnum) shape, (PetscEnum *) &shape, &flg));
31939566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_dim", "Topological dimension of the mesh", "DMGetDimension", dim, &dim, &flg, 0));
319463a3b9bcSJacob Faibussowitsch   PetscCheck(!(dim < 0) && !(dim > 3),comm, PETSC_ERR_ARG_OUTOFRANGE, "Dimension %" PetscInt_FMT " should be in [1, 3]", dim);
31959566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_simplex", "Mesh cell shape", "", simplex,  &simplex, &flg));
31969566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_interpolate", "Flag to create edges and faces automatically", "", interpolate, &interpolate, &flg));
31979566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_adj_cone", "Set adjacency direction", "DMSetBasicAdjacency", adjCone,  &adjCone, &flg));
31989566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_adj_closure", "Set adjacency size", "DMSetBasicAdjacency", adjClosure,  &adjClosure, &flg2));
31999566063dSJacob Faibussowitsch   if (flg || flg2) PetscCall(DMSetBasicAdjacency(dm, adjCone, adjClosure));
32009318fe57SMatthew G. Knepley 
320161a622f3SMatthew G. Knepley   switch (cell) {
320261a622f3SMatthew G. Knepley     case DM_POLYTOPE_POINT:
320361a622f3SMatthew G. Knepley     case DM_POLYTOPE_SEGMENT:
320461a622f3SMatthew G. Knepley     case DM_POLYTOPE_POINT_PRISM_TENSOR:
320561a622f3SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
320661a622f3SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
320761a622f3SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
320861a622f3SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
320961a622f3SMatthew G. Knepley       *useCoordSpace = PETSC_TRUE;break;
321061a622f3SMatthew G. Knepley     default: *useCoordSpace = PETSC_FALSE;break;
321161a622f3SMatthew G. Knepley   }
321261a622f3SMatthew G. Knepley 
32139318fe57SMatthew G. Knepley   if (fflg) {
32149318fe57SMatthew G. Knepley     DM dmnew;
32159318fe57SMatthew G. Knepley 
32169566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject) dm), filename, plexname, interpolate, &dmnew));
32175de52c6dSVaclav Hapla     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
32189566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(dm, &dmnew));
32199318fe57SMatthew G. Knepley   } else if (refDomain) {
32209566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateReferenceCell_Internal(dm, cell));
32219318fe57SMatthew G. Knepley   } else if (bdfflg) {
32229318fe57SMatthew G. Knepley     DM bdm, dmnew;
32239318fe57SMatthew G. Knepley 
32249566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject) dm), bdFilename, plexname, interpolate, &bdm));
32259566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject) bdm, "bd_"));
32269566063dSJacob Faibussowitsch     PetscCall(DMSetFromOptions(bdm));
32279566063dSJacob Faibussowitsch     PetscCall(DMPlexGenerate(bdm, NULL, interpolate, &dmnew));
32289566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&bdm));
32295de52c6dSVaclav Hapla     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
32309566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(dm, &dmnew));
32319318fe57SMatthew G. Knepley   } else {
32329566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) dm, DMPlexShapes[shape]));
32339318fe57SMatthew G. Knepley     switch (shape) {
32349318fe57SMatthew G. Knepley       case DM_SHAPE_BOX:
32359318fe57SMatthew G. Knepley       {
32369318fe57SMatthew G. Knepley         PetscInt       faces[3] = {0, 0, 0};
32379318fe57SMatthew G. Knepley         PetscReal      lower[3] = {0, 0, 0};
32389318fe57SMatthew G. Knepley         PetscReal      upper[3] = {1, 1, 1};
32399318fe57SMatthew G. Knepley         DMBoundaryType bdt[3]   = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
32409318fe57SMatthew G. Knepley         PetscInt       i, n;
32419318fe57SMatthew G. Knepley 
32429318fe57SMatthew G. Knepley         n    = dim;
32439318fe57SMatthew G. Knepley         for (i = 0; i < dim; ++i) faces[i] = (dim == 1 ? 1 : 4-dim);
32449566063dSJacob Faibussowitsch         PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg));
32459318fe57SMatthew G. Knepley         n    = 3;
32469566063dSJacob Faibussowitsch         PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
324763a3b9bcSJacob Faibussowitsch         PetscCheck(!flg || !(n != dim),comm, PETSC_ERR_ARG_SIZ, "Lower box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
32489318fe57SMatthew G. Knepley         n    = 3;
32499566063dSJacob Faibussowitsch         PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
325063a3b9bcSJacob Faibussowitsch         PetscCheck(!flg || !(n != dim),comm, PETSC_ERR_ARG_SIZ, "Upper box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
32519318fe57SMatthew G. Knepley         n    = 3;
32529566063dSJacob Faibussowitsch         PetscCall(PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *) bdt, &n, &flg));
325363a3b9bcSJacob Faibussowitsch         PetscCheck(!flg || !(n != dim),comm, PETSC_ERR_ARG_SIZ, "Box boundary types had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
32549318fe57SMatthew G. Knepley         switch (cell) {
325561a622f3SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
32569566063dSJacob Faibussowitsch             PetscCall(DMPlexCreateWedgeBoxMesh_Internal(dm, faces, lower, upper, bdt));
3257d410b0cfSMatthew G. Knepley             if (!interpolate) {
3258d410b0cfSMatthew G. Knepley               DM udm;
3259d410b0cfSMatthew G. Knepley 
32609566063dSJacob Faibussowitsch               PetscCall(DMPlexUninterpolate(dm, &udm));
32619566063dSJacob Faibussowitsch               PetscCall(DMPlexReplace_Static(dm, &udm));
3262d410b0cfSMatthew G. Knepley             }
32639318fe57SMatthew G. Knepley             break;
32649318fe57SMatthew G. Knepley           default:
32659566063dSJacob Faibussowitsch             PetscCall(DMPlexCreateBoxMesh_Internal(dm, dim, simplex, faces, lower, upper, bdt, interpolate));
32669318fe57SMatthew G. Knepley             break;
32679318fe57SMatthew G. Knepley         }
32689318fe57SMatthew G. Knepley       }
32699318fe57SMatthew G. Knepley       break;
32709318fe57SMatthew G. Knepley       case DM_SHAPE_BOX_SURFACE:
32719318fe57SMatthew G. Knepley       {
32729318fe57SMatthew G. Knepley         PetscInt  faces[3] = {0, 0, 0};
32739318fe57SMatthew G. Knepley         PetscReal lower[3] = {0, 0, 0};
32749318fe57SMatthew G. Knepley         PetscReal upper[3] = {1, 1, 1};
32759318fe57SMatthew G. Knepley         PetscInt  i, n;
32769318fe57SMatthew G. Knepley 
32779318fe57SMatthew G. Knepley         n    = dim+1;
32789318fe57SMatthew G. Knepley         for (i = 0; i < dim+1; ++i) faces[i] = (dim+1 == 1 ? 1 : 4-(dim+1));
32799566063dSJacob Faibussowitsch         PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg));
32809318fe57SMatthew G. Knepley         n    = 3;
32819566063dSJacob Faibussowitsch         PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
328263a3b9bcSJacob Faibussowitsch         PetscCheck(!flg || !(n != dim+1),comm, PETSC_ERR_ARG_SIZ, "Lower box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim+1);
32839318fe57SMatthew G. Knepley         n    = 3;
32849566063dSJacob Faibussowitsch         PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
328563a3b9bcSJacob Faibussowitsch         PetscCheck(!flg || !(n != dim+1),comm, PETSC_ERR_ARG_SIZ, "Upper box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim+1);
32869566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(dm, dim+1, faces, lower, upper, interpolate));
32879318fe57SMatthew G. Knepley       }
32889318fe57SMatthew G. Knepley       break;
32899318fe57SMatthew G. Knepley       case DM_SHAPE_SPHERE:
32909318fe57SMatthew G. Knepley       {
32919318fe57SMatthew G. Knepley         PetscReal R = 1.0;
32929318fe57SMatthew G. Knepley 
32939566063dSJacob Faibussowitsch         PetscCall(PetscOptionsReal("-dm_plex_sphere_radius", "Radius of the sphere", "", R,  &R, &flg));
32949566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateSphereMesh_Internal(dm, dim, simplex, R));
32959318fe57SMatthew G. Knepley       }
32969318fe57SMatthew G. Knepley       break;
32979318fe57SMatthew G. Knepley       case DM_SHAPE_BALL:
32989318fe57SMatthew G. Knepley       {
32999318fe57SMatthew G. Knepley         PetscReal R = 1.0;
33009318fe57SMatthew G. Knepley 
33019566063dSJacob Faibussowitsch         PetscCall(PetscOptionsReal("-dm_plex_ball_radius", "Radius of the ball", "", R,  &R, &flg));
33029566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateBallMesh_Internal(dm, dim, R));
33039318fe57SMatthew G. Knepley       }
33049318fe57SMatthew G. Knepley       break;
33059318fe57SMatthew G. Knepley       case DM_SHAPE_CYLINDER:
33069318fe57SMatthew G. Knepley       {
33079318fe57SMatthew G. Knepley         DMBoundaryType bdt = DM_BOUNDARY_NONE;
33089318fe57SMatthew G. Knepley         PetscInt       Nw  = 6;
33099318fe57SMatthew G. Knepley 
33109566063dSJacob Faibussowitsch         PetscCall(PetscOptionsEnum("-dm_plex_cylinder_bd", "Boundary type in the z direction", "", DMBoundaryTypes, (PetscEnum) bdt, (PetscEnum *) &bdt, NULL));
33119566063dSJacob Faibussowitsch         PetscCall(PetscOptionsInt("-dm_plex_cylinder_num_wedges", "Number of wedges around the cylinder", "", Nw, &Nw, NULL));
33129318fe57SMatthew G. Knepley         switch (cell) {
331361a622f3SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
33149566063dSJacob Faibussowitsch             PetscCall(DMPlexCreateWedgeCylinderMesh_Internal(dm, Nw, interpolate));
33159318fe57SMatthew G. Knepley             break;
33169318fe57SMatthew G. Knepley           default:
33179566063dSJacob Faibussowitsch             PetscCall(DMPlexCreateHexCylinderMesh_Internal(dm, bdt));
33189318fe57SMatthew G. Knepley             break;
33199318fe57SMatthew G. Knepley         }
33209318fe57SMatthew G. Knepley       }
33219318fe57SMatthew G. Knepley       break;
3322b7f5c055SJed Brown       case DM_SHAPE_SCHWARZ_P: // fallthrough
3323b7f5c055SJed Brown       case DM_SHAPE_GYROID:
3324b7f5c055SJed Brown       {
3325b7f5c055SJed Brown         PetscInt       extent[3] = {1,1,1}, refine = 0, layers = 0, three;
3326b7f5c055SJed Brown         PetscReal      thickness = 0.;
3327b7f5c055SJed Brown         DMBoundaryType periodic[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
3328b7f5c055SJed Brown         DMPlexTPSType  tps_type = shape == DM_SHAPE_SCHWARZ_P ? DMPLEX_TPS_SCHWARZ_P : DMPLEX_TPS_GYROID;
33291436d7faSJed Brown         PetscBool      tps_distribute;
33309566063dSJacob Faibussowitsch         PetscCall(PetscOptionsIntArray("-dm_plex_tps_extent", "Number of replicas for each of three dimensions", NULL, extent, (three=3, &three), NULL));
33319566063dSJacob Faibussowitsch         PetscCall(PetscOptionsInt("-dm_plex_tps_refine", "Number of refinements", NULL, refine, &refine, NULL));
33329566063dSJacob Faibussowitsch         PetscCall(PetscOptionsEnumArray("-dm_plex_tps_periodic", "Periodicity in each of three dimensions", NULL, DMBoundaryTypes, (PetscEnum*)periodic, (three=3, &three), NULL));
33339566063dSJacob Faibussowitsch         PetscCall(PetscOptionsInt("-dm_plex_tps_layers", "Number of layers in volumetric extrusion (or zero to not extrude)", NULL, layers, &layers, NULL));
33349566063dSJacob Faibussowitsch         PetscCall(PetscOptionsReal("-dm_plex_tps_thickness", "Thickness of volumetric extrusion", NULL, thickness, &thickness, NULL));
33359566063dSJacob Faibussowitsch         PetscCall(DMPlexDistributeGetDefault(dm, &tps_distribute));
33369566063dSJacob Faibussowitsch         PetscCall(PetscOptionsBool("-dm_plex_tps_distribute", "Distribute the 2D mesh prior to refinement and extrusion", NULL, tps_distribute, &tps_distribute, NULL));
33379566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateTPSMesh_Internal(dm, tps_type, extent, periodic, tps_distribute, refine, layers, thickness));
3338b7f5c055SJed Brown       }
3339b7f5c055SJed Brown       break;
334005bd46c0SStefano Zampini       case DM_SHAPE_DOUBLET:
334105bd46c0SStefano Zampini       {
334205bd46c0SStefano Zampini         DM        dmnew;
334305bd46c0SStefano Zampini         PetscReal rl = 0.0;
334405bd46c0SStefano Zampini 
334505bd46c0SStefano Zampini         PetscCall(PetscOptionsReal("-dm_plex_doublet_refinementlimit", "Refinement limit", NULL, rl, &rl, NULL));
334605bd46c0SStefano Zampini         PetscCall(DMPlexCreateDoublet(PetscObjectComm((PetscObject)dm), dim, simplex, interpolate, rl, &dmnew));
33475de52c6dSVaclav Hapla         PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
334805bd46c0SStefano Zampini         PetscCall(DMPlexReplace_Static(dm, &dmnew));
334905bd46c0SStefano Zampini       }
335005bd46c0SStefano Zampini       break;
335198921bdaSJacob Faibussowitsch       default: SETERRQ(comm, PETSC_ERR_SUP, "Domain shape %s is unsupported", DMPlexShapes[shape]);
33529318fe57SMatthew G. Knepley     }
33539318fe57SMatthew G. Knepley   }
33549566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
3355ed5e4e85SVaclav Hapla   if (!((PetscObject)dm)->name && nameflg) {
33569566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)dm, plexname));
3357ed5e4e85SVaclav Hapla   }
33580a6ba040SMatthew G. Knepley   PetscFunctionReturn(0);
33590a6ba040SMatthew G. Knepley }
33600a6ba040SMatthew G. Knepley 
336147920aaeSMatthew G. Knepley PetscErrorCode DMSetFromOptions_NonRefinement_Plex(PetscOptionItems *PetscOptionsObject, DM dm)
33620a6ba040SMatthew G. Knepley {
33630a6ba040SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
33647f9d8d6cSVaclav Hapla   PetscBool      flg, flg2;
33659318fe57SMatthew G. Knepley   char           bdLabel[PETSC_MAX_PATH_LEN];
33660a6ba040SMatthew G. Knepley 
33670a6ba040SMatthew G. Knepley   PetscFunctionBegin;
33680a6ba040SMatthew G. Knepley   /* Handle viewing */
33699566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_print_set_values", "Output all set values info", "DMPlexMatSetClosure", PETSC_FALSE, &mesh->printSetValues, NULL));
33709566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_fem", "Debug output level all fem computations", "DMPlexSNESComputeResidualFEM", 0, &mesh->printFEM, NULL,0));
33719566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-dm_plex_print_tol", "Tolerance for FEM output", "DMPlexSNESComputeResidualFEM", mesh->printTol, &mesh->printTol, NULL));
33729566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_l2", "Debug output level all L2 diff computations", "DMComputeL2Diff", 0, &mesh->printL2, NULL,0));
33739566063dSJacob Faibussowitsch   PetscCall(DMMonitorSetFromOptions(dm, "-dm_plex_monitor_throughput", "Monitor the simulation throughput", "DMPlexMonitorThroughput", DMPlexMonitorThroughput, NULL, &flg));
33749566063dSJacob Faibussowitsch   if (flg) PetscCall(PetscLogDefaultBegin());
33759318fe57SMatthew G. Knepley   /* Labeling */
33769566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_boundary_label", "Label to mark the mesh boundary", "", bdLabel, bdLabel, sizeof(bdLabel), &flg));
33779566063dSJacob Faibussowitsch   if (flg) PetscCall(DMPlexCreateBoundaryLabel_Private(dm, bdLabel));
3378953fc75cSMatthew G. Knepley   /* Point Location */
33799566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_hash_location", "Use grid hashing for point location", "DMInterpolate", PETSC_FALSE, &mesh->useHashLocation, NULL));
33800848f4b5SMatthew G. Knepley   /* Partitioning and distribution */
33819566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_partition_balance", "Attempt to evenly divide points on partition boundary between processes", "DMPlexSetPartitionBalance", PETSC_FALSE, &mesh->partitionBalance, NULL));
33822e62ab5aSMatthew G. Knepley   /* Generation and remeshing */
33839566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_remesh_bd", "Allow changes to the boundary on remeshing", "DMAdapt", PETSC_FALSE, &mesh->remeshBd, NULL));
3384b29cfa1cSToby Isaac   /* Projection behavior */
33859566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_max_projection_height", "Maxmimum mesh point height used to project locally", "DMPlexSetMaxProjectionHeight", 0, &mesh->maxProjectionHeight, NULL,0));
33869566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_regular_refinement", "Use special nested projection algorithm for regular refinement", "DMPlexSetRegularRefinement", mesh->regularRefinement, &mesh->regularRefinement, NULL));
3387f12cf164SMatthew G. Knepley   /* Checking structure */
3388f12cf164SMatthew G. Knepley   {
33897f9d8d6cSVaclav Hapla     PetscBool   all = PETSC_FALSE;
3390f12cf164SMatthew G. Knepley 
33917f9d8d6cSVaclav Hapla     PetscCall(PetscOptionsBool("-dm_plex_check_all", "Perform all basic checks", "DMPlexCheck", PETSC_FALSE, &all, NULL));
33927f9d8d6cSVaclav Hapla     if (all) {
33937f9d8d6cSVaclav Hapla       PetscCall(DMPlexCheck(dm));
33947f9d8d6cSVaclav Hapla     } else {
33959566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_symmetry", "Check that the adjacency information in the mesh is symmetric", "DMPlexCheckSymmetry", PETSC_FALSE, &flg, &flg2));
33967f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckSymmetry(dm));
33979566063dSJacob Faibussowitsch       PetscCall(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));
33987f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckSkeleton(dm, 0));
33999566063dSJacob Faibussowitsch       PetscCall(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));
34007f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckFaces(dm, 0));
34019566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_geometry", "Check that cells have positive volume", "DMPlexCheckGeometry", PETSC_FALSE, &flg, &flg2));
34027f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckGeometry(dm));
34039566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_pointsf", "Check some necessary conditions for PointSF", "DMPlexCheckPointSF", PETSC_FALSE, &flg, &flg2));
34047f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckPointSF(dm, NULL));
34059566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_interface_cones", "Check points on inter-partition interfaces have conforming order of cone points", "DMPlexCheckInterfaceCones", PETSC_FALSE, &flg, &flg2));
34067f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckInterfaceCones(dm));
34077f9d8d6cSVaclav Hapla     }
34089566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-dm_plex_check_cell_shape", "Check cell shape", "DMPlexCheckCellShape", PETSC_FALSE, &flg, &flg2));
34099566063dSJacob Faibussowitsch     if (flg && flg2) PetscCall(DMPlexCheckCellShape(dm, PETSC_TRUE, PETSC_DETERMINE));
3410f12cf164SMatthew G. Knepley   }
34119318fe57SMatthew G. Knepley   {
34129318fe57SMatthew G. Knepley     PetscReal scale = 1.0;
34134f3833eaSMatthew G. Knepley 
34149566063dSJacob Faibussowitsch     PetscCall(PetscOptionsReal("-dm_plex_scale", "Scale factor for mesh coordinates", "DMPlexScale", scale, &scale, &flg));
34159318fe57SMatthew G. Knepley     if (flg) {
34169318fe57SMatthew G. Knepley       Vec coordinates, coordinatesLocal;
34179318fe57SMatthew G. Knepley 
34189566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinates(dm, &coordinates));
34199566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinatesLocal(dm, &coordinatesLocal));
34209566063dSJacob Faibussowitsch       PetscCall(VecScale(coordinates, scale));
34219566063dSJacob Faibussowitsch       PetscCall(VecScale(coordinatesLocal, scale));
34229318fe57SMatthew G. Knepley     }
34239318fe57SMatthew G. Knepley   }
34249566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerSetFromOptions(mesh->partitioner));
342568d4fef7SMatthew G. Knepley   PetscFunctionReturn(0);
342668d4fef7SMatthew G. Knepley }
342768d4fef7SMatthew G. Knepley 
3428c506a872SMatthew G. Knepley PetscErrorCode DMSetFromOptions_Overlap_Plex(PetscOptionItems *PetscOptionsObject, DM dm, PetscInt *overlap)
3429c506a872SMatthew G. Knepley {
3430c506a872SMatthew G. Knepley   PetscInt  numOvLabels = 16, numOvExLabels = 16;
3431c506a872SMatthew G. Knepley   char     *ovLabelNames[16], *ovExLabelNames[16];
3432c506a872SMatthew G. Knepley   PetscInt  numOvValues = 16, numOvExValues = 16, l;
3433c506a872SMatthew G. Knepley   PetscBool flg;
3434c506a872SMatthew G. Knepley 
3435c506a872SMatthew G. Knepley   PetscFunctionBegin;
3436c506a872SMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_distribute_overlap", "The size of the overlap halo", "DMPlexDistribute", *overlap, overlap, NULL, 0));
3437c506a872SMatthew G. Knepley   PetscCall(PetscOptionsStringArray("-dm_distribute_overlap_labels", "List of overlap label names", "DMPlexDistribute", ovLabelNames, &numOvLabels, &flg));
3438c506a872SMatthew G. Knepley   if (!flg) numOvLabels = 0;
3439c506a872SMatthew G. Knepley   if (numOvLabels) {
3440c506a872SMatthew G. Knepley     ((DM_Plex*) dm->data)->numOvLabels = numOvLabels;
3441c506a872SMatthew G. Knepley     for (l = 0; l < numOvLabels; ++l) {
3442c506a872SMatthew G. Knepley       PetscCall(DMGetLabel(dm, ovLabelNames[l], &((DM_Plex*) dm->data)->ovLabels[l]));
3443c506a872SMatthew G. Knepley       PetscCheck(((DM_Plex*) dm->data)->ovLabels[l], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid label name %s", ovLabelNames[l]);
3444c506a872SMatthew G. Knepley       PetscCall(PetscFree(ovLabelNames[l]));
3445c506a872SMatthew G. Knepley     }
3446c506a872SMatthew G. Knepley     PetscCall(PetscOptionsIntArray("-dm_distribute_overlap_values", "List of overlap label values", "DMPlexDistribute", ((DM_Plex*) dm->data)->ovValues, &numOvValues, &flg));
3447c506a872SMatthew G. Knepley     if (!flg) numOvValues = 0;
3448c506a872SMatthew G. Knepley     PetscCheck(numOvLabels == numOvValues, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "The number of labels %" PetscInt_FMT " must match the number of values %" PetscInt_FMT, numOvLabels, numOvValues);
3449c506a872SMatthew G. Knepley 
3450c506a872SMatthew G. Knepley     PetscCall(PetscOptionsStringArray("-dm_distribute_overlap_exclude_labels", "List of overlap exclude label names", "DMPlexDistribute", ovExLabelNames, &numOvExLabels, &flg));
3451c506a872SMatthew G. Knepley     if (!flg) numOvExLabels = 0;
3452c506a872SMatthew G. Knepley     ((DM_Plex*) dm->data)->numOvExLabels = numOvExLabels;
3453c506a872SMatthew G. Knepley     for (l = 0; l < numOvExLabels; ++l) {
3454c506a872SMatthew G. Knepley       PetscCall(DMGetLabel(dm, ovExLabelNames[l], &((DM_Plex*) dm->data)->ovExLabels[l]));
3455c506a872SMatthew G. Knepley       PetscCheck(((DM_Plex*) dm->data)->ovExLabels[l], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid label name %s", ovExLabelNames[l]);
3456c506a872SMatthew G. Knepley       PetscCall(PetscFree(ovExLabelNames[l]));
3457c506a872SMatthew G. Knepley     }
3458c506a872SMatthew G. Knepley     PetscCall(PetscOptionsIntArray("-dm_distribute_overlap_exclude_values", "List of overlap exclude label values", "DMPlexDistribute", ((DM_Plex*) dm->data)->ovExValues, &numOvExValues, &flg));
3459c506a872SMatthew G. Knepley     if (!flg) numOvExValues = 0;
3460c506a872SMatthew G. Knepley     PetscCheck(numOvExLabels == numOvExValues, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "The number of exclude labels %" PetscInt_FMT " must match the number of values %" PetscInt_FMT, numOvExLabels, numOvExValues);
3461c506a872SMatthew G. Knepley   }
3462c506a872SMatthew G. Knepley   PetscFunctionReturn(0);
3463c506a872SMatthew G. Knepley }
3464c506a872SMatthew G. Knepley 
346546fa42a0SMatthew G. Knepley static PetscErrorCode DMSetFromOptions_Plex(PetscOptionItems *PetscOptionsObject,DM dm)
346668d4fef7SMatthew G. Knepley {
3467bdf63967SMatthew G. Knepley   PetscFunctionList ordlist;
3468bdf63967SMatthew G. Knepley   char                      oname[256];
3469d410b0cfSMatthew G. Knepley   PetscReal                 volume = -1.0;
34709318fe57SMatthew G. Knepley   PetscInt                  prerefine = 0, refine = 0, r, coarsen = 0, overlap = 0, extLayers = 0, dim;
3471e600fa54SMatthew 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;
34726bc1bd01Sksagiyam   DMPlexReorderDefaultFlag  reorder;
347368d4fef7SMatthew G. Knepley 
347468d4fef7SMatthew G. Knepley   PetscFunctionBegin;
3475064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
3476d0609cedSBarry Smith   PetscOptionsHeadBegin(PetscOptionsObject,"DMPlex Options");
34779318fe57SMatthew G. Knepley   /* Handle automatic creation */
34789566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
34796bc1bd01Sksagiyam   if (dim < 0) {
34806bc1bd01Sksagiyam     PetscCall(DMPlexCreateFromOptions_Internal(PetscOptionsObject, &coordSpace, dm));
34816bc1bd01Sksagiyam     created = PETSC_TRUE;
34826bc1bd01Sksagiyam   }
34836bc1bd01Sksagiyam   PetscCall(DMGetDimension(dm, &dim));
3484d89e6e46SMatthew G. Knepley   /* Handle interpolation before distribution */
34859566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_interpolate_pre", "Flag to interpolate mesh before distribution", "", interpolate, &interpolate, &flg));
3486d89e6e46SMatthew G. Knepley   if (flg) {
3487d89e6e46SMatthew G. Knepley     DMPlexInterpolatedFlag interpolated;
3488d89e6e46SMatthew G. Knepley 
34899566063dSJacob Faibussowitsch     PetscCall(DMPlexIsInterpolated(dm, &interpolated));
3490d89e6e46SMatthew G. Knepley     if (interpolated == DMPLEX_INTERPOLATED_FULL && !interpolate) {
3491d89e6e46SMatthew G. Knepley       DM udm;
3492d89e6e46SMatthew G. Knepley 
34939566063dSJacob Faibussowitsch       PetscCall(DMPlexUninterpolate(dm, &udm));
34949566063dSJacob Faibussowitsch       PetscCall(DMPlexReplace_Static(dm, &udm));
3495d89e6e46SMatthew G. Knepley     } else if (interpolated != DMPLEX_INTERPOLATED_FULL && interpolate) {
3496d89e6e46SMatthew G. Knepley       DM idm;
3497d89e6e46SMatthew G. Knepley 
34989566063dSJacob Faibussowitsch       PetscCall(DMPlexInterpolate(dm, &idm));
34999566063dSJacob Faibussowitsch       PetscCall(DMPlexReplace_Static(dm, &idm));
3500d89e6e46SMatthew G. Knepley     }
3501d89e6e46SMatthew G. Knepley   }
35029b44eab4SMatthew G. Knepley   /* Handle DMPlex refinement before distribution */
35039566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_ignore_model", "Flag to ignore the geometry model when refining", "DMCreate", ignoreModel, &ignoreModel, &flg));
3504c1cad2e7SMatthew G. Knepley   if (flg) {((DM_Plex *) dm->data)->ignoreModel = ignoreModel;}
35059566063dSJacob Faibussowitsch   PetscCall(DMPlexGetRefinementUniform(dm, &uniformOrig));
35069566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine_pre", "The number of refinements before distribution", "DMCreate", prerefine, &prerefine, NULL,0));
35079566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_remap_pre", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL));
35089566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_uniform_pre", "Flag for uniform refinement before distribution", "DMCreate", uniform, &uniform, &flg));
35099566063dSJacob Faibussowitsch   if (flg) PetscCall(DMPlexSetRefinementUniform(dm, uniform));
35109566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-dm_refine_volume_limit_pre", "The maximum cell volume after refinement before distribution", "DMCreate", volume, &volume, &flg));
35119318fe57SMatthew G. Knepley   if (flg) {
35129566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementUniform(dm, PETSC_FALSE));
35139566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementLimit(dm, volume));
35149318fe57SMatthew G. Knepley     prerefine = PetscMax(prerefine, 1);
35159318fe57SMatthew G. Knepley   }
35169b44eab4SMatthew G. Knepley   for (r = 0; r < prerefine; ++r) {
35179b44eab4SMatthew G. Knepley     DM             rdm;
35189b44eab4SMatthew G. Knepley     PetscPointFunc coordFunc = ((DM_Plex*) dm->data)->coordFunc;
35199b44eab4SMatthew G. Knepley 
35209566063dSJacob Faibussowitsch     PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm));
35219566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject) dm), &rdm));
35229566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(dm, &rdm));
35239566063dSJacob Faibussowitsch     PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm));
352461a622f3SMatthew G. Knepley     if (coordFunc && remap) {
35259566063dSJacob Faibussowitsch       PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
35269b44eab4SMatthew G. Knepley       ((DM_Plex*) dm->data)->coordFunc = coordFunc;
35279b44eab4SMatthew G. Knepley     }
35289b44eab4SMatthew G. Knepley   }
35299566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, uniformOrig));
35309318fe57SMatthew G. Knepley   /* Handle DMPlex extrusion before distribution */
35319566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_extrude", "The number of layers to extrude", "", extLayers, &extLayers, NULL, 0));
35329318fe57SMatthew G. Knepley   if (extLayers) {
35339318fe57SMatthew G. Knepley     DM edm;
35349318fe57SMatthew G. Knepley 
35359566063dSJacob Faibussowitsch     PetscCall(DMExtrude(dm, extLayers, &edm));
35369566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(dm, &edm));
353748d16a33SMatthew G. Knepley     ((DM_Plex *) dm->data)->coordFunc = NULL;
35389566063dSJacob Faibussowitsch     PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm));
3539d410b0cfSMatthew G. Knepley     extLayers = 0;
35409318fe57SMatthew G. Knepley   }
3541bdf63967SMatthew G. Knepley   /* Handle DMPlex reordering before distribution */
35426bc1bd01Sksagiyam   PetscCall(DMPlexReorderGetDefault(dm, &reorder));
35439566063dSJacob Faibussowitsch   PetscCall(MatGetOrderingList(&ordlist));
35446bc1bd01Sksagiyam   PetscCall(PetscStrncpy(oname, MATORDERINGNATURAL, sizeof(oname)));
35459566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_plex_reorder", "Set mesh reordering type", "DMPlexGetOrdering", ordlist, MATORDERINGNATURAL, oname, sizeof(oname), &flg));
35466bc1bd01Sksagiyam   if (reorder == DMPLEX_REORDER_DEFAULT_TRUE || flg) {
3547bdf63967SMatthew G. Knepley     DM pdm;
3548bdf63967SMatthew G. Knepley     IS perm;
3549bdf63967SMatthew G. Knepley 
35509566063dSJacob Faibussowitsch     PetscCall(DMPlexGetOrdering(dm, oname, NULL, &perm));
35519566063dSJacob Faibussowitsch     PetscCall(DMPlexPermute(dm, perm, &pdm));
35529566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&perm));
35539566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(dm, &pdm));
35549566063dSJacob Faibussowitsch     PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm));
3555bdf63967SMatthew G. Knepley   }
35569b44eab4SMatthew G. Knepley   /* Handle DMPlex distribution */
35579566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeGetDefault(dm, &distribute));
3558c506a872SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_distribute", "Flag to redistribute a mesh among processes", "DMPlexDistribute", distribute, &distribute, NULL));
3559c506a872SMatthew G. Knepley   PetscCall(DMSetFromOptions_Overlap_Plex(PetscOptionsObject, dm, &overlap));
35609b44eab4SMatthew G. Knepley   if (distribute) {
35619b44eab4SMatthew G. Knepley     DM               pdm = NULL;
35629b44eab4SMatthew G. Knepley     PetscPartitioner part;
35639b44eab4SMatthew G. Knepley 
35649566063dSJacob Faibussowitsch     PetscCall(DMPlexGetPartitioner(dm, &part));
35659566063dSJacob Faibussowitsch     PetscCall(PetscPartitionerSetFromOptions(part));
35669566063dSJacob Faibussowitsch     PetscCall(DMPlexDistribute(dm, overlap, NULL, &pdm));
35679b44eab4SMatthew G. Knepley     if (pdm) {
35689566063dSJacob Faibussowitsch       PetscCall(DMPlexReplace_Static(dm, &pdm));
35699b44eab4SMatthew G. Knepley     }
35709b44eab4SMatthew G. Knepley   }
35719318fe57SMatthew G. Knepley   /* Create coordinate space */
35729318fe57SMatthew G. Knepley   if (created) {
357361a622f3SMatthew G. Knepley     DM_Plex  *mesh = (DM_Plex *) dm->data;
35749318fe57SMatthew G. Knepley     PetscInt  degree = 1;
357561a622f3SMatthew G. Knepley     PetscBool periodic, flg;
35769318fe57SMatthew G. Knepley 
35779566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-dm_coord_space", "Use an FEM space for coordinates", "", coordSpace, &coordSpace, &flg));
35789566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-dm_coord_petscspace_degree", "FEM degree for coordinate space", "", degree, &degree, NULL));
35799566063dSJacob Faibussowitsch     if (coordSpace) PetscCall(DMPlexCreateCoordinateSpace(dm, degree, mesh->coordFunc));
358061a622f3SMatthew G. Knepley     if (flg && !coordSpace) {
358161a622f3SMatthew G. Knepley       DM           cdm;
358261a622f3SMatthew G. Knepley       PetscDS      cds;
358361a622f3SMatthew G. Knepley       PetscObject  obj;
358461a622f3SMatthew G. Knepley       PetscClassId id;
358561a622f3SMatthew G. Knepley 
35869566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinateDM(dm, &cdm));
35879566063dSJacob Faibussowitsch       PetscCall(DMGetDS(cdm, &cds));
35889566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
35899566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
359061a622f3SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
359161a622f3SMatthew G. Knepley         PetscContainer dummy;
359261a622f3SMatthew G. Knepley 
35939566063dSJacob Faibussowitsch         PetscCall(PetscContainerCreate(PETSC_COMM_SELF, &dummy));
35949566063dSJacob Faibussowitsch         PetscCall(PetscObjectSetName((PetscObject) dummy, "coordinates"));
35959566063dSJacob Faibussowitsch         PetscCall(DMSetField(cdm, 0, NULL, (PetscObject) dummy));
35969566063dSJacob Faibussowitsch         PetscCall(PetscContainerDestroy(&dummy));
35979566063dSJacob Faibussowitsch         PetscCall(DMClearDS(cdm));
359861a622f3SMatthew G. Knepley       }
359961a622f3SMatthew G. Knepley       mesh->coordFunc = NULL;
360061a622f3SMatthew G. Knepley     }
36019566063dSJacob Faibussowitsch     PetscCall(DMLocalizeCoordinates(dm));
36029566063dSJacob Faibussowitsch     PetscCall(DMGetPeriodicity(dm, &periodic, NULL, NULL, NULL));
36039566063dSJacob Faibussowitsch     if (periodic) PetscCall(DMSetPeriodicity(dm, PETSC_TRUE, NULL, NULL, NULL));
36049318fe57SMatthew G. Knepley   }
360568d4fef7SMatthew G. Knepley   /* Handle DMPlex refinement */
360661a622f3SMatthew G. Knepley   remap = PETSC_TRUE;
36079566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine", "The number of uniform refinements", "DMCreate", refine, &refine, NULL,0));
36089566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_remap", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL));
36099566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine_hierarchy", "The number of uniform refinements", "DMCreate", refine, &refine, &isHierarchy,0));
36109566063dSJacob Faibussowitsch   if (refine) PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
361168d4fef7SMatthew G. Knepley   if (refine && isHierarchy) {
3612acdc6f61SToby Isaac     DM *dms, coarseDM;
361368d4fef7SMatthew G. Knepley 
36149566063dSJacob Faibussowitsch     PetscCall(DMGetCoarseDM(dm, &coarseDM));
36159566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)coarseDM));
36169566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(refine,&dms));
36179566063dSJacob Faibussowitsch     PetscCall(DMRefineHierarchy(dm, refine, dms));
361868d4fef7SMatthew G. Knepley     /* Total hack since we do not pass in a pointer */
36199566063dSJacob Faibussowitsch     PetscCall(DMPlexSwap_Static(dm, dms[refine-1]));
362068d4fef7SMatthew G. Knepley     if (refine == 1) {
36219566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dm, dms[0]));
36229566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dm, PETSC_TRUE));
362368d4fef7SMatthew G. Knepley     } else {
36249566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dm, dms[refine-2]));
36259566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dm, PETSC_TRUE));
36269566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dms[0], dms[refine-1]));
36279566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dms[0], PETSC_TRUE));
362868d4fef7SMatthew G. Knepley     }
36299566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dms[refine-1], coarseDM));
36309566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)coarseDM));
363168d4fef7SMatthew G. Knepley     /* Free DMs */
363268d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
36339566063dSJacob Faibussowitsch       PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]));
36349566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dms[r]));
363568d4fef7SMatthew G. Knepley     }
36369566063dSJacob Faibussowitsch     PetscCall(PetscFree(dms));
363768d4fef7SMatthew G. Knepley   } else {
363868d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
36399318fe57SMatthew G. Knepley       DM             rdm;
364051a74b61SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex*) dm->data)->coordFunc;
364168d4fef7SMatthew G. Knepley 
36429566063dSJacob Faibussowitsch       PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm));
36439566063dSJacob Faibussowitsch       PetscCall(DMRefine(dm, PetscObjectComm((PetscObject) dm), &rdm));
364468d4fef7SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
36459566063dSJacob Faibussowitsch       PetscCall(DMPlexReplace_Static(dm, &rdm));
36469566063dSJacob Faibussowitsch       PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm));
364761a622f3SMatthew G. Knepley       if (coordFunc && remap) {
36489566063dSJacob Faibussowitsch         PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
364951a74b61SMatthew G. Knepley         ((DM_Plex*) dm->data)->coordFunc = coordFunc;
365051a74b61SMatthew G. Knepley       }
365168d4fef7SMatthew G. Knepley     }
365268d4fef7SMatthew G. Knepley   }
36533cf6fe12SMatthew G. Knepley   /* Handle DMPlex coarsening */
36549566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_coarsen", "Coarsen the mesh", "DMCreate", coarsen, &coarsen, NULL,0));
36559566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_coarsen_hierarchy", "The number of coarsenings", "DMCreate", coarsen, &coarsen, &isHierarchy,0));
3656b653a561SMatthew G. Knepley   if (coarsen && isHierarchy) {
3657b653a561SMatthew G. Knepley     DM *dms;
3658b653a561SMatthew G. Knepley 
36599566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(coarsen, &dms));
36609566063dSJacob Faibussowitsch     PetscCall(DMCoarsenHierarchy(dm, coarsen, dms));
3661b653a561SMatthew G. Knepley     /* Free DMs */
3662b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
36639566063dSJacob Faibussowitsch       PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]));
36649566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dms[r]));
3665b653a561SMatthew G. Knepley     }
36669566063dSJacob Faibussowitsch     PetscCall(PetscFree(dms));
3667b653a561SMatthew G. Knepley   } else {
3668b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
36699318fe57SMatthew G. Knepley       DM             cdm;
36709318fe57SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex*) dm->data)->coordFunc;
36713cf6fe12SMatthew G. Knepley 
36729566063dSJacob Faibussowitsch       PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm));
36739566063dSJacob Faibussowitsch       PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject) dm), &cdm));
36743cf6fe12SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
36759566063dSJacob Faibussowitsch       PetscCall(DMPlexReplace_Static(dm, &cdm));
36769566063dSJacob Faibussowitsch       PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm));
36779318fe57SMatthew G. Knepley       if (coordFunc) {
36789566063dSJacob Faibussowitsch         PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
36799318fe57SMatthew G. Knepley         ((DM_Plex*) dm->data)->coordFunc = coordFunc;
36809318fe57SMatthew G. Knepley       }
36813cf6fe12SMatthew G. Knepley     }
3682b653a561SMatthew G. Knepley   }
3683909dfd52SMatthew G. Knepley   /* Handle ghost cells */
36849566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_create_fv_ghost_cells", "Flag to create finite volume ghost cells on the boundary", "DMCreate", ghostCells, &ghostCells, NULL));
3685909dfd52SMatthew G. Knepley   if (ghostCells) {
3686909dfd52SMatthew G. Knepley     DM   gdm;
3687909dfd52SMatthew G. Knepley     char lname[PETSC_MAX_PATH_LEN];
3688909dfd52SMatthew G. Knepley 
3689909dfd52SMatthew G. Knepley     lname[0] = '\0';
36909566063dSJacob Faibussowitsch     PetscCall(PetscOptionsString("-dm_plex_fv_ghost_cells_label", "Label name for ghost cells boundary", "DMCreate", lname, lname, sizeof(lname), &flg));
36919566063dSJacob Faibussowitsch     PetscCall(DMPlexConstructGhostCells(dm, flg ? lname : NULL, NULL, &gdm));
36929566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(dm, &gdm));
3693909dfd52SMatthew G. Knepley   }
36946913077dSMatthew G. Knepley   /* Handle 1D order */
36956bc1bd01Sksagiyam   if (reorder != DMPLEX_REORDER_DEFAULT_FALSE && dim == 1) {
36966913077dSMatthew G. Knepley     DM           cdm, rdm;
36976913077dSMatthew G. Knepley     PetscDS      cds;
36986913077dSMatthew G. Knepley     PetscObject  obj;
36996913077dSMatthew G. Knepley     PetscClassId id = PETSC_OBJECT_CLASSID;
37006913077dSMatthew G. Knepley     IS           perm;
37016bc1bd01Sksagiyam     PetscInt     Nf;
37026913077dSMatthew G. Knepley     PetscBool    distributed;
37036913077dSMatthew G. Knepley 
37049566063dSJacob Faibussowitsch     PetscCall(DMPlexIsDistributed(dm, &distributed));
37059566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
37069566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
37079566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(cds, &Nf));
37086913077dSMatthew G. Knepley     if (Nf) {
37099566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
37109566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
37116913077dSMatthew G. Knepley     }
37126bc1bd01Sksagiyam     if (!distributed && id != PETSCFE_CLASSID) {
37139566063dSJacob Faibussowitsch       PetscCall(DMPlexGetOrdering1D(dm, &perm));
37149566063dSJacob Faibussowitsch       PetscCall(DMPlexPermute(dm, perm, &rdm));
37159566063dSJacob Faibussowitsch       PetscCall(DMPlexReplace_Static(dm, &rdm));
37169566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&perm));
37176913077dSMatthew G. Knepley     }
37186913077dSMatthew G. Knepley   }
37193cf6fe12SMatthew G. Knepley   /* Handle */
37209566063dSJacob Faibussowitsch   PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm));
3721d0609cedSBarry Smith   PetscOptionsHeadEnd();
37220a6ba040SMatthew G. Knepley   PetscFunctionReturn(0);
37230a6ba040SMatthew G. Knepley }
37240a6ba040SMatthew G. Knepley 
3725552f7358SJed Brown static PetscErrorCode DMCreateGlobalVector_Plex(DM dm,Vec *vec)
3726552f7358SJed Brown {
3727552f7358SJed Brown   PetscFunctionBegin;
37289566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector_Section_Private(dm,vec));
37299566063dSJacob Faibussowitsch   /* PetscCall(VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM)); */
37309566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex));
37319566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEWNATIVE, (void (*)(void)) VecView_Plex_Native));
37329566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex));
37339566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOADNATIVE, (void (*)(void)) VecLoad_Plex_Native));
3734552f7358SJed Brown   PetscFunctionReturn(0);
3735552f7358SJed Brown }
3736552f7358SJed Brown 
3737552f7358SJed Brown static PetscErrorCode DMCreateLocalVector_Plex(DM dm,Vec *vec)
3738552f7358SJed Brown {
3739552f7358SJed Brown   PetscFunctionBegin;
37409566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector_Section_Private(dm,vec));
37419566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex_Local));
37429566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex_Local));
3743552f7358SJed Brown   PetscFunctionReturn(0);
3744552f7358SJed Brown }
3745552f7358SJed Brown 
3746793f3fe5SMatthew G. Knepley static PetscErrorCode DMGetDimPoints_Plex(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
3747793f3fe5SMatthew G. Knepley {
3748793f3fe5SMatthew G. Knepley   PetscInt       depth, d;
3749793f3fe5SMatthew G. Knepley 
3750793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
37519566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
3752793f3fe5SMatthew G. Knepley   if (depth == 1) {
37539566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(dm, &d));
37549566063dSJacob Faibussowitsch     if (dim == 0)      PetscCall(DMPlexGetDepthStratum(dm, dim, pStart, pEnd));
37559566063dSJacob Faibussowitsch     else if (dim == d) PetscCall(DMPlexGetDepthStratum(dm, 1, pStart, pEnd));
3756793f3fe5SMatthew G. Knepley     else               {*pStart = 0; *pEnd = 0;}
3757793f3fe5SMatthew G. Knepley   } else {
37589566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, dim, pStart, pEnd));
3759793f3fe5SMatthew G. Knepley   }
3760793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
3761793f3fe5SMatthew G. Knepley }
3762793f3fe5SMatthew G. Knepley 
376328d58a37SPierre Jolivet static PetscErrorCode DMGetNeighbors_Plex(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
3764502a2867SDave May {
3765502a2867SDave May   PetscSF           sf;
37660a19bb7dSprj-   PetscInt          niranks, njranks, n;
37670a19bb7dSprj-   const PetscMPIInt *iranks, *jranks;
37680a19bb7dSprj-   DM_Plex           *data = (DM_Plex*) dm->data;
3769502a2867SDave May 
37702f356facSMatthew G. Knepley   PetscFunctionBegin;
37719566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
37720a19bb7dSprj-   if (!data->neighbors) {
37739566063dSJacob Faibussowitsch     PetscCall(PetscSFSetUp(sf));
37749566063dSJacob Faibussowitsch     PetscCall(PetscSFGetRootRanks(sf, &njranks, &jranks, NULL, NULL, NULL));
37759566063dSJacob Faibussowitsch     PetscCall(PetscSFGetLeafRanks(sf, &niranks, &iranks, NULL, NULL));
37769566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(njranks + niranks + 1, &data->neighbors));
37779566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(data->neighbors + 1, jranks, njranks));
37789566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(data->neighbors + njranks + 1, iranks, niranks));
37790a19bb7dSprj-     n = njranks + niranks;
37809566063dSJacob Faibussowitsch     PetscCall(PetscSortRemoveDupsMPIInt(&n, data->neighbors + 1));
37810a19bb7dSprj-     /* The following cast should never fail: can't have more neighbors than PETSC_MPI_INT_MAX */
37829566063dSJacob Faibussowitsch     PetscCall(PetscMPIIntCast(n, data->neighbors));
37830a19bb7dSprj-   }
37840a19bb7dSprj-   if (nranks) *nranks = data->neighbors[0];
37850a19bb7dSprj-   if (ranks) {
37860a19bb7dSprj-     if (data->neighbors[0]) *ranks = data->neighbors + 1;
37870a19bb7dSprj-     else                    *ranks = NULL;
37880a19bb7dSprj-   }
3789502a2867SDave May   PetscFunctionReturn(0);
3790502a2867SDave May }
3791502a2867SDave May 
37921eb70e55SToby Isaac PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM, DM, Mat, Vec, Vec);
37931eb70e55SToby Isaac 
379446fa42a0SMatthew G. Knepley static PetscErrorCode DMInitialize_Plex(DM dm)
3795552f7358SJed Brown {
3796552f7358SJed Brown   PetscFunctionBegin;
3797552f7358SJed Brown   dm->ops->view                            = DMView_Plex;
37982c40f234SMatthew G. Knepley   dm->ops->load                            = DMLoad_Plex;
3799552f7358SJed Brown   dm->ops->setfromoptions                  = DMSetFromOptions_Plex;
380038221697SMatthew G. Knepley   dm->ops->clone                           = DMClone_Plex;
3801552f7358SJed Brown   dm->ops->setup                           = DMSetUp_Plex;
38021bb6d2a8SBarry Smith   dm->ops->createlocalsection              = DMCreateLocalSection_Plex;
380366ad2231SToby Isaac   dm->ops->createdefaultconstraints        = DMCreateDefaultConstraints_Plex;
3804552f7358SJed Brown   dm->ops->createglobalvector              = DMCreateGlobalVector_Plex;
3805552f7358SJed Brown   dm->ops->createlocalvector               = DMCreateLocalVector_Plex;
3806184d77edSJed Brown   dm->ops->getlocaltoglobalmapping         = NULL;
38070298fd71SBarry Smith   dm->ops->createfieldis                   = NULL;
3808552f7358SJed Brown   dm->ops->createcoordinatedm              = DMCreateCoordinateDM_Plex;
3809f19dbd58SToby Isaac   dm->ops->createcoordinatefield           = DMCreateCoordinateField_Plex;
38100a6ba040SMatthew G. Knepley   dm->ops->getcoloring                     = NULL;
3811552f7358SJed Brown   dm->ops->creatematrix                    = DMCreateMatrix_Plex;
3812bceba477SMatthew G. Knepley   dm->ops->createinterpolation             = DMCreateInterpolation_Plex;
3813bd041c0cSMatthew G. Knepley   dm->ops->createmassmatrix                = DMCreateMassMatrix_Plex;
3814b4937a87SMatthew G. Knepley   dm->ops->createmassmatrixlumped          = DMCreateMassMatrixLumped_Plex;
38155a84ad33SLisandro Dalcin   dm->ops->createinjection                 = DMCreateInjection_Plex;
3816552f7358SJed Brown   dm->ops->refine                          = DMRefine_Plex;
38170a6ba040SMatthew G. Knepley   dm->ops->coarsen                         = DMCoarsen_Plex;
38180a6ba040SMatthew G. Knepley   dm->ops->refinehierarchy                 = DMRefineHierarchy_Plex;
3819b653a561SMatthew G. Knepley   dm->ops->coarsenhierarchy                = DMCoarsenHierarchy_Plex;
3820d410b0cfSMatthew G. Knepley   dm->ops->extrude                         = DMExtrude_Plex;
38210298fd71SBarry Smith   dm->ops->globaltolocalbegin              = NULL;
38220298fd71SBarry Smith   dm->ops->globaltolocalend                = NULL;
38230298fd71SBarry Smith   dm->ops->localtoglobalbegin              = NULL;
38240298fd71SBarry Smith   dm->ops->localtoglobalend                = NULL;
3825552f7358SJed Brown   dm->ops->destroy                         = DMDestroy_Plex;
3826552f7358SJed Brown   dm->ops->createsubdm                     = DMCreateSubDM_Plex;
38272adcc780SMatthew G. Knepley   dm->ops->createsuperdm                   = DMCreateSuperDM_Plex;
3828793f3fe5SMatthew G. Knepley   dm->ops->getdimpoints                    = DMGetDimPoints_Plex;
3829552f7358SJed Brown   dm->ops->locatepoints                    = DMLocatePoints_Plex;
38300709b2feSToby Isaac   dm->ops->projectfunctionlocal            = DMProjectFunctionLocal_Plex;
38310709b2feSToby Isaac   dm->ops->projectfunctionlabellocal       = DMProjectFunctionLabelLocal_Plex;
3832bfc4295aSToby Isaac   dm->ops->projectfieldlocal               = DMProjectFieldLocal_Plex;
38338c6c5593SMatthew G. Knepley   dm->ops->projectfieldlabellocal          = DMProjectFieldLabelLocal_Plex;
3834ece3a9fcSMatthew G. Knepley   dm->ops->projectbdfieldlabellocal        = DMProjectBdFieldLabelLocal_Plex;
38350709b2feSToby Isaac   dm->ops->computel2diff                   = DMComputeL2Diff_Plex;
3836b698f381SToby Isaac   dm->ops->computel2gradientdiff           = DMComputeL2GradientDiff_Plex;
38372a16baeaSToby Isaac   dm->ops->computel2fielddiff              = DMComputeL2FieldDiff_Plex;
383828d58a37SPierre Jolivet   dm->ops->getneighbors                    = DMGetNeighbors_Plex;
38399566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C",DMPlexInsertBoundaryValues_Plex));
38409566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertTimeDerviativeBoundaryValues_C",DMPlexInsertTimeDerivativeBoundaryValues_Plex));
38419566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",DMSetUpGLVisViewer_Plex));
38429566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMCreateNeumannOverlap_C",DMCreateNeumannOverlap_Plex));
38439566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMPlexGetOverlap_C",DMPlexGetOverlap_Plex));
38449566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMPlexDistributeGetDefault_C",DMPlexDistributeGetDefault_Plex));
38459566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMPlexDistributeSetDefault_C",DMPlexDistributeSetDefault_Plex));
38466bc1bd01Sksagiyam   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMPlexReorderGetDefault_C",DMPlexReorderGetDefault_Plex));
38476bc1bd01Sksagiyam   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMPlexReorderSetDefault_C",DMPlexReorderSetDefault_Plex));
38489566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMInterpolateSolution_C",DMInterpolateSolution_Plex));
3849c506a872SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMPlexGetOverlap_C",DMPlexGetOverlap_Plex));
3850c506a872SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMPlexSetOverlap_C",DMPlexSetOverlap_Plex));
3851552f7358SJed Brown   PetscFunctionReturn(0);
3852552f7358SJed Brown }
3853552f7358SJed Brown 
385446fa42a0SMatthew G. Knepley PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm)
385563a16f15SMatthew G. Knepley {
385663a16f15SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex *) dm->data;
385763a16f15SMatthew G. Knepley 
385863a16f15SMatthew G. Knepley   PetscFunctionBegin;
385963a16f15SMatthew G. Knepley   mesh->refct++;
386063a16f15SMatthew G. Knepley   (*newdm)->data = mesh;
38619566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject) *newdm, DMPLEX));
38629566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(*newdm));
386363a16f15SMatthew G. Knepley   PetscFunctionReturn(0);
386463a16f15SMatthew G. Knepley }
386563a16f15SMatthew G. Knepley 
38668818961aSMatthew G Knepley /*MC
38678818961aSMatthew G Knepley   DMPLEX = "plex" - A DM object that encapsulates an unstructured mesh, or CW Complex, which can be expressed using a Hasse Diagram.
38688818961aSMatthew G Knepley                     In the local representation, Vecs contain all unknowns in the interior and shared boundary. This is
38698818961aSMatthew G Knepley                     specified by a PetscSection object. Ownership in the global representation is determined by
38708818961aSMatthew G Knepley                     ownership of the underlying DMPlex points. This is specified by another PetscSection object.
38718818961aSMatthew G Knepley 
3872e5893cccSMatthew G. Knepley   Options Database Keys:
3873250712c9SMatthew G. Knepley + -dm_refine_pre                     - Refine mesh before distribution
3874250712c9SMatthew G. Knepley + -dm_refine_uniform_pre             - Choose uniform or generator-based refinement
3875250712c9SMatthew G. Knepley + -dm_refine_volume_limit_pre        - Cell volume limit after pre-refinement using generator
3876250712c9SMatthew G. Knepley . -dm_distribute                     - Distribute mesh across processes
3877250712c9SMatthew G. Knepley . -dm_distribute_overlap             - Number of cells to overlap for distribution
3878250712c9SMatthew G. Knepley . -dm_refine                         - Refine mesh after distribution
3879250712c9SMatthew G. Knepley . -dm_plex_hash_location             - Use grid hashing for point location
3880ddce0771SMatthew G. Knepley . -dm_plex_hash_box_faces <n,m,p>    - The number of divisions in each direction of the grid hash
3881f12cf164SMatthew G. Knepley . -dm_plex_partition_balance         - Attempt to evenly divide points on partition boundary between processes
3882f12cf164SMatthew G. Knepley . -dm_plex_remesh_bd                 - Allow changes to the boundary on remeshing
3883f12cf164SMatthew G. Knepley . -dm_plex_max_projection_height     - Maxmimum mesh point height used to project locally
3884f12cf164SMatthew G. Knepley . -dm_plex_regular_refinement        - Use special nested projection algorithm for regular refinement
3885250712c9SMatthew G. Knepley . -dm_plex_check_all                 - Perform all shecks below
3886f12cf164SMatthew G. Knepley . -dm_plex_check_symmetry            - Check that the adjacency information in the mesh is symmetric
3887f12cf164SMatthew G. Knepley . -dm_plex_check_skeleton <celltype> - Check that each cell has the correct number of vertices
3888f12cf164SMatthew 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
3889f12cf164SMatthew G. Knepley . -dm_plex_check_geometry            - Check that cells have positive volume
3890f12cf164SMatthew G. Knepley . -dm_view :mesh.tex:ascii_latex     - View the mesh in LaTeX/TikZ
3891e5893cccSMatthew G. Knepley . -dm_plex_view_scale <num>          - Scale the TikZ
3892e5893cccSMatthew G. Knepley - -dm_plex_print_fem <num>           - View FEM assembly information, such as element vectors and matrices
3893e5893cccSMatthew G. Knepley 
38948818961aSMatthew G Knepley   Level: intermediate
38958818961aSMatthew G Knepley 
3896db781477SPatrick Sanan .seealso: `DMType`, `DMPlexCreate()`, `DMCreate()`, `DMSetType()`
38978818961aSMatthew G Knepley M*/
38988818961aSMatthew G Knepley 
38998cc058d9SJed Brown PETSC_EXTERN PetscErrorCode DMCreate_Plex(DM dm)
3900552f7358SJed Brown {
3901552f7358SJed Brown   DM_Plex       *mesh;
3902412e9a14SMatthew G. Knepley   PetscInt       unit;
3903552f7358SJed Brown 
3904552f7358SJed Brown   PetscFunctionBegin;
3905552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39069566063dSJacob Faibussowitsch   PetscCall(PetscNewLog(dm,&mesh));
3907552f7358SJed Brown   dm->data = mesh;
3908552f7358SJed Brown 
3909552f7358SJed Brown   mesh->refct             = 1;
39109566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->coneSection));
39119566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->supportSection));
3912552f7358SJed Brown   mesh->refinementUniform = PETSC_TRUE;
3913552f7358SJed Brown   mesh->refinementLimit   = -1.0;
3914e600fa54SMatthew G. Knepley   mesh->distDefault       = PETSC_TRUE;
39156bc1bd01Sksagiyam   mesh->reorderDefault    = DMPLEX_REORDER_DEFAULT_NOTSET;
39167d0f5628SVaclav Hapla   mesh->interpolated      = DMPLEX_INTERPOLATED_INVALID;
39177d0f5628SVaclav Hapla   mesh->interpolatedCollective = DMPLEX_INTERPOLATED_INVALID;
3918552f7358SJed Brown 
39199566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerCreate(PetscObjectComm((PetscObject)dm), &mesh->partitioner));
39202e62ab5aSMatthew G. Knepley   mesh->remeshBd     = PETSC_FALSE;
3921d9deefdfSMatthew G. Knepley 
39228865f1eaSKarl Rupp   for (unit = 0; unit < NUM_PETSC_UNITS; ++unit) mesh->scale[unit] = 1.0;
3923552f7358SJed Brown 
3924df0420ecSMatthew G. Knepley   mesh->depthState          = -1;
3925ba2698f1SMatthew G. Knepley   mesh->celltypeState       = -1;
39266113b454SMatthew G. Knepley   mesh->printTol            = 1.0e-10;
3927552f7358SJed Brown 
39289566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(dm));
3929552f7358SJed Brown   PetscFunctionReturn(0);
3930552f7358SJed Brown }
3931552f7358SJed Brown 
3932552f7358SJed Brown /*@
3933552f7358SJed Brown   DMPlexCreate - Creates a DMPlex object, which encapsulates an unstructured mesh, or CW complex, which can be expressed using a Hasse Diagram.
3934552f7358SJed Brown 
3935d083f849SBarry Smith   Collective
3936552f7358SJed Brown 
3937552f7358SJed Brown   Input Parameter:
3938552f7358SJed Brown . comm - The communicator for the DMPlex object
3939552f7358SJed Brown 
3940552f7358SJed Brown   Output Parameter:
3941552f7358SJed Brown . mesh  - The DMPlex object
3942552f7358SJed Brown 
3943552f7358SJed Brown   Level: beginner
3944552f7358SJed Brown 
3945552f7358SJed Brown @*/
3946552f7358SJed Brown PetscErrorCode DMPlexCreate(MPI_Comm comm, DM *mesh)
3947552f7358SJed Brown {
3948552f7358SJed Brown   PetscFunctionBegin;
3949552f7358SJed Brown   PetscValidPointer(mesh,2);
39509566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, mesh));
39519566063dSJacob Faibussowitsch   PetscCall(DMSetType(*mesh, DMPLEX));
3952552f7358SJed Brown   PetscFunctionReturn(0);
3953552f7358SJed Brown }
3954552f7358SJed Brown 
3955b09969d6SVaclav Hapla /*@C
3956b09969d6SVaclav Hapla   DMPlexBuildFromCellListParallel - Build distributed DMPLEX topology from a list of vertices for each cell (common mesh generator output)
3957b09969d6SVaclav Hapla 
3958b09969d6SVaclav Hapla   Input Parameters:
3959b09969d6SVaclav Hapla + dm - The DM
3960b09969d6SVaclav Hapla . numCells - The number of cells owned by this process
3961325d53feSBarry Smith . numVertices - The number of vertices to be owned by this process, or PETSC_DECIDE
3962325d53feSBarry Smith . NVertices - The global number of vertices, or PETSC_DETERMINE
3963b09969d6SVaclav Hapla . numCorners - The number of vertices for each cell
39645e488331SVaclav Hapla - cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
3965b09969d6SVaclav Hapla 
3966be8c289dSNicolas Barral   Output Parameters:
3967be8c289dSNicolas Barral + vertexSF - (Optional) SF describing complete vertex ownership
3968be8c289dSNicolas Barral - verticesAdjSaved - (Optional) vertex adjacency array
3969b09969d6SVaclav Hapla 
3970b09969d6SVaclav Hapla   Notes:
3971b09969d6SVaclav Hapla   Two triangles sharing a face
3972b09969d6SVaclav Hapla $
3973b09969d6SVaclav Hapla $        2
3974b09969d6SVaclav Hapla $      / | \
3975b09969d6SVaclav Hapla $     /  |  \
3976b09969d6SVaclav Hapla $    /   |   \
3977b09969d6SVaclav Hapla $   0  0 | 1  3
3978b09969d6SVaclav Hapla $    \   |   /
3979b09969d6SVaclav Hapla $     \  |  /
3980b09969d6SVaclav Hapla $      \ | /
3981b09969d6SVaclav Hapla $        1
3982b09969d6SVaclav Hapla would have input
3983b09969d6SVaclav Hapla $  numCells = 2, numVertices = 4
3984b09969d6SVaclav Hapla $  cells = [0 1 2  1 3 2]
3985b09969d6SVaclav Hapla $
3986b09969d6SVaclav Hapla which would result in the DMPlex
3987b09969d6SVaclav Hapla $
3988b09969d6SVaclav Hapla $        4
3989b09969d6SVaclav Hapla $      / | \
3990b09969d6SVaclav Hapla $     /  |  \
3991b09969d6SVaclav Hapla $    /   |   \
3992b09969d6SVaclav Hapla $   2  0 | 1  5
3993b09969d6SVaclav Hapla $    \   |   /
3994b09969d6SVaclav Hapla $     \  |  /
3995b09969d6SVaclav Hapla $      \ | /
3996b09969d6SVaclav Hapla $        3
3997b09969d6SVaclav Hapla 
399825b6865aSVaclav Hapla   Vertices are implicitly numbered consecutively 0,...,NVertices.
399925b6865aSVaclav Hapla   Each rank owns a chunk of numVertices consecutive vertices.
400025b6865aSVaclav Hapla   If numVertices is PETSC_DECIDE, PETSc will distribute them as evenly as possible using PetscLayout.
4001325d53feSBarry Smith   If NVertices is PETSC_DETERMINE and numVertices is PETSC_DECIDE, NVertices is computed by PETSc as the maximum vertex index in cells + 1.
4002325d53feSBarry Smith   If only NVertices is PETSC_DETERMINE, it is computed as the sum of numVertices over all ranks.
400325b6865aSVaclav Hapla 
4004b09969d6SVaclav Hapla   The cell distribution is arbitrary non-overlapping, independent of the vertex distribution.
4005b09969d6SVaclav Hapla 
4006b09969d6SVaclav Hapla   Not currently supported in Fortran.
4007b09969d6SVaclav Hapla 
4008b09969d6SVaclav Hapla   Level: advanced
4009b09969d6SVaclav Hapla 
4010db781477SPatrick Sanan .seealso: `DMPlexBuildFromCellList()`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildCoordinatesFromCellListParallel()`
4011b09969d6SVaclav Hapla @*/
4012be8c289dSNicolas Barral PetscErrorCode DMPlexBuildFromCellListParallel(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt NVertices, PetscInt numCorners, const PetscInt cells[], PetscSF *vertexSF, PetscInt **verticesAdjSaved)
4013a47d0d45SMatthew G. Knepley {
40142464107aSksagiyam   PetscSF         sfPoint;
40152464107aSksagiyam   PetscLayout     layout;
401682fb893eSVaclav Hapla   PetscInt        numVerticesAdj, *verticesAdj, *cones, c, p;
4017a47d0d45SMatthew G. Knepley 
4018a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
401925b6865aSVaclav Hapla   PetscValidLogicalCollectiveInt(dm,NVertices,4);
40209566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList,dm,0,0,0));
402125b6865aSVaclav Hapla   /* Get/check global number of vertices */
402225b6865aSVaclav Hapla   {
402325b6865aSVaclav Hapla     PetscInt NVerticesInCells, i;
402425b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
402525b6865aSVaclav Hapla 
402625b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
402725b6865aSVaclav Hapla     NVerticesInCells = PETSC_MIN_INT;
402825b6865aSVaclav Hapla     for (i=0; i<len; i++) if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
402925b6865aSVaclav Hapla     ++NVerticesInCells;
40309566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &NVerticesInCells, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm)));
403125b6865aSVaclav Hapla 
403225b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE && NVertices == PETSC_DECIDE) NVertices = NVerticesInCells;
40331dca8a05SBarry Smith     else PetscCheck(NVertices == PETSC_DECIDE || NVertices >= NVerticesInCells,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Specified global number of vertices %" PetscInt_FMT " must be greater than or equal to the number of vertices in cells %" PetscInt_FMT,NVertices,NVerticesInCells);
403425b6865aSVaclav Hapla   }
40359079aca8SVaclav Hapla   /* Count locally unique vertices */
40369079aca8SVaclav Hapla   {
40379079aca8SVaclav Hapla     PetscHSetI vhash;
40389079aca8SVaclav Hapla     PetscInt off = 0;
40399079aca8SVaclav Hapla 
40409566063dSJacob Faibussowitsch     PetscCall(PetscHSetICreate(&vhash));
4041a47d0d45SMatthew G. Knepley     for (c = 0; c < numCells; ++c) {
4042a47d0d45SMatthew G. Knepley       for (p = 0; p < numCorners; ++p) {
40439566063dSJacob Faibussowitsch         PetscCall(PetscHSetIAdd(vhash, cells[c*numCorners+p]));
4044a47d0d45SMatthew G. Knepley       }
4045a47d0d45SMatthew G. Knepley     }
40469566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetSize(vhash, &numVerticesAdj));
40479566063dSJacob Faibussowitsch     if (!verticesAdjSaved) PetscCall(PetscMalloc1(numVerticesAdj, &verticesAdj));
4048be8c289dSNicolas Barral     else { verticesAdj = *verticesAdjSaved; }
40499566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetElems(vhash, &off, verticesAdj));
40509566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&vhash));
405163a3b9bcSJacob Faibussowitsch     PetscCheck(off == numVerticesAdj,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid number of local vertices %" PetscInt_FMT " should be %" PetscInt_FMT, off, numVerticesAdj);
4052a47d0d45SMatthew G. Knepley   }
40539566063dSJacob Faibussowitsch   PetscCall(PetscSortInt(numVerticesAdj, verticesAdj));
4054a47d0d45SMatthew G. Knepley   /* Create cones */
40559566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, 0, numCells+numVerticesAdj));
40569566063dSJacob Faibussowitsch   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, numCorners));
40579566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm));
40589566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCones(dm,&cones));
4059a47d0d45SMatthew G. Knepley   for (c = 0; c < numCells; ++c) {
4060a47d0d45SMatthew G. Knepley     for (p = 0; p < numCorners; ++p) {
4061a47d0d45SMatthew G. Knepley       const PetscInt gv = cells[c*numCorners+p];
4062a47d0d45SMatthew G. Knepley       PetscInt       lv;
4063a47d0d45SMatthew G. Knepley 
40649079aca8SVaclav Hapla       /* Positions within verticesAdj form 0-based local vertex numbering;
40659079aca8SVaclav Hapla          we need to shift it by numCells to get correct DAG points (cells go first) */
40669566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv));
406763a3b9bcSJacob Faibussowitsch       PetscCheck(lv >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not find global vertex %" PetscInt_FMT " in local connectivity", gv);
4068961cfab0SVaclav Hapla       cones[c*numCorners+p] = lv+numCells;
4069a47d0d45SMatthew G. Knepley     }
4070a47d0d45SMatthew G. Knepley   }
40712464107aSksagiyam   /* Build point sf */
40729566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(PetscObjectComm((PetscObject)dm), &layout));
40739566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetSize(layout, NVertices));
40749566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, numVertices));
40759566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
40769566063dSJacob Faibussowitsch   PetscCall(PetscSFCreateByMatchingIndices(layout, numVerticesAdj, verticesAdj, NULL, numCells, numVerticesAdj, verticesAdj, NULL, numCells, vertexSF, &sfPoint));
40779566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
40789566063dSJacob Faibussowitsch   if (!verticesAdjSaved) PetscCall(PetscFree(verticesAdj));
40799566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject) sfPoint, "point SF"));
40802464107aSksagiyam   if (dm->sf) {
40812464107aSksagiyam     const char *prefix;
40822464107aSksagiyam 
40839566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm->sf, &prefix));
40849566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)sfPoint, prefix));
40852464107aSksagiyam   }
40869566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dm, sfPoint));
40879566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&sfPoint));
40889566063dSJacob Faibussowitsch   if (vertexSF) PetscCall(PetscObjectSetName((PetscObject)(*vertexSF), "Vertex Ownership SF"));
4089a47d0d45SMatthew G. Knepley   /* Fill in the rest of the topology structure */
40909566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
40919566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
40929566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList,dm,0,0,0));
4093a47d0d45SMatthew G. Knepley   PetscFunctionReturn(0);
4094a47d0d45SMatthew G. Knepley }
4095a47d0d45SMatthew G. Knepley 
4096b09969d6SVaclav Hapla /*@C
4097b09969d6SVaclav Hapla   DMPlexBuildCoordinatesFromCellListParallel - Build DM coordinates from a list of coordinates for each owned vertex (common mesh generator output)
4098b09969d6SVaclav Hapla 
4099b09969d6SVaclav Hapla   Input Parameters:
4100b09969d6SVaclav Hapla + dm - The DM
4101b09969d6SVaclav Hapla . spaceDim - The spatial dimension used for coordinates
4102b09969d6SVaclav Hapla . sfVert - SF describing complete vertex ownership
4103b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
4104b09969d6SVaclav Hapla 
4105b09969d6SVaclav Hapla   Level: advanced
4106b09969d6SVaclav Hapla 
4107b09969d6SVaclav Hapla   Notes:
4108b09969d6SVaclav Hapla   Not currently supported in Fortran.
4109b09969d6SVaclav Hapla 
4110db781477SPatrick Sanan .seealso: `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildFromCellListParallel()`
4111b09969d6SVaclav Hapla @*/
41121edcf0b2SVaclav Hapla PetscErrorCode DMPlexBuildCoordinatesFromCellListParallel(DM dm, PetscInt spaceDim, PetscSF sfVert, const PetscReal vertexCoords[])
4113a47d0d45SMatthew G. Knepley {
4114a47d0d45SMatthew G. Knepley   PetscSection   coordSection;
4115a47d0d45SMatthew G. Knepley   Vec            coordinates;
4116a47d0d45SMatthew G. Knepley   PetscScalar   *coords;
41171edcf0b2SVaclav Hapla   PetscInt       numVertices, numVerticesAdj, coordSize, v, vStart, vEnd;
4118a47d0d45SMatthew G. Knepley 
4119a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
41209566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0));
41219566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
41221dca8a05SBarry Smith   PetscCheck(vStart >= 0 && vEnd >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
41239566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, spaceDim));
41249566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfVert, &numVertices, &numVerticesAdj, NULL, NULL));
41251dca8a05SBarry Smith   PetscCheck(vEnd - vStart == numVerticesAdj,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Supplied sfVert has wrong number of leaves = %" PetscInt_FMT " != %" PetscInt_FMT " = vEnd - vStart",numVerticesAdj,vEnd - vStart);
41269566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
41279566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
41289566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, spaceDim));
41299566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, vStart, vEnd));
41301edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
41319566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, spaceDim));
41329566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, spaceDim));
4133a47d0d45SMatthew G. Knepley   }
41349566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
41359566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
41369566063dSJacob Faibussowitsch   PetscCall(VecCreate(PetscObjectComm((PetscObject)dm), &coordinates));
41379566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, spaceDim));
41389566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
41399566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
41409566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates,VECSTANDARD));
41419566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
4142a47d0d45SMatthew G. Knepley   {
4143a47d0d45SMatthew G. Knepley     MPI_Datatype coordtype;
4144a47d0d45SMatthew G. Knepley 
4145a47d0d45SMatthew G. Knepley     /* Need a temp buffer for coords if we have complex/single */
41469566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_contiguous(spaceDim, MPIU_SCALAR, &coordtype));
41479566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_commit(&coordtype));
414821016a8bSBarry Smith #if defined(PETSC_USE_COMPLEX)
414921016a8bSBarry Smith     {
415021016a8bSBarry Smith     PetscScalar *svertexCoords;
415121016a8bSBarry Smith     PetscInt    i;
41529566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numVertices*spaceDim,&svertexCoords));
41533612f820SVaclav Hapla     for (i=0; i<numVertices*spaceDim; i++) svertexCoords[i] = vertexCoords[i];
41549566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sfVert, coordtype, svertexCoords, coords,MPI_REPLACE));
41559566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sfVert, coordtype, svertexCoords, coords,MPI_REPLACE));
41569566063dSJacob Faibussowitsch     PetscCall(PetscFree(svertexCoords));
415721016a8bSBarry Smith     }
415821016a8bSBarry Smith #else
41599566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sfVert, coordtype, vertexCoords, coords,MPI_REPLACE));
41609566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sfVert, coordtype, vertexCoords, coords,MPI_REPLACE));
416121016a8bSBarry Smith #endif
41629566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_free(&coordtype));
4163a47d0d45SMatthew G. Knepley   }
41649566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
41659566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
41669566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
41679566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0));
4168a47d0d45SMatthew G. Knepley   PetscFunctionReturn(0);
4169a47d0d45SMatthew G. Knepley }
4170a47d0d45SMatthew G. Knepley 
4171c3edce3dSSatish Balay /*@
4172b09969d6SVaclav Hapla   DMPlexCreateFromCellListParallelPetsc - Create distributed DMPLEX from a list of vertices for each cell (common mesh generator output)
4173a47d0d45SMatthew G. Knepley 
4174a47d0d45SMatthew G. Knepley   Input Parameters:
4175a47d0d45SMatthew G. Knepley + comm - The communicator
4176a47d0d45SMatthew G. Knepley . dim - The topological dimension of the mesh
4177a47d0d45SMatthew G. Knepley . numCells - The number of cells owned by this process
417825b6865aSVaclav Hapla . numVertices - The number of vertices owned by this process, or PETSC_DECIDE
417925b6865aSVaclav Hapla . NVertices - The global number of vertices, or PETSC_DECIDE
4180a47d0d45SMatthew G. Knepley . numCorners - The number of vertices for each cell
4181a47d0d45SMatthew G. Knepley . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
4182a47d0d45SMatthew G. Knepley . cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
4183a47d0d45SMatthew G. Knepley . spaceDim - The spatial dimension used for coordinates
4184a47d0d45SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
4185a47d0d45SMatthew G. Knepley 
4186d8d19677SJose E. Roman   Output Parameters:
418718d54ad4SMichael Lange + dm - The DM
4188be8c289dSNicolas Barral . vertexSF - (Optional) SF describing complete vertex ownership
4189be8c289dSNicolas Barral - verticesAdjSaved - (Optional) vertex adjacency array
4190a47d0d45SMatthew G. Knepley 
4191b09969d6SVaclav Hapla   Notes:
4192b09969d6SVaclav Hapla   This function is just a convenient sequence of DMCreate(), DMSetType(), DMSetDimension(),
4193b09969d6SVaclav Hapla   DMPlexBuildFromCellListParallel(), DMPlexInterpolate(), DMPlexBuildCoordinatesFromCellListParallel()
4194a47d0d45SMatthew G. Knepley 
419525b6865aSVaclav Hapla   See DMPlexBuildFromCellListParallel() for an example and details about the topology-related parameters.
419625b6865aSVaclav Hapla   See DMPlexBuildCoordinatesFromCellListParallel() for details about the geometry-related parameters.
419725b6865aSVaclav Hapla 
4198b09969d6SVaclav Hapla   Level: intermediate
4199a47d0d45SMatthew G. Knepley 
4200db781477SPatrick Sanan .seealso: `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()`
4201a47d0d45SMatthew G. Knepley @*/
4202be8c289dSNicolas 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)
4203a47d0d45SMatthew G. Knepley {
4204a47d0d45SMatthew G. Knepley   PetscSF        sfVert;
4205a47d0d45SMatthew G. Knepley 
4206a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
42079566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
42089566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
4209a47d0d45SMatthew G. Knepley   PetscValidLogicalCollectiveInt(*dm, dim, 2);
4210064a246eSJacob Faibussowitsch   PetscValidLogicalCollectiveInt(*dm, spaceDim, 9);
42119566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
42129566063dSJacob Faibussowitsch   PetscCall(DMPlexBuildFromCellListParallel(*dm, numCells, numVertices, NVertices, numCorners, cells, &sfVert, verticesAdj));
4213a47d0d45SMatthew G. Knepley   if (interpolate) {
42145fd9971aSMatthew G. Knepley     DM idm;
4215a47d0d45SMatthew G. Knepley 
42169566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
42179566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
4218a47d0d45SMatthew G. Knepley     *dm  = idm;
4219a47d0d45SMatthew G. Knepley   }
42209566063dSJacob Faibussowitsch   PetscCall(DMPlexBuildCoordinatesFromCellListParallel(*dm, spaceDim, sfVert, vertexCoords));
422118d54ad4SMichael Lange   if (vertexSF) *vertexSF = sfVert;
42229566063dSJacob Faibussowitsch   else PetscCall(PetscSFDestroy(&sfVert));
4223a47d0d45SMatthew G. Knepley   PetscFunctionReturn(0);
4224a47d0d45SMatthew G. Knepley }
4225a47d0d45SMatthew G. Knepley 
4226b09969d6SVaclav Hapla /*@C
4227b09969d6SVaclav Hapla   DMPlexBuildFromCellList - Build DMPLEX topology from a list of vertices for each cell (common mesh generator output)
42289298eaa6SMatthew G Knepley 
42299298eaa6SMatthew G Knepley   Input Parameters:
4230b09969d6SVaclav Hapla + dm - The DM
4231b09969d6SVaclav Hapla . numCells - The number of cells owned by this process
4232325d53feSBarry Smith . numVertices - The number of vertices owned by this process, or PETSC_DETERMINE
42339298eaa6SMatthew G Knepley . numCorners - The number of vertices for each cell
42345e488331SVaclav Hapla - cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
42359298eaa6SMatthew G Knepley 
4236b09969d6SVaclav Hapla   Level: advanced
42379298eaa6SMatthew G Knepley 
4238b09969d6SVaclav Hapla   Notes:
4239b09969d6SVaclav Hapla   Two triangles sharing a face
42409298eaa6SMatthew G Knepley $
42419298eaa6SMatthew G Knepley $        2
42429298eaa6SMatthew G Knepley $      / | \
42439298eaa6SMatthew G Knepley $     /  |  \
42449298eaa6SMatthew G Knepley $    /   |   \
42459298eaa6SMatthew G Knepley $   0  0 | 1  3
42469298eaa6SMatthew G Knepley $    \   |   /
42479298eaa6SMatthew G Knepley $     \  |  /
42489298eaa6SMatthew G Knepley $      \ | /
42499298eaa6SMatthew G Knepley $        1
42509298eaa6SMatthew G Knepley would have input
42519298eaa6SMatthew G Knepley $  numCells = 2, numVertices = 4
42529298eaa6SMatthew G Knepley $  cells = [0 1 2  1 3 2]
42539298eaa6SMatthew G Knepley $
42549298eaa6SMatthew G Knepley which would result in the DMPlex
42559298eaa6SMatthew G Knepley $
42569298eaa6SMatthew G Knepley $        4
42579298eaa6SMatthew G Knepley $      / | \
42589298eaa6SMatthew G Knepley $     /  |  \
42599298eaa6SMatthew G Knepley $    /   |   \
42609298eaa6SMatthew G Knepley $   2  0 | 1  5
42619298eaa6SMatthew G Knepley $    \   |   /
42629298eaa6SMatthew G Knepley $     \  |  /
42639298eaa6SMatthew G Knepley $      \ | /
42649298eaa6SMatthew G Knepley $        3
42659298eaa6SMatthew G Knepley 
4266325d53feSBarry Smith   If numVertices is PETSC_DETERMINE, it is computed by PETSc as the maximum vertex index in cells + 1.
426725b6865aSVaclav Hapla 
4268b09969d6SVaclav Hapla   Not currently supported in Fortran.
42699298eaa6SMatthew G Knepley 
4270db781477SPatrick Sanan .seealso: `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromCellListPetsc()`
4271b09969d6SVaclav Hapla @*/
42725e488331SVaclav Hapla PetscErrorCode DMPlexBuildFromCellList(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const PetscInt cells[])
4273b09969d6SVaclav Hapla {
4274961cfab0SVaclav Hapla   PetscInt      *cones, c, p, dim;
4275b09969d6SVaclav Hapla 
4276b09969d6SVaclav Hapla   PetscFunctionBegin;
42779566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList,dm,0,0,0));
42789566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
427925b6865aSVaclav Hapla   /* Get/check global number of vertices */
428025b6865aSVaclav Hapla   {
428125b6865aSVaclav Hapla     PetscInt NVerticesInCells, i;
428225b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
428325b6865aSVaclav Hapla 
428425b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
428525b6865aSVaclav Hapla     NVerticesInCells = PETSC_MIN_INT;
428625b6865aSVaclav Hapla     for (i=0; i<len; i++) if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
428725b6865aSVaclav Hapla     ++NVerticesInCells;
428825b6865aSVaclav Hapla 
428925b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE) numVertices = NVerticesInCells;
429063a3b9bcSJacob Faibussowitsch     else PetscCheck(numVertices >= NVerticesInCells,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONG, "Specified number of vertices %" PetscInt_FMT " must be greater than or equal to the number of vertices in cells %" PetscInt_FMT,numVertices,NVerticesInCells);
429125b6865aSVaclav Hapla   }
42929566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, 0, numCells+numVertices));
4293b09969d6SVaclav Hapla   for (c = 0; c < numCells; ++c) {
42949566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeSize(dm, c, numCorners));
4295b09969d6SVaclav Hapla   }
42969566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm));
42979566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCones(dm,&cones));
4298b09969d6SVaclav Hapla   for (c = 0; c < numCells; ++c) {
4299b09969d6SVaclav Hapla     for (p = 0; p < numCorners; ++p) {
4300961cfab0SVaclav Hapla       cones[c*numCorners+p] = cells[c*numCorners+p]+numCells;
4301b09969d6SVaclav Hapla     }
4302b09969d6SVaclav Hapla   }
43039566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
43049566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
43059566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList,dm,0,0,0));
4306b09969d6SVaclav Hapla   PetscFunctionReturn(0);
4307b09969d6SVaclav Hapla }
4308b09969d6SVaclav Hapla 
4309b09969d6SVaclav Hapla /*@C
4310b09969d6SVaclav Hapla   DMPlexBuildCoordinatesFromCellList - Build DM coordinates from a list of coordinates for each owned vertex (common mesh generator output)
4311b09969d6SVaclav Hapla 
4312b09969d6SVaclav Hapla   Input Parameters:
4313b09969d6SVaclav Hapla + dm - The DM
4314b09969d6SVaclav Hapla . spaceDim - The spatial dimension used for coordinates
4315b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
4316b09969d6SVaclav Hapla 
4317b09969d6SVaclav Hapla   Level: advanced
4318b09969d6SVaclav Hapla 
4319b09969d6SVaclav Hapla   Notes:
4320b09969d6SVaclav Hapla   Not currently supported in Fortran.
4321b09969d6SVaclav Hapla 
4322db781477SPatrick Sanan .seealso: `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellList()`
4323b09969d6SVaclav Hapla @*/
43241edcf0b2SVaclav Hapla PetscErrorCode DMPlexBuildCoordinatesFromCellList(DM dm, PetscInt spaceDim, const PetscReal vertexCoords[])
4325b09969d6SVaclav Hapla {
4326b09969d6SVaclav Hapla   PetscSection   coordSection;
4327b09969d6SVaclav Hapla   Vec            coordinates;
4328b09969d6SVaclav Hapla   DM             cdm;
4329b09969d6SVaclav Hapla   PetscScalar   *coords;
43301edcf0b2SVaclav Hapla   PetscInt       v, vStart, vEnd, d;
4331b09969d6SVaclav Hapla 
4332b09969d6SVaclav Hapla   PetscFunctionBegin;
43339566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0));
43349566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
43351dca8a05SBarry Smith   PetscCheck(vStart >= 0 && vEnd >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
43369566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, spaceDim));
43379566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
43389566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
43399566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, spaceDim));
43409566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, vStart, vEnd));
43411edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
43429566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, spaceDim));
43439566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, spaceDim));
4344b09969d6SVaclav Hapla   }
43459566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
4346b09969d6SVaclav Hapla 
43479566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
43489566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(cdm, &coordinates));
43499566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, spaceDim));
43509566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
43519566063dSJacob Faibussowitsch   PetscCall(VecGetArrayWrite(coordinates, &coords));
43521edcf0b2SVaclav Hapla   for (v = 0; v < vEnd-vStart; ++v) {
4353b09969d6SVaclav Hapla     for (d = 0; d < spaceDim; ++d) {
4354b09969d6SVaclav Hapla       coords[v*spaceDim+d] = vertexCoords[v*spaceDim+d];
4355b09969d6SVaclav Hapla     }
4356b09969d6SVaclav Hapla   }
43579566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayWrite(coordinates, &coords));
43589566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
43599566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
43609566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0));
4361b09969d6SVaclav Hapla   PetscFunctionReturn(0);
4362b09969d6SVaclav Hapla }
4363b09969d6SVaclav Hapla 
4364b09969d6SVaclav Hapla /*@
43653df08285SMatthew 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
43663df08285SMatthew G. Knepley 
43673df08285SMatthew G. Knepley   Collective on comm
4368b09969d6SVaclav Hapla 
4369b09969d6SVaclav Hapla   Input Parameters:
4370b09969d6SVaclav Hapla + comm - The communicator
4371b09969d6SVaclav Hapla . dim - The topological dimension of the mesh
43723df08285SMatthew G. Knepley . numCells - The number of cells, only on process 0
43733df08285SMatthew G. Knepley . numVertices - The number of vertices owned by this process, or PETSC_DECIDE, only on process 0
43743df08285SMatthew G. Knepley . numCorners - The number of vertices for each cell, only on process 0
4375b09969d6SVaclav Hapla . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
43763df08285SMatthew G. Knepley . cells - An array of numCells*numCorners numbers, the vertices for each cell, only on process 0
4377b09969d6SVaclav Hapla . spaceDim - The spatial dimension used for coordinates
43783df08285SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex, only on process 0
4379b09969d6SVaclav Hapla 
4380b09969d6SVaclav Hapla   Output Parameter:
43813df08285SMatthew G. Knepley . dm - The DM, which only has points on process 0
4382b09969d6SVaclav Hapla 
4383b09969d6SVaclav Hapla   Notes:
4384b09969d6SVaclav Hapla   This function is just a convenient sequence of DMCreate(), DMSetType(), DMSetDimension(), DMPlexBuildFromCellList(),
4385b09969d6SVaclav Hapla   DMPlexInterpolate(), DMPlexBuildCoordinatesFromCellList()
4386b09969d6SVaclav Hapla 
438725b6865aSVaclav Hapla   See DMPlexBuildFromCellList() for an example and details about the topology-related parameters.
438825b6865aSVaclav Hapla   See DMPlexBuildCoordinatesFromCellList() for details about the geometry-related parameters.
43893df08285SMatthew G. Knepley   See DMPlexCreateFromCellListParallelPetsc() for parallel input
439025b6865aSVaclav Hapla 
4391b09969d6SVaclav Hapla   Level: intermediate
4392b09969d6SVaclav Hapla 
4393db781477SPatrick Sanan .seealso: `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildFromCellList()`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()`
43949298eaa6SMatthew G Knepley @*/
4395a4a685f2SJacob 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)
43969298eaa6SMatthew G Knepley {
43973df08285SMatthew G. Knepley   PetscMPIInt    rank;
43989298eaa6SMatthew G Knepley 
43999298eaa6SMatthew G Knepley   PetscFunctionBegin;
440028b400f6SJacob Faibussowitsch   PetscCheck(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.");
44019566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
44029566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
44039566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
44049566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
4405c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildFromCellList(*dm, numCells, numVertices, numCorners, cells));
44069566063dSJacob Faibussowitsch   else           PetscCall(DMPlexBuildFromCellList(*dm, 0, 0, 0, NULL));
44079298eaa6SMatthew G Knepley   if (interpolate) {
44085fd9971aSMatthew G. Knepley     DM idm;
44099298eaa6SMatthew G Knepley 
44109566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
44119566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
44129298eaa6SMatthew G Knepley     *dm  = idm;
44139298eaa6SMatthew G Knepley   }
4414c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, vertexCoords));
44159566063dSJacob Faibussowitsch   else           PetscCall(DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, NULL));
44169298eaa6SMatthew G Knepley   PetscFunctionReturn(0);
44179298eaa6SMatthew G Knepley }
44189298eaa6SMatthew G Knepley 
4419939f6067SMatthew G. Knepley /*@
4420939f6067SMatthew 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
4421939f6067SMatthew G. Knepley 
4422939f6067SMatthew G. Knepley   Input Parameters:
4423c73cfb54SMatthew G. Knepley + dm - The empty DM object, usually from DMCreate() and DMSetDimension()
4424939f6067SMatthew G. Knepley . depth - The depth of the DAG
4425367003a6SStefano Zampini . numPoints - Array of size depth + 1 containing the number of points at each depth
4426939f6067SMatthew G. Knepley . coneSize - The cone size of each point
4427939f6067SMatthew G. Knepley . cones - The concatenation of the cone points for each point, the cone list must be oriented correctly for each point
4428939f6067SMatthew G. Knepley . coneOrientations - The orientation of each cone point
4429367003a6SStefano Zampini - vertexCoords - An array of numPoints[0]*spacedim numbers representing the coordinates of each vertex, with spacedim the value set via DMSetCoordinateDim()
4430939f6067SMatthew G. Knepley 
4431939f6067SMatthew G. Knepley   Output Parameter:
4432939f6067SMatthew G. Knepley . dm - The DM
4433939f6067SMatthew G. Knepley 
4434939f6067SMatthew G. Knepley   Note: Two triangles sharing a face would have input
4435939f6067SMatthew G. Knepley $  depth = 1, numPoints = [4 2], coneSize = [3 3 0 0 0 0]
4436939f6067SMatthew G. Knepley $  cones = [2 3 4  3 5 4], coneOrientations = [0 0 0  0 0 0]
4437939f6067SMatthew G. Knepley $ vertexCoords = [-1.0 0.0  0.0 -1.0  0.0 1.0  1.0 0.0]
4438939f6067SMatthew G. Knepley $
4439939f6067SMatthew G. Knepley which would result in the DMPlex
4440939f6067SMatthew G. Knepley $
4441939f6067SMatthew G. Knepley $        4
4442939f6067SMatthew G. Knepley $      / | \
4443939f6067SMatthew G. Knepley $     /  |  \
4444939f6067SMatthew G. Knepley $    /   |   \
4445939f6067SMatthew G. Knepley $   2  0 | 1  5
4446939f6067SMatthew G. Knepley $    \   |   /
4447939f6067SMatthew G. Knepley $     \  |  /
4448939f6067SMatthew G. Knepley $      \ | /
4449939f6067SMatthew G. Knepley $        3
4450939f6067SMatthew G. Knepley $
4451a4a685f2SJacob Faibussowitsch $ Notice that all points are numbered consecutively, unlike DMPlexCreateFromCellListPetsc()
4452939f6067SMatthew G. Knepley 
4453939f6067SMatthew G. Knepley   Level: advanced
4454939f6067SMatthew G. Knepley 
4455db781477SPatrick Sanan .seealso: `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`
4456939f6067SMatthew G. Knepley @*/
44579298eaa6SMatthew G Knepley PetscErrorCode DMPlexCreateFromDAG(DM dm, PetscInt depth, const PetscInt numPoints[], const PetscInt coneSize[], const PetscInt cones[], const PetscInt coneOrientations[], const PetscScalar vertexCoords[])
44589298eaa6SMatthew G Knepley {
44599298eaa6SMatthew G Knepley   Vec            coordinates;
44609298eaa6SMatthew G Knepley   PetscSection   coordSection;
44619298eaa6SMatthew G Knepley   PetscScalar    *coords;
4462811e8653SToby Isaac   PetscInt       coordSize, firstVertex = -1, pStart = 0, pEnd = 0, p, v, dim, dimEmbed, d, off;
44639298eaa6SMatthew G Knepley 
44649298eaa6SMatthew G Knepley   PetscFunctionBegin;
44659566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
44669566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dimEmbed));
446763a3b9bcSJacob Faibussowitsch   PetscCheck(dimEmbed >= dim,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Embedding dimension %" PetscInt_FMT " cannot be less than intrinsic dimension %" PetscInt_FMT,dimEmbed,dim);
44689298eaa6SMatthew G Knepley   for (d = 0; d <= depth; ++d) pEnd += numPoints[d];
44699566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, pStart, pEnd));
44709298eaa6SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
44719566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeSize(dm, p, coneSize[p-pStart]));
447297e052ccSToby Isaac     if (firstVertex < 0 && !coneSize[p - pStart]) {
447397e052ccSToby Isaac       firstVertex = p - pStart;
44749298eaa6SMatthew G Knepley     }
447597e052ccSToby Isaac   }
44761dca8a05SBarry Smith   PetscCheck(firstVertex >= 0 || !numPoints[0],PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected %" PetscInt_FMT " vertices but could not find any", numPoints[0]);
44779566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm)); /* Allocate space for cones */
44789298eaa6SMatthew G Knepley   for (p = pStart, off = 0; p < pEnd; off += coneSize[p-pStart], ++p) {
44799566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(dm, p, &cones[off]));
44809566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeOrientation(dm, p, &coneOrientations[off]));
44819298eaa6SMatthew G Knepley   }
44829566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
44839566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
44849298eaa6SMatthew G Knepley   /* Build coordinates */
44859566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
44869566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
44879566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dimEmbed));
44889566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex+numPoints[0]));
44899298eaa6SMatthew G Knepley   for (v = firstVertex; v < firstVertex+numPoints[0]; ++v) {
44909566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, dimEmbed));
44919566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dimEmbed));
44929298eaa6SMatthew G Knepley   }
44939566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
44949566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
44959566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
44969566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
44979566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
44989566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, dimEmbed));
44999566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates,VECSTANDARD));
45009318fe57SMatthew G. Knepley   if (vertexCoords) {
45019566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
45029298eaa6SMatthew G Knepley     for (v = 0; v < numPoints[0]; ++v) {
45039298eaa6SMatthew G Knepley       PetscInt off;
45049298eaa6SMatthew G Knepley 
45059566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, v+firstVertex, &off));
4506811e8653SToby Isaac       for (d = 0; d < dimEmbed; ++d) {
4507811e8653SToby Isaac         coords[off+d] = vertexCoords[v*dimEmbed+d];
45089298eaa6SMatthew G Knepley       }
45099298eaa6SMatthew G Knepley     }
45109318fe57SMatthew G. Knepley   }
45119566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
45129566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
45139566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
45149298eaa6SMatthew G Knepley   PetscFunctionReturn(0);
45159298eaa6SMatthew G Knepley }
45168415267dSToby Isaac 
4517ca522641SMatthew G. Knepley /*@C
45188ca92349SMatthew G. Knepley   DMPlexCreateCellVertexFromFile - Create a DMPlex mesh from a simple cell-vertex file.
45198ca92349SMatthew G. Knepley 
45208ca92349SMatthew G. Knepley + comm        - The MPI communicator
45218ca92349SMatthew G. Knepley . filename    - Name of the .dat file
45228ca92349SMatthew G. Knepley - interpolate - Create faces and edges in the mesh
45238ca92349SMatthew G. Knepley 
45248ca92349SMatthew G. Knepley   Output Parameter:
45258ca92349SMatthew G. Knepley . dm  - The DM object representing the mesh
45268ca92349SMatthew G. Knepley 
45278ca92349SMatthew G. Knepley   Note: The format is the simplest possible:
45288ca92349SMatthew G. Knepley $ Ne
45298ca92349SMatthew G. Knepley $ v0 v1 ... vk
45308ca92349SMatthew G. Knepley $ Nv
45318ca92349SMatthew G. Knepley $ x y z marker
45328ca92349SMatthew G. Knepley 
45338ca92349SMatthew G. Knepley   Level: beginner
45348ca92349SMatthew G. Knepley 
4535db781477SPatrick Sanan .seealso: `DMPlexCreateFromFile()`, `DMPlexCreateMedFromFile()`, `DMPlexCreateGmsh()`, `DMPlexCreate()`
45368ca92349SMatthew G. Knepley @*/
45378ca92349SMatthew G. Knepley PetscErrorCode DMPlexCreateCellVertexFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
45388ca92349SMatthew G. Knepley {
45398ca92349SMatthew G. Knepley   DMLabel         marker;
45408ca92349SMatthew G. Knepley   PetscViewer     viewer;
45418ca92349SMatthew G. Knepley   Vec             coordinates;
45428ca92349SMatthew G. Knepley   PetscSection    coordSection;
45438ca92349SMatthew G. Knepley   PetscScalar    *coords;
45448ca92349SMatthew G. Knepley   char            line[PETSC_MAX_PATH_LEN];
45458ca92349SMatthew G. Knepley   PetscInt        dim = 3, cdim = 3, coordSize, v, c, d;
45468ca92349SMatthew G. Knepley   PetscMPIInt     rank;
4547f8d5e320SMatthew G. Knepley   int             snum, Nv, Nc, Ncn, Nl;
45488ca92349SMatthew G. Knepley 
45498ca92349SMatthew G. Knepley   PetscFunctionBegin;
45509566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
45519566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, &viewer));
45529566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(viewer, PETSCVIEWERASCII));
45539566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetMode(viewer, FILE_MODE_READ));
45549566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetName(viewer, filename));
4555dd400576SPatrick Sanan   if (rank == 0) {
45569566063dSJacob Faibussowitsch     PetscCall(PetscViewerRead(viewer, line, 4, NULL, PETSC_STRING));
4557f8d5e320SMatthew G. Knepley     snum = sscanf(line, "%d %d %d %d", &Nc, &Nv, &Ncn, &Nl);
455808401ef6SPierre Jolivet     PetscCheck(snum == 4,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
455925ce1634SJed Brown   } else {
4560f8d5e320SMatthew G. Knepley     Nc = Nv = Ncn = Nl = 0;
45618ca92349SMatthew G. Knepley   }
45629566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
45639566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
45649566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(*dm, 0, Nc+Nv));
45659566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
45669566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*dm, cdim));
45678ca92349SMatthew G. Knepley   /* Read topology */
4568dd400576SPatrick Sanan   if (rank == 0) {
4569f8d5e320SMatthew G. Knepley     char     format[PETSC_MAX_PATH_LEN];
4570f8d5e320SMatthew G. Knepley     PetscInt cone[8];
45718ca92349SMatthew G. Knepley     int      vbuf[8], v;
45728ca92349SMatthew G. Knepley 
4573f8d5e320SMatthew G. Knepley     for (c = 0; c < Ncn; ++c) {format[c*3+0] = '%'; format[c*3+1] = 'd'; format[c*3+2] = ' ';}
4574f8d5e320SMatthew G. Knepley     format[Ncn*3-1] = '\0';
45759566063dSJacob Faibussowitsch     for (c = 0; c < Nc; ++c) PetscCall(DMPlexSetConeSize(*dm, c, Ncn));
45769566063dSJacob Faibussowitsch     PetscCall(DMSetUp(*dm));
45778ca92349SMatthew G. Knepley     for (c = 0; c < Nc; ++c) {
45789566063dSJacob Faibussowitsch       PetscCall(PetscViewerRead(viewer, line, Ncn, NULL, PETSC_STRING));
4579f8d5e320SMatthew G. Knepley       switch (Ncn) {
4580f8d5e320SMatthew G. Knepley         case 2: snum = sscanf(line, format, &vbuf[0], &vbuf[1]);break;
4581f8d5e320SMatthew G. Knepley         case 3: snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2]);break;
4582f8d5e320SMatthew G. Knepley         case 4: snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3]);break;
4583f8d5e320SMatthew G. Knepley         case 6: snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5]);break;
4584f8d5e320SMatthew 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;
458563a3b9bcSJacob Faibussowitsch         default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No cell shape with %d vertices", Ncn);
4586f8d5e320SMatthew G. Knepley       }
458708401ef6SPierre Jolivet       PetscCheck(snum == Ncn,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
4588f8d5e320SMatthew G. Knepley       for (v = 0; v < Ncn; ++v) cone[v] = vbuf[v] + Nc;
45898ca92349SMatthew G. Knepley       /* Hexahedra are inverted */
4590f8d5e320SMatthew G. Knepley       if (Ncn == 8) {
45918ca92349SMatthew G. Knepley         PetscInt tmp = cone[1];
45928ca92349SMatthew G. Knepley         cone[1] = cone[3];
45938ca92349SMatthew G. Knepley         cone[3] = tmp;
45948ca92349SMatthew G. Knepley       }
45959566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(*dm, c, cone));
45968ca92349SMatthew G. Knepley     }
45978ca92349SMatthew G. Knepley   }
45989566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(*dm));
45999566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(*dm));
46008ca92349SMatthew G. Knepley   /* Read coordinates */
46019566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(*dm, &coordSection));
46029566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
46039566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, cdim));
46049566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, Nc, Nc + Nv));
46058ca92349SMatthew G. Knepley   for (v = Nc; v < Nc+Nv; ++v) {
46069566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, cdim));
46079566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, cdim));
46088ca92349SMatthew G. Knepley   }
46099566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
46109566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
46119566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
46129566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
46139566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
46149566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, cdim));
46159566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
46169566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
4617dd400576SPatrick Sanan   if (rank == 0) {
4618f8d5e320SMatthew G. Knepley     char   format[PETSC_MAX_PATH_LEN];
46198ca92349SMatthew G. Knepley     double x[3];
4620f8d5e320SMatthew G. Knepley     int    l, val[3];
46218ca92349SMatthew G. Knepley 
4622f8d5e320SMatthew G. Knepley     if (Nl) {
4623f8d5e320SMatthew G. Knepley       for (l = 0; l < Nl; ++l) {format[l*3+0] = '%'; format[l*3+1] = 'd'; format[l*3+2] = ' ';}
4624f8d5e320SMatthew G. Knepley       format[Nl*3-1] = '\0';
46259566063dSJacob Faibussowitsch       PetscCall(DMCreateLabel(*dm, "marker"));
46269566063dSJacob Faibussowitsch       PetscCall(DMGetLabel(*dm, "marker", &marker));
4627f8d5e320SMatthew G. Knepley     }
46288ca92349SMatthew G. Knepley     for (v = 0; v < Nv; ++v) {
46299566063dSJacob Faibussowitsch       PetscCall(PetscViewerRead(viewer, line, 3+Nl, NULL, PETSC_STRING));
4630f8d5e320SMatthew G. Knepley       snum = sscanf(line, "%lg %lg %lg", &x[0], &x[1], &x[2]);
463108401ef6SPierre Jolivet       PetscCheck(snum == 3,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
4632f8d5e320SMatthew G. Knepley       switch (Nl) {
4633f8d5e320SMatthew G. Knepley         case 0: snum = 0;break;
4634f8d5e320SMatthew G. Knepley         case 1: snum = sscanf(line, format, &val[0]);break;
4635f8d5e320SMatthew G. Knepley         case 2: snum = sscanf(line, format, &val[0], &val[1]);break;
4636f8d5e320SMatthew G. Knepley         case 3: snum = sscanf(line, format, &val[0], &val[1], &val[2]);break;
463763a3b9bcSJacob Faibussowitsch         default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Request support for %d labels", Nl);
4638f8d5e320SMatthew G. Knepley       }
463908401ef6SPierre Jolivet       PetscCheck(snum == Nl,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
46408ca92349SMatthew G. Knepley       for (d = 0; d < cdim; ++d) coords[v*cdim+d] = x[d];
46419566063dSJacob Faibussowitsch       for (l = 0; l < Nl; ++l) PetscCall(DMLabelSetValue(marker, v+Nc, val[l]));
46428ca92349SMatthew G. Knepley     }
46438ca92349SMatthew G. Knepley   }
46449566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
46459566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(*dm, coordinates));
46469566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
46479566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(&viewer));
46488ca92349SMatthew G. Knepley   if (interpolate) {
46498ca92349SMatthew G. Knepley     DM      idm;
46508ca92349SMatthew G. Knepley     DMLabel bdlabel;
46518ca92349SMatthew G. Knepley 
46529566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
46539566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
46548ca92349SMatthew G. Knepley     *dm  = idm;
46558ca92349SMatthew G. Knepley 
4656f8d5e320SMatthew G. Knepley     if (!Nl) {
46579566063dSJacob Faibussowitsch       PetscCall(DMCreateLabel(*dm, "marker"));
46589566063dSJacob Faibussowitsch       PetscCall(DMGetLabel(*dm, "marker", &bdlabel));
46599566063dSJacob Faibussowitsch       PetscCall(DMPlexMarkBoundaryFaces(*dm, PETSC_DETERMINE, bdlabel));
46609566063dSJacob Faibussowitsch       PetscCall(DMPlexLabelComplete(*dm, bdlabel));
46618ca92349SMatthew G. Knepley     }
4662f8d5e320SMatthew G. Knepley   }
46638ca92349SMatthew G. Knepley   PetscFunctionReturn(0);
46648ca92349SMatthew G. Knepley }
46658ca92349SMatthew G. Knepley 
46668ca92349SMatthew G. Knepley /*@C
4667ca522641SMatthew G. Knepley   DMPlexCreateFromFile - This takes a filename and produces a DM
4668ca522641SMatthew G. Knepley 
4669ca522641SMatthew G. Knepley   Input Parameters:
4670ca522641SMatthew G. Knepley + comm - The communicator
4671ca522641SMatthew G. Knepley . filename - A file name
4672cd7e8a5eSksagiyam . plexname - The object name of the resulting DM, also used for intra-datafile lookup by some formats
4673ca522641SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
4674ca522641SMatthew G. Knepley 
4675ca522641SMatthew G. Knepley   Output Parameter:
4676ca522641SMatthew G. Knepley . dm - The DM
4677ca522641SMatthew G. Knepley 
467802ef0d99SVaclav Hapla   Options Database Keys:
467902ef0d99SVaclav Hapla . -dm_plex_create_from_hdf5_xdmf - use the PETSC_VIEWER_HDF5_XDMF format for reading HDF5
468002ef0d99SVaclav Hapla 
4681bca97951SVaclav Hapla   Use -dm_plex_create_ prefix to pass options to the internal PetscViewer, e.g.
4682bca97951SVaclav Hapla $ -dm_plex_create_viewer_hdf5_collective
4683bca97951SVaclav Hapla 
4684cd7e8a5eSksagiyam   Notes:
4685cd7e8a5eSksagiyam   Using PETSCVIEWERHDF5 type with PETSC_VIEWER_HDF5_PETSC format, one can save multiple DMPlex
4686cd7e8a5eSksagiyam   meshes in a single HDF5 file. This in turn requires one to name the DMPlex object with PetscObjectSetName()
4687cd7e8a5eSksagiyam   before saving it with DMView() and before loading it with DMLoad() for identification of the mesh object.
4688cd7e8a5eSksagiyam   The input parameter name is thus used to name the DMPlex object when DMPlexCreateFromFile() internally
4689cd7e8a5eSksagiyam   calls DMLoad(). Currently, name is ignored for other viewer types and/or formats.
4690cd7e8a5eSksagiyam 
4691ca522641SMatthew G. Knepley   Level: beginner
4692ca522641SMatthew G. Knepley 
4693db781477SPatrick Sanan .seealso: `DMPlexCreateFromDAG()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`, `PetscObjectSetName()`, `DMView()`, `DMLoad()`
4694ca522641SMatthew G. Knepley @*/
4695cd7e8a5eSksagiyam PetscErrorCode DMPlexCreateFromFile(MPI_Comm comm, const char filename[], const char plexname[], PetscBool interpolate, DM *dm)
4696ca522641SMatthew G. Knepley {
4697ef3a5affSJacob Faibussowitsch   const char  extGmsh[]      = ".msh";
4698ef3a5affSJacob Faibussowitsch   const char  extGmsh2[]     = ".msh2";
4699ef3a5affSJacob Faibussowitsch   const char  extGmsh4[]     = ".msh4";
4700ef3a5affSJacob Faibussowitsch   const char  extCGNS[]      = ".cgns";
4701ef3a5affSJacob Faibussowitsch   const char  extExodus[]    = ".exo";
4702ef3a5affSJacob Faibussowitsch   const char  extExodus_e[]  = ".e";
4703ef3a5affSJacob Faibussowitsch   const char  extGenesis[]   = ".gen";
4704ef3a5affSJacob Faibussowitsch   const char  extFluent[]    = ".cas";
4705ef3a5affSJacob Faibussowitsch   const char  extHDF5[]      = ".h5";
4706ef3a5affSJacob Faibussowitsch   const char  extMed[]       = ".med";
4707ef3a5affSJacob Faibussowitsch   const char  extPLY[]       = ".ply";
4708ef3a5affSJacob Faibussowitsch   const char  extEGADSLite[] = ".egadslite";
4709ef3a5affSJacob Faibussowitsch   const char  extEGADS[]     = ".egads";
4710ef3a5affSJacob Faibussowitsch   const char  extIGES[]      = ".igs";
4711ef3a5affSJacob Faibussowitsch   const char  extSTEP[]      = ".stp";
4712ef3a5affSJacob Faibussowitsch   const char  extCV[]        = ".dat";
4713ca522641SMatthew G. Knepley   size_t      len;
4714c1cad2e7SMatthew G. Knepley   PetscBool   isGmsh, isGmsh2, isGmsh4, isCGNS, isExodus, isGenesis, isFluent, isHDF5, isMed, isPLY, isEGADSLite, isEGADS, isIGES, isSTEP, isCV;
4715ca522641SMatthew G. Knepley   PetscMPIInt rank;
4716ca522641SMatthew G. Knepley 
4717ca522641SMatthew G. Knepley   PetscFunctionBegin;
47185d80c0bfSVaclav Hapla   PetscValidCharPointer(filename, 2);
47190d862eaeSPierre Jolivet   if (plexname) PetscValidCharPointer(plexname, 3);
4720cd7e8a5eSksagiyam   PetscValidPointer(dm, 5);
47219566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
47229566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_CreateFromFile,0,0,0,0));
47239566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
47249566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(filename, &len));
472528b400f6SJacob Faibussowitsch   PetscCheck(len,comm, PETSC_ERR_ARG_WRONG, "Filename must be a valid path");
4726ef3a5affSJacob Faibussowitsch 
4727ef3a5affSJacob Faibussowitsch #define CheckExtension(extension__,is_extension__) do {                                        \
4728274aaeaaSJacob Faibussowitsch     PetscAssert(sizeof(extension__), comm, PETSC_ERR_PLIB, "Zero-size extension: %s", extension__); \
4729274aaeaaSJacob Faibussowitsch     /* don't count the null-terminator at the end */                                           \
4730274aaeaaSJacob Faibussowitsch     const size_t ext_len = sizeof(extension__)-1;                                              \
4731274aaeaaSJacob Faibussowitsch     if (len < ext_len) {                                                                       \
4732ef3a5affSJacob Faibussowitsch       is_extension__ = PETSC_FALSE;                                                            \
4733ef3a5affSJacob Faibussowitsch     } else {                                                                                   \
4734274aaeaaSJacob Faibussowitsch       PetscCall(PetscStrncmp(filename+len-ext_len, extension__, ext_len, &is_extension__));    \
4735ef3a5affSJacob Faibussowitsch     }                                                                                          \
4736ef3a5affSJacob Faibussowitsch   } while (0)
4737ef3a5affSJacob Faibussowitsch 
4738ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh, isGmsh);
4739ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh2, isGmsh2);
4740ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh4, isGmsh4);
4741ef3a5affSJacob Faibussowitsch   CheckExtension(extCGNS, isCGNS);
4742ef3a5affSJacob Faibussowitsch   CheckExtension(extExodus, isExodus);
4743ef3a5affSJacob Faibussowitsch   if (!isExodus) CheckExtension(extExodus_e, isExodus);
4744ef3a5affSJacob Faibussowitsch   CheckExtension(extGenesis, isGenesis);
4745ef3a5affSJacob Faibussowitsch   CheckExtension(extFluent, isFluent);
4746ef3a5affSJacob Faibussowitsch   CheckExtension(extHDF5, isHDF5);
4747ef3a5affSJacob Faibussowitsch   CheckExtension(extMed, isMed);
4748ef3a5affSJacob Faibussowitsch   CheckExtension(extPLY, isPLY);
4749ef3a5affSJacob Faibussowitsch   CheckExtension(extEGADSLite, isEGADSLite);
4750ef3a5affSJacob Faibussowitsch   CheckExtension(extEGADS, isEGADS);
4751ef3a5affSJacob Faibussowitsch   CheckExtension(extIGES, isIGES);
4752ef3a5affSJacob Faibussowitsch   CheckExtension(extSTEP, isSTEP);
4753ef3a5affSJacob Faibussowitsch   CheckExtension(extCV, isCV);
4754ef3a5affSJacob Faibussowitsch 
4755ef3a5affSJacob Faibussowitsch #undef CheckExtension
4756ef3a5affSJacob Faibussowitsch 
4757de78e4feSLisandro Dalcin   if (isGmsh || isGmsh2 || isGmsh4) {
47589566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateGmshFromFile(comm, filename, interpolate, dm));
4759ca522641SMatthew G. Knepley   } else if (isCGNS) {
47609566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCGNSFromFile(comm, filename, interpolate, dm));
476190c68965SMatthew G. Knepley   } else if (isExodus || isGenesis) {
47629566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateExodusFromFile(comm, filename, interpolate, dm));
47632f0bd6dcSMichael Lange   } else if (isFluent) {
47649566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFluentFromFile(comm, filename, interpolate, dm));
4765cc2f8f65SMatthew G. Knepley   } else if (isHDF5) {
47669c48423bSVaclav Hapla     PetscBool      load_hdf5_xdmf = PETSC_FALSE;
4767cc2f8f65SMatthew G. Knepley     PetscViewer viewer;
4768cc2f8f65SMatthew G. Knepley 
476943b242b4SVaclav 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 */
47709566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(&filename[PetscMax(0,len-8)], ".xdmf",  5, &load_hdf5_xdmf));
47719566063dSJacob Faibussowitsch     PetscCall(PetscOptionsGetBool(NULL, NULL, "-dm_plex_create_from_hdf5_xdmf", &load_hdf5_xdmf, NULL));
47729566063dSJacob Faibussowitsch     PetscCall(PetscViewerCreate(comm, &viewer));
47739566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetType(viewer, PETSCVIEWERHDF5));
47749566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetOptionsPrefix(viewer, "dm_plex_create_"));
47759566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetFromOptions(viewer));
47769566063dSJacob Faibussowitsch     PetscCall(PetscViewerFileSetMode(viewer, FILE_MODE_READ));
47779566063dSJacob Faibussowitsch     PetscCall(PetscViewerFileSetName(viewer, filename));
4778cd7e8a5eSksagiyam 
47799566063dSJacob Faibussowitsch     PetscCall(DMCreate(comm, dm));
47809566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)(*dm), plexname));
47819566063dSJacob Faibussowitsch     PetscCall(DMSetType(*dm, DMPLEX));
47829566063dSJacob Faibussowitsch     if (load_hdf5_xdmf) PetscCall(PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_XDMF));
47839566063dSJacob Faibussowitsch     PetscCall(DMLoad(*dm, viewer));
47849566063dSJacob Faibussowitsch     if (load_hdf5_xdmf) PetscCall(PetscViewerPopFormat(viewer));
47859566063dSJacob Faibussowitsch     PetscCall(PetscViewerDestroy(&viewer));
47865fd9971aSMatthew G. Knepley 
47875fd9971aSMatthew G. Knepley     if (interpolate) {
47885fd9971aSMatthew G. Knepley       DM idm;
47895fd9971aSMatthew G. Knepley 
47909566063dSJacob Faibussowitsch       PetscCall(DMPlexInterpolate(*dm, &idm));
47919566063dSJacob Faibussowitsch       PetscCall(DMDestroy(dm));
47925fd9971aSMatthew G. Knepley       *dm  = idm;
47935fd9971aSMatthew G. Knepley     }
4794707dd687SMichael Lange   } else if (isMed) {
47959566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateMedFromFile(comm, filename, interpolate, dm));
4796f2801cd6SMatthew G. Knepley   } else if (isPLY) {
47979566063dSJacob Faibussowitsch     PetscCall(DMPlexCreatePLYFromFile(comm, filename, interpolate, dm));
4798c1cad2e7SMatthew G. Knepley   } else if (isEGADSLite || isEGADS || isIGES || isSTEP) {
47999566063dSJacob Faibussowitsch     if (isEGADSLite) PetscCall(DMPlexCreateEGADSLiteFromFile(comm, filename, dm));
48009566063dSJacob Faibussowitsch     else             PetscCall(DMPlexCreateEGADSFromFile(comm, filename, dm));
48017bee2925SMatthew Knepley     if (!interpolate) {
48027bee2925SMatthew Knepley       DM udm;
48037bee2925SMatthew Knepley 
48049566063dSJacob Faibussowitsch       PetscCall(DMPlexUninterpolate(*dm, &udm));
48059566063dSJacob Faibussowitsch       PetscCall(DMDestroy(dm));
48067bee2925SMatthew Knepley       *dm  = udm;
48077bee2925SMatthew Knepley     }
48088ca92349SMatthew G. Knepley   } else if (isCV) {
48099566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCellVertexFromFile(comm, filename, interpolate, dm));
481098921bdaSJacob Faibussowitsch   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot load file %s: unrecognized extension", filename);
48119566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(plexname, &len));
48129566063dSJacob Faibussowitsch   if (len) PetscCall(PetscObjectSetName((PetscObject)(*dm), plexname));
48139566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_CreateFromFile,0,0,0,0));
4814ca522641SMatthew G. Knepley   PetscFunctionReturn(0);
4815ca522641SMatthew G. Knepley }
4816