xref: /petsc/src/dm/impls/plex/plexcreate.c (revision 6925d1c4ad6e2fb22bc80165a6eb365ce0565129)
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>
69f6c5813SMatthew G. Knepley #include <petscdmlabelephemeral.h>
7b7f5c055SJed Brown #include <petsc/private/kernels/blockmatmult.h>
8b7f5c055SJed Brown #include <petsc/private/kernels/blockinvert.h>
9552f7358SJed Brown 
10708be2fdSJed Brown PetscLogEvent DMPLEX_CreateFromFile, DMPLEX_CreateFromOptions, DMPLEX_BuildFromCellList, DMPLEX_BuildCoordinatesFromCellList;
1158cd63d5SVaclav Hapla 
129318fe57SMatthew G. Knepley /* External function declarations here */
139318fe57SMatthew G. Knepley static PetscErrorCode DMInitialize_Plex(DM dm);
149318fe57SMatthew G. Knepley 
15e600fa54SMatthew G. Knepley /* This copies internal things in the Plex structure that we generally want when making a new, related Plex */
16d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCopy_Internal(DM dmin, PetscBool copyPeriodicity, PetscBool copyOverlap, DM dmout)
17d71ae5a4SJacob Faibussowitsch {
184fb89dddSMatthew G. Knepley   const PetscReal         *maxCell, *Lstart, *L;
196858538eSMatthew G. Knepley   PetscBool                dist;
206bc1bd01Sksagiyam   DMPlexReorderDefaultFlag reorder;
21e600fa54SMatthew G. Knepley 
22e600fa54SMatthew G. Knepley   PetscFunctionBegin;
23e600fa54SMatthew G. Knepley   if (copyPeriodicity) {
244fb89dddSMatthew G. Knepley     PetscCall(DMGetPeriodicity(dmin, &maxCell, &Lstart, &L));
254fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dmout, maxCell, Lstart, L));
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;
321baa6e33SBarry Smith   if (copyOverlap) PetscCall(DMPlexSetOverlap_Plex(dmout, dmin, 0));
333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
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 */
41d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexReplace_Internal(DM dm, DM *ndm)
42d71ae5a4SJacob Faibussowitsch {
439318fe57SMatthew G. Knepley   PetscSF          sf;
449318fe57SMatthew G. Knepley   DM               dmNew = *ndm, coordDM, coarseDM;
459318fe57SMatthew G. Knepley   Vec              coords;
464fb89dddSMatthew G. Knepley   const PetscReal *maxCell, *Lstart, *L;
479318fe57SMatthew G. Knepley   PetscInt         dim, cdim;
489318fe57SMatthew G. Knepley 
499318fe57SMatthew G. Knepley   PetscFunctionBegin;
509318fe57SMatthew G. Knepley   if (dm == dmNew) {
519566063dSJacob Faibussowitsch     PetscCall(DMDestroy(ndm));
523ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
539318fe57SMatthew G. Knepley   }
549318fe57SMatthew G. Knepley   dm->setupcalled = dmNew->setupcalled;
559566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dmNew, &dim));
569566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
579566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dmNew, &cdim));
589566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, cdim));
599566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmNew, &sf));
609566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dm, sf));
619566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmNew, &coordDM));
629566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmNew, &coords));
639566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dm, coordDM));
649566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coords));
656858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dmNew, &coordDM));
666858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dmNew, &coords));
676858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinateDM(dm, coordDM));
686858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinatesLocal(dm, coords));
699318fe57SMatthew G. Knepley   /* Do not want to create the coordinate field if it does not already exist, so do not call DMGetCoordinateField() */
706858538eSMatthew G. Knepley   PetscCall(DMFieldDestroy(&dm->coordinates[0].field));
716858538eSMatthew G. Knepley   dm->coordinates[0].field            = dmNew->coordinates[0].field;
7261a622f3SMatthew G. Knepley   ((DM_Plex *)dmNew->data)->coordFunc = ((DM_Plex *)dm->data)->coordFunc;
734fb89dddSMatthew G. Knepley   PetscCall(DMGetPeriodicity(dmNew, &maxCell, &Lstart, &L));
744fb89dddSMatthew G. Knepley   PetscCall(DMSetPeriodicity(dm, maxCell, Lstart, L));
75*6925d1c4SMatthew G. Knepley   PetscCall(DMPlexGetGlobalToNaturalSF(dmNew, &sf));
76*6925d1c4SMatthew G. Knepley   PetscCall(DMPlexSetGlobalToNaturalSF(dm, sf));
779566063dSJacob Faibussowitsch   PetscCall(DMDestroy_Plex(dm));
789566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(dm));
799318fe57SMatthew G. Knepley   dm->data = dmNew->data;
809318fe57SMatthew G. Knepley   ((DM_Plex *)dmNew->data)->refct++;
815f06a3ddSJed Brown   PetscCall(DMPlexGetIsoperiodicFaceSF(dm, &sf));
825f06a3ddSJed Brown   PetscCall(DMPlexSetIsoperiodicFaceSF(dm, sf)); // for the compose function effect on dm
839566063dSJacob Faibussowitsch   PetscCall(DMDestroyLabelLinkList_Internal(dm));
849566063dSJacob Faibussowitsch   PetscCall(DMCopyLabels(dmNew, dm, PETSC_OWN_POINTER, PETSC_TRUE, DM_COPY_LABELS_FAIL));
859566063dSJacob Faibussowitsch   PetscCall(DMGetCoarseDM(dmNew, &coarseDM));
869566063dSJacob Faibussowitsch   PetscCall(DMSetCoarseDM(dm, coarseDM));
879566063dSJacob Faibussowitsch   PetscCall(DMDestroy(ndm));
883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
899318fe57SMatthew G. Knepley }
909318fe57SMatthew G. Knepley 
919318fe57SMatthew G. Knepley /* Swap dm with the contents of dmNew
929318fe57SMatthew G. Knepley    - Swap the DM_Plex structure
939318fe57SMatthew G. Knepley    - Swap the coordinates
949318fe57SMatthew G. Knepley    - Swap the point PetscSF
959318fe57SMatthew G. Knepley */
96d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexSwap_Static(DM dmA, DM dmB)
97d71ae5a4SJacob Faibussowitsch {
989318fe57SMatthew G. Knepley   DM          coordDMA, coordDMB;
999318fe57SMatthew G. Knepley   Vec         coordsA, coordsB;
1009318fe57SMatthew G. Knepley   PetscSF     sfA, sfB;
1019318fe57SMatthew G. Knepley   DMField     fieldTmp;
1029318fe57SMatthew G. Knepley   void       *tmp;
1039318fe57SMatthew G. Knepley   DMLabelLink listTmp;
1049318fe57SMatthew G. Knepley   DMLabel     depthTmp;
1059318fe57SMatthew G. Knepley   PetscInt    tmpI;
1069318fe57SMatthew G. Knepley 
1079318fe57SMatthew G. Knepley   PetscFunctionBegin;
1083ba16761SJacob Faibussowitsch   if (dmA == dmB) PetscFunctionReturn(PETSC_SUCCESS);
1099566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmA, &sfA));
1109566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmB, &sfB));
1119566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sfA));
1129566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dmA, sfB));
1139566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dmB, sfA));
1149566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)sfA));
1159318fe57SMatthew G. Knepley 
1169566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmA, &coordDMA));
1179566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmB, &coordDMB));
1189566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)coordDMA));
1199566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dmA, coordDMB));
1209566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dmB, coordDMA));
1219566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)coordDMA));
1229318fe57SMatthew G. Knepley 
1239566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmA, &coordsA));
1249566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmB, &coordsB));
1259566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)coordsA));
1269566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmA, coordsB));
1279566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmB, coordsA));
1289566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)coordsA));
1299318fe57SMatthew G. Knepley 
1306858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dmA, &coordDMA));
1316858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dmB, &coordDMB));
1326858538eSMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)coordDMA));
1336858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinateDM(dmA, coordDMB));
1346858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinateDM(dmB, coordDMA));
1356858538eSMatthew G. Knepley   PetscCall(PetscObjectDereference((PetscObject)coordDMA));
1366858538eSMatthew G. Knepley 
1376858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dmA, &coordsA));
1386858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dmB, &coordsB));
1396858538eSMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)coordsA));
1406858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinatesLocal(dmA, coordsB));
1416858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinatesLocal(dmB, coordsA));
1426858538eSMatthew G. Knepley   PetscCall(PetscObjectDereference((PetscObject)coordsA));
1436858538eSMatthew G. Knepley 
1446858538eSMatthew G. Knepley   fieldTmp                  = dmA->coordinates[0].field;
1456858538eSMatthew G. Knepley   dmA->coordinates[0].field = dmB->coordinates[0].field;
1466858538eSMatthew G. Knepley   dmB->coordinates[0].field = fieldTmp;
1476858538eSMatthew G. Knepley   fieldTmp                  = dmA->coordinates[1].field;
1486858538eSMatthew G. Knepley   dmA->coordinates[1].field = dmB->coordinates[1].field;
1496858538eSMatthew G. Knepley   dmB->coordinates[1].field = fieldTmp;
1509318fe57SMatthew G. Knepley   tmp                       = dmA->data;
1519318fe57SMatthew G. Knepley   dmA->data                 = dmB->data;
1529318fe57SMatthew G. Knepley   dmB->data                 = tmp;
1539318fe57SMatthew G. Knepley   listTmp                   = dmA->labels;
1549318fe57SMatthew G. Knepley   dmA->labels               = dmB->labels;
1559318fe57SMatthew G. Knepley   dmB->labels               = listTmp;
1569318fe57SMatthew G. Knepley   depthTmp                  = dmA->depthLabel;
1579318fe57SMatthew G. Knepley   dmA->depthLabel           = dmB->depthLabel;
1589318fe57SMatthew G. Knepley   dmB->depthLabel           = depthTmp;
1599318fe57SMatthew G. Knepley   depthTmp                  = dmA->celltypeLabel;
1609318fe57SMatthew G. Knepley   dmA->celltypeLabel        = dmB->celltypeLabel;
1619318fe57SMatthew G. Knepley   dmB->celltypeLabel        = depthTmp;
1629318fe57SMatthew G. Knepley   tmpI                      = dmA->levelup;
1639318fe57SMatthew G. Knepley   dmA->levelup              = dmB->levelup;
1649318fe57SMatthew G. Knepley   dmB->levelup              = tmpI;
1653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1669318fe57SMatthew G. Knepley }
1679318fe57SMatthew G. Knepley 
1683431e603SJed Brown PetscErrorCode DMPlexInterpolateInPlace_Internal(DM dm)
169d71ae5a4SJacob Faibussowitsch {
1709318fe57SMatthew G. Knepley   DM idm;
1719318fe57SMatthew G. Knepley 
1729318fe57SMatthew G. Knepley   PetscFunctionBegin;
1739566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolate(dm, &idm));
1749566063dSJacob Faibussowitsch   PetscCall(DMPlexCopyCoordinates(dm, idm));
17569d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &idm));
1763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1779318fe57SMatthew G. Knepley }
1789318fe57SMatthew G. Knepley 
1799318fe57SMatthew G. Knepley /*@C
1809318fe57SMatthew G. Knepley   DMPlexCreateCoordinateSpace - Creates a finite element space for the coordinates
1819318fe57SMatthew G. Knepley 
18220f4b53cSBarry Smith   Collective
1839318fe57SMatthew G. Knepley 
1849318fe57SMatthew G. Knepley   Input Parameters:
18560225df5SJacob Faibussowitsch + dm        - The `DMPLEX`
18620f4b53cSBarry Smith . degree    - The degree of the finite element or `PETSC_DECIDE`
1879318fe57SMatthew G. Knepley - coordFunc - An optional function to map new points from refinement to the surface
1889318fe57SMatthew G. Knepley 
1899318fe57SMatthew G. Knepley   Level: advanced
1909318fe57SMatthew G. Knepley 
1911cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `PetscPointFunc`, `PetscFECreateLagrange()`, `DMGetCoordinateDM()`
1929318fe57SMatthew G. Knepley @*/
193d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateCoordinateSpace(DM dm, PetscInt degree, PetscPointFunc coordFunc)
194d71ae5a4SJacob Faibussowitsch {
1959318fe57SMatthew G. Knepley   DM_Plex     *mesh = (DM_Plex *)dm->data;
1969318fe57SMatthew G. Knepley   DM           cdm;
1979318fe57SMatthew G. Knepley   PetscDS      cds;
1989318fe57SMatthew G. Knepley   PetscFE      fe;
1999318fe57SMatthew G. Knepley   PetscClassId id;
2009318fe57SMatthew G. Knepley 
2019318fe57SMatthew G. Knepley   PetscFunctionBegin;
2029566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
2039566063dSJacob Faibussowitsch   PetscCall(DMGetDS(cdm, &cds));
2049566063dSJacob Faibussowitsch   PetscCall(PetscDSGetDiscretization(cds, 0, (PetscObject *)&fe));
2059566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId((PetscObject)fe, &id));
2069318fe57SMatthew G. Knepley   if (id != PETSCFE_CLASSID) {
2079318fe57SMatthew G. Knepley     PetscInt dim, dE, qorder;
2089318fe57SMatthew G. Knepley 
2099566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(dm, &dim));
2109566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDim(dm, &dE));
2119318fe57SMatthew G. Knepley     qorder = degree;
212d0609cedSBarry Smith     PetscObjectOptionsBegin((PetscObject)cdm);
213dc431b0cSMatthew G. Knepley     PetscCall(PetscOptionsBoundedInt("-default_quadrature_order", "Quadrature order is one less than quadrature points per edge", "DMPlexCreateCoordinateSpace", qorder, &qorder, NULL, 0));
214d0609cedSBarry Smith     PetscOptionsEnd();
2154f9ab2b4SJed Brown     if (degree == PETSC_DECIDE) fe = NULL;
2164f9ab2b4SJed Brown     else {
217dc431b0cSMatthew G. Knepley       DMPolytopeType ct;
218dc431b0cSMatthew G. Knepley       PetscInt       cStart, cEnd, ctTmp;
219dc431b0cSMatthew G. Knepley 
220dc431b0cSMatthew G. Knepley       PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
221dc431b0cSMatthew G. Knepley       // Want to match cell types
222dc431b0cSMatthew G. Knepley       if (cEnd > cStart) PetscCall(DMPlexGetCellType(dm, cStart, &ct));
223dc431b0cSMatthew G. Knepley       else ct = DM_POLYTOPE_UNKNOWN;
224dc431b0cSMatthew G. Knepley       ctTmp = (PetscInt)ct;
225712fec58SPierre Jolivet       PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &ctTmp, 1, MPIU_INT, MPI_MIN, PetscObjectComm((PetscObject)dm)));
226dc431b0cSMatthew G. Knepley       ct = (DMPolytopeType)ctTmp;
227dc431b0cSMatthew G. Knepley       // Work around current bug
228dc431b0cSMatthew G. Knepley       if (ct == DM_POLYTOPE_SEG_PRISM_TENSOR || ct == DM_POLYTOPE_TRI_PRISM_TENSOR || ct == DM_POLYTOPE_QUAD_PRISM_TENSOR) fe = NULL;
229dc431b0cSMatthew G. Knepley       else PetscCall(PetscFECreateLagrangeByCell(PETSC_COMM_SELF, dim, dE, ct, degree, qorder, &fe));
2304f9ab2b4SJed Brown     }
231dc431b0cSMatthew G. Knepley     if (fe) PetscCall(DMProjectCoordinates(dm, fe));
2329566063dSJacob Faibussowitsch     PetscCall(PetscFEDestroy(&fe));
2339318fe57SMatthew G. Knepley   }
2349318fe57SMatthew G. Knepley   mesh->coordFunc = coordFunc;
2353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2369318fe57SMatthew G. Knepley }
2379318fe57SMatthew G. Knepley 
2381df5d5c5SMatthew G. Knepley /*@
2391df5d5c5SMatthew G. Knepley   DMPlexCreateDoublet - Creates a mesh of two cells of the specified type, optionally with later refinement.
2401df5d5c5SMatthew G. Knepley 
241d083f849SBarry Smith   Collective
2421df5d5c5SMatthew G. Knepley 
2431df5d5c5SMatthew G. Knepley   Input Parameters:
244a1cb98faSBarry Smith + comm            - The communicator for the `DM` object
2451df5d5c5SMatthew G. Knepley . dim             - The spatial dimension
2461df5d5c5SMatthew G. Knepley . simplex         - Flag for simplicial cells, otherwise they are tensor product cells
2471df5d5c5SMatthew G. Knepley . interpolate     - Flag to create intermediate mesh pieces (edges, faces)
2481df5d5c5SMatthew G. Knepley - refinementLimit - A nonzero number indicates the largest admissible volume for a refined cell
2491df5d5c5SMatthew G. Knepley 
2501df5d5c5SMatthew G. Knepley   Output Parameter:
25160225df5SJacob Faibussowitsch . newdm - The `DM` object
2521df5d5c5SMatthew G. Knepley 
2531df5d5c5SMatthew G. Knepley   Level: beginner
2541df5d5c5SMatthew G. Knepley 
2551cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMSetType()`, `DMCreate()`
2561df5d5c5SMatthew G. Knepley @*/
257d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateDoublet(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscBool interpolate, PetscReal refinementLimit, DM *newdm)
258d71ae5a4SJacob Faibussowitsch {
2591df5d5c5SMatthew G. Knepley   DM          dm;
2601df5d5c5SMatthew G. Knepley   PetscMPIInt rank;
2611df5d5c5SMatthew G. Knepley 
2621df5d5c5SMatthew G. Knepley   PetscFunctionBegin;
2639566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, &dm));
2649566063dSJacob Faibussowitsch   PetscCall(DMSetType(dm, DMPLEX));
2659566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
26646139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
2679566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
268ce78fa2fSMatthew G. Knepley   switch (dim) {
269ce78fa2fSMatthew G. Knepley   case 2:
2709566063dSJacob Faibussowitsch     if (simplex) PetscCall(PetscObjectSetName((PetscObject)dm, "triangular"));
2719566063dSJacob Faibussowitsch     else PetscCall(PetscObjectSetName((PetscObject)dm, "quadrilateral"));
272ce78fa2fSMatthew G. Knepley     break;
273ce78fa2fSMatthew G. Knepley   case 3:
2749566063dSJacob Faibussowitsch     if (simplex) PetscCall(PetscObjectSetName((PetscObject)dm, "tetrahedral"));
2759566063dSJacob Faibussowitsch     else PetscCall(PetscObjectSetName((PetscObject)dm, "hexahedral"));
276ce78fa2fSMatthew G. Knepley     break;
277d71ae5a4SJacob Faibussowitsch   default:
278d71ae5a4SJacob Faibussowitsch     SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %" PetscInt_FMT, dim);
279ce78fa2fSMatthew G. Knepley   }
2801df5d5c5SMatthew G. Knepley   if (rank) {
2811df5d5c5SMatthew G. Knepley     PetscInt numPoints[2] = {0, 0};
2829566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, NULL, NULL, NULL, NULL));
2831df5d5c5SMatthew G. Knepley   } else {
2841df5d5c5SMatthew G. Knepley     switch (dim) {
2851df5d5c5SMatthew G. Knepley     case 2:
2861df5d5c5SMatthew G. Knepley       if (simplex) {
2871df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {4, 2};
2881df5d5c5SMatthew G. Knepley         PetscInt    coneSize[6]         = {3, 3, 0, 0, 0, 0};
2891df5d5c5SMatthew G. Knepley         PetscInt    cones[6]            = {2, 3, 4, 5, 4, 3};
2901df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
2911df5d5c5SMatthew G. Knepley         PetscScalar vertexCoords[8]     = {-0.5, 0.5, 0.0, 0.0, 0.0, 1.0, 0.5, 0.5};
2921df5d5c5SMatthew G. Knepley 
2939566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
2941df5d5c5SMatthew G. Knepley       } else {
2951df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {6, 2};
2961df5d5c5SMatthew G. Knepley         PetscInt    coneSize[8]         = {4, 4, 0, 0, 0, 0, 0, 0};
2971df5d5c5SMatthew G. Knepley         PetscInt    cones[8]            = {2, 3, 4, 5, 3, 6, 7, 4};
2981df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
2991df5d5c5SMatthew 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};
3001df5d5c5SMatthew G. Knepley 
3019566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
3021df5d5c5SMatthew G. Knepley       }
3031df5d5c5SMatthew G. Knepley       break;
3041df5d5c5SMatthew G. Knepley     case 3:
3051df5d5c5SMatthew G. Knepley       if (simplex) {
3061df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {5, 2};
3071df5d5c5SMatthew G. Knepley         PetscInt    coneSize[7]         = {4, 4, 0, 0, 0, 0, 0};
3081df5d5c5SMatthew G. Knepley         PetscInt    cones[8]            = {4, 3, 5, 2, 5, 3, 4, 6};
3091df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
3101df5d5c5SMatthew 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};
3111df5d5c5SMatthew G. Knepley 
3129566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
3131df5d5c5SMatthew G. Knepley       } else {
3141df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]         = {12, 2};
3151df5d5c5SMatthew G. Knepley         PetscInt    coneSize[14]         = {8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
3161df5d5c5SMatthew G. Knepley         PetscInt    cones[16]            = {2, 3, 4, 5, 6, 7, 8, 9, 5, 4, 10, 11, 7, 12, 13, 8};
3171df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
3189371c9d4SSatish Balay         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, -1.0, -0.5, 0.5, 0.0, -0.5, 0.5, 0.0, 0.5, 0.5, -1.0, 0.5, 0.5, 1.0, 0.5, -0.5, 1.0, -0.5, -0.5, 1.0, -0.5, 0.5, 1.0, 0.5, 0.5};
3191df5d5c5SMatthew G. Knepley 
3209566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
3211df5d5c5SMatthew G. Knepley       }
3221df5d5c5SMatthew G. Knepley       break;
323d71ae5a4SJacob Faibussowitsch     default:
324d71ae5a4SJacob Faibussowitsch       SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %" PetscInt_FMT, dim);
3251df5d5c5SMatthew G. Knepley     }
3261df5d5c5SMatthew G. Knepley   }
32746139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
3281df5d5c5SMatthew G. Knepley   *newdm = dm;
3291df5d5c5SMatthew G. Knepley   if (refinementLimit > 0.0) {
3301df5d5c5SMatthew G. Knepley     DM          rdm;
3311df5d5c5SMatthew G. Knepley     const char *name;
3321df5d5c5SMatthew G. Knepley 
3339566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementUniform(*newdm, PETSC_FALSE));
3349566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementLimit(*newdm, refinementLimit));
3359566063dSJacob Faibussowitsch     PetscCall(DMRefine(*newdm, comm, &rdm));
3369566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)*newdm, &name));
3379566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)rdm, name));
3389566063dSJacob Faibussowitsch     PetscCall(DMDestroy(newdm));
3391df5d5c5SMatthew G. Knepley     *newdm = rdm;
3401df5d5c5SMatthew G. Knepley   }
3411df5d5c5SMatthew G. Knepley   if (interpolate) {
3425fd9971aSMatthew G. Knepley     DM idm;
3431df5d5c5SMatthew G. Knepley 
3449566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*newdm, &idm));
3459566063dSJacob Faibussowitsch     PetscCall(DMDestroy(newdm));
3461df5d5c5SMatthew G. Knepley     *newdm = idm;
3471df5d5c5SMatthew G. Knepley   }
3483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3491df5d5c5SMatthew G. Knepley }
3501df5d5c5SMatthew G. Knepley 
351d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
352d71ae5a4SJacob Faibussowitsch {
3539318fe57SMatthew G. Knepley   const PetscInt numVertices    = 2;
3549318fe57SMatthew G. Knepley   PetscInt       markerRight    = 1;
3559318fe57SMatthew G. Knepley   PetscInt       markerLeft     = 1;
3569318fe57SMatthew G. Knepley   PetscBool      markerSeparate = PETSC_FALSE;
3579318fe57SMatthew G. Knepley   Vec            coordinates;
3589318fe57SMatthew G. Knepley   PetscSection   coordSection;
3599318fe57SMatthew G. Knepley   PetscScalar   *coords;
3609318fe57SMatthew G. Knepley   PetscInt       coordSize;
3619318fe57SMatthew G. Knepley   PetscMPIInt    rank;
3629318fe57SMatthew G. Knepley   PetscInt       cdim = 1, v;
363552f7358SJed Brown 
3649318fe57SMatthew G. Knepley   PetscFunctionBegin;
3659566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
3669318fe57SMatthew G. Knepley   if (markerSeparate) {
3679318fe57SMatthew G. Knepley     markerRight = 2;
3689318fe57SMatthew G. Knepley     markerLeft  = 1;
3699318fe57SMatthew G. Knepley   }
3709566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
371c5853193SPierre Jolivet   if (rank == 0) {
3729566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numVertices));
3739566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
3749566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", 0, markerLeft));
3759566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", 1, markerRight));
3769318fe57SMatthew G. Knepley   }
3779566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
3789566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
3799318fe57SMatthew G. Knepley   /* Build coordinates */
3809566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, cdim));
3819566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
3829566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
3839566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, 0, numVertices));
3849566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, cdim));
3859318fe57SMatthew G. Knepley   for (v = 0; v < numVertices; ++v) {
3869566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, cdim));
3879566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, cdim));
3889318fe57SMatthew G. Knepley   }
3899566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
3909566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
3919566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
3929566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
3939566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
3949566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, cdim));
3959566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
3969566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
3979318fe57SMatthew G. Knepley   coords[0] = lower[0];
3989318fe57SMatthew G. Knepley   coords[1] = upper[0];
3999566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
4009566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
4019566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
4023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4039318fe57SMatthew G. Knepley }
40426492d91SMatthew G. Knepley 
405d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
406d71ae5a4SJacob Faibussowitsch {
4071df21d24SMatthew G. Knepley   const PetscInt numVertices    = (edges[0] + 1) * (edges[1] + 1);
4081df21d24SMatthew G. Knepley   const PetscInt numEdges       = edges[0] * (edges[1] + 1) + (edges[0] + 1) * edges[1];
409552f7358SJed Brown   PetscInt       markerTop      = 1;
410552f7358SJed Brown   PetscInt       markerBottom   = 1;
411552f7358SJed Brown   PetscInt       markerRight    = 1;
412552f7358SJed Brown   PetscInt       markerLeft     = 1;
413552f7358SJed Brown   PetscBool      markerSeparate = PETSC_FALSE;
414552f7358SJed Brown   Vec            coordinates;
415552f7358SJed Brown   PetscSection   coordSection;
416552f7358SJed Brown   PetscScalar   *coords;
417552f7358SJed Brown   PetscInt       coordSize;
418552f7358SJed Brown   PetscMPIInt    rank;
419552f7358SJed Brown   PetscInt       v, vx, vy;
420552f7358SJed Brown 
421552f7358SJed Brown   PetscFunctionBegin;
4229566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
423552f7358SJed Brown   if (markerSeparate) {
4241df21d24SMatthew G. Knepley     markerTop    = 3;
4251df21d24SMatthew G. Knepley     markerBottom = 1;
4261df21d24SMatthew G. Knepley     markerRight  = 2;
4271df21d24SMatthew G. Knepley     markerLeft   = 4;
428552f7358SJed Brown   }
4299566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
430dd400576SPatrick Sanan   if (rank == 0) {
431552f7358SJed Brown     PetscInt e, ex, ey;
432552f7358SJed Brown 
4339566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numEdges + numVertices));
43448a46eb9SPierre Jolivet     for (e = 0; e < numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2));
4359566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
436552f7358SJed Brown     for (vx = 0; vx <= edges[0]; vx++) {
437552f7358SJed Brown       for (ey = 0; ey < edges[1]; ey++) {
438552f7358SJed Brown         PetscInt edge   = vx * edges[1] + ey + edges[0] * (edges[1] + 1);
439552f7358SJed Brown         PetscInt vertex = ey * (edges[0] + 1) + vx + numEdges;
440da80777bSKarl Rupp         PetscInt cone[2];
441552f7358SJed Brown 
4429371c9d4SSatish Balay         cone[0] = vertex;
4439371c9d4SSatish Balay         cone[1] = vertex + edges[0] + 1;
4449566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, edge, cone));
445552f7358SJed Brown         if (vx == edges[0]) {
4469566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
4479566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
448552f7358SJed Brown           if (ey == edges[1] - 1) {
4499566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
4509566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerRight));
451552f7358SJed Brown           }
452552f7358SJed Brown         } else if (vx == 0) {
4539566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
4549566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
455552f7358SJed Brown           if (ey == edges[1] - 1) {
4569566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
4579566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerLeft));
458552f7358SJed Brown           }
459552f7358SJed Brown         }
460552f7358SJed Brown       }
461552f7358SJed Brown     }
462552f7358SJed Brown     for (vy = 0; vy <= edges[1]; vy++) {
463552f7358SJed Brown       for (ex = 0; ex < edges[0]; ex++) {
464552f7358SJed Brown         PetscInt edge   = vy * edges[0] + ex;
465552f7358SJed Brown         PetscInt vertex = vy * (edges[0] + 1) + ex + numEdges;
466da80777bSKarl Rupp         PetscInt cone[2];
467552f7358SJed Brown 
4689371c9d4SSatish Balay         cone[0] = vertex;
4699371c9d4SSatish Balay         cone[1] = vertex + 1;
4709566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, edge, cone));
471552f7358SJed Brown         if (vy == edges[1]) {
4729566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
4739566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
474552f7358SJed Brown           if (ex == edges[0] - 1) {
4759566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
4769566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerTop));
477552f7358SJed Brown           }
478552f7358SJed Brown         } else if (vy == 0) {
4799566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
4809566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
481552f7358SJed Brown           if (ex == edges[0] - 1) {
4829566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
4839566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerBottom));
484552f7358SJed Brown           }
485552f7358SJed Brown         }
486552f7358SJed Brown       }
487552f7358SJed Brown     }
488552f7358SJed Brown   }
4899566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
4909566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
491552f7358SJed Brown   /* Build coordinates */
4929566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, 2));
4939566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
4949566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
4959566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, numEdges, numEdges + numVertices));
4969566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, 2));
497552f7358SJed Brown   for (v = numEdges; v < numEdges + numVertices; ++v) {
4989566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, 2));
4999566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, 2));
500552f7358SJed Brown   }
5019566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
5029566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
5039566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
5049566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
5059566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
5069566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, 2));
5079566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
5089566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
509552f7358SJed Brown   for (vy = 0; vy <= edges[1]; ++vy) {
510552f7358SJed Brown     for (vx = 0; vx <= edges[0]; ++vx) {
511552f7358SJed Brown       coords[(vy * (edges[0] + 1) + vx) * 2 + 0] = lower[0] + ((upper[0] - lower[0]) / edges[0]) * vx;
512552f7358SJed Brown       coords[(vy * (edges[0] + 1) + vx) * 2 + 1] = lower[1] + ((upper[1] - lower[1]) / edges[1]) * vy;
513552f7358SJed Brown     }
514552f7358SJed Brown   }
5159566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
5169566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
5179566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
5183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
519552f7358SJed Brown }
520552f7358SJed Brown 
521d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt faces[])
522d71ae5a4SJacob Faibussowitsch {
5239e8abbc3SMichael Lange   PetscInt     vertices[3], numVertices;
5247b59f5a9SMichael Lange   PetscInt     numFaces       = 2 * faces[0] * faces[1] + 2 * faces[1] * faces[2] + 2 * faces[0] * faces[2];
525c2df9bbfSMatthew G. Knepley   PetscInt     markerTop      = 1;
526c2df9bbfSMatthew G. Knepley   PetscInt     markerBottom   = 1;
527c2df9bbfSMatthew G. Knepley   PetscInt     markerFront    = 1;
528c2df9bbfSMatthew G. Knepley   PetscInt     markerBack     = 1;
529c2df9bbfSMatthew G. Knepley   PetscInt     markerRight    = 1;
530c2df9bbfSMatthew G. Knepley   PetscInt     markerLeft     = 1;
531c2df9bbfSMatthew G. Knepley   PetscBool    markerSeparate = PETSC_FALSE;
532552f7358SJed Brown   Vec          coordinates;
533552f7358SJed Brown   PetscSection coordSection;
534552f7358SJed Brown   PetscScalar *coords;
535552f7358SJed Brown   PetscInt     coordSize;
536552f7358SJed Brown   PetscMPIInt  rank;
537552f7358SJed Brown   PetscInt     v, vx, vy, vz;
5387b59f5a9SMichael Lange   PetscInt     voffset, iface = 0, cone[4];
539552f7358SJed Brown 
540552f7358SJed Brown   PetscFunctionBegin;
5411dca8a05SBarry 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");
5429566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
543c2df9bbfSMatthew G. Knepley   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
544c2df9bbfSMatthew G. Knepley   if (markerSeparate) {
545c2df9bbfSMatthew G. Knepley     markerBottom = 1;
546c2df9bbfSMatthew G. Knepley     markerTop    = 2;
547c2df9bbfSMatthew G. Knepley     markerFront  = 3;
548c2df9bbfSMatthew G. Knepley     markerBack   = 4;
549c2df9bbfSMatthew G. Knepley     markerRight  = 5;
550c2df9bbfSMatthew G. Knepley     markerLeft   = 6;
551c2df9bbfSMatthew G. Knepley   }
5529371c9d4SSatish Balay   vertices[0] = faces[0] + 1;
5539371c9d4SSatish Balay   vertices[1] = faces[1] + 1;
5549371c9d4SSatish Balay   vertices[2] = faces[2] + 1;
5559e8abbc3SMichael Lange   numVertices = vertices[0] * vertices[1] * vertices[2];
556dd400576SPatrick Sanan   if (rank == 0) {
557552f7358SJed Brown     PetscInt f;
558552f7358SJed Brown 
5599566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numFaces + numVertices));
56048a46eb9SPierre Jolivet     for (f = 0; f < numFaces; ++f) PetscCall(DMPlexSetConeSize(dm, f, 4));
5619566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
5627b59f5a9SMichael Lange 
5637b59f5a9SMichael Lange     /* Side 0 (Top) */
5647b59f5a9SMichael Lange     for (vy = 0; vy < faces[1]; vy++) {
5657b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5667b59f5a9SMichael Lange         voffset = numFaces + vertices[0] * vertices[1] * (vertices[2] - 1) + vy * vertices[0] + vx;
5679371c9d4SSatish Balay         cone[0] = voffset;
5689371c9d4SSatish Balay         cone[1] = voffset + 1;
5699371c9d4SSatish Balay         cone[2] = voffset + vertices[0] + 1;
5709371c9d4SSatish Balay         cone[3] = voffset + vertices[0];
5719566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
572c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerTop));
573c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerTop));
574c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerTop));
575c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerTop));
576c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 1, markerTop));
5777b59f5a9SMichael Lange         iface++;
578552f7358SJed Brown       }
579552f7358SJed Brown     }
5807b59f5a9SMichael Lange 
5817b59f5a9SMichael Lange     /* Side 1 (Bottom) */
5827b59f5a9SMichael Lange     for (vy = 0; vy < faces[1]; vy++) {
5837b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5847b59f5a9SMichael Lange         voffset = numFaces + vy * (faces[0] + 1) + vx;
5859371c9d4SSatish Balay         cone[0] = voffset + 1;
5869371c9d4SSatish Balay         cone[1] = voffset;
5879371c9d4SSatish Balay         cone[2] = voffset + vertices[0];
5889371c9d4SSatish Balay         cone[3] = voffset + vertices[0] + 1;
5899566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
590c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerBottom));
591c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerBottom));
592c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerBottom));
593c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerBottom));
594c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 1, markerBottom));
5957b59f5a9SMichael Lange         iface++;
596552f7358SJed Brown       }
597552f7358SJed Brown     }
5987b59f5a9SMichael Lange 
5997b59f5a9SMichael Lange     /* Side 2 (Front) */
6007b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
6017b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
6027b59f5a9SMichael Lange         voffset = numFaces + vz * vertices[0] * vertices[1] + vx;
6039371c9d4SSatish Balay         cone[0] = voffset;
6049371c9d4SSatish Balay         cone[1] = voffset + 1;
6059371c9d4SSatish Balay         cone[2] = voffset + vertices[0] * vertices[1] + 1;
6069371c9d4SSatish Balay         cone[3] = voffset + vertices[0] * vertices[1];
6079566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
608c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerFront));
609c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerFront));
610c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerFront));
611c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 0, markerFront));
612c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 1, markerFront));
6137b59f5a9SMichael Lange         iface++;
614552f7358SJed Brown       }
6157b59f5a9SMichael Lange     }
6167b59f5a9SMichael Lange 
6177b59f5a9SMichael Lange     /* Side 3 (Back) */
6187b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
6197b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
6207b59f5a9SMichael Lange         voffset = numFaces + vz * vertices[0] * vertices[1] + vertices[0] * (vertices[1] - 1) + vx;
6219371c9d4SSatish Balay         cone[0] = voffset + vertices[0] * vertices[1];
6229371c9d4SSatish Balay         cone[1] = voffset + vertices[0] * vertices[1] + 1;
6239371c9d4SSatish Balay         cone[2] = voffset + 1;
6249371c9d4SSatish Balay         cone[3] = voffset;
6259566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
626c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerBack));
627c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerBack));
628c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerBack));
629c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 0, markerBack));
630c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 1, markerBack));
6317b59f5a9SMichael Lange         iface++;
6327b59f5a9SMichael Lange       }
6337b59f5a9SMichael Lange     }
6347b59f5a9SMichael Lange 
6357b59f5a9SMichael Lange     /* Side 4 (Left) */
6367b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
6377b59f5a9SMichael Lange       for (vy = 0; vy < faces[1]; vy++) {
6387b59f5a9SMichael Lange         voffset = numFaces + vz * vertices[0] * vertices[1] + vy * vertices[0];
6399371c9d4SSatish Balay         cone[0] = voffset;
6409371c9d4SSatish Balay         cone[1] = voffset + vertices[0] * vertices[1];
6419371c9d4SSatish Balay         cone[2] = voffset + vertices[0] * vertices[1] + vertices[0];
6429371c9d4SSatish Balay         cone[3] = voffset + vertices[0];
6439566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
644c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerLeft));
645c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerLeft));
646c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerLeft));
647c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[1] + 0, markerLeft));
648c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + vertices[0], markerLeft));
6497b59f5a9SMichael Lange         iface++;
6507b59f5a9SMichael Lange       }
6517b59f5a9SMichael Lange     }
6527b59f5a9SMichael Lange 
6537b59f5a9SMichael Lange     /* Side 5 (Right) */
6547b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
6557b59f5a9SMichael Lange       for (vy = 0; vy < faces[1]; vy++) {
656aab5bcd8SJed Brown         voffset = numFaces + vz * vertices[0] * vertices[1] + vy * vertices[0] + faces[0];
6579371c9d4SSatish Balay         cone[0] = voffset + vertices[0] * vertices[1];
6589371c9d4SSatish Balay         cone[1] = voffset;
6599371c9d4SSatish Balay         cone[2] = voffset + vertices[0];
6609371c9d4SSatish Balay         cone[3] = voffset + vertices[0] * vertices[1] + vertices[0];
6619566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
662c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerRight));
663c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerRight));
664c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerRight));
665c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 0, markerRight));
666c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + vertices[0], markerRight));
6677b59f5a9SMichael Lange         iface++;
6687b59f5a9SMichael Lange       }
669552f7358SJed Brown     }
670552f7358SJed Brown   }
6719566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
6729566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
673552f7358SJed Brown   /* Build coordinates */
6749566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, 3));
6759566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
6769566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
6779566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, numFaces, numFaces + numVertices));
6789566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, 3));
679552f7358SJed Brown   for (v = numFaces; v < numFaces + numVertices; ++v) {
6809566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, 3));
6819566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, 3));
682552f7358SJed Brown   }
6839566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
6849566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
6859566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
6869566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
6879566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
6889566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, 3));
6899566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
6909566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
691552f7358SJed Brown   for (vz = 0; vz <= faces[2]; ++vz) {
692552f7358SJed Brown     for (vy = 0; vy <= faces[1]; ++vy) {
693552f7358SJed Brown       for (vx = 0; vx <= faces[0]; ++vx) {
694552f7358SJed Brown         coords[((vz * (faces[1] + 1) + vy) * (faces[0] + 1) + vx) * 3 + 0] = lower[0] + ((upper[0] - lower[0]) / faces[0]) * vx;
695552f7358SJed Brown         coords[((vz * (faces[1] + 1) + vy) * (faces[0] + 1) + vx) * 3 + 1] = lower[1] + ((upper[1] - lower[1]) / faces[1]) * vy;
696552f7358SJed Brown         coords[((vz * (faces[1] + 1) + vy) * (faces[0] + 1) + vx) * 3 + 2] = lower[2] + ((upper[2] - lower[2]) / faces[2]) * vz;
697552f7358SJed Brown       }
698552f7358SJed Brown     }
699552f7358SJed Brown   }
7009566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
7019566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
7029566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
7033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
704552f7358SJed Brown }
705552f7358SJed Brown 
706d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate)
707d71ae5a4SJacob Faibussowitsch {
7089318fe57SMatthew G. Knepley   PetscFunctionBegin;
7099318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
71046139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
7119566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim - 1));
7129566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, dim));
7139318fe57SMatthew G. Knepley   switch (dim) {
714d71ae5a4SJacob Faibussowitsch   case 1:
715d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(dm, lower, upper, faces));
716d71ae5a4SJacob Faibussowitsch     break;
717d71ae5a4SJacob Faibussowitsch   case 2:
718d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(dm, lower, upper, faces));
719d71ae5a4SJacob Faibussowitsch     break;
720d71ae5a4SJacob Faibussowitsch   case 3:
721d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(dm, lower, upper, faces));
722d71ae5a4SJacob Faibussowitsch     break;
723d71ae5a4SJacob Faibussowitsch   default:
724d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Dimension not supported: %" PetscInt_FMT, dim);
7259318fe57SMatthew G. Knepley   }
72646139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
7279566063dSJacob Faibussowitsch   if (interpolate) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
7283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7299318fe57SMatthew G. Knepley }
7309318fe57SMatthew G. Knepley 
7319318fe57SMatthew G. Knepley /*@C
7329318fe57SMatthew G. Knepley   DMPlexCreateBoxSurfaceMesh - Creates a mesh on the surface of the tensor product of unit intervals (box) using tensor cells (hexahedra).
7339318fe57SMatthew G. Knepley 
7349318fe57SMatthew G. Knepley   Collective
7359318fe57SMatthew G. Knepley 
7369318fe57SMatthew G. Knepley   Input Parameters:
737a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
73820f4b53cSBarry Smith . dim         - The spatial dimension of the box, so the resulting mesh is has dimension `dim`-1
73920f4b53cSBarry Smith . faces       - Number of faces per dimension, or `NULL` for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
74020f4b53cSBarry Smith . lower       - The lower left corner, or `NULL` for (0, 0, 0)
74120f4b53cSBarry Smith . upper       - The upper right corner, or `NULL` for (1, 1, 1)
7429318fe57SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
7439318fe57SMatthew G. Knepley 
7449318fe57SMatthew G. Knepley   Output Parameter:
745a1cb98faSBarry Smith . dm - The `DM` object
7469318fe57SMatthew G. Knepley 
7479318fe57SMatthew G. Knepley   Level: beginner
7489318fe57SMatthew G. Knepley 
7491cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMSetFromOptions()`, `DMPlexCreateBoxMesh()`, `DMPlexCreateFromFile()`, `DMSetType()`, `DMCreate()`
7509318fe57SMatthew G. Knepley @*/
751d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateBoxSurfaceMesh(MPI_Comm comm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate, DM *dm)
752d71ae5a4SJacob Faibussowitsch {
7539318fe57SMatthew G. Knepley   PetscInt  fac[3] = {1, 1, 1};
7549318fe57SMatthew G. Knepley   PetscReal low[3] = {0, 0, 0};
7559318fe57SMatthew G. Knepley   PetscReal upp[3] = {1, 1, 1};
7569318fe57SMatthew G. Knepley 
7579318fe57SMatthew G. Knepley   PetscFunctionBegin;
7589566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
7599566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
7609566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(*dm, dim, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, interpolate));
7613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7629318fe57SMatthew G. Knepley }
7639318fe57SMatthew G. Knepley 
764d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateLineMesh_Internal(DM dm, PetscInt segments, PetscReal lower, PetscReal upper, DMBoundaryType bd)
765d71ae5a4SJacob Faibussowitsch {
766fdbf62faSLisandro Dalcin   PetscInt     i, fStart, fEnd, numCells = 0, numVerts = 0;
767fdbf62faSLisandro Dalcin   PetscInt     numPoints[2], *coneSize, *cones, *coneOrientations;
768fdbf62faSLisandro Dalcin   PetscScalar *vertexCoords;
769fdbf62faSLisandro Dalcin   PetscReal    L, maxCell;
770fdbf62faSLisandro Dalcin   PetscBool    markerSeparate = PETSC_FALSE;
771fdbf62faSLisandro Dalcin   PetscInt     markerLeft = 1, faceMarkerLeft = 1;
772fdbf62faSLisandro Dalcin   PetscInt     markerRight = 1, faceMarkerRight = 2;
773fdbf62faSLisandro Dalcin   PetscBool    wrap = (bd == DM_BOUNDARY_PERIODIC || bd == DM_BOUNDARY_TWIST) ? PETSC_TRUE : PETSC_FALSE;
774fdbf62faSLisandro Dalcin   PetscMPIInt  rank;
775fdbf62faSLisandro Dalcin 
776fdbf62faSLisandro Dalcin   PetscFunctionBegin;
7774f572ea9SToby Isaac   PetscAssertPointer(dm, 1);
778fdbf62faSLisandro Dalcin 
7799566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, 1));
7809566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
7819566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
782fdbf62faSLisandro Dalcin 
7839566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
784dd400576SPatrick Sanan   if (rank == 0) numCells = segments;
785dd400576SPatrick Sanan   if (rank == 0) numVerts = segments + (wrap ? 0 : 1);
786fdbf62faSLisandro Dalcin 
7879371c9d4SSatish Balay   numPoints[0] = numVerts;
7889371c9d4SSatish Balay   numPoints[1] = numCells;
7899566063dSJacob Faibussowitsch   PetscCall(PetscMalloc4(numCells + numVerts, &coneSize, numCells * 2, &cones, numCells + numVerts, &coneOrientations, numVerts, &vertexCoords));
7909566063dSJacob Faibussowitsch   PetscCall(PetscArrayzero(coneOrientations, numCells + numVerts));
791ad540459SPierre Jolivet   for (i = 0; i < numCells; ++i) coneSize[i] = 2;
792ad540459SPierre Jolivet   for (i = 0; i < numVerts; ++i) coneSize[numCells + i] = 0;
7939371c9d4SSatish Balay   for (i = 0; i < numCells; ++i) {
7949371c9d4SSatish Balay     cones[2 * i]     = numCells + i % numVerts;
7959371c9d4SSatish Balay     cones[2 * i + 1] = numCells + (i + 1) % numVerts;
7969371c9d4SSatish Balay   }
797ad540459SPierre Jolivet   for (i = 0; i < numVerts; ++i) vertexCoords[i] = lower + (upper - lower) * ((PetscReal)i / (PetscReal)numCells);
7989566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
7999566063dSJacob Faibussowitsch   PetscCall(PetscFree4(coneSize, cones, coneOrientations, vertexCoords));
800fdbf62faSLisandro Dalcin 
8019566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
8029371c9d4SSatish Balay   if (markerSeparate) {
8039371c9d4SSatish Balay     markerLeft  = faceMarkerLeft;
8049371c9d4SSatish Balay     markerRight = faceMarkerRight;
8059371c9d4SSatish Balay   }
806dd400576SPatrick Sanan   if (!wrap && rank == 0) {
8079566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
8089566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", fStart, markerLeft));
8099566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", fEnd - 1, markerRight));
8109566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "Face Sets", fStart, faceMarkerLeft));
8119566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "Face Sets", fEnd - 1, faceMarkerRight));
812fdbf62faSLisandro Dalcin   }
813fdbf62faSLisandro Dalcin   if (wrap) {
814fdbf62faSLisandro Dalcin     L       = upper - lower;
815fdbf62faSLisandro Dalcin     maxCell = (PetscReal)1.1 * (L / (PetscReal)PetscMax(1, segments));
8164fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, &maxCell, &lower, &L));
817fdbf62faSLisandro Dalcin   }
8189566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
8193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
820fdbf62faSLisandro Dalcin }
821fdbf62faSLisandro Dalcin 
822d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxMesh_Simplex_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate)
823d71ae5a4SJacob Faibussowitsch {
8249318fe57SMatthew G. Knepley   DM      boundary, vol;
825c22d3578SMatthew G. Knepley   DMLabel bdlabel;
826d6218766SMatthew G. Knepley 
827d6218766SMatthew G. Knepley   PetscFunctionBegin;
8284f572ea9SToby Isaac   PetscAssertPointer(dm, 1);
829c22d3578SMatthew G. Knepley   for (PetscInt i = 0; i < dim; ++i) PetscCheck(periodicity[i] == DM_BOUNDARY_NONE, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Periodicity is not supported for simplex meshes");
8309566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &boundary));
8319566063dSJacob Faibussowitsch   PetscCall(DMSetType(boundary, DMPLEX));
8329566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(boundary, dim, faces, lower, upper, PETSC_FALSE));
8339566063dSJacob Faibussowitsch   PetscCall(DMPlexGenerate(boundary, NULL, interpolate, &vol));
834c22d3578SMatthew G. Knepley   PetscCall(DMGetLabel(vol, "marker", &bdlabel));
835c22d3578SMatthew G. Knepley   if (bdlabel) PetscCall(DMPlexLabelComplete(vol, bdlabel));
8365de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_FALSE, vol));
83769d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &vol));
8389566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&boundary));
8393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
840d6218766SMatthew G. Knepley }
841d6218766SMatthew G. Knepley 
842d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCubeMesh_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], DMBoundaryType bdX, DMBoundaryType bdY, DMBoundaryType bdZ)
843d71ae5a4SJacob Faibussowitsch {
844ed0e4b50SMatthew G. Knepley   DMLabel     cutLabel  = NULL;
845f4eb4c5dSMatthew G. Knepley   PetscInt    markerTop = 1, faceMarkerTop = 1;
846f4eb4c5dSMatthew G. Knepley   PetscInt    markerBottom = 1, faceMarkerBottom = 1;
847f4eb4c5dSMatthew G. Knepley   PetscInt    markerFront = 1, faceMarkerFront = 1;
848f4eb4c5dSMatthew G. Knepley   PetscInt    markerBack = 1, faceMarkerBack = 1;
849f4eb4c5dSMatthew G. Knepley   PetscInt    markerRight = 1, faceMarkerRight = 1;
850f4eb4c5dSMatthew G. Knepley   PetscInt    markerLeft = 1, faceMarkerLeft = 1;
8513dfda0b1SToby Isaac   PetscInt    dim;
852d8211ee3SMatthew G. Knepley   PetscBool   markerSeparate = PETSC_FALSE, cutMarker = PETSC_FALSE;
8533dfda0b1SToby Isaac   PetscMPIInt rank;
8543dfda0b1SToby Isaac 
8553dfda0b1SToby Isaac   PetscFunctionBegin;
8569566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
8579566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
8589566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
8599566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
8609566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL));
8619371c9d4SSatish Balay   if (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST || bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST || bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST) {
8629371c9d4SSatish Balay     if (cutMarker) {
8639371c9d4SSatish Balay       PetscCall(DMCreateLabel(dm, "periodic_cut"));
8649371c9d4SSatish Balay       PetscCall(DMGetLabel(dm, "periodic_cut", &cutLabel));
8659371c9d4SSatish Balay     }
866d8211ee3SMatthew G. Knepley   }
8673dfda0b1SToby Isaac   switch (dim) {
8683dfda0b1SToby Isaac   case 2:
869f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 3;
870f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
871f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 2;
872f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 4;
8733dfda0b1SToby Isaac     break;
8743dfda0b1SToby Isaac   case 3:
875f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
876f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 2;
877f4eb4c5dSMatthew G. Knepley     faceMarkerFront  = 3;
878f4eb4c5dSMatthew G. Knepley     faceMarkerBack   = 4;
879f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 5;
880f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 6;
8813dfda0b1SToby Isaac     break;
882d71ae5a4SJacob Faibussowitsch   default:
883d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Dimension %" PetscInt_FMT " not supported", dim);
8843dfda0b1SToby Isaac   }
8859566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
886f4eb4c5dSMatthew G. Knepley   if (markerSeparate) {
887f4eb4c5dSMatthew G. Knepley     markerBottom = faceMarkerBottom;
888f4eb4c5dSMatthew G. Knepley     markerTop    = faceMarkerTop;
889f4eb4c5dSMatthew G. Knepley     markerFront  = faceMarkerFront;
890f4eb4c5dSMatthew G. Knepley     markerBack   = faceMarkerBack;
891f4eb4c5dSMatthew G. Knepley     markerRight  = faceMarkerRight;
892f4eb4c5dSMatthew G. Knepley     markerLeft   = faceMarkerLeft;
8933dfda0b1SToby Isaac   }
8943dfda0b1SToby Isaac   {
895dd400576SPatrick Sanan     const PetscInt numXEdges    = rank == 0 ? edges[0] : 0;
896dd400576SPatrick Sanan     const PetscInt numYEdges    = rank == 0 ? edges[1] : 0;
897dd400576SPatrick Sanan     const PetscInt numZEdges    = rank == 0 ? edges[2] : 0;
898dd400576SPatrick Sanan     const PetscInt numXVertices = rank == 0 ? (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ? edges[0] : edges[0] + 1) : 0;
899dd400576SPatrick Sanan     const PetscInt numYVertices = rank == 0 ? (bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ? edges[1] : edges[1] + 1) : 0;
900dd400576SPatrick Sanan     const PetscInt numZVertices = rank == 0 ? (bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST ? edges[2] : edges[2] + 1) : 0;
9013dfda0b1SToby Isaac     const PetscInt numCells     = numXEdges * numYEdges * numZEdges;
9023dfda0b1SToby Isaac     const PetscInt numXFaces    = numYEdges * numZEdges;
9033dfda0b1SToby Isaac     const PetscInt numYFaces    = numXEdges * numZEdges;
9043dfda0b1SToby Isaac     const PetscInt numZFaces    = numXEdges * numYEdges;
9053dfda0b1SToby Isaac     const PetscInt numTotXFaces = numXVertices * numXFaces;
9063dfda0b1SToby Isaac     const PetscInt numTotYFaces = numYVertices * numYFaces;
9073dfda0b1SToby Isaac     const PetscInt numTotZFaces = numZVertices * numZFaces;
9083dfda0b1SToby Isaac     const PetscInt numFaces     = numTotXFaces + numTotYFaces + numTotZFaces;
9093dfda0b1SToby Isaac     const PetscInt numTotXEdges = numXEdges * numYVertices * numZVertices;
9103dfda0b1SToby Isaac     const PetscInt numTotYEdges = numYEdges * numXVertices * numZVertices;
9113dfda0b1SToby Isaac     const PetscInt numTotZEdges = numZEdges * numXVertices * numYVertices;
9123dfda0b1SToby Isaac     const PetscInt numVertices  = numXVertices * numYVertices * numZVertices;
9133dfda0b1SToby Isaac     const PetscInt numEdges     = numTotXEdges + numTotYEdges + numTotZEdges;
9143dfda0b1SToby Isaac     const PetscInt firstVertex  = (dim == 2) ? numFaces : numCells;
9153dfda0b1SToby Isaac     const PetscInt firstXFace   = (dim == 2) ? 0 : numCells + numVertices;
9163dfda0b1SToby Isaac     const PetscInt firstYFace   = firstXFace + numTotXFaces;
9173dfda0b1SToby Isaac     const PetscInt firstZFace   = firstYFace + numTotYFaces;
9183dfda0b1SToby Isaac     const PetscInt firstXEdge   = numCells + numFaces + numVertices;
9193dfda0b1SToby Isaac     const PetscInt firstYEdge   = firstXEdge + numTotXEdges;
9203dfda0b1SToby Isaac     const PetscInt firstZEdge   = firstYEdge + numTotYEdges;
9213dfda0b1SToby Isaac     Vec            coordinates;
9223dfda0b1SToby Isaac     PetscSection   coordSection;
9233dfda0b1SToby Isaac     PetscScalar   *coords;
9243dfda0b1SToby Isaac     PetscInt       coordSize;
9253dfda0b1SToby Isaac     PetscInt       v, vx, vy, vz;
9263dfda0b1SToby Isaac     PetscInt       c, f, fx, fy, fz, e, ex, ey, ez;
9273dfda0b1SToby Isaac 
9289566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells + numFaces + numEdges + numVertices));
92948a46eb9SPierre Jolivet     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 6));
93048a46eb9SPierre Jolivet     for (f = firstXFace; f < firstXFace + numFaces; ++f) PetscCall(DMPlexSetConeSize(dm, f, 4));
93148a46eb9SPierre Jolivet     for (e = firstXEdge; e < firstXEdge + numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2));
9329566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
9333dfda0b1SToby Isaac     /* Build cells */
9343dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
9353dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
9363dfda0b1SToby Isaac         for (fx = 0; fx < numXEdges; ++fx) {
9373dfda0b1SToby Isaac           PetscInt cell  = (fz * numYEdges + fy) * numXEdges + fx;
9383dfda0b1SToby Isaac           PetscInt faceB = firstZFace + (fy * numXEdges + fx) * numZVertices + fz;
9393dfda0b1SToby Isaac           PetscInt faceT = firstZFace + (fy * numXEdges + fx) * numZVertices + ((fz + 1) % numZVertices);
9403dfda0b1SToby Isaac           PetscInt faceF = firstYFace + (fz * numXEdges + fx) * numYVertices + fy;
9413dfda0b1SToby Isaac           PetscInt faceK = firstYFace + (fz * numXEdges + fx) * numYVertices + ((fy + 1) % numYVertices);
9423dfda0b1SToby Isaac           PetscInt faceL = firstXFace + (fz * numYEdges + fy) * numXVertices + fx;
9433dfda0b1SToby Isaac           PetscInt faceR = firstXFace + (fz * numYEdges + fy) * numXVertices + ((fx + 1) % numXVertices);
9443dfda0b1SToby Isaac           /* B,  T,  F,  K,  R,  L */
945b5a892a1SMatthew G. Knepley           PetscInt ornt[6] = {-2, 0, 0, -3, 0, -2}; /* ??? */
94642206facSLisandro Dalcin           PetscInt cone[6];
9473dfda0b1SToby Isaac 
9483dfda0b1SToby Isaac           /* no boundary twisting in 3D */
9499371c9d4SSatish Balay           cone[0] = faceB;
9509371c9d4SSatish Balay           cone[1] = faceT;
9519371c9d4SSatish Balay           cone[2] = faceF;
9529371c9d4SSatish Balay           cone[3] = faceK;
9539371c9d4SSatish Balay           cone[4] = faceR;
9549371c9d4SSatish Balay           cone[5] = faceL;
9559566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, cell, cone));
9569566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, cell, ornt));
9579566063dSJacob Faibussowitsch           if (bdX != DM_BOUNDARY_NONE && fx == numXEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
9589566063dSJacob Faibussowitsch           if (bdY != DM_BOUNDARY_NONE && fy == numYEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
9599566063dSJacob Faibussowitsch           if (bdZ != DM_BOUNDARY_NONE && fz == numZEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
9603dfda0b1SToby Isaac         }
9613dfda0b1SToby Isaac       }
9623dfda0b1SToby Isaac     }
9633dfda0b1SToby Isaac     /* Build x faces */
9643dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
9653dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
9663dfda0b1SToby Isaac         for (fx = 0; fx < numXVertices; ++fx) {
9673dfda0b1SToby Isaac           PetscInt face    = firstXFace + (fz * numYEdges + fy) * numXVertices + fx;
9683dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy * numXVertices + fx) * numZEdges + fz;
9693dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (((fy + 1) % numYVertices) * numXVertices + fx) * numZEdges + fz;
9703dfda0b1SToby Isaac           PetscInt edgeB   = firstYEdge + (fz * numXVertices + fx) * numYEdges + fy;
9713dfda0b1SToby Isaac           PetscInt edgeT   = firstYEdge + (((fz + 1) % numZVertices) * numXVertices + fx) * numYEdges + fy;
972b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
9733dfda0b1SToby Isaac           PetscInt cone[4];
9743dfda0b1SToby Isaac 
9753dfda0b1SToby Isaac           if (dim == 3) {
9763dfda0b1SToby Isaac             /* markers */
9773dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
9783dfda0b1SToby Isaac               if (fx == numXVertices - 1) {
9799566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerRight));
9809566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerRight));
9819371c9d4SSatish Balay               } else if (fx == 0) {
9829566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerLeft));
9839566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerLeft));
9843dfda0b1SToby Isaac               }
9853dfda0b1SToby Isaac             }
9863dfda0b1SToby Isaac           }
9879371c9d4SSatish Balay           cone[0] = edgeB;
9889371c9d4SSatish Balay           cone[1] = edgeR;
9899371c9d4SSatish Balay           cone[2] = edgeT;
9909371c9d4SSatish Balay           cone[3] = edgeL;
9919566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
9929566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
9933dfda0b1SToby Isaac         }
9943dfda0b1SToby Isaac       }
9953dfda0b1SToby Isaac     }
9963dfda0b1SToby Isaac     /* Build y faces */
9973dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
99842206facSLisandro Dalcin       for (fx = 0; fx < numXEdges; ++fx) {
9993dfda0b1SToby Isaac         for (fy = 0; fy < numYVertices; ++fy) {
10003dfda0b1SToby Isaac           PetscInt face    = firstYFace + (fz * numXEdges + fx) * numYVertices + fy;
10013dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy * numXVertices + fx) * numZEdges + fz;
10023dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (fy * numXVertices + ((fx + 1) % numXVertices)) * numZEdges + fz;
10033dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz * numYVertices + fy) * numXEdges + fx;
10043dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (((fz + 1) % numZVertices) * numYVertices + fy) * numXEdges + fx;
1005b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
10063dfda0b1SToby Isaac           PetscInt cone[4];
10073dfda0b1SToby Isaac 
10083dfda0b1SToby Isaac           if (dim == 3) {
10093dfda0b1SToby Isaac             /* markers */
10103dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
10113dfda0b1SToby Isaac               if (fy == numYVertices - 1) {
10129566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerBack));
10139566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerBack));
10149371c9d4SSatish Balay               } else if (fy == 0) {
10159566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerFront));
10169566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerFront));
10173dfda0b1SToby Isaac               }
10183dfda0b1SToby Isaac             }
10193dfda0b1SToby Isaac           }
10209371c9d4SSatish Balay           cone[0] = edgeB;
10219371c9d4SSatish Balay           cone[1] = edgeR;
10229371c9d4SSatish Balay           cone[2] = edgeT;
10239371c9d4SSatish Balay           cone[3] = edgeL;
10249566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
10259566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
10263dfda0b1SToby Isaac         }
10273dfda0b1SToby Isaac       }
10283dfda0b1SToby Isaac     }
10293dfda0b1SToby Isaac     /* Build z faces */
10303dfda0b1SToby Isaac     for (fy = 0; fy < numYEdges; ++fy) {
10313dfda0b1SToby Isaac       for (fx = 0; fx < numXEdges; ++fx) {
10323dfda0b1SToby Isaac         for (fz = 0; fz < numZVertices; fz++) {
10333dfda0b1SToby Isaac           PetscInt face    = firstZFace + (fy * numXEdges + fx) * numZVertices + fz;
10343dfda0b1SToby Isaac           PetscInt edgeL   = firstYEdge + (fz * numXVertices + fx) * numYEdges + fy;
10353dfda0b1SToby Isaac           PetscInt edgeR   = firstYEdge + (fz * numXVertices + ((fx + 1) % numXVertices)) * numYEdges + fy;
10363dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz * numYVertices + fy) * numXEdges + fx;
10373dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (fz * numYVertices + ((fy + 1) % numYVertices)) * numXEdges + fx;
1038b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
10393dfda0b1SToby Isaac           PetscInt cone[4];
10403dfda0b1SToby Isaac 
10413dfda0b1SToby Isaac           if (dim == 2) {
10429371c9d4SSatish Balay             if (bdX == DM_BOUNDARY_TWIST && fx == numXEdges - 1) {
10439371c9d4SSatish Balay               edgeR += numYEdges - 1 - 2 * fy;
10449371c9d4SSatish Balay               ornt[1] = -1;
10459371c9d4SSatish Balay             }
10469371c9d4SSatish Balay             if (bdY == DM_BOUNDARY_TWIST && fy == numYEdges - 1) {
10479371c9d4SSatish Balay               edgeT += numXEdges - 1 - 2 * fx;
10489371c9d4SSatish Balay               ornt[2] = 0;
10499371c9d4SSatish Balay             }
10509566063dSJacob Faibussowitsch             if (bdX != DM_BOUNDARY_NONE && fx == numXEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, face, 2));
10519566063dSJacob Faibussowitsch             if (bdY != DM_BOUNDARY_NONE && fy == numYEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, face, 2));
1052d1c88043SMatthew G. Knepley           } else {
10533dfda0b1SToby Isaac             /* markers */
10543dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
10553dfda0b1SToby Isaac               if (fz == numZVertices - 1) {
10569566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerTop));
10579566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerTop));
10589371c9d4SSatish Balay               } else if (fz == 0) {
10599566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerBottom));
10609566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerBottom));
10613dfda0b1SToby Isaac               }
10623dfda0b1SToby Isaac             }
10633dfda0b1SToby Isaac           }
10649371c9d4SSatish Balay           cone[0] = edgeB;
10659371c9d4SSatish Balay           cone[1] = edgeR;
10669371c9d4SSatish Balay           cone[2] = edgeT;
10679371c9d4SSatish Balay           cone[3] = edgeL;
10689566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
10699566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
10703dfda0b1SToby Isaac         }
10713dfda0b1SToby Isaac       }
10723dfda0b1SToby Isaac     }
10733dfda0b1SToby Isaac     /* Build Z edges*/
10743dfda0b1SToby Isaac     for (vy = 0; vy < numYVertices; vy++) {
10753dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
10763dfda0b1SToby Isaac         for (ez = 0; ez < numZEdges; ez++) {
10773dfda0b1SToby Isaac           const PetscInt edge    = firstZEdge + (vy * numXVertices + vx) * numZEdges + ez;
10783dfda0b1SToby Isaac           const PetscInt vertexB = firstVertex + (ez * numYVertices + vy) * numXVertices + vx;
10793dfda0b1SToby Isaac           const PetscInt vertexT = firstVertex + (((ez + 1) % numZVertices) * numYVertices + vy) * numXVertices + vx;
10803dfda0b1SToby Isaac           PetscInt       cone[2];
10813dfda0b1SToby Isaac 
10829371c9d4SSatish Balay           cone[0] = vertexB;
10839371c9d4SSatish Balay           cone[1] = vertexT;
1084c2df9bbfSMatthew G. Knepley           PetscCall(DMPlexSetCone(dm, edge, cone));
10853dfda0b1SToby Isaac           if (dim == 3) {
10863dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
10873dfda0b1SToby Isaac               if (vx == numXVertices - 1) {
10889566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
1089c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1090c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1091c2df9bbfSMatthew G. Knepley               } else if (vx == 0) {
10929566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
1093c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1094c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
10953dfda0b1SToby Isaac               }
10963dfda0b1SToby Isaac             }
10973dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
10983dfda0b1SToby Isaac               if (vy == numYVertices - 1) {
10999566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBack));
1100c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBack));
1101c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBack));
1102c2df9bbfSMatthew G. Knepley               } else if (vy == 0) {
11039566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerFront));
1104c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerFront));
1105c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerFront));
11063dfda0b1SToby Isaac               }
11073dfda0b1SToby Isaac             }
11083dfda0b1SToby Isaac           }
11093dfda0b1SToby Isaac         }
11103dfda0b1SToby Isaac       }
11113dfda0b1SToby Isaac     }
11123dfda0b1SToby Isaac     /* Build Y edges*/
11133dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
11143dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
11153dfda0b1SToby Isaac         for (ey = 0; ey < numYEdges; ey++) {
11163dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdY == DM_BOUNDARY_TWIST && ey == numYEdges - 1) ? (numXVertices - vx - 1) : (vz * numYVertices + ((ey + 1) % numYVertices)) * numXVertices + vx;
11173dfda0b1SToby Isaac           const PetscInt edge    = firstYEdge + (vz * numXVertices + vx) * numYEdges + ey;
11183dfda0b1SToby Isaac           const PetscInt vertexF = firstVertex + (vz * numYVertices + ey) * numXVertices + vx;
11193dfda0b1SToby Isaac           const PetscInt vertexK = firstVertex + nextv;
11203dfda0b1SToby Isaac           PetscInt       cone[2];
11213dfda0b1SToby Isaac 
11229371c9d4SSatish Balay           cone[0] = vertexF;
11239371c9d4SSatish Balay           cone[1] = vertexK;
11249566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, edge, cone));
11253dfda0b1SToby Isaac           if (dim == 2) {
11263dfda0b1SToby Isaac             if ((bdX != DM_BOUNDARY_PERIODIC) && (bdX != DM_BOUNDARY_TWIST)) {
11273dfda0b1SToby Isaac               if (vx == numXVertices - 1) {
11289566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerRight));
11299566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
11309566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1131c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1132d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
11339566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerLeft));
11349566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
11359566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1136c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
11373dfda0b1SToby Isaac               }
1138d8211ee3SMatthew G. Knepley             } else {
11394c67ea77SStefano Zampini               if (vx == 0 && cutLabel) {
11409566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, edge, 1));
11419566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, cone[0], 1));
1142c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMLabelSetValue(cutLabel, cone[1], 1));
11433dfda0b1SToby Isaac               }
1144d8211ee3SMatthew G. Knepley             }
1145d8211ee3SMatthew G. Knepley           } else {
11463dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
11473dfda0b1SToby Isaac               if (vx == numXVertices - 1) {
11489566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
1149c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1150c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1151d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
11529566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
1153c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1154c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
11553dfda0b1SToby Isaac               }
11563dfda0b1SToby Isaac             }
11573dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
11583dfda0b1SToby Isaac               if (vz == numZVertices - 1) {
11599566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
1160c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1161c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1162d8211ee3SMatthew G. Knepley               } else if (vz == 0) {
11639566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
1164c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1165c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
11663dfda0b1SToby Isaac               }
11673dfda0b1SToby Isaac             }
11683dfda0b1SToby Isaac           }
11693dfda0b1SToby Isaac         }
11703dfda0b1SToby Isaac       }
11713dfda0b1SToby Isaac     }
11723dfda0b1SToby Isaac     /* Build X edges*/
11733dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
11743dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; vy++) {
11753dfda0b1SToby Isaac         for (ex = 0; ex < numXEdges; ex++) {
11763dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdX == DM_BOUNDARY_TWIST && ex == numXEdges - 1) ? (numYVertices - vy - 1) * numXVertices : (vz * numYVertices + vy) * numXVertices + (ex + 1) % numXVertices;
11773dfda0b1SToby Isaac           const PetscInt edge    = firstXEdge + (vz * numYVertices + vy) * numXEdges + ex;
11783dfda0b1SToby Isaac           const PetscInt vertexL = firstVertex + (vz * numYVertices + vy) * numXVertices + ex;
11793dfda0b1SToby Isaac           const PetscInt vertexR = firstVertex + nextv;
11803dfda0b1SToby Isaac           PetscInt       cone[2];
11813dfda0b1SToby Isaac 
11829371c9d4SSatish Balay           cone[0] = vertexL;
11839371c9d4SSatish Balay           cone[1] = vertexR;
11849566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, edge, cone));
11853dfda0b1SToby Isaac           if (dim == 2) {
11863dfda0b1SToby Isaac             if ((bdY != DM_BOUNDARY_PERIODIC) && (bdY != DM_BOUNDARY_TWIST)) {
11873dfda0b1SToby Isaac               if (vy == numYVertices - 1) {
11889566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerTop));
11899566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
11909566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1191c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1192d8211ee3SMatthew G. Knepley               } else if (vy == 0) {
11939566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerBottom));
11949566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
11959566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1196c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
11973dfda0b1SToby Isaac               }
1198d8211ee3SMatthew G. Knepley             } else {
11994c67ea77SStefano Zampini               if (vy == 0 && cutLabel) {
12009566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, edge, 1));
12019566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, cone[0], 1));
1202c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMLabelSetValue(cutLabel, cone[1], 1));
12033dfda0b1SToby Isaac               }
1204d8211ee3SMatthew G. Knepley             }
1205d8211ee3SMatthew G. Knepley           } else {
12063dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
12073dfda0b1SToby Isaac               if (vy == numYVertices - 1) {
12089566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBack));
1209c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBack));
1210c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBack));
1211c2df9bbfSMatthew G. Knepley               } else if (vy == 0) {
12129566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerFront));
1213c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerFront));
1214c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerFront));
12153dfda0b1SToby Isaac               }
12163dfda0b1SToby Isaac             }
12173dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
12183dfda0b1SToby Isaac               if (vz == numZVertices - 1) {
12199566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
1220c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1221c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1222c2df9bbfSMatthew G. Knepley               } else if (vz == 0) {
12239566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
1224c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1225c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
12263dfda0b1SToby Isaac               }
12273dfda0b1SToby Isaac             }
12283dfda0b1SToby Isaac           }
12293dfda0b1SToby Isaac         }
12303dfda0b1SToby Isaac       }
12313dfda0b1SToby Isaac     }
12329566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
12339566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
12343dfda0b1SToby Isaac     /* Build coordinates */
12359566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
12369566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
12379566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
12389566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numVertices));
12393dfda0b1SToby Isaac     for (v = firstVertex; v < firstVertex + numVertices; ++v) {
12409566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
12419566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
12423dfda0b1SToby Isaac     }
12439566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
12449566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
12459566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
12469566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
12479566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
12489566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
12499566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates, VECSTANDARD));
12509566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
12513dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; ++vz) {
12523dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; ++vy) {
12533dfda0b1SToby Isaac         for (vx = 0; vx < numXVertices; ++vx) {
12543dfda0b1SToby Isaac           coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 0] = lower[0] + ((upper[0] - lower[0]) / numXEdges) * vx;
12553dfda0b1SToby Isaac           coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 1] = lower[1] + ((upper[1] - lower[1]) / numYEdges) * vy;
1256ad540459SPierre Jolivet           if (dim == 3) coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 2] = lower[2] + ((upper[2] - lower[2]) / numZEdges) * vz;
12573dfda0b1SToby Isaac         }
12583dfda0b1SToby Isaac       }
12593dfda0b1SToby Isaac     }
12609566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
12619566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
12629566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
12633dfda0b1SToby Isaac   }
12643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12653dfda0b1SToby Isaac }
12663dfda0b1SToby Isaac 
1267d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxMesh_Tensor_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
1268d71ae5a4SJacob Faibussowitsch {
12699318fe57SMatthew G. Knepley   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
12709318fe57SMatthew G. Knepley   PetscInt       fac[3] = {0, 0, 0}, d;
1271552f7358SJed Brown 
1272552f7358SJed Brown   PetscFunctionBegin;
12734f572ea9SToby Isaac   PetscAssertPointer(dm, 1);
12749318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
12759566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
12769371c9d4SSatish Balay   for (d = 0; d < dim; ++d) {
12779371c9d4SSatish Balay     fac[d] = faces[d];
12789371c9d4SSatish Balay     bdt[d] = periodicity[d];
12799371c9d4SSatish Balay   }
12809566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateCubeMesh_Internal(dm, lower, upper, fac, bdt[0], bdt[1], bdt[2]));
12819371c9d4SSatish Balay   if (periodicity[0] == DM_BOUNDARY_PERIODIC || periodicity[0] == DM_BOUNDARY_TWIST || periodicity[1] == DM_BOUNDARY_PERIODIC || periodicity[1] == DM_BOUNDARY_TWIST || (dim > 2 && (periodicity[2] == DM_BOUNDARY_PERIODIC || periodicity[2] == DM_BOUNDARY_TWIST))) {
12826858538eSMatthew G. Knepley     PetscReal L[3]       = {-1., -1., 0.};
12836858538eSMatthew G. Knepley     PetscReal maxCell[3] = {-1., -1., 0.};
1284552f7358SJed Brown 
12859318fe57SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
12866858538eSMatthew G. Knepley       if (periodicity[d] != DM_BOUNDARY_NONE) {
12879318fe57SMatthew G. Knepley         L[d]       = upper[d] - lower[d];
12889318fe57SMatthew G. Knepley         maxCell[d] = 1.1 * (L[d] / PetscMax(1, faces[d]));
1289768d5fceSMatthew G. Knepley       }
12906858538eSMatthew G. Knepley     }
12914fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, maxCell, lower, L));
1292768d5fceSMatthew G. Knepley   }
12939566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
12943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12959318fe57SMatthew G. Knepley }
12969318fe57SMatthew G. Knepley 
12975dca41c3SJed Brown static PetscErrorCode DMPlexCreateBoxMesh_Internal(DM dm, DMPlexShape shape, PetscInt dim, PetscBool simplex, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate)
1298d71ae5a4SJacob Faibussowitsch {
12999318fe57SMatthew G. Knepley   PetscFunctionBegin;
130046139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
13015dca41c3SJed Brown   if (shape == DM_SHAPE_ZBOX) PetscCall(DMPlexCreateBoxMesh_Tensor_SFC_Internal(dm, dim, faces, lower, upper, periodicity, interpolate));
13026725e60dSJed Brown   else if (dim == 1) PetscCall(DMPlexCreateLineMesh_Internal(dm, faces[0], lower[0], upper[0], periodicity[0]));
13039566063dSJacob Faibussowitsch   else if (simplex) PetscCall(DMPlexCreateBoxMesh_Simplex_Internal(dm, dim, faces, lower, upper, periodicity, interpolate));
13049566063dSJacob Faibussowitsch   else PetscCall(DMPlexCreateBoxMesh_Tensor_Internal(dm, dim, faces, lower, upper, periodicity));
13059318fe57SMatthew G. Knepley   if (!interpolate && dim > 1 && !simplex) {
1306768d5fceSMatthew G. Knepley     DM udm;
1307768d5fceSMatthew G. Knepley 
13089566063dSJacob Faibussowitsch     PetscCall(DMPlexUninterpolate(dm, &udm));
13099566063dSJacob Faibussowitsch     PetscCall(DMPlexCopyCoordinates(dm, udm));
131069d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &udm));
1311768d5fceSMatthew G. Knepley   }
131246139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
13133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1314c8c68bd8SToby Isaac }
1315c8c68bd8SToby Isaac 
1316768d5fceSMatthew G. Knepley /*@C
1317768d5fceSMatthew G. Knepley   DMPlexCreateBoxMesh - Creates a mesh on the tensor product of unit intervals (box) using simplices or tensor cells (hexahedra).
1318768d5fceSMatthew G. Knepley 
1319d083f849SBarry Smith   Collective
1320768d5fceSMatthew G. Knepley 
1321768d5fceSMatthew G. Knepley   Input Parameters:
1322a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
1323768d5fceSMatthew G. Knepley . dim         - The spatial dimension
1324a1cb98faSBarry Smith . simplex     - `PETSC_TRUE` for simplices, `PETSC_FALSE` for tensor cells
132520f4b53cSBarry Smith . faces       - Number of faces per dimension, or `NULL` for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
132620f4b53cSBarry Smith . lower       - The lower left corner, or `NULL` for (0, 0, 0)
132720f4b53cSBarry Smith . upper       - The upper right corner, or `NULL` for (1, 1, 1)
132820f4b53cSBarry Smith . periodicity - The boundary type for the X,Y,Z direction, or `NULL` for `DM_BOUNDARY_NONE`
1329768d5fceSMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
1330768d5fceSMatthew G. Knepley 
1331768d5fceSMatthew G. Knepley   Output Parameter:
1332a1cb98faSBarry Smith . dm - The `DM` object
1333768d5fceSMatthew G. Knepley 
1334768d5fceSMatthew G. Knepley   Level: beginner
1335768d5fceSMatthew G. Knepley 
1336a1cb98faSBarry Smith   Note:
1337a1cb98faSBarry Smith   To customize this mesh using options, use
1338a1cb98faSBarry Smith .vb
1339a1cb98faSBarry Smith   DMCreate(comm, &dm);
1340a1cb98faSBarry Smith   DMSetType(dm, DMPLEX);
1341a1cb98faSBarry Smith   DMSetFromOptions(dm);
1342a1cb98faSBarry Smith .ve
1343a1cb98faSBarry Smith   and use the options in `DMSetFromOptions()`.
1344a1cb98faSBarry Smith 
1345a4e35b19SJacob Faibussowitsch   Here is the numbering returned for 2 faces in each direction for tensor cells\:
1346a1cb98faSBarry Smith .vb
1347a1cb98faSBarry Smith  10---17---11---18----12
1348a1cb98faSBarry Smith   |         |         |
1349a1cb98faSBarry Smith   |         |         |
1350a1cb98faSBarry Smith  20    2   22    3    24
1351a1cb98faSBarry Smith   |         |         |
1352a1cb98faSBarry Smith   |         |         |
1353a1cb98faSBarry Smith   7---15----8---16----9
1354a1cb98faSBarry Smith   |         |         |
1355a1cb98faSBarry Smith   |         |         |
1356a1cb98faSBarry Smith  19    0   21    1   23
1357a1cb98faSBarry Smith   |         |         |
1358a1cb98faSBarry Smith   |         |         |
1359a1cb98faSBarry Smith   4---13----5---14----6
1360a1cb98faSBarry Smith .ve
1361a1cb98faSBarry Smith   and for simplicial cells
1362a1cb98faSBarry Smith .vb
1363a1cb98faSBarry Smith  14----8---15----9----16
1364a1cb98faSBarry Smith   |\     5  |\      7 |
1365a1cb98faSBarry Smith   | \       | \       |
1366a1cb98faSBarry Smith  13   2    14    3    15
1367a1cb98faSBarry Smith   | 4   \   | 6   \   |
1368a1cb98faSBarry Smith   |       \ |       \ |
1369a1cb98faSBarry Smith  11----6---12----7----13
1370a1cb98faSBarry Smith   |\        |\        |
1371a1cb98faSBarry Smith   | \    1  | \     3 |
1372a1cb98faSBarry Smith  10   0    11    1    12
1373a1cb98faSBarry Smith   | 0   \   | 2   \   |
1374a1cb98faSBarry Smith   |       \ |       \ |
1375a1cb98faSBarry Smith   8----4----9----5----10
1376a1cb98faSBarry Smith .ve
1377a1cb98faSBarry Smith 
13781cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMSetFromOptions()`, `DMPlexCreateFromFile()`, `DMPlexCreateHexCylinderMesh()`, `DMSetType()`, `DMCreate()`
1379768d5fceSMatthew G. Knepley @*/
1380d71ae5a4SJacob Faibussowitsch 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)
1381d71ae5a4SJacob Faibussowitsch {
13829318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
1383fdbf62faSLisandro Dalcin   PetscReal      low[3] = {0, 0, 0};
1384fdbf62faSLisandro Dalcin   PetscReal      upp[3] = {1, 1, 1};
1385fdbf62faSLisandro Dalcin   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
1386552f7358SJed Brown 
1387768d5fceSMatthew G. Knepley   PetscFunctionBegin;
13889566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
13899566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
13905dca41c3SJed Brown   PetscCall(DMPlexCreateBoxMesh_Internal(*dm, DM_SHAPE_BOX, dim, simplex, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt, interpolate));
13917ff04441SMatthew G. Knepley   if (periodicity) PetscCall(DMLocalizeCoordinates(*dm));
13923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13939318fe57SMatthew G. Knepley }
1394fdbf62faSLisandro Dalcin 
1395d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateWedgeBoxMesh_Internal(DM dm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
1396d71ae5a4SJacob Faibussowitsch {
13979318fe57SMatthew G. Knepley   DM       bdm, vol;
13989318fe57SMatthew G. Knepley   PetscInt i;
13999318fe57SMatthew G. Knepley 
14009318fe57SMatthew G. Knepley   PetscFunctionBegin;
14011fcf445aSMatthew G. Knepley   // TODO Now we can support periodicity
140208401ef6SPierre Jolivet   for (i = 0; i < 3; ++i) PetscCheck(periodicity[i] == DM_BOUNDARY_NONE, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Periodicity not yet supported");
14039566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &bdm));
14049566063dSJacob Faibussowitsch   PetscCall(DMSetType(bdm, DMPLEX));
14059566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(bdm, 2));
140646139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, bdm, 0, 0, 0));
14079566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxMesh_Simplex_Internal(bdm, 2, faces, lower, upper, periodicity, PETSC_TRUE));
14081fcf445aSMatthew G. Knepley   PetscCall(DMPlexExtrude(bdm, faces[2], upper[2] - lower[2], PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, NULL, NULL, &vol));
140946139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, bdm, 0, 0, 0));
14109566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&bdm));
141169d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &vol));
14129318fe57SMatthew G. Knepley   if (lower[2] != 0.0) {
14139318fe57SMatthew G. Knepley     Vec          v;
14149318fe57SMatthew G. Knepley     PetscScalar *x;
14159318fe57SMatthew G. Knepley     PetscInt     cDim, n;
14169318fe57SMatthew G. Knepley 
14179566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &v));
14189566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(v, &cDim));
14199566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(v, &n));
14209566063dSJacob Faibussowitsch     PetscCall(VecGetArray(v, &x));
14219318fe57SMatthew G. Knepley     x += cDim;
14229318fe57SMatthew G. Knepley     for (i = 0; i < n; i += cDim) x[i] += lower[2];
14239566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(v, &x));
14249566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, v));
14259318fe57SMatthew G. Knepley   }
14263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1427552f7358SJed Brown }
1428552f7358SJed Brown 
142900dabe28SStefano Zampini /*@
143000dabe28SStefano Zampini   DMPlexCreateWedgeBoxMesh - Creates a 3-D mesh tesselating the (x,y) plane and extruding in the third direction using wedge cells.
143100dabe28SStefano Zampini 
1432d083f849SBarry Smith   Collective
143300dabe28SStefano Zampini 
143400dabe28SStefano Zampini   Input Parameters:
1435a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
143620f4b53cSBarry Smith . faces       - Number of faces per dimension, or `NULL` for (1, 1, 1)
143720f4b53cSBarry Smith . lower       - The lower left corner, or `NULL` for (0, 0, 0)
143820f4b53cSBarry Smith . upper       - The upper right corner, or `NULL` for (1, 1, 1)
143920f4b53cSBarry Smith . periodicity - The boundary type for the X,Y,Z direction, or `NULL` for `DM_BOUNDARY_NONE`
1440a1cb98faSBarry Smith . orderHeight - If `PETSC_TRUE`, orders the extruded cells in the height first. Otherwise, orders the cell on the layers first
144100dabe28SStefano Zampini - interpolate - Flag to create intermediate mesh pieces (edges, faces)
144200dabe28SStefano Zampini 
144300dabe28SStefano Zampini   Output Parameter:
1444a1cb98faSBarry Smith . dm - The `DM` object
144500dabe28SStefano Zampini 
144600dabe28SStefano Zampini   Level: beginner
144700dabe28SStefano Zampini 
14481cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateHexCylinderMesh()`, `DMPlexCreateWedgeCylinderMesh()`, `DMExtrude()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
144900dabe28SStefano Zampini @*/
1450d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateWedgeBoxMesh(MPI_Comm comm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool orderHeight, PetscBool interpolate, DM *dm)
1451d71ae5a4SJacob Faibussowitsch {
14529318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
145300dabe28SStefano Zampini   PetscReal      low[3] = {0, 0, 0};
145400dabe28SStefano Zampini   PetscReal      upp[3] = {1, 1, 1};
145500dabe28SStefano Zampini   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
145600dabe28SStefano Zampini 
145700dabe28SStefano Zampini   PetscFunctionBegin;
14589566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
14599566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
14609566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateWedgeBoxMesh_Internal(*dm, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt));
1461d410b0cfSMatthew G. Knepley   if (!interpolate) {
1462d410b0cfSMatthew G. Knepley     DM udm;
146300dabe28SStefano Zampini 
14649566063dSJacob Faibussowitsch     PetscCall(DMPlexUninterpolate(*dm, &udm));
146569d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(*dm, &udm));
146600dabe28SStefano Zampini   }
14677ff04441SMatthew G. Knepley   if (periodicity) PetscCall(DMLocalizeCoordinates(*dm));
14683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
146900dabe28SStefano Zampini }
147000dabe28SStefano Zampini 
1471cfb853baSMatthew G. Knepley /*
1472cfb853baSMatthew G. Knepley   DMPlexTensorPointLexicographic_Private - Returns all tuples of size 'len' with nonnegative integers that are all less than or equal to 'max' for that dimension.
1473cfb853baSMatthew G. Knepley 
1474cfb853baSMatthew G. Knepley   Input Parameters:
1475cfb853baSMatthew G. Knepley + len - The length of the tuple
1476cfb853baSMatthew G. Knepley . max - The maximum for each dimension, so values are in [0, max)
1477cfb853baSMatthew G. Knepley - tup - A tuple of length len+1: tup[len] > 0 indicates a stopping condition
1478cfb853baSMatthew G. Knepley 
1479cfb853baSMatthew G. Knepley   Output Parameter:
148020f4b53cSBarry Smith . tup - A tuple of `len` integers whose entries are at most `max`
1481cfb853baSMatthew G. Knepley 
1482cfb853baSMatthew G. Knepley   Level: developer
1483cfb853baSMatthew G. Knepley 
148420f4b53cSBarry Smith   Note:
148520f4b53cSBarry Smith   Ordering is lexicographic with lowest index as least significant in ordering.
148620f4b53cSBarry Smith   e.g. for len == 2 and max == 2, this will return, in order, {0,0}, {1,0}, {2,0}, {0,1}, {1,1}, {2,1}, {0,2}, {1,2}, {2,2}.
148720f4b53cSBarry Smith 
1488cfb853baSMatthew G. Knepley .seealso: PetscDualSpaceTensorPointLexicographic_Internal(), PetscDualSpaceLatticePointLexicographic_Internal()
1489cfb853baSMatthew G. Knepley */
1490cfb853baSMatthew G. Knepley static PetscErrorCode DMPlexTensorPointLexicographic_Private(PetscInt len, const PetscInt max[], PetscInt tup[])
1491cfb853baSMatthew G. Knepley {
1492cfb853baSMatthew G. Knepley   PetscInt i;
1493cfb853baSMatthew G. Knepley 
1494cfb853baSMatthew G. Knepley   PetscFunctionBegin;
1495cfb853baSMatthew G. Knepley   for (i = 0; i < len; ++i) {
1496cfb853baSMatthew G. Knepley     if (tup[i] < max[i] - 1) {
1497cfb853baSMatthew G. Knepley       break;
1498cfb853baSMatthew G. Knepley     } else {
1499cfb853baSMatthew G. Knepley       tup[i] = 0;
1500cfb853baSMatthew G. Knepley     }
1501cfb853baSMatthew G. Knepley   }
1502cfb853baSMatthew G. Knepley   if (i == len) tup[i - 1] = max[i - 1];
1503cfb853baSMatthew G. Knepley   else ++tup[i];
15043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1505cfb853baSMatthew G. Knepley }
1506cfb853baSMatthew G. Knepley 
1507cfb853baSMatthew G. Knepley static PetscInt TupleToIndex_Private(PetscInt len, const PetscInt max[], const PetscInt tup[])
1508cfb853baSMatthew G. Knepley {
1509cfb853baSMatthew G. Knepley   PetscInt i, idx = tup[len - 1];
1510cfb853baSMatthew G. Knepley 
1511cfb853baSMatthew G. Knepley   for (i = len - 2; i >= 0; --i) {
1512cfb853baSMatthew G. Knepley     idx *= max[i];
1513cfb853baSMatthew G. Knepley     idx += tup[i];
1514cfb853baSMatthew G. Knepley   }
1515cfb853baSMatthew G. Knepley   return idx;
1516cfb853baSMatthew G. Knepley }
1517cfb853baSMatthew G. Knepley 
1518cfb853baSMatthew G. Knepley static PetscErrorCode DestroyExtent_Private(void *extent)
1519cfb853baSMatthew G. Knepley {
1520cfb853baSMatthew G. Knepley   return PetscFree(extent);
1521cfb853baSMatthew G. Knepley }
1522cfb853baSMatthew G. Knepley 
1523cfb853baSMatthew G. Knepley static PetscErrorCode DMPlexCreateHypercubicMesh_Internal(DM dm, PetscInt dim, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], const DMBoundaryType bd[])
1524cfb853baSMatthew G. Knepley {
1525cfb853baSMatthew G. Knepley   Vec          coordinates;
1526cfb853baSMatthew G. Knepley   PetscSection coordSection;
1527cfb853baSMatthew G. Knepley   DMLabel      cutLabel    = NULL;
1528cfb853baSMatthew G. Knepley   PetscBool    cutMarker   = PETSC_FALSE;
1529cfb853baSMatthew G. Knepley   PetscBool    periodic    = PETSC_FALSE;
1530cfb853baSMatthew G. Knepley   PetscInt     numCells    = 1, c;
1531cfb853baSMatthew G. Knepley   PetscInt     numVertices = 1, v;
1532cfb853baSMatthew G. Knepley   PetscScalar *coords;
1533cfb853baSMatthew G. Knepley   PetscInt    *vertices, *vert, *vtmp, *supp, cone[2];
1534cfb853baSMatthew G. Knepley   PetscInt     d, e, cell = 0, coordSize;
1535cfb853baSMatthew G. Knepley   PetscMPIInt  rank;
1536cfb853baSMatthew G. Knepley 
1537cfb853baSMatthew G. Knepley   PetscFunctionBegin;
1538cfb853baSMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
1539cfb853baSMatthew G. Knepley   PetscCall(DMSetDimension(dm, dim));
1540cfb853baSMatthew G. Knepley   PetscCall(PetscCalloc4(dim, &vertices, dim, &vert, dim, &vtmp, 2 * dim, &supp));
1541cfb853baSMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "marker"));
1542cfb853baSMatthew G. Knepley   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL));
1543cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) periodic = (periodic || bd[d] == DM_BOUNDARY_PERIODIC) ? PETSC_TRUE : PETSC_FALSE;
1544cfb853baSMatthew G. Knepley   if (periodic && cutMarker) {
1545cfb853baSMatthew G. Knepley     PetscCall(DMCreateLabel(dm, "periodic_cut"));
1546cfb853baSMatthew G. Knepley     PetscCall(DMGetLabel(dm, "periodic_cut", &cutLabel));
1547cfb853baSMatthew G. Knepley   }
1548cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) PetscCheck(bd[d] == DM_BOUNDARY_PERIODIC, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Hypercubic mesh must be periodic now");
1549cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) {
1550cfb853baSMatthew G. Knepley     vertices[d] = edges[d];
1551cfb853baSMatthew G. Knepley     numVertices *= vertices[d];
1552cfb853baSMatthew G. Knepley   }
1553cfb853baSMatthew G. Knepley   numCells = numVertices * dim;
1554cfb853baSMatthew G. Knepley   PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
1555cfb853baSMatthew G. Knepley   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, 2));
1556cfb853baSMatthew G. Knepley   for (v = numCells; v < numCells + numVertices; ++v) PetscCall(DMPlexSetSupportSize(dm, v, 2 * dim));
1557cfb853baSMatthew G. Knepley   /* TODO Loop over boundary and reset support sizes */
1558cfb853baSMatthew G. Knepley   PetscCall(DMSetUp(dm)); /* Allocate space for cones and supports */
1559cfb853baSMatthew G. Knepley   /* Build cell cones and vertex supports */
1560cfb853baSMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "celltype"));
1561cfb853baSMatthew G. Knepley   while (vert[dim - 1] < vertices[dim - 1]) {
1562cfb853baSMatthew G. Knepley     const PetscInt vertex = TupleToIndex_Private(dim, vertices, vert) + numCells;
1563cfb853baSMatthew G. Knepley     PetscInt       s      = 0;
1564cfb853baSMatthew G. Knepley 
15653ba16761SJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "Vertex %" PetscInt_FMT ":", vertex));
15663ba16761SJacob Faibussowitsch     for (d = 0; d < dim; ++d) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, vert[d]));
15673ba16761SJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
1568cfb853baSMatthew G. Knepley     PetscCall(DMPlexSetCellType(dm, vertex, DM_POLYTOPE_POINT));
1569cfb853baSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
1570cfb853baSMatthew G. Knepley       for (e = 0; e < dim; ++e) vtmp[e] = vert[e];
1571cfb853baSMatthew G. Knepley       vtmp[d] = (vert[d] + 1) % vertices[d];
1572cfb853baSMatthew G. Knepley       cone[0] = vertex;
1573cfb853baSMatthew G. Knepley       cone[1] = TupleToIndex_Private(dim, vertices, vtmp) + numCells;
15743ba16761SJacob Faibussowitsch       PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Vertex %" PetscInt_FMT ":", cone[1]));
15753ba16761SJacob Faibussowitsch       for (e = 0; e < dim; ++e) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, vtmp[e]));
15763ba16761SJacob Faibussowitsch       PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
1577cfb853baSMatthew G. Knepley       PetscCall(DMPlexSetCone(dm, cell, cone));
1578cfb853baSMatthew G. Knepley       PetscCall(DMPlexSetCellType(dm, cell, DM_POLYTOPE_SEGMENT));
15793ba16761SJacob Faibussowitsch       PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Edge %" PetscInt_FMT " (%" PetscInt_FMT " %" PetscInt_FMT ")\n", cell, cone[0], cone[1]));
1580cfb853baSMatthew G. Knepley       ++cell;
1581cfb853baSMatthew G. Knepley     }
1582cfb853baSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
1583cfb853baSMatthew G. Knepley       for (e = 0; e < dim; ++e) vtmp[e] = vert[e];
1584cfb853baSMatthew G. Knepley       vtmp[d]   = (vert[d] + vertices[d] - 1) % vertices[d];
1585cfb853baSMatthew G. Knepley       supp[s++] = TupleToIndex_Private(dim, vertices, vtmp) * dim + d;
1586cfb853baSMatthew G. Knepley       supp[s++] = (vertex - numCells) * dim + d;
1587cfb853baSMatthew G. Knepley       PetscCall(DMPlexSetSupport(dm, vertex, supp));
1588cfb853baSMatthew G. Knepley     }
1589cfb853baSMatthew G. Knepley     PetscCall(DMPlexTensorPointLexicographic_Private(dim, vertices, vert));
1590cfb853baSMatthew G. Knepley   }
1591cfb853baSMatthew G. Knepley   PetscCall(DMPlexStratify(dm));
1592cfb853baSMatthew G. Knepley   /* Build coordinates */
1593cfb853baSMatthew G. Knepley   PetscCall(DMGetCoordinateSection(dm, &coordSection));
1594cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetNumFields(coordSection, 1));
1595cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
1596cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices));
1597cfb853baSMatthew G. Knepley   for (v = numCells; v < numCells + numVertices; ++v) {
1598cfb853baSMatthew G. Knepley     PetscCall(PetscSectionSetDof(coordSection, v, dim));
1599cfb853baSMatthew G. Knepley     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
1600cfb853baSMatthew G. Knepley   }
1601cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetUp(coordSection));
1602cfb853baSMatthew G. Knepley   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
1603cfb853baSMatthew G. Knepley   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
1604cfb853baSMatthew G. Knepley   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
1605cfb853baSMatthew G. Knepley   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
1606cfb853baSMatthew G. Knepley   PetscCall(VecSetBlockSize(coordinates, dim));
1607cfb853baSMatthew G. Knepley   PetscCall(VecSetType(coordinates, VECSTANDARD));
1608cfb853baSMatthew G. Knepley   PetscCall(VecGetArray(coordinates, &coords));
1609cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) vert[d] = 0;
1610cfb853baSMatthew G. Knepley   while (vert[dim - 1] < vertices[dim - 1]) {
1611cfb853baSMatthew G. Knepley     const PetscInt vertex = TupleToIndex_Private(dim, vertices, vert);
1612cfb853baSMatthew G. Knepley 
1613cfb853baSMatthew G. Knepley     for (d = 0; d < dim; ++d) coords[vertex * dim + d] = lower[d] + ((upper[d] - lower[d]) / vertices[d]) * vert[d];
16143ba16761SJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "Vertex %" PetscInt_FMT ":", vertex));
16153ba16761SJacob Faibussowitsch     for (d = 0; d < dim; ++d) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, vert[d]));
16163ba16761SJacob Faibussowitsch     for (d = 0; d < dim; ++d) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %g", (double)PetscRealPart(coords[vertex * dim + d])));
16173ba16761SJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
1618cfb853baSMatthew G. Knepley     PetscCall(DMPlexTensorPointLexicographic_Private(dim, vertices, vert));
1619cfb853baSMatthew G. Knepley   }
1620cfb853baSMatthew G. Knepley   PetscCall(VecRestoreArray(coordinates, &coords));
1621cfb853baSMatthew G. Knepley   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
1622cfb853baSMatthew G. Knepley   PetscCall(VecDestroy(&coordinates));
1623cfb853baSMatthew G. Knepley   PetscCall(PetscFree4(vertices, vert, vtmp, supp));
1624cfb853baSMatthew G. Knepley   //PetscCall(DMSetPeriodicity(dm, NULL, lower, upper));
1625cfb853baSMatthew G. Knepley   // Attach the extent
1626cfb853baSMatthew G. Knepley   {
1627cfb853baSMatthew G. Knepley     PetscContainer c;
1628cfb853baSMatthew G. Knepley     PetscInt      *extent;
1629cfb853baSMatthew G. Knepley 
1630cfb853baSMatthew G. Knepley     PetscCall(PetscMalloc1(dim, &extent));
1631cfb853baSMatthew G. Knepley     for (PetscInt d = 0; d < dim; ++d) extent[d] = edges[d];
1632cfb853baSMatthew G. Knepley     PetscCall(PetscContainerCreate(PETSC_COMM_SELF, &c));
1633cfb853baSMatthew G. Knepley     PetscCall(PetscContainerSetUserDestroy(c, DestroyExtent_Private));
1634cfb853baSMatthew G. Knepley     PetscCall(PetscContainerSetPointer(c, extent));
1635cfb853baSMatthew G. Knepley     PetscCall(PetscObjectCompose((PetscObject)dm, "_extent", (PetscObject)c));
1636cfb853baSMatthew G. Knepley     PetscCall(PetscContainerDestroy(&c));
1637cfb853baSMatthew G. Knepley   }
16383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1639cfb853baSMatthew G. Knepley }
1640cfb853baSMatthew G. Knepley 
1641cfb853baSMatthew G. Knepley /*@C
1642aaa8cc7dSPierre Jolivet   DMPlexCreateHypercubicMesh - Creates a periodic mesh on the tensor product of unit intervals using only vertices and edges.
1643cfb853baSMatthew G. Knepley 
1644cfb853baSMatthew G. Knepley   Collective
1645cfb853baSMatthew G. Knepley 
1646cfb853baSMatthew G. Knepley   Input Parameters:
1647cfb853baSMatthew G. Knepley + comm  - The communicator for the DM object
1648cfb853baSMatthew G. Knepley . dim   - The spatial dimension
164920f4b53cSBarry Smith . edges - Number of edges per dimension, or `NULL` for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
165020f4b53cSBarry Smith . lower - The lower left corner, or `NULL` for (0, 0, 0)
165120f4b53cSBarry Smith - upper - The upper right corner, or `NULL` for (1, 1, 1)
1652cfb853baSMatthew G. Knepley 
1653cfb853baSMatthew G. Knepley   Output Parameter:
1654cfb853baSMatthew G. Knepley . dm - The DM object
1655cfb853baSMatthew G. Knepley 
165620f4b53cSBarry Smith   Level: beginner
165720f4b53cSBarry Smith 
165820f4b53cSBarry Smith   Note:
165920f4b53cSBarry Smith   If you want to customize this mesh using options, you just need to
166020f4b53cSBarry Smith .vb
166120f4b53cSBarry Smith   DMCreate(comm, &dm);
166220f4b53cSBarry Smith   DMSetType(dm, DMPLEX);
166320f4b53cSBarry Smith   DMSetFromOptions(dm);
166420f4b53cSBarry Smith .ve
166520f4b53cSBarry Smith   and use the options on the `DMSetFromOptions()` page.
1666cfb853baSMatthew G. Knepley 
1667cfb853baSMatthew G. Knepley   The vertices are numbered is lexicographic order, and the dim edges exiting a vertex in the positive orthant are number consecutively,
166820f4b53cSBarry Smith .vb
166920f4b53cSBarry Smith  18--0-19--2-20--4-18
167020f4b53cSBarry Smith   |     |     |     |
167120f4b53cSBarry Smith  13    15    17    13
167220f4b53cSBarry Smith   |     |     |     |
167320f4b53cSBarry Smith  24-12-25-14-26-16-24
167420f4b53cSBarry Smith   |     |     |     |
167520f4b53cSBarry Smith   7     9    11     7
167620f4b53cSBarry Smith   |     |     |     |
167720f4b53cSBarry Smith  21--6-22--8-23-10-21
167820f4b53cSBarry Smith   |     |     |     |
167920f4b53cSBarry Smith   1     3     5     1
168020f4b53cSBarry Smith   |     |     |     |
168120f4b53cSBarry Smith  18--0-19--2-20--4-18
168220f4b53cSBarry Smith .ve
1683cfb853baSMatthew G. Knepley 
168476fbde31SPierre Jolivet .seealso: `DMSetFromOptions()`, `DMPlexCreateFromFile()`, `DMPlexCreateHexCylinderMesh()`, `DMSetType()`, `DMCreate()`
1685cfb853baSMatthew G. Knepley @*/
1686cfb853baSMatthew G. Knepley PetscErrorCode DMPlexCreateHypercubicMesh(MPI_Comm comm, PetscInt dim, const PetscInt edges[], const PetscReal lower[], const PetscReal upper[], DM *dm)
1687cfb853baSMatthew G. Knepley {
1688cfb853baSMatthew G. Knepley   PetscInt       *edg;
1689cfb853baSMatthew G. Knepley   PetscReal      *low, *upp;
1690cfb853baSMatthew G. Knepley   DMBoundaryType *bdt;
1691cfb853baSMatthew G. Knepley   PetscInt        d;
1692cfb853baSMatthew G. Knepley 
1693cfb853baSMatthew G. Knepley   PetscFunctionBegin;
1694cfb853baSMatthew G. Knepley   PetscCall(DMCreate(comm, dm));
1695cfb853baSMatthew G. Knepley   PetscCall(DMSetType(*dm, DMPLEX));
1696cfb853baSMatthew G. Knepley   PetscCall(PetscMalloc4(dim, &edg, dim, &low, dim, &upp, dim, &bdt));
1697cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) {
1698cfb853baSMatthew G. Knepley     edg[d] = edges ? edges[d] : 1;
1699cfb853baSMatthew G. Knepley     low[d] = lower ? lower[d] : 0.;
1700cfb853baSMatthew G. Knepley     upp[d] = upper ? upper[d] : 1.;
1701cfb853baSMatthew G. Knepley     bdt[d] = DM_BOUNDARY_PERIODIC;
1702cfb853baSMatthew G. Knepley   }
1703cfb853baSMatthew G. Knepley   PetscCall(DMPlexCreateHypercubicMesh_Internal(*dm, dim, low, upp, edg, bdt));
1704cfb853baSMatthew G. Knepley   PetscCall(PetscFree4(edg, low, upp, bdt));
17053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1706cfb853baSMatthew G. Knepley }
1707cfb853baSMatthew G. Knepley 
1708a9074c1eSMatthew G. Knepley /*@C
1709a1cb98faSBarry Smith   DMPlexSetOptionsPrefix - Sets the prefix used for searching for all `DM` options in the database.
1710a9074c1eSMatthew G. Knepley 
171120f4b53cSBarry Smith   Logically Collective
1712a9074c1eSMatthew G. Knepley 
1713a9074c1eSMatthew G. Knepley   Input Parameters:
171420f4b53cSBarry Smith + dm     - the `DM` context
1715a9074c1eSMatthew G. Knepley - prefix - the prefix to prepend to all option names
1716a9074c1eSMatthew G. Knepley 
1717a1cb98faSBarry Smith   Level: advanced
1718a1cb98faSBarry Smith 
1719a1cb98faSBarry Smith   Note:
1720a9074c1eSMatthew G. Knepley   A hyphen (-) must NOT be given at the beginning of the prefix name.
1721a9074c1eSMatthew G. Knepley   The first character of all runtime options is AUTOMATICALLY the hyphen.
1722a9074c1eSMatthew G. Knepley 
17231cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `SNESSetFromOptions()`
1724a9074c1eSMatthew G. Knepley @*/
1725d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetOptionsPrefix(DM dm, const char prefix[])
1726d71ae5a4SJacob Faibussowitsch {
1727a9074c1eSMatthew G. Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
1728a9074c1eSMatthew G. Knepley 
1729a9074c1eSMatthew G. Knepley   PetscFunctionBegin;
1730a9074c1eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17319566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, prefix));
17329566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)mesh->partitioner, prefix));
17333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1734a9074c1eSMatthew G. Knepley }
1735a9074c1eSMatthew G. Knepley 
17369318fe57SMatthew G. Knepley /* Remap geometry to cylinder
173761a622f3SMatthew G. Knepley    TODO: This only works for a single refinement, then it is broken
173861a622f3SMatthew G. Knepley 
17399318fe57SMatthew G. Knepley      Interior square: Linear interpolation is correct
17409318fe57SMatthew G. Knepley      The other cells all have vertices on rays from the origin. We want to uniformly expand the spacing
17419318fe57SMatthew G. Knepley      such that the last vertex is on the unit circle. So the closest and farthest vertices are at distance
17420510c589SMatthew G. Knepley 
17439318fe57SMatthew G. Knepley        phi     = arctan(y/x)
17449318fe57SMatthew G. Knepley        d_close = sqrt(1/8 + 1/4 sin^2(phi))
17459318fe57SMatthew G. Knepley        d_far   = sqrt(1/2 + sin^2(phi))
17460510c589SMatthew G. Knepley 
17479318fe57SMatthew G. Knepley      so we remap them using
17480510c589SMatthew G. Knepley 
17499318fe57SMatthew G. Knepley        x_new = x_close + (x - x_close) (1 - d_close) / (d_far - d_close)
17509318fe57SMatthew G. Knepley        y_new = y_close + (y - y_close) (1 - d_close) / (d_far - d_close)
17510510c589SMatthew G. Knepley 
17529318fe57SMatthew G. Knepley      If pi/4 < phi < 3pi/4 or -3pi/4 < phi < -pi/4, then we switch x and y.
17539318fe57SMatthew G. Knepley */
1754d71ae5a4SJacob Faibussowitsch static void snapToCylinder(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
1755d71ae5a4SJacob Faibussowitsch {
17569318fe57SMatthew G. Knepley   const PetscReal dis = 1.0 / PetscSqrtReal(2.0);
17579318fe57SMatthew G. Knepley   const PetscReal ds2 = 0.5 * dis;
175822cc497dSMatthew G. Knepley 
17599318fe57SMatthew G. Knepley   if ((PetscAbsScalar(u[0]) <= ds2) && (PetscAbsScalar(u[1]) <= ds2)) {
17609318fe57SMatthew G. Knepley     f0[0] = u[0];
17619318fe57SMatthew G. Knepley     f0[1] = u[1];
17629318fe57SMatthew G. Knepley   } else {
17639318fe57SMatthew G. Knepley     PetscReal phi, sinp, cosp, dc, df, x, y, xc, yc;
17640510c589SMatthew G. Knepley 
17659318fe57SMatthew G. Knepley     x    = PetscRealPart(u[0]);
17669318fe57SMatthew G. Knepley     y    = PetscRealPart(u[1]);
17679318fe57SMatthew G. Knepley     phi  = PetscAtan2Real(y, x);
17689318fe57SMatthew G. Knepley     sinp = PetscSinReal(phi);
17699318fe57SMatthew G. Knepley     cosp = PetscCosReal(phi);
17709318fe57SMatthew G. Knepley     if ((PetscAbsReal(phi) > PETSC_PI / 4.0) && (PetscAbsReal(phi) < 3.0 * PETSC_PI / 4.0)) {
17719318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2 / sinp);
17729318fe57SMatthew G. Knepley       df = PetscAbsReal(dis / sinp);
17739318fe57SMatthew G. Knepley       xc = ds2 * x / PetscAbsReal(y);
17749318fe57SMatthew G. Knepley       yc = ds2 * PetscSignReal(y);
17759318fe57SMatthew G. Knepley     } else {
17769318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2 / cosp);
17779318fe57SMatthew G. Knepley       df = PetscAbsReal(dis / cosp);
17789318fe57SMatthew G. Knepley       xc = ds2 * PetscSignReal(x);
17799318fe57SMatthew G. Knepley       yc = ds2 * y / PetscAbsReal(x);
17809318fe57SMatthew G. Knepley     }
17819318fe57SMatthew G. Knepley     f0[0] = xc + (u[0] - xc) * (1.0 - dc) / (df - dc);
17829318fe57SMatthew G. Knepley     f0[1] = yc + (u[1] - yc) * (1.0 - dc) / (df - dc);
17839318fe57SMatthew G. Knepley   }
17849318fe57SMatthew G. Knepley   f0[2] = u[2];
17859318fe57SMatthew G. Knepley }
17860510c589SMatthew G. Knepley 
1787d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateHexCylinderMesh_Internal(DM dm, DMBoundaryType periodicZ)
1788d71ae5a4SJacob Faibussowitsch {
17890510c589SMatthew G. Knepley   const PetscInt dim = 3;
17909318fe57SMatthew G. Knepley   PetscInt       numCells, numVertices;
1791d8c47e87SMatthew G. Knepley   PetscMPIInt    rank;
17920510c589SMatthew G. Knepley 
17930510c589SMatthew G. Knepley   PetscFunctionBegin;
179446139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
17959566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
17969566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
17970510c589SMatthew G. Knepley   /* Create topology */
17980510c589SMatthew G. Knepley   {
17990510c589SMatthew G. Knepley     PetscInt cone[8], c;
18000510c589SMatthew G. Knepley 
1801dd400576SPatrick Sanan     numCells    = rank == 0 ? 5 : 0;
1802dd400576SPatrick Sanan     numVertices = rank == 0 ? 16 : 0;
1803006a8963SMatthew G. Knepley     if (periodicZ == DM_BOUNDARY_PERIODIC) {
1804ae8bcbbbSMatthew G. Knepley       numCells *= 3;
1805dd400576SPatrick Sanan       numVertices = rank == 0 ? 24 : 0;
1806006a8963SMatthew G. Knepley     }
18079566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
18089566063dSJacob Faibussowitsch     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 8));
18099566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm));
1810dd400576SPatrick Sanan     if (rank == 0) {
1811006a8963SMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
18129371c9d4SSatish Balay         cone[0] = 15;
18139371c9d4SSatish Balay         cone[1] = 18;
18149371c9d4SSatish Balay         cone[2] = 17;
18159371c9d4SSatish Balay         cone[3] = 16;
18169371c9d4SSatish Balay         cone[4] = 31;
18179371c9d4SSatish Balay         cone[5] = 32;
18189371c9d4SSatish Balay         cone[6] = 33;
18199371c9d4SSatish Balay         cone[7] = 34;
18209566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
18219371c9d4SSatish Balay         cone[0] = 16;
18229371c9d4SSatish Balay         cone[1] = 17;
18239371c9d4SSatish Balay         cone[2] = 24;
18249371c9d4SSatish Balay         cone[3] = 23;
18259371c9d4SSatish Balay         cone[4] = 32;
18269371c9d4SSatish Balay         cone[5] = 36;
18279371c9d4SSatish Balay         cone[6] = 37;
18289371c9d4SSatish Balay         cone[7] = 33; /* 22 25 26 21 */
18299566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
18309371c9d4SSatish Balay         cone[0] = 18;
18319371c9d4SSatish Balay         cone[1] = 27;
18329371c9d4SSatish Balay         cone[2] = 24;
18339371c9d4SSatish Balay         cone[3] = 17;
18349371c9d4SSatish Balay         cone[4] = 34;
18359371c9d4SSatish Balay         cone[5] = 33;
18369371c9d4SSatish Balay         cone[6] = 37;
18379371c9d4SSatish Balay         cone[7] = 38;
18389566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
18399371c9d4SSatish Balay         cone[0] = 29;
18409371c9d4SSatish Balay         cone[1] = 27;
18419371c9d4SSatish Balay         cone[2] = 18;
18429371c9d4SSatish Balay         cone[3] = 15;
18439371c9d4SSatish Balay         cone[4] = 35;
18449371c9d4SSatish Balay         cone[5] = 31;
18459371c9d4SSatish Balay         cone[6] = 34;
18469371c9d4SSatish Balay         cone[7] = 38;
18479566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
18489371c9d4SSatish Balay         cone[0] = 29;
18499371c9d4SSatish Balay         cone[1] = 15;
18509371c9d4SSatish Balay         cone[2] = 16;
18519371c9d4SSatish Balay         cone[3] = 23;
18529371c9d4SSatish Balay         cone[4] = 35;
18539371c9d4SSatish Balay         cone[5] = 36;
18549371c9d4SSatish Balay         cone[6] = 32;
18559371c9d4SSatish Balay         cone[7] = 31;
18569566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
1857006a8963SMatthew G. Knepley 
18589371c9d4SSatish Balay         cone[0] = 31;
18599371c9d4SSatish Balay         cone[1] = 34;
18609371c9d4SSatish Balay         cone[2] = 33;
18619371c9d4SSatish Balay         cone[3] = 32;
18629371c9d4SSatish Balay         cone[4] = 19;
18639371c9d4SSatish Balay         cone[5] = 22;
18649371c9d4SSatish Balay         cone[6] = 21;
18659371c9d4SSatish Balay         cone[7] = 20;
18669566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 5, cone));
18679371c9d4SSatish Balay         cone[0] = 32;
18689371c9d4SSatish Balay         cone[1] = 33;
18699371c9d4SSatish Balay         cone[2] = 37;
18709371c9d4SSatish Balay         cone[3] = 36;
18719371c9d4SSatish Balay         cone[4] = 22;
18729371c9d4SSatish Balay         cone[5] = 25;
18739371c9d4SSatish Balay         cone[6] = 26;
18749371c9d4SSatish Balay         cone[7] = 21;
18759566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 6, cone));
18769371c9d4SSatish Balay         cone[0] = 34;
18779371c9d4SSatish Balay         cone[1] = 38;
18789371c9d4SSatish Balay         cone[2] = 37;
18799371c9d4SSatish Balay         cone[3] = 33;
18809371c9d4SSatish Balay         cone[4] = 20;
18819371c9d4SSatish Balay         cone[5] = 21;
18829371c9d4SSatish Balay         cone[6] = 26;
18839371c9d4SSatish Balay         cone[7] = 28;
18849566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 7, cone));
18859371c9d4SSatish Balay         cone[0] = 35;
18869371c9d4SSatish Balay         cone[1] = 38;
18879371c9d4SSatish Balay         cone[2] = 34;
18889371c9d4SSatish Balay         cone[3] = 31;
18899371c9d4SSatish Balay         cone[4] = 30;
18909371c9d4SSatish Balay         cone[5] = 19;
18919371c9d4SSatish Balay         cone[6] = 20;
18929371c9d4SSatish Balay         cone[7] = 28;
18939566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 8, cone));
18949371c9d4SSatish Balay         cone[0] = 35;
18959371c9d4SSatish Balay         cone[1] = 31;
18969371c9d4SSatish Balay         cone[2] = 32;
18979371c9d4SSatish Balay         cone[3] = 36;
18989371c9d4SSatish Balay         cone[4] = 30;
18999371c9d4SSatish Balay         cone[5] = 25;
19009371c9d4SSatish Balay         cone[6] = 22;
19019371c9d4SSatish Balay         cone[7] = 19;
19029566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 9, cone));
1903ae8bcbbbSMatthew G. Knepley 
19049371c9d4SSatish Balay         cone[0] = 19;
19059371c9d4SSatish Balay         cone[1] = 20;
19069371c9d4SSatish Balay         cone[2] = 21;
19079371c9d4SSatish Balay         cone[3] = 22;
19089371c9d4SSatish Balay         cone[4] = 15;
19099371c9d4SSatish Balay         cone[5] = 16;
19109371c9d4SSatish Balay         cone[6] = 17;
19119371c9d4SSatish Balay         cone[7] = 18;
19129566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 10, cone));
19139371c9d4SSatish Balay         cone[0] = 22;
19149371c9d4SSatish Balay         cone[1] = 21;
19159371c9d4SSatish Balay         cone[2] = 26;
19169371c9d4SSatish Balay         cone[3] = 25;
19179371c9d4SSatish Balay         cone[4] = 16;
19189371c9d4SSatish Balay         cone[5] = 23;
19199371c9d4SSatish Balay         cone[6] = 24;
19209371c9d4SSatish Balay         cone[7] = 17;
19219566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 11, cone));
19229371c9d4SSatish Balay         cone[0] = 20;
19239371c9d4SSatish Balay         cone[1] = 28;
19249371c9d4SSatish Balay         cone[2] = 26;
19259371c9d4SSatish Balay         cone[3] = 21;
19269371c9d4SSatish Balay         cone[4] = 18;
19279371c9d4SSatish Balay         cone[5] = 17;
19289371c9d4SSatish Balay         cone[6] = 24;
19299371c9d4SSatish Balay         cone[7] = 27;
19309566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 12, cone));
19319371c9d4SSatish Balay         cone[0] = 30;
19329371c9d4SSatish Balay         cone[1] = 28;
19339371c9d4SSatish Balay         cone[2] = 20;
19349371c9d4SSatish Balay         cone[3] = 19;
19359371c9d4SSatish Balay         cone[4] = 29;
19369371c9d4SSatish Balay         cone[5] = 15;
19379371c9d4SSatish Balay         cone[6] = 18;
19389371c9d4SSatish Balay         cone[7] = 27;
19399566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 13, cone));
19409371c9d4SSatish Balay         cone[0] = 30;
19419371c9d4SSatish Balay         cone[1] = 19;
19429371c9d4SSatish Balay         cone[2] = 22;
19439371c9d4SSatish Balay         cone[3] = 25;
19449371c9d4SSatish Balay         cone[4] = 29;
19459371c9d4SSatish Balay         cone[5] = 23;
19469371c9d4SSatish Balay         cone[6] = 16;
19479371c9d4SSatish Balay         cone[7] = 15;
19489566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 14, cone));
1949006a8963SMatthew G. Knepley       } else {
19509371c9d4SSatish Balay         cone[0] = 5;
19519371c9d4SSatish Balay         cone[1] = 8;
19529371c9d4SSatish Balay         cone[2] = 7;
19539371c9d4SSatish Balay         cone[3] = 6;
19549371c9d4SSatish Balay         cone[4] = 9;
19559371c9d4SSatish Balay         cone[5] = 12;
19569371c9d4SSatish Balay         cone[6] = 11;
19579371c9d4SSatish Balay         cone[7] = 10;
19589566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
19599371c9d4SSatish Balay         cone[0] = 6;
19609371c9d4SSatish Balay         cone[1] = 7;
19619371c9d4SSatish Balay         cone[2] = 14;
19629371c9d4SSatish Balay         cone[3] = 13;
19639371c9d4SSatish Balay         cone[4] = 12;
19649371c9d4SSatish Balay         cone[5] = 15;
19659371c9d4SSatish Balay         cone[6] = 16;
19669371c9d4SSatish Balay         cone[7] = 11;
19679566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
19689371c9d4SSatish Balay         cone[0] = 8;
19699371c9d4SSatish Balay         cone[1] = 17;
19709371c9d4SSatish Balay         cone[2] = 14;
19719371c9d4SSatish Balay         cone[3] = 7;
19729371c9d4SSatish Balay         cone[4] = 10;
19739371c9d4SSatish Balay         cone[5] = 11;
19749371c9d4SSatish Balay         cone[6] = 16;
19759371c9d4SSatish Balay         cone[7] = 18;
19769566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
19779371c9d4SSatish Balay         cone[0] = 19;
19789371c9d4SSatish Balay         cone[1] = 17;
19799371c9d4SSatish Balay         cone[2] = 8;
19809371c9d4SSatish Balay         cone[3] = 5;
19819371c9d4SSatish Balay         cone[4] = 20;
19829371c9d4SSatish Balay         cone[5] = 9;
19839371c9d4SSatish Balay         cone[6] = 10;
19849371c9d4SSatish Balay         cone[7] = 18;
19859566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
19869371c9d4SSatish Balay         cone[0] = 19;
19879371c9d4SSatish Balay         cone[1] = 5;
19889371c9d4SSatish Balay         cone[2] = 6;
19899371c9d4SSatish Balay         cone[3] = 13;
19909371c9d4SSatish Balay         cone[4] = 20;
19919371c9d4SSatish Balay         cone[5] = 15;
19929371c9d4SSatish Balay         cone[6] = 12;
19939371c9d4SSatish Balay         cone[7] = 9;
19949566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
1995006a8963SMatthew G. Knepley       }
1996d8c47e87SMatthew G. Knepley     }
19979566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
19989566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
19990510c589SMatthew G. Knepley   }
2000dbc1dc17SMatthew G. Knepley   /* Create cube geometry */
20010510c589SMatthew G. Knepley   {
20020510c589SMatthew G. Knepley     Vec             coordinates;
20030510c589SMatthew G. Knepley     PetscSection    coordSection;
20040510c589SMatthew G. Knepley     PetscScalar    *coords;
20050510c589SMatthew G. Knepley     PetscInt        coordSize, v;
20060510c589SMatthew G. Knepley     const PetscReal dis = 1.0 / PetscSqrtReal(2.0);
20070510c589SMatthew G. Knepley     const PetscReal ds2 = dis / 2.0;
20080510c589SMatthew G. Knepley 
20090510c589SMatthew G. Knepley     /* Build coordinates */
20109566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
20119566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
20129566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
20139566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices));
20140510c589SMatthew G. Knepley     for (v = numCells; v < numCells + numVertices; ++v) {
20159566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
20169566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
20170510c589SMatthew G. Knepley     }
20189566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
20199566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
20209566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
20219566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
20229566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
20239566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
20249566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates, VECSTANDARD));
20259566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
2026dd400576SPatrick Sanan     if (rank == 0) {
20279371c9d4SSatish Balay       coords[0 * dim + 0]  = -ds2;
20289371c9d4SSatish Balay       coords[0 * dim + 1]  = -ds2;
20299371c9d4SSatish Balay       coords[0 * dim + 2]  = 0.0;
20309371c9d4SSatish Balay       coords[1 * dim + 0]  = ds2;
20319371c9d4SSatish Balay       coords[1 * dim + 1]  = -ds2;
20329371c9d4SSatish Balay       coords[1 * dim + 2]  = 0.0;
20339371c9d4SSatish Balay       coords[2 * dim + 0]  = ds2;
20349371c9d4SSatish Balay       coords[2 * dim + 1]  = ds2;
20359371c9d4SSatish Balay       coords[2 * dim + 2]  = 0.0;
20369371c9d4SSatish Balay       coords[3 * dim + 0]  = -ds2;
20379371c9d4SSatish Balay       coords[3 * dim + 1]  = ds2;
20389371c9d4SSatish Balay       coords[3 * dim + 2]  = 0.0;
20399371c9d4SSatish Balay       coords[4 * dim + 0]  = -ds2;
20409371c9d4SSatish Balay       coords[4 * dim + 1]  = -ds2;
20419371c9d4SSatish Balay       coords[4 * dim + 2]  = 1.0;
20429371c9d4SSatish Balay       coords[5 * dim + 0]  = -ds2;
20439371c9d4SSatish Balay       coords[5 * dim + 1]  = ds2;
20449371c9d4SSatish Balay       coords[5 * dim + 2]  = 1.0;
20459371c9d4SSatish Balay       coords[6 * dim + 0]  = ds2;
20469371c9d4SSatish Balay       coords[6 * dim + 1]  = ds2;
20479371c9d4SSatish Balay       coords[6 * dim + 2]  = 1.0;
20489371c9d4SSatish Balay       coords[7 * dim + 0]  = ds2;
20499371c9d4SSatish Balay       coords[7 * dim + 1]  = -ds2;
20509371c9d4SSatish Balay       coords[7 * dim + 2]  = 1.0;
20519371c9d4SSatish Balay       coords[8 * dim + 0]  = dis;
20529371c9d4SSatish Balay       coords[8 * dim + 1]  = -dis;
20539371c9d4SSatish Balay       coords[8 * dim + 2]  = 0.0;
20549371c9d4SSatish Balay       coords[9 * dim + 0]  = dis;
20559371c9d4SSatish Balay       coords[9 * dim + 1]  = dis;
20569371c9d4SSatish Balay       coords[9 * dim + 2]  = 0.0;
20579371c9d4SSatish Balay       coords[10 * dim + 0] = dis;
20589371c9d4SSatish Balay       coords[10 * dim + 1] = -dis;
20599371c9d4SSatish Balay       coords[10 * dim + 2] = 1.0;
20609371c9d4SSatish Balay       coords[11 * dim + 0] = dis;
20619371c9d4SSatish Balay       coords[11 * dim + 1] = dis;
20629371c9d4SSatish Balay       coords[11 * dim + 2] = 1.0;
20639371c9d4SSatish Balay       coords[12 * dim + 0] = -dis;
20649371c9d4SSatish Balay       coords[12 * dim + 1] = dis;
20659371c9d4SSatish Balay       coords[12 * dim + 2] = 0.0;
20669371c9d4SSatish Balay       coords[13 * dim + 0] = -dis;
20679371c9d4SSatish Balay       coords[13 * dim + 1] = dis;
20689371c9d4SSatish Balay       coords[13 * dim + 2] = 1.0;
20699371c9d4SSatish Balay       coords[14 * dim + 0] = -dis;
20709371c9d4SSatish Balay       coords[14 * dim + 1] = -dis;
20719371c9d4SSatish Balay       coords[14 * dim + 2] = 0.0;
20729371c9d4SSatish Balay       coords[15 * dim + 0] = -dis;
20739371c9d4SSatish Balay       coords[15 * dim + 1] = -dis;
20749371c9d4SSatish Balay       coords[15 * dim + 2] = 1.0;
2075ae8bcbbbSMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
20769371c9d4SSatish Balay         /* 15 31 19 */ coords[16 * dim + 0] = -ds2;
20779371c9d4SSatish Balay         coords[16 * dim + 1]                = -ds2;
20789371c9d4SSatish Balay         coords[16 * dim + 2]                = 0.5;
20799371c9d4SSatish Balay         /* 16 32 22 */ coords[17 * dim + 0] = ds2;
20809371c9d4SSatish Balay         coords[17 * dim + 1]                = -ds2;
20819371c9d4SSatish Balay         coords[17 * dim + 2]                = 0.5;
20829371c9d4SSatish Balay         /* 17 33 21 */ coords[18 * dim + 0] = ds2;
20839371c9d4SSatish Balay         coords[18 * dim + 1]                = ds2;
20849371c9d4SSatish Balay         coords[18 * dim + 2]                = 0.5;
20859371c9d4SSatish Balay         /* 18 34 20 */ coords[19 * dim + 0] = -ds2;
20869371c9d4SSatish Balay         coords[19 * dim + 1]                = ds2;
20879371c9d4SSatish Balay         coords[19 * dim + 2]                = 0.5;
20889371c9d4SSatish Balay         /* 29 35 30 */ coords[20 * dim + 0] = -dis;
20899371c9d4SSatish Balay         coords[20 * dim + 1]                = -dis;
20909371c9d4SSatish Balay         coords[20 * dim + 2]                = 0.5;
20919371c9d4SSatish Balay         /* 23 36 25 */ coords[21 * dim + 0] = dis;
20929371c9d4SSatish Balay         coords[21 * dim + 1]                = -dis;
20939371c9d4SSatish Balay         coords[21 * dim + 2]                = 0.5;
20949371c9d4SSatish Balay         /* 24 37 26 */ coords[22 * dim + 0] = dis;
20959371c9d4SSatish Balay         coords[22 * dim + 1]                = dis;
20969371c9d4SSatish Balay         coords[22 * dim + 2]                = 0.5;
20979371c9d4SSatish Balay         /* 27 38 28 */ coords[23 * dim + 0] = -dis;
20989371c9d4SSatish Balay         coords[23 * dim + 1]                = dis;
20999371c9d4SSatish Balay         coords[23 * dim + 2]                = 0.5;
2100ae8bcbbbSMatthew G. Knepley       }
2101d8c47e87SMatthew G. Knepley     }
21029566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
21039566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
21049566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
21050510c589SMatthew G. Knepley   }
2106006a8963SMatthew G. Knepley   /* Create periodicity */
2107006a8963SMatthew G. Knepley   if (periodicZ == DM_BOUNDARY_PERIODIC || periodicZ == DM_BOUNDARY_TWIST) {
21086858538eSMatthew G. Knepley     PetscReal L[3]       = {-1., -1., 0.};
21096858538eSMatthew G. Knepley     PetscReal maxCell[3] = {-1., -1., 0.};
2110006a8963SMatthew G. Knepley     PetscReal lower[3]   = {0.0, 0.0, 0.0};
2111ae8bcbbbSMatthew G. Knepley     PetscReal upper[3]   = {1.0, 1.0, 1.5};
21126858538eSMatthew G. Knepley     PetscInt  numZCells  = 3;
2113006a8963SMatthew G. Knepley 
21146858538eSMatthew G. Knepley     L[2]       = upper[2] - lower[2];
21156858538eSMatthew G. Knepley     maxCell[2] = 1.1 * (L[2] / numZCells);
21164fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, maxCell, lower, L));
2117006a8963SMatthew G. Knepley   }
2118dbc1dc17SMatthew G. Knepley   {
21199318fe57SMatthew G. Knepley     DM          cdm;
21209318fe57SMatthew G. Knepley     PetscDS     cds;
21219318fe57SMatthew G. Knepley     PetscScalar c[2] = {1.0, 1.0};
2122dbc1dc17SMatthew G. Knepley 
21239566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCoordinateSpace(dm, 1, snapToCylinder));
21249566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
21259566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
21269566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, 2, c));
2127dbc1dc17SMatthew G. Knepley   }
212846139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
212946139095SJed Brown 
21309318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
21319566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolateInPlace_Internal(dm));
21323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21330510c589SMatthew G. Knepley }
21340510c589SMatthew G. Knepley 
213524119c2aSMatthew G. Knepley /*@
21369318fe57SMatthew G. Knepley   DMPlexCreateHexCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using hexahedra.
213724119c2aSMatthew G. Knepley 
2138d083f849SBarry Smith   Collective
213924119c2aSMatthew G. Knepley 
214024119c2aSMatthew G. Knepley   Input Parameters:
2141a1cb98faSBarry Smith + comm      - The communicator for the `DM` object
21429318fe57SMatthew G. Knepley - periodicZ - The boundary type for the Z direction
214324119c2aSMatthew G. Knepley 
214424119c2aSMatthew G. Knepley   Output Parameter:
214520f4b53cSBarry Smith . dm - The `DM` object
214624119c2aSMatthew G. Knepley 
214724119c2aSMatthew G. Knepley   Level: beginner
214824119c2aSMatthew G. Knepley 
2149a1cb98faSBarry Smith   Note:
2150a4e35b19SJacob Faibussowitsch   Here is the output numbering looking from the bottom of the cylinder\:
2151a1cb98faSBarry Smith .vb
2152a1cb98faSBarry Smith        17-----14
2153a1cb98faSBarry Smith         |     |
2154a1cb98faSBarry Smith         |  2  |
2155a1cb98faSBarry Smith         |     |
2156a1cb98faSBarry Smith  17-----8-----7-----14
2157a1cb98faSBarry Smith   |     |     |     |
2158a1cb98faSBarry Smith   |  3  |  0  |  1  |
2159a1cb98faSBarry Smith   |     |     |     |
2160a1cb98faSBarry Smith  19-----5-----6-----13
2161a1cb98faSBarry Smith         |     |
2162a1cb98faSBarry Smith         |  4  |
2163a1cb98faSBarry Smith         |     |
2164a1cb98faSBarry Smith        19-----13
2165a1cb98faSBarry Smith 
2166a1cb98faSBarry Smith  and up through the top
2167a1cb98faSBarry Smith 
2168a1cb98faSBarry Smith        18-----16
2169a1cb98faSBarry Smith         |     |
2170a1cb98faSBarry Smith         |  2  |
2171a1cb98faSBarry Smith         |     |
2172a1cb98faSBarry Smith  18----10----11-----16
2173a1cb98faSBarry Smith   |     |     |     |
2174a1cb98faSBarry Smith   |  3  |  0  |  1  |
2175a1cb98faSBarry Smith   |     |     |     |
2176a1cb98faSBarry Smith  20-----9----12-----15
2177a1cb98faSBarry Smith         |     |
2178a1cb98faSBarry Smith         |  4  |
2179a1cb98faSBarry Smith         |     |
2180a1cb98faSBarry Smith        20-----15
2181a1cb98faSBarry Smith .ve
2182a1cb98faSBarry Smith 
21831cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
218424119c2aSMatthew G. Knepley @*/
2185d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateHexCylinderMesh(MPI_Comm comm, DMBoundaryType periodicZ, DM *dm)
2186d71ae5a4SJacob Faibussowitsch {
21879318fe57SMatthew G. Knepley   PetscFunctionBegin;
21884f572ea9SToby Isaac   PetscAssertPointer(dm, 3);
21899566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
21909566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
21919566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateHexCylinderMesh_Internal(*dm, periodicZ));
21923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21939318fe57SMatthew G. Knepley }
21949318fe57SMatthew G. Knepley 
2195d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateWedgeCylinderMesh_Internal(DM dm, PetscInt n, PetscBool interpolate)
2196d71ae5a4SJacob Faibussowitsch {
219724119c2aSMatthew G. Knepley   const PetscInt dim = 3;
2198412e9a14SMatthew G. Knepley   PetscInt       numCells, numVertices, v;
21999fe9f049SMatthew G. Knepley   PetscMPIInt    rank;
220024119c2aSMatthew G. Knepley 
220124119c2aSMatthew G. Knepley   PetscFunctionBegin;
220263a3b9bcSJacob Faibussowitsch   PetscCheck(n >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of wedges %" PetscInt_FMT " cannot be negative", n);
220346139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
22049566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
22059566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
2206412e9a14SMatthew G. Knepley   /* Must create the celltype label here so that we do not automatically try to compute the types */
22079566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "celltype"));
220824119c2aSMatthew G. Knepley   /* Create topology */
220924119c2aSMatthew G. Knepley   {
221024119c2aSMatthew G. Knepley     PetscInt cone[6], c;
221124119c2aSMatthew G. Knepley 
2212dd400576SPatrick Sanan     numCells    = rank == 0 ? n : 0;
2213dd400576SPatrick Sanan     numVertices = rank == 0 ? 2 * (n + 1) : 0;
22149566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
22159566063dSJacob Faibussowitsch     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 6));
22169566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm));
221724119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
22189371c9d4SSatish Balay       cone[0] = c + n * 1;
22199371c9d4SSatish Balay       cone[1] = (c + 1) % n + n * 1;
22209371c9d4SSatish Balay       cone[2] = 0 + 3 * n;
22219371c9d4SSatish Balay       cone[3] = c + n * 2;
22229371c9d4SSatish Balay       cone[4] = (c + 1) % n + n * 2;
22239371c9d4SSatish Balay       cone[5] = 1 + 3 * n;
22249566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(dm, c, cone));
22259566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(dm, c, DM_POLYTOPE_TRI_PRISM_TENSOR));
222624119c2aSMatthew G. Knepley     }
22279566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
22289566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
222924119c2aSMatthew G. Knepley   }
223048a46eb9SPierre Jolivet   for (v = numCells; v < numCells + numVertices; ++v) PetscCall(DMPlexSetCellType(dm, v, DM_POLYTOPE_POINT));
223124119c2aSMatthew G. Knepley   /* Create cylinder geometry */
223224119c2aSMatthew G. Knepley   {
223324119c2aSMatthew G. Knepley     Vec          coordinates;
223424119c2aSMatthew G. Knepley     PetscSection coordSection;
223524119c2aSMatthew G. Knepley     PetscScalar *coords;
2236412e9a14SMatthew G. Knepley     PetscInt     coordSize, c;
223724119c2aSMatthew G. Knepley 
223824119c2aSMatthew G. Knepley     /* Build coordinates */
22399566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
22409566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
22419566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
22429566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices));
224324119c2aSMatthew G. Knepley     for (v = numCells; v < numCells + numVertices; ++v) {
22449566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
22459566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
224624119c2aSMatthew G. Knepley     }
22479566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
22489566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
22499566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
22509566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
22519566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
22529566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
22539566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates, VECSTANDARD));
22549566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
225524119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
22569371c9d4SSatish Balay       coords[(c + 0 * n) * dim + 0] = PetscCosReal(2.0 * c * PETSC_PI / n);
22579371c9d4SSatish Balay       coords[(c + 0 * n) * dim + 1] = PetscSinReal(2.0 * c * PETSC_PI / n);
22589371c9d4SSatish Balay       coords[(c + 0 * n) * dim + 2] = 1.0;
22599371c9d4SSatish Balay       coords[(c + 1 * n) * dim + 0] = PetscCosReal(2.0 * c * PETSC_PI / n);
22609371c9d4SSatish Balay       coords[(c + 1 * n) * dim + 1] = PetscSinReal(2.0 * c * PETSC_PI / n);
22619371c9d4SSatish Balay       coords[(c + 1 * n) * dim + 2] = 0.0;
226224119c2aSMatthew G. Knepley     }
2263dd400576SPatrick Sanan     if (rank == 0) {
22649371c9d4SSatish Balay       coords[(2 * n + 0) * dim + 0] = 0.0;
22659371c9d4SSatish Balay       coords[(2 * n + 0) * dim + 1] = 0.0;
22669371c9d4SSatish Balay       coords[(2 * n + 0) * dim + 2] = 1.0;
22679371c9d4SSatish Balay       coords[(2 * n + 1) * dim + 0] = 0.0;
22689371c9d4SSatish Balay       coords[(2 * n + 1) * dim + 1] = 0.0;
22699371c9d4SSatish Balay       coords[(2 * n + 1) * dim + 2] = 0.0;
22709fe9f049SMatthew G. Knepley     }
22719566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
22729566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
22739566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
227424119c2aSMatthew G. Knepley   }
227546139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
22769318fe57SMatthew G. Knepley   /* Interpolate */
22779566063dSJacob Faibussowitsch   if (interpolate) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
22783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
22799318fe57SMatthew G. Knepley }
22809318fe57SMatthew G. Knepley 
22819318fe57SMatthew G. Knepley /*@
22829318fe57SMatthew G. Knepley   DMPlexCreateWedgeCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using wedges.
22839318fe57SMatthew G. Knepley 
22849318fe57SMatthew G. Knepley   Collective
22859318fe57SMatthew G. Knepley 
22869318fe57SMatthew G. Knepley   Input Parameters:
2287a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
22889318fe57SMatthew G. Knepley . n           - The number of wedges around the origin
22899318fe57SMatthew G. Knepley - interpolate - Create edges and faces
22909318fe57SMatthew G. Knepley 
22919318fe57SMatthew G. Knepley   Output Parameter:
2292a1cb98faSBarry Smith . dm - The `DM` object
22939318fe57SMatthew G. Knepley 
22949318fe57SMatthew G. Knepley   Level: beginner
22959318fe57SMatthew G. Knepley 
22961cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateHexCylinderMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
22979318fe57SMatthew G. Knepley @*/
2298d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateWedgeCylinderMesh(MPI_Comm comm, PetscInt n, PetscBool interpolate, DM *dm)
2299d71ae5a4SJacob Faibussowitsch {
23009318fe57SMatthew G. Knepley   PetscFunctionBegin;
23014f572ea9SToby Isaac   PetscAssertPointer(dm, 4);
23029566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
23039566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
23049566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateWedgeCylinderMesh_Internal(*dm, n, interpolate));
23053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
230624119c2aSMatthew G. Knepley }
230724119c2aSMatthew G. Knepley 
2308d71ae5a4SJacob Faibussowitsch static inline PetscReal DiffNormReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
2309d71ae5a4SJacob Faibussowitsch {
231065a81367SMatthew G. Knepley   PetscReal prod = 0.0;
231165a81367SMatthew G. Knepley   PetscInt  i;
231265a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += PetscSqr(x[i] - y[i]);
231365a81367SMatthew G. Knepley   return PetscSqrtReal(prod);
231465a81367SMatthew G. Knepley }
2315d71ae5a4SJacob Faibussowitsch static inline PetscReal DotReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
2316d71ae5a4SJacob Faibussowitsch {
231765a81367SMatthew G. Knepley   PetscReal prod = 0.0;
231865a81367SMatthew G. Knepley   PetscInt  i;
231965a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += x[i] * y[i];
232065a81367SMatthew G. Knepley   return prod;
232165a81367SMatthew G. Knepley }
232265a81367SMatthew G. Knepley 
232351a74b61SMatthew G. Knepley /* The first constant is the sphere radius */
2324d71ae5a4SJacob Faibussowitsch static void snapToSphere(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
2325d71ae5a4SJacob Faibussowitsch {
232651a74b61SMatthew G. Knepley   PetscReal r     = PetscRealPart(constants[0]);
232751a74b61SMatthew G. Knepley   PetscReal norm2 = 0.0, fac;
232851a74b61SMatthew G. Knepley   PetscInt  n     = uOff[1] - uOff[0], d;
232951a74b61SMatthew G. Knepley 
233051a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) norm2 += PetscSqr(PetscRealPart(u[d]));
233151a74b61SMatthew G. Knepley   fac = r / PetscSqrtReal(norm2);
233251a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) f0[d] = u[d] * fac;
233351a74b61SMatthew G. Knepley }
233451a74b61SMatthew G. Knepley 
2335d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSphereMesh_Internal(DM dm, PetscInt dim, PetscBool simplex, PetscReal R)
2336d71ae5a4SJacob Faibussowitsch {
233765a81367SMatthew G. Knepley   const PetscInt embedDim = dim + 1;
233865a81367SMatthew G. Knepley   PetscSection   coordSection;
233965a81367SMatthew G. Knepley   Vec            coordinates;
234065a81367SMatthew G. Knepley   PetscScalar   *coords;
234165a81367SMatthew G. Knepley   PetscReal     *coordsIn;
2342064cae4fSPierre Jolivet   PetscInt       numCells, numEdges, numVerts = 0, firstVertex = 0, v, firstEdge, coordSize, d, c, e;
234365a81367SMatthew G. Knepley   PetscMPIInt    rank;
234465a81367SMatthew G. Knepley 
234565a81367SMatthew G. Knepley   PetscFunctionBegin;
23469318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveBool(dm, simplex, 3);
234746139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
23489566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
23499566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, dim + 1));
23509566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
235165a81367SMatthew G. Knepley   switch (dim) {
235265a81367SMatthew G. Knepley   case 2:
235365a81367SMatthew G. Knepley     if (simplex) {
235451a74b61SMatthew G. Knepley       const PetscReal radius    = PetscSqrtReal(1 + PETSC_PHI * PETSC_PHI) / (1.0 + PETSC_PHI);
235551a74b61SMatthew G. Knepley       const PetscReal edgeLen   = 2.0 / (1.0 + PETSC_PHI) * (R / radius);
235665a81367SMatthew G. Knepley       const PetscInt  degree    = 5;
235751a74b61SMatthew G. Knepley       PetscReal       vertex[3] = {0.0, 1.0 / (1.0 + PETSC_PHI), PETSC_PHI / (1.0 + PETSC_PHI)};
235865a81367SMatthew G. Knepley       PetscInt        s[3]      = {1, 1, 1};
235965a81367SMatthew G. Knepley       PetscInt        cone[3];
236065a81367SMatthew G. Knepley       PetscInt       *graph, p, i, j, k;
236165a81367SMatthew G. Knepley 
23629371c9d4SSatish Balay       vertex[0] *= R / radius;
23639371c9d4SSatish Balay       vertex[1] *= R / radius;
23649371c9d4SSatish Balay       vertex[2] *= R / radius;
2365dd400576SPatrick Sanan       numCells    = rank == 0 ? 20 : 0;
2366dd400576SPatrick Sanan       numVerts    = rank == 0 ? 12 : 0;
236765a81367SMatthew G. Knepley       firstVertex = numCells;
236851a74b61SMatthew G. Knepley       /* Use icosahedron, which for a R-sphere has coordinates which are all cyclic permutations of
236965a81367SMatthew G. Knepley 
237065a81367SMatthew G. Knepley            (0, \pm 1/\phi+1, \pm \phi/\phi+1)
237165a81367SMatthew G. Knepley 
237265a81367SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
237351a74b61SMatthew G. Knepley          length is then given by 2/(1+\phi) = 2 * 0.38197 = 0.76393.
237465a81367SMatthew G. Knepley       */
237565a81367SMatthew G. Knepley       /* Construct vertices */
23769566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
2377dd400576SPatrick Sanan       if (rank == 0) {
237865a81367SMatthew G. Knepley         for (p = 0, i = 0; p < embedDim; ++p) {
237965a81367SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
238065a81367SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
238165a81367SMatthew G. Knepley               for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[(d + p) % embedDim] * vertex[(d + p) % embedDim];
238265a81367SMatthew G. Knepley               ++i;
238365a81367SMatthew G. Knepley             }
238465a81367SMatthew G. Knepley           }
238565a81367SMatthew G. Knepley         }
238645da822fSValeria Barra       }
238765a81367SMatthew G. Knepley       /* Construct graph */
23889566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * numVerts, &graph));
238965a81367SMatthew G. Knepley       for (i = 0; i < numVerts; ++i) {
239065a81367SMatthew G. Knepley         for (j = 0, k = 0; j < numVerts; ++j) {
23919371c9d4SSatish Balay           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i * embedDim], &coordsIn[j * embedDim]) - edgeLen) < PETSC_SMALL) {
23929371c9d4SSatish Balay             graph[i * numVerts + j] = 1;
23939371c9d4SSatish Balay             ++k;
23949371c9d4SSatish Balay           }
239565a81367SMatthew G. Knepley         }
239663a3b9bcSJacob Faibussowitsch         PetscCheck(k == degree, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid icosahedron, vertex %" PetscInt_FMT " degree %" PetscInt_FMT " != %" PetscInt_FMT, i, k, degree);
239765a81367SMatthew G. Knepley       }
239865a81367SMatthew G. Knepley       /* Build Topology */
23999566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts));
240048a46eb9SPierre Jolivet       for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim));
24019566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
240265a81367SMatthew G. Knepley       /* Cells */
240365a81367SMatthew G. Knepley       for (i = 0, c = 0; i < numVerts; ++i) {
240465a81367SMatthew G. Knepley         for (j = 0; j < i; ++j) {
240565a81367SMatthew G. Knepley           for (k = 0; k < j; ++k) {
240665a81367SMatthew G. Knepley             if (graph[i * numVerts + j] && graph[j * numVerts + k] && graph[k * numVerts + i]) {
24079371c9d4SSatish Balay               cone[0] = firstVertex + i;
24089371c9d4SSatish Balay               cone[1] = firstVertex + j;
24099371c9d4SSatish Balay               cone[2] = firstVertex + k;
241065a81367SMatthew G. Knepley               /* Check orientation */
241165a81367SMatthew G. Knepley               {
24129371c9d4SSatish Balay                 const PetscInt epsilon[3][3][3] = {
24139371c9d4SSatish Balay                   {{0, 0, 0},  {0, 0, 1},  {0, -1, 0}},
24149371c9d4SSatish Balay                   {{0, 0, -1}, {0, 0, 0},  {1, 0, 0} },
24159371c9d4SSatish Balay                   {{0, 1, 0},  {-1, 0, 0}, {0, 0, 0} }
24169371c9d4SSatish Balay                 };
241765a81367SMatthew G. Knepley                 PetscReal normal[3];
241865a81367SMatthew G. Knepley                 PetscInt  e, f;
241965a81367SMatthew G. Knepley 
242065a81367SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) {
242165a81367SMatthew G. Knepley                   normal[d] = 0.0;
242265a81367SMatthew G. Knepley                   for (e = 0; e < embedDim; ++e) {
2423ad540459SPierre Jolivet                     for (f = 0; f < embedDim; ++f) normal[d] += epsilon[d][e][f] * (coordsIn[j * embedDim + e] - coordsIn[i * embedDim + e]) * (coordsIn[k * embedDim + f] - coordsIn[i * embedDim + f]);
242465a81367SMatthew G. Knepley                   }
242565a81367SMatthew G. Knepley                 }
24269371c9d4SSatish Balay                 if (DotReal(embedDim, normal, &coordsIn[i * embedDim]) < 0) {
24279371c9d4SSatish Balay                   PetscInt tmp = cone[1];
24289371c9d4SSatish Balay                   cone[1]      = cone[2];
24299371c9d4SSatish Balay                   cone[2]      = tmp;
243065a81367SMatthew G. Knepley                 }
243165a81367SMatthew G. Knepley               }
24329566063dSJacob Faibussowitsch               PetscCall(DMPlexSetCone(dm, c++, cone));
243365a81367SMatthew G. Knepley             }
243465a81367SMatthew G. Knepley           }
243565a81367SMatthew G. Knepley         }
243665a81367SMatthew G. Knepley       }
24379566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
24389566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
24399566063dSJacob Faibussowitsch       PetscCall(PetscFree(graph));
244065a81367SMatthew G. Knepley     } else {
24412829fed8SMatthew G. Knepley       /*
24422829fed8SMatthew G. Knepley         12-21--13
24432829fed8SMatthew G. Knepley          |     |
24442829fed8SMatthew G. Knepley         25  4  24
24452829fed8SMatthew G. Knepley          |     |
24462829fed8SMatthew G. Knepley   12-25--9-16--8-24--13
24472829fed8SMatthew G. Knepley    |     |     |     |
24482829fed8SMatthew G. Knepley   23  5 17  0 15  3  22
24492829fed8SMatthew G. Knepley    |     |     |     |
24502829fed8SMatthew G. Knepley   10-20--6-14--7-19--11
24512829fed8SMatthew G. Knepley          |     |
24522829fed8SMatthew G. Knepley         20  1  19
24532829fed8SMatthew G. Knepley          |     |
24542829fed8SMatthew G. Knepley         10-18--11
24552829fed8SMatthew G. Knepley          |     |
24562829fed8SMatthew G. Knepley         23  2  22
24572829fed8SMatthew G. Knepley          |     |
24582829fed8SMatthew G. Knepley         12-21--13
24592829fed8SMatthew G. Knepley        */
24602829fed8SMatthew G. Knepley       PetscInt cone[4], ornt[4];
24612829fed8SMatthew G. Knepley 
2462dd400576SPatrick Sanan       numCells    = rank == 0 ? 6 : 0;
2463dd400576SPatrick Sanan       numEdges    = rank == 0 ? 12 : 0;
2464dd400576SPatrick Sanan       numVerts    = rank == 0 ? 8 : 0;
246565a81367SMatthew G. Knepley       firstVertex = numCells;
246665a81367SMatthew G. Knepley       firstEdge   = numCells + numVerts;
24672829fed8SMatthew G. Knepley       /* Build Topology */
24689566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells + numEdges + numVerts));
246948a46eb9SPierre Jolivet       for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 4));
247048a46eb9SPierre Jolivet       for (e = firstEdge; e < firstEdge + numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2));
24719566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
2472dd400576SPatrick Sanan       if (rank == 0) {
24732829fed8SMatthew G. Knepley         /* Cell 0 */
24749371c9d4SSatish Balay         cone[0] = 14;
24759371c9d4SSatish Balay         cone[1] = 15;
24769371c9d4SSatish Balay         cone[2] = 16;
24779371c9d4SSatish Balay         cone[3] = 17;
24789566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
24799371c9d4SSatish Balay         ornt[0] = 0;
24809371c9d4SSatish Balay         ornt[1] = 0;
24819371c9d4SSatish Balay         ornt[2] = 0;
24829371c9d4SSatish Balay         ornt[3] = 0;
24839566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 0, ornt));
24842829fed8SMatthew G. Knepley         /* Cell 1 */
24859371c9d4SSatish Balay         cone[0] = 18;
24869371c9d4SSatish Balay         cone[1] = 19;
24879371c9d4SSatish Balay         cone[2] = 14;
24889371c9d4SSatish Balay         cone[3] = 20;
24899566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
24909371c9d4SSatish Balay         ornt[0] = 0;
24919371c9d4SSatish Balay         ornt[1] = 0;
24929371c9d4SSatish Balay         ornt[2] = -1;
24939371c9d4SSatish Balay         ornt[3] = 0;
24949566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 1, ornt));
24952829fed8SMatthew G. Knepley         /* Cell 2 */
24969371c9d4SSatish Balay         cone[0] = 21;
24979371c9d4SSatish Balay         cone[1] = 22;
24989371c9d4SSatish Balay         cone[2] = 18;
24999371c9d4SSatish Balay         cone[3] = 23;
25009566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
25019371c9d4SSatish Balay         ornt[0] = 0;
25029371c9d4SSatish Balay         ornt[1] = 0;
25039371c9d4SSatish Balay         ornt[2] = -1;
25049371c9d4SSatish Balay         ornt[3] = 0;
25059566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 2, ornt));
25062829fed8SMatthew G. Knepley         /* Cell 3 */
25079371c9d4SSatish Balay         cone[0] = 19;
25089371c9d4SSatish Balay         cone[1] = 22;
25099371c9d4SSatish Balay         cone[2] = 24;
25109371c9d4SSatish Balay         cone[3] = 15;
25119566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
25129371c9d4SSatish Balay         ornt[0] = -1;
25139371c9d4SSatish Balay         ornt[1] = -1;
25149371c9d4SSatish Balay         ornt[2] = 0;
25159371c9d4SSatish Balay         ornt[3] = -1;
25169566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 3, ornt));
25172829fed8SMatthew G. Knepley         /* Cell 4 */
25189371c9d4SSatish Balay         cone[0] = 16;
25199371c9d4SSatish Balay         cone[1] = 24;
25209371c9d4SSatish Balay         cone[2] = 21;
25219371c9d4SSatish Balay         cone[3] = 25;
25229566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
25239371c9d4SSatish Balay         ornt[0] = -1;
25249371c9d4SSatish Balay         ornt[1] = -1;
25259371c9d4SSatish Balay         ornt[2] = -1;
25269371c9d4SSatish Balay         ornt[3] = 0;
25279566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 4, ornt));
25282829fed8SMatthew G. Knepley         /* Cell 5 */
25299371c9d4SSatish Balay         cone[0] = 20;
25309371c9d4SSatish Balay         cone[1] = 17;
25319371c9d4SSatish Balay         cone[2] = 25;
25329371c9d4SSatish Balay         cone[3] = 23;
25339566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 5, cone));
25349371c9d4SSatish Balay         ornt[0] = -1;
25359371c9d4SSatish Balay         ornt[1] = -1;
25369371c9d4SSatish Balay         ornt[2] = -1;
25379371c9d4SSatish Balay         ornt[3] = -1;
25389566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 5, ornt));
25392829fed8SMatthew G. Knepley         /* Edges */
25409371c9d4SSatish Balay         cone[0] = 6;
25419371c9d4SSatish Balay         cone[1] = 7;
25429566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 14, cone));
25439371c9d4SSatish Balay         cone[0] = 7;
25449371c9d4SSatish Balay         cone[1] = 8;
25459566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 15, cone));
25469371c9d4SSatish Balay         cone[0] = 8;
25479371c9d4SSatish Balay         cone[1] = 9;
25489566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 16, cone));
25499371c9d4SSatish Balay         cone[0] = 9;
25509371c9d4SSatish Balay         cone[1] = 6;
25519566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 17, cone));
25529371c9d4SSatish Balay         cone[0] = 10;
25539371c9d4SSatish Balay         cone[1] = 11;
25549566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 18, cone));
25559371c9d4SSatish Balay         cone[0] = 11;
25569371c9d4SSatish Balay         cone[1] = 7;
25579566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 19, cone));
25589371c9d4SSatish Balay         cone[0] = 6;
25599371c9d4SSatish Balay         cone[1] = 10;
25609566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 20, cone));
25619371c9d4SSatish Balay         cone[0] = 12;
25629371c9d4SSatish Balay         cone[1] = 13;
25639566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 21, cone));
25649371c9d4SSatish Balay         cone[0] = 13;
25659371c9d4SSatish Balay         cone[1] = 11;
25669566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 22, cone));
25679371c9d4SSatish Balay         cone[0] = 10;
25689371c9d4SSatish Balay         cone[1] = 12;
25699566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 23, cone));
25709371c9d4SSatish Balay         cone[0] = 13;
25719371c9d4SSatish Balay         cone[1] = 8;
25729566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 24, cone));
25739371c9d4SSatish Balay         cone[0] = 12;
25749371c9d4SSatish Balay         cone[1] = 9;
25759566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 25, cone));
257645da822fSValeria Barra       }
25779566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
25789566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
25792829fed8SMatthew G. Knepley       /* Build coordinates */
25809566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
2581dd400576SPatrick Sanan       if (rank == 0) {
25829371c9d4SSatish Balay         coordsIn[0 * embedDim + 0] = -R;
25839371c9d4SSatish Balay         coordsIn[0 * embedDim + 1] = R;
25849371c9d4SSatish Balay         coordsIn[0 * embedDim + 2] = -R;
25859371c9d4SSatish Balay         coordsIn[1 * embedDim + 0] = R;
25869371c9d4SSatish Balay         coordsIn[1 * embedDim + 1] = R;
25879371c9d4SSatish Balay         coordsIn[1 * embedDim + 2] = -R;
25889371c9d4SSatish Balay         coordsIn[2 * embedDim + 0] = R;
25899371c9d4SSatish Balay         coordsIn[2 * embedDim + 1] = -R;
25909371c9d4SSatish Balay         coordsIn[2 * embedDim + 2] = -R;
25919371c9d4SSatish Balay         coordsIn[3 * embedDim + 0] = -R;
25929371c9d4SSatish Balay         coordsIn[3 * embedDim + 1] = -R;
25939371c9d4SSatish Balay         coordsIn[3 * embedDim + 2] = -R;
25949371c9d4SSatish Balay         coordsIn[4 * embedDim + 0] = -R;
25959371c9d4SSatish Balay         coordsIn[4 * embedDim + 1] = R;
25969371c9d4SSatish Balay         coordsIn[4 * embedDim + 2] = R;
25979371c9d4SSatish Balay         coordsIn[5 * embedDim + 0] = R;
25989371c9d4SSatish Balay         coordsIn[5 * embedDim + 1] = R;
25999371c9d4SSatish Balay         coordsIn[5 * embedDim + 2] = R;
26009371c9d4SSatish Balay         coordsIn[6 * embedDim + 0] = -R;
26019371c9d4SSatish Balay         coordsIn[6 * embedDim + 1] = -R;
26029371c9d4SSatish Balay         coordsIn[6 * embedDim + 2] = R;
26039371c9d4SSatish Balay         coordsIn[7 * embedDim + 0] = R;
26049371c9d4SSatish Balay         coordsIn[7 * embedDim + 1] = -R;
26059371c9d4SSatish Balay         coordsIn[7 * embedDim + 2] = R;
260665a81367SMatthew G. Knepley       }
260745da822fSValeria Barra     }
260865a81367SMatthew G. Knepley     break;
260965a81367SMatthew G. Knepley   case 3:
2610116ded15SMatthew G. Knepley     if (simplex) {
2611116ded15SMatthew G. Knepley       const PetscReal edgeLen         = 1.0 / PETSC_PHI;
261251a74b61SMatthew G. Knepley       PetscReal       vertexA[4]      = {0.5, 0.5, 0.5, 0.5};
261351a74b61SMatthew G. Knepley       PetscReal       vertexB[4]      = {1.0, 0.0, 0.0, 0.0};
261451a74b61SMatthew G. Knepley       PetscReal       vertexC[4]      = {0.5, 0.5 * PETSC_PHI, 0.5 / PETSC_PHI, 0.0};
2615116ded15SMatthew G. Knepley       const PetscInt  degree          = 12;
2616116ded15SMatthew G. Knepley       PetscInt        s[4]            = {1, 1, 1};
26179371c9d4SSatish Balay       PetscInt        evenPerm[12][4] = {
26189371c9d4SSatish Balay         {0, 1, 2, 3},
26199371c9d4SSatish Balay         {0, 2, 3, 1},
26209371c9d4SSatish Balay         {0, 3, 1, 2},
26219371c9d4SSatish Balay         {1, 0, 3, 2},
26229371c9d4SSatish Balay         {1, 2, 0, 3},
26239371c9d4SSatish Balay         {1, 3, 2, 0},
26249371c9d4SSatish Balay         {2, 0, 1, 3},
26259371c9d4SSatish Balay         {2, 1, 3, 0},
26269371c9d4SSatish Balay         {2, 3, 0, 1},
26279371c9d4SSatish Balay         {3, 0, 2, 1},
26289371c9d4SSatish Balay         {3, 1, 0, 2},
26299371c9d4SSatish Balay         {3, 2, 1, 0}
26309371c9d4SSatish Balay       };
2631116ded15SMatthew G. Knepley       PetscInt  cone[4];
2632116ded15SMatthew G. Knepley       PetscInt *graph, p, i, j, k, l;
2633116ded15SMatthew G. Knepley 
26349371c9d4SSatish Balay       vertexA[0] *= R;
26359371c9d4SSatish Balay       vertexA[1] *= R;
26369371c9d4SSatish Balay       vertexA[2] *= R;
26379371c9d4SSatish Balay       vertexA[3] *= R;
26389371c9d4SSatish Balay       vertexB[0] *= R;
26399371c9d4SSatish Balay       vertexB[1] *= R;
26409371c9d4SSatish Balay       vertexB[2] *= R;
26419371c9d4SSatish Balay       vertexB[3] *= R;
26429371c9d4SSatish Balay       vertexC[0] *= R;
26439371c9d4SSatish Balay       vertexC[1] *= R;
26449371c9d4SSatish Balay       vertexC[2] *= R;
26459371c9d4SSatish Balay       vertexC[3] *= R;
2646dd400576SPatrick Sanan       numCells    = rank == 0 ? 600 : 0;
2647dd400576SPatrick Sanan       numVerts    = rank == 0 ? 120 : 0;
2648116ded15SMatthew G. Knepley       firstVertex = numCells;
2649116ded15SMatthew G. Knepley       /* Use the 600-cell, which for a unit sphere has coordinates which are
2650116ded15SMatthew G. Knepley 
2651116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm 1,    \pm 1, \pm 1)                          16
2652116ded15SMatthew G. Knepley                (\pm 1,    0,       0,      0)  all cyclic permutations   8
2653116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm phi, \pm 1/phi, 0)  all even permutations    96
2654116ded15SMatthew G. Knepley 
2655116ded15SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
26566333ae4fSvaleriabarra          length is then given by 1/\phi = 0.61803.
2657116ded15SMatthew G. Knepley 
2658116ded15SMatthew G. Knepley          http://buzzard.pugetsound.edu/sage-practice/ch03s03.html
2659116ded15SMatthew G. Knepley          http://mathworld.wolfram.com/600-Cell.html
2660116ded15SMatthew G. Knepley       */
2661116ded15SMatthew G. Knepley       /* Construct vertices */
26629566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
2663116ded15SMatthew G. Knepley       i = 0;
2664dd400576SPatrick Sanan       if (rank == 0) {
2665116ded15SMatthew G. Knepley         for (s[0] = -1; s[0] < 2; s[0] += 2) {
2666116ded15SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
2667116ded15SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
2668116ded15SMatthew G. Knepley               for (s[3] = -1; s[3] < 2; s[3] += 2) {
2669116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[d] * vertexA[d];
2670116ded15SMatthew G. Knepley                 ++i;
2671116ded15SMatthew G. Knepley               }
2672116ded15SMatthew G. Knepley             }
2673116ded15SMatthew G. Knepley           }
2674116ded15SMatthew G. Knepley         }
2675116ded15SMatthew G. Knepley         for (p = 0; p < embedDim; ++p) {
2676116ded15SMatthew G. Knepley           s[1] = s[2] = s[3] = 1;
2677116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
2678116ded15SMatthew G. Knepley             for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[(d + p) % embedDim] * vertexB[(d + p) % embedDim];
2679116ded15SMatthew G. Knepley             ++i;
2680116ded15SMatthew G. Knepley           }
2681116ded15SMatthew G. Knepley         }
2682116ded15SMatthew G. Knepley         for (p = 0; p < 12; ++p) {
2683116ded15SMatthew G. Knepley           s[3] = 1;
2684116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
2685116ded15SMatthew G. Knepley             for (s[1] = -1; s[1] < 2; s[1] += 2) {
2686116ded15SMatthew G. Knepley               for (s[2] = -1; s[2] < 2; s[2] += 2) {
2687116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[evenPerm[p][d]] * vertexC[evenPerm[p][d]];
2688116ded15SMatthew G. Knepley                 ++i;
2689116ded15SMatthew G. Knepley               }
2690116ded15SMatthew G. Knepley             }
2691116ded15SMatthew G. Knepley           }
2692116ded15SMatthew G. Knepley         }
269345da822fSValeria Barra       }
269463a3b9bcSJacob Faibussowitsch       PetscCheck(i == numVerts, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertices %" PetscInt_FMT " != %" PetscInt_FMT, i, numVerts);
2695116ded15SMatthew G. Knepley       /* Construct graph */
26969566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * numVerts, &graph));
2697116ded15SMatthew G. Knepley       for (i = 0; i < numVerts; ++i) {
2698116ded15SMatthew G. Knepley         for (j = 0, k = 0; j < numVerts; ++j) {
26999371c9d4SSatish Balay           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i * embedDim], &coordsIn[j * embedDim]) - edgeLen) < PETSC_SMALL) {
27009371c9d4SSatish Balay             graph[i * numVerts + j] = 1;
27019371c9d4SSatish Balay             ++k;
27029371c9d4SSatish Balay           }
2703116ded15SMatthew G. Knepley         }
270463a3b9bcSJacob Faibussowitsch         PetscCheck(k == degree, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertex %" PetscInt_FMT " degree %" PetscInt_FMT " != %" PetscInt_FMT, i, k, degree);
2705116ded15SMatthew G. Knepley       }
2706116ded15SMatthew G. Knepley       /* Build Topology */
27079566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts));
270848a46eb9SPierre Jolivet       for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim));
27099566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
2710116ded15SMatthew G. Knepley       /* Cells */
2711dd400576SPatrick Sanan       if (rank == 0) {
2712116ded15SMatthew G. Knepley         for (i = 0, c = 0; i < numVerts; ++i) {
2713116ded15SMatthew G. Knepley           for (j = 0; j < i; ++j) {
2714116ded15SMatthew G. Knepley             for (k = 0; k < j; ++k) {
2715116ded15SMatthew G. Knepley               for (l = 0; l < k; ++l) {
27169371c9d4SSatish Balay                 if (graph[i * numVerts + j] && graph[j * numVerts + k] && graph[k * numVerts + i] && graph[l * numVerts + i] && graph[l * numVerts + j] && graph[l * numVerts + k]) {
27179371c9d4SSatish Balay                   cone[0] = firstVertex + i;
27189371c9d4SSatish Balay                   cone[1] = firstVertex + j;
27199371c9d4SSatish Balay                   cone[2] = firstVertex + k;
27209371c9d4SSatish Balay                   cone[3] = firstVertex + l;
2721116ded15SMatthew G. Knepley                   /* Check orientation: https://ef.gy/linear-algebra:normal-vectors-in-higher-dimensional-spaces */
2722116ded15SMatthew G. Knepley                   {
27239371c9d4SSatish Balay                     const PetscInt epsilon[4][4][4][4] = {
27249371c9d4SSatish Balay                       {{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},  {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, -1, 0}}, {{0, 0, 0, 0}, {0, 0, 0, -1}, {0, 0, 0, 0}, {0, 1, 0, 0}}, {{0, 0, 0, 0}, {0, 0, 1, 0}, {0, -1, 0, 0}, {0, 0, 0, 0}}},
2725116ded15SMatthew G. Knepley 
27269371c9d4SSatish Balay                       {{{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, -1}, {0, 0, 1, 0}}, {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},  {{0, 0, 0, 1}, {0, 0, 0, 0}, {0, 0, 0, 0}, {-1, 0, 0, 0}}, {{0, 0, -1, 0}, {0, 0, 0, 0}, {1, 0, 0, 0}, {0, 0, 0, 0}}},
2727116ded15SMatthew G. Knepley 
27289371c9d4SSatish Balay                       {{{0, 0, 0, 0}, {0, 0, 0, 1}, {0, 0, 0, 0}, {0, -1, 0, 0}}, {{0, 0, 0, -1}, {0, 0, 0, 0}, {0, 0, 0, 0}, {1, 0, 0, 0}}, {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}},  {{0, 1, 0, 0}, {-1, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}},
2729116ded15SMatthew G. Knepley 
27309371c9d4SSatish Balay                       {{{0, 0, 0, 0}, {0, 0, -1, 0}, {0, 1, 0, 0}, {0, 0, 0, 0}}, {{0, 0, 1, 0}, {0, 0, 0, 0}, {-1, 0, 0, 0}, {0, 0, 0, 0}}, {{0, -1, 0, 0}, {1, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}}, {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}} }
27319371c9d4SSatish Balay                     };
2732116ded15SMatthew G. Knepley                     PetscReal normal[4];
2733116ded15SMatthew G. Knepley                     PetscInt  e, f, g;
2734116ded15SMatthew G. Knepley 
2735116ded15SMatthew G. Knepley                     for (d = 0; d < embedDim; ++d) {
2736116ded15SMatthew G. Knepley                       normal[d] = 0.0;
2737116ded15SMatthew G. Knepley                       for (e = 0; e < embedDim; ++e) {
2738116ded15SMatthew G. Knepley                         for (f = 0; f < embedDim; ++f) {
2739116ded15SMatthew G. Knepley                           for (g = 0; g < embedDim; ++g) {
2740116ded15SMatthew 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]);
2741116ded15SMatthew G. Knepley                           }
2742116ded15SMatthew G. Knepley                         }
2743116ded15SMatthew G. Knepley                       }
2744116ded15SMatthew G. Knepley                     }
27459371c9d4SSatish Balay                     if (DotReal(embedDim, normal, &coordsIn[i * embedDim]) < 0) {
27469371c9d4SSatish Balay                       PetscInt tmp = cone[1];
27479371c9d4SSatish Balay                       cone[1]      = cone[2];
27489371c9d4SSatish Balay                       cone[2]      = tmp;
27499371c9d4SSatish Balay                     }
2750116ded15SMatthew G. Knepley                   }
27519566063dSJacob Faibussowitsch                   PetscCall(DMPlexSetCone(dm, c++, cone));
2752116ded15SMatthew G. Knepley                 }
2753116ded15SMatthew G. Knepley               }
2754116ded15SMatthew G. Knepley             }
2755116ded15SMatthew G. Knepley           }
2756116ded15SMatthew G. Knepley         }
275745da822fSValeria Barra       }
27589566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
27599566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
27609566063dSJacob Faibussowitsch       PetscCall(PetscFree(graph));
2761116ded15SMatthew G. Knepley     }
2762f4d061e9SPierre Jolivet     break;
2763d71ae5a4SJacob Faibussowitsch   default:
2764d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension for sphere: %" PetscInt_FMT, dim);
276565a81367SMatthew G. Knepley   }
276665a81367SMatthew G. Knepley   /* Create coordinates */
27679566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
27689566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
27699566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, embedDim));
27709566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numVerts));
27712829fed8SMatthew G. Knepley   for (v = firstVertex; v < firstVertex + numVerts; ++v) {
27729566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, embedDim));
27739566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, embedDim));
27742829fed8SMatthew G. Knepley   }
27759566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
27769566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
27779566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
27789566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, embedDim));
27799566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
27809566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
27819566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
27829566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
27839371c9d4SSatish Balay   for (v = 0; v < numVerts; ++v)
2784ad540459SPierre Jolivet     for (d = 0; d < embedDim; ++d) coords[v * embedDim + d] = coordsIn[v * embedDim + d];
27859566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
27869566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
27879566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
27889566063dSJacob Faibussowitsch   PetscCall(PetscFree(coordsIn));
278951a74b61SMatthew G. Knepley   {
279051a74b61SMatthew G. Knepley     DM          cdm;
279151a74b61SMatthew G. Knepley     PetscDS     cds;
27929318fe57SMatthew G. Knepley     PetscScalar c = R;
279351a74b61SMatthew G. Knepley 
27949566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCoordinateSpace(dm, 1, snapToSphere));
27959566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
27969566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
27979566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, 1, &c));
279851a74b61SMatthew G. Knepley   }
279946139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
28009318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
28019566063dSJacob Faibussowitsch   if (simplex) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
28023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
28039318fe57SMatthew G. Knepley }
28049318fe57SMatthew G. Knepley 
2805b7f5c055SJed Brown typedef void (*TPSEvaluateFunc)(const PetscReal[], PetscReal *, PetscReal[], PetscReal (*)[3]);
2806b7f5c055SJed Brown 
2807b7f5c055SJed Brown /*
2808b7f5c055SJed Brown  The Schwarz P implicit surface is
2809b7f5c055SJed Brown 
2810b7f5c055SJed Brown      f(x) = cos(x0) + cos(x1) + cos(x2) = 0
2811b7f5c055SJed Brown */
2812d71ae5a4SJacob Faibussowitsch static void TPSEvaluate_SchwarzP(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3])
2813d71ae5a4SJacob Faibussowitsch {
2814b7f5c055SJed Brown   PetscReal c[3] = {PetscCosReal(y[0] * PETSC_PI), PetscCosReal(y[1] * PETSC_PI), PetscCosReal(y[2] * PETSC_PI)};
2815b7f5c055SJed Brown   PetscReal g[3] = {-PetscSinReal(y[0] * PETSC_PI), -PetscSinReal(y[1] * PETSC_PI), -PetscSinReal(y[2] * PETSC_PI)};
2816b7f5c055SJed Brown   f[0]           = c[0] + c[1] + c[2];
2817b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) {
2818b7f5c055SJed Brown     grad[i] = PETSC_PI * g[i];
2819ad540459SPierre Jolivet     for (PetscInt j = 0; j < 3; j++) hess[i][j] = (i == j) ? -PetscSqr(PETSC_PI) * c[i] : 0.;
2820b7f5c055SJed Brown   }
2821b7f5c055SJed Brown }
2822b7f5c055SJed Brown 
28234663dae6SJed Brown // u[] is a tentative normal on input. Replace with the implicit function gradient in the same direction
2824d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSExtrudeNormalFunc_SchwarzP(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt r, PetscScalar u[], void *ctx)
2825d71ae5a4SJacob Faibussowitsch {
2826ad540459SPierre Jolivet   for (PetscInt i = 0; i < 3; i++) u[i] = -PETSC_PI * PetscSinReal(x[i] * PETSC_PI);
28273ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
28284663dae6SJed Brown }
28294663dae6SJed Brown 
2830b7f5c055SJed Brown /*
2831b7f5c055SJed Brown  The Gyroid implicit surface is
2832b7f5c055SJed Brown 
2833b7f5c055SJed 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)
2834b7f5c055SJed Brown 
2835b7f5c055SJed Brown */
2836d71ae5a4SJacob Faibussowitsch static void TPSEvaluate_Gyroid(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3])
2837d71ae5a4SJacob Faibussowitsch {
2838b7f5c055SJed Brown   PetscReal s[3] = {PetscSinReal(PETSC_PI * y[0]), PetscSinReal(PETSC_PI * (y[1] + .5)), PetscSinReal(PETSC_PI * (y[2] + .25))};
2839b7f5c055SJed Brown   PetscReal c[3] = {PetscCosReal(PETSC_PI * y[0]), PetscCosReal(PETSC_PI * (y[1] + .5)), PetscCosReal(PETSC_PI * (y[2] + .25))};
2840b7f5c055SJed Brown   f[0]           = s[0] * c[1] + s[1] * c[2] + s[2] * c[0];
2841b7f5c055SJed Brown   grad[0]        = PETSC_PI * (c[0] * c[1] - s[2] * s[0]);
2842b7f5c055SJed Brown   grad[1]        = PETSC_PI * (c[1] * c[2] - s[0] * s[1]);
2843b7f5c055SJed Brown   grad[2]        = PETSC_PI * (c[2] * c[0] - s[1] * s[2]);
2844b7f5c055SJed Brown   hess[0][0]     = -PetscSqr(PETSC_PI) * (s[0] * c[1] + s[2] * c[0]);
2845b7f5c055SJed Brown   hess[0][1]     = -PetscSqr(PETSC_PI) * (c[0] * s[1]);
2846b7f5c055SJed Brown   hess[0][2]     = -PetscSqr(PETSC_PI) * (c[2] * s[0]);
2847b7f5c055SJed Brown   hess[1][0]     = -PetscSqr(PETSC_PI) * (s[1] * c[2] + s[0] * c[1]);
2848b7f5c055SJed Brown   hess[1][1]     = -PetscSqr(PETSC_PI) * (c[1] * s[2]);
2849b7f5c055SJed Brown   hess[2][2]     = -PetscSqr(PETSC_PI) * (c[0] * s[1]);
2850b7f5c055SJed Brown   hess[2][0]     = -PetscSqr(PETSC_PI) * (s[2] * c[0] + s[1] * c[2]);
2851b7f5c055SJed Brown   hess[2][1]     = -PetscSqr(PETSC_PI) * (c[2] * s[0]);
2852b7f5c055SJed Brown   hess[2][2]     = -PetscSqr(PETSC_PI) * (c[1] * s[2]);
2853b7f5c055SJed Brown }
2854b7f5c055SJed Brown 
28554663dae6SJed Brown // u[] is a tentative normal on input. Replace with the implicit function gradient in the same direction
2856d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSExtrudeNormalFunc_Gyroid(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt r, PetscScalar u[], void *ctx)
2857d71ae5a4SJacob Faibussowitsch {
28584663dae6SJed Brown   PetscReal s[3] = {PetscSinReal(PETSC_PI * x[0]), PetscSinReal(PETSC_PI * (x[1] + .5)), PetscSinReal(PETSC_PI * (x[2] + .25))};
28594663dae6SJed Brown   PetscReal c[3] = {PetscCosReal(PETSC_PI * x[0]), PetscCosReal(PETSC_PI * (x[1] + .5)), PetscCosReal(PETSC_PI * (x[2] + .25))};
28604663dae6SJed Brown   u[0]           = PETSC_PI * (c[0] * c[1] - s[2] * s[0]);
28614663dae6SJed Brown   u[1]           = PETSC_PI * (c[1] * c[2] - s[0] * s[1]);
28624663dae6SJed Brown   u[2]           = PETSC_PI * (c[2] * c[0] - s[1] * s[2]);
28633ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
28644663dae6SJed Brown }
28654663dae6SJed Brown 
2866b7f5c055SJed Brown /*
2867b7f5c055SJed Brown    We wish to solve
2868b7f5c055SJed Brown 
2869b7f5c055SJed Brown          min_y || y - x ||^2  subject to f(y) = 0
2870b7f5c055SJed Brown 
2871b7f5c055SJed Brown    Let g(y) = grad(f).  The minimization problem is equivalent to asking to satisfy
2872b7f5c055SJed 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
2873b7f5c055SJed Brown    tangent space and ask for both components in the tangent space to be zero.
2874b7f5c055SJed Brown 
2875b7f5c055SJed Brown    Take g to be a column vector and compute the "full QR" factorization Q R = g,
2876b7f5c055SJed Brown    where Q = I - 2 n n^T is a symmetric orthogonal matrix.
2877b7f5c055SJed Brown    The first column of Q is parallel to g so the remaining two columns span the null space.
2878b7f5c055SJed Brown    Let Qn = Q[:,1:] be those remaining columns.  Then Qn Qn^T is an orthogonal projector into the tangent space.
2879da81f932SPierre Jolivet    Since Q is symmetric, this is equivalent to multiplying by Q and taking the last two entries.
2880b7f5c055SJed Brown    In total, we have a system of 3 equations in 3 unknowns:
2881b7f5c055SJed Brown 
2882b7f5c055SJed Brown      f(y) = 0                       1 equation
2883b7f5c055SJed Brown      Qn^T (y - x) = 0               2 equations
2884b7f5c055SJed Brown 
2885b7f5c055SJed Brown    Here, we compute the residual and Jacobian of this system.
2886b7f5c055SJed Brown */
2887d71ae5a4SJacob Faibussowitsch static void TPSNearestPointResJac(TPSEvaluateFunc feval, const PetscScalar x[], const PetscScalar y[], PetscScalar res[], PetscScalar J[])
2888d71ae5a4SJacob Faibussowitsch {
2889b7f5c055SJed Brown   PetscReal yreal[3] = {PetscRealPart(y[0]), PetscRealPart(y[1]), PetscRealPart(y[2])};
2890b7f5c055SJed Brown   PetscReal d[3]     = {PetscRealPart(y[0] - x[0]), PetscRealPart(y[1] - x[1]), PetscRealPart(y[2] - x[2])};
28912f0490c0SSatish Balay   PetscReal f, grad[3], n[3], norm, norm_y[3], nd, nd_y[3], sign;
28929371c9d4SSatish Balay   PetscReal n_y[3][3] = {
28939371c9d4SSatish Balay     {0, 0, 0},
28949371c9d4SSatish Balay     {0, 0, 0},
28959371c9d4SSatish Balay     {0, 0, 0}
28969371c9d4SSatish Balay   };
2897b7f5c055SJed Brown 
2898b7f5c055SJed Brown   feval(yreal, &f, grad, n_y);
2899b7f5c055SJed Brown 
2900b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) n[i] = grad[i];
2901b7f5c055SJed Brown   norm = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2]));
2902ad540459SPierre Jolivet   for (PetscInt i = 0; i < 3; i++) norm_y[i] = 1. / norm * n[i] * n_y[i][i];
2903b7f5c055SJed Brown 
2904b7f5c055SJed Brown   // Define the Householder reflector
2905b7f5c055SJed Brown   sign = n[0] >= 0 ? 1. : -1.;
2906b7f5c055SJed Brown   n[0] += norm * sign;
2907b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) n_y[0][i] += norm_y[i] * sign;
2908b7f5c055SJed Brown 
2909b7f5c055SJed Brown   norm      = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2]));
2910b7f5c055SJed Brown   norm_y[0] = 1. / norm * (n[0] * n_y[0][0]);
2911b7f5c055SJed Brown   norm_y[1] = 1. / norm * (n[0] * n_y[0][1] + n[1] * n_y[1][1]);
2912b7f5c055SJed Brown   norm_y[2] = 1. / norm * (n[0] * n_y[0][2] + n[2] * n_y[2][2]);
2913b7f5c055SJed Brown 
2914b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) {
2915b7f5c055SJed Brown     n[i] /= norm;
2916b7f5c055SJed Brown     for (PetscInt j = 0; j < 3; j++) {
2917b7f5c055SJed Brown       // note that n[i] is n_old[i]/norm when executing the code below
2918b7f5c055SJed Brown       n_y[i][j] = n_y[i][j] / norm - n[i] / norm * norm_y[j];
2919b7f5c055SJed Brown     }
2920b7f5c055SJed Brown   }
2921b7f5c055SJed Brown 
2922b7f5c055SJed Brown   nd = n[0] * d[0] + n[1] * d[1] + n[2] * d[2];
2923b7f5c055SJed 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];
2924b7f5c055SJed Brown 
2925b7f5c055SJed Brown   res[0] = f;
2926b7f5c055SJed Brown   res[1] = d[1] - 2 * n[1] * nd;
2927b7f5c055SJed Brown   res[2] = d[2] - 2 * n[2] * nd;
2928b7f5c055SJed Brown   // J[j][i] is J_{ij} (column major)
2929b7f5c055SJed Brown   for (PetscInt j = 0; j < 3; j++) {
2930b7f5c055SJed Brown     J[0 + j * 3] = grad[j];
2931b7f5c055SJed Brown     J[1 + j * 3] = (j == 1) * 1. - 2 * (n_y[1][j] * nd + n[1] * nd_y[j]);
2932b7f5c055SJed Brown     J[2 + j * 3] = (j == 2) * 1. - 2 * (n_y[2][j] * nd + n[2] * nd_y[j]);
2933b7f5c055SJed Brown   }
2934b7f5c055SJed Brown }
2935b7f5c055SJed Brown 
2936b7f5c055SJed Brown /*
2937b7f5c055SJed Brown    Project x to the nearest point on the implicit surface using Newton's method.
2938b7f5c055SJed Brown */
2939d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSNearestPoint(TPSEvaluateFunc feval, PetscScalar x[])
2940d71ae5a4SJacob Faibussowitsch {
2941b7f5c055SJed Brown   PetscScalar y[3] = {x[0], x[1], x[2]}; // Initial guess
2942b7f5c055SJed Brown 
2943b7f5c055SJed Brown   PetscFunctionBegin;
2944b7f5c055SJed Brown   for (PetscInt iter = 0; iter < 10; iter++) {
2945b7f5c055SJed Brown     PetscScalar res[3], J[9];
2946b7f5c055SJed Brown     PetscReal   resnorm;
2947b7f5c055SJed Brown     TPSNearestPointResJac(feval, x, y, res, J);
2948b7f5c055SJed Brown     resnorm = PetscSqrtReal(PetscSqr(PetscRealPart(res[0])) + PetscSqr(PetscRealPart(res[1])) + PetscSqr(PetscRealPart(res[2])));
2949b7f5c055SJed Brown     if (0) { // Turn on this monitor if you need to confirm quadratic convergence
295063a3b9bcSJacob 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])));
2951b7f5c055SJed Brown     }
2952b7f5c055SJed Brown     if (resnorm < PETSC_SMALL) break;
2953b7f5c055SJed Brown 
2954b7f5c055SJed Brown     // Take the Newton step
29559566063dSJacob Faibussowitsch     PetscCall(PetscKernel_A_gets_inverse_A_3(J, 0., PETSC_FALSE, NULL));
2956b7f5c055SJed Brown     PetscKernel_v_gets_v_minus_A_times_w_3(y, J, res);
2957b7f5c055SJed Brown   }
2958b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) x[i] = y[i];
29593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2960b7f5c055SJed Brown }
2961b7f5c055SJed Brown 
2962b7f5c055SJed Brown const char *const DMPlexTPSTypes[] = {"SCHWARZ_P", "GYROID", "DMPlexTPSType", "DMPLEX_TPS_", NULL};
2963b7f5c055SJed Brown 
2964d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateTPSMesh_Internal(DM dm, DMPlexTPSType tpstype, const PetscInt extent[], const DMBoundaryType periodic[], PetscBool tps_distribute, PetscInt refinements, PetscInt layers, PetscReal thickness)
2965d71ae5a4SJacob Faibussowitsch {
2966b7f5c055SJed Brown   PetscMPIInt rank;
2967b7f5c055SJed Brown   PetscInt    topoDim = 2, spaceDim = 3, numFaces = 0, numVertices = 0, numEdges = 0;
2968b7f5c055SJed Brown   PetscInt(*edges)[2] = NULL, *edgeSets = NULL;
2969b7f5c055SJed Brown   PetscInt            *cells_flat = NULL;
2970b7f5c055SJed Brown   PetscReal           *vtxCoords  = NULL;
2971b7f5c055SJed Brown   TPSEvaluateFunc      evalFunc   = NULL;
29724663dae6SJed Brown   PetscSimplePointFunc normalFunc = NULL;
2973b7f5c055SJed Brown   DMLabel              label;
2974b7f5c055SJed Brown 
2975b7f5c055SJed Brown   PetscFunctionBegin;
297646139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
29779566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
297863a3b9bcSJacob 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);
2979b7f5c055SJed Brown   switch (tpstype) {
2980b7f5c055SJed Brown   case DMPLEX_TPS_SCHWARZ_P:
2981b7f5c055SJed 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");
2982c5853193SPierre Jolivet     if (rank == 0) {
2983b7f5c055SJed Brown       PetscInt(*cells)[6][4][4] = NULL; // [junction, junction-face, cell, conn]
2984b7f5c055SJed Brown       PetscInt  Njunctions = 0, Ncuts = 0, Npipes[3], vcount;
2985b7f5c055SJed Brown       PetscReal L = 1;
2986b7f5c055SJed Brown 
2987b7f5c055SJed Brown       Npipes[0]   = (extent[0] + 1) * extent[1] * extent[2];
2988b7f5c055SJed Brown       Npipes[1]   = extent[0] * (extent[1] + 1) * extent[2];
2989b7f5c055SJed Brown       Npipes[2]   = extent[0] * extent[1] * (extent[2] + 1);
2990b7f5c055SJed Brown       Njunctions  = extent[0] * extent[1] * extent[2];
2991b7f5c055SJed Brown       Ncuts       = 2 * (extent[0] * extent[1] + extent[1] * extent[2] + extent[2] * extent[0]);
2992b7f5c055SJed Brown       numVertices = 4 * (Npipes[0] + Npipes[1] + Npipes[2]) + 8 * Njunctions;
29939566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(3 * numVertices, &vtxCoords));
29949566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Njunctions, &cells));
29959566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Ncuts * 4, &edges));
29969566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Ncuts * 4, &edgeSets));
2997b7f5c055SJed Brown       // x-normal pipes
2998b7f5c055SJed Brown       vcount = 0;
2999b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0] + 1; i++) {
3000b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3001b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2]; k++) {
3002b7f5c055SJed Brown             for (PetscInt l = 0; l < 4; l++) {
3003b7f5c055SJed Brown               vtxCoords[vcount++] = (2 * i - 1) * L;
3004b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * j * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3005b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * k * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3006b7f5c055SJed Brown             }
3007b7f5c055SJed Brown           }
3008b7f5c055SJed Brown         }
3009b7f5c055SJed Brown       }
3010b7f5c055SJed Brown       // y-normal pipes
3011b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0]; i++) {
3012b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1] + 1; j++) {
3013b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2]; k++) {
3014b7f5c055SJed Brown             for (PetscInt l = 0; l < 4; l++) {
3015b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * i * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3016b7f5c055SJed Brown               vtxCoords[vcount++] = (2 * j - 1) * L;
3017b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * k * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3018b7f5c055SJed Brown             }
3019b7f5c055SJed Brown           }
3020b7f5c055SJed Brown         }
3021b7f5c055SJed Brown       }
3022b7f5c055SJed Brown       // z-normal pipes
3023b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0]; i++) {
3024b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3025b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2] + 1; k++) {
3026b7f5c055SJed Brown             for (PetscInt l = 0; l < 4; l++) {
3027b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * i * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3028b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * j * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3029b7f5c055SJed Brown               vtxCoords[vcount++] = (2 * k - 1) * L;
3030b7f5c055SJed Brown             }
3031b7f5c055SJed Brown           }
3032b7f5c055SJed Brown         }
3033b7f5c055SJed Brown       }
3034b7f5c055SJed Brown       // junctions
3035b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0]; i++) {
3036b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3037b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2]; k++) {
3038b7f5c055SJed Brown             const PetscInt J = (i * extent[1] + j) * extent[2] + k, Jvoff = (Npipes[0] + Npipes[1] + Npipes[2]) * 4 + J * 8;
3039b7f5c055SJed Brown             PetscCheck(vcount / 3 == Jvoff, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unexpected vertex count");
3040b7f5c055SJed Brown             for (PetscInt ii = 0; ii < 2; ii++) {
3041b7f5c055SJed Brown               for (PetscInt jj = 0; jj < 2; jj++) {
3042b7f5c055SJed Brown                 for (PetscInt kk = 0; kk < 2; kk++) {
3043b7f5c055SJed Brown                   double Ls           = (1 - sqrt(2) / 4) * L;
3044b7f5c055SJed Brown                   vtxCoords[vcount++] = 2 * i * L + (2 * ii - 1) * Ls;
3045b7f5c055SJed Brown                   vtxCoords[vcount++] = 2 * j * L + (2 * jj - 1) * Ls;
3046b7f5c055SJed Brown                   vtxCoords[vcount++] = 2 * k * L + (2 * kk - 1) * Ls;
3047b7f5c055SJed Brown                 }
3048b7f5c055SJed Brown               }
3049b7f5c055SJed Brown             }
3050b7f5c055SJed Brown             const PetscInt jfaces[3][2][4] = {
3051b7f5c055SJed Brown               {{3, 1, 0, 2}, {7, 5, 4, 6}}, // x-aligned
3052b7f5c055SJed Brown               {{5, 4, 0, 1}, {7, 6, 2, 3}}, // y-aligned
3053b7f5c055SJed Brown               {{6, 2, 0, 4}, {7, 3, 1, 5}}  // z-aligned
3054b7f5c055SJed Brown             };
3055b7f5c055SJed Brown             const PetscInt pipe_lo[3] = {// vertex numbers of pipes
30569371c9d4SSatish Balay                                          ((i * extent[1] + j) * extent[2] + k) * 4, ((i * (extent[1] + 1) + j) * extent[2] + k + Npipes[0]) * 4, ((i * extent[1] + j) * (extent[2] + 1) + k + Npipes[0] + Npipes[1]) * 4};
3057b7f5c055SJed Brown             const PetscInt pipe_hi[3] = {// vertex numbers of pipes
30589371c9d4SSatish Balay                                          (((i + 1) * extent[1] + j) * extent[2] + k) * 4, ((i * (extent[1] + 1) + j + 1) * extent[2] + k + Npipes[0]) * 4, ((i * extent[1] + j) * (extent[2] + 1) + k + 1 + Npipes[0] + Npipes[1]) * 4};
3059b7f5c055SJed Brown             for (PetscInt dir = 0; dir < 3; dir++) { // x,y,z
3060b7f5c055SJed Brown               const PetscInt ijk[3] = {i, j, k};
3061b7f5c055SJed Brown               for (PetscInt l = 0; l < 4; l++) { // rotations
3062b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][0] = pipe_lo[dir] + l;
3063b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][1] = Jvoff + jfaces[dir][0][l];
3064b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][2] = Jvoff + jfaces[dir][0][(l - 1 + 4) % 4];
3065b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][3] = pipe_lo[dir] + (l - 1 + 4) % 4;
3066b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][0] = Jvoff + jfaces[dir][1][l];
3067b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][1] = pipe_hi[dir] + l;
3068b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][2] = pipe_hi[dir] + (l - 1 + 4) % 4;
3069b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][3] = Jvoff + jfaces[dir][1][(l - 1 + 4) % 4];
3070b7f5c055SJed Brown                 if (ijk[dir] == 0) {
3071b7f5c055SJed Brown                   edges[numEdges][0] = pipe_lo[dir] + l;
3072b7f5c055SJed Brown                   edges[numEdges][1] = pipe_lo[dir] + (l + 1) % 4;
3073b7f5c055SJed Brown                   edgeSets[numEdges] = dir * 2 + 1;
3074b7f5c055SJed Brown                   numEdges++;
3075b7f5c055SJed Brown                 }
3076b7f5c055SJed Brown                 if (ijk[dir] + 1 == extent[dir]) {
3077b7f5c055SJed Brown                   edges[numEdges][0] = pipe_hi[dir] + l;
3078b7f5c055SJed Brown                   edges[numEdges][1] = pipe_hi[dir] + (l + 1) % 4;
3079b7f5c055SJed Brown                   edgeSets[numEdges] = dir * 2 + 2;
3080b7f5c055SJed Brown                   numEdges++;
3081b7f5c055SJed Brown                 }
3082b7f5c055SJed Brown               }
3083b7f5c055SJed Brown             }
3084b7f5c055SJed Brown           }
3085b7f5c055SJed Brown         }
3086b7f5c055SJed Brown       }
308763a3b9bcSJacob Faibussowitsch       PetscCheck(numEdges == Ncuts * 4, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Edge count %" PetscInt_FMT " incompatible with number of cuts %" PetscInt_FMT, numEdges, Ncuts);
3088b7f5c055SJed Brown       numFaces   = 24 * Njunctions;
3089b7f5c055SJed Brown       cells_flat = cells[0][0][0];
3090b7f5c055SJed Brown     }
3091b7f5c055SJed Brown     evalFunc   = TPSEvaluate_SchwarzP;
30924663dae6SJed Brown     normalFunc = TPSExtrudeNormalFunc_SchwarzP;
3093b7f5c055SJed Brown     break;
3094b7f5c055SJed Brown   case DMPLEX_TPS_GYROID:
3095c5853193SPierre Jolivet     if (rank == 0) {
3096b7f5c055SJed Brown       // This is a coarse mesh approximation of the gyroid shifted to being the zero of the level set
3097b7f5c055SJed Brown       //
3098b7f5c055SJed 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)
3099b7f5c055SJed Brown       //
3100b7f5c055SJed Brown       // on the cell [0,2]^3.
3101b7f5c055SJed Brown       //
3102b7f5c055SJed Brown       // Think about dividing that cell into four columns, and focus on the column [0,1]x[0,1]x[0,2].
3103b7f5c055SJed Brown       // If you looked at the gyroid in that column at different slices of z you would see that it kind of spins
3104b7f5c055SJed Brown       // like a boomerang:
3105b7f5c055SJed Brown       //
3106b7f5c055SJed Brown       //     z = 0          z = 1/4        z = 1/2        z = 3/4     //
3107b7f5c055SJed Brown       //     -----          -------        -------        -------     //
3108b7f5c055SJed Brown       //                                                              //
3109b7f5c055SJed Brown       //     +       +      +       +      +       +      +   \   +   //
3110b7f5c055SJed Brown       //      \                                   /            \      //
3111b7f5c055SJed Brown       //       \            `-_   _-'            /              }     //
3112b7f5c055SJed Brown       //        *-_            `-'            _-'              /      //
3113b7f5c055SJed Brown       //     +     `-+      +       +      +-'     +      +   /   +   //
3114b7f5c055SJed Brown       //                                                              //
3115b7f5c055SJed Brown       //                                                              //
3116b7f5c055SJed Brown       //     z = 1          z = 5/4        z = 3/2        z = 7/4     //
3117b7f5c055SJed Brown       //     -----          -------        -------        -------     //
3118b7f5c055SJed Brown       //                                                              //
3119b7f5c055SJed Brown       //     +-_     +      +       +      +     _-+      +   /   +   //
3120b7f5c055SJed Brown       //        `-_            _-_            _-`            /        //
3121b7f5c055SJed Brown       //           \        _-'   `-_        /              {         //
3122b7f5c055SJed Brown       //            \                       /                \        //
3123b7f5c055SJed Brown       //     +       +      +       +      +       +      +   \   +   //
3124b7f5c055SJed Brown       //
3125b7f5c055SJed Brown       //
3126b7f5c055SJed Brown       // This course mesh approximates each of these slices by two line segments,
3127b7f5c055SJed Brown       // and then connects the segments in consecutive layers with quadrilateral faces.
3128b7f5c055SJed Brown       // All of the end points of the segments are multiples of 1/4 except for the
3129b7f5c055SJed Brown       // point * in the picture for z = 0 above and the similar points in other layers.
3130b7f5c055SJed Brown       // That point is at (gamma, gamma, 0), where gamma is calculated below.
3131b7f5c055SJed Brown       //
3132b7f5c055SJed Brown       // The column  [1,2]x[1,2]x[0,2] looks the same as this column;
3133b7f5c055SJed Brown       // The columns [1,2]x[0,1]x[0,2] and [0,1]x[1,2]x[0,2] are mirror images.
3134b7f5c055SJed Brown       //
3135b7f5c055SJed Brown       // As for how this method turned into the names given to the vertices:
3136b7f5c055SJed Brown       // that was not systematic, it was just the way it worked out in my handwritten notes.
3137b7f5c055SJed Brown 
3138b7f5c055SJed Brown       PetscInt facesPerBlock = 64;
3139b7f5c055SJed Brown       PetscInt vertsPerBlock = 56;
3140b7f5c055SJed Brown       PetscInt extentPlus[3];
3141b7f5c055SJed Brown       PetscInt numBlocks, numBlocksPlus;
31429371c9d4SSatish Balay       const PetscInt A = 0, B = 1, C = 2, D = 3, E = 4, F = 5, G = 6, H = 7, II = 8, J = 9, K = 10, L = 11, M = 12, N = 13, O = 14, P = 15, Q = 16, R = 17, S = 18, T = 19, U = 20, V = 21, W = 22, X = 23, Y = 24, Z = 25, Ap = 26, Bp = 27, Cp = 28, Dp = 29, Ep = 30, Fp = 31, Gp = 32, Hp = 33, Ip = 34, Jp = 35, Kp = 36, Lp = 37, Mp = 38, Np = 39, Op = 40, Pp = 41, Qp = 42, Rp = 43, Sp = 44, Tp = 45, Up = 46, Vp = 47, Wp = 48, Xp = 49, Yp = 50, Zp = 51, Aq = 52, Bq = 53, Cq = 54, Dq = 55;
31439371c9d4SSatish Balay       const PetscInt pattern[64][4] = {
31449371c9d4SSatish Balay   /* face to vertex within the coarse discretization of a single gyroid block */
3145b7f5c055SJed Brown   /* layer 0 */
31469371c9d4SSatish Balay         {A,           C,           K,           G          },
31479371c9d4SSatish Balay         {C,           B,           II,          K          },
31489371c9d4SSatish Balay         {D,           A,           H,           L          },
31499371c9d4SSatish Balay         {B + 56 * 1,  D,           L,           J          },
31509371c9d4SSatish Balay         {E,           B + 56 * 1,  J,           N          },
31519371c9d4SSatish Balay         {A + 56 * 2,  E,           N,           H + 56 * 2 },
31529371c9d4SSatish Balay         {F,           A + 56 * 2,  G + 56 * 2,  M          },
31539371c9d4SSatish Balay         {B,           F,           M,           II         },
3154b7f5c055SJed Brown  /* layer 1 */
31559371c9d4SSatish Balay         {G,           K,           Q,           O          },
31569371c9d4SSatish Balay         {K,           II,          P,           Q          },
31579371c9d4SSatish Balay         {L,           H,           O + 56 * 1,  R          },
31589371c9d4SSatish Balay         {J,           L,           R,           P          },
31599371c9d4SSatish Balay         {N,           J,           P,           S          },
31609371c9d4SSatish Balay         {H + 56 * 2,  N,           S,           O + 56 * 3 },
31619371c9d4SSatish Balay         {M,           G + 56 * 2,  O + 56 * 2,  T          },
31629371c9d4SSatish Balay         {II,          M,           T,           P          },
3163b7f5c055SJed Brown  /* layer 2 */
31649371c9d4SSatish Balay         {O,           Q,           Y,           U          },
31659371c9d4SSatish Balay         {Q,           P,           W,           Y          },
31669371c9d4SSatish Balay         {R,           O + 56 * 1,  U + 56 * 1,  Ap         },
31679371c9d4SSatish Balay         {P,           R,           Ap,          W          },
31689371c9d4SSatish Balay         {S,           P,           X,           Bp         },
31699371c9d4SSatish Balay         {O + 56 * 3,  S,           Bp,          V + 56 * 1 },
31709371c9d4SSatish Balay         {T,           O + 56 * 2,  V,           Z          },
31719371c9d4SSatish Balay         {P,           T,           Z,           X          },
3172b7f5c055SJed Brown  /* layer 3 */
31739371c9d4SSatish Balay         {U,           Y,           Ep,          Dp         },
31749371c9d4SSatish Balay         {Y,           W,           Cp,          Ep         },
31759371c9d4SSatish Balay         {Ap,          U + 56 * 1,  Dp + 56 * 1, Gp         },
31769371c9d4SSatish Balay         {W,           Ap,          Gp,          Cp         },
31779371c9d4SSatish Balay         {Bp,          X,           Cp + 56 * 2, Fp         },
31789371c9d4SSatish Balay         {V + 56 * 1,  Bp,          Fp,          Dp + 56 * 1},
31799371c9d4SSatish Balay         {Z,           V,           Dp,          Hp         },
31809371c9d4SSatish Balay         {X,           Z,           Hp,          Cp + 56 * 2},
3181b7f5c055SJed Brown  /* layer 4 */
31829371c9d4SSatish Balay         {Dp,          Ep,          Mp,          Kp         },
31839371c9d4SSatish Balay         {Ep,          Cp,          Ip,          Mp         },
31849371c9d4SSatish Balay         {Gp,          Dp + 56 * 1, Lp,          Np         },
31859371c9d4SSatish Balay         {Cp,          Gp,          Np,          Jp         },
31869371c9d4SSatish Balay         {Fp,          Cp + 56 * 2, Jp + 56 * 2, Pp         },
31879371c9d4SSatish Balay         {Dp + 56 * 1, Fp,          Pp,          Lp         },
31889371c9d4SSatish Balay         {Hp,          Dp,          Kp,          Op         },
31899371c9d4SSatish Balay         {Cp + 56 * 2, Hp,          Op,          Ip + 56 * 2},
3190b7f5c055SJed Brown  /* layer 5 */
31919371c9d4SSatish Balay         {Kp,          Mp,          Sp,          Rp         },
31929371c9d4SSatish Balay         {Mp,          Ip,          Qp,          Sp         },
31939371c9d4SSatish Balay         {Np,          Lp,          Rp,          Tp         },
31949371c9d4SSatish Balay         {Jp,          Np,          Tp,          Qp + 56 * 1},
31959371c9d4SSatish Balay         {Pp,          Jp + 56 * 2, Qp + 56 * 3, Up         },
31969371c9d4SSatish Balay         {Lp,          Pp,          Up,          Rp         },
31979371c9d4SSatish Balay         {Op,          Kp,          Rp,          Vp         },
31989371c9d4SSatish Balay         {Ip + 56 * 2, Op,          Vp,          Qp + 56 * 2},
3199b7f5c055SJed Brown  /* layer 6 */
32009371c9d4SSatish Balay         {Rp,          Sp,          Aq,          Yp         },
32019371c9d4SSatish Balay         {Sp,          Qp,          Wp,          Aq         },
32029371c9d4SSatish Balay         {Tp,          Rp,          Yp,          Cq         },
32039371c9d4SSatish Balay         {Qp + 56 * 1, Tp,          Cq,          Wp + 56 * 1},
32049371c9d4SSatish Balay         {Up,          Qp + 56 * 3, Xp + 56 * 1, Dq         },
32059371c9d4SSatish Balay         {Rp,          Up,          Dq,          Zp         },
32069371c9d4SSatish Balay         {Vp,          Rp,          Zp,          Bq         },
32079371c9d4SSatish Balay         {Qp + 56 * 2, Vp,          Bq,          Xp         },
3208b7f5c055SJed Brown  /* layer 7 (the top is the periodic image of the bottom of layer 0) */
32099371c9d4SSatish Balay         {Yp,          Aq,          C + 56 * 4,  A + 56 * 4 },
32109371c9d4SSatish Balay         {Aq,          Wp,          B + 56 * 4,  C + 56 * 4 },
32119371c9d4SSatish Balay         {Cq,          Yp,          A + 56 * 4,  D + 56 * 4 },
32129371c9d4SSatish Balay         {Wp + 56 * 1, Cq,          D + 56 * 4,  B + 56 * 5 },
32139371c9d4SSatish Balay         {Dq,          Xp + 56 * 1, B + 56 * 5,  E + 56 * 4 },
32149371c9d4SSatish Balay         {Zp,          Dq,          E + 56 * 4,  A + 56 * 6 },
32159371c9d4SSatish Balay         {Bq,          Zp,          A + 56 * 6,  F + 56 * 4 },
32169371c9d4SSatish Balay         {Xp,          Bq,          F + 56 * 4,  B + 56 * 4 }
3217b7f5c055SJed Brown       };
3218b7f5c055SJed Brown       const PetscReal gamma                = PetscAcosReal((PetscSqrtReal(3.) - 1.) / PetscSqrtReal(2.)) / PETSC_PI;
32199371c9d4SSatish Balay       const PetscReal patternCoords[56][3] = {
3220bee3fc89SBarry Smith         {1.,        0.,        0.  }, /* A  */
3221bee3fc89SBarry Smith         {0.,        1.,        0.  }, /* B  */
3222bee3fc89SBarry Smith         {gamma,     gamma,     0.  }, /* C  */
3223bee3fc89SBarry Smith         {1 + gamma, 1 - gamma, 0.  }, /* D  */
3224bee3fc89SBarry Smith         {2 - gamma, 2 - gamma, 0.  }, /* E  */
3225bee3fc89SBarry Smith         {1 - gamma, 1 + gamma, 0.  }, /* F  */
3226b7f5c055SJed Brown 
3227bee3fc89SBarry Smith         {.5,        0,         .25 }, /* G  */
3228bee3fc89SBarry Smith         {1.5,       0.,        .25 }, /* H  */
3229bee3fc89SBarry Smith         {.5,        1.,        .25 }, /* II */
3230bee3fc89SBarry Smith         {1.5,       1.,        .25 }, /* J  */
3231bee3fc89SBarry Smith         {.25,       .5,        .25 }, /* K  */
3232bee3fc89SBarry Smith         {1.25,      .5,        .25 }, /* L  */
3233bee3fc89SBarry Smith         {.75,       1.5,       .25 }, /* M  */
3234bee3fc89SBarry Smith         {1.75,      1.5,       .25 }, /* N  */
3235b7f5c055SJed Brown 
3236bee3fc89SBarry Smith         {0.,        0.,        .5  }, /* O  */
3237bee3fc89SBarry Smith         {1.,        1.,        .5  }, /* P  */
3238bee3fc89SBarry Smith         {gamma,     1 - gamma, .5  }, /* Q  */
3239bee3fc89SBarry Smith         {1 + gamma, gamma,     .5  }, /* R  */
3240bee3fc89SBarry Smith         {2 - gamma, 1 + gamma, .5  }, /* S  */
3241bee3fc89SBarry Smith         {1 - gamma, 2 - gamma, .5  }, /* T  */
3242b7f5c055SJed Brown 
3243bee3fc89SBarry Smith         {0.,        .5,        .75 }, /* U  */
3244bee3fc89SBarry Smith         {0.,        1.5,       .75 }, /* V  */
3245bee3fc89SBarry Smith         {1.,        .5,        .75 }, /* W  */
3246bee3fc89SBarry Smith         {1.,        1.5,       .75 }, /* X  */
3247bee3fc89SBarry Smith         {.5,        .75,       .75 }, /* Y  */
3248bee3fc89SBarry Smith         {.5,        1.75,      .75 }, /* Z  */
3249bee3fc89SBarry Smith         {1.5,       .25,       .75 }, /* Ap */
3250bee3fc89SBarry Smith         {1.5,       1.25,      .75 }, /* Bp */
3251b7f5c055SJed Brown 
3252bee3fc89SBarry Smith         {1.,        0.,        1.  }, /* Cp */
3253bee3fc89SBarry Smith         {0.,        1.,        1.  }, /* Dp */
3254bee3fc89SBarry Smith         {1 - gamma, 1 - gamma, 1.  }, /* Ep */
3255bee3fc89SBarry Smith         {1 + gamma, 1 + gamma, 1.  }, /* Fp */
3256bee3fc89SBarry Smith         {2 - gamma, gamma,     1.  }, /* Gp */
3257bee3fc89SBarry Smith         {gamma,     2 - gamma, 1.  }, /* Hp */
3258b7f5c055SJed Brown 
3259bee3fc89SBarry Smith         {.5,        0.,        1.25}, /* Ip */
3260bee3fc89SBarry Smith         {1.5,       0.,        1.25}, /* Jp */
3261bee3fc89SBarry Smith         {.5,        1.,        1.25}, /* Kp */
3262bee3fc89SBarry Smith         {1.5,       1.,        1.25}, /* Lp */
3263bee3fc89SBarry Smith         {.75,       .5,        1.25}, /* Mp */
3264bee3fc89SBarry Smith         {1.75,      .5,        1.25}, /* Np */
3265bee3fc89SBarry Smith         {.25,       1.5,       1.25}, /* Op */
3266bee3fc89SBarry Smith         {1.25,      1.5,       1.25}, /* Pp */
3267b7f5c055SJed Brown 
3268bee3fc89SBarry Smith         {0.,        0.,        1.5 }, /* Qp */
3269bee3fc89SBarry Smith         {1.,        1.,        1.5 }, /* Rp */
3270bee3fc89SBarry Smith         {1 - gamma, gamma,     1.5 }, /* Sp */
3271bee3fc89SBarry Smith         {2 - gamma, 1 - gamma, 1.5 }, /* Tp */
3272bee3fc89SBarry Smith         {1 + gamma, 2 - gamma, 1.5 }, /* Up */
3273bee3fc89SBarry Smith         {gamma,     1 + gamma, 1.5 }, /* Vp */
3274b7f5c055SJed Brown 
3275bee3fc89SBarry Smith         {0.,        .5,        1.75}, /* Wp */
3276bee3fc89SBarry Smith         {0.,        1.5,       1.75}, /* Xp */
3277bee3fc89SBarry Smith         {1.,        .5,        1.75}, /* Yp */
3278bee3fc89SBarry Smith         {1.,        1.5,       1.75}, /* Zp */
3279bee3fc89SBarry Smith         {.5,        .25,       1.75}, /* Aq */
3280bee3fc89SBarry Smith         {.5,        1.25,      1.75}, /* Bq */
3281bee3fc89SBarry Smith         {1.5,       .75,       1.75}, /* Cq */
3282bee3fc89SBarry Smith         {1.5,       1.75,      1.75}, /* Dq */
3283b7f5c055SJed Brown       };
3284b7f5c055SJed Brown       PetscInt(*cells)[64][4] = NULL;
3285b7f5c055SJed Brown       PetscBool *seen;
3286b7f5c055SJed Brown       PetscInt  *vertToTrueVert;
3287b7f5c055SJed Brown       PetscInt   count;
3288b7f5c055SJed Brown 
3289b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) extentPlus[i] = extent[i] + 1;
3290b7f5c055SJed Brown       numBlocks = 1;
3291b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) numBlocks *= extent[i];
3292b7f5c055SJed Brown       numBlocksPlus = 1;
3293b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) numBlocksPlus *= extentPlus[i];
3294b7f5c055SJed Brown       numFaces = numBlocks * facesPerBlock;
32959566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numBlocks, &cells));
32969566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numBlocksPlus * vertsPerBlock, &seen));
3297b7f5c055SJed Brown       for (PetscInt k = 0; k < extent[2]; k++) {
3298b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3299b7f5c055SJed Brown           for (PetscInt i = 0; i < extent[0]; i++) {
3300b7f5c055SJed Brown             for (PetscInt f = 0; f < facesPerBlock; f++) {
3301b7f5c055SJed Brown               for (PetscInt v = 0; v < 4; v++) {
3302b7f5c055SJed Brown                 PetscInt vertRaw     = pattern[f][v];
3303b7f5c055SJed Brown                 PetscInt blockidx    = vertRaw / 56;
3304b7f5c055SJed Brown                 PetscInt patternvert = vertRaw % 56;
3305b7f5c055SJed Brown                 PetscInt xplus       = (blockidx & 1);
3306b7f5c055SJed Brown                 PetscInt yplus       = (blockidx & 2) >> 1;
3307b7f5c055SJed Brown                 PetscInt zplus       = (blockidx & 4) >> 2;
3308b7f5c055SJed Brown                 PetscInt zcoord      = (periodic && periodic[2] == DM_BOUNDARY_PERIODIC) ? ((k + zplus) % extent[2]) : (k + zplus);
3309b7f5c055SJed Brown                 PetscInt ycoord      = (periodic && periodic[1] == DM_BOUNDARY_PERIODIC) ? ((j + yplus) % extent[1]) : (j + yplus);
3310b7f5c055SJed Brown                 PetscInt xcoord      = (periodic && periodic[0] == DM_BOUNDARY_PERIODIC) ? ((i + xplus) % extent[0]) : (i + xplus);
3311b7f5c055SJed Brown                 PetscInt vert        = ((zcoord * extentPlus[1] + ycoord) * extentPlus[0] + xcoord) * 56 + patternvert;
3312b7f5c055SJed Brown 
3313b7f5c055SJed Brown                 cells[(k * extent[1] + j) * extent[0] + i][f][v] = vert;
3314b7f5c055SJed Brown                 seen[vert]                                       = PETSC_TRUE;
3315b7f5c055SJed Brown               }
3316b7f5c055SJed Brown             }
3317b7f5c055SJed Brown           }
3318b7f5c055SJed Brown         }
3319b7f5c055SJed Brown       }
33209371c9d4SSatish Balay       for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++)
33219371c9d4SSatish Balay         if (seen[i]) numVertices++;
3322b7f5c055SJed Brown       count = 0;
33239566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numBlocksPlus * vertsPerBlock, &vertToTrueVert));
33249566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numVertices * 3, &vtxCoords));
3325b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++) vertToTrueVert[i] = -1;
3326b7f5c055SJed Brown       for (PetscInt k = 0; k < extentPlus[2]; k++) {
3327b7f5c055SJed Brown         for (PetscInt j = 0; j < extentPlus[1]; j++) {
3328b7f5c055SJed Brown           for (PetscInt i = 0; i < extentPlus[0]; i++) {
3329b7f5c055SJed Brown             for (PetscInt v = 0; v < vertsPerBlock; v++) {
3330b7f5c055SJed Brown               PetscInt vIdx = ((k * extentPlus[1] + j) * extentPlus[0] + i) * vertsPerBlock + v;
3331b7f5c055SJed Brown 
3332b7f5c055SJed Brown               if (seen[vIdx]) {
3333b7f5c055SJed Brown                 PetscInt thisVert;
3334b7f5c055SJed Brown 
3335b7f5c055SJed Brown                 vertToTrueVert[vIdx] = thisVert = count++;
3336b7f5c055SJed Brown 
3337b7f5c055SJed Brown                 for (PetscInt d = 0; d < 3; d++) vtxCoords[3 * thisVert + d] = patternCoords[v][d];
3338b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 0] += i * 2;
3339b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 1] += j * 2;
3340b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 2] += k * 2;
3341b7f5c055SJed Brown               }
3342b7f5c055SJed Brown             }
3343b7f5c055SJed Brown           }
3344b7f5c055SJed Brown         }
3345b7f5c055SJed Brown       }
3346b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocks; i++) {
3347b7f5c055SJed Brown         for (PetscInt f = 0; f < facesPerBlock; f++) {
3348ad540459SPierre Jolivet           for (PetscInt v = 0; v < 4; v++) cells[i][f][v] = vertToTrueVert[cells[i][f][v]];
3349b7f5c055SJed Brown         }
3350b7f5c055SJed Brown       }
33519566063dSJacob Faibussowitsch       PetscCall(PetscFree(vertToTrueVert));
33529566063dSJacob Faibussowitsch       PetscCall(PetscFree(seen));
3353b7f5c055SJed Brown       cells_flat = cells[0][0];
3354b7f5c055SJed Brown       numEdges   = 0;
3355b7f5c055SJed Brown       for (PetscInt i = 0; i < numFaces; i++) {
3356b7f5c055SJed Brown         for (PetscInt e = 0; e < 4; e++) {
3357b7f5c055SJed Brown           PetscInt         ev[]       = {cells_flat[i * 4 + e], cells_flat[i * 4 + ((e + 1) % 4)]};
3358b7f5c055SJed Brown           const PetscReal *evCoords[] = {&vtxCoords[3 * ev[0]], &vtxCoords[3 * ev[1]]};
3359b7f5c055SJed Brown 
3360b7f5c055SJed Brown           for (PetscInt d = 0; d < 3; d++) {
3361b7f5c055SJed Brown             if (!periodic || periodic[0] != DM_BOUNDARY_PERIODIC) {
3362b7f5c055SJed Brown               if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) numEdges++;
3363b7f5c055SJed Brown               if (evCoords[0][d] == 2. * extent[d] && evCoords[1][d] == 2. * extent[d]) numEdges++;
3364b7f5c055SJed Brown             }
3365b7f5c055SJed Brown           }
3366b7f5c055SJed Brown         }
3367b7f5c055SJed Brown       }
33689566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numEdges, &edges));
33699566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numEdges, &edgeSets));
3370b7f5c055SJed Brown       for (PetscInt edge = 0, i = 0; i < numFaces; i++) {
3371b7f5c055SJed Brown         for (PetscInt e = 0; e < 4; e++) {
3372b7f5c055SJed Brown           PetscInt         ev[]       = {cells_flat[i * 4 + e], cells_flat[i * 4 + ((e + 1) % 4)]};
3373b7f5c055SJed Brown           const PetscReal *evCoords[] = {&vtxCoords[3 * ev[0]], &vtxCoords[3 * ev[1]]};
3374b7f5c055SJed Brown 
3375b7f5c055SJed Brown           for (PetscInt d = 0; d < 3; d++) {
3376b7f5c055SJed Brown             if (!periodic || periodic[d] != DM_BOUNDARY_PERIODIC) {
3377b7f5c055SJed Brown               if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) {
3378b7f5c055SJed Brown                 edges[edge][0]   = ev[0];
3379b7f5c055SJed Brown                 edges[edge][1]   = ev[1];
3380b7f5c055SJed Brown                 edgeSets[edge++] = 2 * d;
3381b7f5c055SJed Brown               }
3382b7f5c055SJed Brown               if (evCoords[0][d] == 2. * extent[d] && evCoords[1][d] == 2. * extent[d]) {
3383b7f5c055SJed Brown                 edges[edge][0]   = ev[0];
3384b7f5c055SJed Brown                 edges[edge][1]   = ev[1];
3385b7f5c055SJed Brown                 edgeSets[edge++] = 2 * d + 1;
3386b7f5c055SJed Brown               }
3387b7f5c055SJed Brown             }
3388b7f5c055SJed Brown           }
3389b7f5c055SJed Brown         }
3390b7f5c055SJed Brown       }
3391b7f5c055SJed Brown     }
3392b7f5c055SJed Brown     evalFunc   = TPSEvaluate_Gyroid;
33934663dae6SJed Brown     normalFunc = TPSExtrudeNormalFunc_Gyroid;
3394b7f5c055SJed Brown     break;
3395b7f5c055SJed Brown   }
3396b7f5c055SJed Brown 
33979566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, topoDim));
3398c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildFromCellList(dm, numFaces, numVertices, 4, cells_flat));
33999566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildFromCellList(dm, 0, 0, 0, NULL));
34009566063dSJacob Faibussowitsch   PetscCall(PetscFree(cells_flat));
3401b7f5c055SJed Brown   {
3402b7f5c055SJed Brown     DM idm;
34039566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(dm, &idm));
340469d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &idm));
3405b7f5c055SJed Brown   }
3406c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildCoordinatesFromCellList(dm, spaceDim, vtxCoords));
34079566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildCoordinatesFromCellList(dm, spaceDim, NULL));
34089566063dSJacob Faibussowitsch   PetscCall(PetscFree(vtxCoords));
3409b7f5c055SJed Brown 
34109566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
34119566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "Face Sets", &label));
3412b7f5c055SJed Brown   for (PetscInt e = 0; e < numEdges; e++) {
3413b7f5c055SJed Brown     PetscInt        njoin;
3414b7f5c055SJed Brown     const PetscInt *join, verts[] = {numFaces + edges[e][0], numFaces + edges[e][1]};
34159566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, 2, verts, &njoin, &join));
341663a3b9bcSJacob 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]);
34179566063dSJacob Faibussowitsch     PetscCall(DMLabelSetValue(label, join[0], edgeSets[e]));
34189566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, 2, verts, &njoin, &join));
3419b7f5c055SJed Brown   }
34209566063dSJacob Faibussowitsch   PetscCall(PetscFree(edges));
34219566063dSJacob Faibussowitsch   PetscCall(PetscFree(edgeSets));
34221436d7faSJed Brown   if (tps_distribute) {
34231436d7faSJed Brown     DM               pdm = NULL;
34241436d7faSJed Brown     PetscPartitioner part;
34251436d7faSJed Brown 
34269566063dSJacob Faibussowitsch     PetscCall(DMPlexGetPartitioner(dm, &part));
34279566063dSJacob Faibussowitsch     PetscCall(PetscPartitionerSetFromOptions(part));
34289566063dSJacob Faibussowitsch     PetscCall(DMPlexDistribute(dm, 0, NULL, &pdm));
342948a46eb9SPierre Jolivet     if (pdm) PetscCall(DMPlexReplace_Internal(dm, &pdm));
34301436d7faSJed Brown     // Do not auto-distribute again
34319566063dSJacob Faibussowitsch     PetscCall(DMPlexDistributeSetDefault(dm, PETSC_FALSE));
34321436d7faSJed Brown   }
3433b7f5c055SJed Brown 
34349566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
3435b7f5c055SJed Brown   for (PetscInt refine = 0; refine < refinements; refine++) {
3436b7f5c055SJed Brown     PetscInt     m;
3437b7f5c055SJed Brown     DM           dmf;
3438b7f5c055SJed Brown     Vec          X;
3439b7f5c055SJed Brown     PetscScalar *x;
34409566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, MPI_COMM_NULL, &dmf));
344169d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &dmf));
3442b7f5c055SJed Brown 
34439566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &X));
34449566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(X, &m));
34459566063dSJacob Faibussowitsch     PetscCall(VecGetArray(X, &x));
344648a46eb9SPierre Jolivet     for (PetscInt i = 0; i < m; i += 3) PetscCall(TPSNearestPoint(evalFunc, &x[i]));
34479566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(X, &x));
3448b7f5c055SJed Brown   }
3449b7f5c055SJed Brown 
3450b7f5c055SJed Brown   // Face Sets has already been propagated to new vertices during refinement; this propagates to the initial vertices.
34519566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "Face Sets", &label));
34529566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete(dm, label));
3453b7f5c055SJed Brown 
345446139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
345546139095SJed Brown 
3456b7f5c055SJed Brown   if (thickness > 0) {
34574663dae6SJed Brown     DM              edm, cdm, ecdm;
34584663dae6SJed Brown     DMPlexTransform tr;
34594663dae6SJed Brown     const char     *prefix;
34604663dae6SJed Brown     PetscOptions    options;
34614663dae6SJed Brown     // Code from DMPlexExtrude
34624663dae6SJed Brown     PetscCall(DMPlexTransformCreate(PetscObjectComm((PetscObject)dm), &tr));
34634663dae6SJed Brown     PetscCall(DMPlexTransformSetDM(tr, dm));
34644663dae6SJed Brown     PetscCall(DMPlexTransformSetType(tr, DMPLEXEXTRUDE));
34654663dae6SJed Brown     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, &prefix));
34664663dae6SJed Brown     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)tr, prefix));
34674663dae6SJed Brown     PetscCall(PetscObjectGetOptions((PetscObject)dm, &options));
34684663dae6SJed Brown     PetscCall(PetscObjectSetOptions((PetscObject)tr, options));
34694663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetLayers(tr, layers));
34704663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetThickness(tr, thickness));
34714663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetTensor(tr, PETSC_FALSE));
34724663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetSymmetric(tr, PETSC_TRUE));
34734663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetNormalFunction(tr, normalFunc));
34744663dae6SJed Brown     PetscCall(DMPlexTransformSetFromOptions(tr));
34754663dae6SJed Brown     PetscCall(PetscObjectSetOptions((PetscObject)tr, NULL));
34764663dae6SJed Brown     PetscCall(DMPlexTransformSetUp(tr));
34774663dae6SJed Brown     PetscCall(PetscObjectViewFromOptions((PetscObject)tr, NULL, "-dm_plex_tps_transform_view"));
34784663dae6SJed Brown     PetscCall(DMPlexTransformApply(tr, dm, &edm));
34794663dae6SJed Brown     PetscCall(DMCopyDisc(dm, edm));
34804663dae6SJed Brown     PetscCall(DMGetCoordinateDM(dm, &cdm));
34814663dae6SJed Brown     PetscCall(DMGetCoordinateDM(edm, &ecdm));
34824663dae6SJed Brown     PetscCall(DMCopyDisc(cdm, ecdm));
34834663dae6SJed Brown     PetscCall(DMPlexTransformCreateDiscLabels(tr, edm));
34844663dae6SJed Brown     PetscCall(DMPlexTransformDestroy(&tr));
34854663dae6SJed Brown     if (edm) {
34864663dae6SJed Brown       ((DM_Plex *)edm->data)->printFEM    = ((DM_Plex *)dm->data)->printFEM;
34874663dae6SJed Brown       ((DM_Plex *)edm->data)->printL2     = ((DM_Plex *)dm->data)->printL2;
3488f5867de0SMatthew G. Knepley       ((DM_Plex *)edm->data)->printLocate = ((DM_Plex *)dm->data)->printLocate;
34894663dae6SJed Brown     }
349069d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &edm));
3491b7f5c055SJed Brown   }
34923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3493b7f5c055SJed Brown }
3494b7f5c055SJed Brown 
3495b7f5c055SJed Brown /*@
3496b7f5c055SJed Brown   DMPlexCreateTPSMesh - Create a distributed, interpolated mesh of a triply-periodic surface
3497b7f5c055SJed Brown 
3498b7f5c055SJed Brown   Collective
3499b7f5c055SJed Brown 
3500b7f5c055SJed Brown   Input Parameters:
3501a1cb98faSBarry Smith + comm           - The communicator for the `DM` object
3502b7f5c055SJed Brown . tpstype        - Type of triply-periodic surface
3503b7f5c055SJed Brown . extent         - Array of length 3 containing number of periods in each direction
350420f4b53cSBarry Smith . periodic       - array of length 3 with periodicity, or `NULL` for non-periodic
35051436d7faSJed Brown . tps_distribute - Distribute 2D manifold mesh prior to refinement and extrusion (more scalable)
3506817da375SSatish Balay . refinements    - Number of factor-of-2 refinements of 2D manifold mesh
35071436d7faSJed Brown . layers         - Number of cell layers extruded in normal direction
3508817da375SSatish Balay - thickness      - Thickness in normal direction
3509b7f5c055SJed Brown 
3510b7f5c055SJed Brown   Output Parameter:
3511a1cb98faSBarry Smith . dm - The `DM` object
3512a1cb98faSBarry Smith 
3513a1cb98faSBarry Smith   Level: beginner
3514b7f5c055SJed Brown 
3515b7f5c055SJed Brown   Notes:
3516b7f5c055SJed 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.
3517b7f5c055SJed Brown   https://en.wikipedia.org/wiki/Schwarz_minimal_surface#Schwarz_P_(%22Primitive%22) and can be cut with "clean" boundaries.
3518b7f5c055SJed 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.
3519b7f5c055SJed Brown   Our implementation creates a very coarse mesh of the surface and refines (by 4-way splitting) as many times as requested.
3520b7f5c055SJed Brown   On each refinement, all vertices are projected to their nearest point on the surface.
3521b7f5c055SJed Brown   This projection could readily be extended to related surfaces.
3522b7f5c055SJed Brown 
3523b7f5c055SJed Brown   The face (edge) sets for the Schwarz P surface are numbered 1(-x), 2(+x), 3(-y), 4(+y), 5(-z), 6(+z).
352420f4b53cSBarry Smith   When the mesh is refined, "Face Sets" contain the new vertices (created during refinement).  Use `DMPlexLabelComplete()` to propagate to coarse-level vertices.
3525b7f5c055SJed Brown 
352660225df5SJacob Faibussowitsch   Developer Notes:
3527b7f5c055SJed Brown   The Gyroid mesh does not currently mark boundary sets.
3528b7f5c055SJed Brown 
3529a1cb98faSBarry Smith   References:
3530a1cb98faSBarry Smith . * - Maskery et al, Insights into the mechanical properties of several triply periodic minimal surface lattice structures made by polymer additive manufacturing, 2017.
3531a1cb98faSBarry Smith   https://doi.org/10.1016/j.polymer.2017.11.049
3532b7f5c055SJed Brown 
35331cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSphereMesh()`, `DMSetType()`, `DMCreate()`
3534b7f5c055SJed Brown @*/
3535d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateTPSMesh(MPI_Comm comm, DMPlexTPSType tpstype, const PetscInt extent[], const DMBoundaryType periodic[], PetscBool tps_distribute, PetscInt refinements, PetscInt layers, PetscReal thickness, DM *dm)
3536d71ae5a4SJacob Faibussowitsch {
3537b7f5c055SJed Brown   PetscFunctionBegin;
35389566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
35399566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
35409566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateTPSMesh_Internal(*dm, tpstype, extent, periodic, tps_distribute, refinements, layers, thickness));
35413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3542b7f5c055SJed Brown }
3543b7f5c055SJed Brown 
35449318fe57SMatthew G. Knepley /*@
35459318fe57SMatthew G. Knepley   DMPlexCreateSphereMesh - Creates a mesh on the d-dimensional sphere, S^d.
35469318fe57SMatthew G. Knepley 
35479318fe57SMatthew G. Knepley   Collective
35489318fe57SMatthew G. Knepley 
35499318fe57SMatthew G. Knepley   Input Parameters:
3550a1cb98faSBarry Smith + comm    - The communicator for the `DM` object
35519318fe57SMatthew G. Knepley . dim     - The dimension
35529318fe57SMatthew G. Knepley . simplex - Use simplices, or tensor product cells
35539318fe57SMatthew G. Knepley - R       - The radius
35549318fe57SMatthew G. Knepley 
35559318fe57SMatthew G. Knepley   Output Parameter:
3556a1cb98faSBarry Smith . dm - The `DM` object
35579318fe57SMatthew G. Knepley 
35589318fe57SMatthew G. Knepley   Level: beginner
35599318fe57SMatthew G. Knepley 
35601cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBallMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
35619318fe57SMatthew G. Knepley @*/
3562d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateSphereMesh(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscReal R, DM *dm)
3563d71ae5a4SJacob Faibussowitsch {
35649318fe57SMatthew G. Knepley   PetscFunctionBegin;
35654f572ea9SToby Isaac   PetscAssertPointer(dm, 5);
35669566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
35679566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
35689566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSphereMesh_Internal(*dm, dim, simplex, R));
35693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35709318fe57SMatthew G. Knepley }
35719318fe57SMatthew G. Knepley 
3572d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBallMesh_Internal(DM dm, PetscInt dim, PetscReal R)
3573d71ae5a4SJacob Faibussowitsch {
35749318fe57SMatthew G. Knepley   DM      sdm, vol;
35759318fe57SMatthew G. Knepley   DMLabel bdlabel;
35769318fe57SMatthew G. Knepley 
35779318fe57SMatthew G. Knepley   PetscFunctionBegin;
35789566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &sdm));
35799566063dSJacob Faibussowitsch   PetscCall(DMSetType(sdm, DMPLEX));
35809566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)sdm, "bd_"));
35819566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSphereMesh_Internal(sdm, dim - 1, PETSC_TRUE, R));
35829566063dSJacob Faibussowitsch   PetscCall(DMSetFromOptions(sdm));
35839566063dSJacob Faibussowitsch   PetscCall(DMViewFromOptions(sdm, NULL, "-dm_view"));
35849566063dSJacob Faibussowitsch   PetscCall(DMPlexGenerate(sdm, NULL, PETSC_TRUE, &vol));
35859566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&sdm));
358669d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &vol));
35879566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
35889566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "marker", &bdlabel));
35899566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkBoundaryFaces(dm, PETSC_DETERMINE, bdlabel));
35909566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete(dm, bdlabel));
35913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
359251a74b61SMatthew G. Knepley }
359351a74b61SMatthew G. Knepley 
359451a74b61SMatthew G. Knepley /*@
359551a74b61SMatthew G. Knepley   DMPlexCreateBallMesh - Creates a simplex mesh on the d-dimensional ball, B^d.
359651a74b61SMatthew G. Knepley 
359751a74b61SMatthew G. Knepley   Collective
359851a74b61SMatthew G. Knepley 
359951a74b61SMatthew G. Knepley   Input Parameters:
3600a1cb98faSBarry Smith + comm - The communicator for the `DM` object
360151a74b61SMatthew G. Knepley . dim  - The dimension
360251a74b61SMatthew G. Knepley - R    - The radius
360351a74b61SMatthew G. Knepley 
360451a74b61SMatthew G. Knepley   Output Parameter:
3605a1cb98faSBarry Smith . dm - The `DM` object
360651a74b61SMatthew G. Knepley 
3607a1cb98faSBarry Smith   Options Database Key:
360860225df5SJacob Faibussowitsch . bd_dm_refine - This will refine the surface mesh preserving the sphere geometry
360951a74b61SMatthew G. Knepley 
361051a74b61SMatthew G. Knepley   Level: beginner
361151a74b61SMatthew G. Knepley 
36121cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSphereMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
361351a74b61SMatthew G. Knepley @*/
3614d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateBallMesh(MPI_Comm comm, PetscInt dim, PetscReal R, DM *dm)
3615d71ae5a4SJacob Faibussowitsch {
361651a74b61SMatthew G. Knepley   PetscFunctionBegin;
36179566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
36189566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
36199566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBallMesh_Internal(*dm, dim, R));
36203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36212829fed8SMatthew G. Knepley }
36222829fed8SMatthew G. Knepley 
3623d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateReferenceCell_Internal(DM rdm, DMPolytopeType ct)
3624d71ae5a4SJacob Faibussowitsch {
36250a6ba040SMatthew G. Knepley   PetscFunctionBegin;
36269318fe57SMatthew G. Knepley   switch (ct) {
36279371c9d4SSatish Balay   case DM_POLYTOPE_POINT: {
36289318fe57SMatthew G. Knepley     PetscInt    numPoints[1]        = {1};
36299318fe57SMatthew G. Knepley     PetscInt    coneSize[1]         = {0};
36309318fe57SMatthew G. Knepley     PetscInt    cones[1]            = {0};
36319318fe57SMatthew G. Knepley     PetscInt    coneOrientations[1] = {0};
36329318fe57SMatthew G. Knepley     PetscScalar vertexCoords[1]     = {0.0};
36339318fe57SMatthew G. Knepley 
36349566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 0));
36359566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 0, numPoints, coneSize, cones, coneOrientations, vertexCoords));
36369371c9d4SSatish Balay   } break;
36379371c9d4SSatish Balay   case DM_POLYTOPE_SEGMENT: {
36389318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {2, 1};
36399318fe57SMatthew G. Knepley     PetscInt    coneSize[3]         = {2, 0, 0};
36409318fe57SMatthew G. Knepley     PetscInt    cones[2]            = {1, 2};
36419318fe57SMatthew G. Knepley     PetscInt    coneOrientations[2] = {0, 0};
36429318fe57SMatthew G. Knepley     PetscScalar vertexCoords[2]     = {-1.0, 1.0};
36439318fe57SMatthew G. Knepley 
36449566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 1));
36459566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
36469371c9d4SSatish Balay   } break;
36479371c9d4SSatish Balay   case DM_POLYTOPE_POINT_PRISM_TENSOR: {
3648b5a892a1SMatthew G. Knepley     PetscInt    numPoints[2]        = {2, 1};
3649b5a892a1SMatthew G. Knepley     PetscInt    coneSize[3]         = {2, 0, 0};
3650b5a892a1SMatthew G. Knepley     PetscInt    cones[2]            = {1, 2};
3651b5a892a1SMatthew G. Knepley     PetscInt    coneOrientations[2] = {0, 0};
3652b5a892a1SMatthew G. Knepley     PetscScalar vertexCoords[2]     = {-1.0, 1.0};
3653b5a892a1SMatthew G. Knepley 
36549566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 1));
36559566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
36569371c9d4SSatish Balay   } break;
36579371c9d4SSatish Balay   case DM_POLYTOPE_TRIANGLE: {
36589318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {3, 1};
36599318fe57SMatthew G. Knepley     PetscInt    coneSize[4]         = {3, 0, 0, 0};
36609318fe57SMatthew G. Knepley     PetscInt    cones[3]            = {1, 2, 3};
36619318fe57SMatthew G. Knepley     PetscInt    coneOrientations[3] = {0, 0, 0};
36629318fe57SMatthew G. Knepley     PetscScalar vertexCoords[6]     = {-1.0, -1.0, 1.0, -1.0, -1.0, 1.0};
36639318fe57SMatthew G. Knepley 
36649566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 2));
36659566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
36669371c9d4SSatish Balay   } break;
36679371c9d4SSatish Balay   case DM_POLYTOPE_QUADRILATERAL: {
36689318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {4, 1};
36699318fe57SMatthew G. Knepley     PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
36709318fe57SMatthew G. Knepley     PetscInt    cones[4]            = {1, 2, 3, 4};
36719318fe57SMatthew G. Knepley     PetscInt    coneOrientations[4] = {0, 0, 0, 0};
36729318fe57SMatthew G. Knepley     PetscScalar vertexCoords[8]     = {-1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0};
36739318fe57SMatthew G. Knepley 
36749566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 2));
36759566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
36769371c9d4SSatish Balay   } break;
36779371c9d4SSatish Balay   case DM_POLYTOPE_SEG_PRISM_TENSOR: {
36789318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {4, 1};
36799318fe57SMatthew G. Knepley     PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
36809318fe57SMatthew G. Knepley     PetscInt    cones[4]            = {1, 2, 3, 4};
36819318fe57SMatthew G. Knepley     PetscInt    coneOrientations[4] = {0, 0, 0, 0};
36829318fe57SMatthew G. Knepley     PetscScalar vertexCoords[8]     = {-1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0};
36839318fe57SMatthew G. Knepley 
36849566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 2));
36859566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
36869371c9d4SSatish Balay   } break;
36879371c9d4SSatish Balay   case DM_POLYTOPE_TETRAHEDRON: {
36889318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {4, 1};
36899318fe57SMatthew G. Knepley     PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
3690f0edb160SMatthew G. Knepley     PetscInt    cones[4]            = {1, 2, 3, 4};
36919318fe57SMatthew G. Knepley     PetscInt    coneOrientations[4] = {0, 0, 0, 0};
3692f0edb160SMatthew 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};
36939318fe57SMatthew G. Knepley 
36949566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
36959566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
36969371c9d4SSatish Balay   } break;
36979371c9d4SSatish Balay   case DM_POLYTOPE_HEXAHEDRON: {
36989318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {8, 1};
36999318fe57SMatthew G. Knepley     PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
3700f0edb160SMatthew G. Knepley     PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
37019318fe57SMatthew G. Knepley     PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
37029371c9d4SSatish Balay     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, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0};
37039318fe57SMatthew G. Knepley 
37049566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
37059566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37069371c9d4SSatish Balay   } break;
37079371c9d4SSatish Balay   case DM_POLYTOPE_TRI_PRISM: {
37089318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {6, 1};
37099318fe57SMatthew G. Knepley     PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
3710f0edb160SMatthew G. Knepley     PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
37119318fe57SMatthew G. Knepley     PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
37129371c9d4SSatish Balay     PetscScalar vertexCoords[18]    = {-1.0, -1.0, -1.0, -1.0, 1.0, -1.0, 1.0, -1.0, -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, 1.0, 1.0};
37139318fe57SMatthew G. Knepley 
37149566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
37159566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37169371c9d4SSatish Balay   } break;
37179371c9d4SSatish Balay   case DM_POLYTOPE_TRI_PRISM_TENSOR: {
37189318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {6, 1};
37199318fe57SMatthew G. Knepley     PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
37209318fe57SMatthew G. Knepley     PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
37219318fe57SMatthew G. Knepley     PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
37229371c9d4SSatish Balay     PetscScalar vertexCoords[18]    = {-1.0, -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, -1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, -1.0, 1.0, 1.0};
37239318fe57SMatthew G. Knepley 
37249566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
37259566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37269371c9d4SSatish Balay   } break;
37279371c9d4SSatish Balay   case DM_POLYTOPE_QUAD_PRISM_TENSOR: {
37289318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {8, 1};
37299318fe57SMatthew G. Knepley     PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
37309318fe57SMatthew G. Knepley     PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
37319318fe57SMatthew G. Knepley     PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
37329371c9d4SSatish Balay     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, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0, 1.0, 1.0, -1.0, 1.0, 1.0};
37339318fe57SMatthew G. Knepley 
37349566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
37359566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37369371c9d4SSatish Balay   } break;
37379371c9d4SSatish Balay   case DM_POLYTOPE_PYRAMID: {
37389318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {5, 1};
37399318fe57SMatthew G. Knepley     PetscInt    coneSize[6]         = {5, 0, 0, 0, 0, 0};
3740f0edb160SMatthew G. Knepley     PetscInt    cones[5]            = {1, 2, 3, 4, 5};
37419318fe57SMatthew G. Knepley     PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
37429371c9d4SSatish Balay     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, 0.0, 0.0, 1.0};
37439318fe57SMatthew G. Knepley 
37449566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
37459566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37469371c9d4SSatish Balay   } break;
3747d71ae5a4SJacob Faibussowitsch   default:
3748d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)rdm), PETSC_ERR_ARG_WRONG, "Cannot create reference cell for cell type %s", DMPolytopeTypes[ct]);
37499318fe57SMatthew G. Knepley   }
37509318fe57SMatthew G. Knepley   {
37519318fe57SMatthew G. Knepley     PetscInt Nv, v;
37529318fe57SMatthew G. Knepley 
37539318fe57SMatthew G. Knepley     /* Must create the celltype label here so that we do not automatically try to compute the types */
37549566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(rdm, "celltype"));
37559566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCellType(rdm, 0, ct));
37569566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(rdm, NULL, &Nv));
37579566063dSJacob Faibussowitsch     for (v = 1; v < Nv; ++v) PetscCall(DMPlexSetCellType(rdm, v, DM_POLYTOPE_POINT));
37589318fe57SMatthew G. Knepley   }
37599566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolateInPlace_Internal(rdm));
37609566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)rdm, DMPolytopeTypes[ct]));
37613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
37620a6ba040SMatthew G. Knepley }
37630a6ba040SMatthew G. Knepley 
37649318fe57SMatthew G. Knepley /*@
3765a1cb98faSBarry Smith   DMPlexCreateReferenceCell - Create a `DMPLEX` with the appropriate FEM reference cell
37669318fe57SMatthew G. Knepley 
37679318fe57SMatthew G. Knepley   Collective
37689318fe57SMatthew G. Knepley 
37699318fe57SMatthew G. Knepley   Input Parameters:
37709318fe57SMatthew G. Knepley + comm - The communicator
37719318fe57SMatthew G. Knepley - ct   - The cell type of the reference cell
37729318fe57SMatthew G. Knepley 
37739318fe57SMatthew G. Knepley   Output Parameter:
37749318fe57SMatthew G. Knepley . refdm - The reference cell
37759318fe57SMatthew G. Knepley 
37769318fe57SMatthew G. Knepley   Level: intermediate
37779318fe57SMatthew G. Knepley 
377842747ad1SJacob Faibussowitsch .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBoxMesh()`
37799318fe57SMatthew G. Knepley @*/
3780d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateReferenceCell(MPI_Comm comm, DMPolytopeType ct, DM *refdm)
3781d71ae5a4SJacob Faibussowitsch {
37820a6ba040SMatthew G. Knepley   PetscFunctionBegin;
37839566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, refdm));
37849566063dSJacob Faibussowitsch   PetscCall(DMSetType(*refdm, DMPLEX));
37859566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateReferenceCell_Internal(*refdm, ct));
37863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
37879318fe57SMatthew G. Knepley }
378879a015ccSMatthew G. Knepley 
3789d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoundaryLabel_Private(DM dm, const char name[])
3790d71ae5a4SJacob Faibussowitsch {
37919318fe57SMatthew G. Knepley   DM        plex;
37929318fe57SMatthew G. Knepley   DMLabel   label;
37939318fe57SMatthew G. Knepley   PetscBool hasLabel;
37940a6ba040SMatthew G. Knepley 
3795c22d3578SMatthew G. Knepley   PetscFunctionBegin;
37969566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &hasLabel));
37973ba16761SJacob Faibussowitsch   if (hasLabel) PetscFunctionReturn(PETSC_SUCCESS);
37989566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, name));
37999566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
38009566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
38019566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkBoundaryFaces(plex, 1, label));
38021c8afea9SMatthew G. Knepley   PetscCall(DMPlexLabelComplete(plex, label));
38039566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plex));
38043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
38059318fe57SMatthew G. Knepley }
3806acdc6f61SToby Isaac 
3807669647acSMatthew G. Knepley /*
3808669647acSMatthew G. Knepley   We use the last coordinate as the radius, the inner radius is lower[dim-1] and the outer radius is upper[dim-1]. Then we map the first coordinate around the circle.
3809669647acSMatthew G. Knepley 
3810669647acSMatthew G. Knepley     (x, y) -> (r, theta) = (x[1], (x[0] - lower[0]) * 2\pi/(upper[0] - lower[0]))
3811669647acSMatthew G. Knepley */
3812d71ae5a4SJacob Faibussowitsch static void boxToAnnulus(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
3813d71ae5a4SJacob Faibussowitsch {
3814669647acSMatthew G. Knepley   const PetscReal low = PetscRealPart(constants[0]);
3815669647acSMatthew G. Knepley   const PetscReal upp = PetscRealPart(constants[1]);
3816669647acSMatthew G. Knepley   const PetscReal r   = PetscRealPart(u[1]);
3817669647acSMatthew G. Knepley   const PetscReal th  = 2. * PETSC_PI * (PetscRealPart(u[0]) - low) / (upp - low);
3818669647acSMatthew G. Knepley 
3819669647acSMatthew G. Knepley   f0[0] = r * PetscCosReal(th);
3820669647acSMatthew G. Knepley   f0[1] = r * PetscSinReal(th);
3821669647acSMatthew G. Knepley }
3822669647acSMatthew G. Knepley 
38235dca41c3SJed Brown const char *const DMPlexShapes[] = {"box", "box_surface", "ball", "sphere", "cylinder", "schwarz_p", "gyroid", "doublet", "annulus", "hypercubic", "zbox", "unknown", "DMPlexShape", "DM_SHAPE_", NULL};
38249318fe57SMatthew G. Knepley 
3825d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateFromOptions_Internal(PetscOptionItems *PetscOptionsObject, PetscBool *useCoordSpace, DM dm)
3826d71ae5a4SJacob Faibussowitsch {
38279318fe57SMatthew G. Knepley   DMPlexShape    shape   = DM_SHAPE_BOX;
38289318fe57SMatthew G. Knepley   DMPolytopeType cell    = DM_POLYTOPE_TRIANGLE;
38299318fe57SMatthew G. Knepley   PetscInt       dim     = 2;
38309318fe57SMatthew G. Knepley   PetscBool      simplex = PETSC_TRUE, interpolate = PETSC_TRUE, adjCone = PETSC_FALSE, adjClosure = PETSC_TRUE, refDomain = PETSC_FALSE;
3831cd7e8a5eSksagiyam   PetscBool      flg, flg2, fflg, bdfflg, nameflg;
38329318fe57SMatthew G. Knepley   MPI_Comm       comm;
3833ed5e4e85SVaclav Hapla   char           filename[PETSC_MAX_PATH_LEN]   = "<unspecified>";
3834ed5e4e85SVaclav Hapla   char           bdFilename[PETSC_MAX_PATH_LEN] = "<unspecified>";
3835ed5e4e85SVaclav Hapla   char           plexname[PETSC_MAX_PATH_LEN]   = "";
38369318fe57SMatthew G. Knepley 
38379318fe57SMatthew G. Knepley   PetscFunctionBegin;
3838708be2fdSJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_CreateFromOptions, dm, 0, 0, 0));
38399566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
38409318fe57SMatthew G. Knepley   /* TODO Turn this into a registration interface */
38419566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_filename", "File containing a mesh", "DMPlexCreateFromFile", filename, filename, sizeof(filename), &fflg));
38429566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_boundary_filename", "File containing a mesh boundary", "DMPlexCreateFromFile", bdFilename, bdFilename, sizeof(bdFilename), &bdfflg));
38439566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_name", "Name of the mesh in the file", "DMPlexCreateFromFile", plexname, plexname, sizeof(plexname), &nameflg));
38449566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_plex_cell", "Cell shape", "", DMPolytopeTypes, (PetscEnum)cell, (PetscEnum *)&cell, NULL));
38459566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_reference_cell_domain", "Use a reference cell domain", "", refDomain, &refDomain, NULL));
38469566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_plex_shape", "Shape for built-in mesh", "", DMPlexShapes, (PetscEnum)shape, (PetscEnum *)&shape, &flg));
38479566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_dim", "Topological dimension of the mesh", "DMGetDimension", dim, &dim, &flg, 0));
3848cfb853baSMatthew G. Knepley   PetscCheck(dim >= 0, comm, PETSC_ERR_ARG_OUTOFRANGE, "Dimension %" PetscInt_FMT " should be in [0, infinity)", dim);
38499566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_simplex", "Mesh cell shape", "", simplex, &simplex, &flg));
38509566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_interpolate", "Flag to create edges and faces automatically", "", interpolate, &interpolate, &flg));
38519566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_adj_cone", "Set adjacency direction", "DMSetBasicAdjacency", adjCone, &adjCone, &flg));
38529566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_adj_closure", "Set adjacency size", "DMSetBasicAdjacency", adjClosure, &adjClosure, &flg2));
38539566063dSJacob Faibussowitsch   if (flg || flg2) PetscCall(DMSetBasicAdjacency(dm, adjCone, adjClosure));
38549318fe57SMatthew G. Knepley 
385561a622f3SMatthew G. Knepley   switch (cell) {
385661a622f3SMatthew G. Knepley   case DM_POLYTOPE_POINT:
385761a622f3SMatthew G. Knepley   case DM_POLYTOPE_SEGMENT:
385861a622f3SMatthew G. Knepley   case DM_POLYTOPE_POINT_PRISM_TENSOR:
385961a622f3SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
386061a622f3SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
386161a622f3SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
3862d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_HEXAHEDRON:
3863d71ae5a4SJacob Faibussowitsch     *useCoordSpace = PETSC_TRUE;
3864d71ae5a4SJacob Faibussowitsch     break;
3865d71ae5a4SJacob Faibussowitsch   default:
3866d71ae5a4SJacob Faibussowitsch     *useCoordSpace = PETSC_FALSE;
3867d71ae5a4SJacob Faibussowitsch     break;
386861a622f3SMatthew G. Knepley   }
386961a622f3SMatthew G. Knepley 
38709318fe57SMatthew G. Knepley   if (fflg) {
38719318fe57SMatthew G. Knepley     DM dmnew;
38729318fe57SMatthew G. Knepley 
38739566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), filename, plexname, interpolate, &dmnew));
38745de52c6dSVaclav Hapla     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
387569d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &dmnew));
38769318fe57SMatthew G. Knepley   } else if (refDomain) {
38779566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateReferenceCell_Internal(dm, cell));
38789318fe57SMatthew G. Knepley   } else if (bdfflg) {
38799318fe57SMatthew G. Knepley     DM bdm, dmnew;
38809318fe57SMatthew G. Knepley 
38819566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), bdFilename, plexname, interpolate, &bdm));
38829566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)bdm, "bd_"));
38839566063dSJacob Faibussowitsch     PetscCall(DMSetFromOptions(bdm));
38849566063dSJacob Faibussowitsch     PetscCall(DMPlexGenerate(bdm, NULL, interpolate, &dmnew));
38859566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&bdm));
38865de52c6dSVaclav Hapla     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
388769d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &dmnew));
38889318fe57SMatthew G. Knepley   } else {
38899566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)dm, DMPlexShapes[shape]));
38909318fe57SMatthew G. Knepley     switch (shape) {
3891669647acSMatthew G. Knepley     case DM_SHAPE_BOX:
38925dca41c3SJed Brown     case DM_SHAPE_ZBOX:
3893669647acSMatthew G. Knepley     case DM_SHAPE_ANNULUS: {
38949318fe57SMatthew G. Knepley       PetscInt       faces[3]  = {0, 0, 0};
38959318fe57SMatthew G. Knepley       PetscReal      lower[3]  = {0, 0, 0};
38969318fe57SMatthew G. Knepley       PetscReal      upper[3]  = {1, 1, 1};
38979318fe57SMatthew G. Knepley       DMBoundaryType bdt[3]    = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
3898669647acSMatthew G. Knepley       PetscBool      isAnnular = shape == DM_SHAPE_ANNULUS ? PETSC_TRUE : PETSC_FALSE;
38999318fe57SMatthew G. Knepley       PetscInt       i, n;
39009318fe57SMatthew G. Knepley 
39019318fe57SMatthew G. Knepley       n = dim;
39029318fe57SMatthew G. Knepley       for (i = 0; i < dim; ++i) faces[i] = (dim == 1 ? 1 : 4 - dim);
39039566063dSJacob Faibussowitsch       PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg));
39049318fe57SMatthew G. Knepley       n = 3;
39059566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
390663a3b9bcSJacob Faibussowitsch       PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Lower box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
39079318fe57SMatthew G. Knepley       n = 3;
39089566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
390963a3b9bcSJacob Faibussowitsch       PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Upper box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
39109318fe57SMatthew G. Knepley       n = 3;
39119566063dSJacob Faibussowitsch       PetscCall(PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *)bdt, &n, &flg));
391263a3b9bcSJacob Faibussowitsch       PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Box boundary types had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
3913669647acSMatthew G. Knepley 
3914669647acSMatthew G. Knepley       PetscCheck(!isAnnular || dim == 2, comm, PETSC_ERR_ARG_OUTOFRANGE, "Only two dimensional annuli have been implemented");
3915669647acSMatthew G. Knepley       if (isAnnular)
3916669647acSMatthew G. Knepley         for (i = 0; i < dim - 1; ++i) bdt[i] = DM_BOUNDARY_PERIODIC;
3917669647acSMatthew G. Knepley 
39189318fe57SMatthew G. Knepley       switch (cell) {
391961a622f3SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
39209566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateWedgeBoxMesh_Internal(dm, faces, lower, upper, bdt));
3921d410b0cfSMatthew G. Knepley         if (!interpolate) {
3922d410b0cfSMatthew G. Knepley           DM udm;
3923d410b0cfSMatthew G. Knepley 
39249566063dSJacob Faibussowitsch           PetscCall(DMPlexUninterpolate(dm, &udm));
392569d8a87bSksagiyam           PetscCall(DMPlexReplace_Internal(dm, &udm));
3926d410b0cfSMatthew G. Knepley         }
39279318fe57SMatthew G. Knepley         break;
3928d71ae5a4SJacob Faibussowitsch       default:
39295dca41c3SJed Brown         PetscCall(DMPlexCreateBoxMesh_Internal(dm, shape, dim, simplex, faces, lower, upper, bdt, interpolate));
3930d71ae5a4SJacob Faibussowitsch         break;
39319318fe57SMatthew G. Knepley       }
3932669647acSMatthew G. Knepley       if (isAnnular) {
3933669647acSMatthew G. Knepley         DM          cdm;
3934669647acSMatthew G. Knepley         PetscDS     cds;
3935669647acSMatthew G. Knepley         PetscScalar bounds[2] = {lower[0], upper[0]};
3936669647acSMatthew G. Knepley 
3937669647acSMatthew G. Knepley         // Fix coordinates for annular region
3938669647acSMatthew G. Knepley         PetscCall(DMSetPeriodicity(dm, NULL, NULL, NULL));
3939669647acSMatthew G. Knepley         PetscCall(DMSetCellCoordinatesLocal(dm, NULL));
3940669647acSMatthew G. Knepley         PetscCall(DMSetCellCoordinates(dm, NULL));
3941669647acSMatthew G. Knepley         PetscCall(DMPlexCreateCoordinateSpace(dm, 1, NULL));
3942669647acSMatthew G. Knepley         PetscCall(DMGetCoordinateDM(dm, &cdm));
3943669647acSMatthew G. Knepley         PetscCall(DMGetDS(cdm, &cds));
3944669647acSMatthew G. Knepley         PetscCall(PetscDSSetConstants(cds, 2, bounds));
3945669647acSMatthew G. Knepley         PetscCall(DMPlexRemapGeometry(dm, 0.0, boxToAnnulus));
3946669647acSMatthew G. Knepley       }
39479371c9d4SSatish Balay     } break;
39489371c9d4SSatish Balay     case DM_SHAPE_BOX_SURFACE: {
39499318fe57SMatthew G. Knepley       PetscInt  faces[3] = {0, 0, 0};
39509318fe57SMatthew G. Knepley       PetscReal lower[3] = {0, 0, 0};
39519318fe57SMatthew G. Knepley       PetscReal upper[3] = {1, 1, 1};
39529318fe57SMatthew G. Knepley       PetscInt  i, n;
39539318fe57SMatthew G. Knepley 
39549318fe57SMatthew G. Knepley       n = dim + 1;
39559318fe57SMatthew G. Knepley       for (i = 0; i < dim + 1; ++i) faces[i] = (dim + 1 == 1 ? 1 : 4 - (dim + 1));
39569566063dSJacob Faibussowitsch       PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg));
39579318fe57SMatthew G. Knepley       n = 3;
39589566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
395963a3b9bcSJacob 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);
39609318fe57SMatthew G. Knepley       n = 3;
39619566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
396263a3b9bcSJacob 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);
39639566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(dm, dim + 1, faces, lower, upper, interpolate));
39649371c9d4SSatish Balay     } break;
39659371c9d4SSatish Balay     case DM_SHAPE_SPHERE: {
39669318fe57SMatthew G. Knepley       PetscReal R = 1.0;
39679318fe57SMatthew G. Knepley 
39689566063dSJacob Faibussowitsch       PetscCall(PetscOptionsReal("-dm_plex_sphere_radius", "Radius of the sphere", "", R, &R, &flg));
39699566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateSphereMesh_Internal(dm, dim, simplex, R));
39709371c9d4SSatish Balay     } break;
39719371c9d4SSatish Balay     case DM_SHAPE_BALL: {
39729318fe57SMatthew G. Knepley       PetscReal R = 1.0;
39739318fe57SMatthew G. Knepley 
39749566063dSJacob Faibussowitsch       PetscCall(PetscOptionsReal("-dm_plex_ball_radius", "Radius of the ball", "", R, &R, &flg));
39759566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateBallMesh_Internal(dm, dim, R));
39769371c9d4SSatish Balay     } break;
39779371c9d4SSatish Balay     case DM_SHAPE_CYLINDER: {
39789318fe57SMatthew G. Knepley       DMBoundaryType bdt = DM_BOUNDARY_NONE;
39799318fe57SMatthew G. Knepley       PetscInt       Nw  = 6;
39809318fe57SMatthew G. Knepley 
39819566063dSJacob Faibussowitsch       PetscCall(PetscOptionsEnum("-dm_plex_cylinder_bd", "Boundary type in the z direction", "", DMBoundaryTypes, (PetscEnum)bdt, (PetscEnum *)&bdt, NULL));
39829566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-dm_plex_cylinder_num_wedges", "Number of wedges around the cylinder", "", Nw, &Nw, NULL));
39839318fe57SMatthew G. Knepley       switch (cell) {
3984d71ae5a4SJacob Faibussowitsch       case DM_POLYTOPE_TRI_PRISM_TENSOR:
3985d71ae5a4SJacob Faibussowitsch         PetscCall(DMPlexCreateWedgeCylinderMesh_Internal(dm, Nw, interpolate));
3986d71ae5a4SJacob Faibussowitsch         break;
3987d71ae5a4SJacob Faibussowitsch       default:
3988d71ae5a4SJacob Faibussowitsch         PetscCall(DMPlexCreateHexCylinderMesh_Internal(dm, bdt));
3989d71ae5a4SJacob Faibussowitsch         break;
39909318fe57SMatthew G. Knepley       }
39919371c9d4SSatish Balay     } break;
3992b7f5c055SJed Brown     case DM_SHAPE_SCHWARZ_P: // fallthrough
39939371c9d4SSatish Balay     case DM_SHAPE_GYROID: {
3994b7f5c055SJed Brown       PetscInt       extent[3] = {1, 1, 1}, refine = 0, layers = 0, three;
3995b7f5c055SJed Brown       PetscReal      thickness   = 0.;
3996b7f5c055SJed Brown       DMBoundaryType periodic[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
3997b7f5c055SJed Brown       DMPlexTPSType  tps_type    = shape == DM_SHAPE_SCHWARZ_P ? DMPLEX_TPS_SCHWARZ_P : DMPLEX_TPS_GYROID;
39981436d7faSJed Brown       PetscBool      tps_distribute;
39999566063dSJacob Faibussowitsch       PetscCall(PetscOptionsIntArray("-dm_plex_tps_extent", "Number of replicas for each of three dimensions", NULL, extent, (three = 3, &three), NULL));
40009566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-dm_plex_tps_refine", "Number of refinements", NULL, refine, &refine, NULL));
40019566063dSJacob Faibussowitsch       PetscCall(PetscOptionsEnumArray("-dm_plex_tps_periodic", "Periodicity in each of three dimensions", NULL, DMBoundaryTypes, (PetscEnum *)periodic, (three = 3, &three), NULL));
40029566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-dm_plex_tps_layers", "Number of layers in volumetric extrusion (or zero to not extrude)", NULL, layers, &layers, NULL));
40039566063dSJacob Faibussowitsch       PetscCall(PetscOptionsReal("-dm_plex_tps_thickness", "Thickness of volumetric extrusion", NULL, thickness, &thickness, NULL));
40049566063dSJacob Faibussowitsch       PetscCall(DMPlexDistributeGetDefault(dm, &tps_distribute));
40059566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_tps_distribute", "Distribute the 2D mesh prior to refinement and extrusion", NULL, tps_distribute, &tps_distribute, NULL));
40069566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateTPSMesh_Internal(dm, tps_type, extent, periodic, tps_distribute, refine, layers, thickness));
40079371c9d4SSatish Balay     } break;
40089371c9d4SSatish Balay     case DM_SHAPE_DOUBLET: {
400905bd46c0SStefano Zampini       DM        dmnew;
401005bd46c0SStefano Zampini       PetscReal rl = 0.0;
401105bd46c0SStefano Zampini 
401205bd46c0SStefano Zampini       PetscCall(PetscOptionsReal("-dm_plex_doublet_refinementlimit", "Refinement limit", NULL, rl, &rl, NULL));
401305bd46c0SStefano Zampini       PetscCall(DMPlexCreateDoublet(PetscObjectComm((PetscObject)dm), dim, simplex, interpolate, rl, &dmnew));
40145de52c6dSVaclav Hapla       PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
401569d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &dmnew));
40169371c9d4SSatish Balay     } break;
4017cfb853baSMatthew G. Knepley     case DM_SHAPE_HYPERCUBIC: {
4018cfb853baSMatthew G. Knepley       PetscInt       *edges;
4019cfb853baSMatthew G. Knepley       PetscReal      *lower, *upper;
4020cfb853baSMatthew G. Knepley       DMBoundaryType *bdt;
4021cfb853baSMatthew G. Knepley       PetscInt        n, d;
4022cfb853baSMatthew G. Knepley 
4023cfb853baSMatthew G. Knepley       *useCoordSpace = PETSC_FALSE;
4024cfb853baSMatthew G. Knepley       PetscCall(PetscMalloc4(dim, &edges, dim, &lower, dim, &upper, dim, &bdt));
4025cfb853baSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
4026cfb853baSMatthew G. Knepley         edges[d] = 1;
4027cfb853baSMatthew G. Knepley         lower[d] = 0.;
4028cfb853baSMatthew G. Knepley         upper[d] = 1.;
4029cfb853baSMatthew G. Knepley         bdt[d]   = DM_BOUNDARY_PERIODIC;
4030cfb853baSMatthew G. Knepley       }
4031cfb853baSMatthew G. Knepley       n = dim;
4032cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", edges, &n, &flg));
4033cfb853baSMatthew G. Knepley       n = dim;
4034cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
4035cfb853baSMatthew G. Knepley       PetscCheck(!flg || n == dim, comm, PETSC_ERR_ARG_SIZ, "Lower box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
4036cfb853baSMatthew G. Knepley       n = dim;
4037cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
4038cfb853baSMatthew G. Knepley       PetscCheck(!flg || n == dim, comm, PETSC_ERR_ARG_SIZ, "Upper box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
4039cfb853baSMatthew G. Knepley       n = dim;
4040cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *)bdt, &n, &flg));
4041cfb853baSMatthew G. Knepley       PetscCheck(!flg || n == dim, comm, PETSC_ERR_ARG_SIZ, "Box boundary types had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
4042cfb853baSMatthew G. Knepley       PetscCall(DMPlexCreateHypercubicMesh_Internal(dm, dim, lower, upper, edges, bdt));
4043cfb853baSMatthew G. Knepley       PetscCall(PetscFree4(edges, lower, upper, bdt));
4044cfb853baSMatthew G. Knepley     } break;
4045d71ae5a4SJacob Faibussowitsch     default:
4046d71ae5a4SJacob Faibussowitsch       SETERRQ(comm, PETSC_ERR_SUP, "Domain shape %s is unsupported", DMPlexShapes[shape]);
40479318fe57SMatthew G. Knepley     }
40489318fe57SMatthew G. Knepley   }
40499566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
405048a46eb9SPierre Jolivet   if (!((PetscObject)dm)->name && nameflg) PetscCall(PetscObjectSetName((PetscObject)dm, plexname));
4051708be2fdSJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_CreateFromOptions, dm, 0, 0, 0));
40523ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
40530a6ba040SMatthew G. Knepley }
40540a6ba040SMatthew G. Knepley 
4055d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions_NonRefinement_Plex(DM dm, PetscOptionItems *PetscOptionsObject)
4056d71ae5a4SJacob Faibussowitsch {
40570a6ba040SMatthew G. Knepley   DM_Plex  *mesh = (DM_Plex *)dm->data;
40587f9d8d6cSVaclav Hapla   PetscBool flg, flg2;
40599318fe57SMatthew G. Knepley   char      bdLabel[PETSC_MAX_PATH_LEN];
40600a6ba040SMatthew G. Knepley 
40610a6ba040SMatthew G. Knepley   PetscFunctionBegin;
40620a6ba040SMatthew G. Knepley   /* Handle viewing */
40639566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_print_set_values", "Output all set values info", "DMPlexMatSetClosure", PETSC_FALSE, &mesh->printSetValues, NULL));
40649566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_fem", "Debug output level all fem computations", "DMPlexSNESComputeResidualFEM", 0, &mesh->printFEM, NULL, 0));
40659566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-dm_plex_print_tol", "Tolerance for FEM output", "DMPlexSNESComputeResidualFEM", mesh->printTol, &mesh->printTol, NULL));
40669566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_l2", "Debug output level all L2 diff computations", "DMComputeL2Diff", 0, &mesh->printL2, NULL, 0));
4067f5867de0SMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_locate", "Debug output level all point location computations", "DMLocatePoints", 0, &mesh->printLocate, NULL, 0));
40689566063dSJacob Faibussowitsch   PetscCall(DMMonitorSetFromOptions(dm, "-dm_plex_monitor_throughput", "Monitor the simulation throughput", "DMPlexMonitorThroughput", DMPlexMonitorThroughput, NULL, &flg));
40699566063dSJacob Faibussowitsch   if (flg) PetscCall(PetscLogDefaultBegin());
40709318fe57SMatthew G. Knepley   /* Labeling */
40719566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_boundary_label", "Label to mark the mesh boundary", "", bdLabel, bdLabel, sizeof(bdLabel), &flg));
40729566063dSJacob Faibussowitsch   if (flg) PetscCall(DMPlexCreateBoundaryLabel_Private(dm, bdLabel));
4073953fc75cSMatthew G. Knepley   /* Point Location */
40749566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_hash_location", "Use grid hashing for point location", "DMInterpolate", PETSC_FALSE, &mesh->useHashLocation, NULL));
40750848f4b5SMatthew G. Knepley   /* Partitioning and distribution */
40769566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_partition_balance", "Attempt to evenly divide points on partition boundary between processes", "DMPlexSetPartitionBalance", PETSC_FALSE, &mesh->partitionBalance, NULL));
40772e62ab5aSMatthew G. Knepley   /* Generation and remeshing */
40789566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_remesh_bd", "Allow changes to the boundary on remeshing", "DMAdapt", PETSC_FALSE, &mesh->remeshBd, NULL));
4079b29cfa1cSToby Isaac   /* Projection behavior */
4080d5b43468SJose E. Roman   PetscCall(PetscOptionsBoundedInt("-dm_plex_max_projection_height", "Maximum mesh point height used to project locally", "DMPlexSetMaxProjectionHeight", 0, &mesh->maxProjectionHeight, NULL, 0));
40819566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_regular_refinement", "Use special nested projection algorithm for regular refinement", "DMPlexSetRegularRefinement", mesh->regularRefinement, &mesh->regularRefinement, NULL));
4082f12cf164SMatthew G. Knepley   /* Checking structure */
4083f12cf164SMatthew G. Knepley   {
40847f9d8d6cSVaclav Hapla     PetscBool all = PETSC_FALSE;
4085f12cf164SMatthew G. Knepley 
40867f9d8d6cSVaclav Hapla     PetscCall(PetscOptionsBool("-dm_plex_check_all", "Perform all basic checks", "DMPlexCheck", PETSC_FALSE, &all, NULL));
40877f9d8d6cSVaclav Hapla     if (all) {
40887f9d8d6cSVaclav Hapla       PetscCall(DMPlexCheck(dm));
40897f9d8d6cSVaclav Hapla     } else {
40909566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_symmetry", "Check that the adjacency information in the mesh is symmetric", "DMPlexCheckSymmetry", PETSC_FALSE, &flg, &flg2));
40917f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckSymmetry(dm));
40929566063dSJacob 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));
40937f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckSkeleton(dm, 0));
40949566063dSJacob 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));
40957f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckFaces(dm, 0));
40969566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_geometry", "Check that cells have positive volume", "DMPlexCheckGeometry", PETSC_FALSE, &flg, &flg2));
40977f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckGeometry(dm));
40989566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_pointsf", "Check some necessary conditions for PointSF", "DMPlexCheckPointSF", PETSC_FALSE, &flg, &flg2));
4099d7d32a9aSMatthew G. Knepley       if (flg && flg2) PetscCall(DMPlexCheckPointSF(dm, NULL, PETSC_FALSE));
41009566063dSJacob 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));
41017f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckInterfaceCones(dm));
41027f9d8d6cSVaclav Hapla     }
41039566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-dm_plex_check_cell_shape", "Check cell shape", "DMPlexCheckCellShape", PETSC_FALSE, &flg, &flg2));
41049566063dSJacob Faibussowitsch     if (flg && flg2) PetscCall(DMPlexCheckCellShape(dm, PETSC_TRUE, PETSC_DETERMINE));
4105f12cf164SMatthew G. Knepley   }
41069318fe57SMatthew G. Knepley   {
41079318fe57SMatthew G. Knepley     PetscReal scale = 1.0;
41084f3833eaSMatthew G. Knepley 
41099566063dSJacob Faibussowitsch     PetscCall(PetscOptionsReal("-dm_plex_scale", "Scale factor for mesh coordinates", "DMPlexScale", scale, &scale, &flg));
41109318fe57SMatthew G. Knepley     if (flg) {
41119318fe57SMatthew G. Knepley       Vec coordinates, coordinatesLocal;
41129318fe57SMatthew G. Knepley 
41139566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinates(dm, &coordinates));
41149566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinatesLocal(dm, &coordinatesLocal));
41159566063dSJacob Faibussowitsch       PetscCall(VecScale(coordinates, scale));
41169566063dSJacob Faibussowitsch       PetscCall(VecScale(coordinatesLocal, scale));
41179318fe57SMatthew G. Knepley     }
41189318fe57SMatthew G. Knepley   }
41199566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerSetFromOptions(mesh->partitioner));
41203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
412168d4fef7SMatthew G. Knepley }
412268d4fef7SMatthew G. Knepley 
4123d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions_Overlap_Plex(DM dm, PetscOptionItems *PetscOptionsObject, PetscInt *overlap)
4124d71ae5a4SJacob Faibussowitsch {
4125c506a872SMatthew G. Knepley   PetscInt  numOvLabels = 16, numOvExLabels = 16;
4126c506a872SMatthew G. Knepley   char     *ovLabelNames[16], *ovExLabelNames[16];
4127c506a872SMatthew G. Knepley   PetscInt  numOvValues = 16, numOvExValues = 16, l;
4128c506a872SMatthew G. Knepley   PetscBool flg;
4129c506a872SMatthew G. Knepley 
4130c506a872SMatthew G. Knepley   PetscFunctionBegin;
4131c506a872SMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_distribute_overlap", "The size of the overlap halo", "DMPlexDistribute", *overlap, overlap, NULL, 0));
4132c506a872SMatthew G. Knepley   PetscCall(PetscOptionsStringArray("-dm_distribute_overlap_labels", "List of overlap label names", "DMPlexDistribute", ovLabelNames, &numOvLabels, &flg));
4133c506a872SMatthew G. Knepley   if (!flg) numOvLabels = 0;
4134c506a872SMatthew G. Knepley   if (numOvLabels) {
4135c506a872SMatthew G. Knepley     ((DM_Plex *)dm->data)->numOvLabels = numOvLabels;
4136c506a872SMatthew G. Knepley     for (l = 0; l < numOvLabels; ++l) {
4137c506a872SMatthew G. Knepley       PetscCall(DMGetLabel(dm, ovLabelNames[l], &((DM_Plex *)dm->data)->ovLabels[l]));
4138c506a872SMatthew G. Knepley       PetscCheck(((DM_Plex *)dm->data)->ovLabels[l], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid label name %s", ovLabelNames[l]);
4139c506a872SMatthew G. Knepley       PetscCall(PetscFree(ovLabelNames[l]));
4140c506a872SMatthew G. Knepley     }
4141c506a872SMatthew G. Knepley     PetscCall(PetscOptionsIntArray("-dm_distribute_overlap_values", "List of overlap label values", "DMPlexDistribute", ((DM_Plex *)dm->data)->ovValues, &numOvValues, &flg));
4142c506a872SMatthew G. Knepley     if (!flg) numOvValues = 0;
4143c506a872SMatthew 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);
4144c506a872SMatthew G. Knepley 
4145c506a872SMatthew G. Knepley     PetscCall(PetscOptionsStringArray("-dm_distribute_overlap_exclude_labels", "List of overlap exclude label names", "DMPlexDistribute", ovExLabelNames, &numOvExLabels, &flg));
4146c506a872SMatthew G. Knepley     if (!flg) numOvExLabels = 0;
4147c506a872SMatthew G. Knepley     ((DM_Plex *)dm->data)->numOvExLabels = numOvExLabels;
4148c506a872SMatthew G. Knepley     for (l = 0; l < numOvExLabels; ++l) {
4149c506a872SMatthew G. Knepley       PetscCall(DMGetLabel(dm, ovExLabelNames[l], &((DM_Plex *)dm->data)->ovExLabels[l]));
4150c506a872SMatthew G. Knepley       PetscCheck(((DM_Plex *)dm->data)->ovExLabels[l], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid label name %s", ovExLabelNames[l]);
4151c506a872SMatthew G. Knepley       PetscCall(PetscFree(ovExLabelNames[l]));
4152c506a872SMatthew G. Knepley     }
4153c506a872SMatthew G. Knepley     PetscCall(PetscOptionsIntArray("-dm_distribute_overlap_exclude_values", "List of overlap exclude label values", "DMPlexDistribute", ((DM_Plex *)dm->data)->ovExValues, &numOvExValues, &flg));
4154c506a872SMatthew G. Knepley     if (!flg) numOvExValues = 0;
4155c506a872SMatthew 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);
4156c506a872SMatthew G. Knepley   }
41573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4158c506a872SMatthew G. Knepley }
4159c506a872SMatthew G. Knepley 
4160d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetFromOptions_Plex(DM dm, PetscOptionItems *PetscOptionsObject)
4161d71ae5a4SJacob Faibussowitsch {
4162bdf63967SMatthew G. Knepley   PetscFunctionList        ordlist;
4163bdf63967SMatthew G. Knepley   char                     oname[256];
4164a286e215SMatthew G. Knepley   DMPlexReorderDefaultFlag reorder;
4165d410b0cfSMatthew G. Knepley   PetscReal                volume    = -1.0;
41669318fe57SMatthew G. Knepley   PetscInt                 prerefine = 0, refine = 0, r, coarsen = 0, overlap = 0, extLayers = 0, dim;
4167a286e215SMatthew G. Knepley   PetscBool uniformOrig = PETSC_FALSE, created = PETSC_FALSE, uniform = PETSC_TRUE, distribute, saveSF = PETSC_FALSE, interpolate = PETSC_TRUE, coordSpace = PETSC_TRUE, remap = PETSC_TRUE, ghostCells = PETSC_FALSE, isHierarchy, ignoreModel = PETSC_FALSE, flg;
416868d4fef7SMatthew G. Knepley 
416968d4fef7SMatthew G. Knepley   PetscFunctionBegin;
4170d0609cedSBarry Smith   PetscOptionsHeadBegin(PetscOptionsObject, "DMPlex Options");
4171dd4c3f67SMatthew G. Knepley   if (dm->cloneOpts) goto non_refine;
41729318fe57SMatthew G. Knepley   /* Handle automatic creation */
41739566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
41746bc1bd01Sksagiyam   if (dim < 0) {
41756bc1bd01Sksagiyam     PetscCall(DMPlexCreateFromOptions_Internal(PetscOptionsObject, &coordSpace, dm));
41766bc1bd01Sksagiyam     created = PETSC_TRUE;
41776bc1bd01Sksagiyam   }
41786bc1bd01Sksagiyam   PetscCall(DMGetDimension(dm, &dim));
4179d89e6e46SMatthew G. Knepley   /* Handle interpolation before distribution */
41809566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_interpolate_pre", "Flag to interpolate mesh before distribution", "", interpolate, &interpolate, &flg));
4181d89e6e46SMatthew G. Knepley   if (flg) {
4182d89e6e46SMatthew G. Knepley     DMPlexInterpolatedFlag interpolated;
4183d89e6e46SMatthew G. Knepley 
41849566063dSJacob Faibussowitsch     PetscCall(DMPlexIsInterpolated(dm, &interpolated));
4185d89e6e46SMatthew G. Knepley     if (interpolated == DMPLEX_INTERPOLATED_FULL && !interpolate) {
4186d89e6e46SMatthew G. Knepley       DM udm;
4187d89e6e46SMatthew G. Knepley 
41889566063dSJacob Faibussowitsch       PetscCall(DMPlexUninterpolate(dm, &udm));
418969d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &udm));
4190d89e6e46SMatthew G. Knepley     } else if (interpolated != DMPLEX_INTERPOLATED_FULL && interpolate) {
4191d89e6e46SMatthew G. Knepley       DM idm;
4192d89e6e46SMatthew G. Knepley 
41939566063dSJacob Faibussowitsch       PetscCall(DMPlexInterpolate(dm, &idm));
419469d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &idm));
4195d89e6e46SMatthew G. Knepley     }
4196d89e6e46SMatthew G. Knepley   }
41979b44eab4SMatthew G. Knepley   /* Handle DMPlex refinement before distribution */
41989566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_ignore_model", "Flag to ignore the geometry model when refining", "DMCreate", ignoreModel, &ignoreModel, &flg));
4199ad540459SPierre Jolivet   if (flg) ((DM_Plex *)dm->data)->ignoreModel = ignoreModel;
42009566063dSJacob Faibussowitsch   PetscCall(DMPlexGetRefinementUniform(dm, &uniformOrig));
42019566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine_pre", "The number of refinements before distribution", "DMCreate", prerefine, &prerefine, NULL, 0));
42029566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_remap_pre", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL));
42039566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_uniform_pre", "Flag for uniform refinement before distribution", "DMCreate", uniform, &uniform, &flg));
42049566063dSJacob Faibussowitsch   if (flg) PetscCall(DMPlexSetRefinementUniform(dm, uniform));
42059566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-dm_refine_volume_limit_pre", "The maximum cell volume after refinement before distribution", "DMCreate", volume, &volume, &flg));
42069318fe57SMatthew G. Knepley   if (flg) {
42079566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementUniform(dm, PETSC_FALSE));
42089566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementLimit(dm, volume));
42099318fe57SMatthew G. Knepley     prerefine = PetscMax(prerefine, 1);
42109318fe57SMatthew G. Knepley   }
42119b44eab4SMatthew G. Knepley   for (r = 0; r < prerefine; ++r) {
42129b44eab4SMatthew G. Knepley     DM             rdm;
42139b44eab4SMatthew G. Knepley     PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc;
42149b44eab4SMatthew G. Knepley 
4215dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
42169566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &rdm));
421769d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &rdm));
4218dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
421961a622f3SMatthew G. Knepley     if (coordFunc && remap) {
42209566063dSJacob Faibussowitsch       PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
42219b44eab4SMatthew G. Knepley       ((DM_Plex *)dm->data)->coordFunc = coordFunc;
42229b44eab4SMatthew G. Knepley     }
42239b44eab4SMatthew G. Knepley   }
42249566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, uniformOrig));
42259318fe57SMatthew G. Knepley   /* Handle DMPlex extrusion before distribution */
42269566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_extrude", "The number of layers to extrude", "", extLayers, &extLayers, NULL, 0));
42279318fe57SMatthew G. Knepley   if (extLayers) {
42289318fe57SMatthew G. Knepley     DM edm;
42299318fe57SMatthew G. Knepley 
42309566063dSJacob Faibussowitsch     PetscCall(DMExtrude(dm, extLayers, &edm));
423169d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &edm));
423248d16a33SMatthew G. Knepley     ((DM_Plex *)dm->data)->coordFunc = NULL;
4233dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
4234d410b0cfSMatthew G. Knepley     extLayers = 0;
42355e17fc22SAidan Hamilton     PetscCall(DMGetDimension(dm, &dim));
42369318fe57SMatthew G. Knepley   }
4237bdf63967SMatthew G. Knepley   /* Handle DMPlex reordering before distribution */
42386bc1bd01Sksagiyam   PetscCall(DMPlexReorderGetDefault(dm, &reorder));
42399566063dSJacob Faibussowitsch   PetscCall(MatGetOrderingList(&ordlist));
42406bc1bd01Sksagiyam   PetscCall(PetscStrncpy(oname, MATORDERINGNATURAL, sizeof(oname)));
42419566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_plex_reorder", "Set mesh reordering type", "DMPlexGetOrdering", ordlist, MATORDERINGNATURAL, oname, sizeof(oname), &flg));
42426bc1bd01Sksagiyam   if (reorder == DMPLEX_REORDER_DEFAULT_TRUE || flg) {
4243bdf63967SMatthew G. Knepley     DM pdm;
4244bdf63967SMatthew G. Knepley     IS perm;
4245bdf63967SMatthew G. Knepley 
42469566063dSJacob Faibussowitsch     PetscCall(DMPlexGetOrdering(dm, oname, NULL, &perm));
42479566063dSJacob Faibussowitsch     PetscCall(DMPlexPermute(dm, perm, &pdm));
42489566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&perm));
424969d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &pdm));
4250dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
4251bdf63967SMatthew G. Knepley   }
42529b44eab4SMatthew G. Knepley   /* Handle DMPlex distribution */
42539566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeGetDefault(dm, &distribute));
4254c506a872SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_distribute", "Flag to redistribute a mesh among processes", "DMPlexDistribute", distribute, &distribute, NULL));
4255a286e215SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_distribute_save_sf", "Flag to save the migration SF", "DMPlexSetMigrationSF", saveSF, &saveSF, NULL));
4256dbbe0bcdSBarry Smith   PetscCall(DMSetFromOptions_Overlap_Plex(dm, PetscOptionsObject, &overlap));
42579b44eab4SMatthew G. Knepley   if (distribute) {
42589b44eab4SMatthew G. Knepley     DM               pdm = NULL;
42599b44eab4SMatthew G. Knepley     PetscPartitioner part;
4260a286e215SMatthew G. Knepley     PetscSF          sfMigration;
42619b44eab4SMatthew G. Knepley 
42629566063dSJacob Faibussowitsch     PetscCall(DMPlexGetPartitioner(dm, &part));
42639566063dSJacob Faibussowitsch     PetscCall(PetscPartitionerSetFromOptions(part));
4264a286e215SMatthew G. Knepley     PetscCall(DMPlexDistribute(dm, overlap, &sfMigration, &pdm));
426548a46eb9SPierre Jolivet     if (pdm) PetscCall(DMPlexReplace_Internal(dm, &pdm));
4266a286e215SMatthew G. Knepley     if (saveSF) PetscCall(DMPlexSetMigrationSF(dm, sfMigration));
4267a286e215SMatthew G. Knepley     PetscCall(PetscSFDestroy(&sfMigration));
42689b44eab4SMatthew G. Knepley   }
4269d2b2dc1eSMatthew G. Knepley   /* Must check CEED options before creating function space for coordinates */
4270d2b2dc1eSMatthew G. Knepley   {
4271d2b2dc1eSMatthew G. Knepley     PetscBool useCeed = PETSC_FALSE, flg;
4272d2b2dc1eSMatthew G. Knepley 
4273d2b2dc1eSMatthew G. Knepley     PetscCall(PetscOptionsBool("-dm_plex_use_ceed", "Use LibCEED as the FEM backend", "DMPlexSetUseCeed", useCeed, &useCeed, &flg));
4274d2b2dc1eSMatthew G. Knepley     if (flg) PetscCall(DMPlexSetUseCeed(dm, useCeed));
4275d2b2dc1eSMatthew G. Knepley   }
42769318fe57SMatthew G. Knepley   /* Create coordinate space */
42779318fe57SMatthew G. Knepley   if (created) {
427861a622f3SMatthew G. Knepley     DM_Plex  *mesh   = (DM_Plex *)dm->data;
42799318fe57SMatthew G. Knepley     PetscInt  degree = 1;
42805515ebd3SMatthew G. Knepley     PetscInt  height = 0;
42815515ebd3SMatthew G. Knepley     DM        cdm;
42826858538eSMatthew G. Knepley     PetscBool flg;
42839318fe57SMatthew G. Knepley 
42849566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-dm_coord_space", "Use an FEM space for coordinates", "", coordSpace, &coordSpace, &flg));
42859566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-dm_coord_petscspace_degree", "FEM degree for coordinate space", "", degree, &degree, NULL));
42869566063dSJacob Faibussowitsch     if (coordSpace) PetscCall(DMPlexCreateCoordinateSpace(dm, degree, mesh->coordFunc));
42875515ebd3SMatthew G. Knepley     PetscCall(DMGetCoordinateDM(dm, &cdm));
428861a622f3SMatthew G. Knepley     if (flg && !coordSpace) {
428961a622f3SMatthew G. Knepley       PetscDS      cds;
429061a622f3SMatthew G. Knepley       PetscObject  obj;
429161a622f3SMatthew G. Knepley       PetscClassId id;
429261a622f3SMatthew G. Knepley 
42939566063dSJacob Faibussowitsch       PetscCall(DMGetDS(cdm, &cds));
42949566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
42959566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
429661a622f3SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
429761a622f3SMatthew G. Knepley         PetscContainer dummy;
429861a622f3SMatthew G. Knepley 
42999566063dSJacob Faibussowitsch         PetscCall(PetscContainerCreate(PETSC_COMM_SELF, &dummy));
43009566063dSJacob Faibussowitsch         PetscCall(PetscObjectSetName((PetscObject)dummy, "coordinates"));
43019566063dSJacob Faibussowitsch         PetscCall(DMSetField(cdm, 0, NULL, (PetscObject)dummy));
43029566063dSJacob Faibussowitsch         PetscCall(PetscContainerDestroy(&dummy));
43039566063dSJacob Faibussowitsch         PetscCall(DMClearDS(cdm));
430461a622f3SMatthew G. Knepley       }
430561a622f3SMatthew G. Knepley       mesh->coordFunc = NULL;
430661a622f3SMatthew G. Knepley     }
43076858538eSMatthew G. Knepley     PetscCall(PetscOptionsBool("-dm_sparse_localize", "Localize only necessary cells", "", dm->sparseLocalize, &dm->sparseLocalize, &flg));
43085515ebd3SMatthew G. Knepley     PetscCall(PetscOptionsInt("-dm_localize_height", "Localize edges and faces in addition to cells", "", height, &height, &flg));
43095515ebd3SMatthew G. Knepley     if (flg) PetscCall(DMPlexSetMaxProjectionHeight(cdm, height));
43109566063dSJacob Faibussowitsch     PetscCall(DMLocalizeCoordinates(dm));
43119318fe57SMatthew G. Knepley   }
431268d4fef7SMatthew G. Knepley   /* Handle DMPlex refinement */
431361a622f3SMatthew G. Knepley   remap = PETSC_TRUE;
43149566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine", "The number of uniform refinements", "DMCreate", refine, &refine, NULL, 0));
43159566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_remap", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL));
43169566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine_hierarchy", "The number of uniform refinements", "DMCreate", refine, &refine, &isHierarchy, 0));
43179566063dSJacob Faibussowitsch   if (refine) PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
431868d4fef7SMatthew G. Knepley   if (refine && isHierarchy) {
4319acdc6f61SToby Isaac     DM *dms, coarseDM;
432068d4fef7SMatthew G. Knepley 
43219566063dSJacob Faibussowitsch     PetscCall(DMGetCoarseDM(dm, &coarseDM));
43229566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)coarseDM));
43239566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(refine, &dms));
43249566063dSJacob Faibussowitsch     PetscCall(DMRefineHierarchy(dm, refine, dms));
432568d4fef7SMatthew G. Knepley     /* Total hack since we do not pass in a pointer */
43269566063dSJacob Faibussowitsch     PetscCall(DMPlexSwap_Static(dm, dms[refine - 1]));
432768d4fef7SMatthew G. Knepley     if (refine == 1) {
43289566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dm, dms[0]));
43299566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dm, PETSC_TRUE));
433068d4fef7SMatthew G. Knepley     } else {
43319566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dm, dms[refine - 2]));
43329566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dm, PETSC_TRUE));
43339566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dms[0], dms[refine - 1]));
43349566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dms[0], PETSC_TRUE));
433568d4fef7SMatthew G. Knepley     }
43369566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dms[refine - 1], coarseDM));
43379566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)coarseDM));
433868d4fef7SMatthew G. Knepley     /* Free DMs */
433968d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
4340dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dms[r], PetscOptionsObject));
43419566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dms[r]));
434268d4fef7SMatthew G. Knepley     }
43439566063dSJacob Faibussowitsch     PetscCall(PetscFree(dms));
434468d4fef7SMatthew G. Knepley   } else {
434568d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
43469318fe57SMatthew G. Knepley       DM             rdm;
434751a74b61SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc;
434868d4fef7SMatthew G. Knepley 
4349dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
43509566063dSJacob Faibussowitsch       PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &rdm));
435168d4fef7SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
435269d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &rdm));
4353dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
435461a622f3SMatthew G. Knepley       if (coordFunc && remap) {
43559566063dSJacob Faibussowitsch         PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
435651a74b61SMatthew G. Knepley         ((DM_Plex *)dm->data)->coordFunc = coordFunc;
435751a74b61SMatthew G. Knepley       }
435868d4fef7SMatthew G. Knepley     }
435968d4fef7SMatthew G. Knepley   }
43603cf6fe12SMatthew G. Knepley   /* Handle DMPlex coarsening */
43619566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_coarsen", "Coarsen the mesh", "DMCreate", coarsen, &coarsen, NULL, 0));
43629566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_coarsen_hierarchy", "The number of coarsenings", "DMCreate", coarsen, &coarsen, &isHierarchy, 0));
4363b653a561SMatthew G. Knepley   if (coarsen && isHierarchy) {
4364b653a561SMatthew G. Knepley     DM *dms;
4365b653a561SMatthew G. Knepley 
43669566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(coarsen, &dms));
43679566063dSJacob Faibussowitsch     PetscCall(DMCoarsenHierarchy(dm, coarsen, dms));
4368b653a561SMatthew G. Knepley     /* Free DMs */
4369b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
4370dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dms[r], PetscOptionsObject));
43719566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dms[r]));
4372b653a561SMatthew G. Knepley     }
43739566063dSJacob Faibussowitsch     PetscCall(PetscFree(dms));
4374b653a561SMatthew G. Knepley   } else {
4375b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
43769318fe57SMatthew G. Knepley       DM             cdm;
43779318fe57SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc;
43783cf6fe12SMatthew G. Knepley 
4379dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
43809566063dSJacob Faibussowitsch       PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &cdm));
43813cf6fe12SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
438269d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &cdm));
4383dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
43849318fe57SMatthew G. Knepley       if (coordFunc) {
43859566063dSJacob Faibussowitsch         PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
43869318fe57SMatthew G. Knepley         ((DM_Plex *)dm->data)->coordFunc = coordFunc;
43879318fe57SMatthew G. Knepley       }
43883cf6fe12SMatthew G. Knepley     }
4389b653a561SMatthew G. Knepley   }
4390909dfd52SMatthew G. Knepley   /* Handle ghost cells */
43919566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_create_fv_ghost_cells", "Flag to create finite volume ghost cells on the boundary", "DMCreate", ghostCells, &ghostCells, NULL));
4392909dfd52SMatthew G. Knepley   if (ghostCells) {
4393909dfd52SMatthew G. Knepley     DM   gdm;
4394909dfd52SMatthew G. Knepley     char lname[PETSC_MAX_PATH_LEN];
4395909dfd52SMatthew G. Knepley 
4396909dfd52SMatthew G. Knepley     lname[0] = '\0';
43979566063dSJacob Faibussowitsch     PetscCall(PetscOptionsString("-dm_plex_fv_ghost_cells_label", "Label name for ghost cells boundary", "DMCreate", lname, lname, sizeof(lname), &flg));
43989566063dSJacob Faibussowitsch     PetscCall(DMPlexConstructGhostCells(dm, flg ? lname : NULL, NULL, &gdm));
439969d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &gdm));
4400909dfd52SMatthew G. Knepley   }
44016913077dSMatthew G. Knepley   /* Handle 1D order */
44026bc1bd01Sksagiyam   if (reorder != DMPLEX_REORDER_DEFAULT_FALSE && dim == 1) {
44036913077dSMatthew G. Knepley     DM           cdm, rdm;
44046913077dSMatthew G. Knepley     PetscDS      cds;
44056913077dSMatthew G. Knepley     PetscObject  obj;
44066913077dSMatthew G. Knepley     PetscClassId id = PETSC_OBJECT_CLASSID;
44076913077dSMatthew G. Knepley     IS           perm;
44086bc1bd01Sksagiyam     PetscInt     Nf;
44096913077dSMatthew G. Knepley     PetscBool    distributed;
44106913077dSMatthew G. Knepley 
44119566063dSJacob Faibussowitsch     PetscCall(DMPlexIsDistributed(dm, &distributed));
44129566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
44139566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
44149566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(cds, &Nf));
44156913077dSMatthew G. Knepley     if (Nf) {
44169566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
44179566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
44186913077dSMatthew G. Knepley     }
44196bc1bd01Sksagiyam     if (!distributed && id != PETSCFE_CLASSID) {
44209566063dSJacob Faibussowitsch       PetscCall(DMPlexGetOrdering1D(dm, &perm));
44219566063dSJacob Faibussowitsch       PetscCall(DMPlexPermute(dm, perm, &rdm));
442269d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &rdm));
44239566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&perm));
44246913077dSMatthew G. Knepley     }
44256913077dSMatthew G. Knepley   }
44263cf6fe12SMatthew G. Knepley /* Handle */
4427dd4c3f67SMatthew G. Knepley non_refine:
4428dbbe0bcdSBarry Smith   PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
4429d0609cedSBarry Smith   PetscOptionsHeadEnd();
44303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44310a6ba040SMatthew G. Knepley }
44320a6ba040SMatthew G. Knepley 
4433d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMCreateGlobalVector_Plex(DM dm, Vec *vec)
4434d71ae5a4SJacob Faibussowitsch {
4435552f7358SJed Brown   PetscFunctionBegin;
44369566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector_Section_Private(dm, vec));
44379566063dSJacob Faibussowitsch   /* PetscCall(VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM)); */
44389566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEW, (void (*)(void))VecView_Plex));
44399566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEWNATIVE, (void (*)(void))VecView_Plex_Native));
44409566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOAD, (void (*)(void))VecLoad_Plex));
44419566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOADNATIVE, (void (*)(void))VecLoad_Plex_Native));
44423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4443552f7358SJed Brown }
4444552f7358SJed Brown 
4445d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMCreateLocalVector_Plex(DM dm, Vec *vec)
4446d71ae5a4SJacob Faibussowitsch {
4447552f7358SJed Brown   PetscFunctionBegin;
44489566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector_Section_Private(dm, vec));
44499566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEW, (void (*)(void))VecView_Plex_Local));
44509566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOAD, (void (*)(void))VecLoad_Plex_Local));
44513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4452552f7358SJed Brown }
4453552f7358SJed Brown 
4454d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGetDimPoints_Plex(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
4455d71ae5a4SJacob Faibussowitsch {
4456793f3fe5SMatthew G. Knepley   PetscInt depth, d;
4457793f3fe5SMatthew G. Knepley 
4458793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
44599566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
4460793f3fe5SMatthew G. Knepley   if (depth == 1) {
44619566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(dm, &d));
44629566063dSJacob Faibussowitsch     if (dim == 0) PetscCall(DMPlexGetDepthStratum(dm, dim, pStart, pEnd));
44639566063dSJacob Faibussowitsch     else if (dim == d) PetscCall(DMPlexGetDepthStratum(dm, 1, pStart, pEnd));
44649371c9d4SSatish Balay     else {
44659371c9d4SSatish Balay       *pStart = 0;
44669371c9d4SSatish Balay       *pEnd   = 0;
44679371c9d4SSatish Balay     }
4468793f3fe5SMatthew G. Knepley   } else {
44699566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, dim, pStart, pEnd));
4470793f3fe5SMatthew G. Knepley   }
44713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4472793f3fe5SMatthew G. Knepley }
4473793f3fe5SMatthew G. Knepley 
4474d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGetNeighbors_Plex(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
4475d71ae5a4SJacob Faibussowitsch {
4476502a2867SDave May   PetscSF            sf;
44770a19bb7dSprj-   PetscInt           niranks, njranks, n;
44780a19bb7dSprj-   const PetscMPIInt *iranks, *jranks;
44790a19bb7dSprj-   DM_Plex           *data = (DM_Plex *)dm->data;
4480502a2867SDave May 
44812f356facSMatthew G. Knepley   PetscFunctionBegin;
44829566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
44830a19bb7dSprj-   if (!data->neighbors) {
44849566063dSJacob Faibussowitsch     PetscCall(PetscSFSetUp(sf));
44859566063dSJacob Faibussowitsch     PetscCall(PetscSFGetRootRanks(sf, &njranks, &jranks, NULL, NULL, NULL));
44869566063dSJacob Faibussowitsch     PetscCall(PetscSFGetLeafRanks(sf, &niranks, &iranks, NULL, NULL));
44879566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(njranks + niranks + 1, &data->neighbors));
44889566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(data->neighbors + 1, jranks, njranks));
44899566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(data->neighbors + njranks + 1, iranks, niranks));
44900a19bb7dSprj-     n = njranks + niranks;
44919566063dSJacob Faibussowitsch     PetscCall(PetscSortRemoveDupsMPIInt(&n, data->neighbors + 1));
44920a19bb7dSprj-     /* The following cast should never fail: can't have more neighbors than PETSC_MPI_INT_MAX */
44939566063dSJacob Faibussowitsch     PetscCall(PetscMPIIntCast(n, data->neighbors));
44940a19bb7dSprj-   }
44950a19bb7dSprj-   if (nranks) *nranks = data->neighbors[0];
44960a19bb7dSprj-   if (ranks) {
44970a19bb7dSprj-     if (data->neighbors[0]) *ranks = data->neighbors + 1;
44980a19bb7dSprj-     else *ranks = NULL;
44990a19bb7dSprj-   }
45003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4501502a2867SDave May }
4502502a2867SDave May 
45031eb70e55SToby Isaac PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM, DM, Mat, Vec, Vec);
45041eb70e55SToby Isaac 
4505d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMInitialize_Plex(DM dm)
4506d71ae5a4SJacob Faibussowitsch {
4507552f7358SJed Brown   PetscFunctionBegin;
4508552f7358SJed Brown   dm->ops->view                      = DMView_Plex;
45092c40f234SMatthew G. Knepley   dm->ops->load                      = DMLoad_Plex;
4510552f7358SJed Brown   dm->ops->setfromoptions            = DMSetFromOptions_Plex;
451138221697SMatthew G. Knepley   dm->ops->clone                     = DMClone_Plex;
4512552f7358SJed Brown   dm->ops->setup                     = DMSetUp_Plex;
45131bb6d2a8SBarry Smith   dm->ops->createlocalsection        = DMCreateLocalSection_Plex;
451466ad2231SToby Isaac   dm->ops->createdefaultconstraints  = DMCreateDefaultConstraints_Plex;
4515552f7358SJed Brown   dm->ops->createglobalvector        = DMCreateGlobalVector_Plex;
4516552f7358SJed Brown   dm->ops->createlocalvector         = DMCreateLocalVector_Plex;
4517184d77edSJed Brown   dm->ops->getlocaltoglobalmapping   = NULL;
45180298fd71SBarry Smith   dm->ops->createfieldis             = NULL;
4519552f7358SJed Brown   dm->ops->createcoordinatedm        = DMCreateCoordinateDM_Plex;
4520f19dbd58SToby Isaac   dm->ops->createcoordinatefield     = DMCreateCoordinateField_Plex;
45210a6ba040SMatthew G. Knepley   dm->ops->getcoloring               = NULL;
4522552f7358SJed Brown   dm->ops->creatematrix              = DMCreateMatrix_Plex;
4523bceba477SMatthew G. Knepley   dm->ops->createinterpolation       = DMCreateInterpolation_Plex;
4524bd041c0cSMatthew G. Knepley   dm->ops->createmassmatrix          = DMCreateMassMatrix_Plex;
4525b4937a87SMatthew G. Knepley   dm->ops->createmassmatrixlumped    = DMCreateMassMatrixLumped_Plex;
45265a84ad33SLisandro Dalcin   dm->ops->createinjection           = DMCreateInjection_Plex;
4527552f7358SJed Brown   dm->ops->refine                    = DMRefine_Plex;
45280a6ba040SMatthew G. Knepley   dm->ops->coarsen                   = DMCoarsen_Plex;
45290a6ba040SMatthew G. Knepley   dm->ops->refinehierarchy           = DMRefineHierarchy_Plex;
4530b653a561SMatthew G. Knepley   dm->ops->coarsenhierarchy          = DMCoarsenHierarchy_Plex;
4531d410b0cfSMatthew G. Knepley   dm->ops->extrude                   = DMExtrude_Plex;
45320298fd71SBarry Smith   dm->ops->globaltolocalbegin        = NULL;
45330298fd71SBarry Smith   dm->ops->globaltolocalend          = NULL;
45340298fd71SBarry Smith   dm->ops->localtoglobalbegin        = NULL;
45350298fd71SBarry Smith   dm->ops->localtoglobalend          = NULL;
4536552f7358SJed Brown   dm->ops->destroy                   = DMDestroy_Plex;
4537552f7358SJed Brown   dm->ops->createsubdm               = DMCreateSubDM_Plex;
45382adcc780SMatthew G. Knepley   dm->ops->createsuperdm             = DMCreateSuperDM_Plex;
4539793f3fe5SMatthew G. Knepley   dm->ops->getdimpoints              = DMGetDimPoints_Plex;
4540552f7358SJed Brown   dm->ops->locatepoints              = DMLocatePoints_Plex;
45410709b2feSToby Isaac   dm->ops->projectfunctionlocal      = DMProjectFunctionLocal_Plex;
45420709b2feSToby Isaac   dm->ops->projectfunctionlabellocal = DMProjectFunctionLabelLocal_Plex;
4543bfc4295aSToby Isaac   dm->ops->projectfieldlocal         = DMProjectFieldLocal_Plex;
45448c6c5593SMatthew G. Knepley   dm->ops->projectfieldlabellocal    = DMProjectFieldLabelLocal_Plex;
4545ece3a9fcSMatthew G. Knepley   dm->ops->projectbdfieldlabellocal  = DMProjectBdFieldLabelLocal_Plex;
45460709b2feSToby Isaac   dm->ops->computel2diff             = DMComputeL2Diff_Plex;
4547b698f381SToby Isaac   dm->ops->computel2gradientdiff     = DMComputeL2GradientDiff_Plex;
45482a16baeaSToby Isaac   dm->ops->computel2fielddiff        = DMComputeL2FieldDiff_Plex;
454928d58a37SPierre Jolivet   dm->ops->getneighbors              = DMGetNeighbors_Plex;
45509566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertBoundaryValues_C", DMPlexInsertBoundaryValues_Plex));
45519566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertTimeDerviativeBoundaryValues_C", DMPlexInsertTimeDerivativeBoundaryValues_Plex));
45529566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMSetUpGLVisViewer_C", DMSetUpGLVisViewer_Plex));
45539566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMCreateNeumannOverlap_C", DMCreateNeumannOverlap_Plex));
45549566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetOverlap_C", DMPlexGetOverlap_Plex));
45559566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeGetDefault_C", DMPlexDistributeGetDefault_Plex));
45569566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeSetDefault_C", DMPlexDistributeSetDefault_Plex));
45576bc1bd01Sksagiyam   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderGetDefault_C", DMPlexReorderGetDefault_Plex));
45586bc1bd01Sksagiyam   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderSetDefault_C", DMPlexReorderSetDefault_Plex));
45599566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMInterpolateSolution_C", DMInterpolateSolution_Plex));
4560c506a872SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetOverlap_C", DMPlexGetOverlap_Plex));
4561c506a872SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexSetOverlap_C", DMPlexSetOverlap_Plex));
4562d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetUseCeed_C", DMPlexGetUseCeed_Plex));
4563d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexSetUseCeed_C", DMPlexSetUseCeed_Plex));
45643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4565552f7358SJed Brown }
4566552f7358SJed Brown 
4567d71ae5a4SJacob Faibussowitsch PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm)
4568d71ae5a4SJacob Faibussowitsch {
456963a16f15SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
45706725e60dSJed Brown   PetscSF  face_sf;
457163a16f15SMatthew G. Knepley 
457263a16f15SMatthew G. Knepley   PetscFunctionBegin;
457363a16f15SMatthew G. Knepley   mesh->refct++;
457463a16f15SMatthew G. Knepley   (*newdm)->data = mesh;
45755f06a3ddSJed Brown   PetscCall(DMPlexGetIsoperiodicFaceSF(dm, &face_sf));
45765f06a3ddSJed Brown   PetscCall(DMPlexSetIsoperiodicFaceSF(*newdm, face_sf));
45779566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)*newdm, DMPLEX));
45789566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(*newdm));
45793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
458063a16f15SMatthew G. Knepley }
458163a16f15SMatthew G. Knepley 
45828818961aSMatthew G Knepley /*MC
4583a1cb98faSBarry Smith   DMPLEX = "plex" - A `DM` object that encapsulates an unstructured mesh, or CW Complex, which can be expressed using a Hasse Diagram.
458420f4b53cSBarry Smith                     In the local representation, `Vec`s contain all unknowns in the interior and shared boundary. This is
45858818961aSMatthew G Knepley                     specified by a PetscSection object. Ownership in the global representation is determined by
4586a1cb98faSBarry Smith                     ownership of the underlying `DMPLEX` points. This is specified by another `PetscSection` object.
45878818961aSMatthew G Knepley 
4588e5893cccSMatthew G. Knepley   Options Database Keys:
4589250712c9SMatthew G. Knepley + -dm_refine_pre                     - Refine mesh before distribution
4590250712c9SMatthew G. Knepley + -dm_refine_uniform_pre             - Choose uniform or generator-based refinement
4591250712c9SMatthew G. Knepley + -dm_refine_volume_limit_pre        - Cell volume limit after pre-refinement using generator
4592250712c9SMatthew G. Knepley . -dm_distribute                     - Distribute mesh across processes
4593250712c9SMatthew G. Knepley . -dm_distribute_overlap             - Number of cells to overlap for distribution
4594250712c9SMatthew G. Knepley . -dm_refine                         - Refine mesh after distribution
4595250712c9SMatthew G. Knepley . -dm_plex_hash_location             - Use grid hashing for point location
4596ddce0771SMatthew G. Knepley . -dm_plex_hash_box_faces <n,m,p>    - The number of divisions in each direction of the grid hash
4597f12cf164SMatthew G. Knepley . -dm_plex_partition_balance         - Attempt to evenly divide points on partition boundary between processes
4598f12cf164SMatthew G. Knepley . -dm_plex_remesh_bd                 - Allow changes to the boundary on remeshing
4599d5b43468SJose E. Roman . -dm_plex_max_projection_height     - Maximum mesh point height used to project locally
4600f12cf164SMatthew G. Knepley . -dm_plex_regular_refinement        - Use special nested projection algorithm for regular refinement
4601aaa8cc7dSPierre Jolivet . -dm_plex_check_all                 - Perform all checks below
4602f12cf164SMatthew G. Knepley . -dm_plex_check_symmetry            - Check that the adjacency information in the mesh is symmetric
4603f12cf164SMatthew G. Knepley . -dm_plex_check_skeleton <celltype> - Check that each cell has the correct number of vertices
4604f12cf164SMatthew 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
4605f12cf164SMatthew G. Knepley . -dm_plex_check_geometry            - Check that cells have positive volume
4606f12cf164SMatthew G. Knepley . -dm_view :mesh.tex:ascii_latex     - View the mesh in LaTeX/TikZ
4607e5893cccSMatthew G. Knepley . -dm_plex_view_scale <num>          - Scale the TikZ
4608e5893cccSMatthew G. Knepley - -dm_plex_print_fem <num>           - View FEM assembly information, such as element vectors and matrices
4609e5893cccSMatthew G. Knepley 
46108818961aSMatthew G Knepley   Level: intermediate
46118818961aSMatthew G Knepley 
46121cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMType`, `DMPlexCreate()`, `DMCreate()`, `DMSetType()`, `PetscSection`
46138818961aSMatthew G Knepley M*/
46148818961aSMatthew G Knepley 
4615d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode DMCreate_Plex(DM dm)
4616d71ae5a4SJacob Faibussowitsch {
4617552f7358SJed Brown   DM_Plex *mesh;
4618412e9a14SMatthew G. Knepley   PetscInt unit;
4619552f7358SJed Brown 
4620552f7358SJed Brown   PetscFunctionBegin;
4621f39ec787SMatthew G. Knepley   PetscCall(PetscCitationsRegister(PlexCitation, &Plexcite));
4622552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46234dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&mesh));
4624552f7358SJed Brown   dm->data = mesh;
4625552f7358SJed Brown 
4626552f7358SJed Brown   mesh->refct = 1;
46279566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->coneSection));
46289566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->supportSection));
4629552f7358SJed Brown   mesh->refinementUniform      = PETSC_TRUE;
4630552f7358SJed Brown   mesh->refinementLimit        = -1.0;
4631e600fa54SMatthew G. Knepley   mesh->distDefault            = PETSC_TRUE;
46326bc1bd01Sksagiyam   mesh->reorderDefault         = DMPLEX_REORDER_DEFAULT_NOTSET;
46331d1f2f2aSksagiyam   mesh->distributionName       = NULL;
46347d0f5628SVaclav Hapla   mesh->interpolated           = DMPLEX_INTERPOLATED_INVALID;
46357d0f5628SVaclav Hapla   mesh->interpolatedCollective = DMPLEX_INTERPOLATED_INVALID;
4636552f7358SJed Brown 
46379566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerCreate(PetscObjectComm((PetscObject)dm), &mesh->partitioner));
46382e62ab5aSMatthew G. Knepley   mesh->remeshBd = PETSC_FALSE;
4639d9deefdfSMatthew G. Knepley 
46408865f1eaSKarl Rupp   for (unit = 0; unit < NUM_PETSC_UNITS; ++unit) mesh->scale[unit] = 1.0;
4641552f7358SJed Brown 
4642df0420ecSMatthew G. Knepley   mesh->depthState    = -1;
4643ba2698f1SMatthew G. Knepley   mesh->celltypeState = -1;
46446113b454SMatthew G. Knepley   mesh->printTol      = 1.0e-10;
4645552f7358SJed Brown 
46469566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(dm));
46473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4648552f7358SJed Brown }
4649552f7358SJed Brown 
4650552f7358SJed Brown /*@
4651a1cb98faSBarry Smith   DMPlexCreate - Creates a `DMPLEX` object, which encapsulates an unstructured mesh, or CW complex, which can be expressed using a Hasse Diagram.
4652552f7358SJed Brown 
4653d083f849SBarry Smith   Collective
4654552f7358SJed Brown 
4655552f7358SJed Brown   Input Parameter:
4656a1cb98faSBarry Smith . comm - The communicator for the `DMPLEX` object
4657552f7358SJed Brown 
4658552f7358SJed Brown   Output Parameter:
4659a1cb98faSBarry Smith . mesh - The `DMPLEX` object
4660552f7358SJed Brown 
4661552f7358SJed Brown   Level: beginner
4662552f7358SJed Brown 
466342747ad1SJacob Faibussowitsch .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMType`, `DMCreate()`, `DMSetType()`
4664552f7358SJed Brown @*/
4665d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreate(MPI_Comm comm, DM *mesh)
4666d71ae5a4SJacob Faibussowitsch {
4667552f7358SJed Brown   PetscFunctionBegin;
46684f572ea9SToby Isaac   PetscAssertPointer(mesh, 2);
46699566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, mesh));
46709566063dSJacob Faibussowitsch   PetscCall(DMSetType(*mesh, DMPLEX));
46713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4672552f7358SJed Brown }
4673552f7358SJed Brown 
4674b09969d6SVaclav Hapla /*@C
4675a1cb98faSBarry Smith   DMPlexBuildFromCellListParallel - Build distributed `DMPLEX` topology from a list of vertices for each cell (common mesh generator output)
4676a1cb98faSBarry Smith 
467720f4b53cSBarry Smith   Collective; No Fortran Support
4678b09969d6SVaclav Hapla 
4679b09969d6SVaclav Hapla   Input Parameters:
4680a1cb98faSBarry Smith + dm          - The `DM`
4681b09969d6SVaclav Hapla . numCells    - The number of cells owned by this process
4682a1cb98faSBarry Smith . numVertices - The number of vertices to be owned by this process, or `PETSC_DECIDE`
4683a1cb98faSBarry Smith . NVertices   - The global number of vertices, or `PETSC_DETERMINE`
4684b09969d6SVaclav Hapla . numCorners  - The number of vertices for each cell
46855e488331SVaclav Hapla - cells       - An array of numCells*numCorners numbers, the global vertex numbers for each cell
4686b09969d6SVaclav Hapla 
4687be8c289dSNicolas Barral   Output Parameters:
4688a1cb98faSBarry Smith + vertexSF         - (Optional) `PetscSF` describing complete vertex ownership
4689be8c289dSNicolas Barral - verticesAdjSaved - (Optional) vertex adjacency array
4690b09969d6SVaclav Hapla 
4691b09969d6SVaclav Hapla   Level: advanced
4692b09969d6SVaclav Hapla 
4693a1cb98faSBarry Smith   Notes:
4694a1cb98faSBarry Smith   Two triangles sharing a face
4695a1cb98faSBarry Smith .vb
4696a1cb98faSBarry Smith 
4697a1cb98faSBarry Smith         2
4698a1cb98faSBarry Smith       / | \
4699a1cb98faSBarry Smith      /  |  \
4700a1cb98faSBarry Smith     /   |   \
4701a1cb98faSBarry Smith    0  0 | 1  3
4702a1cb98faSBarry Smith     \   |   /
4703a1cb98faSBarry Smith      \  |  /
4704a1cb98faSBarry Smith       \ | /
4705a1cb98faSBarry Smith         1
4706a1cb98faSBarry Smith .ve
4707a1cb98faSBarry Smith   would have input
4708a1cb98faSBarry Smith .vb
4709a1cb98faSBarry Smith   numCells = 2, numVertices = 4
4710a1cb98faSBarry Smith   cells = [0 1 2  1 3 2]
4711a1cb98faSBarry Smith .ve
4712a1cb98faSBarry Smith   which would result in the `DMPLEX`
4713a1cb98faSBarry Smith .vb
4714a1cb98faSBarry Smith 
4715a1cb98faSBarry Smith         4
4716a1cb98faSBarry Smith       / | \
4717a1cb98faSBarry Smith      /  |  \
4718a1cb98faSBarry Smith     /   |   \
4719a1cb98faSBarry Smith    2  0 | 1  5
4720a1cb98faSBarry Smith     \   |   /
4721a1cb98faSBarry Smith      \  |  /
4722a1cb98faSBarry Smith       \ | /
4723a1cb98faSBarry Smith         3
4724a1cb98faSBarry Smith .ve
4725a1cb98faSBarry Smith 
4726a1cb98faSBarry Smith   Vertices are implicitly numbered consecutively 0,...,NVertices.
4727a1cb98faSBarry Smith   Each rank owns a chunk of numVertices consecutive vertices.
4728a1cb98faSBarry Smith   If numVertices is `PETSC_DECIDE`, PETSc will distribute them as evenly as possible using PetscLayout.
4729a1cb98faSBarry Smith   If NVertices is `PETSC_DETERMINE` and numVertices is PETSC_DECIDE, NVertices is computed by PETSc as the maximum vertex index in cells + 1.
4730a1cb98faSBarry Smith   If only NVertices is `PETSC_DETERMINE`, it is computed as the sum of numVertices over all ranks.
4731a1cb98faSBarry Smith 
4732a1cb98faSBarry Smith   The cell distribution is arbitrary non-overlapping, independent of the vertex distribution.
4733a1cb98faSBarry Smith 
47341cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildFromCellList()`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildCoordinatesFromCellListParallel()`,
4735a1cb98faSBarry Smith           `PetscSF`
4736b09969d6SVaclav Hapla @*/
4737d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildFromCellListParallel(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt NVertices, PetscInt numCorners, const PetscInt cells[], PetscSF *vertexSF, PetscInt **verticesAdjSaved)
4738d71ae5a4SJacob Faibussowitsch {
47392464107aSksagiyam   PetscSF     sfPoint;
47402464107aSksagiyam   PetscLayout layout;
474182fb893eSVaclav Hapla   PetscInt    numVerticesAdj, *verticesAdj, *cones, c, p;
4742a47d0d45SMatthew G. Knepley 
4743a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
474425b6865aSVaclav Hapla   PetscValidLogicalCollectiveInt(dm, NVertices, 4);
47459566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
474625b6865aSVaclav Hapla   /* Get/check global number of vertices */
474725b6865aSVaclav Hapla   {
474825b6865aSVaclav Hapla     PetscInt       NVerticesInCells, i;
474925b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
475025b6865aSVaclav Hapla 
475125b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
475225b6865aSVaclav Hapla     NVerticesInCells = PETSC_MIN_INT;
47539371c9d4SSatish Balay     for (i = 0; i < len; i++)
47549371c9d4SSatish Balay       if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
475525b6865aSVaclav Hapla     ++NVerticesInCells;
4756712fec58SPierre Jolivet     PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &NVerticesInCells, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
475725b6865aSVaclav Hapla 
475825b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE && NVertices == PETSC_DECIDE) NVertices = NVerticesInCells;
47599371c9d4SSatish Balay     else
47609371c9d4SSatish Balay       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);
476125b6865aSVaclav Hapla   }
47629079aca8SVaclav Hapla   /* Count locally unique vertices */
47639079aca8SVaclav Hapla   {
47649079aca8SVaclav Hapla     PetscHSetI vhash;
47659079aca8SVaclav Hapla     PetscInt   off = 0;
47669079aca8SVaclav Hapla 
47679566063dSJacob Faibussowitsch     PetscCall(PetscHSetICreate(&vhash));
4768a47d0d45SMatthew G. Knepley     for (c = 0; c < numCells; ++c) {
476948a46eb9SPierre Jolivet       for (p = 0; p < numCorners; ++p) PetscCall(PetscHSetIAdd(vhash, cells[c * numCorners + p]));
4770a47d0d45SMatthew G. Knepley     }
47719566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetSize(vhash, &numVerticesAdj));
47729566063dSJacob Faibussowitsch     if (!verticesAdjSaved) PetscCall(PetscMalloc1(numVerticesAdj, &verticesAdj));
4773ad540459SPierre Jolivet     else verticesAdj = *verticesAdjSaved;
47749566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetElems(vhash, &off, verticesAdj));
47759566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&vhash));
477663a3b9bcSJacob Faibussowitsch     PetscCheck(off == numVerticesAdj, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid number of local vertices %" PetscInt_FMT " should be %" PetscInt_FMT, off, numVerticesAdj);
4777a47d0d45SMatthew G. Knepley   }
47789566063dSJacob Faibussowitsch   PetscCall(PetscSortInt(numVerticesAdj, verticesAdj));
4779a47d0d45SMatthew G. Knepley   /* Create cones */
47809566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, 0, numCells + numVerticesAdj));
47819566063dSJacob Faibussowitsch   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, numCorners));
47829566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm));
47839566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCones(dm, &cones));
4784a47d0d45SMatthew G. Knepley   for (c = 0; c < numCells; ++c) {
4785a47d0d45SMatthew G. Knepley     for (p = 0; p < numCorners; ++p) {
4786a47d0d45SMatthew G. Knepley       const PetscInt gv = cells[c * numCorners + p];
4787a47d0d45SMatthew G. Knepley       PetscInt       lv;
4788a47d0d45SMatthew G. Knepley 
47899079aca8SVaclav Hapla       /* Positions within verticesAdj form 0-based local vertex numbering;
47909079aca8SVaclav Hapla          we need to shift it by numCells to get correct DAG points (cells go first) */
47919566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv));
479263a3b9bcSJacob Faibussowitsch       PetscCheck(lv >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not find global vertex %" PetscInt_FMT " in local connectivity", gv);
4793961cfab0SVaclav Hapla       cones[c * numCorners + p] = lv + numCells;
4794a47d0d45SMatthew G. Knepley     }
4795a47d0d45SMatthew G. Knepley   }
47962464107aSksagiyam   /* Build point sf */
47979566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(PetscObjectComm((PetscObject)dm), &layout));
47989566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetSize(layout, NVertices));
47999566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, numVertices));
48009566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
48019566063dSJacob Faibussowitsch   PetscCall(PetscSFCreateByMatchingIndices(layout, numVerticesAdj, verticesAdj, NULL, numCells, numVerticesAdj, verticesAdj, NULL, numCells, vertexSF, &sfPoint));
48029566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
48039566063dSJacob Faibussowitsch   if (!verticesAdjSaved) PetscCall(PetscFree(verticesAdj));
48049566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)sfPoint, "point SF"));
48052464107aSksagiyam   if (dm->sf) {
48062464107aSksagiyam     const char *prefix;
48072464107aSksagiyam 
48089566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm->sf, &prefix));
48099566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)sfPoint, prefix));
48102464107aSksagiyam   }
48119566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dm, sfPoint));
48129566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&sfPoint));
48139566063dSJacob Faibussowitsch   if (vertexSF) PetscCall(PetscObjectSetName((PetscObject)(*vertexSF), "Vertex Ownership SF"));
4814a47d0d45SMatthew G. Knepley   /* Fill in the rest of the topology structure */
48159566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
48169566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
48179566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
48183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4819a47d0d45SMatthew G. Knepley }
4820a47d0d45SMatthew G. Knepley 
4821b09969d6SVaclav Hapla /*@C
4822a1cb98faSBarry Smith   DMPlexBuildCoordinatesFromCellListParallel - Build `DM` coordinates from a list of coordinates for each owned vertex (common mesh generator output)
4823a1cb98faSBarry Smith 
482420f4b53cSBarry Smith   Collective; No Fortran Support
4825b09969d6SVaclav Hapla 
4826b09969d6SVaclav Hapla   Input Parameters:
4827a1cb98faSBarry Smith + dm           - The `DM`
4828b09969d6SVaclav Hapla . spaceDim     - The spatial dimension used for coordinates
4829a1cb98faSBarry Smith . sfVert       - `PetscSF` describing complete vertex ownership
4830b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
4831b09969d6SVaclav Hapla 
4832b09969d6SVaclav Hapla   Level: advanced
4833b09969d6SVaclav Hapla 
48341cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildFromCellListParallel()`
4835b09969d6SVaclav Hapla @*/
4836d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildCoordinatesFromCellListParallel(DM dm, PetscInt spaceDim, PetscSF sfVert, const PetscReal vertexCoords[])
4837d71ae5a4SJacob Faibussowitsch {
4838a47d0d45SMatthew G. Knepley   PetscSection coordSection;
4839a47d0d45SMatthew G. Knepley   Vec          coordinates;
4840a47d0d45SMatthew G. Knepley   PetscScalar *coords;
48411edcf0b2SVaclav Hapla   PetscInt     numVertices, numVerticesAdj, coordSize, v, vStart, vEnd;
4842a47d0d45SMatthew G. Knepley 
4843a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
48449566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
48459566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
48461dca8a05SBarry Smith   PetscCheck(vStart >= 0 && vEnd >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
48479566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, spaceDim));
48489566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfVert, &numVertices, &numVerticesAdj, NULL, NULL));
48491dca8a05SBarry 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);
48509566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
48519566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
48529566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, spaceDim));
48539566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, vStart, vEnd));
48541edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
48559566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, spaceDim));
48569566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, spaceDim));
4857a47d0d45SMatthew G. Knepley   }
48589566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
48599566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
48609566063dSJacob Faibussowitsch   PetscCall(VecCreate(PetscObjectComm((PetscObject)dm), &coordinates));
48619566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, spaceDim));
48629566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
48639566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
48649566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
48659566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
4866a47d0d45SMatthew G. Knepley   {
4867a47d0d45SMatthew G. Knepley     MPI_Datatype coordtype;
4868a47d0d45SMatthew G. Knepley 
4869a47d0d45SMatthew G. Knepley     /* Need a temp buffer for coords if we have complex/single */
48709566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_contiguous(spaceDim, MPIU_SCALAR, &coordtype));
48719566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_commit(&coordtype));
487221016a8bSBarry Smith #if defined(PETSC_USE_COMPLEX)
487321016a8bSBarry Smith     {
487421016a8bSBarry Smith       PetscScalar *svertexCoords;
487521016a8bSBarry Smith       PetscInt     i;
48769566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numVertices * spaceDim, &svertexCoords));
48773612f820SVaclav Hapla       for (i = 0; i < numVertices * spaceDim; i++) svertexCoords[i] = vertexCoords[i];
48789566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(sfVert, coordtype, svertexCoords, coords, MPI_REPLACE));
48799566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(sfVert, coordtype, svertexCoords, coords, MPI_REPLACE));
48809566063dSJacob Faibussowitsch       PetscCall(PetscFree(svertexCoords));
488121016a8bSBarry Smith     }
488221016a8bSBarry Smith #else
48839566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sfVert, coordtype, vertexCoords, coords, MPI_REPLACE));
48849566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sfVert, coordtype, vertexCoords, coords, MPI_REPLACE));
488521016a8bSBarry Smith #endif
48869566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_free(&coordtype));
4887a47d0d45SMatthew G. Knepley   }
48889566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
48899566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
48909566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
48919566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
48923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4893a47d0d45SMatthew G. Knepley }
4894a47d0d45SMatthew G. Knepley 
4895c3edce3dSSatish Balay /*@
4896a1cb98faSBarry Smith   DMPlexCreateFromCellListParallelPetsc - Create distributed `DMPLEX` from a list of vertices for each cell (common mesh generator output)
4897a1cb98faSBarry Smith 
4898a1cb98faSBarry Smith   Collective
4899a47d0d45SMatthew G. Knepley 
4900a47d0d45SMatthew G. Knepley   Input Parameters:
4901a47d0d45SMatthew G. Knepley + comm         - The communicator
4902a47d0d45SMatthew G. Knepley . dim          - The topological dimension of the mesh
4903a47d0d45SMatthew G. Knepley . numCells     - The number of cells owned by this process
4904a1cb98faSBarry Smith . numVertices  - The number of vertices owned by this process, or `PETSC_DECIDE`
4905a1cb98faSBarry Smith . NVertices    - The global number of vertices, or `PETSC_DECIDE`
4906a47d0d45SMatthew G. Knepley . numCorners   - The number of vertices for each cell
4907a47d0d45SMatthew G. Knepley . interpolate  - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
4908a47d0d45SMatthew G. Knepley . cells        - An array of numCells*numCorners numbers, the global vertex numbers for each cell
4909a47d0d45SMatthew G. Knepley . spaceDim     - The spatial dimension used for coordinates
4910a47d0d45SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
4911a47d0d45SMatthew G. Knepley 
4912d8d19677SJose E. Roman   Output Parameters:
4913a1cb98faSBarry Smith + dm          - The `DM`
4914a1cb98faSBarry Smith . vertexSF    - (Optional) `PetscSF` describing complete vertex ownership
491560225df5SJacob Faibussowitsch - verticesAdj - (Optional) vertex adjacency array
4916a47d0d45SMatthew G. Knepley 
4917b09969d6SVaclav Hapla   Level: intermediate
4918a47d0d45SMatthew G. Knepley 
4919a1cb98faSBarry Smith   Notes:
4920a1cb98faSBarry Smith   This function is just a convenient sequence of `DMCreate()`, `DMSetType()`, `DMSetDimension()`,
4921a1cb98faSBarry Smith   `DMPlexBuildFromCellListParallel()`, `DMPlexInterpolate()`, `DMPlexBuildCoordinatesFromCellListParallel()`
4922a1cb98faSBarry Smith 
4923a1cb98faSBarry Smith   See `DMPlexBuildFromCellListParallel()` for an example and details about the topology-related parameters.
4924a1cb98faSBarry Smith 
4925a1cb98faSBarry Smith   See `DMPlexBuildCoordinatesFromCellListParallel()` for details about the geometry-related parameters.
4926a1cb98faSBarry Smith 
49271cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()`
4928a47d0d45SMatthew G. Knepley @*/
4929d71ae5a4SJacob Faibussowitsch 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)
4930d71ae5a4SJacob Faibussowitsch {
4931a47d0d45SMatthew G. Knepley   PetscSF sfVert;
4932a47d0d45SMatthew G. Knepley 
4933a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
49349566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
49359566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
4936a47d0d45SMatthew G. Knepley   PetscValidLogicalCollectiveInt(*dm, dim, 2);
4937064a246eSJacob Faibussowitsch   PetscValidLogicalCollectiveInt(*dm, spaceDim, 9);
49389566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
49399566063dSJacob Faibussowitsch   PetscCall(DMPlexBuildFromCellListParallel(*dm, numCells, numVertices, NVertices, numCorners, cells, &sfVert, verticesAdj));
4940a47d0d45SMatthew G. Knepley   if (interpolate) {
49415fd9971aSMatthew G. Knepley     DM idm;
4942a47d0d45SMatthew G. Knepley 
49439566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
49449566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
4945a47d0d45SMatthew G. Knepley     *dm = idm;
4946a47d0d45SMatthew G. Knepley   }
49479566063dSJacob Faibussowitsch   PetscCall(DMPlexBuildCoordinatesFromCellListParallel(*dm, spaceDim, sfVert, vertexCoords));
494818d54ad4SMichael Lange   if (vertexSF) *vertexSF = sfVert;
49499566063dSJacob Faibussowitsch   else PetscCall(PetscSFDestroy(&sfVert));
49503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4951a47d0d45SMatthew G. Knepley }
4952a47d0d45SMatthew G. Knepley 
4953b09969d6SVaclav Hapla /*@C
4954a1cb98faSBarry Smith   DMPlexBuildFromCellList - Build `DMPLEX` topology from a list of vertices for each cell (common mesh generator output)
4955a1cb98faSBarry Smith 
495620f4b53cSBarry Smith   Collective; No Fortran Support
49579298eaa6SMatthew G Knepley 
49589298eaa6SMatthew G Knepley   Input Parameters:
4959a1cb98faSBarry Smith + dm          - The `DM`
4960b09969d6SVaclav Hapla . numCells    - The number of cells owned by this process
4961a1cb98faSBarry Smith . numVertices - The number of vertices owned by this process, or `PETSC_DETERMINE`
49629298eaa6SMatthew G Knepley . numCorners  - The number of vertices for each cell
49635e488331SVaclav Hapla - cells       - An array of numCells*numCorners numbers, the global vertex numbers for each cell
49649298eaa6SMatthew G Knepley 
4965b09969d6SVaclav Hapla   Level: advanced
49669298eaa6SMatthew G Knepley 
4967b09969d6SVaclav Hapla   Notes:
4968b09969d6SVaclav Hapla   Two triangles sharing a face
4969a1cb98faSBarry Smith .vb
49709298eaa6SMatthew G Knepley 
4971a1cb98faSBarry Smith         2
4972a1cb98faSBarry Smith       / | \
4973a1cb98faSBarry Smith      /  |  \
4974a1cb98faSBarry Smith     /   |   \
4975a1cb98faSBarry Smith    0  0 | 1  3
4976a1cb98faSBarry Smith     \   |   /
4977a1cb98faSBarry Smith      \  |  /
4978a1cb98faSBarry Smith       \ | /
4979a1cb98faSBarry Smith         1
4980a1cb98faSBarry Smith .ve
4981a1cb98faSBarry Smith   would have input
4982a1cb98faSBarry Smith .vb
4983a1cb98faSBarry Smith   numCells = 2, numVertices = 4
4984a1cb98faSBarry Smith   cells = [0 1 2  1 3 2]
4985a1cb98faSBarry Smith .ve
4986a1cb98faSBarry Smith   which would result in the `DMPLEX`
4987a1cb98faSBarry Smith .vb
4988a1cb98faSBarry Smith 
4989a1cb98faSBarry Smith         4
4990a1cb98faSBarry Smith       / | \
4991a1cb98faSBarry Smith      /  |  \
4992a1cb98faSBarry Smith     /   |   \
4993a1cb98faSBarry Smith    2  0 | 1  5
4994a1cb98faSBarry Smith     \   |   /
4995a1cb98faSBarry Smith      \  |  /
4996a1cb98faSBarry Smith       \ | /
4997a1cb98faSBarry Smith         3
4998a1cb98faSBarry Smith .ve
4999a1cb98faSBarry Smith 
5000a1cb98faSBarry Smith   If numVertices is `PETSC_DETERMINE`, it is computed by PETSc as the maximum vertex index in cells + 1.
500125b6865aSVaclav Hapla 
50021cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromCellListPetsc()`
5003b09969d6SVaclav Hapla @*/
5004d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildFromCellList(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const PetscInt cells[])
5005d71ae5a4SJacob Faibussowitsch {
5006961cfab0SVaclav Hapla   PetscInt *cones, c, p, dim;
5007b09969d6SVaclav Hapla 
5008b09969d6SVaclav Hapla   PetscFunctionBegin;
50099566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
50109566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
501125b6865aSVaclav Hapla   /* Get/check global number of vertices */
501225b6865aSVaclav Hapla   {
501325b6865aSVaclav Hapla     PetscInt       NVerticesInCells, i;
501425b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
501525b6865aSVaclav Hapla 
501625b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
501725b6865aSVaclav Hapla     NVerticesInCells = PETSC_MIN_INT;
50189371c9d4SSatish Balay     for (i = 0; i < len; i++)
50199371c9d4SSatish Balay       if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
502025b6865aSVaclav Hapla     ++NVerticesInCells;
502125b6865aSVaclav Hapla 
502225b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE) numVertices = NVerticesInCells;
50239371c9d4SSatish Balay     else
50249371c9d4SSatish Balay       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);
502525b6865aSVaclav Hapla   }
50269566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
502748a46eb9SPierre Jolivet   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, numCorners));
50289566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm));
50299566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCones(dm, &cones));
5030b09969d6SVaclav Hapla   for (c = 0; c < numCells; ++c) {
5031ad540459SPierre Jolivet     for (p = 0; p < numCorners; ++p) cones[c * numCorners + p] = cells[c * numCorners + p] + numCells;
5032b09969d6SVaclav Hapla   }
50339566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
50349566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
50359566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
50363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5037b09969d6SVaclav Hapla }
5038b09969d6SVaclav Hapla 
5039b09969d6SVaclav Hapla /*@C
5040a1cb98faSBarry Smith   DMPlexBuildCoordinatesFromCellList - Build `DM` coordinates from a list of coordinates for each owned vertex (common mesh generator output)
5041a1cb98faSBarry Smith 
504220f4b53cSBarry Smith   Collective; No Fortran Support
5043b09969d6SVaclav Hapla 
5044b09969d6SVaclav Hapla   Input Parameters:
5045a1cb98faSBarry Smith + dm           - The `DM`
5046b09969d6SVaclav Hapla . spaceDim     - The spatial dimension used for coordinates
5047b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
5048b09969d6SVaclav Hapla 
5049b09969d6SVaclav Hapla   Level: advanced
5050b09969d6SVaclav Hapla 
50511cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellList()`
5052b09969d6SVaclav Hapla @*/
5053d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildCoordinatesFromCellList(DM dm, PetscInt spaceDim, const PetscReal vertexCoords[])
5054d71ae5a4SJacob Faibussowitsch {
5055b09969d6SVaclav Hapla   PetscSection coordSection;
5056b09969d6SVaclav Hapla   Vec          coordinates;
5057b09969d6SVaclav Hapla   DM           cdm;
5058b09969d6SVaclav Hapla   PetscScalar *coords;
50591edcf0b2SVaclav Hapla   PetscInt     v, vStart, vEnd, d;
5060b09969d6SVaclav Hapla 
5061b09969d6SVaclav Hapla   PetscFunctionBegin;
50629566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
50639566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
50641dca8a05SBarry Smith   PetscCheck(vStart >= 0 && vEnd >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
50659566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, spaceDim));
50669566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
50679566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
50689566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, spaceDim));
50699566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, vStart, vEnd));
50701edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
50719566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, spaceDim));
50729566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, spaceDim));
5073b09969d6SVaclav Hapla   }
50749566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
5075b09969d6SVaclav Hapla 
50769566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
50779566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(cdm, &coordinates));
50789566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, spaceDim));
50799566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
50809566063dSJacob Faibussowitsch   PetscCall(VecGetArrayWrite(coordinates, &coords));
50811edcf0b2SVaclav Hapla   for (v = 0; v < vEnd - vStart; ++v) {
5082ad540459SPierre Jolivet     for (d = 0; d < spaceDim; ++d) coords[v * spaceDim + d] = vertexCoords[v * spaceDim + d];
5083b09969d6SVaclav Hapla   }
50849566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayWrite(coordinates, &coords));
50859566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
50869566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
50879566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
50883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5089b09969d6SVaclav Hapla }
5090b09969d6SVaclav Hapla 
5091b09969d6SVaclav Hapla /*@
5092a1cb98faSBarry Smith   DMPlexCreateFromCellListPetsc - Create `DMPLEX` from a list of vertices for each cell (common mesh generator output), but only process 0 takes in the input
50933df08285SMatthew G. Knepley 
5094a1cb98faSBarry Smith   Collective
5095b09969d6SVaclav Hapla 
5096b09969d6SVaclav Hapla   Input Parameters:
5097b09969d6SVaclav Hapla + comm         - The communicator
5098b09969d6SVaclav Hapla . dim          - The topological dimension of the mesh
50993df08285SMatthew G. Knepley . numCells     - The number of cells, only on process 0
5100a1cb98faSBarry Smith . numVertices  - The number of vertices owned by this process, or `PETSC_DECIDE`, only on process 0
51013df08285SMatthew G. Knepley . numCorners   - The number of vertices for each cell, only on process 0
5102b09969d6SVaclav Hapla . interpolate  - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
51033df08285SMatthew G. Knepley . cells        - An array of numCells*numCorners numbers, the vertices for each cell, only on process 0
5104b09969d6SVaclav Hapla . spaceDim     - The spatial dimension used for coordinates
51053df08285SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex, only on process 0
5106b09969d6SVaclav Hapla 
5107b09969d6SVaclav Hapla   Output Parameter:
5108a1cb98faSBarry Smith . dm - The `DM`, which only has points on process 0
510925b6865aSVaclav Hapla 
5110b09969d6SVaclav Hapla   Level: intermediate
5111b09969d6SVaclav Hapla 
5112a1cb98faSBarry Smith   Notes:
5113a1cb98faSBarry Smith   This function is just a convenient sequence of `DMCreate()`, `DMSetType()`, `DMSetDimension()`, `DMPlexBuildFromCellList()`,
5114a1cb98faSBarry Smith   `DMPlexInterpolate()`, `DMPlexBuildCoordinatesFromCellList()`
5115a1cb98faSBarry Smith 
5116a1cb98faSBarry Smith   See `DMPlexBuildFromCellList()` for an example and details about the topology-related parameters.
5117a1cb98faSBarry Smith   See `DMPlexBuildCoordinatesFromCellList()` for details about the geometry-related parameters.
5118a1cb98faSBarry Smith   See `DMPlexCreateFromCellListParallelPetsc()` for parallel input
5119a1cb98faSBarry Smith 
51201cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildFromCellList()`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()`
51219298eaa6SMatthew G Knepley @*/
5122d71ae5a4SJacob 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)
5123d71ae5a4SJacob Faibussowitsch {
51243df08285SMatthew G. Knepley   PetscMPIInt rank;
51259298eaa6SMatthew G Knepley 
51269298eaa6SMatthew G Knepley   PetscFunctionBegin;
512728b400f6SJacob 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.");
51289566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
51299566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
51309566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
51319566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
5132c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildFromCellList(*dm, numCells, numVertices, numCorners, cells));
51339566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildFromCellList(*dm, 0, 0, 0, NULL));
51349298eaa6SMatthew G Knepley   if (interpolate) {
51355fd9971aSMatthew G. Knepley     DM idm;
51369298eaa6SMatthew G Knepley 
51379566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
51389566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
51399298eaa6SMatthew G Knepley     *dm = idm;
51409298eaa6SMatthew G Knepley   }
5141c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, vertexCoords));
51429566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, NULL));
51433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
51449298eaa6SMatthew G Knepley }
51459298eaa6SMatthew G Knepley 
5146939f6067SMatthew G. Knepley /*@
514720f4b53cSBarry Smith   DMPlexCreateFromDAG - This takes as input the adjacency-list representation of the Directed Acyclic Graph (Hasse Diagram) encoding a mesh, and produces a `DM`
5148939f6067SMatthew G. Knepley 
5149939f6067SMatthew G. Knepley   Input Parameters:
515020f4b53cSBarry Smith + dm               - The empty `DM` object, usually from `DMCreate()` and `DMSetDimension()`
5151939f6067SMatthew G. Knepley . depth            - The depth of the DAG
515220f4b53cSBarry Smith . numPoints        - Array of size depth + 1 containing the number of points at each `depth`
5153939f6067SMatthew G. Knepley . coneSize         - The cone size of each point
5154939f6067SMatthew G. Knepley . cones            - The concatenation of the cone points for each point, the cone list must be oriented correctly for each point
5155939f6067SMatthew G. Knepley . coneOrientations - The orientation of each cone point
515620f4b53cSBarry Smith - vertexCoords     - An array of `numPoints`[0]*spacedim numbers representing the coordinates of each vertex, with spacedim the value set via `DMSetCoordinateDim()`
5157939f6067SMatthew G. Knepley 
5158939f6067SMatthew G. Knepley   Output Parameter:
515920f4b53cSBarry Smith . dm - The `DM`
516020f4b53cSBarry Smith 
516120f4b53cSBarry Smith   Level: advanced
5162939f6067SMatthew G. Knepley 
5163a1cb98faSBarry Smith   Note:
5164a1cb98faSBarry Smith   Two triangles sharing a face would have input
5165a1cb98faSBarry Smith .vb
5166a1cb98faSBarry Smith   depth = 1, numPoints = [4 2], coneSize = [3 3 0 0 0 0]
5167a1cb98faSBarry Smith   cones = [2 3 4  3 5 4], coneOrientations = [0 0 0  0 0 0]
5168a1cb98faSBarry Smith  vertexCoords = [-1.0 0.0  0.0 -1.0  0.0 1.0  1.0 0.0]
5169a1cb98faSBarry Smith .ve
5170939f6067SMatthew G. Knepley   which would result in the DMPlex
5171a1cb98faSBarry Smith .vb
5172a1cb98faSBarry Smith         4
5173a1cb98faSBarry Smith       / | \
5174a1cb98faSBarry Smith      /  |  \
5175a1cb98faSBarry Smith     /   |   \
5176a1cb98faSBarry Smith    2  0 | 1  5
5177a1cb98faSBarry Smith     \   |   /
5178a1cb98faSBarry Smith      \  |  /
5179a1cb98faSBarry Smith       \ | /
5180a1cb98faSBarry Smith         3
5181a1cb98faSBarry Smith .ve
5182a1cb98faSBarry Smith   Notice that all points are numbered consecutively, unlike `DMPlexCreateFromCellListPetsc()`
5183939f6067SMatthew G. Knepley 
51841cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`
5185939f6067SMatthew G. Knepley @*/
5186d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateFromDAG(DM dm, PetscInt depth, const PetscInt numPoints[], const PetscInt coneSize[], const PetscInt cones[], const PetscInt coneOrientations[], const PetscScalar vertexCoords[])
5187d71ae5a4SJacob Faibussowitsch {
51889298eaa6SMatthew G Knepley   Vec          coordinates;
51899298eaa6SMatthew G Knepley   PetscSection coordSection;
51909298eaa6SMatthew G Knepley   PetscScalar *coords;
5191811e8653SToby Isaac   PetscInt     coordSize, firstVertex = -1, pStart = 0, pEnd = 0, p, v, dim, dimEmbed, d, off;
51929298eaa6SMatthew G Knepley 
51939298eaa6SMatthew G Knepley   PetscFunctionBegin;
51949566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
51959566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dimEmbed));
519663a3b9bcSJacob Faibussowitsch   PetscCheck(dimEmbed >= dim, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Embedding dimension %" PetscInt_FMT " cannot be less than intrinsic dimension %" PetscInt_FMT, dimEmbed, dim);
51979298eaa6SMatthew G Knepley   for (d = 0; d <= depth; ++d) pEnd += numPoints[d];
51989566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, pStart, pEnd));
51999298eaa6SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
52009566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeSize(dm, p, coneSize[p - pStart]));
5201ad540459SPierre Jolivet     if (firstVertex < 0 && !coneSize[p - pStart]) firstVertex = p - pStart;
520297e052ccSToby Isaac   }
52031dca8a05SBarry Smith   PetscCheck(firstVertex >= 0 || !numPoints[0], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Expected %" PetscInt_FMT " vertices but could not find any", numPoints[0]);
52049566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm)); /* Allocate space for cones */
52059298eaa6SMatthew G Knepley   for (p = pStart, off = 0; p < pEnd; off += coneSize[p - pStart], ++p) {
52069566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(dm, p, &cones[off]));
52079566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeOrientation(dm, p, &coneOrientations[off]));
52089298eaa6SMatthew G Knepley   }
52099566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
52109566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
52119298eaa6SMatthew G Knepley   /* Build coordinates */
52129566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
52139566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
52149566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dimEmbed));
52159566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numPoints[0]));
52169298eaa6SMatthew G Knepley   for (v = firstVertex; v < firstVertex + numPoints[0]; ++v) {
52179566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, dimEmbed));
52189566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dimEmbed));
52199298eaa6SMatthew G Knepley   }
52209566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
52219566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
52229566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
52239566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
52249566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
52259566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, dimEmbed));
52269566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
52279318fe57SMatthew G. Knepley   if (vertexCoords) {
52289566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
52299298eaa6SMatthew G Knepley     for (v = 0; v < numPoints[0]; ++v) {
52309298eaa6SMatthew G Knepley       PetscInt off;
52319298eaa6SMatthew G Knepley 
52329566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, v + firstVertex, &off));
5233ad540459SPierre Jolivet       for (d = 0; d < dimEmbed; ++d) coords[off + d] = vertexCoords[v * dimEmbed + d];
52349298eaa6SMatthew G Knepley     }
52359318fe57SMatthew G. Knepley   }
52369566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
52379566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
52389566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
52393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
52409298eaa6SMatthew G Knepley }
52418415267dSToby Isaac 
5242a4e35b19SJacob Faibussowitsch /*
5243a1cb98faSBarry Smith   DMPlexCreateCellVertexFromFile - Create a `DMPLEX` mesh from a simple cell-vertex file.
5244a1cb98faSBarry Smith 
5245a1cb98faSBarry Smith   Collective
52468ca92349SMatthew G. Knepley 
52478ca92349SMatthew G. Knepley + comm        - The MPI communicator
52488ca92349SMatthew G. Knepley . filename    - Name of the .dat file
52498ca92349SMatthew G. Knepley - interpolate - Create faces and edges in the mesh
52508ca92349SMatthew G. Knepley 
52518ca92349SMatthew G. Knepley   Output Parameter:
5252a1cb98faSBarry Smith . dm  - The `DM` object representing the mesh
52538ca92349SMatthew G. Knepley 
52548ca92349SMatthew G. Knepley   Level: beginner
52558ca92349SMatthew G. Knepley 
5256a1cb98faSBarry Smith   Note:
5257a1cb98faSBarry Smith   The format is the simplest possible:
5258a1cb98faSBarry Smith .vb
5259a1cb98faSBarry Smith   Ne
5260a1cb98faSBarry Smith   v0 v1 ... vk
5261a1cb98faSBarry Smith   Nv
5262a1cb98faSBarry Smith   x y z marker
5263a1cb98faSBarry Smith .ve
5264a1cb98faSBarry Smith 
5265a1cb98faSBarry Smith   Developer Note:
5266a1cb98faSBarry Smith   Should use a `PetscViewer` not a filename
5267a1cb98faSBarry Smith 
52681cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromFile()`, `DMPlexCreateMedFromFile()`, `DMPlexCreateGmsh()`, `DMPlexCreate()`
5269a4e35b19SJacob Faibussowitsch */
5270ff6a9541SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCellVertexFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
5271d71ae5a4SJacob Faibussowitsch {
52728ca92349SMatthew G. Knepley   DMLabel      marker;
52738ca92349SMatthew G. Knepley   PetscViewer  viewer;
52748ca92349SMatthew G. Knepley   Vec          coordinates;
52758ca92349SMatthew G. Knepley   PetscSection coordSection;
52768ca92349SMatthew G. Knepley   PetscScalar *coords;
52778ca92349SMatthew G. Knepley   char         line[PETSC_MAX_PATH_LEN];
52788ca92349SMatthew G. Knepley   PetscInt     dim = 3, cdim = 3, coordSize, v, c, d;
52798ca92349SMatthew G. Knepley   PetscMPIInt  rank;
5280f8d5e320SMatthew G. Knepley   int          snum, Nv, Nc, Ncn, Nl;
52818ca92349SMatthew G. Knepley 
52828ca92349SMatthew G. Knepley   PetscFunctionBegin;
52839566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
52849566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, &viewer));
52859566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(viewer, PETSCVIEWERASCII));
52869566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetMode(viewer, FILE_MODE_READ));
52879566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetName(viewer, filename));
5288dd400576SPatrick Sanan   if (rank == 0) {
52899566063dSJacob Faibussowitsch     PetscCall(PetscViewerRead(viewer, line, 4, NULL, PETSC_STRING));
5290f8d5e320SMatthew G. Knepley     snum = sscanf(line, "%d %d %d %d", &Nc, &Nv, &Ncn, &Nl);
529108401ef6SPierre Jolivet     PetscCheck(snum == 4, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
529225ce1634SJed Brown   } else {
5293f8d5e320SMatthew G. Knepley     Nc = Nv = Ncn = Nl = 0;
52948ca92349SMatthew G. Knepley   }
52959566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
52969566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
52979566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(*dm, 0, Nc + Nv));
52989566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
52999566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*dm, cdim));
53008ca92349SMatthew G. Knepley   /* Read topology */
5301dd400576SPatrick Sanan   if (rank == 0) {
5302f8d5e320SMatthew G. Knepley     char     format[PETSC_MAX_PATH_LEN];
5303f8d5e320SMatthew G. Knepley     PetscInt cone[8];
53048ca92349SMatthew G. Knepley     int      vbuf[8], v;
53058ca92349SMatthew G. Knepley 
53069371c9d4SSatish Balay     for (c = 0; c < Ncn; ++c) {
53079371c9d4SSatish Balay       format[c * 3 + 0] = '%';
53089371c9d4SSatish Balay       format[c * 3 + 1] = 'd';
53099371c9d4SSatish Balay       format[c * 3 + 2] = ' ';
53109371c9d4SSatish Balay     }
5311f8d5e320SMatthew G. Knepley     format[Ncn * 3 - 1] = '\0';
53129566063dSJacob Faibussowitsch     for (c = 0; c < Nc; ++c) PetscCall(DMPlexSetConeSize(*dm, c, Ncn));
53139566063dSJacob Faibussowitsch     PetscCall(DMSetUp(*dm));
53148ca92349SMatthew G. Knepley     for (c = 0; c < Nc; ++c) {
53159566063dSJacob Faibussowitsch       PetscCall(PetscViewerRead(viewer, line, Ncn, NULL, PETSC_STRING));
5316f8d5e320SMatthew G. Knepley       switch (Ncn) {
5317d71ae5a4SJacob Faibussowitsch       case 2:
5318d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1]);
5319d71ae5a4SJacob Faibussowitsch         break;
5320d71ae5a4SJacob Faibussowitsch       case 3:
5321d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2]);
5322d71ae5a4SJacob Faibussowitsch         break;
5323d71ae5a4SJacob Faibussowitsch       case 4:
5324d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3]);
5325d71ae5a4SJacob Faibussowitsch         break;
5326d71ae5a4SJacob Faibussowitsch       case 6:
5327d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5]);
5328d71ae5a4SJacob Faibussowitsch         break;
5329d71ae5a4SJacob Faibussowitsch       case 8:
5330d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5], &vbuf[6], &vbuf[7]);
5331d71ae5a4SJacob Faibussowitsch         break;
5332d71ae5a4SJacob Faibussowitsch       default:
5333d71ae5a4SJacob Faibussowitsch         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No cell shape with %d vertices", Ncn);
5334f8d5e320SMatthew G. Knepley       }
533508401ef6SPierre Jolivet       PetscCheck(snum == Ncn, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
5336f8d5e320SMatthew G. Knepley       for (v = 0; v < Ncn; ++v) cone[v] = vbuf[v] + Nc;
53378ca92349SMatthew G. Knepley       /* Hexahedra are inverted */
5338f8d5e320SMatthew G. Knepley       if (Ncn == 8) {
53398ca92349SMatthew G. Knepley         PetscInt tmp = cone[1];
53408ca92349SMatthew G. Knepley         cone[1]      = cone[3];
53418ca92349SMatthew G. Knepley         cone[3]      = tmp;
53428ca92349SMatthew G. Knepley       }
53439566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(*dm, c, cone));
53448ca92349SMatthew G. Knepley     }
53458ca92349SMatthew G. Knepley   }
53469566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(*dm));
53479566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(*dm));
53488ca92349SMatthew G. Knepley   /* Read coordinates */
53499566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(*dm, &coordSection));
53509566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
53519566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, cdim));
53529566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, Nc, Nc + Nv));
53538ca92349SMatthew G. Knepley   for (v = Nc; v < Nc + Nv; ++v) {
53549566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, cdim));
53559566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, cdim));
53568ca92349SMatthew G. Knepley   }
53579566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
53589566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
53599566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
53609566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
53619566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
53629566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, cdim));
53639566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
53649566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
5365dd400576SPatrick Sanan   if (rank == 0) {
5366f8d5e320SMatthew G. Knepley     char   format[PETSC_MAX_PATH_LEN];
53678ca92349SMatthew G. Knepley     double x[3];
5368f8d5e320SMatthew G. Knepley     int    l, val[3];
53698ca92349SMatthew G. Knepley 
5370f8d5e320SMatthew G. Knepley     if (Nl) {
53719371c9d4SSatish Balay       for (l = 0; l < Nl; ++l) {
53729371c9d4SSatish Balay         format[l * 3 + 0] = '%';
53739371c9d4SSatish Balay         format[l * 3 + 1] = 'd';
53749371c9d4SSatish Balay         format[l * 3 + 2] = ' ';
53759371c9d4SSatish Balay       }
5376f8d5e320SMatthew G. Knepley       format[Nl * 3 - 1] = '\0';
53779566063dSJacob Faibussowitsch       PetscCall(DMCreateLabel(*dm, "marker"));
53789566063dSJacob Faibussowitsch       PetscCall(DMGetLabel(*dm, "marker", &marker));
5379f8d5e320SMatthew G. Knepley     }
53808ca92349SMatthew G. Knepley     for (v = 0; v < Nv; ++v) {
53819566063dSJacob Faibussowitsch       PetscCall(PetscViewerRead(viewer, line, 3 + Nl, NULL, PETSC_STRING));
5382f8d5e320SMatthew G. Knepley       snum = sscanf(line, "%lg %lg %lg", &x[0], &x[1], &x[2]);
538308401ef6SPierre Jolivet       PetscCheck(snum == 3, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
5384f8d5e320SMatthew G. Knepley       switch (Nl) {
5385d71ae5a4SJacob Faibussowitsch       case 0:
5386d71ae5a4SJacob Faibussowitsch         snum = 0;
5387d71ae5a4SJacob Faibussowitsch         break;
5388d71ae5a4SJacob Faibussowitsch       case 1:
5389d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &val[0]);
5390d71ae5a4SJacob Faibussowitsch         break;
5391d71ae5a4SJacob Faibussowitsch       case 2:
5392d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &val[0], &val[1]);
5393d71ae5a4SJacob Faibussowitsch         break;
5394d71ae5a4SJacob Faibussowitsch       case 3:
5395d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &val[0], &val[1], &val[2]);
5396d71ae5a4SJacob Faibussowitsch         break;
5397d71ae5a4SJacob Faibussowitsch       default:
5398d71ae5a4SJacob Faibussowitsch         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Request support for %d labels", Nl);
5399f8d5e320SMatthew G. Knepley       }
540008401ef6SPierre Jolivet       PetscCheck(snum == Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
54018ca92349SMatthew G. Knepley       for (d = 0; d < cdim; ++d) coords[v * cdim + d] = x[d];
54029566063dSJacob Faibussowitsch       for (l = 0; l < Nl; ++l) PetscCall(DMLabelSetValue(marker, v + Nc, val[l]));
54038ca92349SMatthew G. Knepley     }
54048ca92349SMatthew G. Knepley   }
54059566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
54069566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(*dm, coordinates));
54079566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
54089566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(&viewer));
54098ca92349SMatthew G. Knepley   if (interpolate) {
54108ca92349SMatthew G. Knepley     DM      idm;
54118ca92349SMatthew G. Knepley     DMLabel bdlabel;
54128ca92349SMatthew G. Knepley 
54139566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
54149566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
54158ca92349SMatthew G. Knepley     *dm = idm;
54168ca92349SMatthew G. Knepley 
5417f8d5e320SMatthew G. Knepley     if (!Nl) {
54189566063dSJacob Faibussowitsch       PetscCall(DMCreateLabel(*dm, "marker"));
54199566063dSJacob Faibussowitsch       PetscCall(DMGetLabel(*dm, "marker", &bdlabel));
54209566063dSJacob Faibussowitsch       PetscCall(DMPlexMarkBoundaryFaces(*dm, PETSC_DETERMINE, bdlabel));
54219566063dSJacob Faibussowitsch       PetscCall(DMPlexLabelComplete(*dm, bdlabel));
54228ca92349SMatthew G. Knepley     }
5423f8d5e320SMatthew G. Knepley   }
54243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
54258ca92349SMatthew G. Knepley }
54268ca92349SMatthew G. Knepley 
54278ca92349SMatthew G. Knepley /*@C
5428a1cb98faSBarry Smith   DMPlexCreateFromFile - This takes a filename and produces a `DM`
5429a1cb98faSBarry Smith 
5430a1cb98faSBarry Smith   Collective
5431ca522641SMatthew G. Knepley 
5432ca522641SMatthew G. Knepley   Input Parameters:
5433ca522641SMatthew G. Knepley + comm        - The communicator
5434ca522641SMatthew G. Knepley . filename    - A file name
5435a1cb98faSBarry Smith . plexname    - The object name of the resulting `DM`, also used for intra-datafile lookup by some formats
5436ca522641SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
5437ca522641SMatthew G. Knepley 
5438ca522641SMatthew G. Knepley   Output Parameter:
5439a1cb98faSBarry Smith . dm - The `DM`
5440ca522641SMatthew G. Knepley 
5441a1cb98faSBarry Smith   Options Database Key:
5442a1cb98faSBarry Smith . -dm_plex_create_from_hdf5_xdmf - use the `PETSC_VIEWER_HDF5_XDMF` format for reading HDF5
544302ef0d99SVaclav Hapla 
544437fdd005SBarry Smith   Use `-dm_plex_create_ prefix` to pass options to the internal `PetscViewer`, e.g.
5445bca97951SVaclav Hapla $ -dm_plex_create_viewer_hdf5_collective
5446bca97951SVaclav Hapla 
5447ca522641SMatthew G. Knepley   Level: beginner
5448ca522641SMatthew G. Knepley 
5449a1cb98faSBarry Smith   Notes:
5450a1cb98faSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
5451a1cb98faSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
5452a1cb98faSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
5453a1cb98faSBarry Smith   The input parameter name is thus used to name the `DMPLEX` object when `DMPlexCreateFromFile()` internally
5454a1cb98faSBarry Smith   calls `DMLoad()`. Currently, name is ignored for other viewer types and/or formats.
5455a1cb98faSBarry Smith 
54561cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromDAG()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`, `PetscObjectSetName()`, `DMView()`, `DMLoad()`
5457ca522641SMatthew G. Knepley @*/
5458d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateFromFile(MPI_Comm comm, const char filename[], const char plexname[], PetscBool interpolate, DM *dm)
5459d71ae5a4SJacob Faibussowitsch {
5460ef3a5affSJacob Faibussowitsch   const char  extGmsh[]      = ".msh";
5461ef3a5affSJacob Faibussowitsch   const char  extGmsh2[]     = ".msh2";
5462ef3a5affSJacob Faibussowitsch   const char  extGmsh4[]     = ".msh4";
5463ef3a5affSJacob Faibussowitsch   const char  extCGNS[]      = ".cgns";
5464ef3a5affSJacob Faibussowitsch   const char  extExodus[]    = ".exo";
5465ef3a5affSJacob Faibussowitsch   const char  extExodus_e[]  = ".e";
5466ef3a5affSJacob Faibussowitsch   const char  extGenesis[]   = ".gen";
5467ef3a5affSJacob Faibussowitsch   const char  extFluent[]    = ".cas";
5468ef3a5affSJacob Faibussowitsch   const char  extHDF5[]      = ".h5";
54696f2c871aSStefano Zampini   const char  extXDMFHDF5[]  = ".xdmf.h5";
5470ef3a5affSJacob Faibussowitsch   const char  extMed[]       = ".med";
5471ef3a5affSJacob Faibussowitsch   const char  extPLY[]       = ".ply";
5472ef3a5affSJacob Faibussowitsch   const char  extEGADSLite[] = ".egadslite";
5473ef3a5affSJacob Faibussowitsch   const char  extEGADS[]     = ".egads";
5474ef3a5affSJacob Faibussowitsch   const char  extIGES[]      = ".igs";
5475ef3a5affSJacob Faibussowitsch   const char  extSTEP[]      = ".stp";
5476ef3a5affSJacob Faibussowitsch   const char  extCV[]        = ".dat";
5477ca522641SMatthew G. Knepley   size_t      len;
54786f2c871aSStefano Zampini   PetscBool   isGmsh, isGmsh2, isGmsh4, isCGNS, isExodus, isGenesis, isFluent, isHDF5, isMed, isPLY, isEGADSLite, isEGADS, isIGES, isSTEP, isCV, isXDMFHDF5;
5479ca522641SMatthew G. Knepley   PetscMPIInt rank;
5480ca522641SMatthew G. Knepley 
5481ca522641SMatthew G. Knepley   PetscFunctionBegin;
54824f572ea9SToby Isaac   PetscAssertPointer(filename, 2);
54834f572ea9SToby Isaac   if (plexname) PetscAssertPointer(plexname, 3);
54844f572ea9SToby Isaac   PetscAssertPointer(dm, 5);
54859566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
54869566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_CreateFromFile, 0, 0, 0, 0));
54879566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
54889566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(filename, &len));
548928b400f6SJacob Faibussowitsch   PetscCheck(len, comm, PETSC_ERR_ARG_WRONG, "Filename must be a valid path");
5490ef3a5affSJacob Faibussowitsch 
54919371c9d4SSatish Balay #define CheckExtension(extension__, is_extension__) \
54929371c9d4SSatish Balay   do { \
5493274aaeaaSJacob Faibussowitsch     PetscAssert(sizeof(extension__), comm, PETSC_ERR_PLIB, "Zero-size extension: %s", extension__); \
5494274aaeaaSJacob Faibussowitsch     /* don't count the null-terminator at the end */ \
5495274aaeaaSJacob Faibussowitsch     const size_t ext_len = sizeof(extension__) - 1; \
5496274aaeaaSJacob Faibussowitsch     if (len < ext_len) { \
5497ef3a5affSJacob Faibussowitsch       is_extension__ = PETSC_FALSE; \
5498ef3a5affSJacob Faibussowitsch     } else { \
5499274aaeaaSJacob Faibussowitsch       PetscCall(PetscStrncmp(filename + len - ext_len, extension__, ext_len, &is_extension__)); \
5500ef3a5affSJacob Faibussowitsch     } \
5501ef3a5affSJacob Faibussowitsch   } while (0)
5502ef3a5affSJacob Faibussowitsch 
5503ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh, isGmsh);
5504ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh2, isGmsh2);
5505ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh4, isGmsh4);
5506ef3a5affSJacob Faibussowitsch   CheckExtension(extCGNS, isCGNS);
5507ef3a5affSJacob Faibussowitsch   CheckExtension(extExodus, isExodus);
5508ef3a5affSJacob Faibussowitsch   if (!isExodus) CheckExtension(extExodus_e, isExodus);
5509ef3a5affSJacob Faibussowitsch   CheckExtension(extGenesis, isGenesis);
5510ef3a5affSJacob Faibussowitsch   CheckExtension(extFluent, isFluent);
5511ef3a5affSJacob Faibussowitsch   CheckExtension(extHDF5, isHDF5);
5512ef3a5affSJacob Faibussowitsch   CheckExtension(extMed, isMed);
5513ef3a5affSJacob Faibussowitsch   CheckExtension(extPLY, isPLY);
5514ef3a5affSJacob Faibussowitsch   CheckExtension(extEGADSLite, isEGADSLite);
5515ef3a5affSJacob Faibussowitsch   CheckExtension(extEGADS, isEGADS);
5516ef3a5affSJacob Faibussowitsch   CheckExtension(extIGES, isIGES);
5517ef3a5affSJacob Faibussowitsch   CheckExtension(extSTEP, isSTEP);
5518ef3a5affSJacob Faibussowitsch   CheckExtension(extCV, isCV);
55196f2c871aSStefano Zampini   CheckExtension(extXDMFHDF5, isXDMFHDF5);
5520ef3a5affSJacob Faibussowitsch 
5521ef3a5affSJacob Faibussowitsch #undef CheckExtension
5522ef3a5affSJacob Faibussowitsch 
5523de78e4feSLisandro Dalcin   if (isGmsh || isGmsh2 || isGmsh4) {
55249566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateGmshFromFile(comm, filename, interpolate, dm));
5525ca522641SMatthew G. Knepley   } else if (isCGNS) {
55269566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCGNSFromFile(comm, filename, interpolate, dm));
552790c68965SMatthew G. Knepley   } else if (isExodus || isGenesis) {
55289566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateExodusFromFile(comm, filename, interpolate, dm));
55292f0bd6dcSMichael Lange   } else if (isFluent) {
55309566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFluentFromFile(comm, filename, interpolate, dm));
5531cc2f8f65SMatthew G. Knepley   } else if (isHDF5) {
5532cc2f8f65SMatthew G. Knepley     PetscViewer viewer;
5533cc2f8f65SMatthew G. Knepley 
553443b242b4SVaclav 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 */
55356f2c871aSStefano Zampini     PetscCall(PetscOptionsGetBool(NULL, NULL, "-dm_plex_create_from_hdf5_xdmf", &isXDMFHDF5, NULL));
55369566063dSJacob Faibussowitsch     PetscCall(PetscViewerCreate(comm, &viewer));
55379566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetType(viewer, PETSCVIEWERHDF5));
55389566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetOptionsPrefix(viewer, "dm_plex_create_"));
55399566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetFromOptions(viewer));
55409566063dSJacob Faibussowitsch     PetscCall(PetscViewerFileSetMode(viewer, FILE_MODE_READ));
55419566063dSJacob Faibussowitsch     PetscCall(PetscViewerFileSetName(viewer, filename));
5542cd7e8a5eSksagiyam 
55439566063dSJacob Faibussowitsch     PetscCall(DMCreate(comm, dm));
55449566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)(*dm), plexname));
55459566063dSJacob Faibussowitsch     PetscCall(DMSetType(*dm, DMPLEX));
55466f2c871aSStefano Zampini     if (isXDMFHDF5) PetscCall(PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_XDMF));
55479566063dSJacob Faibussowitsch     PetscCall(DMLoad(*dm, viewer));
55486f2c871aSStefano Zampini     if (isXDMFHDF5) PetscCall(PetscViewerPopFormat(viewer));
55499566063dSJacob Faibussowitsch     PetscCall(PetscViewerDestroy(&viewer));
55505fd9971aSMatthew G. Knepley 
55515fd9971aSMatthew G. Knepley     if (interpolate) {
55525fd9971aSMatthew G. Knepley       DM idm;
55535fd9971aSMatthew G. Knepley 
55549566063dSJacob Faibussowitsch       PetscCall(DMPlexInterpolate(*dm, &idm));
55559566063dSJacob Faibussowitsch       PetscCall(DMDestroy(dm));
55565fd9971aSMatthew G. Knepley       *dm = idm;
55575fd9971aSMatthew G. Knepley     }
5558707dd687SMichael Lange   } else if (isMed) {
55599566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateMedFromFile(comm, filename, interpolate, dm));
5560f2801cd6SMatthew G. Knepley   } else if (isPLY) {
55619566063dSJacob Faibussowitsch     PetscCall(DMPlexCreatePLYFromFile(comm, filename, interpolate, dm));
5562c1cad2e7SMatthew G. Knepley   } else if (isEGADSLite || isEGADS || isIGES || isSTEP) {
55639566063dSJacob Faibussowitsch     if (isEGADSLite) PetscCall(DMPlexCreateEGADSLiteFromFile(comm, filename, dm));
55649566063dSJacob Faibussowitsch     else PetscCall(DMPlexCreateEGADSFromFile(comm, filename, dm));
55657bee2925SMatthew Knepley     if (!interpolate) {
55667bee2925SMatthew Knepley       DM udm;
55677bee2925SMatthew Knepley 
55689566063dSJacob Faibussowitsch       PetscCall(DMPlexUninterpolate(*dm, &udm));
55699566063dSJacob Faibussowitsch       PetscCall(DMDestroy(dm));
55707bee2925SMatthew Knepley       *dm = udm;
55717bee2925SMatthew Knepley     }
55728ca92349SMatthew G. Knepley   } else if (isCV) {
55739566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCellVertexFromFile(comm, filename, interpolate, dm));
557498921bdaSJacob Faibussowitsch   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot load file %s: unrecognized extension", filename);
55759566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(plexname, &len));
55769566063dSJacob Faibussowitsch   if (len) PetscCall(PetscObjectSetName((PetscObject)(*dm), plexname));
55779566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_CreateFromFile, 0, 0, 0, 0));
55783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5579ca522641SMatthew G. Knepley }
558012b8a6daSStefano Zampini 
55819f6c5813SMatthew G. Knepley /*@C
55829f6c5813SMatthew G. Knepley   DMPlexCreateEphemeral - This takes a `DMPlexTransform` and a base `DMPlex` and produces an ephemeral `DM`, meaning one that is created on the fly in response to queries.
55839f6c5813SMatthew G. Knepley 
55840528010dSStefano Zampini   Input Parameters:
55850528010dSStefano Zampini + tr     - The `DMPlexTransform`
55860528010dSStefano Zampini - prefix - An options prefix, or NULL
55879f6c5813SMatthew G. Knepley 
55889f6c5813SMatthew G. Knepley   Output Parameter:
55899f6c5813SMatthew G. Knepley . dm - The `DM`
55909f6c5813SMatthew G. Knepley 
55919f6c5813SMatthew G. Knepley   Level: beginner
55929f6c5813SMatthew G. Knepley 
559320f4b53cSBarry Smith   Notes:
559420f4b53cSBarry Smith   An emphemeral mesh is one that is not stored concretely, as in the default `DMPLEX` implementation, but rather is produced on the fly in response to queries, using information from the transform and the base mesh.
559520f4b53cSBarry Smith 
55969f6c5813SMatthew G. Knepley .seealso: `DMPlexCreateFromFile`, `DMPlexCreateFromDAG()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`
55979f6c5813SMatthew G. Knepley @*/
55980528010dSStefano Zampini PetscErrorCode DMPlexCreateEphemeral(DMPlexTransform tr, const char prefix[], DM *dm)
55999f6c5813SMatthew G. Knepley {
56000528010dSStefano Zampini   DM           bdm, bcdm, cdm;
56010528010dSStefano Zampini   Vec          coordinates, coordinatesNew;
56020528010dSStefano Zampini   PetscSection cs;
56030528010dSStefano Zampini   PetscInt     dim, cdim, Nl;
56049f6c5813SMatthew G. Knepley 
56059f6c5813SMatthew G. Knepley   PetscFunctionBegin;
56069f6c5813SMatthew G. Knepley   PetscCall(DMCreate(PetscObjectComm((PetscObject)tr), dm));
56079f6c5813SMatthew G. Knepley   PetscCall(DMSetType(*dm, DMPLEX));
56080528010dSStefano Zampini   ((DM_Plex *)(*dm)->data)->interpolated = DMPLEX_INTERPOLATED_FULL;
56090528010dSStefano Zampini   // Handle coordinates
56100528010dSStefano Zampini   PetscCall(DMPlexTransformGetDM(tr, &bdm));
56110528010dSStefano Zampini   PetscCall(DMGetCoordinateDim(bdm, &cdim));
56120528010dSStefano Zampini   PetscCall(DMSetCoordinateDim(*dm, cdim));
56130528010dSStefano Zampini   PetscCall(DMGetDimension(bdm, &dim));
56140528010dSStefano Zampini   PetscCall(DMSetDimension(*dm, dim));
56150528010dSStefano Zampini   PetscCall(DMGetCoordinateDM(bdm, &bcdm));
56160528010dSStefano Zampini   PetscCall(DMGetCoordinateDM(*dm, &cdm));
56170528010dSStefano Zampini   PetscCall(DMCopyDisc(bcdm, cdm));
56180528010dSStefano Zampini   PetscCall(DMGetLocalSection(cdm, &cs));
56190528010dSStefano Zampini   PetscCall(PetscSectionSetNumFields(cs, 1));
56200528010dSStefano Zampini   PetscCall(PetscSectionSetFieldComponents(cs, 0, cdim));
56210528010dSStefano Zampini   PetscCall(DMGetCoordinatesLocal(bdm, &coordinates));
56220528010dSStefano Zampini   PetscCall(VecDuplicate(coordinates, &coordinatesNew));
56230528010dSStefano Zampini   PetscCall(VecCopy(coordinates, coordinatesNew));
56240528010dSStefano Zampini   PetscCall(DMSetCoordinatesLocal(*dm, coordinatesNew));
56250528010dSStefano Zampini   PetscCall(VecDestroy(&coordinatesNew));
56269f6c5813SMatthew G. Knepley 
56279f6c5813SMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)tr));
56289f6c5813SMatthew G. Knepley   PetscCall(DMPlexTransformDestroy(&((DM_Plex *)(*dm)->data)->tr));
56299f6c5813SMatthew G. Knepley   ((DM_Plex *)(*dm)->data)->tr = tr;
56300528010dSStefano Zampini   PetscCall(DMPlexDistributeSetDefault(*dm, PETSC_FALSE));
56310528010dSStefano Zampini   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)*dm, prefix));
56320528010dSStefano Zampini   PetscCall(DMSetFromOptions(*dm));
56339f6c5813SMatthew G. Knepley 
56349f6c5813SMatthew G. Knepley   PetscCall(DMGetNumLabels(bdm, &Nl));
56359f6c5813SMatthew G. Knepley   for (PetscInt l = 0; l < Nl; ++l) {
56369f6c5813SMatthew G. Knepley     DMLabel     label, labelNew;
56379f6c5813SMatthew G. Knepley     const char *lname;
56389f6c5813SMatthew G. Knepley     PetscBool   isDepth, isCellType;
56399f6c5813SMatthew G. Knepley 
56409f6c5813SMatthew G. Knepley     PetscCall(DMGetLabelName(bdm, l, &lname));
56419f6c5813SMatthew G. Knepley     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
56429f6c5813SMatthew G. Knepley     if (isDepth) continue;
56439f6c5813SMatthew G. Knepley     PetscCall(PetscStrcmp(lname, "celltype", &isCellType));
56449f6c5813SMatthew G. Knepley     if (isCellType) continue;
56459f6c5813SMatthew G. Knepley     PetscCall(DMCreateLabel(*dm, lname));
56469f6c5813SMatthew G. Knepley     PetscCall(DMGetLabel(bdm, lname, &label));
56479f6c5813SMatthew G. Knepley     PetscCall(DMGetLabel(*dm, lname, &labelNew));
56489f6c5813SMatthew G. Knepley     PetscCall(DMLabelSetType(labelNew, DMLABELEPHEMERAL));
56499f6c5813SMatthew G. Knepley     PetscCall(DMLabelEphemeralSetLabel(labelNew, label));
56509f6c5813SMatthew G. Knepley     PetscCall(DMLabelEphemeralSetTransform(labelNew, tr));
56519f6c5813SMatthew G. Knepley     PetscCall(DMLabelSetUp(labelNew));
56529f6c5813SMatthew G. Knepley   }
56533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
56549f6c5813SMatthew G. Knepley }
5655