xref: /petsc/src/dm/impls/plex/plexcreate.c (revision 07c565c5e677eeea04952398ac257d3601d0b81a)
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;
195962854dSMatthew G. Knepley   PetscBool                dist, useCeed;
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));
315962854dSMatthew G. Knepley   PetscCall(DMPlexGetUseCeed(dmin, &useCeed));
325962854dSMatthew G. Knepley   PetscCall(DMPlexSetUseCeed(dmout, useCeed));
33e600fa54SMatthew G. Knepley   ((DM_Plex *)dmout->data)->useHashLocation = ((DM_Plex *)dmin->data)->useHashLocation;
345962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printSetValues  = ((DM_Plex *)dmin->data)->printSetValues;
355962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printFEM        = ((DM_Plex *)dmin->data)->printFEM;
365962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printFVM        = ((DM_Plex *)dmin->data)->printFVM;
375962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printL2         = ((DM_Plex *)dmin->data)->printL2;
385962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printLocate     = ((DM_Plex *)dmin->data)->printLocate;
395962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printTol        = ((DM_Plex *)dmin->data)->printTol;
401baa6e33SBarry Smith   if (copyOverlap) PetscCall(DMPlexSetOverlap_Plex(dmout, dmin, 0));
413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
42e600fa54SMatthew G. Knepley }
43e600fa54SMatthew G. Knepley 
449318fe57SMatthew G. Knepley /* Replace dm with the contents of ndm, and then destroy ndm
459318fe57SMatthew G. Knepley    - Share the DM_Plex structure
469318fe57SMatthew G. Knepley    - Share the coordinates
479318fe57SMatthew G. Knepley    - Share the SF
489318fe57SMatthew G. Knepley */
49d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexReplace_Internal(DM dm, DM *ndm)
50d71ae5a4SJacob Faibussowitsch {
519318fe57SMatthew G. Knepley   PetscSF          sf;
529318fe57SMatthew G. Knepley   DM               dmNew = *ndm, coordDM, coarseDM;
539318fe57SMatthew G. Knepley   Vec              coords;
544fb89dddSMatthew G. Knepley   const PetscReal *maxCell, *Lstart, *L;
559318fe57SMatthew G. Knepley   PetscInt         dim, cdim;
569318fe57SMatthew G. Knepley 
579318fe57SMatthew G. Knepley   PetscFunctionBegin;
589318fe57SMatthew G. Knepley   if (dm == dmNew) {
599566063dSJacob Faibussowitsch     PetscCall(DMDestroy(ndm));
603ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
619318fe57SMatthew G. Knepley   }
629318fe57SMatthew G. Knepley   dm->setupcalled = dmNew->setupcalled;
639566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dmNew, &dim));
649566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
659566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dmNew, &cdim));
669566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, cdim));
679566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmNew, &sf));
689566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dm, sf));
699566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmNew, &coordDM));
709566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmNew, &coords));
719566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dm, coordDM));
729566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coords));
736858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dmNew, &coordDM));
746858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dmNew, &coords));
756858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinateDM(dm, coordDM));
766858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinatesLocal(dm, coords));
779318fe57SMatthew G. Knepley   /* Do not want to create the coordinate field if it does not already exist, so do not call DMGetCoordinateField() */
786858538eSMatthew G. Knepley   PetscCall(DMFieldDestroy(&dm->coordinates[0].field));
796858538eSMatthew G. Knepley   dm->coordinates[0].field            = dmNew->coordinates[0].field;
8061a622f3SMatthew G. Knepley   ((DM_Plex *)dmNew->data)->coordFunc = ((DM_Plex *)dm->data)->coordFunc;
814fb89dddSMatthew G. Knepley   PetscCall(DMGetPeriodicity(dmNew, &maxCell, &Lstart, &L));
824fb89dddSMatthew G. Knepley   PetscCall(DMSetPeriodicity(dm, maxCell, Lstart, L));
836925d1c4SMatthew G. Knepley   PetscCall(DMPlexGetGlobalToNaturalSF(dmNew, &sf));
846925d1c4SMatthew G. Knepley   PetscCall(DMPlexSetGlobalToNaturalSF(dm, sf));
859566063dSJacob Faibussowitsch   PetscCall(DMDestroy_Plex(dm));
869566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(dm));
879318fe57SMatthew G. Knepley   dm->data = dmNew->data;
889318fe57SMatthew G. Knepley   ((DM_Plex *)dmNew->data)->refct++;
895f06a3ddSJed Brown   PetscCall(DMPlexGetIsoperiodicFaceSF(dm, &sf));
905f06a3ddSJed Brown   PetscCall(DMPlexSetIsoperiodicFaceSF(dm, sf)); // for the compose function effect on dm
919566063dSJacob Faibussowitsch   PetscCall(DMDestroyLabelLinkList_Internal(dm));
929566063dSJacob Faibussowitsch   PetscCall(DMCopyLabels(dmNew, dm, PETSC_OWN_POINTER, PETSC_TRUE, DM_COPY_LABELS_FAIL));
939566063dSJacob Faibussowitsch   PetscCall(DMGetCoarseDM(dmNew, &coarseDM));
949566063dSJacob Faibussowitsch   PetscCall(DMSetCoarseDM(dm, coarseDM));
959566063dSJacob Faibussowitsch   PetscCall(DMDestroy(ndm));
963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
979318fe57SMatthew G. Knepley }
989318fe57SMatthew G. Knepley 
999318fe57SMatthew G. Knepley /* Swap dm with the contents of dmNew
1009318fe57SMatthew G. Knepley    - Swap the DM_Plex structure
1019318fe57SMatthew G. Knepley    - Swap the coordinates
1029318fe57SMatthew G. Knepley    - Swap the point PetscSF
1039318fe57SMatthew G. Knepley */
104d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexSwap_Static(DM dmA, DM dmB)
105d71ae5a4SJacob Faibussowitsch {
1069318fe57SMatthew G. Knepley   DM          coordDMA, coordDMB;
1079318fe57SMatthew G. Knepley   Vec         coordsA, coordsB;
1089318fe57SMatthew G. Knepley   PetscSF     sfA, sfB;
1099318fe57SMatthew G. Knepley   DMField     fieldTmp;
1109318fe57SMatthew G. Knepley   void       *tmp;
1119318fe57SMatthew G. Knepley   DMLabelLink listTmp;
1129318fe57SMatthew G. Knepley   DMLabel     depthTmp;
1139318fe57SMatthew G. Knepley   PetscInt    tmpI;
1149318fe57SMatthew G. Knepley 
1159318fe57SMatthew G. Knepley   PetscFunctionBegin;
1163ba16761SJacob Faibussowitsch   if (dmA == dmB) PetscFunctionReturn(PETSC_SUCCESS);
1179566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmA, &sfA));
1189566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmB, &sfB));
1199566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sfA));
1209566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dmA, sfB));
1219566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dmB, sfA));
1229566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)sfA));
1239318fe57SMatthew G. Knepley 
1249566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmA, &coordDMA));
1259566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmB, &coordDMB));
1269566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)coordDMA));
1279566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dmA, coordDMB));
1289566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dmB, coordDMA));
1299566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)coordDMA));
1309318fe57SMatthew G. Knepley 
1319566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmA, &coordsA));
1329566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmB, &coordsB));
1339566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)coordsA));
1349566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmA, coordsB));
1359566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmB, coordsA));
1369566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)coordsA));
1379318fe57SMatthew G. Knepley 
1386858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dmA, &coordDMA));
1396858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dmB, &coordDMB));
1406858538eSMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)coordDMA));
1416858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinateDM(dmA, coordDMB));
1426858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinateDM(dmB, coordDMA));
1436858538eSMatthew G. Knepley   PetscCall(PetscObjectDereference((PetscObject)coordDMA));
1446858538eSMatthew G. Knepley 
1456858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dmA, &coordsA));
1466858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dmB, &coordsB));
1476858538eSMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)coordsA));
1486858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinatesLocal(dmA, coordsB));
1496858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinatesLocal(dmB, coordsA));
1506858538eSMatthew G. Knepley   PetscCall(PetscObjectDereference((PetscObject)coordsA));
1516858538eSMatthew G. Knepley 
1526858538eSMatthew G. Knepley   fieldTmp                  = dmA->coordinates[0].field;
1536858538eSMatthew G. Knepley   dmA->coordinates[0].field = dmB->coordinates[0].field;
1546858538eSMatthew G. Knepley   dmB->coordinates[0].field = fieldTmp;
1556858538eSMatthew G. Knepley   fieldTmp                  = dmA->coordinates[1].field;
1566858538eSMatthew G. Knepley   dmA->coordinates[1].field = dmB->coordinates[1].field;
1576858538eSMatthew G. Knepley   dmB->coordinates[1].field = fieldTmp;
1589318fe57SMatthew G. Knepley   tmp                       = dmA->data;
1599318fe57SMatthew G. Knepley   dmA->data                 = dmB->data;
1609318fe57SMatthew G. Knepley   dmB->data                 = tmp;
1619318fe57SMatthew G. Knepley   listTmp                   = dmA->labels;
1629318fe57SMatthew G. Knepley   dmA->labels               = dmB->labels;
1639318fe57SMatthew G. Knepley   dmB->labels               = listTmp;
1649318fe57SMatthew G. Knepley   depthTmp                  = dmA->depthLabel;
1659318fe57SMatthew G. Knepley   dmA->depthLabel           = dmB->depthLabel;
1669318fe57SMatthew G. Knepley   dmB->depthLabel           = depthTmp;
1679318fe57SMatthew G. Knepley   depthTmp                  = dmA->celltypeLabel;
1689318fe57SMatthew G. Knepley   dmA->celltypeLabel        = dmB->celltypeLabel;
1699318fe57SMatthew G. Knepley   dmB->celltypeLabel        = depthTmp;
1709318fe57SMatthew G. Knepley   tmpI                      = dmA->levelup;
1719318fe57SMatthew G. Knepley   dmA->levelup              = dmB->levelup;
1729318fe57SMatthew G. Knepley   dmB->levelup              = tmpI;
1733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1749318fe57SMatthew G. Knepley }
1759318fe57SMatthew G. Knepley 
1763431e603SJed Brown PetscErrorCode DMPlexInterpolateInPlace_Internal(DM dm)
177d71ae5a4SJacob Faibussowitsch {
1789318fe57SMatthew G. Knepley   DM idm;
1799318fe57SMatthew G. Knepley 
1809318fe57SMatthew G. Knepley   PetscFunctionBegin;
1819566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolate(dm, &idm));
1829566063dSJacob Faibussowitsch   PetscCall(DMPlexCopyCoordinates(dm, idm));
18369d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &idm));
1843ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1859318fe57SMatthew G. Knepley }
1869318fe57SMatthew G. Knepley 
1879318fe57SMatthew G. Knepley /*@C
1889318fe57SMatthew G. Knepley   DMPlexCreateCoordinateSpace - Creates a finite element space for the coordinates
1899318fe57SMatthew G. Knepley 
19020f4b53cSBarry Smith   Collective
1919318fe57SMatthew G. Knepley 
1929318fe57SMatthew G. Knepley   Input Parameters:
19360225df5SJacob Faibussowitsch + dm        - The `DMPLEX`
19420f4b53cSBarry Smith . degree    - The degree of the finite element or `PETSC_DECIDE`
195e44f6aebSMatthew G. Knepley . project   - Flag to project current coordinates into the space
1969318fe57SMatthew G. Knepley - coordFunc - An optional function to map new points from refinement to the surface
1979318fe57SMatthew G. Knepley 
1989318fe57SMatthew G. Knepley   Level: advanced
1999318fe57SMatthew G. Knepley 
2001cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `PetscPointFunc`, `PetscFECreateLagrange()`, `DMGetCoordinateDM()`
2019318fe57SMatthew G. Knepley @*/
202e44f6aebSMatthew G. Knepley PetscErrorCode DMPlexCreateCoordinateSpace(DM dm, PetscInt degree, PetscBool project, PetscPointFunc coordFunc)
203d71ae5a4SJacob Faibussowitsch {
2049318fe57SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
205e44f6aebSMatthew G. Knepley   PetscFE  fe   = NULL;
2069318fe57SMatthew G. Knepley   DM       cdm;
2079318fe57SMatthew G. Knepley   PetscInt dim, dE, qorder;
2089318fe57SMatthew G. Knepley 
209e44f6aebSMatthew G. Knepley   PetscFunctionBegin;
2109566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
2119566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
2129318fe57SMatthew G. Knepley   qorder = degree;
213e44f6aebSMatthew G. Knepley   PetscCall(DMGetCoordinateDM(dm, &cdm));
214d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)cdm);
215dc431b0cSMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-default_quadrature_order", "Quadrature order is one less than quadrature points per edge", "DMPlexCreateCoordinateSpace", qorder, &qorder, NULL, 0));
216d0609cedSBarry Smith   PetscOptionsEnd();
217e44f6aebSMatthew G. Knepley   if (degree >= 0) {
218e44f6aebSMatthew G. Knepley     DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
219e44f6aebSMatthew G. Knepley     PetscInt       cStart, cEnd, gct;
220dc431b0cSMatthew G. Knepley 
221dc431b0cSMatthew G. Knepley     PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
222dc431b0cSMatthew G. Knepley     if (cEnd > cStart) PetscCall(DMPlexGetCellType(dm, cStart, &ct));
223e44f6aebSMatthew G. Knepley     gct = (PetscInt)ct;
224e44f6aebSMatthew G. Knepley     PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &gct, 1, MPIU_INT, MPI_MIN, PetscObjectComm((PetscObject)dm)));
225e44f6aebSMatthew G. Knepley     ct = (DMPolytopeType)gct;
226e44f6aebSMatthew G. Knepley     // Work around current bug in PetscDualSpaceSetUp_Lagrange()
227e44f6aebSMatthew G. Knepley     //   Can be seen in plex_tutorials-ex10_1
228e44f6aebSMatthew G. Knepley     if (ct != DM_POLYTOPE_SEG_PRISM_TENSOR && ct != DM_POLYTOPE_TRI_PRISM_TENSOR && ct != DM_POLYTOPE_QUAD_PRISM_TENSOR) PetscCall(PetscFECreateLagrangeByCell(PETSC_COMM_SELF, dim, dE, ct, degree, qorder, &fe));
2294f9ab2b4SJed Brown   }
230e44f6aebSMatthew G. Knepley   PetscCall(DMSetCoordinateDisc(dm, fe, project));
2319566063dSJacob Faibussowitsch   PetscCall(PetscFEDestroy(&fe));
2329318fe57SMatthew G. Knepley   mesh->coordFunc = coordFunc;
2333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2349318fe57SMatthew G. Knepley }
2359318fe57SMatthew G. Knepley 
2361df5d5c5SMatthew G. Knepley /*@
2371df5d5c5SMatthew G. Knepley   DMPlexCreateDoublet - Creates a mesh of two cells of the specified type, optionally with later refinement.
2381df5d5c5SMatthew G. Knepley 
239d083f849SBarry Smith   Collective
2401df5d5c5SMatthew G. Knepley 
2411df5d5c5SMatthew G. Knepley   Input Parameters:
242a1cb98faSBarry Smith + comm            - The communicator for the `DM` object
2431df5d5c5SMatthew G. Knepley . dim             - The spatial dimension
2441df5d5c5SMatthew G. Knepley . simplex         - Flag for simplicial cells, otherwise they are tensor product cells
2451df5d5c5SMatthew G. Knepley . interpolate     - Flag to create intermediate mesh pieces (edges, faces)
2461df5d5c5SMatthew G. Knepley - refinementLimit - A nonzero number indicates the largest admissible volume for a refined cell
2471df5d5c5SMatthew G. Knepley 
2481df5d5c5SMatthew G. Knepley   Output Parameter:
24960225df5SJacob Faibussowitsch . newdm - The `DM` object
2501df5d5c5SMatthew G. Knepley 
2511df5d5c5SMatthew G. Knepley   Level: beginner
2521df5d5c5SMatthew G. Knepley 
2531cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMSetType()`, `DMCreate()`
2541df5d5c5SMatthew G. Knepley @*/
255d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateDoublet(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscBool interpolate, PetscReal refinementLimit, DM *newdm)
256d71ae5a4SJacob Faibussowitsch {
2571df5d5c5SMatthew G. Knepley   DM          dm;
2581df5d5c5SMatthew G. Knepley   PetscMPIInt rank;
2591df5d5c5SMatthew G. Knepley 
2601df5d5c5SMatthew G. Knepley   PetscFunctionBegin;
2619566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, &dm));
2629566063dSJacob Faibussowitsch   PetscCall(DMSetType(dm, DMPLEX));
2639566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
26446139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
2659566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
266ce78fa2fSMatthew G. Knepley   switch (dim) {
267ce78fa2fSMatthew G. Knepley   case 2:
2689566063dSJacob Faibussowitsch     if (simplex) PetscCall(PetscObjectSetName((PetscObject)dm, "triangular"));
2699566063dSJacob Faibussowitsch     else PetscCall(PetscObjectSetName((PetscObject)dm, "quadrilateral"));
270ce78fa2fSMatthew G. Knepley     break;
271ce78fa2fSMatthew G. Knepley   case 3:
2729566063dSJacob Faibussowitsch     if (simplex) PetscCall(PetscObjectSetName((PetscObject)dm, "tetrahedral"));
2739566063dSJacob Faibussowitsch     else PetscCall(PetscObjectSetName((PetscObject)dm, "hexahedral"));
274ce78fa2fSMatthew G. Knepley     break;
275d71ae5a4SJacob Faibussowitsch   default:
276d71ae5a4SJacob Faibussowitsch     SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %" PetscInt_FMT, dim);
277ce78fa2fSMatthew G. Knepley   }
2781df5d5c5SMatthew G. Knepley   if (rank) {
2791df5d5c5SMatthew G. Knepley     PetscInt numPoints[2] = {0, 0};
2809566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, NULL, NULL, NULL, NULL));
2811df5d5c5SMatthew G. Knepley   } else {
2821df5d5c5SMatthew G. Knepley     switch (dim) {
2831df5d5c5SMatthew G. Knepley     case 2:
2841df5d5c5SMatthew G. Knepley       if (simplex) {
2851df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {4, 2};
2861df5d5c5SMatthew G. Knepley         PetscInt    coneSize[6]         = {3, 3, 0, 0, 0, 0};
2871df5d5c5SMatthew G. Knepley         PetscInt    cones[6]            = {2, 3, 4, 5, 4, 3};
2881df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
2891df5d5c5SMatthew G. Knepley         PetscScalar vertexCoords[8]     = {-0.5, 0.5, 0.0, 0.0, 0.0, 1.0, 0.5, 0.5};
2901df5d5c5SMatthew G. Knepley 
2919566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
2921df5d5c5SMatthew G. Knepley       } else {
2931df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {6, 2};
2941df5d5c5SMatthew G. Knepley         PetscInt    coneSize[8]         = {4, 4, 0, 0, 0, 0, 0, 0};
2951df5d5c5SMatthew G. Knepley         PetscInt    cones[8]            = {2, 3, 4, 5, 3, 6, 7, 4};
2961df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
2971df5d5c5SMatthew 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};
2981df5d5c5SMatthew G. Knepley 
2999566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
3001df5d5c5SMatthew G. Knepley       }
3011df5d5c5SMatthew G. Knepley       break;
3021df5d5c5SMatthew G. Knepley     case 3:
3031df5d5c5SMatthew G. Knepley       if (simplex) {
3041df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {5, 2};
3051df5d5c5SMatthew G. Knepley         PetscInt    coneSize[7]         = {4, 4, 0, 0, 0, 0, 0};
3061df5d5c5SMatthew G. Knepley         PetscInt    cones[8]            = {4, 3, 5, 2, 5, 3, 4, 6};
3071df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
3081df5d5c5SMatthew 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};
3091df5d5c5SMatthew G. Knepley 
3109566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
3111df5d5c5SMatthew G. Knepley       } else {
3121df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]         = {12, 2};
3131df5d5c5SMatthew G. Knepley         PetscInt    coneSize[14]         = {8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
3141df5d5c5SMatthew G. Knepley         PetscInt    cones[16]            = {2, 3, 4, 5, 6, 7, 8, 9, 5, 4, 10, 11, 7, 12, 13, 8};
3151df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
3169371c9d4SSatish 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};
3171df5d5c5SMatthew G. Knepley 
3189566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
3191df5d5c5SMatthew G. Knepley       }
3201df5d5c5SMatthew G. Knepley       break;
321d71ae5a4SJacob Faibussowitsch     default:
322d71ae5a4SJacob Faibussowitsch       SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %" PetscInt_FMT, dim);
3231df5d5c5SMatthew G. Knepley     }
3241df5d5c5SMatthew G. Knepley   }
32546139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
3261df5d5c5SMatthew G. Knepley   *newdm = dm;
3271df5d5c5SMatthew G. Knepley   if (refinementLimit > 0.0) {
3281df5d5c5SMatthew G. Knepley     DM          rdm;
3291df5d5c5SMatthew G. Knepley     const char *name;
3301df5d5c5SMatthew G. Knepley 
3319566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementUniform(*newdm, PETSC_FALSE));
3329566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementLimit(*newdm, refinementLimit));
3339566063dSJacob Faibussowitsch     PetscCall(DMRefine(*newdm, comm, &rdm));
3349566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)*newdm, &name));
3359566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)rdm, name));
3369566063dSJacob Faibussowitsch     PetscCall(DMDestroy(newdm));
3371df5d5c5SMatthew G. Knepley     *newdm = rdm;
3381df5d5c5SMatthew G. Knepley   }
3391df5d5c5SMatthew G. Knepley   if (interpolate) {
3405fd9971aSMatthew G. Knepley     DM idm;
3411df5d5c5SMatthew G. Knepley 
3429566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*newdm, &idm));
3439566063dSJacob Faibussowitsch     PetscCall(DMDestroy(newdm));
3441df5d5c5SMatthew G. Knepley     *newdm = idm;
3451df5d5c5SMatthew G. Knepley   }
3463ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3471df5d5c5SMatthew G. Knepley }
3481df5d5c5SMatthew G. Knepley 
349d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
350d71ae5a4SJacob Faibussowitsch {
3519318fe57SMatthew G. Knepley   const PetscInt numVertices    = 2;
3529318fe57SMatthew G. Knepley   PetscInt       markerRight    = 1;
3539318fe57SMatthew G. Knepley   PetscInt       markerLeft     = 1;
3549318fe57SMatthew G. Knepley   PetscBool      markerSeparate = PETSC_FALSE;
3559318fe57SMatthew G. Knepley   Vec            coordinates;
3569318fe57SMatthew G. Knepley   PetscSection   coordSection;
3579318fe57SMatthew G. Knepley   PetscScalar   *coords;
3589318fe57SMatthew G. Knepley   PetscInt       coordSize;
3599318fe57SMatthew G. Knepley   PetscMPIInt    rank;
3609318fe57SMatthew G. Knepley   PetscInt       cdim = 1, v;
361552f7358SJed Brown 
3629318fe57SMatthew G. Knepley   PetscFunctionBegin;
3639566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
3649318fe57SMatthew G. Knepley   if (markerSeparate) {
3659318fe57SMatthew G. Knepley     markerRight = 2;
3669318fe57SMatthew G. Knepley     markerLeft  = 1;
3679318fe57SMatthew G. Knepley   }
3689566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
369c5853193SPierre Jolivet   if (rank == 0) {
3709566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numVertices));
3719566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
3729566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", 0, markerLeft));
3739566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", 1, markerRight));
3749318fe57SMatthew G. Knepley   }
3759566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
3769566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
3779318fe57SMatthew G. Knepley   /* Build coordinates */
3789566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, cdim));
3799566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
3809566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
3819566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, 0, numVertices));
3829566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, cdim));
3839318fe57SMatthew G. Knepley   for (v = 0; v < numVertices; ++v) {
3849566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, cdim));
3859566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, cdim));
3869318fe57SMatthew G. Knepley   }
3879566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
3889566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
3899566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
3909566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
3919566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
3929566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, cdim));
3939566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
3949566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
3959318fe57SMatthew G. Knepley   coords[0] = lower[0];
3969318fe57SMatthew G. Knepley   coords[1] = upper[0];
3979566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
3989566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
3999566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
4003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4019318fe57SMatthew G. Knepley }
40226492d91SMatthew G. Knepley 
403d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
404d71ae5a4SJacob Faibussowitsch {
4051df21d24SMatthew G. Knepley   const PetscInt numVertices    = (edges[0] + 1) * (edges[1] + 1);
4061df21d24SMatthew G. Knepley   const PetscInt numEdges       = edges[0] * (edges[1] + 1) + (edges[0] + 1) * edges[1];
407552f7358SJed Brown   PetscInt       markerTop      = 1;
408552f7358SJed Brown   PetscInt       markerBottom   = 1;
409552f7358SJed Brown   PetscInt       markerRight    = 1;
410552f7358SJed Brown   PetscInt       markerLeft     = 1;
411552f7358SJed Brown   PetscBool      markerSeparate = PETSC_FALSE;
412552f7358SJed Brown   Vec            coordinates;
413552f7358SJed Brown   PetscSection   coordSection;
414552f7358SJed Brown   PetscScalar   *coords;
415552f7358SJed Brown   PetscInt       coordSize;
416552f7358SJed Brown   PetscMPIInt    rank;
417552f7358SJed Brown   PetscInt       v, vx, vy;
418552f7358SJed Brown 
419552f7358SJed Brown   PetscFunctionBegin;
4209566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
421552f7358SJed Brown   if (markerSeparate) {
4221df21d24SMatthew G. Knepley     markerTop    = 3;
4231df21d24SMatthew G. Knepley     markerBottom = 1;
4241df21d24SMatthew G. Knepley     markerRight  = 2;
4251df21d24SMatthew G. Knepley     markerLeft   = 4;
426552f7358SJed Brown   }
4279566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
428dd400576SPatrick Sanan   if (rank == 0) {
429552f7358SJed Brown     PetscInt e, ex, ey;
430552f7358SJed Brown 
4319566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numEdges + numVertices));
43248a46eb9SPierre Jolivet     for (e = 0; e < numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2));
4339566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
434552f7358SJed Brown     for (vx = 0; vx <= edges[0]; vx++) {
435552f7358SJed Brown       for (ey = 0; ey < edges[1]; ey++) {
436552f7358SJed Brown         PetscInt edge   = vx * edges[1] + ey + edges[0] * (edges[1] + 1);
437552f7358SJed Brown         PetscInt vertex = ey * (edges[0] + 1) + vx + numEdges;
438da80777bSKarl Rupp         PetscInt cone[2];
439552f7358SJed Brown 
4409371c9d4SSatish Balay         cone[0] = vertex;
4419371c9d4SSatish Balay         cone[1] = vertex + edges[0] + 1;
4429566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, edge, cone));
443552f7358SJed Brown         if (vx == edges[0]) {
4449566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
4459566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
446552f7358SJed Brown           if (ey == edges[1] - 1) {
4479566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
4489566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerRight));
449552f7358SJed Brown           }
450552f7358SJed Brown         } else if (vx == 0) {
4519566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
4529566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
453552f7358SJed Brown           if (ey == edges[1] - 1) {
4549566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
4559566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerLeft));
456552f7358SJed Brown           }
457552f7358SJed Brown         }
458552f7358SJed Brown       }
459552f7358SJed Brown     }
460552f7358SJed Brown     for (vy = 0; vy <= edges[1]; vy++) {
461552f7358SJed Brown       for (ex = 0; ex < edges[0]; ex++) {
462552f7358SJed Brown         PetscInt edge   = vy * edges[0] + ex;
463552f7358SJed Brown         PetscInt vertex = vy * (edges[0] + 1) + ex + numEdges;
464da80777bSKarl Rupp         PetscInt cone[2];
465552f7358SJed Brown 
4669371c9d4SSatish Balay         cone[0] = vertex;
4679371c9d4SSatish Balay         cone[1] = vertex + 1;
4689566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, edge, cone));
469552f7358SJed Brown         if (vy == edges[1]) {
4709566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
4719566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
472552f7358SJed Brown           if (ex == edges[0] - 1) {
4739566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
4749566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerTop));
475552f7358SJed Brown           }
476552f7358SJed Brown         } else if (vy == 0) {
4779566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
4789566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
479552f7358SJed Brown           if (ex == edges[0] - 1) {
4809566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
4819566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerBottom));
482552f7358SJed Brown           }
483552f7358SJed Brown         }
484552f7358SJed Brown       }
485552f7358SJed Brown     }
486552f7358SJed Brown   }
4879566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
4889566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
489552f7358SJed Brown   /* Build coordinates */
4909566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, 2));
4919566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
4929566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
4939566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, numEdges, numEdges + numVertices));
4949566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, 2));
495552f7358SJed Brown   for (v = numEdges; v < numEdges + numVertices; ++v) {
4969566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, 2));
4979566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, 2));
498552f7358SJed Brown   }
4999566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
5009566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
5019566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
5029566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
5039566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
5049566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, 2));
5059566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
5069566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
507552f7358SJed Brown   for (vy = 0; vy <= edges[1]; ++vy) {
508552f7358SJed Brown     for (vx = 0; vx <= edges[0]; ++vx) {
509552f7358SJed Brown       coords[(vy * (edges[0] + 1) + vx) * 2 + 0] = lower[0] + ((upper[0] - lower[0]) / edges[0]) * vx;
510552f7358SJed Brown       coords[(vy * (edges[0] + 1) + vx) * 2 + 1] = lower[1] + ((upper[1] - lower[1]) / edges[1]) * vy;
511552f7358SJed Brown     }
512552f7358SJed Brown   }
5139566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
5149566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
5159566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
5163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
517552f7358SJed Brown }
518552f7358SJed Brown 
519d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt faces[])
520d71ae5a4SJacob Faibussowitsch {
5219e8abbc3SMichael Lange   PetscInt     vertices[3], numVertices;
5227b59f5a9SMichael Lange   PetscInt     numFaces       = 2 * faces[0] * faces[1] + 2 * faces[1] * faces[2] + 2 * faces[0] * faces[2];
523c2df9bbfSMatthew G. Knepley   PetscInt     markerTop      = 1;
524c2df9bbfSMatthew G. Knepley   PetscInt     markerBottom   = 1;
525c2df9bbfSMatthew G. Knepley   PetscInt     markerFront    = 1;
526c2df9bbfSMatthew G. Knepley   PetscInt     markerBack     = 1;
527c2df9bbfSMatthew G. Knepley   PetscInt     markerRight    = 1;
528c2df9bbfSMatthew G. Knepley   PetscInt     markerLeft     = 1;
529c2df9bbfSMatthew G. Knepley   PetscBool    markerSeparate = PETSC_FALSE;
530552f7358SJed Brown   Vec          coordinates;
531552f7358SJed Brown   PetscSection coordSection;
532552f7358SJed Brown   PetscScalar *coords;
533552f7358SJed Brown   PetscInt     coordSize;
534552f7358SJed Brown   PetscMPIInt  rank;
535552f7358SJed Brown   PetscInt     v, vx, vy, vz;
5367b59f5a9SMichael Lange   PetscInt     voffset, iface = 0, cone[4];
537552f7358SJed Brown 
538552f7358SJed Brown   PetscFunctionBegin;
5391dca8a05SBarry 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");
5409566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
541c2df9bbfSMatthew G. Knepley   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
542c2df9bbfSMatthew G. Knepley   if (markerSeparate) {
543c2df9bbfSMatthew G. Knepley     markerBottom = 1;
544c2df9bbfSMatthew G. Knepley     markerTop    = 2;
545c2df9bbfSMatthew G. Knepley     markerFront  = 3;
546c2df9bbfSMatthew G. Knepley     markerBack   = 4;
547c2df9bbfSMatthew G. Knepley     markerRight  = 5;
548c2df9bbfSMatthew G. Knepley     markerLeft   = 6;
549c2df9bbfSMatthew G. Knepley   }
5509371c9d4SSatish Balay   vertices[0] = faces[0] + 1;
5519371c9d4SSatish Balay   vertices[1] = faces[1] + 1;
5529371c9d4SSatish Balay   vertices[2] = faces[2] + 1;
5539e8abbc3SMichael Lange   numVertices = vertices[0] * vertices[1] * vertices[2];
554dd400576SPatrick Sanan   if (rank == 0) {
555552f7358SJed Brown     PetscInt f;
556552f7358SJed Brown 
5579566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numFaces + numVertices));
55848a46eb9SPierre Jolivet     for (f = 0; f < numFaces; ++f) PetscCall(DMPlexSetConeSize(dm, f, 4));
5599566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
5607b59f5a9SMichael Lange 
5617b59f5a9SMichael Lange     /* Side 0 (Top) */
5627b59f5a9SMichael Lange     for (vy = 0; vy < faces[1]; vy++) {
5637b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5647b59f5a9SMichael Lange         voffset = numFaces + vertices[0] * vertices[1] * (vertices[2] - 1) + vy * vertices[0] + vx;
5659371c9d4SSatish Balay         cone[0] = voffset;
5669371c9d4SSatish Balay         cone[1] = voffset + 1;
5679371c9d4SSatish Balay         cone[2] = voffset + vertices[0] + 1;
5689371c9d4SSatish Balay         cone[3] = voffset + vertices[0];
5699566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
570c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerTop));
571c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerTop));
572c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerTop));
573c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerTop));
574c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 1, markerTop));
5757b59f5a9SMichael Lange         iface++;
576552f7358SJed Brown       }
577552f7358SJed Brown     }
5787b59f5a9SMichael Lange 
5797b59f5a9SMichael Lange     /* Side 1 (Bottom) */
5807b59f5a9SMichael Lange     for (vy = 0; vy < faces[1]; vy++) {
5817b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5827b59f5a9SMichael Lange         voffset = numFaces + vy * (faces[0] + 1) + vx;
5839371c9d4SSatish Balay         cone[0] = voffset + 1;
5849371c9d4SSatish Balay         cone[1] = voffset;
5859371c9d4SSatish Balay         cone[2] = voffset + vertices[0];
5869371c9d4SSatish Balay         cone[3] = voffset + vertices[0] + 1;
5879566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
588c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerBottom));
589c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerBottom));
590c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerBottom));
591c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerBottom));
592c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 1, markerBottom));
5937b59f5a9SMichael Lange         iface++;
594552f7358SJed Brown       }
595552f7358SJed Brown     }
5967b59f5a9SMichael Lange 
5977b59f5a9SMichael Lange     /* Side 2 (Front) */
5987b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
5997b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
6007b59f5a9SMichael Lange         voffset = numFaces + vz * vertices[0] * vertices[1] + vx;
6019371c9d4SSatish Balay         cone[0] = voffset;
6029371c9d4SSatish Balay         cone[1] = voffset + 1;
6039371c9d4SSatish Balay         cone[2] = voffset + vertices[0] * vertices[1] + 1;
6049371c9d4SSatish Balay         cone[3] = voffset + vertices[0] * vertices[1];
6059566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
606c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerFront));
607c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerFront));
608c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerFront));
609c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 0, markerFront));
610c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 1, markerFront));
6117b59f5a9SMichael Lange         iface++;
612552f7358SJed Brown       }
6137b59f5a9SMichael Lange     }
6147b59f5a9SMichael Lange 
6157b59f5a9SMichael Lange     /* Side 3 (Back) */
6167b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
6177b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
6187b59f5a9SMichael Lange         voffset = numFaces + vz * vertices[0] * vertices[1] + vertices[0] * (vertices[1] - 1) + vx;
6199371c9d4SSatish Balay         cone[0] = voffset + vertices[0] * vertices[1];
6209371c9d4SSatish Balay         cone[1] = voffset + vertices[0] * vertices[1] + 1;
6219371c9d4SSatish Balay         cone[2] = voffset + 1;
6229371c9d4SSatish Balay         cone[3] = voffset;
6239566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
624c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerBack));
625c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerBack));
626c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerBack));
627c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 0, markerBack));
628c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 1, markerBack));
6297b59f5a9SMichael Lange         iface++;
6307b59f5a9SMichael Lange       }
6317b59f5a9SMichael Lange     }
6327b59f5a9SMichael Lange 
6337b59f5a9SMichael Lange     /* Side 4 (Left) */
6347b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
6357b59f5a9SMichael Lange       for (vy = 0; vy < faces[1]; vy++) {
6367b59f5a9SMichael Lange         voffset = numFaces + vz * vertices[0] * vertices[1] + vy * vertices[0];
6379371c9d4SSatish Balay         cone[0] = voffset;
6389371c9d4SSatish Balay         cone[1] = voffset + vertices[0] * vertices[1];
6399371c9d4SSatish Balay         cone[2] = voffset + vertices[0] * vertices[1] + vertices[0];
6409371c9d4SSatish Balay         cone[3] = voffset + vertices[0];
6419566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
642c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerLeft));
643c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerLeft));
644c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerLeft));
645c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[1] + 0, markerLeft));
646c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + vertices[0], markerLeft));
6477b59f5a9SMichael Lange         iface++;
6487b59f5a9SMichael Lange       }
6497b59f5a9SMichael Lange     }
6507b59f5a9SMichael Lange 
6517b59f5a9SMichael Lange     /* Side 5 (Right) */
6527b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
6537b59f5a9SMichael Lange       for (vy = 0; vy < faces[1]; vy++) {
654aab5bcd8SJed Brown         voffset = numFaces + vz * vertices[0] * vertices[1] + vy * vertices[0] + faces[0];
6559371c9d4SSatish Balay         cone[0] = voffset + vertices[0] * vertices[1];
6569371c9d4SSatish Balay         cone[1] = voffset;
6579371c9d4SSatish Balay         cone[2] = voffset + vertices[0];
6589371c9d4SSatish Balay         cone[3] = voffset + vertices[0] * vertices[1] + vertices[0];
6599566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
660c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerRight));
661c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerRight));
662c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerRight));
663c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 0, markerRight));
664c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + vertices[0], markerRight));
6657b59f5a9SMichael Lange         iface++;
6667b59f5a9SMichael Lange       }
667552f7358SJed Brown     }
668552f7358SJed Brown   }
6699566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
6709566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
671552f7358SJed Brown   /* Build coordinates */
6729566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, 3));
6739566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
6749566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
6759566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, numFaces, numFaces + numVertices));
6769566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, 3));
677552f7358SJed Brown   for (v = numFaces; v < numFaces + numVertices; ++v) {
6789566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, 3));
6799566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, 3));
680552f7358SJed Brown   }
6819566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
6829566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
6839566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
6849566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
6859566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
6869566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, 3));
6879566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
6889566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
689552f7358SJed Brown   for (vz = 0; vz <= faces[2]; ++vz) {
690552f7358SJed Brown     for (vy = 0; vy <= faces[1]; ++vy) {
691552f7358SJed Brown       for (vx = 0; vx <= faces[0]; ++vx) {
692552f7358SJed Brown         coords[((vz * (faces[1] + 1) + vy) * (faces[0] + 1) + vx) * 3 + 0] = lower[0] + ((upper[0] - lower[0]) / faces[0]) * vx;
693552f7358SJed Brown         coords[((vz * (faces[1] + 1) + vy) * (faces[0] + 1) + vx) * 3 + 1] = lower[1] + ((upper[1] - lower[1]) / faces[1]) * vy;
694552f7358SJed Brown         coords[((vz * (faces[1] + 1) + vy) * (faces[0] + 1) + vx) * 3 + 2] = lower[2] + ((upper[2] - lower[2]) / faces[2]) * vz;
695552f7358SJed Brown       }
696552f7358SJed Brown     }
697552f7358SJed Brown   }
6989566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
6999566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
7009566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
7013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
702552f7358SJed Brown }
703552f7358SJed Brown 
704d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate)
705d71ae5a4SJacob Faibussowitsch {
7069318fe57SMatthew G. Knepley   PetscFunctionBegin;
7079318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
70846139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
7099566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim - 1));
7109566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, dim));
7119318fe57SMatthew G. Knepley   switch (dim) {
712d71ae5a4SJacob Faibussowitsch   case 1:
713d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(dm, lower, upper, faces));
714d71ae5a4SJacob Faibussowitsch     break;
715d71ae5a4SJacob Faibussowitsch   case 2:
716d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(dm, lower, upper, faces));
717d71ae5a4SJacob Faibussowitsch     break;
718d71ae5a4SJacob Faibussowitsch   case 3:
719d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(dm, lower, upper, faces));
720d71ae5a4SJacob Faibussowitsch     break;
721d71ae5a4SJacob Faibussowitsch   default:
722d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Dimension not supported: %" PetscInt_FMT, dim);
7239318fe57SMatthew G. Knepley   }
72446139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
7259566063dSJacob Faibussowitsch   if (interpolate) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
7263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7279318fe57SMatthew G. Knepley }
7289318fe57SMatthew G. Knepley 
7299318fe57SMatthew G. Knepley /*@C
7309318fe57SMatthew G. Knepley   DMPlexCreateBoxSurfaceMesh - Creates a mesh on the surface of the tensor product of unit intervals (box) using tensor cells (hexahedra).
7319318fe57SMatthew G. Knepley 
7329318fe57SMatthew G. Knepley   Collective
7339318fe57SMatthew G. Knepley 
7349318fe57SMatthew G. Knepley   Input Parameters:
735a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
73620f4b53cSBarry Smith . dim         - The spatial dimension of the box, so the resulting mesh is has dimension `dim`-1
73720f4b53cSBarry Smith . faces       - Number of faces per dimension, or `NULL` for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
73820f4b53cSBarry Smith . lower       - The lower left corner, or `NULL` for (0, 0, 0)
73920f4b53cSBarry Smith . upper       - The upper right corner, or `NULL` for (1, 1, 1)
7409318fe57SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
7419318fe57SMatthew G. Knepley 
7429318fe57SMatthew G. Knepley   Output Parameter:
743a1cb98faSBarry Smith . dm - The `DM` object
7449318fe57SMatthew G. Knepley 
7459318fe57SMatthew G. Knepley   Level: beginner
7469318fe57SMatthew G. Knepley 
7471cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMSetFromOptions()`, `DMPlexCreateBoxMesh()`, `DMPlexCreateFromFile()`, `DMSetType()`, `DMCreate()`
7489318fe57SMatthew G. Knepley @*/
749d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateBoxSurfaceMesh(MPI_Comm comm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate, DM *dm)
750d71ae5a4SJacob Faibussowitsch {
7519318fe57SMatthew G. Knepley   PetscInt  fac[3] = {1, 1, 1};
7529318fe57SMatthew G. Knepley   PetscReal low[3] = {0, 0, 0};
7539318fe57SMatthew G. Knepley   PetscReal upp[3] = {1, 1, 1};
7549318fe57SMatthew G. Knepley 
7559318fe57SMatthew G. Knepley   PetscFunctionBegin;
7569566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
7579566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
7589566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(*dm, dim, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, interpolate));
7593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7609318fe57SMatthew G. Knepley }
7619318fe57SMatthew G. Knepley 
762d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateLineMesh_Internal(DM dm, PetscInt segments, PetscReal lower, PetscReal upper, DMBoundaryType bd)
763d71ae5a4SJacob Faibussowitsch {
764fdbf62faSLisandro Dalcin   PetscInt     i, fStart, fEnd, numCells = 0, numVerts = 0;
765fdbf62faSLisandro Dalcin   PetscInt     numPoints[2], *coneSize, *cones, *coneOrientations;
766fdbf62faSLisandro Dalcin   PetscScalar *vertexCoords;
767fdbf62faSLisandro Dalcin   PetscReal    L, maxCell;
768fdbf62faSLisandro Dalcin   PetscBool    markerSeparate = PETSC_FALSE;
769fdbf62faSLisandro Dalcin   PetscInt     markerLeft = 1, faceMarkerLeft = 1;
770fdbf62faSLisandro Dalcin   PetscInt     markerRight = 1, faceMarkerRight = 2;
771fdbf62faSLisandro Dalcin   PetscBool    wrap = (bd == DM_BOUNDARY_PERIODIC || bd == DM_BOUNDARY_TWIST) ? PETSC_TRUE : PETSC_FALSE;
772fdbf62faSLisandro Dalcin   PetscMPIInt  rank;
773fdbf62faSLisandro Dalcin 
774fdbf62faSLisandro Dalcin   PetscFunctionBegin;
7754f572ea9SToby Isaac   PetscAssertPointer(dm, 1);
776fdbf62faSLisandro Dalcin 
7779566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, 1));
7789566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
7799566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
780fdbf62faSLisandro Dalcin 
7819566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
782dd400576SPatrick Sanan   if (rank == 0) numCells = segments;
783dd400576SPatrick Sanan   if (rank == 0) numVerts = segments + (wrap ? 0 : 1);
784fdbf62faSLisandro Dalcin 
7859371c9d4SSatish Balay   numPoints[0] = numVerts;
7869371c9d4SSatish Balay   numPoints[1] = numCells;
7879566063dSJacob Faibussowitsch   PetscCall(PetscMalloc4(numCells + numVerts, &coneSize, numCells * 2, &cones, numCells + numVerts, &coneOrientations, numVerts, &vertexCoords));
7889566063dSJacob Faibussowitsch   PetscCall(PetscArrayzero(coneOrientations, numCells + numVerts));
789ad540459SPierre Jolivet   for (i = 0; i < numCells; ++i) coneSize[i] = 2;
790ad540459SPierre Jolivet   for (i = 0; i < numVerts; ++i) coneSize[numCells + i] = 0;
7919371c9d4SSatish Balay   for (i = 0; i < numCells; ++i) {
7929371c9d4SSatish Balay     cones[2 * i]     = numCells + i % numVerts;
7939371c9d4SSatish Balay     cones[2 * i + 1] = numCells + (i + 1) % numVerts;
7949371c9d4SSatish Balay   }
795ad540459SPierre Jolivet   for (i = 0; i < numVerts; ++i) vertexCoords[i] = lower + (upper - lower) * ((PetscReal)i / (PetscReal)numCells);
7969566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
7979566063dSJacob Faibussowitsch   PetscCall(PetscFree4(coneSize, cones, coneOrientations, vertexCoords));
798fdbf62faSLisandro Dalcin 
7999566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
8009371c9d4SSatish Balay   if (markerSeparate) {
8019371c9d4SSatish Balay     markerLeft  = faceMarkerLeft;
8029371c9d4SSatish Balay     markerRight = faceMarkerRight;
8039371c9d4SSatish Balay   }
804dd400576SPatrick Sanan   if (!wrap && rank == 0) {
8059566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
8069566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", fStart, markerLeft));
8079566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", fEnd - 1, markerRight));
8089566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "Face Sets", fStart, faceMarkerLeft));
8099566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "Face Sets", fEnd - 1, faceMarkerRight));
810fdbf62faSLisandro Dalcin   }
811fdbf62faSLisandro Dalcin   if (wrap) {
812fdbf62faSLisandro Dalcin     L       = upper - lower;
813fdbf62faSLisandro Dalcin     maxCell = (PetscReal)1.1 * (L / (PetscReal)PetscMax(1, segments));
8144fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, &maxCell, &lower, &L));
815fdbf62faSLisandro Dalcin   }
8169566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
8173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
818fdbf62faSLisandro Dalcin }
819fdbf62faSLisandro Dalcin 
820d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxMesh_Simplex_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate)
821d71ae5a4SJacob Faibussowitsch {
8229318fe57SMatthew G. Knepley   DM      boundary, vol;
823c22d3578SMatthew G. Knepley   DMLabel bdlabel;
824d6218766SMatthew G. Knepley 
825d6218766SMatthew G. Knepley   PetscFunctionBegin;
8264f572ea9SToby Isaac   PetscAssertPointer(dm, 1);
827c22d3578SMatthew 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");
8289566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &boundary));
8299566063dSJacob Faibussowitsch   PetscCall(DMSetType(boundary, DMPLEX));
8309566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(boundary, dim, faces, lower, upper, PETSC_FALSE));
8319566063dSJacob Faibussowitsch   PetscCall(DMPlexGenerate(boundary, NULL, interpolate, &vol));
832c22d3578SMatthew G. Knepley   PetscCall(DMGetLabel(vol, "marker", &bdlabel));
833c22d3578SMatthew G. Knepley   if (bdlabel) PetscCall(DMPlexLabelComplete(vol, bdlabel));
8345de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_FALSE, vol));
83569d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &vol));
8369566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&boundary));
8373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
838d6218766SMatthew G. Knepley }
839d6218766SMatthew G. Knepley 
840d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCubeMesh_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], DMBoundaryType bdX, DMBoundaryType bdY, DMBoundaryType bdZ)
841d71ae5a4SJacob Faibussowitsch {
842ed0e4b50SMatthew G. Knepley   DMLabel     cutLabel  = NULL;
843f4eb4c5dSMatthew G. Knepley   PetscInt    markerTop = 1, faceMarkerTop = 1;
844f4eb4c5dSMatthew G. Knepley   PetscInt    markerBottom = 1, faceMarkerBottom = 1;
845f4eb4c5dSMatthew G. Knepley   PetscInt    markerFront = 1, faceMarkerFront = 1;
846f4eb4c5dSMatthew G. Knepley   PetscInt    markerBack = 1, faceMarkerBack = 1;
847f4eb4c5dSMatthew G. Knepley   PetscInt    markerRight = 1, faceMarkerRight = 1;
848f4eb4c5dSMatthew G. Knepley   PetscInt    markerLeft = 1, faceMarkerLeft = 1;
8493dfda0b1SToby Isaac   PetscInt    dim;
850d8211ee3SMatthew G. Knepley   PetscBool   markerSeparate = PETSC_FALSE, cutMarker = PETSC_FALSE;
8513dfda0b1SToby Isaac   PetscMPIInt rank;
8523dfda0b1SToby Isaac 
8533dfda0b1SToby Isaac   PetscFunctionBegin;
8549566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
8559566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
8569566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
8579566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
8589566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL));
8599371c9d4SSatish 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) {
8609371c9d4SSatish Balay     if (cutMarker) {
8619371c9d4SSatish Balay       PetscCall(DMCreateLabel(dm, "periodic_cut"));
8629371c9d4SSatish Balay       PetscCall(DMGetLabel(dm, "periodic_cut", &cutLabel));
8639371c9d4SSatish Balay     }
864d8211ee3SMatthew G. Knepley   }
8653dfda0b1SToby Isaac   switch (dim) {
8663dfda0b1SToby Isaac   case 2:
867f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 3;
868f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
869f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 2;
870f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 4;
8713dfda0b1SToby Isaac     break;
8723dfda0b1SToby Isaac   case 3:
873f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
874f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 2;
875f4eb4c5dSMatthew G. Knepley     faceMarkerFront  = 3;
876f4eb4c5dSMatthew G. Knepley     faceMarkerBack   = 4;
877f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 5;
878f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 6;
8793dfda0b1SToby Isaac     break;
880d71ae5a4SJacob Faibussowitsch   default:
881d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Dimension %" PetscInt_FMT " not supported", dim);
8823dfda0b1SToby Isaac   }
8839566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
884f4eb4c5dSMatthew G. Knepley   if (markerSeparate) {
885f4eb4c5dSMatthew G. Knepley     markerBottom = faceMarkerBottom;
886f4eb4c5dSMatthew G. Knepley     markerTop    = faceMarkerTop;
887f4eb4c5dSMatthew G. Knepley     markerFront  = faceMarkerFront;
888f4eb4c5dSMatthew G. Knepley     markerBack   = faceMarkerBack;
889f4eb4c5dSMatthew G. Knepley     markerRight  = faceMarkerRight;
890f4eb4c5dSMatthew G. Knepley     markerLeft   = faceMarkerLeft;
8913dfda0b1SToby Isaac   }
8923dfda0b1SToby Isaac   {
893dd400576SPatrick Sanan     const PetscInt numXEdges    = rank == 0 ? edges[0] : 0;
894dd400576SPatrick Sanan     const PetscInt numYEdges    = rank == 0 ? edges[1] : 0;
895dd400576SPatrick Sanan     const PetscInt numZEdges    = rank == 0 ? edges[2] : 0;
896dd400576SPatrick Sanan     const PetscInt numXVertices = rank == 0 ? (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ? edges[0] : edges[0] + 1) : 0;
897dd400576SPatrick Sanan     const PetscInt numYVertices = rank == 0 ? (bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ? edges[1] : edges[1] + 1) : 0;
898dd400576SPatrick Sanan     const PetscInt numZVertices = rank == 0 ? (bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST ? edges[2] : edges[2] + 1) : 0;
8993dfda0b1SToby Isaac     const PetscInt numCells     = numXEdges * numYEdges * numZEdges;
9003dfda0b1SToby Isaac     const PetscInt numXFaces    = numYEdges * numZEdges;
9013dfda0b1SToby Isaac     const PetscInt numYFaces    = numXEdges * numZEdges;
9023dfda0b1SToby Isaac     const PetscInt numZFaces    = numXEdges * numYEdges;
9033dfda0b1SToby Isaac     const PetscInt numTotXFaces = numXVertices * numXFaces;
9043dfda0b1SToby Isaac     const PetscInt numTotYFaces = numYVertices * numYFaces;
9053dfda0b1SToby Isaac     const PetscInt numTotZFaces = numZVertices * numZFaces;
9063dfda0b1SToby Isaac     const PetscInt numFaces     = numTotXFaces + numTotYFaces + numTotZFaces;
9073dfda0b1SToby Isaac     const PetscInt numTotXEdges = numXEdges * numYVertices * numZVertices;
9083dfda0b1SToby Isaac     const PetscInt numTotYEdges = numYEdges * numXVertices * numZVertices;
9093dfda0b1SToby Isaac     const PetscInt numTotZEdges = numZEdges * numXVertices * numYVertices;
9103dfda0b1SToby Isaac     const PetscInt numVertices  = numXVertices * numYVertices * numZVertices;
9113dfda0b1SToby Isaac     const PetscInt numEdges     = numTotXEdges + numTotYEdges + numTotZEdges;
9123dfda0b1SToby Isaac     const PetscInt firstVertex  = (dim == 2) ? numFaces : numCells;
9133dfda0b1SToby Isaac     const PetscInt firstXFace   = (dim == 2) ? 0 : numCells + numVertices;
9143dfda0b1SToby Isaac     const PetscInt firstYFace   = firstXFace + numTotXFaces;
9153dfda0b1SToby Isaac     const PetscInt firstZFace   = firstYFace + numTotYFaces;
9163dfda0b1SToby Isaac     const PetscInt firstXEdge   = numCells + numFaces + numVertices;
9173dfda0b1SToby Isaac     const PetscInt firstYEdge   = firstXEdge + numTotXEdges;
9183dfda0b1SToby Isaac     const PetscInt firstZEdge   = firstYEdge + numTotYEdges;
9193dfda0b1SToby Isaac     Vec            coordinates;
9203dfda0b1SToby Isaac     PetscSection   coordSection;
9213dfda0b1SToby Isaac     PetscScalar   *coords;
9223dfda0b1SToby Isaac     PetscInt       coordSize;
9233dfda0b1SToby Isaac     PetscInt       v, vx, vy, vz;
9243dfda0b1SToby Isaac     PetscInt       c, f, fx, fy, fz, e, ex, ey, ez;
9253dfda0b1SToby Isaac 
9269566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells + numFaces + numEdges + numVertices));
92748a46eb9SPierre Jolivet     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 6));
92848a46eb9SPierre Jolivet     for (f = firstXFace; f < firstXFace + numFaces; ++f) PetscCall(DMPlexSetConeSize(dm, f, 4));
92948a46eb9SPierre Jolivet     for (e = firstXEdge; e < firstXEdge + numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2));
9309566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
9313dfda0b1SToby Isaac     /* Build cells */
9323dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
9333dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
9343dfda0b1SToby Isaac         for (fx = 0; fx < numXEdges; ++fx) {
9353dfda0b1SToby Isaac           PetscInt cell  = (fz * numYEdges + fy) * numXEdges + fx;
9363dfda0b1SToby Isaac           PetscInt faceB = firstZFace + (fy * numXEdges + fx) * numZVertices + fz;
9373dfda0b1SToby Isaac           PetscInt faceT = firstZFace + (fy * numXEdges + fx) * numZVertices + ((fz + 1) % numZVertices);
9383dfda0b1SToby Isaac           PetscInt faceF = firstYFace + (fz * numXEdges + fx) * numYVertices + fy;
9393dfda0b1SToby Isaac           PetscInt faceK = firstYFace + (fz * numXEdges + fx) * numYVertices + ((fy + 1) % numYVertices);
9403dfda0b1SToby Isaac           PetscInt faceL = firstXFace + (fz * numYEdges + fy) * numXVertices + fx;
9413dfda0b1SToby Isaac           PetscInt faceR = firstXFace + (fz * numYEdges + fy) * numXVertices + ((fx + 1) % numXVertices);
9423dfda0b1SToby Isaac           /* B,  T,  F,  K,  R,  L */
943b5a892a1SMatthew G. Knepley           PetscInt ornt[6] = {-2, 0, 0, -3, 0, -2}; /* ??? */
94442206facSLisandro Dalcin           PetscInt cone[6];
9453dfda0b1SToby Isaac 
9463dfda0b1SToby Isaac           /* no boundary twisting in 3D */
9479371c9d4SSatish Balay           cone[0] = faceB;
9489371c9d4SSatish Balay           cone[1] = faceT;
9499371c9d4SSatish Balay           cone[2] = faceF;
9509371c9d4SSatish Balay           cone[3] = faceK;
9519371c9d4SSatish Balay           cone[4] = faceR;
9529371c9d4SSatish Balay           cone[5] = faceL;
9539566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, cell, cone));
9549566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, cell, ornt));
9559566063dSJacob Faibussowitsch           if (bdX != DM_BOUNDARY_NONE && fx == numXEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
9569566063dSJacob Faibussowitsch           if (bdY != DM_BOUNDARY_NONE && fy == numYEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
9579566063dSJacob Faibussowitsch           if (bdZ != DM_BOUNDARY_NONE && fz == numZEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
9583dfda0b1SToby Isaac         }
9593dfda0b1SToby Isaac       }
9603dfda0b1SToby Isaac     }
9613dfda0b1SToby Isaac     /* Build x faces */
9623dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
9633dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
9643dfda0b1SToby Isaac         for (fx = 0; fx < numXVertices; ++fx) {
9653dfda0b1SToby Isaac           PetscInt face    = firstXFace + (fz * numYEdges + fy) * numXVertices + fx;
9663dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy * numXVertices + fx) * numZEdges + fz;
9673dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (((fy + 1) % numYVertices) * numXVertices + fx) * numZEdges + fz;
9683dfda0b1SToby Isaac           PetscInt edgeB   = firstYEdge + (fz * numXVertices + fx) * numYEdges + fy;
9693dfda0b1SToby Isaac           PetscInt edgeT   = firstYEdge + (((fz + 1) % numZVertices) * numXVertices + fx) * numYEdges + fy;
970b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
9713dfda0b1SToby Isaac           PetscInt cone[4];
9723dfda0b1SToby Isaac 
9733dfda0b1SToby Isaac           if (dim == 3) {
9743dfda0b1SToby Isaac             /* markers */
9753dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
9763dfda0b1SToby Isaac               if (fx == numXVertices - 1) {
9779566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerRight));
9789566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerRight));
9799371c9d4SSatish Balay               } else if (fx == 0) {
9809566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerLeft));
9819566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerLeft));
9823dfda0b1SToby Isaac               }
9833dfda0b1SToby Isaac             }
9843dfda0b1SToby Isaac           }
9859371c9d4SSatish Balay           cone[0] = edgeB;
9869371c9d4SSatish Balay           cone[1] = edgeR;
9879371c9d4SSatish Balay           cone[2] = edgeT;
9889371c9d4SSatish Balay           cone[3] = edgeL;
9899566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
9909566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
9913dfda0b1SToby Isaac         }
9923dfda0b1SToby Isaac       }
9933dfda0b1SToby Isaac     }
9943dfda0b1SToby Isaac     /* Build y faces */
9953dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
99642206facSLisandro Dalcin       for (fx = 0; fx < numXEdges; ++fx) {
9973dfda0b1SToby Isaac         for (fy = 0; fy < numYVertices; ++fy) {
9983dfda0b1SToby Isaac           PetscInt face    = firstYFace + (fz * numXEdges + fx) * numYVertices + fy;
9993dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy * numXVertices + fx) * numZEdges + fz;
10003dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (fy * numXVertices + ((fx + 1) % numXVertices)) * numZEdges + fz;
10013dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz * numYVertices + fy) * numXEdges + fx;
10023dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (((fz + 1) % numZVertices) * numYVertices + fy) * numXEdges + fx;
1003b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
10043dfda0b1SToby Isaac           PetscInt cone[4];
10053dfda0b1SToby Isaac 
10063dfda0b1SToby Isaac           if (dim == 3) {
10073dfda0b1SToby Isaac             /* markers */
10083dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
10093dfda0b1SToby Isaac               if (fy == numYVertices - 1) {
10109566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerBack));
10119566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerBack));
10129371c9d4SSatish Balay               } else if (fy == 0) {
10139566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerFront));
10149566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerFront));
10153dfda0b1SToby Isaac               }
10163dfda0b1SToby Isaac             }
10173dfda0b1SToby Isaac           }
10189371c9d4SSatish Balay           cone[0] = edgeB;
10199371c9d4SSatish Balay           cone[1] = edgeR;
10209371c9d4SSatish Balay           cone[2] = edgeT;
10219371c9d4SSatish Balay           cone[3] = edgeL;
10229566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
10239566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
10243dfda0b1SToby Isaac         }
10253dfda0b1SToby Isaac       }
10263dfda0b1SToby Isaac     }
10273dfda0b1SToby Isaac     /* Build z faces */
10283dfda0b1SToby Isaac     for (fy = 0; fy < numYEdges; ++fy) {
10293dfda0b1SToby Isaac       for (fx = 0; fx < numXEdges; ++fx) {
10303dfda0b1SToby Isaac         for (fz = 0; fz < numZVertices; fz++) {
10313dfda0b1SToby Isaac           PetscInt face    = firstZFace + (fy * numXEdges + fx) * numZVertices + fz;
10323dfda0b1SToby Isaac           PetscInt edgeL   = firstYEdge + (fz * numXVertices + fx) * numYEdges + fy;
10333dfda0b1SToby Isaac           PetscInt edgeR   = firstYEdge + (fz * numXVertices + ((fx + 1) % numXVertices)) * numYEdges + fy;
10343dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz * numYVertices + fy) * numXEdges + fx;
10353dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (fz * numYVertices + ((fy + 1) % numYVertices)) * numXEdges + fx;
1036b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
10373dfda0b1SToby Isaac           PetscInt cone[4];
10383dfda0b1SToby Isaac 
10393dfda0b1SToby Isaac           if (dim == 2) {
10409371c9d4SSatish Balay             if (bdX == DM_BOUNDARY_TWIST && fx == numXEdges - 1) {
10419371c9d4SSatish Balay               edgeR += numYEdges - 1 - 2 * fy;
10429371c9d4SSatish Balay               ornt[1] = -1;
10439371c9d4SSatish Balay             }
10449371c9d4SSatish Balay             if (bdY == DM_BOUNDARY_TWIST && fy == numYEdges - 1) {
10459371c9d4SSatish Balay               edgeT += numXEdges - 1 - 2 * fx;
10469371c9d4SSatish Balay               ornt[2] = 0;
10479371c9d4SSatish Balay             }
10489566063dSJacob Faibussowitsch             if (bdX != DM_BOUNDARY_NONE && fx == numXEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, face, 2));
10499566063dSJacob Faibussowitsch             if (bdY != DM_BOUNDARY_NONE && fy == numYEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, face, 2));
1050d1c88043SMatthew G. Knepley           } else {
10513dfda0b1SToby Isaac             /* markers */
10523dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
10533dfda0b1SToby Isaac               if (fz == numZVertices - 1) {
10549566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerTop));
10559566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerTop));
10569371c9d4SSatish Balay               } else if (fz == 0) {
10579566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerBottom));
10589566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerBottom));
10593dfda0b1SToby Isaac               }
10603dfda0b1SToby Isaac             }
10613dfda0b1SToby Isaac           }
10629371c9d4SSatish Balay           cone[0] = edgeB;
10639371c9d4SSatish Balay           cone[1] = edgeR;
10649371c9d4SSatish Balay           cone[2] = edgeT;
10659371c9d4SSatish Balay           cone[3] = edgeL;
10669566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
10679566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
10683dfda0b1SToby Isaac         }
10693dfda0b1SToby Isaac       }
10703dfda0b1SToby Isaac     }
10713dfda0b1SToby Isaac     /* Build Z edges*/
10723dfda0b1SToby Isaac     for (vy = 0; vy < numYVertices; vy++) {
10733dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
10743dfda0b1SToby Isaac         for (ez = 0; ez < numZEdges; ez++) {
10753dfda0b1SToby Isaac           const PetscInt edge    = firstZEdge + (vy * numXVertices + vx) * numZEdges + ez;
10763dfda0b1SToby Isaac           const PetscInt vertexB = firstVertex + (ez * numYVertices + vy) * numXVertices + vx;
10773dfda0b1SToby Isaac           const PetscInt vertexT = firstVertex + (((ez + 1) % numZVertices) * numYVertices + vy) * numXVertices + vx;
10783dfda0b1SToby Isaac           PetscInt       cone[2];
10793dfda0b1SToby Isaac 
10809371c9d4SSatish Balay           cone[0] = vertexB;
10819371c9d4SSatish Balay           cone[1] = vertexT;
1082c2df9bbfSMatthew G. Knepley           PetscCall(DMPlexSetCone(dm, edge, cone));
10833dfda0b1SToby Isaac           if (dim == 3) {
10843dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
10853dfda0b1SToby Isaac               if (vx == numXVertices - 1) {
10869566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
1087c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1088c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1089c2df9bbfSMatthew G. Knepley               } else if (vx == 0) {
10909566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
1091c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1092c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
10933dfda0b1SToby Isaac               }
10943dfda0b1SToby Isaac             }
10953dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
10963dfda0b1SToby Isaac               if (vy == numYVertices - 1) {
10979566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBack));
1098c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBack));
1099c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBack));
1100c2df9bbfSMatthew G. Knepley               } else if (vy == 0) {
11019566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerFront));
1102c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerFront));
1103c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerFront));
11043dfda0b1SToby Isaac               }
11053dfda0b1SToby Isaac             }
11063dfda0b1SToby Isaac           }
11073dfda0b1SToby Isaac         }
11083dfda0b1SToby Isaac       }
11093dfda0b1SToby Isaac     }
11103dfda0b1SToby Isaac     /* Build Y edges*/
11113dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
11123dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
11133dfda0b1SToby Isaac         for (ey = 0; ey < numYEdges; ey++) {
11143dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdY == DM_BOUNDARY_TWIST && ey == numYEdges - 1) ? (numXVertices - vx - 1) : (vz * numYVertices + ((ey + 1) % numYVertices)) * numXVertices + vx;
11153dfda0b1SToby Isaac           const PetscInt edge    = firstYEdge + (vz * numXVertices + vx) * numYEdges + ey;
11163dfda0b1SToby Isaac           const PetscInt vertexF = firstVertex + (vz * numYVertices + ey) * numXVertices + vx;
11173dfda0b1SToby Isaac           const PetscInt vertexK = firstVertex + nextv;
11183dfda0b1SToby Isaac           PetscInt       cone[2];
11193dfda0b1SToby Isaac 
11209371c9d4SSatish Balay           cone[0] = vertexF;
11219371c9d4SSatish Balay           cone[1] = vertexK;
11229566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, edge, cone));
11233dfda0b1SToby Isaac           if (dim == 2) {
11243dfda0b1SToby Isaac             if ((bdX != DM_BOUNDARY_PERIODIC) && (bdX != DM_BOUNDARY_TWIST)) {
11253dfda0b1SToby Isaac               if (vx == numXVertices - 1) {
11269566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerRight));
11279566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
11289566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1129c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1130d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
11319566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerLeft));
11329566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
11339566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1134c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
11353dfda0b1SToby Isaac               }
1136d8211ee3SMatthew G. Knepley             } else {
11374c67ea77SStefano Zampini               if (vx == 0 && cutLabel) {
11389566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, edge, 1));
11399566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, cone[0], 1));
1140c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMLabelSetValue(cutLabel, cone[1], 1));
11413dfda0b1SToby Isaac               }
1142d8211ee3SMatthew G. Knepley             }
1143d8211ee3SMatthew G. Knepley           } else {
11443dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
11453dfda0b1SToby Isaac               if (vx == numXVertices - 1) {
11469566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
1147c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1148c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1149d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
11509566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
1151c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1152c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
11533dfda0b1SToby Isaac               }
11543dfda0b1SToby Isaac             }
11553dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
11563dfda0b1SToby Isaac               if (vz == numZVertices - 1) {
11579566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
1158c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1159c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1160d8211ee3SMatthew G. Knepley               } else if (vz == 0) {
11619566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
1162c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1163c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
11643dfda0b1SToby Isaac               }
11653dfda0b1SToby Isaac             }
11663dfda0b1SToby Isaac           }
11673dfda0b1SToby Isaac         }
11683dfda0b1SToby Isaac       }
11693dfda0b1SToby Isaac     }
11703dfda0b1SToby Isaac     /* Build X edges*/
11713dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
11723dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; vy++) {
11733dfda0b1SToby Isaac         for (ex = 0; ex < numXEdges; ex++) {
11743dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdX == DM_BOUNDARY_TWIST && ex == numXEdges - 1) ? (numYVertices - vy - 1) * numXVertices : (vz * numYVertices + vy) * numXVertices + (ex + 1) % numXVertices;
11753dfda0b1SToby Isaac           const PetscInt edge    = firstXEdge + (vz * numYVertices + vy) * numXEdges + ex;
11763dfda0b1SToby Isaac           const PetscInt vertexL = firstVertex + (vz * numYVertices + vy) * numXVertices + ex;
11773dfda0b1SToby Isaac           const PetscInt vertexR = firstVertex + nextv;
11783dfda0b1SToby Isaac           PetscInt       cone[2];
11793dfda0b1SToby Isaac 
11809371c9d4SSatish Balay           cone[0] = vertexL;
11819371c9d4SSatish Balay           cone[1] = vertexR;
11829566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, edge, cone));
11833dfda0b1SToby Isaac           if (dim == 2) {
11843dfda0b1SToby Isaac             if ((bdY != DM_BOUNDARY_PERIODIC) && (bdY != DM_BOUNDARY_TWIST)) {
11853dfda0b1SToby Isaac               if (vy == numYVertices - 1) {
11869566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerTop));
11879566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
11889566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1189c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1190d8211ee3SMatthew G. Knepley               } else if (vy == 0) {
11919566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerBottom));
11929566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
11939566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1194c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
11953dfda0b1SToby Isaac               }
1196d8211ee3SMatthew G. Knepley             } else {
11974c67ea77SStefano Zampini               if (vy == 0 && cutLabel) {
11989566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, edge, 1));
11999566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, cone[0], 1));
1200c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMLabelSetValue(cutLabel, cone[1], 1));
12013dfda0b1SToby Isaac               }
1202d8211ee3SMatthew G. Knepley             }
1203d8211ee3SMatthew G. Knepley           } else {
12043dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
12053dfda0b1SToby Isaac               if (vy == numYVertices - 1) {
12069566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBack));
1207c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBack));
1208c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBack));
1209c2df9bbfSMatthew G. Knepley               } else if (vy == 0) {
12109566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerFront));
1211c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerFront));
1212c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerFront));
12133dfda0b1SToby Isaac               }
12143dfda0b1SToby Isaac             }
12153dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
12163dfda0b1SToby Isaac               if (vz == numZVertices - 1) {
12179566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
1218c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1219c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1220c2df9bbfSMatthew G. Knepley               } else if (vz == 0) {
12219566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
1222c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1223c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
12243dfda0b1SToby Isaac               }
12253dfda0b1SToby Isaac             }
12263dfda0b1SToby Isaac           }
12273dfda0b1SToby Isaac         }
12283dfda0b1SToby Isaac       }
12293dfda0b1SToby Isaac     }
12309566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
12319566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
12323dfda0b1SToby Isaac     /* Build coordinates */
12339566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
12349566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
12359566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
12369566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numVertices));
12373dfda0b1SToby Isaac     for (v = firstVertex; v < firstVertex + numVertices; ++v) {
12389566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
12399566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
12403dfda0b1SToby Isaac     }
12419566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
12429566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
12439566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
12449566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
12459566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
12469566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
12479566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates, VECSTANDARD));
12489566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
12493dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; ++vz) {
12503dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; ++vy) {
12513dfda0b1SToby Isaac         for (vx = 0; vx < numXVertices; ++vx) {
12523dfda0b1SToby Isaac           coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 0] = lower[0] + ((upper[0] - lower[0]) / numXEdges) * vx;
12533dfda0b1SToby Isaac           coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 1] = lower[1] + ((upper[1] - lower[1]) / numYEdges) * vy;
1254ad540459SPierre Jolivet           if (dim == 3) coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 2] = lower[2] + ((upper[2] - lower[2]) / numZEdges) * vz;
12553dfda0b1SToby Isaac         }
12563dfda0b1SToby Isaac       }
12573dfda0b1SToby Isaac     }
12589566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
12599566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
12609566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
12613dfda0b1SToby Isaac   }
12623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12633dfda0b1SToby Isaac }
12643dfda0b1SToby Isaac 
1265d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxMesh_Tensor_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
1266d71ae5a4SJacob Faibussowitsch {
12679318fe57SMatthew G. Knepley   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
12689318fe57SMatthew G. Knepley   PetscInt       fac[3] = {0, 0, 0}, d;
1269552f7358SJed Brown 
1270552f7358SJed Brown   PetscFunctionBegin;
12714f572ea9SToby Isaac   PetscAssertPointer(dm, 1);
12729318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
12739566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
12749371c9d4SSatish Balay   for (d = 0; d < dim; ++d) {
12759371c9d4SSatish Balay     fac[d] = faces[d];
12769371c9d4SSatish Balay     bdt[d] = periodicity[d];
12779371c9d4SSatish Balay   }
12789566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateCubeMesh_Internal(dm, lower, upper, fac, bdt[0], bdt[1], bdt[2]));
12799371c9d4SSatish 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))) {
12806858538eSMatthew G. Knepley     PetscReal L[3]       = {-1., -1., 0.};
12816858538eSMatthew G. Knepley     PetscReal maxCell[3] = {-1., -1., 0.};
1282552f7358SJed Brown 
12839318fe57SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
12846858538eSMatthew G. Knepley       if (periodicity[d] != DM_BOUNDARY_NONE) {
12859318fe57SMatthew G. Knepley         L[d]       = upper[d] - lower[d];
12869318fe57SMatthew G. Knepley         maxCell[d] = 1.1 * (L[d] / PetscMax(1, faces[d]));
1287768d5fceSMatthew G. Knepley       }
12886858538eSMatthew G. Knepley     }
12894fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, maxCell, lower, L));
1290768d5fceSMatthew G. Knepley   }
12919566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
12923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12939318fe57SMatthew G. Knepley }
12949318fe57SMatthew G. Knepley 
12955dca41c3SJed 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)
1296d71ae5a4SJacob Faibussowitsch {
12979318fe57SMatthew G. Knepley   PetscFunctionBegin;
129846139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
12995dca41c3SJed Brown   if (shape == DM_SHAPE_ZBOX) PetscCall(DMPlexCreateBoxMesh_Tensor_SFC_Internal(dm, dim, faces, lower, upper, periodicity, interpolate));
13006725e60dSJed Brown   else if (dim == 1) PetscCall(DMPlexCreateLineMesh_Internal(dm, faces[0], lower[0], upper[0], periodicity[0]));
13019566063dSJacob Faibussowitsch   else if (simplex) PetscCall(DMPlexCreateBoxMesh_Simplex_Internal(dm, dim, faces, lower, upper, periodicity, interpolate));
13029566063dSJacob Faibussowitsch   else PetscCall(DMPlexCreateBoxMesh_Tensor_Internal(dm, dim, faces, lower, upper, periodicity));
13039318fe57SMatthew G. Knepley   if (!interpolate && dim > 1 && !simplex) {
1304768d5fceSMatthew G. Knepley     DM udm;
1305768d5fceSMatthew G. Knepley 
13069566063dSJacob Faibussowitsch     PetscCall(DMPlexUninterpolate(dm, &udm));
13079566063dSJacob Faibussowitsch     PetscCall(DMPlexCopyCoordinates(dm, udm));
130869d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &udm));
1309768d5fceSMatthew G. Knepley   }
131046139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
13113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1312c8c68bd8SToby Isaac }
1313c8c68bd8SToby Isaac 
1314768d5fceSMatthew G. Knepley /*@C
1315768d5fceSMatthew G. Knepley   DMPlexCreateBoxMesh - Creates a mesh on the tensor product of unit intervals (box) using simplices or tensor cells (hexahedra).
1316768d5fceSMatthew G. Knepley 
1317d083f849SBarry Smith   Collective
1318768d5fceSMatthew G. Knepley 
1319768d5fceSMatthew G. Knepley   Input Parameters:
1320a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
1321768d5fceSMatthew G. Knepley . dim         - The spatial dimension
1322a1cb98faSBarry Smith . simplex     - `PETSC_TRUE` for simplices, `PETSC_FALSE` for tensor cells
132320f4b53cSBarry Smith . faces       - Number of faces per dimension, or `NULL` for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
132420f4b53cSBarry Smith . lower       - The lower left corner, or `NULL` for (0, 0, 0)
132520f4b53cSBarry Smith . upper       - The upper right corner, or `NULL` for (1, 1, 1)
132620f4b53cSBarry Smith . periodicity - The boundary type for the X,Y,Z direction, or `NULL` for `DM_BOUNDARY_NONE`
1327768d5fceSMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
1328768d5fceSMatthew G. Knepley 
1329768d5fceSMatthew G. Knepley   Output Parameter:
1330a1cb98faSBarry Smith . dm - The `DM` object
1331768d5fceSMatthew G. Knepley 
1332768d5fceSMatthew G. Knepley   Level: beginner
1333768d5fceSMatthew G. Knepley 
1334a1cb98faSBarry Smith   Note:
1335a1cb98faSBarry Smith   To customize this mesh using options, use
1336a1cb98faSBarry Smith .vb
1337a1cb98faSBarry Smith   DMCreate(comm, &dm);
1338a1cb98faSBarry Smith   DMSetType(dm, DMPLEX);
1339a1cb98faSBarry Smith   DMSetFromOptions(dm);
1340a1cb98faSBarry Smith .ve
1341a1cb98faSBarry Smith   and use the options in `DMSetFromOptions()`.
1342a1cb98faSBarry Smith 
1343a4e35b19SJacob Faibussowitsch   Here is the numbering returned for 2 faces in each direction for tensor cells\:
1344a1cb98faSBarry Smith .vb
1345a1cb98faSBarry Smith  10---17---11---18----12
1346a1cb98faSBarry Smith   |         |         |
1347a1cb98faSBarry Smith   |         |         |
1348a1cb98faSBarry Smith  20    2   22    3    24
1349a1cb98faSBarry Smith   |         |         |
1350a1cb98faSBarry Smith   |         |         |
1351a1cb98faSBarry Smith   7---15----8---16----9
1352a1cb98faSBarry Smith   |         |         |
1353a1cb98faSBarry Smith   |         |         |
1354a1cb98faSBarry Smith  19    0   21    1   23
1355a1cb98faSBarry Smith   |         |         |
1356a1cb98faSBarry Smith   |         |         |
1357a1cb98faSBarry Smith   4---13----5---14----6
1358a1cb98faSBarry Smith .ve
1359a1cb98faSBarry Smith   and for simplicial cells
1360a1cb98faSBarry Smith .vb
1361a1cb98faSBarry Smith  14----8---15----9----16
1362a1cb98faSBarry Smith   |\     5  |\      7 |
1363a1cb98faSBarry Smith   | \       | \       |
1364a1cb98faSBarry Smith  13   2    14    3    15
1365a1cb98faSBarry Smith   | 4   \   | 6   \   |
1366a1cb98faSBarry Smith   |       \ |       \ |
1367a1cb98faSBarry Smith  11----6---12----7----13
1368a1cb98faSBarry Smith   |\        |\        |
1369a1cb98faSBarry Smith   | \    1  | \     3 |
1370a1cb98faSBarry Smith  10   0    11    1    12
1371a1cb98faSBarry Smith   | 0   \   | 2   \   |
1372a1cb98faSBarry Smith   |       \ |       \ |
1373a1cb98faSBarry Smith   8----4----9----5----10
1374a1cb98faSBarry Smith .ve
1375a1cb98faSBarry Smith 
13761cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMSetFromOptions()`, `DMPlexCreateFromFile()`, `DMPlexCreateHexCylinderMesh()`, `DMSetType()`, `DMCreate()`
1377768d5fceSMatthew G. Knepley @*/
1378d71ae5a4SJacob 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)
1379d71ae5a4SJacob Faibussowitsch {
13809318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
1381fdbf62faSLisandro Dalcin   PetscReal      low[3] = {0, 0, 0};
1382fdbf62faSLisandro Dalcin   PetscReal      upp[3] = {1, 1, 1};
1383fdbf62faSLisandro Dalcin   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
1384552f7358SJed Brown 
1385768d5fceSMatthew G. Knepley   PetscFunctionBegin;
13869566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
13879566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
13885dca41c3SJed Brown   PetscCall(DMPlexCreateBoxMesh_Internal(*dm, DM_SHAPE_BOX, dim, simplex, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt, interpolate));
13897ff04441SMatthew G. Knepley   if (periodicity) PetscCall(DMLocalizeCoordinates(*dm));
13903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13919318fe57SMatthew G. Knepley }
1392fdbf62faSLisandro Dalcin 
1393d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateWedgeBoxMesh_Internal(DM dm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
1394d71ae5a4SJacob Faibussowitsch {
13959318fe57SMatthew G. Knepley   DM       bdm, vol;
13969318fe57SMatthew G. Knepley   PetscInt i;
13979318fe57SMatthew G. Knepley 
13989318fe57SMatthew G. Knepley   PetscFunctionBegin;
13991fcf445aSMatthew G. Knepley   // TODO Now we can support periodicity
140008401ef6SPierre Jolivet   for (i = 0; i < 3; ++i) PetscCheck(periodicity[i] == DM_BOUNDARY_NONE, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Periodicity not yet supported");
14019566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &bdm));
14029566063dSJacob Faibussowitsch   PetscCall(DMSetType(bdm, DMPLEX));
14039566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(bdm, 2));
140446139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, bdm, 0, 0, 0));
14059566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxMesh_Simplex_Internal(bdm, 2, faces, lower, upper, periodicity, PETSC_TRUE));
14061fcf445aSMatthew G. Knepley   PetscCall(DMPlexExtrude(bdm, faces[2], upper[2] - lower[2], PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, NULL, NULL, &vol));
140746139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, bdm, 0, 0, 0));
14089566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&bdm));
140969d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &vol));
14109318fe57SMatthew G. Knepley   if (lower[2] != 0.0) {
14119318fe57SMatthew G. Knepley     Vec          v;
14129318fe57SMatthew G. Knepley     PetscScalar *x;
14139318fe57SMatthew G. Knepley     PetscInt     cDim, n;
14149318fe57SMatthew G. Knepley 
14159566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &v));
14169566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(v, &cDim));
14179566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(v, &n));
14189566063dSJacob Faibussowitsch     PetscCall(VecGetArray(v, &x));
14199318fe57SMatthew G. Knepley     x += cDim;
14209318fe57SMatthew G. Knepley     for (i = 0; i < n; i += cDim) x[i] += lower[2];
14219566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(v, &x));
14229566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, v));
14239318fe57SMatthew G. Knepley   }
14243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1425552f7358SJed Brown }
1426552f7358SJed Brown 
142700dabe28SStefano Zampini /*@
142800dabe28SStefano Zampini   DMPlexCreateWedgeBoxMesh - Creates a 3-D mesh tesselating the (x,y) plane and extruding in the third direction using wedge cells.
142900dabe28SStefano Zampini 
1430d083f849SBarry Smith   Collective
143100dabe28SStefano Zampini 
143200dabe28SStefano Zampini   Input Parameters:
1433a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
143420f4b53cSBarry Smith . faces       - Number of faces per dimension, or `NULL` for (1, 1, 1)
143520f4b53cSBarry Smith . lower       - The lower left corner, or `NULL` for (0, 0, 0)
143620f4b53cSBarry Smith . upper       - The upper right corner, or `NULL` for (1, 1, 1)
143720f4b53cSBarry Smith . periodicity - The boundary type for the X,Y,Z direction, or `NULL` for `DM_BOUNDARY_NONE`
1438a1cb98faSBarry Smith . orderHeight - If `PETSC_TRUE`, orders the extruded cells in the height first. Otherwise, orders the cell on the layers first
143900dabe28SStefano Zampini - interpolate - Flag to create intermediate mesh pieces (edges, faces)
144000dabe28SStefano Zampini 
144100dabe28SStefano Zampini   Output Parameter:
1442a1cb98faSBarry Smith . dm - The `DM` object
144300dabe28SStefano Zampini 
144400dabe28SStefano Zampini   Level: beginner
144500dabe28SStefano Zampini 
14461cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateHexCylinderMesh()`, `DMPlexCreateWedgeCylinderMesh()`, `DMExtrude()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
144700dabe28SStefano Zampini @*/
1448d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateWedgeBoxMesh(MPI_Comm comm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool orderHeight, PetscBool interpolate, DM *dm)
1449d71ae5a4SJacob Faibussowitsch {
14509318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
145100dabe28SStefano Zampini   PetscReal      low[3] = {0, 0, 0};
145200dabe28SStefano Zampini   PetscReal      upp[3] = {1, 1, 1};
145300dabe28SStefano Zampini   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
145400dabe28SStefano Zampini 
145500dabe28SStefano Zampini   PetscFunctionBegin;
14569566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
14579566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
14589566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateWedgeBoxMesh_Internal(*dm, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt));
1459d410b0cfSMatthew G. Knepley   if (!interpolate) {
1460d410b0cfSMatthew G. Knepley     DM udm;
146100dabe28SStefano Zampini 
14629566063dSJacob Faibussowitsch     PetscCall(DMPlexUninterpolate(*dm, &udm));
146369d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(*dm, &udm));
146400dabe28SStefano Zampini   }
14657ff04441SMatthew G. Knepley   if (periodicity) PetscCall(DMLocalizeCoordinates(*dm));
14663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
146700dabe28SStefano Zampini }
146800dabe28SStefano Zampini 
1469cfb853baSMatthew G. Knepley /*
1470cfb853baSMatthew 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.
1471cfb853baSMatthew G. Knepley 
1472cfb853baSMatthew G. Knepley   Input Parameters:
1473cfb853baSMatthew G. Knepley + len - The length of the tuple
1474cfb853baSMatthew G. Knepley . max - The maximum for each dimension, so values are in [0, max)
1475cfb853baSMatthew G. Knepley - tup - A tuple of length len+1: tup[len] > 0 indicates a stopping condition
1476cfb853baSMatthew G. Knepley 
1477cfb853baSMatthew G. Knepley   Output Parameter:
147820f4b53cSBarry Smith . tup - A tuple of `len` integers whose entries are at most `max`
1479cfb853baSMatthew G. Knepley 
1480cfb853baSMatthew G. Knepley   Level: developer
1481cfb853baSMatthew G. Knepley 
148220f4b53cSBarry Smith   Note:
148320f4b53cSBarry Smith   Ordering is lexicographic with lowest index as least significant in ordering.
148420f4b53cSBarry 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}.
148520f4b53cSBarry Smith 
1486cfb853baSMatthew G. Knepley .seealso: PetscDualSpaceTensorPointLexicographic_Internal(), PetscDualSpaceLatticePointLexicographic_Internal()
1487cfb853baSMatthew G. Knepley */
1488cfb853baSMatthew G. Knepley static PetscErrorCode DMPlexTensorPointLexicographic_Private(PetscInt len, const PetscInt max[], PetscInt tup[])
1489cfb853baSMatthew G. Knepley {
1490cfb853baSMatthew G. Knepley   PetscInt i;
1491cfb853baSMatthew G. Knepley 
1492cfb853baSMatthew G. Knepley   PetscFunctionBegin;
1493cfb853baSMatthew G. Knepley   for (i = 0; i < len; ++i) {
1494cfb853baSMatthew G. Knepley     if (tup[i] < max[i] - 1) {
1495cfb853baSMatthew G. Knepley       break;
1496cfb853baSMatthew G. Knepley     } else {
1497cfb853baSMatthew G. Knepley       tup[i] = 0;
1498cfb853baSMatthew G. Knepley     }
1499cfb853baSMatthew G. Knepley   }
1500cfb853baSMatthew G. Knepley   if (i == len) tup[i - 1] = max[i - 1];
1501cfb853baSMatthew G. Knepley   else ++tup[i];
15023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1503cfb853baSMatthew G. Knepley }
1504cfb853baSMatthew G. Knepley 
1505cfb853baSMatthew G. Knepley static PetscInt TupleToIndex_Private(PetscInt len, const PetscInt max[], const PetscInt tup[])
1506cfb853baSMatthew G. Knepley {
1507cfb853baSMatthew G. Knepley   PetscInt i, idx = tup[len - 1];
1508cfb853baSMatthew G. Knepley 
1509cfb853baSMatthew G. Knepley   for (i = len - 2; i >= 0; --i) {
1510cfb853baSMatthew G. Knepley     idx *= max[i];
1511cfb853baSMatthew G. Knepley     idx += tup[i];
1512cfb853baSMatthew G. Knepley   }
1513cfb853baSMatthew G. Knepley   return idx;
1514cfb853baSMatthew G. Knepley }
1515cfb853baSMatthew G. Knepley 
1516cfb853baSMatthew G. Knepley static PetscErrorCode DestroyExtent_Private(void *extent)
1517cfb853baSMatthew G. Knepley {
1518cfb853baSMatthew G. Knepley   return PetscFree(extent);
1519cfb853baSMatthew G. Knepley }
1520cfb853baSMatthew G. Knepley 
1521cfb853baSMatthew G. Knepley static PetscErrorCode DMPlexCreateHypercubicMesh_Internal(DM dm, PetscInt dim, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], const DMBoundaryType bd[])
1522cfb853baSMatthew G. Knepley {
1523cfb853baSMatthew G. Knepley   Vec          coordinates;
1524cfb853baSMatthew G. Knepley   PetscSection coordSection;
1525cfb853baSMatthew G. Knepley   DMLabel      cutLabel    = NULL;
1526cfb853baSMatthew G. Knepley   PetscBool    cutMarker   = PETSC_FALSE;
1527cfb853baSMatthew G. Knepley   PetscBool    periodic    = PETSC_FALSE;
1528cfb853baSMatthew G. Knepley   PetscInt     numCells    = 1, c;
1529cfb853baSMatthew G. Knepley   PetscInt     numVertices = 1, v;
1530cfb853baSMatthew G. Knepley   PetscScalar *coords;
1531cfb853baSMatthew G. Knepley   PetscInt    *vertices, *vert, *vtmp, *supp, cone[2];
1532cfb853baSMatthew G. Knepley   PetscInt     d, e, cell = 0, coordSize;
1533cfb853baSMatthew G. Knepley   PetscMPIInt  rank;
1534cfb853baSMatthew G. Knepley 
1535cfb853baSMatthew G. Knepley   PetscFunctionBegin;
1536cfb853baSMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
1537cfb853baSMatthew G. Knepley   PetscCall(DMSetDimension(dm, dim));
1538cfb853baSMatthew G. Knepley   PetscCall(PetscCalloc4(dim, &vertices, dim, &vert, dim, &vtmp, 2 * dim, &supp));
1539cfb853baSMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "marker"));
1540cfb853baSMatthew G. Knepley   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL));
1541cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) periodic = (periodic || bd[d] == DM_BOUNDARY_PERIODIC) ? PETSC_TRUE : PETSC_FALSE;
1542cfb853baSMatthew G. Knepley   if (periodic && cutMarker) {
1543cfb853baSMatthew G. Knepley     PetscCall(DMCreateLabel(dm, "periodic_cut"));
1544cfb853baSMatthew G. Knepley     PetscCall(DMGetLabel(dm, "periodic_cut", &cutLabel));
1545cfb853baSMatthew G. Knepley   }
1546cfb853baSMatthew 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");
1547cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) {
1548cfb853baSMatthew G. Knepley     vertices[d] = edges[d];
1549cfb853baSMatthew G. Knepley     numVertices *= vertices[d];
1550cfb853baSMatthew G. Knepley   }
1551cfb853baSMatthew G. Knepley   numCells = numVertices * dim;
1552cfb853baSMatthew G. Knepley   PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
1553cfb853baSMatthew G. Knepley   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, 2));
1554cfb853baSMatthew G. Knepley   for (v = numCells; v < numCells + numVertices; ++v) PetscCall(DMPlexSetSupportSize(dm, v, 2 * dim));
1555cfb853baSMatthew G. Knepley   /* TODO Loop over boundary and reset support sizes */
1556cfb853baSMatthew G. Knepley   PetscCall(DMSetUp(dm)); /* Allocate space for cones and supports */
1557cfb853baSMatthew G. Knepley   /* Build cell cones and vertex supports */
1558cfb853baSMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "celltype"));
1559cfb853baSMatthew G. Knepley   while (vert[dim - 1] < vertices[dim - 1]) {
1560cfb853baSMatthew G. Knepley     const PetscInt vertex = TupleToIndex_Private(dim, vertices, vert) + numCells;
1561cfb853baSMatthew G. Knepley     PetscInt       s      = 0;
1562cfb853baSMatthew G. Knepley 
15633ba16761SJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "Vertex %" PetscInt_FMT ":", vertex));
15643ba16761SJacob Faibussowitsch     for (d = 0; d < dim; ++d) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, vert[d]));
15653ba16761SJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
1566cfb853baSMatthew G. Knepley     PetscCall(DMPlexSetCellType(dm, vertex, DM_POLYTOPE_POINT));
1567cfb853baSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
1568cfb853baSMatthew G. Knepley       for (e = 0; e < dim; ++e) vtmp[e] = vert[e];
1569cfb853baSMatthew G. Knepley       vtmp[d] = (vert[d] + 1) % vertices[d];
1570cfb853baSMatthew G. Knepley       cone[0] = vertex;
1571cfb853baSMatthew G. Knepley       cone[1] = TupleToIndex_Private(dim, vertices, vtmp) + numCells;
15723ba16761SJacob Faibussowitsch       PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Vertex %" PetscInt_FMT ":", cone[1]));
15733ba16761SJacob Faibussowitsch       for (e = 0; e < dim; ++e) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, vtmp[e]));
15743ba16761SJacob Faibussowitsch       PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
1575cfb853baSMatthew G. Knepley       PetscCall(DMPlexSetCone(dm, cell, cone));
1576cfb853baSMatthew G. Knepley       PetscCall(DMPlexSetCellType(dm, cell, DM_POLYTOPE_SEGMENT));
15773ba16761SJacob Faibussowitsch       PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Edge %" PetscInt_FMT " (%" PetscInt_FMT " %" PetscInt_FMT ")\n", cell, cone[0], cone[1]));
1578cfb853baSMatthew G. Knepley       ++cell;
1579cfb853baSMatthew G. Knepley     }
1580cfb853baSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
1581cfb853baSMatthew G. Knepley       for (e = 0; e < dim; ++e) vtmp[e] = vert[e];
1582cfb853baSMatthew G. Knepley       vtmp[d]   = (vert[d] + vertices[d] - 1) % vertices[d];
1583cfb853baSMatthew G. Knepley       supp[s++] = TupleToIndex_Private(dim, vertices, vtmp) * dim + d;
1584cfb853baSMatthew G. Knepley       supp[s++] = (vertex - numCells) * dim + d;
1585cfb853baSMatthew G. Knepley       PetscCall(DMPlexSetSupport(dm, vertex, supp));
1586cfb853baSMatthew G. Knepley     }
1587cfb853baSMatthew G. Knepley     PetscCall(DMPlexTensorPointLexicographic_Private(dim, vertices, vert));
1588cfb853baSMatthew G. Knepley   }
1589cfb853baSMatthew G. Knepley   PetscCall(DMPlexStratify(dm));
1590cfb853baSMatthew G. Knepley   /* Build coordinates */
1591cfb853baSMatthew G. Knepley   PetscCall(DMGetCoordinateSection(dm, &coordSection));
1592cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetNumFields(coordSection, 1));
1593cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
1594cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices));
1595cfb853baSMatthew G. Knepley   for (v = numCells; v < numCells + numVertices; ++v) {
1596cfb853baSMatthew G. Knepley     PetscCall(PetscSectionSetDof(coordSection, v, dim));
1597cfb853baSMatthew G. Knepley     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
1598cfb853baSMatthew G. Knepley   }
1599cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetUp(coordSection));
1600cfb853baSMatthew G. Knepley   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
1601cfb853baSMatthew G. Knepley   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
1602cfb853baSMatthew G. Knepley   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
1603cfb853baSMatthew G. Knepley   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
1604cfb853baSMatthew G. Knepley   PetscCall(VecSetBlockSize(coordinates, dim));
1605cfb853baSMatthew G. Knepley   PetscCall(VecSetType(coordinates, VECSTANDARD));
1606cfb853baSMatthew G. Knepley   PetscCall(VecGetArray(coordinates, &coords));
1607cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) vert[d] = 0;
1608cfb853baSMatthew G. Knepley   while (vert[dim - 1] < vertices[dim - 1]) {
1609cfb853baSMatthew G. Knepley     const PetscInt vertex = TupleToIndex_Private(dim, vertices, vert);
1610cfb853baSMatthew G. Knepley 
1611cfb853baSMatthew G. Knepley     for (d = 0; d < dim; ++d) coords[vertex * dim + d] = lower[d] + ((upper[d] - lower[d]) / vertices[d]) * vert[d];
16123ba16761SJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "Vertex %" PetscInt_FMT ":", vertex));
16133ba16761SJacob Faibussowitsch     for (d = 0; d < dim; ++d) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, vert[d]));
16143ba16761SJacob Faibussowitsch     for (d = 0; d < dim; ++d) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %g", (double)PetscRealPart(coords[vertex * dim + d])));
16153ba16761SJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
1616cfb853baSMatthew G. Knepley     PetscCall(DMPlexTensorPointLexicographic_Private(dim, vertices, vert));
1617cfb853baSMatthew G. Knepley   }
1618cfb853baSMatthew G. Knepley   PetscCall(VecRestoreArray(coordinates, &coords));
1619cfb853baSMatthew G. Knepley   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
1620cfb853baSMatthew G. Knepley   PetscCall(VecDestroy(&coordinates));
1621cfb853baSMatthew G. Knepley   PetscCall(PetscFree4(vertices, vert, vtmp, supp));
1622cfb853baSMatthew G. Knepley   //PetscCall(DMSetPeriodicity(dm, NULL, lower, upper));
1623cfb853baSMatthew G. Knepley   // Attach the extent
1624cfb853baSMatthew G. Knepley   {
1625cfb853baSMatthew G. Knepley     PetscContainer c;
1626cfb853baSMatthew G. Knepley     PetscInt      *extent;
1627cfb853baSMatthew G. Knepley 
1628cfb853baSMatthew G. Knepley     PetscCall(PetscMalloc1(dim, &extent));
1629cfb853baSMatthew G. Knepley     for (PetscInt d = 0; d < dim; ++d) extent[d] = edges[d];
1630cfb853baSMatthew G. Knepley     PetscCall(PetscContainerCreate(PETSC_COMM_SELF, &c));
1631cfb853baSMatthew G. Knepley     PetscCall(PetscContainerSetUserDestroy(c, DestroyExtent_Private));
1632cfb853baSMatthew G. Knepley     PetscCall(PetscContainerSetPointer(c, extent));
1633cfb853baSMatthew G. Knepley     PetscCall(PetscObjectCompose((PetscObject)dm, "_extent", (PetscObject)c));
1634cfb853baSMatthew G. Knepley     PetscCall(PetscContainerDestroy(&c));
1635cfb853baSMatthew G. Knepley   }
16363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1637cfb853baSMatthew G. Knepley }
1638cfb853baSMatthew G. Knepley 
1639cfb853baSMatthew G. Knepley /*@C
1640aaa8cc7dSPierre Jolivet   DMPlexCreateHypercubicMesh - Creates a periodic mesh on the tensor product of unit intervals using only vertices and edges.
1641cfb853baSMatthew G. Knepley 
1642cfb853baSMatthew G. Knepley   Collective
1643cfb853baSMatthew G. Knepley 
1644cfb853baSMatthew G. Knepley   Input Parameters:
1645cfb853baSMatthew G. Knepley + comm  - The communicator for the DM object
1646cfb853baSMatthew G. Knepley . dim   - The spatial dimension
164720f4b53cSBarry Smith . edges - Number of edges per dimension, or `NULL` for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
164820f4b53cSBarry Smith . lower - The lower left corner, or `NULL` for (0, 0, 0)
164920f4b53cSBarry Smith - upper - The upper right corner, or `NULL` for (1, 1, 1)
1650cfb853baSMatthew G. Knepley 
1651cfb853baSMatthew G. Knepley   Output Parameter:
1652cfb853baSMatthew G. Knepley . dm - The DM object
1653cfb853baSMatthew G. Knepley 
165420f4b53cSBarry Smith   Level: beginner
165520f4b53cSBarry Smith 
165620f4b53cSBarry Smith   Note:
165720f4b53cSBarry Smith   If you want to customize this mesh using options, you just need to
165820f4b53cSBarry Smith .vb
165920f4b53cSBarry Smith   DMCreate(comm, &dm);
166020f4b53cSBarry Smith   DMSetType(dm, DMPLEX);
166120f4b53cSBarry Smith   DMSetFromOptions(dm);
166220f4b53cSBarry Smith .ve
166320f4b53cSBarry Smith   and use the options on the `DMSetFromOptions()` page.
1664cfb853baSMatthew G. Knepley 
1665cfb853baSMatthew G. Knepley   The vertices are numbered is lexicographic order, and the dim edges exiting a vertex in the positive orthant are number consecutively,
166620f4b53cSBarry Smith .vb
166720f4b53cSBarry Smith  18--0-19--2-20--4-18
166820f4b53cSBarry Smith   |     |     |     |
166920f4b53cSBarry Smith  13    15    17    13
167020f4b53cSBarry Smith   |     |     |     |
167120f4b53cSBarry Smith  24-12-25-14-26-16-24
167220f4b53cSBarry Smith   |     |     |     |
167320f4b53cSBarry Smith   7     9    11     7
167420f4b53cSBarry Smith   |     |     |     |
167520f4b53cSBarry Smith  21--6-22--8-23-10-21
167620f4b53cSBarry Smith   |     |     |     |
167720f4b53cSBarry Smith   1     3     5     1
167820f4b53cSBarry Smith   |     |     |     |
167920f4b53cSBarry Smith  18--0-19--2-20--4-18
168020f4b53cSBarry Smith .ve
1681cfb853baSMatthew G. Knepley 
168276fbde31SPierre Jolivet .seealso: `DMSetFromOptions()`, `DMPlexCreateFromFile()`, `DMPlexCreateHexCylinderMesh()`, `DMSetType()`, `DMCreate()`
1683cfb853baSMatthew G. Knepley @*/
1684cfb853baSMatthew G. Knepley PetscErrorCode DMPlexCreateHypercubicMesh(MPI_Comm comm, PetscInt dim, const PetscInt edges[], const PetscReal lower[], const PetscReal upper[], DM *dm)
1685cfb853baSMatthew G. Knepley {
1686cfb853baSMatthew G. Knepley   PetscInt       *edg;
1687cfb853baSMatthew G. Knepley   PetscReal      *low, *upp;
1688cfb853baSMatthew G. Knepley   DMBoundaryType *bdt;
1689cfb853baSMatthew G. Knepley   PetscInt        d;
1690cfb853baSMatthew G. Knepley 
1691cfb853baSMatthew G. Knepley   PetscFunctionBegin;
1692cfb853baSMatthew G. Knepley   PetscCall(DMCreate(comm, dm));
1693cfb853baSMatthew G. Knepley   PetscCall(DMSetType(*dm, DMPLEX));
1694cfb853baSMatthew G. Knepley   PetscCall(PetscMalloc4(dim, &edg, dim, &low, dim, &upp, dim, &bdt));
1695cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) {
1696cfb853baSMatthew G. Knepley     edg[d] = edges ? edges[d] : 1;
1697cfb853baSMatthew G. Knepley     low[d] = lower ? lower[d] : 0.;
1698cfb853baSMatthew G. Knepley     upp[d] = upper ? upper[d] : 1.;
1699cfb853baSMatthew G. Knepley     bdt[d] = DM_BOUNDARY_PERIODIC;
1700cfb853baSMatthew G. Knepley   }
1701cfb853baSMatthew G. Knepley   PetscCall(DMPlexCreateHypercubicMesh_Internal(*dm, dim, low, upp, edg, bdt));
1702cfb853baSMatthew G. Knepley   PetscCall(PetscFree4(edg, low, upp, bdt));
17033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1704cfb853baSMatthew G. Knepley }
1705cfb853baSMatthew G. Knepley 
1706a9074c1eSMatthew G. Knepley /*@C
1707a1cb98faSBarry Smith   DMPlexSetOptionsPrefix - Sets the prefix used for searching for all `DM` options in the database.
1708a9074c1eSMatthew G. Knepley 
170920f4b53cSBarry Smith   Logically Collective
1710a9074c1eSMatthew G. Knepley 
1711a9074c1eSMatthew G. Knepley   Input Parameters:
171220f4b53cSBarry Smith + dm     - the `DM` context
1713a9074c1eSMatthew G. Knepley - prefix - the prefix to prepend to all option names
1714a9074c1eSMatthew G. Knepley 
1715a1cb98faSBarry Smith   Level: advanced
1716a1cb98faSBarry Smith 
1717a1cb98faSBarry Smith   Note:
1718a9074c1eSMatthew G. Knepley   A hyphen (-) must NOT be given at the beginning of the prefix name.
1719a9074c1eSMatthew G. Knepley   The first character of all runtime options is AUTOMATICALLY the hyphen.
1720a9074c1eSMatthew G. Knepley 
17211cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `SNESSetFromOptions()`
1722a9074c1eSMatthew G. Knepley @*/
1723d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetOptionsPrefix(DM dm, const char prefix[])
1724d71ae5a4SJacob Faibussowitsch {
1725a9074c1eSMatthew G. Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
1726a9074c1eSMatthew G. Knepley 
1727a9074c1eSMatthew G. Knepley   PetscFunctionBegin;
1728a9074c1eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17299566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, prefix));
17309566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)mesh->partitioner, prefix));
17313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1732a9074c1eSMatthew G. Knepley }
1733a9074c1eSMatthew G. Knepley 
17349318fe57SMatthew G. Knepley /* Remap geometry to cylinder
173561a622f3SMatthew G. Knepley    TODO: This only works for a single refinement, then it is broken
173661a622f3SMatthew G. Knepley 
17379318fe57SMatthew G. Knepley      Interior square: Linear interpolation is correct
17389318fe57SMatthew G. Knepley      The other cells all have vertices on rays from the origin. We want to uniformly expand the spacing
17399318fe57SMatthew G. Knepley      such that the last vertex is on the unit circle. So the closest and farthest vertices are at distance
17400510c589SMatthew G. Knepley 
17419318fe57SMatthew G. Knepley        phi     = arctan(y/x)
17429318fe57SMatthew G. Knepley        d_close = sqrt(1/8 + 1/4 sin^2(phi))
17439318fe57SMatthew G. Knepley        d_far   = sqrt(1/2 + sin^2(phi))
17440510c589SMatthew G. Knepley 
17459318fe57SMatthew G. Knepley      so we remap them using
17460510c589SMatthew G. Knepley 
17479318fe57SMatthew G. Knepley        x_new = x_close + (x - x_close) (1 - d_close) / (d_far - d_close)
17489318fe57SMatthew G. Knepley        y_new = y_close + (y - y_close) (1 - d_close) / (d_far - d_close)
17490510c589SMatthew G. Knepley 
17509318fe57SMatthew G. Knepley      If pi/4 < phi < 3pi/4 or -3pi/4 < phi < -pi/4, then we switch x and y.
17519318fe57SMatthew G. Knepley */
1752d71ae5a4SJacob 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[])
1753d71ae5a4SJacob Faibussowitsch {
17549318fe57SMatthew G. Knepley   const PetscReal dis = 1.0 / PetscSqrtReal(2.0);
17559318fe57SMatthew G. Knepley   const PetscReal ds2 = 0.5 * dis;
175622cc497dSMatthew G. Knepley 
17579318fe57SMatthew G. Knepley   if ((PetscAbsScalar(u[0]) <= ds2) && (PetscAbsScalar(u[1]) <= ds2)) {
17589318fe57SMatthew G. Knepley     f0[0] = u[0];
17599318fe57SMatthew G. Knepley     f0[1] = u[1];
17609318fe57SMatthew G. Knepley   } else {
17619318fe57SMatthew G. Knepley     PetscReal phi, sinp, cosp, dc, df, x, y, xc, yc;
17620510c589SMatthew G. Knepley 
17639318fe57SMatthew G. Knepley     x    = PetscRealPart(u[0]);
17649318fe57SMatthew G. Knepley     y    = PetscRealPart(u[1]);
17659318fe57SMatthew G. Knepley     phi  = PetscAtan2Real(y, x);
17669318fe57SMatthew G. Knepley     sinp = PetscSinReal(phi);
17679318fe57SMatthew G. Knepley     cosp = PetscCosReal(phi);
17689318fe57SMatthew G. Knepley     if ((PetscAbsReal(phi) > PETSC_PI / 4.0) && (PetscAbsReal(phi) < 3.0 * PETSC_PI / 4.0)) {
17699318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2 / sinp);
17709318fe57SMatthew G. Knepley       df = PetscAbsReal(dis / sinp);
17719318fe57SMatthew G. Knepley       xc = ds2 * x / PetscAbsReal(y);
17729318fe57SMatthew G. Knepley       yc = ds2 * PetscSignReal(y);
17739318fe57SMatthew G. Knepley     } else {
17749318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2 / cosp);
17759318fe57SMatthew G. Knepley       df = PetscAbsReal(dis / cosp);
17769318fe57SMatthew G. Knepley       xc = ds2 * PetscSignReal(x);
17779318fe57SMatthew G. Knepley       yc = ds2 * y / PetscAbsReal(x);
17789318fe57SMatthew G. Knepley     }
17799318fe57SMatthew G. Knepley     f0[0] = xc + (u[0] - xc) * (1.0 - dc) / (df - dc);
17809318fe57SMatthew G. Knepley     f0[1] = yc + (u[1] - yc) * (1.0 - dc) / (df - dc);
17819318fe57SMatthew G. Knepley   }
17829318fe57SMatthew G. Knepley   f0[2] = u[2];
17839318fe57SMatthew G. Knepley }
17840510c589SMatthew G. Knepley 
1785d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateHexCylinderMesh_Internal(DM dm, DMBoundaryType periodicZ)
1786d71ae5a4SJacob Faibussowitsch {
17870510c589SMatthew G. Knepley   const PetscInt dim = 3;
17889318fe57SMatthew G. Knepley   PetscInt       numCells, numVertices;
1789d8c47e87SMatthew G. Knepley   PetscMPIInt    rank;
17900510c589SMatthew G. Knepley 
17910510c589SMatthew G. Knepley   PetscFunctionBegin;
179246139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
17939566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
17949566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
17950510c589SMatthew G. Knepley   /* Create topology */
17960510c589SMatthew G. Knepley   {
17970510c589SMatthew G. Knepley     PetscInt cone[8], c;
17980510c589SMatthew G. Knepley 
1799dd400576SPatrick Sanan     numCells    = rank == 0 ? 5 : 0;
1800dd400576SPatrick Sanan     numVertices = rank == 0 ? 16 : 0;
1801006a8963SMatthew G. Knepley     if (periodicZ == DM_BOUNDARY_PERIODIC) {
1802ae8bcbbbSMatthew G. Knepley       numCells *= 3;
1803dd400576SPatrick Sanan       numVertices = rank == 0 ? 24 : 0;
1804006a8963SMatthew G. Knepley     }
18059566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
18069566063dSJacob Faibussowitsch     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 8));
18079566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm));
1808dd400576SPatrick Sanan     if (rank == 0) {
1809006a8963SMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
18109371c9d4SSatish Balay         cone[0] = 15;
18119371c9d4SSatish Balay         cone[1] = 18;
18129371c9d4SSatish Balay         cone[2] = 17;
18139371c9d4SSatish Balay         cone[3] = 16;
18149371c9d4SSatish Balay         cone[4] = 31;
18159371c9d4SSatish Balay         cone[5] = 32;
18169371c9d4SSatish Balay         cone[6] = 33;
18179371c9d4SSatish Balay         cone[7] = 34;
18189566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
18199371c9d4SSatish Balay         cone[0] = 16;
18209371c9d4SSatish Balay         cone[1] = 17;
18219371c9d4SSatish Balay         cone[2] = 24;
18229371c9d4SSatish Balay         cone[3] = 23;
18239371c9d4SSatish Balay         cone[4] = 32;
18249371c9d4SSatish Balay         cone[5] = 36;
18259371c9d4SSatish Balay         cone[6] = 37;
18269371c9d4SSatish Balay         cone[7] = 33; /* 22 25 26 21 */
18279566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
18289371c9d4SSatish Balay         cone[0] = 18;
18299371c9d4SSatish Balay         cone[1] = 27;
18309371c9d4SSatish Balay         cone[2] = 24;
18319371c9d4SSatish Balay         cone[3] = 17;
18329371c9d4SSatish Balay         cone[4] = 34;
18339371c9d4SSatish Balay         cone[5] = 33;
18349371c9d4SSatish Balay         cone[6] = 37;
18359371c9d4SSatish Balay         cone[7] = 38;
18369566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
18379371c9d4SSatish Balay         cone[0] = 29;
18389371c9d4SSatish Balay         cone[1] = 27;
18399371c9d4SSatish Balay         cone[2] = 18;
18409371c9d4SSatish Balay         cone[3] = 15;
18419371c9d4SSatish Balay         cone[4] = 35;
18429371c9d4SSatish Balay         cone[5] = 31;
18439371c9d4SSatish Balay         cone[6] = 34;
18449371c9d4SSatish Balay         cone[7] = 38;
18459566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
18469371c9d4SSatish Balay         cone[0] = 29;
18479371c9d4SSatish Balay         cone[1] = 15;
18489371c9d4SSatish Balay         cone[2] = 16;
18499371c9d4SSatish Balay         cone[3] = 23;
18509371c9d4SSatish Balay         cone[4] = 35;
18519371c9d4SSatish Balay         cone[5] = 36;
18529371c9d4SSatish Balay         cone[6] = 32;
18539371c9d4SSatish Balay         cone[7] = 31;
18549566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
1855006a8963SMatthew G. Knepley 
18569371c9d4SSatish Balay         cone[0] = 31;
18579371c9d4SSatish Balay         cone[1] = 34;
18589371c9d4SSatish Balay         cone[2] = 33;
18599371c9d4SSatish Balay         cone[3] = 32;
18609371c9d4SSatish Balay         cone[4] = 19;
18619371c9d4SSatish Balay         cone[5] = 22;
18629371c9d4SSatish Balay         cone[6] = 21;
18639371c9d4SSatish Balay         cone[7] = 20;
18649566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 5, cone));
18659371c9d4SSatish Balay         cone[0] = 32;
18669371c9d4SSatish Balay         cone[1] = 33;
18679371c9d4SSatish Balay         cone[2] = 37;
18689371c9d4SSatish Balay         cone[3] = 36;
18699371c9d4SSatish Balay         cone[4] = 22;
18709371c9d4SSatish Balay         cone[5] = 25;
18719371c9d4SSatish Balay         cone[6] = 26;
18729371c9d4SSatish Balay         cone[7] = 21;
18739566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 6, cone));
18749371c9d4SSatish Balay         cone[0] = 34;
18759371c9d4SSatish Balay         cone[1] = 38;
18769371c9d4SSatish Balay         cone[2] = 37;
18779371c9d4SSatish Balay         cone[3] = 33;
18789371c9d4SSatish Balay         cone[4] = 20;
18799371c9d4SSatish Balay         cone[5] = 21;
18809371c9d4SSatish Balay         cone[6] = 26;
18819371c9d4SSatish Balay         cone[7] = 28;
18829566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 7, cone));
18839371c9d4SSatish Balay         cone[0] = 35;
18849371c9d4SSatish Balay         cone[1] = 38;
18859371c9d4SSatish Balay         cone[2] = 34;
18869371c9d4SSatish Balay         cone[3] = 31;
18879371c9d4SSatish Balay         cone[4] = 30;
18889371c9d4SSatish Balay         cone[5] = 19;
18899371c9d4SSatish Balay         cone[6] = 20;
18909371c9d4SSatish Balay         cone[7] = 28;
18919566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 8, cone));
18929371c9d4SSatish Balay         cone[0] = 35;
18939371c9d4SSatish Balay         cone[1] = 31;
18949371c9d4SSatish Balay         cone[2] = 32;
18959371c9d4SSatish Balay         cone[3] = 36;
18969371c9d4SSatish Balay         cone[4] = 30;
18979371c9d4SSatish Balay         cone[5] = 25;
18989371c9d4SSatish Balay         cone[6] = 22;
18999371c9d4SSatish Balay         cone[7] = 19;
19009566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 9, cone));
1901ae8bcbbbSMatthew G. Knepley 
19029371c9d4SSatish Balay         cone[0] = 19;
19039371c9d4SSatish Balay         cone[1] = 20;
19049371c9d4SSatish Balay         cone[2] = 21;
19059371c9d4SSatish Balay         cone[3] = 22;
19069371c9d4SSatish Balay         cone[4] = 15;
19079371c9d4SSatish Balay         cone[5] = 16;
19089371c9d4SSatish Balay         cone[6] = 17;
19099371c9d4SSatish Balay         cone[7] = 18;
19109566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 10, cone));
19119371c9d4SSatish Balay         cone[0] = 22;
19129371c9d4SSatish Balay         cone[1] = 21;
19139371c9d4SSatish Balay         cone[2] = 26;
19149371c9d4SSatish Balay         cone[3] = 25;
19159371c9d4SSatish Balay         cone[4] = 16;
19169371c9d4SSatish Balay         cone[5] = 23;
19179371c9d4SSatish Balay         cone[6] = 24;
19189371c9d4SSatish Balay         cone[7] = 17;
19199566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 11, cone));
19209371c9d4SSatish Balay         cone[0] = 20;
19219371c9d4SSatish Balay         cone[1] = 28;
19229371c9d4SSatish Balay         cone[2] = 26;
19239371c9d4SSatish Balay         cone[3] = 21;
19249371c9d4SSatish Balay         cone[4] = 18;
19259371c9d4SSatish Balay         cone[5] = 17;
19269371c9d4SSatish Balay         cone[6] = 24;
19279371c9d4SSatish Balay         cone[7] = 27;
19289566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 12, cone));
19299371c9d4SSatish Balay         cone[0] = 30;
19309371c9d4SSatish Balay         cone[1] = 28;
19319371c9d4SSatish Balay         cone[2] = 20;
19329371c9d4SSatish Balay         cone[3] = 19;
19339371c9d4SSatish Balay         cone[4] = 29;
19349371c9d4SSatish Balay         cone[5] = 15;
19359371c9d4SSatish Balay         cone[6] = 18;
19369371c9d4SSatish Balay         cone[7] = 27;
19379566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 13, cone));
19389371c9d4SSatish Balay         cone[0] = 30;
19399371c9d4SSatish Balay         cone[1] = 19;
19409371c9d4SSatish Balay         cone[2] = 22;
19419371c9d4SSatish Balay         cone[3] = 25;
19429371c9d4SSatish Balay         cone[4] = 29;
19439371c9d4SSatish Balay         cone[5] = 23;
19449371c9d4SSatish Balay         cone[6] = 16;
19459371c9d4SSatish Balay         cone[7] = 15;
19469566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 14, cone));
1947006a8963SMatthew G. Knepley       } else {
19489371c9d4SSatish Balay         cone[0] = 5;
19499371c9d4SSatish Balay         cone[1] = 8;
19509371c9d4SSatish Balay         cone[2] = 7;
19519371c9d4SSatish Balay         cone[3] = 6;
19529371c9d4SSatish Balay         cone[4] = 9;
19539371c9d4SSatish Balay         cone[5] = 12;
19549371c9d4SSatish Balay         cone[6] = 11;
19559371c9d4SSatish Balay         cone[7] = 10;
19569566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
19579371c9d4SSatish Balay         cone[0] = 6;
19589371c9d4SSatish Balay         cone[1] = 7;
19599371c9d4SSatish Balay         cone[2] = 14;
19609371c9d4SSatish Balay         cone[3] = 13;
19619371c9d4SSatish Balay         cone[4] = 12;
19629371c9d4SSatish Balay         cone[5] = 15;
19639371c9d4SSatish Balay         cone[6] = 16;
19649371c9d4SSatish Balay         cone[7] = 11;
19659566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
19669371c9d4SSatish Balay         cone[0] = 8;
19679371c9d4SSatish Balay         cone[1] = 17;
19689371c9d4SSatish Balay         cone[2] = 14;
19699371c9d4SSatish Balay         cone[3] = 7;
19709371c9d4SSatish Balay         cone[4] = 10;
19719371c9d4SSatish Balay         cone[5] = 11;
19729371c9d4SSatish Balay         cone[6] = 16;
19739371c9d4SSatish Balay         cone[7] = 18;
19749566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
19759371c9d4SSatish Balay         cone[0] = 19;
19769371c9d4SSatish Balay         cone[1] = 17;
19779371c9d4SSatish Balay         cone[2] = 8;
19789371c9d4SSatish Balay         cone[3] = 5;
19799371c9d4SSatish Balay         cone[4] = 20;
19809371c9d4SSatish Balay         cone[5] = 9;
19819371c9d4SSatish Balay         cone[6] = 10;
19829371c9d4SSatish Balay         cone[7] = 18;
19839566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
19849371c9d4SSatish Balay         cone[0] = 19;
19859371c9d4SSatish Balay         cone[1] = 5;
19869371c9d4SSatish Balay         cone[2] = 6;
19879371c9d4SSatish Balay         cone[3] = 13;
19889371c9d4SSatish Balay         cone[4] = 20;
19899371c9d4SSatish Balay         cone[5] = 15;
19909371c9d4SSatish Balay         cone[6] = 12;
19919371c9d4SSatish Balay         cone[7] = 9;
19929566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
1993006a8963SMatthew G. Knepley       }
1994d8c47e87SMatthew G. Knepley     }
19959566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
19969566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
19970510c589SMatthew G. Knepley   }
1998dbc1dc17SMatthew G. Knepley   /* Create cube geometry */
19990510c589SMatthew G. Knepley   {
20000510c589SMatthew G. Knepley     Vec             coordinates;
20010510c589SMatthew G. Knepley     PetscSection    coordSection;
20020510c589SMatthew G. Knepley     PetscScalar    *coords;
20030510c589SMatthew G. Knepley     PetscInt        coordSize, v;
20040510c589SMatthew G. Knepley     const PetscReal dis = 1.0 / PetscSqrtReal(2.0);
20050510c589SMatthew G. Knepley     const PetscReal ds2 = dis / 2.0;
20060510c589SMatthew G. Knepley 
20070510c589SMatthew G. Knepley     /* Build coordinates */
20089566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
20099566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
20109566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
20119566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices));
20120510c589SMatthew G. Knepley     for (v = numCells; v < numCells + numVertices; ++v) {
20139566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
20149566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
20150510c589SMatthew G. Knepley     }
20169566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
20179566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
20189566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
20199566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
20209566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
20219566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
20229566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates, VECSTANDARD));
20239566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
2024dd400576SPatrick Sanan     if (rank == 0) {
20259371c9d4SSatish Balay       coords[0 * dim + 0]  = -ds2;
20269371c9d4SSatish Balay       coords[0 * dim + 1]  = -ds2;
20279371c9d4SSatish Balay       coords[0 * dim + 2]  = 0.0;
20289371c9d4SSatish Balay       coords[1 * dim + 0]  = ds2;
20299371c9d4SSatish Balay       coords[1 * dim + 1]  = -ds2;
20309371c9d4SSatish Balay       coords[1 * dim + 2]  = 0.0;
20319371c9d4SSatish Balay       coords[2 * dim + 0]  = ds2;
20329371c9d4SSatish Balay       coords[2 * dim + 1]  = ds2;
20339371c9d4SSatish Balay       coords[2 * dim + 2]  = 0.0;
20349371c9d4SSatish Balay       coords[3 * dim + 0]  = -ds2;
20359371c9d4SSatish Balay       coords[3 * dim + 1]  = ds2;
20369371c9d4SSatish Balay       coords[3 * dim + 2]  = 0.0;
20379371c9d4SSatish Balay       coords[4 * dim + 0]  = -ds2;
20389371c9d4SSatish Balay       coords[4 * dim + 1]  = -ds2;
20399371c9d4SSatish Balay       coords[4 * dim + 2]  = 1.0;
20409371c9d4SSatish Balay       coords[5 * dim + 0]  = -ds2;
20419371c9d4SSatish Balay       coords[5 * dim + 1]  = ds2;
20429371c9d4SSatish Balay       coords[5 * dim + 2]  = 1.0;
20439371c9d4SSatish Balay       coords[6 * dim + 0]  = ds2;
20449371c9d4SSatish Balay       coords[6 * dim + 1]  = ds2;
20459371c9d4SSatish Balay       coords[6 * dim + 2]  = 1.0;
20469371c9d4SSatish Balay       coords[7 * dim + 0]  = ds2;
20479371c9d4SSatish Balay       coords[7 * dim + 1]  = -ds2;
20489371c9d4SSatish Balay       coords[7 * dim + 2]  = 1.0;
20499371c9d4SSatish Balay       coords[8 * dim + 0]  = dis;
20509371c9d4SSatish Balay       coords[8 * dim + 1]  = -dis;
20519371c9d4SSatish Balay       coords[8 * dim + 2]  = 0.0;
20529371c9d4SSatish Balay       coords[9 * dim + 0]  = dis;
20539371c9d4SSatish Balay       coords[9 * dim + 1]  = dis;
20549371c9d4SSatish Balay       coords[9 * dim + 2]  = 0.0;
20559371c9d4SSatish Balay       coords[10 * dim + 0] = dis;
20569371c9d4SSatish Balay       coords[10 * dim + 1] = -dis;
20579371c9d4SSatish Balay       coords[10 * dim + 2] = 1.0;
20589371c9d4SSatish Balay       coords[11 * dim + 0] = dis;
20599371c9d4SSatish Balay       coords[11 * dim + 1] = dis;
20609371c9d4SSatish Balay       coords[11 * dim + 2] = 1.0;
20619371c9d4SSatish Balay       coords[12 * dim + 0] = -dis;
20629371c9d4SSatish Balay       coords[12 * dim + 1] = dis;
20639371c9d4SSatish Balay       coords[12 * dim + 2] = 0.0;
20649371c9d4SSatish Balay       coords[13 * dim + 0] = -dis;
20659371c9d4SSatish Balay       coords[13 * dim + 1] = dis;
20669371c9d4SSatish Balay       coords[13 * dim + 2] = 1.0;
20679371c9d4SSatish Balay       coords[14 * dim + 0] = -dis;
20689371c9d4SSatish Balay       coords[14 * dim + 1] = -dis;
20699371c9d4SSatish Balay       coords[14 * dim + 2] = 0.0;
20709371c9d4SSatish Balay       coords[15 * dim + 0] = -dis;
20719371c9d4SSatish Balay       coords[15 * dim + 1] = -dis;
20729371c9d4SSatish Balay       coords[15 * dim + 2] = 1.0;
2073ae8bcbbbSMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
20749371c9d4SSatish Balay         /* 15 31 19 */ coords[16 * dim + 0] = -ds2;
20759371c9d4SSatish Balay         coords[16 * dim + 1]                = -ds2;
20769371c9d4SSatish Balay         coords[16 * dim + 2]                = 0.5;
20779371c9d4SSatish Balay         /* 16 32 22 */ coords[17 * dim + 0] = ds2;
20789371c9d4SSatish Balay         coords[17 * dim + 1]                = -ds2;
20799371c9d4SSatish Balay         coords[17 * dim + 2]                = 0.5;
20809371c9d4SSatish Balay         /* 17 33 21 */ coords[18 * dim + 0] = ds2;
20819371c9d4SSatish Balay         coords[18 * dim + 1]                = ds2;
20829371c9d4SSatish Balay         coords[18 * dim + 2]                = 0.5;
20839371c9d4SSatish Balay         /* 18 34 20 */ coords[19 * dim + 0] = -ds2;
20849371c9d4SSatish Balay         coords[19 * dim + 1]                = ds2;
20859371c9d4SSatish Balay         coords[19 * dim + 2]                = 0.5;
20869371c9d4SSatish Balay         /* 29 35 30 */ coords[20 * dim + 0] = -dis;
20879371c9d4SSatish Balay         coords[20 * dim + 1]                = -dis;
20889371c9d4SSatish Balay         coords[20 * dim + 2]                = 0.5;
20899371c9d4SSatish Balay         /* 23 36 25 */ coords[21 * dim + 0] = dis;
20909371c9d4SSatish Balay         coords[21 * dim + 1]                = -dis;
20919371c9d4SSatish Balay         coords[21 * dim + 2]                = 0.5;
20929371c9d4SSatish Balay         /* 24 37 26 */ coords[22 * dim + 0] = dis;
20939371c9d4SSatish Balay         coords[22 * dim + 1]                = dis;
20949371c9d4SSatish Balay         coords[22 * dim + 2]                = 0.5;
20959371c9d4SSatish Balay         /* 27 38 28 */ coords[23 * dim + 0] = -dis;
20969371c9d4SSatish Balay         coords[23 * dim + 1]                = dis;
20979371c9d4SSatish Balay         coords[23 * dim + 2]                = 0.5;
2098ae8bcbbbSMatthew G. Knepley       }
2099d8c47e87SMatthew G. Knepley     }
21009566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
21019566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
21029566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
21030510c589SMatthew G. Knepley   }
2104006a8963SMatthew G. Knepley   /* Create periodicity */
2105006a8963SMatthew G. Knepley   if (periodicZ == DM_BOUNDARY_PERIODIC || periodicZ == DM_BOUNDARY_TWIST) {
21066858538eSMatthew G. Knepley     PetscReal L[3]       = {-1., -1., 0.};
21076858538eSMatthew G. Knepley     PetscReal maxCell[3] = {-1., -1., 0.};
2108006a8963SMatthew G. Knepley     PetscReal lower[3]   = {0.0, 0.0, 0.0};
2109ae8bcbbbSMatthew G. Knepley     PetscReal upper[3]   = {1.0, 1.0, 1.5};
21106858538eSMatthew G. Knepley     PetscInt  numZCells  = 3;
2111006a8963SMatthew G. Knepley 
21126858538eSMatthew G. Knepley     L[2]       = upper[2] - lower[2];
21136858538eSMatthew G. Knepley     maxCell[2] = 1.1 * (L[2] / numZCells);
21144fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, maxCell, lower, L));
2115006a8963SMatthew G. Knepley   }
2116dbc1dc17SMatthew G. Knepley   {
21179318fe57SMatthew G. Knepley     DM          cdm;
21189318fe57SMatthew G. Knepley     PetscDS     cds;
21199318fe57SMatthew G. Knepley     PetscScalar c[2] = {1.0, 1.0};
2120dbc1dc17SMatthew G. Knepley 
2121e44f6aebSMatthew G. Knepley     PetscCall(DMPlexCreateCoordinateSpace(dm, 1, PETSC_TRUE, snapToCylinder));
21229566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
21239566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
21249566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, 2, c));
2125dbc1dc17SMatthew G. Knepley   }
212646139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
212746139095SJed Brown 
21289318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
21299566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolateInPlace_Internal(dm));
21303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21310510c589SMatthew G. Knepley }
21320510c589SMatthew G. Knepley 
213324119c2aSMatthew G. Knepley /*@
21349318fe57SMatthew G. Knepley   DMPlexCreateHexCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using hexahedra.
213524119c2aSMatthew G. Knepley 
2136d083f849SBarry Smith   Collective
213724119c2aSMatthew G. Knepley 
213824119c2aSMatthew G. Knepley   Input Parameters:
2139a1cb98faSBarry Smith + comm      - The communicator for the `DM` object
21409318fe57SMatthew G. Knepley - periodicZ - The boundary type for the Z direction
214124119c2aSMatthew G. Knepley 
214224119c2aSMatthew G. Knepley   Output Parameter:
214320f4b53cSBarry Smith . dm - The `DM` object
214424119c2aSMatthew G. Knepley 
214524119c2aSMatthew G. Knepley   Level: beginner
214624119c2aSMatthew G. Knepley 
2147a1cb98faSBarry Smith   Note:
2148a4e35b19SJacob Faibussowitsch   Here is the output numbering looking from the bottom of the cylinder\:
2149a1cb98faSBarry Smith .vb
2150a1cb98faSBarry Smith        17-----14
2151a1cb98faSBarry Smith         |     |
2152a1cb98faSBarry Smith         |  2  |
2153a1cb98faSBarry Smith         |     |
2154a1cb98faSBarry Smith  17-----8-----7-----14
2155a1cb98faSBarry Smith   |     |     |     |
2156a1cb98faSBarry Smith   |  3  |  0  |  1  |
2157a1cb98faSBarry Smith   |     |     |     |
2158a1cb98faSBarry Smith  19-----5-----6-----13
2159a1cb98faSBarry Smith         |     |
2160a1cb98faSBarry Smith         |  4  |
2161a1cb98faSBarry Smith         |     |
2162a1cb98faSBarry Smith        19-----13
2163a1cb98faSBarry Smith 
2164a1cb98faSBarry Smith  and up through the top
2165a1cb98faSBarry Smith 
2166a1cb98faSBarry Smith        18-----16
2167a1cb98faSBarry Smith         |     |
2168a1cb98faSBarry Smith         |  2  |
2169a1cb98faSBarry Smith         |     |
2170a1cb98faSBarry Smith  18----10----11-----16
2171a1cb98faSBarry Smith   |     |     |     |
2172a1cb98faSBarry Smith   |  3  |  0  |  1  |
2173a1cb98faSBarry Smith   |     |     |     |
2174a1cb98faSBarry Smith  20-----9----12-----15
2175a1cb98faSBarry Smith         |     |
2176a1cb98faSBarry Smith         |  4  |
2177a1cb98faSBarry Smith         |     |
2178a1cb98faSBarry Smith        20-----15
2179a1cb98faSBarry Smith .ve
2180a1cb98faSBarry Smith 
21811cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
218224119c2aSMatthew G. Knepley @*/
2183d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateHexCylinderMesh(MPI_Comm comm, DMBoundaryType periodicZ, DM *dm)
2184d71ae5a4SJacob Faibussowitsch {
21859318fe57SMatthew G. Knepley   PetscFunctionBegin;
21864f572ea9SToby Isaac   PetscAssertPointer(dm, 3);
21879566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
21889566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
21899566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateHexCylinderMesh_Internal(*dm, periodicZ));
21903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21919318fe57SMatthew G. Knepley }
21929318fe57SMatthew G. Knepley 
2193d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateWedgeCylinderMesh_Internal(DM dm, PetscInt n, PetscBool interpolate)
2194d71ae5a4SJacob Faibussowitsch {
219524119c2aSMatthew G. Knepley   const PetscInt dim = 3;
2196412e9a14SMatthew G. Knepley   PetscInt       numCells, numVertices, v;
21979fe9f049SMatthew G. Knepley   PetscMPIInt    rank;
219824119c2aSMatthew G. Knepley 
219924119c2aSMatthew G. Knepley   PetscFunctionBegin;
220063a3b9bcSJacob Faibussowitsch   PetscCheck(n >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of wedges %" PetscInt_FMT " cannot be negative", n);
220146139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
22029566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
22039566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
2204412e9a14SMatthew G. Knepley   /* Must create the celltype label here so that we do not automatically try to compute the types */
22059566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "celltype"));
220624119c2aSMatthew G. Knepley   /* Create topology */
220724119c2aSMatthew G. Knepley   {
220824119c2aSMatthew G. Knepley     PetscInt cone[6], c;
220924119c2aSMatthew G. Knepley 
2210dd400576SPatrick Sanan     numCells    = rank == 0 ? n : 0;
2211dd400576SPatrick Sanan     numVertices = rank == 0 ? 2 * (n + 1) : 0;
22129566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
22139566063dSJacob Faibussowitsch     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 6));
22149566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm));
221524119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
22169371c9d4SSatish Balay       cone[0] = c + n * 1;
22179371c9d4SSatish Balay       cone[1] = (c + 1) % n + n * 1;
22189371c9d4SSatish Balay       cone[2] = 0 + 3 * n;
22199371c9d4SSatish Balay       cone[3] = c + n * 2;
22209371c9d4SSatish Balay       cone[4] = (c + 1) % n + n * 2;
22219371c9d4SSatish Balay       cone[5] = 1 + 3 * n;
22229566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(dm, c, cone));
22239566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(dm, c, DM_POLYTOPE_TRI_PRISM_TENSOR));
222424119c2aSMatthew G. Knepley     }
22259566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
22269566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
222724119c2aSMatthew G. Knepley   }
222848a46eb9SPierre Jolivet   for (v = numCells; v < numCells + numVertices; ++v) PetscCall(DMPlexSetCellType(dm, v, DM_POLYTOPE_POINT));
222924119c2aSMatthew G. Knepley   /* Create cylinder geometry */
223024119c2aSMatthew G. Knepley   {
223124119c2aSMatthew G. Knepley     Vec          coordinates;
223224119c2aSMatthew G. Knepley     PetscSection coordSection;
223324119c2aSMatthew G. Knepley     PetscScalar *coords;
2234412e9a14SMatthew G. Knepley     PetscInt     coordSize, c;
223524119c2aSMatthew G. Knepley 
223624119c2aSMatthew G. Knepley     /* Build coordinates */
22379566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
22389566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
22399566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
22409566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices));
224124119c2aSMatthew G. Knepley     for (v = numCells; v < numCells + numVertices; ++v) {
22429566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
22439566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
224424119c2aSMatthew G. Knepley     }
22459566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
22469566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
22479566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
22489566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
22499566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
22509566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
22519566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates, VECSTANDARD));
22529566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
225324119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
22549371c9d4SSatish Balay       coords[(c + 0 * n) * dim + 0] = PetscCosReal(2.0 * c * PETSC_PI / n);
22559371c9d4SSatish Balay       coords[(c + 0 * n) * dim + 1] = PetscSinReal(2.0 * c * PETSC_PI / n);
22569371c9d4SSatish Balay       coords[(c + 0 * n) * dim + 2] = 1.0;
22579371c9d4SSatish Balay       coords[(c + 1 * n) * dim + 0] = PetscCosReal(2.0 * c * PETSC_PI / n);
22589371c9d4SSatish Balay       coords[(c + 1 * n) * dim + 1] = PetscSinReal(2.0 * c * PETSC_PI / n);
22599371c9d4SSatish Balay       coords[(c + 1 * n) * dim + 2] = 0.0;
226024119c2aSMatthew G. Knepley     }
2261dd400576SPatrick Sanan     if (rank == 0) {
22629371c9d4SSatish Balay       coords[(2 * n + 0) * dim + 0] = 0.0;
22639371c9d4SSatish Balay       coords[(2 * n + 0) * dim + 1] = 0.0;
22649371c9d4SSatish Balay       coords[(2 * n + 0) * dim + 2] = 1.0;
22659371c9d4SSatish Balay       coords[(2 * n + 1) * dim + 0] = 0.0;
22669371c9d4SSatish Balay       coords[(2 * n + 1) * dim + 1] = 0.0;
22679371c9d4SSatish Balay       coords[(2 * n + 1) * dim + 2] = 0.0;
22689fe9f049SMatthew G. Knepley     }
22699566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
22709566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
22719566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
227224119c2aSMatthew G. Knepley   }
227346139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
22749318fe57SMatthew G. Knepley   /* Interpolate */
22759566063dSJacob Faibussowitsch   if (interpolate) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
22763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
22779318fe57SMatthew G. Knepley }
22789318fe57SMatthew G. Knepley 
22799318fe57SMatthew G. Knepley /*@
22809318fe57SMatthew G. Knepley   DMPlexCreateWedgeCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using wedges.
22819318fe57SMatthew G. Knepley 
22829318fe57SMatthew G. Knepley   Collective
22839318fe57SMatthew G. Knepley 
22849318fe57SMatthew G. Knepley   Input Parameters:
2285a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
22869318fe57SMatthew G. Knepley . n           - The number of wedges around the origin
22879318fe57SMatthew G. Knepley - interpolate - Create edges and faces
22889318fe57SMatthew G. Knepley 
22899318fe57SMatthew G. Knepley   Output Parameter:
2290a1cb98faSBarry Smith . dm - The `DM` object
22919318fe57SMatthew G. Knepley 
22929318fe57SMatthew G. Knepley   Level: beginner
22939318fe57SMatthew G. Knepley 
22941cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateHexCylinderMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
22959318fe57SMatthew G. Knepley @*/
2296d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateWedgeCylinderMesh(MPI_Comm comm, PetscInt n, PetscBool interpolate, DM *dm)
2297d71ae5a4SJacob Faibussowitsch {
22989318fe57SMatthew G. Knepley   PetscFunctionBegin;
22994f572ea9SToby Isaac   PetscAssertPointer(dm, 4);
23009566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
23019566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
23029566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateWedgeCylinderMesh_Internal(*dm, n, interpolate));
23033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
230424119c2aSMatthew G. Knepley }
230524119c2aSMatthew G. Knepley 
2306d71ae5a4SJacob Faibussowitsch static inline PetscReal DiffNormReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
2307d71ae5a4SJacob Faibussowitsch {
230865a81367SMatthew G. Knepley   PetscReal prod = 0.0;
230965a81367SMatthew G. Knepley   PetscInt  i;
231065a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += PetscSqr(x[i] - y[i]);
231165a81367SMatthew G. Knepley   return PetscSqrtReal(prod);
231265a81367SMatthew G. Knepley }
2313d71ae5a4SJacob Faibussowitsch static inline PetscReal DotReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
2314d71ae5a4SJacob Faibussowitsch {
231565a81367SMatthew G. Knepley   PetscReal prod = 0.0;
231665a81367SMatthew G. Knepley   PetscInt  i;
231765a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += x[i] * y[i];
231865a81367SMatthew G. Knepley   return prod;
231965a81367SMatthew G. Knepley }
232065a81367SMatthew G. Knepley 
232151a74b61SMatthew G. Knepley /* The first constant is the sphere radius */
2322d71ae5a4SJacob 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[])
2323d71ae5a4SJacob Faibussowitsch {
232451a74b61SMatthew G. Knepley   PetscReal r     = PetscRealPart(constants[0]);
232551a74b61SMatthew G. Knepley   PetscReal norm2 = 0.0, fac;
232651a74b61SMatthew G. Knepley   PetscInt  n     = uOff[1] - uOff[0], d;
232751a74b61SMatthew G. Knepley 
232851a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) norm2 += PetscSqr(PetscRealPart(u[d]));
232951a74b61SMatthew G. Knepley   fac = r / PetscSqrtReal(norm2);
233051a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) f0[d] = u[d] * fac;
233151a74b61SMatthew G. Knepley }
233251a74b61SMatthew G. Knepley 
2333d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSphereMesh_Internal(DM dm, PetscInt dim, PetscBool simplex, PetscReal R)
2334d71ae5a4SJacob Faibussowitsch {
233565a81367SMatthew G. Knepley   const PetscInt embedDim = dim + 1;
233665a81367SMatthew G. Knepley   PetscSection   coordSection;
233765a81367SMatthew G. Knepley   Vec            coordinates;
233865a81367SMatthew G. Knepley   PetscScalar   *coords;
233965a81367SMatthew G. Knepley   PetscReal     *coordsIn;
2340*07c565c5SJose E. Roman   PetscInt       numCells, numEdges, numVerts = 0, firstVertex = 0, v, firstEdge, coordSize, d, e;
234165a81367SMatthew G. Knepley   PetscMPIInt    rank;
234265a81367SMatthew G. Knepley 
234365a81367SMatthew G. Knepley   PetscFunctionBegin;
23449318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveBool(dm, simplex, 3);
234546139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
23469566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
23479566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, dim + 1));
23489566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
234965a81367SMatthew G. Knepley   switch (dim) {
23505c344501SMatthew G. Knepley   case 1:
23515c344501SMatthew G. Knepley     numCells = 16;
23525c344501SMatthew G. Knepley     numVerts = numCells;
23535c344501SMatthew G. Knepley 
23545c344501SMatthew G. Knepley     // Build Topology
23555c344501SMatthew G. Knepley     PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts));
23565c344501SMatthew G. Knepley     for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim));
23575c344501SMatthew G. Knepley     PetscCall(DMSetUp(dm));
23585c344501SMatthew G. Knepley     for (PetscInt c = 0; c < numCells; ++c) {
23595c344501SMatthew G. Knepley       PetscInt cone[2];
23605c344501SMatthew G. Knepley 
23615c344501SMatthew G. Knepley       cone[0] = c + numCells;
23625c344501SMatthew G. Knepley       cone[1] = (c + 1) % numVerts + numCells;
23635c344501SMatthew G. Knepley       PetscCall(DMPlexSetCone(dm, c, cone));
23645c344501SMatthew G. Knepley     }
23655c344501SMatthew G. Knepley     PetscCall(DMPlexSymmetrize(dm));
23665c344501SMatthew G. Knepley     PetscCall(DMPlexStratify(dm));
23675c344501SMatthew G. Knepley     PetscCall(PetscMalloc1(numVerts * embedDim, &coordsIn));
23685c344501SMatthew G. Knepley     for (PetscInt v = 0; v < numVerts; ++v) {
23695c344501SMatthew G. Knepley       const PetscReal rad = 2. * PETSC_PI * v / numVerts;
23705c344501SMatthew G. Knepley 
23715c344501SMatthew G. Knepley       coordsIn[v * embedDim + 0] = PetscCosReal(rad);
23725c344501SMatthew G. Knepley       coordsIn[v * embedDim + 1] = PetscSinReal(rad);
23735c344501SMatthew G. Knepley     }
23745c344501SMatthew G. Knepley     break;
237565a81367SMatthew G. Knepley   case 2:
237665a81367SMatthew G. Knepley     if (simplex) {
237751a74b61SMatthew G. Knepley       const PetscReal radius    = PetscSqrtReal(1 + PETSC_PHI * PETSC_PHI) / (1.0 + PETSC_PHI);
237851a74b61SMatthew G. Knepley       const PetscReal edgeLen   = 2.0 / (1.0 + PETSC_PHI) * (R / radius);
237965a81367SMatthew G. Knepley       const PetscInt  degree    = 5;
238051a74b61SMatthew G. Knepley       PetscReal       vertex[3] = {0.0, 1.0 / (1.0 + PETSC_PHI), PETSC_PHI / (1.0 + PETSC_PHI)};
238165a81367SMatthew G. Knepley       PetscInt        s[3]      = {1, 1, 1};
238265a81367SMatthew G. Knepley       PetscInt        cone[3];
2383*07c565c5SJose E. Roman       PetscInt       *graph;
238465a81367SMatthew G. Knepley 
23859371c9d4SSatish Balay       vertex[0] *= R / radius;
23869371c9d4SSatish Balay       vertex[1] *= R / radius;
23879371c9d4SSatish Balay       vertex[2] *= R / radius;
2388dd400576SPatrick Sanan       numCells    = rank == 0 ? 20 : 0;
2389dd400576SPatrick Sanan       numVerts    = rank == 0 ? 12 : 0;
239065a81367SMatthew G. Knepley       firstVertex = numCells;
239151a74b61SMatthew G. Knepley       /* Use icosahedron, which for a R-sphere has coordinates which are all cyclic permutations of
239265a81367SMatthew G. Knepley 
239365a81367SMatthew G. Knepley            (0, \pm 1/\phi+1, \pm \phi/\phi+1)
239465a81367SMatthew G. Knepley 
239565a81367SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
239651a74b61SMatthew G. Knepley          length is then given by 2/(1+\phi) = 2 * 0.38197 = 0.76393.
239765a81367SMatthew G. Knepley       */
239865a81367SMatthew G. Knepley       /* Construct vertices */
23999566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
2400dd400576SPatrick Sanan       if (rank == 0) {
2401*07c565c5SJose E. Roman         for (PetscInt p = 0, i = 0; p < embedDim; ++p) {
240265a81367SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
240365a81367SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
240465a81367SMatthew G. Knepley               for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[(d + p) % embedDim] * vertex[(d + p) % embedDim];
240565a81367SMatthew G. Knepley               ++i;
240665a81367SMatthew G. Knepley             }
240765a81367SMatthew G. Knepley           }
240865a81367SMatthew G. Knepley         }
240945da822fSValeria Barra       }
241065a81367SMatthew G. Knepley       /* Construct graph */
24119566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * numVerts, &graph));
2412*07c565c5SJose E. Roman       for (PetscInt i = 0; i < numVerts; ++i) {
2413*07c565c5SJose E. Roman         PetscInt k = 0;
2414*07c565c5SJose E. Roman         for (PetscInt j = 0; j < numVerts; ++j) {
24159371c9d4SSatish Balay           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i * embedDim], &coordsIn[j * embedDim]) - edgeLen) < PETSC_SMALL) {
24169371c9d4SSatish Balay             graph[i * numVerts + j] = 1;
24179371c9d4SSatish Balay             ++k;
24189371c9d4SSatish Balay           }
241965a81367SMatthew G. Knepley         }
242063a3b9bcSJacob Faibussowitsch         PetscCheck(k == degree, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid icosahedron, vertex %" PetscInt_FMT " degree %" PetscInt_FMT " != %" PetscInt_FMT, i, k, degree);
242165a81367SMatthew G. Knepley       }
242265a81367SMatthew G. Knepley       /* Build Topology */
24239566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts));
2424*07c565c5SJose E. Roman       for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim));
24259566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
242665a81367SMatthew G. Knepley       /* Cells */
2427*07c565c5SJose E. Roman       for (PetscInt i = 0, c = 0; i < numVerts; ++i) {
2428*07c565c5SJose E. Roman         for (PetscInt j = 0; j < i; ++j) {
2429*07c565c5SJose E. Roman           for (PetscInt k = 0; k < j; ++k) {
243065a81367SMatthew G. Knepley             if (graph[i * numVerts + j] && graph[j * numVerts + k] && graph[k * numVerts + i]) {
24319371c9d4SSatish Balay               cone[0] = firstVertex + i;
24329371c9d4SSatish Balay               cone[1] = firstVertex + j;
24339371c9d4SSatish Balay               cone[2] = firstVertex + k;
243465a81367SMatthew G. Knepley               /* Check orientation */
243565a81367SMatthew G. Knepley               {
24369371c9d4SSatish Balay                 const PetscInt epsilon[3][3][3] = {
24379371c9d4SSatish Balay                   {{0, 0, 0},  {0, 0, 1},  {0, -1, 0}},
24389371c9d4SSatish Balay                   {{0, 0, -1}, {0, 0, 0},  {1, 0, 0} },
24399371c9d4SSatish Balay                   {{0, 1, 0},  {-1, 0, 0}, {0, 0, 0} }
24409371c9d4SSatish Balay                 };
244165a81367SMatthew G. Knepley                 PetscReal normal[3];
244265a81367SMatthew G. Knepley                 PetscInt  e, f;
244365a81367SMatthew G. Knepley 
244465a81367SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) {
244565a81367SMatthew G. Knepley                   normal[d] = 0.0;
244665a81367SMatthew G. Knepley                   for (e = 0; e < embedDim; ++e) {
2447ad540459SPierre 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]);
244865a81367SMatthew G. Knepley                   }
244965a81367SMatthew G. Knepley                 }
24509371c9d4SSatish Balay                 if (DotReal(embedDim, normal, &coordsIn[i * embedDim]) < 0) {
24519371c9d4SSatish Balay                   PetscInt tmp = cone[1];
24529371c9d4SSatish Balay                   cone[1]      = cone[2];
24539371c9d4SSatish Balay                   cone[2]      = tmp;
245465a81367SMatthew G. Knepley                 }
245565a81367SMatthew G. Knepley               }
24569566063dSJacob Faibussowitsch               PetscCall(DMPlexSetCone(dm, c++, cone));
245765a81367SMatthew G. Knepley             }
245865a81367SMatthew G. Knepley           }
245965a81367SMatthew G. Knepley         }
246065a81367SMatthew G. Knepley       }
24619566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
24629566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
24639566063dSJacob Faibussowitsch       PetscCall(PetscFree(graph));
246465a81367SMatthew G. Knepley     } else {
24652829fed8SMatthew G. Knepley       /*
24662829fed8SMatthew G. Knepley         12-21--13
24672829fed8SMatthew G. Knepley          |     |
24682829fed8SMatthew G. Knepley         25  4  24
24692829fed8SMatthew G. Knepley          |     |
24702829fed8SMatthew G. Knepley   12-25--9-16--8-24--13
24712829fed8SMatthew G. Knepley    |     |     |     |
24722829fed8SMatthew G. Knepley   23  5 17  0 15  3  22
24732829fed8SMatthew G. Knepley    |     |     |     |
24742829fed8SMatthew G. Knepley   10-20--6-14--7-19--11
24752829fed8SMatthew G. Knepley          |     |
24762829fed8SMatthew G. Knepley         20  1  19
24772829fed8SMatthew G. Knepley          |     |
24782829fed8SMatthew G. Knepley         10-18--11
24792829fed8SMatthew G. Knepley          |     |
24802829fed8SMatthew G. Knepley         23  2  22
24812829fed8SMatthew G. Knepley          |     |
24822829fed8SMatthew G. Knepley         12-21--13
24832829fed8SMatthew G. Knepley        */
24842829fed8SMatthew G. Knepley       PetscInt cone[4], ornt[4];
24852829fed8SMatthew G. Knepley 
2486dd400576SPatrick Sanan       numCells    = rank == 0 ? 6 : 0;
2487dd400576SPatrick Sanan       numEdges    = rank == 0 ? 12 : 0;
2488dd400576SPatrick Sanan       numVerts    = rank == 0 ? 8 : 0;
248965a81367SMatthew G. Knepley       firstVertex = numCells;
249065a81367SMatthew G. Knepley       firstEdge   = numCells + numVerts;
24912829fed8SMatthew G. Knepley       /* Build Topology */
24929566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells + numEdges + numVerts));
2493*07c565c5SJose E. Roman       for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 4));
249448a46eb9SPierre Jolivet       for (e = firstEdge; e < firstEdge + numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2));
24959566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
2496dd400576SPatrick Sanan       if (rank == 0) {
24972829fed8SMatthew G. Knepley         /* Cell 0 */
24989371c9d4SSatish Balay         cone[0] = 14;
24999371c9d4SSatish Balay         cone[1] = 15;
25009371c9d4SSatish Balay         cone[2] = 16;
25019371c9d4SSatish Balay         cone[3] = 17;
25029566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
25039371c9d4SSatish Balay         ornt[0] = 0;
25049371c9d4SSatish Balay         ornt[1] = 0;
25059371c9d4SSatish Balay         ornt[2] = 0;
25069371c9d4SSatish Balay         ornt[3] = 0;
25079566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 0, ornt));
25082829fed8SMatthew G. Knepley         /* Cell 1 */
25099371c9d4SSatish Balay         cone[0] = 18;
25109371c9d4SSatish Balay         cone[1] = 19;
25119371c9d4SSatish Balay         cone[2] = 14;
25129371c9d4SSatish Balay         cone[3] = 20;
25139566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
25149371c9d4SSatish Balay         ornt[0] = 0;
25159371c9d4SSatish Balay         ornt[1] = 0;
25169371c9d4SSatish Balay         ornt[2] = -1;
25179371c9d4SSatish Balay         ornt[3] = 0;
25189566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 1, ornt));
25192829fed8SMatthew G. Knepley         /* Cell 2 */
25209371c9d4SSatish Balay         cone[0] = 21;
25219371c9d4SSatish Balay         cone[1] = 22;
25229371c9d4SSatish Balay         cone[2] = 18;
25239371c9d4SSatish Balay         cone[3] = 23;
25249566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
25259371c9d4SSatish Balay         ornt[0] = 0;
25269371c9d4SSatish Balay         ornt[1] = 0;
25279371c9d4SSatish Balay         ornt[2] = -1;
25289371c9d4SSatish Balay         ornt[3] = 0;
25299566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 2, ornt));
25302829fed8SMatthew G. Knepley         /* Cell 3 */
25319371c9d4SSatish Balay         cone[0] = 19;
25329371c9d4SSatish Balay         cone[1] = 22;
25339371c9d4SSatish Balay         cone[2] = 24;
25349371c9d4SSatish Balay         cone[3] = 15;
25359566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
25369371c9d4SSatish Balay         ornt[0] = -1;
25379371c9d4SSatish Balay         ornt[1] = -1;
25389371c9d4SSatish Balay         ornt[2] = 0;
25399371c9d4SSatish Balay         ornt[3] = -1;
25409566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 3, ornt));
25412829fed8SMatthew G. Knepley         /* Cell 4 */
25429371c9d4SSatish Balay         cone[0] = 16;
25439371c9d4SSatish Balay         cone[1] = 24;
25449371c9d4SSatish Balay         cone[2] = 21;
25459371c9d4SSatish Balay         cone[3] = 25;
25469566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
25479371c9d4SSatish Balay         ornt[0] = -1;
25489371c9d4SSatish Balay         ornt[1] = -1;
25499371c9d4SSatish Balay         ornt[2] = -1;
25509371c9d4SSatish Balay         ornt[3] = 0;
25519566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 4, ornt));
25522829fed8SMatthew G. Knepley         /* Cell 5 */
25539371c9d4SSatish Balay         cone[0] = 20;
25549371c9d4SSatish Balay         cone[1] = 17;
25559371c9d4SSatish Balay         cone[2] = 25;
25569371c9d4SSatish Balay         cone[3] = 23;
25579566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 5, cone));
25589371c9d4SSatish Balay         ornt[0] = -1;
25599371c9d4SSatish Balay         ornt[1] = -1;
25609371c9d4SSatish Balay         ornt[2] = -1;
25619371c9d4SSatish Balay         ornt[3] = -1;
25629566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 5, ornt));
25632829fed8SMatthew G. Knepley         /* Edges */
25649371c9d4SSatish Balay         cone[0] = 6;
25659371c9d4SSatish Balay         cone[1] = 7;
25669566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 14, cone));
25679371c9d4SSatish Balay         cone[0] = 7;
25689371c9d4SSatish Balay         cone[1] = 8;
25699566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 15, cone));
25709371c9d4SSatish Balay         cone[0] = 8;
25719371c9d4SSatish Balay         cone[1] = 9;
25729566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 16, cone));
25739371c9d4SSatish Balay         cone[0] = 9;
25749371c9d4SSatish Balay         cone[1] = 6;
25759566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 17, cone));
25769371c9d4SSatish Balay         cone[0] = 10;
25779371c9d4SSatish Balay         cone[1] = 11;
25789566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 18, cone));
25799371c9d4SSatish Balay         cone[0] = 11;
25809371c9d4SSatish Balay         cone[1] = 7;
25819566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 19, cone));
25829371c9d4SSatish Balay         cone[0] = 6;
25839371c9d4SSatish Balay         cone[1] = 10;
25849566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 20, cone));
25859371c9d4SSatish Balay         cone[0] = 12;
25869371c9d4SSatish Balay         cone[1] = 13;
25879566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 21, cone));
25889371c9d4SSatish Balay         cone[0] = 13;
25899371c9d4SSatish Balay         cone[1] = 11;
25909566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 22, cone));
25919371c9d4SSatish Balay         cone[0] = 10;
25929371c9d4SSatish Balay         cone[1] = 12;
25939566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 23, cone));
25949371c9d4SSatish Balay         cone[0] = 13;
25959371c9d4SSatish Balay         cone[1] = 8;
25969566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 24, cone));
25979371c9d4SSatish Balay         cone[0] = 12;
25989371c9d4SSatish Balay         cone[1] = 9;
25999566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 25, cone));
260045da822fSValeria Barra       }
26019566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
26029566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
26032829fed8SMatthew G. Knepley       /* Build coordinates */
26049566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
2605dd400576SPatrick Sanan       if (rank == 0) {
26069371c9d4SSatish Balay         coordsIn[0 * embedDim + 0] = -R;
26079371c9d4SSatish Balay         coordsIn[0 * embedDim + 1] = R;
26089371c9d4SSatish Balay         coordsIn[0 * embedDim + 2] = -R;
26099371c9d4SSatish Balay         coordsIn[1 * embedDim + 0] = R;
26109371c9d4SSatish Balay         coordsIn[1 * embedDim + 1] = R;
26119371c9d4SSatish Balay         coordsIn[1 * embedDim + 2] = -R;
26129371c9d4SSatish Balay         coordsIn[2 * embedDim + 0] = R;
26139371c9d4SSatish Balay         coordsIn[2 * embedDim + 1] = -R;
26149371c9d4SSatish Balay         coordsIn[2 * embedDim + 2] = -R;
26159371c9d4SSatish Balay         coordsIn[3 * embedDim + 0] = -R;
26169371c9d4SSatish Balay         coordsIn[3 * embedDim + 1] = -R;
26179371c9d4SSatish Balay         coordsIn[3 * embedDim + 2] = -R;
26189371c9d4SSatish Balay         coordsIn[4 * embedDim + 0] = -R;
26199371c9d4SSatish Balay         coordsIn[4 * embedDim + 1] = R;
26209371c9d4SSatish Balay         coordsIn[4 * embedDim + 2] = R;
26219371c9d4SSatish Balay         coordsIn[5 * embedDim + 0] = R;
26229371c9d4SSatish Balay         coordsIn[5 * embedDim + 1] = R;
26239371c9d4SSatish Balay         coordsIn[5 * embedDim + 2] = R;
26249371c9d4SSatish Balay         coordsIn[6 * embedDim + 0] = -R;
26259371c9d4SSatish Balay         coordsIn[6 * embedDim + 1] = -R;
26269371c9d4SSatish Balay         coordsIn[6 * embedDim + 2] = R;
26279371c9d4SSatish Balay         coordsIn[7 * embedDim + 0] = R;
26289371c9d4SSatish Balay         coordsIn[7 * embedDim + 1] = -R;
26299371c9d4SSatish Balay         coordsIn[7 * embedDim + 2] = R;
263065a81367SMatthew G. Knepley       }
263145da822fSValeria Barra     }
263265a81367SMatthew G. Knepley     break;
263365a81367SMatthew G. Knepley   case 3:
2634116ded15SMatthew G. Knepley     if (simplex) {
2635116ded15SMatthew G. Knepley       const PetscReal edgeLen         = 1.0 / PETSC_PHI;
263651a74b61SMatthew G. Knepley       PetscReal       vertexA[4]      = {0.5, 0.5, 0.5, 0.5};
263751a74b61SMatthew G. Knepley       PetscReal       vertexB[4]      = {1.0, 0.0, 0.0, 0.0};
263851a74b61SMatthew G. Knepley       PetscReal       vertexC[4]      = {0.5, 0.5 * PETSC_PHI, 0.5 / PETSC_PHI, 0.0};
2639116ded15SMatthew G. Knepley       const PetscInt  degree          = 12;
2640116ded15SMatthew G. Knepley       PetscInt        s[4]            = {1, 1, 1};
26419371c9d4SSatish Balay       PetscInt        evenPerm[12][4] = {
26429371c9d4SSatish Balay         {0, 1, 2, 3},
26439371c9d4SSatish Balay         {0, 2, 3, 1},
26449371c9d4SSatish Balay         {0, 3, 1, 2},
26459371c9d4SSatish Balay         {1, 0, 3, 2},
26469371c9d4SSatish Balay         {1, 2, 0, 3},
26479371c9d4SSatish Balay         {1, 3, 2, 0},
26489371c9d4SSatish Balay         {2, 0, 1, 3},
26499371c9d4SSatish Balay         {2, 1, 3, 0},
26509371c9d4SSatish Balay         {2, 3, 0, 1},
26519371c9d4SSatish Balay         {3, 0, 2, 1},
26529371c9d4SSatish Balay         {3, 1, 0, 2},
26539371c9d4SSatish Balay         {3, 2, 1, 0}
26549371c9d4SSatish Balay       };
2655116ded15SMatthew G. Knepley       PetscInt  cone[4];
2656116ded15SMatthew G. Knepley       PetscInt *graph, p, i, j, k, l;
2657116ded15SMatthew G. Knepley 
26589371c9d4SSatish Balay       vertexA[0] *= R;
26599371c9d4SSatish Balay       vertexA[1] *= R;
26609371c9d4SSatish Balay       vertexA[2] *= R;
26619371c9d4SSatish Balay       vertexA[3] *= R;
26629371c9d4SSatish Balay       vertexB[0] *= R;
26639371c9d4SSatish Balay       vertexB[1] *= R;
26649371c9d4SSatish Balay       vertexB[2] *= R;
26659371c9d4SSatish Balay       vertexB[3] *= R;
26669371c9d4SSatish Balay       vertexC[0] *= R;
26679371c9d4SSatish Balay       vertexC[1] *= R;
26689371c9d4SSatish Balay       vertexC[2] *= R;
26699371c9d4SSatish Balay       vertexC[3] *= R;
2670dd400576SPatrick Sanan       numCells    = rank == 0 ? 600 : 0;
2671dd400576SPatrick Sanan       numVerts    = rank == 0 ? 120 : 0;
2672116ded15SMatthew G. Knepley       firstVertex = numCells;
2673116ded15SMatthew G. Knepley       /* Use the 600-cell, which for a unit sphere has coordinates which are
2674116ded15SMatthew G. Knepley 
2675116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm 1,    \pm 1, \pm 1)                          16
2676116ded15SMatthew G. Knepley                (\pm 1,    0,       0,      0)  all cyclic permutations   8
2677116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm phi, \pm 1/phi, 0)  all even permutations    96
2678116ded15SMatthew G. Knepley 
2679116ded15SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
26806333ae4fSvaleriabarra          length is then given by 1/\phi = 0.61803.
2681116ded15SMatthew G. Knepley 
2682116ded15SMatthew G. Knepley          http://buzzard.pugetsound.edu/sage-practice/ch03s03.html
2683116ded15SMatthew G. Knepley          http://mathworld.wolfram.com/600-Cell.html
2684116ded15SMatthew G. Knepley       */
2685116ded15SMatthew G. Knepley       /* Construct vertices */
26869566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
2687116ded15SMatthew G. Knepley       i = 0;
2688dd400576SPatrick Sanan       if (rank == 0) {
2689116ded15SMatthew G. Knepley         for (s[0] = -1; s[0] < 2; s[0] += 2) {
2690116ded15SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
2691116ded15SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
2692116ded15SMatthew G. Knepley               for (s[3] = -1; s[3] < 2; s[3] += 2) {
2693116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[d] * vertexA[d];
2694116ded15SMatthew G. Knepley                 ++i;
2695116ded15SMatthew G. Knepley               }
2696116ded15SMatthew G. Knepley             }
2697116ded15SMatthew G. Knepley           }
2698116ded15SMatthew G. Knepley         }
2699116ded15SMatthew G. Knepley         for (p = 0; p < embedDim; ++p) {
2700116ded15SMatthew G. Knepley           s[1] = s[2] = s[3] = 1;
2701116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
2702116ded15SMatthew G. Knepley             for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[(d + p) % embedDim] * vertexB[(d + p) % embedDim];
2703116ded15SMatthew G. Knepley             ++i;
2704116ded15SMatthew G. Knepley           }
2705116ded15SMatthew G. Knepley         }
2706116ded15SMatthew G. Knepley         for (p = 0; p < 12; ++p) {
2707116ded15SMatthew G. Knepley           s[3] = 1;
2708116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
2709116ded15SMatthew G. Knepley             for (s[1] = -1; s[1] < 2; s[1] += 2) {
2710116ded15SMatthew G. Knepley               for (s[2] = -1; s[2] < 2; s[2] += 2) {
2711116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[evenPerm[p][d]] * vertexC[evenPerm[p][d]];
2712116ded15SMatthew G. Knepley                 ++i;
2713116ded15SMatthew G. Knepley               }
2714116ded15SMatthew G. Knepley             }
2715116ded15SMatthew G. Knepley           }
2716116ded15SMatthew G. Knepley         }
271745da822fSValeria Barra       }
271863a3b9bcSJacob Faibussowitsch       PetscCheck(i == numVerts, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertices %" PetscInt_FMT " != %" PetscInt_FMT, i, numVerts);
2719116ded15SMatthew G. Knepley       /* Construct graph */
27209566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * numVerts, &graph));
2721116ded15SMatthew G. Knepley       for (i = 0; i < numVerts; ++i) {
2722116ded15SMatthew G. Knepley         for (j = 0, k = 0; j < numVerts; ++j) {
27239371c9d4SSatish Balay           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i * embedDim], &coordsIn[j * embedDim]) - edgeLen) < PETSC_SMALL) {
27249371c9d4SSatish Balay             graph[i * numVerts + j] = 1;
27259371c9d4SSatish Balay             ++k;
27269371c9d4SSatish Balay           }
2727116ded15SMatthew G. Knepley         }
272863a3b9bcSJacob Faibussowitsch         PetscCheck(k == degree, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertex %" PetscInt_FMT " degree %" PetscInt_FMT " != %" PetscInt_FMT, i, k, degree);
2729116ded15SMatthew G. Knepley       }
2730116ded15SMatthew G. Knepley       /* Build Topology */
27319566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts));
2732*07c565c5SJose E. Roman       for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim));
27339566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
2734116ded15SMatthew G. Knepley       /* Cells */
2735dd400576SPatrick Sanan       if (rank == 0) {
2736*07c565c5SJose E. Roman         for (PetscInt i = 0, c = 0; i < numVerts; ++i) {
2737116ded15SMatthew G. Knepley           for (j = 0; j < i; ++j) {
2738116ded15SMatthew G. Knepley             for (k = 0; k < j; ++k) {
2739116ded15SMatthew G. Knepley               for (l = 0; l < k; ++l) {
27409371c9d4SSatish 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]) {
27419371c9d4SSatish Balay                   cone[0] = firstVertex + i;
27429371c9d4SSatish Balay                   cone[1] = firstVertex + j;
27439371c9d4SSatish Balay                   cone[2] = firstVertex + k;
27449371c9d4SSatish Balay                   cone[3] = firstVertex + l;
2745116ded15SMatthew G. Knepley                   /* Check orientation: https://ef.gy/linear-algebra:normal-vectors-in-higher-dimensional-spaces */
2746116ded15SMatthew G. Knepley                   {
27479371c9d4SSatish Balay                     const PetscInt epsilon[4][4][4][4] = {
27489371c9d4SSatish 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}}},
2749116ded15SMatthew G. Knepley 
27509371c9d4SSatish 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}}},
2751116ded15SMatthew G. Knepley 
27529371c9d4SSatish 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}}},
2753116ded15SMatthew G. Knepley 
27549371c9d4SSatish 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}} }
27559371c9d4SSatish Balay                     };
2756116ded15SMatthew G. Knepley                     PetscReal normal[4];
2757116ded15SMatthew G. Knepley                     PetscInt  e, f, g;
2758116ded15SMatthew G. Knepley 
2759116ded15SMatthew G. Knepley                     for (d = 0; d < embedDim; ++d) {
2760116ded15SMatthew G. Knepley                       normal[d] = 0.0;
2761116ded15SMatthew G. Knepley                       for (e = 0; e < embedDim; ++e) {
2762116ded15SMatthew G. Knepley                         for (f = 0; f < embedDim; ++f) {
2763116ded15SMatthew G. Knepley                           for (g = 0; g < embedDim; ++g) {
2764116ded15SMatthew 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]);
2765116ded15SMatthew G. Knepley                           }
2766116ded15SMatthew G. Knepley                         }
2767116ded15SMatthew G. Knepley                       }
2768116ded15SMatthew G. Knepley                     }
27699371c9d4SSatish Balay                     if (DotReal(embedDim, normal, &coordsIn[i * embedDim]) < 0) {
27709371c9d4SSatish Balay                       PetscInt tmp = cone[1];
27719371c9d4SSatish Balay                       cone[1]      = cone[2];
27729371c9d4SSatish Balay                       cone[2]      = tmp;
27739371c9d4SSatish Balay                     }
2774116ded15SMatthew G. Knepley                   }
27759566063dSJacob Faibussowitsch                   PetscCall(DMPlexSetCone(dm, c++, cone));
2776116ded15SMatthew G. Knepley                 }
2777116ded15SMatthew G. Knepley               }
2778116ded15SMatthew G. Knepley             }
2779116ded15SMatthew G. Knepley           }
2780116ded15SMatthew G. Knepley         }
278145da822fSValeria Barra       }
27829566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
27839566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
27849566063dSJacob Faibussowitsch       PetscCall(PetscFree(graph));
2785116ded15SMatthew G. Knepley     }
2786f4d061e9SPierre Jolivet     break;
2787d71ae5a4SJacob Faibussowitsch   default:
2788d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension for sphere: %" PetscInt_FMT, dim);
278965a81367SMatthew G. Knepley   }
279065a81367SMatthew G. Knepley   /* Create coordinates */
27919566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
27929566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
27939566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, embedDim));
27949566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numVerts));
27952829fed8SMatthew G. Knepley   for (v = firstVertex; v < firstVertex + numVerts; ++v) {
27969566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, embedDim));
27979566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, embedDim));
27982829fed8SMatthew G. Knepley   }
27999566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
28009566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
28019566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
28029566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, embedDim));
28039566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
28049566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
28059566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
28069566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
28079371c9d4SSatish Balay   for (v = 0; v < numVerts; ++v)
2808ad540459SPierre Jolivet     for (d = 0; d < embedDim; ++d) coords[v * embedDim + d] = coordsIn[v * embedDim + d];
28099566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
28109566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
28119566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
28129566063dSJacob Faibussowitsch   PetscCall(PetscFree(coordsIn));
281351a74b61SMatthew G. Knepley   {
281451a74b61SMatthew G. Knepley     DM          cdm;
281551a74b61SMatthew G. Knepley     PetscDS     cds;
28169318fe57SMatthew G. Knepley     PetscScalar c = R;
281751a74b61SMatthew G. Knepley 
2818e44f6aebSMatthew G. Knepley     PetscCall(DMPlexCreateCoordinateSpace(dm, 1, PETSC_TRUE, snapToSphere));
28199566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
28209566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
28219566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, 1, &c));
282251a74b61SMatthew G. Knepley   }
282346139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
28249318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
28259566063dSJacob Faibussowitsch   if (simplex) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
28263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
28279318fe57SMatthew G. Knepley }
28289318fe57SMatthew G. Knepley 
2829b7f5c055SJed Brown typedef void (*TPSEvaluateFunc)(const PetscReal[], PetscReal *, PetscReal[], PetscReal (*)[3]);
2830b7f5c055SJed Brown 
2831b7f5c055SJed Brown /*
2832b7f5c055SJed Brown  The Schwarz P implicit surface is
2833b7f5c055SJed Brown 
2834b7f5c055SJed Brown      f(x) = cos(x0) + cos(x1) + cos(x2) = 0
2835b7f5c055SJed Brown */
2836d71ae5a4SJacob Faibussowitsch static void TPSEvaluate_SchwarzP(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3])
2837d71ae5a4SJacob Faibussowitsch {
2838b7f5c055SJed Brown   PetscReal c[3] = {PetscCosReal(y[0] * PETSC_PI), PetscCosReal(y[1] * PETSC_PI), PetscCosReal(y[2] * PETSC_PI)};
2839b7f5c055SJed Brown   PetscReal g[3] = {-PetscSinReal(y[0] * PETSC_PI), -PetscSinReal(y[1] * PETSC_PI), -PetscSinReal(y[2] * PETSC_PI)};
2840b7f5c055SJed Brown   f[0]           = c[0] + c[1] + c[2];
2841b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) {
2842b7f5c055SJed Brown     grad[i] = PETSC_PI * g[i];
2843ad540459SPierre Jolivet     for (PetscInt j = 0; j < 3; j++) hess[i][j] = (i == j) ? -PetscSqr(PETSC_PI) * c[i] : 0.;
2844b7f5c055SJed Brown   }
2845b7f5c055SJed Brown }
2846b7f5c055SJed Brown 
28474663dae6SJed Brown // u[] is a tentative normal on input. Replace with the implicit function gradient in the same direction
2848d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSExtrudeNormalFunc_SchwarzP(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt r, PetscScalar u[], void *ctx)
2849d71ae5a4SJacob Faibussowitsch {
2850ad540459SPierre Jolivet   for (PetscInt i = 0; i < 3; i++) u[i] = -PETSC_PI * PetscSinReal(x[i] * PETSC_PI);
28513ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
28524663dae6SJed Brown }
28534663dae6SJed Brown 
2854b7f5c055SJed Brown /*
2855b7f5c055SJed Brown  The Gyroid implicit surface is
2856b7f5c055SJed Brown 
2857b7f5c055SJed 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)
2858b7f5c055SJed Brown 
2859b7f5c055SJed Brown */
2860d71ae5a4SJacob Faibussowitsch static void TPSEvaluate_Gyroid(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3])
2861d71ae5a4SJacob Faibussowitsch {
2862b7f5c055SJed Brown   PetscReal s[3] = {PetscSinReal(PETSC_PI * y[0]), PetscSinReal(PETSC_PI * (y[1] + .5)), PetscSinReal(PETSC_PI * (y[2] + .25))};
2863b7f5c055SJed Brown   PetscReal c[3] = {PetscCosReal(PETSC_PI * y[0]), PetscCosReal(PETSC_PI * (y[1] + .5)), PetscCosReal(PETSC_PI * (y[2] + .25))};
2864b7f5c055SJed Brown   f[0]           = s[0] * c[1] + s[1] * c[2] + s[2] * c[0];
2865b7f5c055SJed Brown   grad[0]        = PETSC_PI * (c[0] * c[1] - s[2] * s[0]);
2866b7f5c055SJed Brown   grad[1]        = PETSC_PI * (c[1] * c[2] - s[0] * s[1]);
2867b7f5c055SJed Brown   grad[2]        = PETSC_PI * (c[2] * c[0] - s[1] * s[2]);
2868b7f5c055SJed Brown   hess[0][0]     = -PetscSqr(PETSC_PI) * (s[0] * c[1] + s[2] * c[0]);
2869b7f5c055SJed Brown   hess[0][1]     = -PetscSqr(PETSC_PI) * (c[0] * s[1]);
2870b7f5c055SJed Brown   hess[0][2]     = -PetscSqr(PETSC_PI) * (c[2] * s[0]);
2871b7f5c055SJed Brown   hess[1][0]     = -PetscSqr(PETSC_PI) * (s[1] * c[2] + s[0] * c[1]);
2872b7f5c055SJed Brown   hess[1][1]     = -PetscSqr(PETSC_PI) * (c[1] * s[2]);
2873b7f5c055SJed Brown   hess[2][2]     = -PetscSqr(PETSC_PI) * (c[0] * s[1]);
2874b7f5c055SJed Brown   hess[2][0]     = -PetscSqr(PETSC_PI) * (s[2] * c[0] + s[1] * c[2]);
2875b7f5c055SJed Brown   hess[2][1]     = -PetscSqr(PETSC_PI) * (c[2] * s[0]);
2876b7f5c055SJed Brown   hess[2][2]     = -PetscSqr(PETSC_PI) * (c[1] * s[2]);
2877b7f5c055SJed Brown }
2878b7f5c055SJed Brown 
28794663dae6SJed Brown // u[] is a tentative normal on input. Replace with the implicit function gradient in the same direction
2880d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSExtrudeNormalFunc_Gyroid(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt r, PetscScalar u[], void *ctx)
2881d71ae5a4SJacob Faibussowitsch {
28824663dae6SJed Brown   PetscReal s[3] = {PetscSinReal(PETSC_PI * x[0]), PetscSinReal(PETSC_PI * (x[1] + .5)), PetscSinReal(PETSC_PI * (x[2] + .25))};
28834663dae6SJed Brown   PetscReal c[3] = {PetscCosReal(PETSC_PI * x[0]), PetscCosReal(PETSC_PI * (x[1] + .5)), PetscCosReal(PETSC_PI * (x[2] + .25))};
28844663dae6SJed Brown   u[0]           = PETSC_PI * (c[0] * c[1] - s[2] * s[0]);
28854663dae6SJed Brown   u[1]           = PETSC_PI * (c[1] * c[2] - s[0] * s[1]);
28864663dae6SJed Brown   u[2]           = PETSC_PI * (c[2] * c[0] - s[1] * s[2]);
28873ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
28884663dae6SJed Brown }
28894663dae6SJed Brown 
2890b7f5c055SJed Brown /*
2891b7f5c055SJed Brown    We wish to solve
2892b7f5c055SJed Brown 
2893b7f5c055SJed Brown          min_y || y - x ||^2  subject to f(y) = 0
2894b7f5c055SJed Brown 
2895b7f5c055SJed Brown    Let g(y) = grad(f).  The minimization problem is equivalent to asking to satisfy
2896b7f5c055SJed 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
2897b7f5c055SJed Brown    tangent space and ask for both components in the tangent space to be zero.
2898b7f5c055SJed Brown 
2899b7f5c055SJed Brown    Take g to be a column vector and compute the "full QR" factorization Q R = g,
2900b7f5c055SJed Brown    where Q = I - 2 n n^T is a symmetric orthogonal matrix.
2901b7f5c055SJed Brown    The first column of Q is parallel to g so the remaining two columns span the null space.
2902b7f5c055SJed Brown    Let Qn = Q[:,1:] be those remaining columns.  Then Qn Qn^T is an orthogonal projector into the tangent space.
2903da81f932SPierre Jolivet    Since Q is symmetric, this is equivalent to multiplying by Q and taking the last two entries.
2904b7f5c055SJed Brown    In total, we have a system of 3 equations in 3 unknowns:
2905b7f5c055SJed Brown 
2906b7f5c055SJed Brown      f(y) = 0                       1 equation
2907b7f5c055SJed Brown      Qn^T (y - x) = 0               2 equations
2908b7f5c055SJed Brown 
2909b7f5c055SJed Brown    Here, we compute the residual and Jacobian of this system.
2910b7f5c055SJed Brown */
2911d71ae5a4SJacob Faibussowitsch static void TPSNearestPointResJac(TPSEvaluateFunc feval, const PetscScalar x[], const PetscScalar y[], PetscScalar res[], PetscScalar J[])
2912d71ae5a4SJacob Faibussowitsch {
2913b7f5c055SJed Brown   PetscReal yreal[3] = {PetscRealPart(y[0]), PetscRealPart(y[1]), PetscRealPart(y[2])};
2914b7f5c055SJed Brown   PetscReal d[3]     = {PetscRealPart(y[0] - x[0]), PetscRealPart(y[1] - x[1]), PetscRealPart(y[2] - x[2])};
29152f0490c0SSatish Balay   PetscReal f, grad[3], n[3], norm, norm_y[3], nd, nd_y[3], sign;
29169371c9d4SSatish Balay   PetscReal n_y[3][3] = {
29179371c9d4SSatish Balay     {0, 0, 0},
29189371c9d4SSatish Balay     {0, 0, 0},
29199371c9d4SSatish Balay     {0, 0, 0}
29209371c9d4SSatish Balay   };
2921b7f5c055SJed Brown 
2922b7f5c055SJed Brown   feval(yreal, &f, grad, n_y);
2923b7f5c055SJed Brown 
2924b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) n[i] = grad[i];
2925b7f5c055SJed Brown   norm = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2]));
2926ad540459SPierre Jolivet   for (PetscInt i = 0; i < 3; i++) norm_y[i] = 1. / norm * n[i] * n_y[i][i];
2927b7f5c055SJed Brown 
2928b7f5c055SJed Brown   // Define the Householder reflector
2929b7f5c055SJed Brown   sign = n[0] >= 0 ? 1. : -1.;
2930b7f5c055SJed Brown   n[0] += norm * sign;
2931b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) n_y[0][i] += norm_y[i] * sign;
2932b7f5c055SJed Brown 
2933b7f5c055SJed Brown   norm      = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2]));
2934b7f5c055SJed Brown   norm_y[0] = 1. / norm * (n[0] * n_y[0][0]);
2935b7f5c055SJed Brown   norm_y[1] = 1. / norm * (n[0] * n_y[0][1] + n[1] * n_y[1][1]);
2936b7f5c055SJed Brown   norm_y[2] = 1. / norm * (n[0] * n_y[0][2] + n[2] * n_y[2][2]);
2937b7f5c055SJed Brown 
2938b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) {
2939b7f5c055SJed Brown     n[i] /= norm;
2940b7f5c055SJed Brown     for (PetscInt j = 0; j < 3; j++) {
2941b7f5c055SJed Brown       // note that n[i] is n_old[i]/norm when executing the code below
2942b7f5c055SJed Brown       n_y[i][j] = n_y[i][j] / norm - n[i] / norm * norm_y[j];
2943b7f5c055SJed Brown     }
2944b7f5c055SJed Brown   }
2945b7f5c055SJed Brown 
2946b7f5c055SJed Brown   nd = n[0] * d[0] + n[1] * d[1] + n[2] * d[2];
2947b7f5c055SJed 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];
2948b7f5c055SJed Brown 
2949b7f5c055SJed Brown   res[0] = f;
2950b7f5c055SJed Brown   res[1] = d[1] - 2 * n[1] * nd;
2951b7f5c055SJed Brown   res[2] = d[2] - 2 * n[2] * nd;
2952b7f5c055SJed Brown   // J[j][i] is J_{ij} (column major)
2953b7f5c055SJed Brown   for (PetscInt j = 0; j < 3; j++) {
2954b7f5c055SJed Brown     J[0 + j * 3] = grad[j];
2955b7f5c055SJed Brown     J[1 + j * 3] = (j == 1) * 1. - 2 * (n_y[1][j] * nd + n[1] * nd_y[j]);
2956b7f5c055SJed Brown     J[2 + j * 3] = (j == 2) * 1. - 2 * (n_y[2][j] * nd + n[2] * nd_y[j]);
2957b7f5c055SJed Brown   }
2958b7f5c055SJed Brown }
2959b7f5c055SJed Brown 
2960b7f5c055SJed Brown /*
2961b7f5c055SJed Brown    Project x to the nearest point on the implicit surface using Newton's method.
2962b7f5c055SJed Brown */
2963d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSNearestPoint(TPSEvaluateFunc feval, PetscScalar x[])
2964d71ae5a4SJacob Faibussowitsch {
2965b7f5c055SJed Brown   PetscScalar y[3] = {x[0], x[1], x[2]}; // Initial guess
2966b7f5c055SJed Brown 
2967b7f5c055SJed Brown   PetscFunctionBegin;
2968b7f5c055SJed Brown   for (PetscInt iter = 0; iter < 10; iter++) {
2969b7f5c055SJed Brown     PetscScalar res[3], J[9];
2970b7f5c055SJed Brown     PetscReal   resnorm;
2971b7f5c055SJed Brown     TPSNearestPointResJac(feval, x, y, res, J);
2972b7f5c055SJed Brown     resnorm = PetscSqrtReal(PetscSqr(PetscRealPart(res[0])) + PetscSqr(PetscRealPart(res[1])) + PetscSqr(PetscRealPart(res[2])));
2973b7f5c055SJed Brown     if (0) { // Turn on this monitor if you need to confirm quadratic convergence
297463a3b9bcSJacob 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])));
2975b7f5c055SJed Brown     }
2976b7f5c055SJed Brown     if (resnorm < PETSC_SMALL) break;
2977b7f5c055SJed Brown 
2978b7f5c055SJed Brown     // Take the Newton step
29799566063dSJacob Faibussowitsch     PetscCall(PetscKernel_A_gets_inverse_A_3(J, 0., PETSC_FALSE, NULL));
2980b7f5c055SJed Brown     PetscKernel_v_gets_v_minus_A_times_w_3(y, J, res);
2981b7f5c055SJed Brown   }
2982b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) x[i] = y[i];
29833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2984b7f5c055SJed Brown }
2985b7f5c055SJed Brown 
2986b7f5c055SJed Brown const char *const DMPlexTPSTypes[] = {"SCHWARZ_P", "GYROID", "DMPlexTPSType", "DMPLEX_TPS_", NULL};
2987b7f5c055SJed Brown 
2988d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateTPSMesh_Internal(DM dm, DMPlexTPSType tpstype, const PetscInt extent[], const DMBoundaryType periodic[], PetscBool tps_distribute, PetscInt refinements, PetscInt layers, PetscReal thickness)
2989d71ae5a4SJacob Faibussowitsch {
2990b7f5c055SJed Brown   PetscMPIInt rank;
2991b7f5c055SJed Brown   PetscInt    topoDim = 2, spaceDim = 3, numFaces = 0, numVertices = 0, numEdges = 0;
2992b7f5c055SJed Brown   PetscInt(*edges)[2] = NULL, *edgeSets = NULL;
2993b7f5c055SJed Brown   PetscInt            *cells_flat = NULL;
2994b7f5c055SJed Brown   PetscReal           *vtxCoords  = NULL;
2995b7f5c055SJed Brown   TPSEvaluateFunc      evalFunc   = NULL;
29964663dae6SJed Brown   PetscSimplePointFunc normalFunc = NULL;
2997b7f5c055SJed Brown   DMLabel              label;
2998b7f5c055SJed Brown 
2999b7f5c055SJed Brown   PetscFunctionBegin;
300046139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
30019566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
300263a3b9bcSJacob 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);
3003b7f5c055SJed Brown   switch (tpstype) {
3004b7f5c055SJed Brown   case DMPLEX_TPS_SCHWARZ_P:
3005b7f5c055SJed 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");
3006c5853193SPierre Jolivet     if (rank == 0) {
3007b7f5c055SJed Brown       PetscInt(*cells)[6][4][4] = NULL; // [junction, junction-face, cell, conn]
3008b7f5c055SJed Brown       PetscInt  Njunctions = 0, Ncuts = 0, Npipes[3], vcount;
3009b7f5c055SJed Brown       PetscReal L = 1;
3010b7f5c055SJed Brown 
3011b7f5c055SJed Brown       Npipes[0]   = (extent[0] + 1) * extent[1] * extent[2];
3012b7f5c055SJed Brown       Npipes[1]   = extent[0] * (extent[1] + 1) * extent[2];
3013b7f5c055SJed Brown       Npipes[2]   = extent[0] * extent[1] * (extent[2] + 1);
3014b7f5c055SJed Brown       Njunctions  = extent[0] * extent[1] * extent[2];
3015b7f5c055SJed Brown       Ncuts       = 2 * (extent[0] * extent[1] + extent[1] * extent[2] + extent[2] * extent[0]);
3016b7f5c055SJed Brown       numVertices = 4 * (Npipes[0] + Npipes[1] + Npipes[2]) + 8 * Njunctions;
30179566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(3 * numVertices, &vtxCoords));
30189566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Njunctions, &cells));
30199566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Ncuts * 4, &edges));
30209566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Ncuts * 4, &edgeSets));
3021b7f5c055SJed Brown       // x-normal pipes
3022b7f5c055SJed Brown       vcount = 0;
3023b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0] + 1; i++) {
3024b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3025b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2]; k++) {
3026b7f5c055SJed Brown             for (PetscInt l = 0; l < 4; l++) {
3027b7f5c055SJed Brown               vtxCoords[vcount++] = (2 * i - 1) * L;
3028b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * j * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3029b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * k * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3030b7f5c055SJed Brown             }
3031b7f5c055SJed Brown           }
3032b7f5c055SJed Brown         }
3033b7f5c055SJed Brown       }
3034b7f5c055SJed Brown       // y-normal pipes
3035b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0]; i++) {
3036b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1] + 1; j++) {
3037b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2]; k++) {
3038b7f5c055SJed Brown             for (PetscInt l = 0; l < 4; l++) {
3039b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * i * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3040b7f5c055SJed Brown               vtxCoords[vcount++] = (2 * j - 1) * L;
3041b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * k * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3042b7f5c055SJed Brown             }
3043b7f5c055SJed Brown           }
3044b7f5c055SJed Brown         }
3045b7f5c055SJed Brown       }
3046b7f5c055SJed Brown       // z-normal pipes
3047b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0]; i++) {
3048b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3049b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2] + 1; k++) {
3050b7f5c055SJed Brown             for (PetscInt l = 0; l < 4; l++) {
3051b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * i * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3052b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * j * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3053b7f5c055SJed Brown               vtxCoords[vcount++] = (2 * k - 1) * L;
3054b7f5c055SJed Brown             }
3055b7f5c055SJed Brown           }
3056b7f5c055SJed Brown         }
3057b7f5c055SJed Brown       }
3058b7f5c055SJed Brown       // junctions
3059b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0]; i++) {
3060b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3061b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2]; k++) {
3062b7f5c055SJed Brown             const PetscInt J = (i * extent[1] + j) * extent[2] + k, Jvoff = (Npipes[0] + Npipes[1] + Npipes[2]) * 4 + J * 8;
3063b7f5c055SJed Brown             PetscCheck(vcount / 3 == Jvoff, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unexpected vertex count");
3064b7f5c055SJed Brown             for (PetscInt ii = 0; ii < 2; ii++) {
3065b7f5c055SJed Brown               for (PetscInt jj = 0; jj < 2; jj++) {
3066b7f5c055SJed Brown                 for (PetscInt kk = 0; kk < 2; kk++) {
3067b7f5c055SJed Brown                   double Ls           = (1 - sqrt(2) / 4) * L;
3068b7f5c055SJed Brown                   vtxCoords[vcount++] = 2 * i * L + (2 * ii - 1) * Ls;
3069b7f5c055SJed Brown                   vtxCoords[vcount++] = 2 * j * L + (2 * jj - 1) * Ls;
3070b7f5c055SJed Brown                   vtxCoords[vcount++] = 2 * k * L + (2 * kk - 1) * Ls;
3071b7f5c055SJed Brown                 }
3072b7f5c055SJed Brown               }
3073b7f5c055SJed Brown             }
3074b7f5c055SJed Brown             const PetscInt jfaces[3][2][4] = {
3075b7f5c055SJed Brown               {{3, 1, 0, 2}, {7, 5, 4, 6}}, // x-aligned
3076b7f5c055SJed Brown               {{5, 4, 0, 1}, {7, 6, 2, 3}}, // y-aligned
3077b7f5c055SJed Brown               {{6, 2, 0, 4}, {7, 3, 1, 5}}  // z-aligned
3078b7f5c055SJed Brown             };
3079b7f5c055SJed Brown             const PetscInt pipe_lo[3] = {// vertex numbers of pipes
30809371c9d4SSatish 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};
3081b7f5c055SJed Brown             const PetscInt pipe_hi[3] = {// vertex numbers of pipes
30829371c9d4SSatish 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};
3083b7f5c055SJed Brown             for (PetscInt dir = 0; dir < 3; dir++) { // x,y,z
3084b7f5c055SJed Brown               const PetscInt ijk[3] = {i, j, k};
3085b7f5c055SJed Brown               for (PetscInt l = 0; l < 4; l++) { // rotations
3086b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][0] = pipe_lo[dir] + l;
3087b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][1] = Jvoff + jfaces[dir][0][l];
3088b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][2] = Jvoff + jfaces[dir][0][(l - 1 + 4) % 4];
3089b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][3] = pipe_lo[dir] + (l - 1 + 4) % 4;
3090b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][0] = Jvoff + jfaces[dir][1][l];
3091b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][1] = pipe_hi[dir] + l;
3092b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][2] = pipe_hi[dir] + (l - 1 + 4) % 4;
3093b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][3] = Jvoff + jfaces[dir][1][(l - 1 + 4) % 4];
3094b7f5c055SJed Brown                 if (ijk[dir] == 0) {
3095b7f5c055SJed Brown                   edges[numEdges][0] = pipe_lo[dir] + l;
3096b7f5c055SJed Brown                   edges[numEdges][1] = pipe_lo[dir] + (l + 1) % 4;
3097b7f5c055SJed Brown                   edgeSets[numEdges] = dir * 2 + 1;
3098b7f5c055SJed Brown                   numEdges++;
3099b7f5c055SJed Brown                 }
3100b7f5c055SJed Brown                 if (ijk[dir] + 1 == extent[dir]) {
3101b7f5c055SJed Brown                   edges[numEdges][0] = pipe_hi[dir] + l;
3102b7f5c055SJed Brown                   edges[numEdges][1] = pipe_hi[dir] + (l + 1) % 4;
3103b7f5c055SJed Brown                   edgeSets[numEdges] = dir * 2 + 2;
3104b7f5c055SJed Brown                   numEdges++;
3105b7f5c055SJed Brown                 }
3106b7f5c055SJed Brown               }
3107b7f5c055SJed Brown             }
3108b7f5c055SJed Brown           }
3109b7f5c055SJed Brown         }
3110b7f5c055SJed Brown       }
311163a3b9bcSJacob Faibussowitsch       PetscCheck(numEdges == Ncuts * 4, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Edge count %" PetscInt_FMT " incompatible with number of cuts %" PetscInt_FMT, numEdges, Ncuts);
3112b7f5c055SJed Brown       numFaces   = 24 * Njunctions;
3113b7f5c055SJed Brown       cells_flat = cells[0][0][0];
3114b7f5c055SJed Brown     }
3115b7f5c055SJed Brown     evalFunc   = TPSEvaluate_SchwarzP;
31164663dae6SJed Brown     normalFunc = TPSExtrudeNormalFunc_SchwarzP;
3117b7f5c055SJed Brown     break;
3118b7f5c055SJed Brown   case DMPLEX_TPS_GYROID:
3119c5853193SPierre Jolivet     if (rank == 0) {
3120b7f5c055SJed Brown       // This is a coarse mesh approximation of the gyroid shifted to being the zero of the level set
3121b7f5c055SJed Brown       //
3122b7f5c055SJed 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)
3123b7f5c055SJed Brown       //
3124b7f5c055SJed Brown       // on the cell [0,2]^3.
3125b7f5c055SJed Brown       //
3126b7f5c055SJed Brown       // Think about dividing that cell into four columns, and focus on the column [0,1]x[0,1]x[0,2].
3127b7f5c055SJed Brown       // If you looked at the gyroid in that column at different slices of z you would see that it kind of spins
3128b7f5c055SJed Brown       // like a boomerang:
3129b7f5c055SJed Brown       //
3130b7f5c055SJed Brown       //     z = 0          z = 1/4        z = 1/2        z = 3/4     //
3131b7f5c055SJed Brown       //     -----          -------        -------        -------     //
3132b7f5c055SJed Brown       //                                                              //
3133b7f5c055SJed Brown       //     +       +      +       +      +       +      +   \   +   //
3134b7f5c055SJed Brown       //      \                                   /            \      //
3135b7f5c055SJed Brown       //       \            `-_   _-'            /              }     //
3136b7f5c055SJed Brown       //        *-_            `-'            _-'              /      //
3137b7f5c055SJed Brown       //     +     `-+      +       +      +-'     +      +   /   +   //
3138b7f5c055SJed Brown       //                                                              //
3139b7f5c055SJed Brown       //                                                              //
3140b7f5c055SJed Brown       //     z = 1          z = 5/4        z = 3/2        z = 7/4     //
3141b7f5c055SJed Brown       //     -----          -------        -------        -------     //
3142b7f5c055SJed Brown       //                                                              //
3143b7f5c055SJed Brown       //     +-_     +      +       +      +     _-+      +   /   +   //
3144b7f5c055SJed Brown       //        `-_            _-_            _-`            /        //
3145b7f5c055SJed Brown       //           \        _-'   `-_        /              {         //
3146b7f5c055SJed Brown       //            \                       /                \        //
3147b7f5c055SJed Brown       //     +       +      +       +      +       +      +   \   +   //
3148b7f5c055SJed Brown       //
3149b7f5c055SJed Brown       //
3150b7f5c055SJed Brown       // This course mesh approximates each of these slices by two line segments,
3151b7f5c055SJed Brown       // and then connects the segments in consecutive layers with quadrilateral faces.
3152b7f5c055SJed Brown       // All of the end points of the segments are multiples of 1/4 except for the
3153b7f5c055SJed Brown       // point * in the picture for z = 0 above and the similar points in other layers.
3154b7f5c055SJed Brown       // That point is at (gamma, gamma, 0), where gamma is calculated below.
3155b7f5c055SJed Brown       //
3156b7f5c055SJed Brown       // The column  [1,2]x[1,2]x[0,2] looks the same as this column;
3157b7f5c055SJed Brown       // The columns [1,2]x[0,1]x[0,2] and [0,1]x[1,2]x[0,2] are mirror images.
3158b7f5c055SJed Brown       //
3159b7f5c055SJed Brown       // As for how this method turned into the names given to the vertices:
3160b7f5c055SJed Brown       // that was not systematic, it was just the way it worked out in my handwritten notes.
3161b7f5c055SJed Brown 
3162b7f5c055SJed Brown       PetscInt facesPerBlock = 64;
3163b7f5c055SJed Brown       PetscInt vertsPerBlock = 56;
3164b7f5c055SJed Brown       PetscInt extentPlus[3];
3165b7f5c055SJed Brown       PetscInt numBlocks, numBlocksPlus;
31669371c9d4SSatish 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;
31679371c9d4SSatish Balay       const PetscInt pattern[64][4] = {
31689371c9d4SSatish Balay   /* face to vertex within the coarse discretization of a single gyroid block */
3169b7f5c055SJed Brown   /* layer 0 */
31709371c9d4SSatish Balay         {A,           C,           K,           G          },
31719371c9d4SSatish Balay         {C,           B,           II,          K          },
31729371c9d4SSatish Balay         {D,           A,           H,           L          },
31739371c9d4SSatish Balay         {B + 56 * 1,  D,           L,           J          },
31749371c9d4SSatish Balay         {E,           B + 56 * 1,  J,           N          },
31759371c9d4SSatish Balay         {A + 56 * 2,  E,           N,           H + 56 * 2 },
31769371c9d4SSatish Balay         {F,           A + 56 * 2,  G + 56 * 2,  M          },
31779371c9d4SSatish Balay         {B,           F,           M,           II         },
3178b7f5c055SJed Brown  /* layer 1 */
31799371c9d4SSatish Balay         {G,           K,           Q,           O          },
31809371c9d4SSatish Balay         {K,           II,          P,           Q          },
31819371c9d4SSatish Balay         {L,           H,           O + 56 * 1,  R          },
31829371c9d4SSatish Balay         {J,           L,           R,           P          },
31839371c9d4SSatish Balay         {N,           J,           P,           S          },
31849371c9d4SSatish Balay         {H + 56 * 2,  N,           S,           O + 56 * 3 },
31859371c9d4SSatish Balay         {M,           G + 56 * 2,  O + 56 * 2,  T          },
31869371c9d4SSatish Balay         {II,          M,           T,           P          },
3187b7f5c055SJed Brown  /* layer 2 */
31889371c9d4SSatish Balay         {O,           Q,           Y,           U          },
31899371c9d4SSatish Balay         {Q,           P,           W,           Y          },
31909371c9d4SSatish Balay         {R,           O + 56 * 1,  U + 56 * 1,  Ap         },
31919371c9d4SSatish Balay         {P,           R,           Ap,          W          },
31929371c9d4SSatish Balay         {S,           P,           X,           Bp         },
31939371c9d4SSatish Balay         {O + 56 * 3,  S,           Bp,          V + 56 * 1 },
31949371c9d4SSatish Balay         {T,           O + 56 * 2,  V,           Z          },
31959371c9d4SSatish Balay         {P,           T,           Z,           X          },
3196b7f5c055SJed Brown  /* layer 3 */
31979371c9d4SSatish Balay         {U,           Y,           Ep,          Dp         },
31989371c9d4SSatish Balay         {Y,           W,           Cp,          Ep         },
31999371c9d4SSatish Balay         {Ap,          U + 56 * 1,  Dp + 56 * 1, Gp         },
32009371c9d4SSatish Balay         {W,           Ap,          Gp,          Cp         },
32019371c9d4SSatish Balay         {Bp,          X,           Cp + 56 * 2, Fp         },
32029371c9d4SSatish Balay         {V + 56 * 1,  Bp,          Fp,          Dp + 56 * 1},
32039371c9d4SSatish Balay         {Z,           V,           Dp,          Hp         },
32049371c9d4SSatish Balay         {X,           Z,           Hp,          Cp + 56 * 2},
3205b7f5c055SJed Brown  /* layer 4 */
32069371c9d4SSatish Balay         {Dp,          Ep,          Mp,          Kp         },
32079371c9d4SSatish Balay         {Ep,          Cp,          Ip,          Mp         },
32089371c9d4SSatish Balay         {Gp,          Dp + 56 * 1, Lp,          Np         },
32099371c9d4SSatish Balay         {Cp,          Gp,          Np,          Jp         },
32109371c9d4SSatish Balay         {Fp,          Cp + 56 * 2, Jp + 56 * 2, Pp         },
32119371c9d4SSatish Balay         {Dp + 56 * 1, Fp,          Pp,          Lp         },
32129371c9d4SSatish Balay         {Hp,          Dp,          Kp,          Op         },
32139371c9d4SSatish Balay         {Cp + 56 * 2, Hp,          Op,          Ip + 56 * 2},
3214b7f5c055SJed Brown  /* layer 5 */
32159371c9d4SSatish Balay         {Kp,          Mp,          Sp,          Rp         },
32169371c9d4SSatish Balay         {Mp,          Ip,          Qp,          Sp         },
32179371c9d4SSatish Balay         {Np,          Lp,          Rp,          Tp         },
32189371c9d4SSatish Balay         {Jp,          Np,          Tp,          Qp + 56 * 1},
32199371c9d4SSatish Balay         {Pp,          Jp + 56 * 2, Qp + 56 * 3, Up         },
32209371c9d4SSatish Balay         {Lp,          Pp,          Up,          Rp         },
32219371c9d4SSatish Balay         {Op,          Kp,          Rp,          Vp         },
32229371c9d4SSatish Balay         {Ip + 56 * 2, Op,          Vp,          Qp + 56 * 2},
3223b7f5c055SJed Brown  /* layer 6 */
32249371c9d4SSatish Balay         {Rp,          Sp,          Aq,          Yp         },
32259371c9d4SSatish Balay         {Sp,          Qp,          Wp,          Aq         },
32269371c9d4SSatish Balay         {Tp,          Rp,          Yp,          Cq         },
32279371c9d4SSatish Balay         {Qp + 56 * 1, Tp,          Cq,          Wp + 56 * 1},
32289371c9d4SSatish Balay         {Up,          Qp + 56 * 3, Xp + 56 * 1, Dq         },
32299371c9d4SSatish Balay         {Rp,          Up,          Dq,          Zp         },
32309371c9d4SSatish Balay         {Vp,          Rp,          Zp,          Bq         },
32319371c9d4SSatish Balay         {Qp + 56 * 2, Vp,          Bq,          Xp         },
3232b7f5c055SJed Brown  /* layer 7 (the top is the periodic image of the bottom of layer 0) */
32339371c9d4SSatish Balay         {Yp,          Aq,          C + 56 * 4,  A + 56 * 4 },
32349371c9d4SSatish Balay         {Aq,          Wp,          B + 56 * 4,  C + 56 * 4 },
32359371c9d4SSatish Balay         {Cq,          Yp,          A + 56 * 4,  D + 56 * 4 },
32369371c9d4SSatish Balay         {Wp + 56 * 1, Cq,          D + 56 * 4,  B + 56 * 5 },
32379371c9d4SSatish Balay         {Dq,          Xp + 56 * 1, B + 56 * 5,  E + 56 * 4 },
32389371c9d4SSatish Balay         {Zp,          Dq,          E + 56 * 4,  A + 56 * 6 },
32399371c9d4SSatish Balay         {Bq,          Zp,          A + 56 * 6,  F + 56 * 4 },
32409371c9d4SSatish Balay         {Xp,          Bq,          F + 56 * 4,  B + 56 * 4 }
3241b7f5c055SJed Brown       };
3242b7f5c055SJed Brown       const PetscReal gamma                = PetscAcosReal((PetscSqrtReal(3.) - 1.) / PetscSqrtReal(2.)) / PETSC_PI;
32439371c9d4SSatish Balay       const PetscReal patternCoords[56][3] = {
3244bee3fc89SBarry Smith         {1.,        0.,        0.  }, /* A  */
3245bee3fc89SBarry Smith         {0.,        1.,        0.  }, /* B  */
3246bee3fc89SBarry Smith         {gamma,     gamma,     0.  }, /* C  */
3247bee3fc89SBarry Smith         {1 + gamma, 1 - gamma, 0.  }, /* D  */
3248bee3fc89SBarry Smith         {2 - gamma, 2 - gamma, 0.  }, /* E  */
3249bee3fc89SBarry Smith         {1 - gamma, 1 + gamma, 0.  }, /* F  */
3250b7f5c055SJed Brown 
3251bee3fc89SBarry Smith         {.5,        0,         .25 }, /* G  */
3252bee3fc89SBarry Smith         {1.5,       0.,        .25 }, /* H  */
3253bee3fc89SBarry Smith         {.5,        1.,        .25 }, /* II */
3254bee3fc89SBarry Smith         {1.5,       1.,        .25 }, /* J  */
3255bee3fc89SBarry Smith         {.25,       .5,        .25 }, /* K  */
3256bee3fc89SBarry Smith         {1.25,      .5,        .25 }, /* L  */
3257bee3fc89SBarry Smith         {.75,       1.5,       .25 }, /* M  */
3258bee3fc89SBarry Smith         {1.75,      1.5,       .25 }, /* N  */
3259b7f5c055SJed Brown 
3260bee3fc89SBarry Smith         {0.,        0.,        .5  }, /* O  */
3261bee3fc89SBarry Smith         {1.,        1.,        .5  }, /* P  */
3262bee3fc89SBarry Smith         {gamma,     1 - gamma, .5  }, /* Q  */
3263bee3fc89SBarry Smith         {1 + gamma, gamma,     .5  }, /* R  */
3264bee3fc89SBarry Smith         {2 - gamma, 1 + gamma, .5  }, /* S  */
3265bee3fc89SBarry Smith         {1 - gamma, 2 - gamma, .5  }, /* T  */
3266b7f5c055SJed Brown 
3267bee3fc89SBarry Smith         {0.,        .5,        .75 }, /* U  */
3268bee3fc89SBarry Smith         {0.,        1.5,       .75 }, /* V  */
3269bee3fc89SBarry Smith         {1.,        .5,        .75 }, /* W  */
3270bee3fc89SBarry Smith         {1.,        1.5,       .75 }, /* X  */
3271bee3fc89SBarry Smith         {.5,        .75,       .75 }, /* Y  */
3272bee3fc89SBarry Smith         {.5,        1.75,      .75 }, /* Z  */
3273bee3fc89SBarry Smith         {1.5,       .25,       .75 }, /* Ap */
3274bee3fc89SBarry Smith         {1.5,       1.25,      .75 }, /* Bp */
3275b7f5c055SJed Brown 
3276bee3fc89SBarry Smith         {1.,        0.,        1.  }, /* Cp */
3277bee3fc89SBarry Smith         {0.,        1.,        1.  }, /* Dp */
3278bee3fc89SBarry Smith         {1 - gamma, 1 - gamma, 1.  }, /* Ep */
3279bee3fc89SBarry Smith         {1 + gamma, 1 + gamma, 1.  }, /* Fp */
3280bee3fc89SBarry Smith         {2 - gamma, gamma,     1.  }, /* Gp */
3281bee3fc89SBarry Smith         {gamma,     2 - gamma, 1.  }, /* Hp */
3282b7f5c055SJed Brown 
3283bee3fc89SBarry Smith         {.5,        0.,        1.25}, /* Ip */
3284bee3fc89SBarry Smith         {1.5,       0.,        1.25}, /* Jp */
3285bee3fc89SBarry Smith         {.5,        1.,        1.25}, /* Kp */
3286bee3fc89SBarry Smith         {1.5,       1.,        1.25}, /* Lp */
3287bee3fc89SBarry Smith         {.75,       .5,        1.25}, /* Mp */
3288bee3fc89SBarry Smith         {1.75,      .5,        1.25}, /* Np */
3289bee3fc89SBarry Smith         {.25,       1.5,       1.25}, /* Op */
3290bee3fc89SBarry Smith         {1.25,      1.5,       1.25}, /* Pp */
3291b7f5c055SJed Brown 
3292bee3fc89SBarry Smith         {0.,        0.,        1.5 }, /* Qp */
3293bee3fc89SBarry Smith         {1.,        1.,        1.5 }, /* Rp */
3294bee3fc89SBarry Smith         {1 - gamma, gamma,     1.5 }, /* Sp */
3295bee3fc89SBarry Smith         {2 - gamma, 1 - gamma, 1.5 }, /* Tp */
3296bee3fc89SBarry Smith         {1 + gamma, 2 - gamma, 1.5 }, /* Up */
3297bee3fc89SBarry Smith         {gamma,     1 + gamma, 1.5 }, /* Vp */
3298b7f5c055SJed Brown 
3299bee3fc89SBarry Smith         {0.,        .5,        1.75}, /* Wp */
3300bee3fc89SBarry Smith         {0.,        1.5,       1.75}, /* Xp */
3301bee3fc89SBarry Smith         {1.,        .5,        1.75}, /* Yp */
3302bee3fc89SBarry Smith         {1.,        1.5,       1.75}, /* Zp */
3303bee3fc89SBarry Smith         {.5,        .25,       1.75}, /* Aq */
3304bee3fc89SBarry Smith         {.5,        1.25,      1.75}, /* Bq */
3305bee3fc89SBarry Smith         {1.5,       .75,       1.75}, /* Cq */
3306bee3fc89SBarry Smith         {1.5,       1.75,      1.75}, /* Dq */
3307b7f5c055SJed Brown       };
3308b7f5c055SJed Brown       PetscInt(*cells)[64][4] = NULL;
3309b7f5c055SJed Brown       PetscBool *seen;
3310b7f5c055SJed Brown       PetscInt  *vertToTrueVert;
3311b7f5c055SJed Brown       PetscInt   count;
3312b7f5c055SJed Brown 
3313b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) extentPlus[i] = extent[i] + 1;
3314b7f5c055SJed Brown       numBlocks = 1;
3315b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) numBlocks *= extent[i];
3316b7f5c055SJed Brown       numBlocksPlus = 1;
3317b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) numBlocksPlus *= extentPlus[i];
3318b7f5c055SJed Brown       numFaces = numBlocks * facesPerBlock;
33199566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numBlocks, &cells));
33209566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numBlocksPlus * vertsPerBlock, &seen));
3321b7f5c055SJed Brown       for (PetscInt k = 0; k < extent[2]; k++) {
3322b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3323b7f5c055SJed Brown           for (PetscInt i = 0; i < extent[0]; i++) {
3324b7f5c055SJed Brown             for (PetscInt f = 0; f < facesPerBlock; f++) {
3325b7f5c055SJed Brown               for (PetscInt v = 0; v < 4; v++) {
3326b7f5c055SJed Brown                 PetscInt vertRaw     = pattern[f][v];
3327b7f5c055SJed Brown                 PetscInt blockidx    = vertRaw / 56;
3328b7f5c055SJed Brown                 PetscInt patternvert = vertRaw % 56;
3329b7f5c055SJed Brown                 PetscInt xplus       = (blockidx & 1);
3330b7f5c055SJed Brown                 PetscInt yplus       = (blockidx & 2) >> 1;
3331b7f5c055SJed Brown                 PetscInt zplus       = (blockidx & 4) >> 2;
3332b7f5c055SJed Brown                 PetscInt zcoord      = (periodic && periodic[2] == DM_BOUNDARY_PERIODIC) ? ((k + zplus) % extent[2]) : (k + zplus);
3333b7f5c055SJed Brown                 PetscInt ycoord      = (periodic && periodic[1] == DM_BOUNDARY_PERIODIC) ? ((j + yplus) % extent[1]) : (j + yplus);
3334b7f5c055SJed Brown                 PetscInt xcoord      = (periodic && periodic[0] == DM_BOUNDARY_PERIODIC) ? ((i + xplus) % extent[0]) : (i + xplus);
3335b7f5c055SJed Brown                 PetscInt vert        = ((zcoord * extentPlus[1] + ycoord) * extentPlus[0] + xcoord) * 56 + patternvert;
3336b7f5c055SJed Brown 
3337b7f5c055SJed Brown                 cells[(k * extent[1] + j) * extent[0] + i][f][v] = vert;
3338b7f5c055SJed Brown                 seen[vert]                                       = PETSC_TRUE;
3339b7f5c055SJed Brown               }
3340b7f5c055SJed Brown             }
3341b7f5c055SJed Brown           }
3342b7f5c055SJed Brown         }
3343b7f5c055SJed Brown       }
33449371c9d4SSatish Balay       for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++)
33459371c9d4SSatish Balay         if (seen[i]) numVertices++;
3346b7f5c055SJed Brown       count = 0;
33479566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numBlocksPlus * vertsPerBlock, &vertToTrueVert));
33489566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numVertices * 3, &vtxCoords));
3349b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++) vertToTrueVert[i] = -1;
3350b7f5c055SJed Brown       for (PetscInt k = 0; k < extentPlus[2]; k++) {
3351b7f5c055SJed Brown         for (PetscInt j = 0; j < extentPlus[1]; j++) {
3352b7f5c055SJed Brown           for (PetscInt i = 0; i < extentPlus[0]; i++) {
3353b7f5c055SJed Brown             for (PetscInt v = 0; v < vertsPerBlock; v++) {
3354b7f5c055SJed Brown               PetscInt vIdx = ((k * extentPlus[1] + j) * extentPlus[0] + i) * vertsPerBlock + v;
3355b7f5c055SJed Brown 
3356b7f5c055SJed Brown               if (seen[vIdx]) {
3357b7f5c055SJed Brown                 PetscInt thisVert;
3358b7f5c055SJed Brown 
3359b7f5c055SJed Brown                 vertToTrueVert[vIdx] = thisVert = count++;
3360b7f5c055SJed Brown 
3361b7f5c055SJed Brown                 for (PetscInt d = 0; d < 3; d++) vtxCoords[3 * thisVert + d] = patternCoords[v][d];
3362b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 0] += i * 2;
3363b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 1] += j * 2;
3364b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 2] += k * 2;
3365b7f5c055SJed Brown               }
3366b7f5c055SJed Brown             }
3367b7f5c055SJed Brown           }
3368b7f5c055SJed Brown         }
3369b7f5c055SJed Brown       }
3370b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocks; i++) {
3371b7f5c055SJed Brown         for (PetscInt f = 0; f < facesPerBlock; f++) {
3372ad540459SPierre Jolivet           for (PetscInt v = 0; v < 4; v++) cells[i][f][v] = vertToTrueVert[cells[i][f][v]];
3373b7f5c055SJed Brown         }
3374b7f5c055SJed Brown       }
33759566063dSJacob Faibussowitsch       PetscCall(PetscFree(vertToTrueVert));
33769566063dSJacob Faibussowitsch       PetscCall(PetscFree(seen));
3377b7f5c055SJed Brown       cells_flat = cells[0][0];
3378b7f5c055SJed Brown       numEdges   = 0;
3379b7f5c055SJed Brown       for (PetscInt i = 0; i < numFaces; i++) {
3380b7f5c055SJed Brown         for (PetscInt e = 0; e < 4; e++) {
3381b7f5c055SJed Brown           PetscInt         ev[]       = {cells_flat[i * 4 + e], cells_flat[i * 4 + ((e + 1) % 4)]};
3382b7f5c055SJed Brown           const PetscReal *evCoords[] = {&vtxCoords[3 * ev[0]], &vtxCoords[3 * ev[1]]};
3383b7f5c055SJed Brown 
3384b7f5c055SJed Brown           for (PetscInt d = 0; d < 3; d++) {
3385b7f5c055SJed Brown             if (!periodic || periodic[0] != DM_BOUNDARY_PERIODIC) {
3386b7f5c055SJed Brown               if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) numEdges++;
3387b7f5c055SJed Brown               if (evCoords[0][d] == 2. * extent[d] && evCoords[1][d] == 2. * extent[d]) numEdges++;
3388b7f5c055SJed Brown             }
3389b7f5c055SJed Brown           }
3390b7f5c055SJed Brown         }
3391b7f5c055SJed Brown       }
33929566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numEdges, &edges));
33939566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numEdges, &edgeSets));
3394b7f5c055SJed Brown       for (PetscInt edge = 0, i = 0; i < numFaces; i++) {
3395b7f5c055SJed Brown         for (PetscInt e = 0; e < 4; e++) {
3396b7f5c055SJed Brown           PetscInt         ev[]       = {cells_flat[i * 4 + e], cells_flat[i * 4 + ((e + 1) % 4)]};
3397b7f5c055SJed Brown           const PetscReal *evCoords[] = {&vtxCoords[3 * ev[0]], &vtxCoords[3 * ev[1]]};
3398b7f5c055SJed Brown 
3399b7f5c055SJed Brown           for (PetscInt d = 0; d < 3; d++) {
3400b7f5c055SJed Brown             if (!periodic || periodic[d] != DM_BOUNDARY_PERIODIC) {
3401b7f5c055SJed Brown               if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) {
3402b7f5c055SJed Brown                 edges[edge][0]   = ev[0];
3403b7f5c055SJed Brown                 edges[edge][1]   = ev[1];
3404b7f5c055SJed Brown                 edgeSets[edge++] = 2 * d;
3405b7f5c055SJed Brown               }
3406b7f5c055SJed Brown               if (evCoords[0][d] == 2. * extent[d] && evCoords[1][d] == 2. * extent[d]) {
3407b7f5c055SJed Brown                 edges[edge][0]   = ev[0];
3408b7f5c055SJed Brown                 edges[edge][1]   = ev[1];
3409b7f5c055SJed Brown                 edgeSets[edge++] = 2 * d + 1;
3410b7f5c055SJed Brown               }
3411b7f5c055SJed Brown             }
3412b7f5c055SJed Brown           }
3413b7f5c055SJed Brown         }
3414b7f5c055SJed Brown       }
3415b7f5c055SJed Brown     }
3416b7f5c055SJed Brown     evalFunc   = TPSEvaluate_Gyroid;
34174663dae6SJed Brown     normalFunc = TPSExtrudeNormalFunc_Gyroid;
3418b7f5c055SJed Brown     break;
3419b7f5c055SJed Brown   }
3420b7f5c055SJed Brown 
34219566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, topoDim));
3422c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildFromCellList(dm, numFaces, numVertices, 4, cells_flat));
34239566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildFromCellList(dm, 0, 0, 0, NULL));
34249566063dSJacob Faibussowitsch   PetscCall(PetscFree(cells_flat));
3425b7f5c055SJed Brown   {
3426b7f5c055SJed Brown     DM idm;
34279566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(dm, &idm));
342869d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &idm));
3429b7f5c055SJed Brown   }
3430c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildCoordinatesFromCellList(dm, spaceDim, vtxCoords));
34319566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildCoordinatesFromCellList(dm, spaceDim, NULL));
34329566063dSJacob Faibussowitsch   PetscCall(PetscFree(vtxCoords));
3433b7f5c055SJed Brown 
34349566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
34359566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "Face Sets", &label));
3436b7f5c055SJed Brown   for (PetscInt e = 0; e < numEdges; e++) {
3437b7f5c055SJed Brown     PetscInt        njoin;
3438b7f5c055SJed Brown     const PetscInt *join, verts[] = {numFaces + edges[e][0], numFaces + edges[e][1]};
34399566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, 2, verts, &njoin, &join));
344063a3b9bcSJacob 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]);
34419566063dSJacob Faibussowitsch     PetscCall(DMLabelSetValue(label, join[0], edgeSets[e]));
34429566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, 2, verts, &njoin, &join));
3443b7f5c055SJed Brown   }
34449566063dSJacob Faibussowitsch   PetscCall(PetscFree(edges));
34459566063dSJacob Faibussowitsch   PetscCall(PetscFree(edgeSets));
34461436d7faSJed Brown   if (tps_distribute) {
34471436d7faSJed Brown     DM               pdm = NULL;
34481436d7faSJed Brown     PetscPartitioner part;
34491436d7faSJed Brown 
34509566063dSJacob Faibussowitsch     PetscCall(DMPlexGetPartitioner(dm, &part));
34519566063dSJacob Faibussowitsch     PetscCall(PetscPartitionerSetFromOptions(part));
34529566063dSJacob Faibussowitsch     PetscCall(DMPlexDistribute(dm, 0, NULL, &pdm));
345348a46eb9SPierre Jolivet     if (pdm) PetscCall(DMPlexReplace_Internal(dm, &pdm));
34541436d7faSJed Brown     // Do not auto-distribute again
34559566063dSJacob Faibussowitsch     PetscCall(DMPlexDistributeSetDefault(dm, PETSC_FALSE));
34561436d7faSJed Brown   }
3457b7f5c055SJed Brown 
34589566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
3459b7f5c055SJed Brown   for (PetscInt refine = 0; refine < refinements; refine++) {
3460b7f5c055SJed Brown     PetscInt     m;
3461b7f5c055SJed Brown     DM           dmf;
3462b7f5c055SJed Brown     Vec          X;
3463b7f5c055SJed Brown     PetscScalar *x;
34649566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, MPI_COMM_NULL, &dmf));
346569d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &dmf));
3466b7f5c055SJed Brown 
34679566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &X));
34689566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(X, &m));
34699566063dSJacob Faibussowitsch     PetscCall(VecGetArray(X, &x));
347048a46eb9SPierre Jolivet     for (PetscInt i = 0; i < m; i += 3) PetscCall(TPSNearestPoint(evalFunc, &x[i]));
34719566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(X, &x));
3472b7f5c055SJed Brown   }
3473b7f5c055SJed Brown 
3474b7f5c055SJed Brown   // Face Sets has already been propagated to new vertices during refinement; this propagates to the initial vertices.
34759566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "Face Sets", &label));
34769566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete(dm, label));
3477b7f5c055SJed Brown 
347846139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
347946139095SJed Brown 
3480b7f5c055SJed Brown   if (thickness > 0) {
34814663dae6SJed Brown     DM              edm, cdm, ecdm;
34824663dae6SJed Brown     DMPlexTransform tr;
34834663dae6SJed Brown     const char     *prefix;
34844663dae6SJed Brown     PetscOptions    options;
34854663dae6SJed Brown     // Code from DMPlexExtrude
34864663dae6SJed Brown     PetscCall(DMPlexTransformCreate(PetscObjectComm((PetscObject)dm), &tr));
34874663dae6SJed Brown     PetscCall(DMPlexTransformSetDM(tr, dm));
34884663dae6SJed Brown     PetscCall(DMPlexTransformSetType(tr, DMPLEXEXTRUDE));
34894663dae6SJed Brown     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, &prefix));
34904663dae6SJed Brown     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)tr, prefix));
34914663dae6SJed Brown     PetscCall(PetscObjectGetOptions((PetscObject)dm, &options));
34924663dae6SJed Brown     PetscCall(PetscObjectSetOptions((PetscObject)tr, options));
34934663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetLayers(tr, layers));
34944663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetThickness(tr, thickness));
34954663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetTensor(tr, PETSC_FALSE));
34964663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetSymmetric(tr, PETSC_TRUE));
34974663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetNormalFunction(tr, normalFunc));
34984663dae6SJed Brown     PetscCall(DMPlexTransformSetFromOptions(tr));
34994663dae6SJed Brown     PetscCall(PetscObjectSetOptions((PetscObject)tr, NULL));
35004663dae6SJed Brown     PetscCall(DMPlexTransformSetUp(tr));
35014663dae6SJed Brown     PetscCall(PetscObjectViewFromOptions((PetscObject)tr, NULL, "-dm_plex_tps_transform_view"));
35024663dae6SJed Brown     PetscCall(DMPlexTransformApply(tr, dm, &edm));
35034663dae6SJed Brown     PetscCall(DMCopyDisc(dm, edm));
35044663dae6SJed Brown     PetscCall(DMGetCoordinateDM(dm, &cdm));
35054663dae6SJed Brown     PetscCall(DMGetCoordinateDM(edm, &ecdm));
35064663dae6SJed Brown     PetscCall(DMCopyDisc(cdm, ecdm));
35074663dae6SJed Brown     PetscCall(DMPlexTransformCreateDiscLabels(tr, edm));
35084663dae6SJed Brown     PetscCall(DMPlexTransformDestroy(&tr));
35094663dae6SJed Brown     if (edm) {
35104663dae6SJed Brown       ((DM_Plex *)edm->data)->printFEM    = ((DM_Plex *)dm->data)->printFEM;
35114663dae6SJed Brown       ((DM_Plex *)edm->data)->printL2     = ((DM_Plex *)dm->data)->printL2;
3512f5867de0SMatthew G. Knepley       ((DM_Plex *)edm->data)->printLocate = ((DM_Plex *)dm->data)->printLocate;
35134663dae6SJed Brown     }
351469d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &edm));
3515b7f5c055SJed Brown   }
35163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3517b7f5c055SJed Brown }
3518b7f5c055SJed Brown 
3519b7f5c055SJed Brown /*@
3520b7f5c055SJed Brown   DMPlexCreateTPSMesh - Create a distributed, interpolated mesh of a triply-periodic surface
3521b7f5c055SJed Brown 
3522b7f5c055SJed Brown   Collective
3523b7f5c055SJed Brown 
3524b7f5c055SJed Brown   Input Parameters:
3525a1cb98faSBarry Smith + comm           - The communicator for the `DM` object
3526b7f5c055SJed Brown . tpstype        - Type of triply-periodic surface
3527b7f5c055SJed Brown . extent         - Array of length 3 containing number of periods in each direction
352820f4b53cSBarry Smith . periodic       - array of length 3 with periodicity, or `NULL` for non-periodic
35291436d7faSJed Brown . tps_distribute - Distribute 2D manifold mesh prior to refinement and extrusion (more scalable)
3530817da375SSatish Balay . refinements    - Number of factor-of-2 refinements of 2D manifold mesh
35311436d7faSJed Brown . layers         - Number of cell layers extruded in normal direction
3532817da375SSatish Balay - thickness      - Thickness in normal direction
3533b7f5c055SJed Brown 
3534b7f5c055SJed Brown   Output Parameter:
3535a1cb98faSBarry Smith . dm - The `DM` object
3536a1cb98faSBarry Smith 
3537a1cb98faSBarry Smith   Level: beginner
3538b7f5c055SJed Brown 
3539b7f5c055SJed Brown   Notes:
3540b7f5c055SJed 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.
3541b7f5c055SJed Brown   https://en.wikipedia.org/wiki/Schwarz_minimal_surface#Schwarz_P_(%22Primitive%22) and can be cut with "clean" boundaries.
3542b7f5c055SJed 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.
3543b7f5c055SJed Brown   Our implementation creates a very coarse mesh of the surface and refines (by 4-way splitting) as many times as requested.
3544b7f5c055SJed Brown   On each refinement, all vertices are projected to their nearest point on the surface.
3545b7f5c055SJed Brown   This projection could readily be extended to related surfaces.
3546b7f5c055SJed Brown 
3547b7f5c055SJed Brown   The face (edge) sets for the Schwarz P surface are numbered 1(-x), 2(+x), 3(-y), 4(+y), 5(-z), 6(+z).
354820f4b53cSBarry Smith   When the mesh is refined, "Face Sets" contain the new vertices (created during refinement).  Use `DMPlexLabelComplete()` to propagate to coarse-level vertices.
3549b7f5c055SJed Brown 
355060225df5SJacob Faibussowitsch   Developer Notes:
3551b7f5c055SJed Brown   The Gyroid mesh does not currently mark boundary sets.
3552b7f5c055SJed Brown 
3553a1cb98faSBarry Smith   References:
3554a1cb98faSBarry Smith . * - Maskery et al, Insights into the mechanical properties of several triply periodic minimal surface lattice structures made by polymer additive manufacturing, 2017.
3555a1cb98faSBarry Smith   https://doi.org/10.1016/j.polymer.2017.11.049
3556b7f5c055SJed Brown 
35571cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSphereMesh()`, `DMSetType()`, `DMCreate()`
3558b7f5c055SJed Brown @*/
3559d71ae5a4SJacob 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)
3560d71ae5a4SJacob Faibussowitsch {
3561b7f5c055SJed Brown   PetscFunctionBegin;
35629566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
35639566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
35649566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateTPSMesh_Internal(*dm, tpstype, extent, periodic, tps_distribute, refinements, layers, thickness));
35653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3566b7f5c055SJed Brown }
3567b7f5c055SJed Brown 
35689318fe57SMatthew G. Knepley /*@
35699318fe57SMatthew G. Knepley   DMPlexCreateSphereMesh - Creates a mesh on the d-dimensional sphere, S^d.
35709318fe57SMatthew G. Knepley 
35719318fe57SMatthew G. Knepley   Collective
35729318fe57SMatthew G. Knepley 
35739318fe57SMatthew G. Knepley   Input Parameters:
3574a1cb98faSBarry Smith + comm    - The communicator for the `DM` object
35759318fe57SMatthew G. Knepley . dim     - The dimension
35769318fe57SMatthew G. Knepley . simplex - Use simplices, or tensor product cells
35779318fe57SMatthew G. Knepley - R       - The radius
35789318fe57SMatthew G. Knepley 
35799318fe57SMatthew G. Knepley   Output Parameter:
3580a1cb98faSBarry Smith . dm - The `DM` object
35819318fe57SMatthew G. Knepley 
35829318fe57SMatthew G. Knepley   Level: beginner
35839318fe57SMatthew G. Knepley 
35841cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBallMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
35859318fe57SMatthew G. Knepley @*/
3586d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateSphereMesh(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscReal R, DM *dm)
3587d71ae5a4SJacob Faibussowitsch {
35889318fe57SMatthew G. Knepley   PetscFunctionBegin;
35894f572ea9SToby Isaac   PetscAssertPointer(dm, 5);
35909566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
35919566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
35929566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSphereMesh_Internal(*dm, dim, simplex, R));
35933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
35949318fe57SMatthew G. Knepley }
35959318fe57SMatthew G. Knepley 
3596d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBallMesh_Internal(DM dm, PetscInt dim, PetscReal R)
3597d71ae5a4SJacob Faibussowitsch {
35989318fe57SMatthew G. Knepley   DM      sdm, vol;
35999318fe57SMatthew G. Knepley   DMLabel bdlabel;
36009318fe57SMatthew G. Knepley 
36019318fe57SMatthew G. Knepley   PetscFunctionBegin;
36029566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &sdm));
36039566063dSJacob Faibussowitsch   PetscCall(DMSetType(sdm, DMPLEX));
36049566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)sdm, "bd_"));
36059566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSphereMesh_Internal(sdm, dim - 1, PETSC_TRUE, R));
36069566063dSJacob Faibussowitsch   PetscCall(DMSetFromOptions(sdm));
36079566063dSJacob Faibussowitsch   PetscCall(DMViewFromOptions(sdm, NULL, "-dm_view"));
36089566063dSJacob Faibussowitsch   PetscCall(DMPlexGenerate(sdm, NULL, PETSC_TRUE, &vol));
36099566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&sdm));
361069d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &vol));
36119566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
36129566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "marker", &bdlabel));
36139566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkBoundaryFaces(dm, PETSC_DETERMINE, bdlabel));
36149566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete(dm, bdlabel));
36153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
361651a74b61SMatthew G. Knepley }
361751a74b61SMatthew G. Knepley 
361851a74b61SMatthew G. Knepley /*@
361951a74b61SMatthew G. Knepley   DMPlexCreateBallMesh - Creates a simplex mesh on the d-dimensional ball, B^d.
362051a74b61SMatthew G. Knepley 
362151a74b61SMatthew G. Knepley   Collective
362251a74b61SMatthew G. Knepley 
362351a74b61SMatthew G. Knepley   Input Parameters:
3624a1cb98faSBarry Smith + comm - The communicator for the `DM` object
362551a74b61SMatthew G. Knepley . dim  - The dimension
362651a74b61SMatthew G. Knepley - R    - The radius
362751a74b61SMatthew G. Knepley 
362851a74b61SMatthew G. Knepley   Output Parameter:
3629a1cb98faSBarry Smith . dm - The `DM` object
363051a74b61SMatthew G. Knepley 
3631a1cb98faSBarry Smith   Options Database Key:
363260225df5SJacob Faibussowitsch . bd_dm_refine - This will refine the surface mesh preserving the sphere geometry
363351a74b61SMatthew G. Knepley 
363451a74b61SMatthew G. Knepley   Level: beginner
363551a74b61SMatthew G. Knepley 
36361cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSphereMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
363751a74b61SMatthew G. Knepley @*/
3638d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateBallMesh(MPI_Comm comm, PetscInt dim, PetscReal R, DM *dm)
3639d71ae5a4SJacob Faibussowitsch {
364051a74b61SMatthew G. Knepley   PetscFunctionBegin;
36419566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
36429566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
36439566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBallMesh_Internal(*dm, dim, R));
36443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36452829fed8SMatthew G. Knepley }
36462829fed8SMatthew G. Knepley 
3647d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateReferenceCell_Internal(DM rdm, DMPolytopeType ct)
3648d71ae5a4SJacob Faibussowitsch {
36490a6ba040SMatthew G. Knepley   PetscFunctionBegin;
36509318fe57SMatthew G. Knepley   switch (ct) {
36519371c9d4SSatish Balay   case DM_POLYTOPE_POINT: {
36529318fe57SMatthew G. Knepley     PetscInt    numPoints[1]        = {1};
36539318fe57SMatthew G. Knepley     PetscInt    coneSize[1]         = {0};
36549318fe57SMatthew G. Knepley     PetscInt    cones[1]            = {0};
36559318fe57SMatthew G. Knepley     PetscInt    coneOrientations[1] = {0};
36569318fe57SMatthew G. Knepley     PetscScalar vertexCoords[1]     = {0.0};
36579318fe57SMatthew G. Knepley 
36589566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 0));
36599566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 0, numPoints, coneSize, cones, coneOrientations, vertexCoords));
36609371c9d4SSatish Balay   } break;
36619371c9d4SSatish Balay   case DM_POLYTOPE_SEGMENT: {
36629318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {2, 1};
36639318fe57SMatthew G. Knepley     PetscInt    coneSize[3]         = {2, 0, 0};
36649318fe57SMatthew G. Knepley     PetscInt    cones[2]            = {1, 2};
36659318fe57SMatthew G. Knepley     PetscInt    coneOrientations[2] = {0, 0};
36669318fe57SMatthew G. Knepley     PetscScalar vertexCoords[2]     = {-1.0, 1.0};
36679318fe57SMatthew G. Knepley 
36689566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 1));
36699566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
36709371c9d4SSatish Balay   } break;
36719371c9d4SSatish Balay   case DM_POLYTOPE_POINT_PRISM_TENSOR: {
3672b5a892a1SMatthew G. Knepley     PetscInt    numPoints[2]        = {2, 1};
3673b5a892a1SMatthew G. Knepley     PetscInt    coneSize[3]         = {2, 0, 0};
3674b5a892a1SMatthew G. Knepley     PetscInt    cones[2]            = {1, 2};
3675b5a892a1SMatthew G. Knepley     PetscInt    coneOrientations[2] = {0, 0};
3676b5a892a1SMatthew G. Knepley     PetscScalar vertexCoords[2]     = {-1.0, 1.0};
3677b5a892a1SMatthew G. Knepley 
36789566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 1));
36799566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
36809371c9d4SSatish Balay   } break;
36819371c9d4SSatish Balay   case DM_POLYTOPE_TRIANGLE: {
36829318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {3, 1};
36839318fe57SMatthew G. Knepley     PetscInt    coneSize[4]         = {3, 0, 0, 0};
36849318fe57SMatthew G. Knepley     PetscInt    cones[3]            = {1, 2, 3};
36859318fe57SMatthew G. Knepley     PetscInt    coneOrientations[3] = {0, 0, 0};
36869318fe57SMatthew G. Knepley     PetscScalar vertexCoords[6]     = {-1.0, -1.0, 1.0, -1.0, -1.0, 1.0};
36879318fe57SMatthew G. Knepley 
36889566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 2));
36899566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
36909371c9d4SSatish Balay   } break;
36919371c9d4SSatish Balay   case DM_POLYTOPE_QUADRILATERAL: {
36929318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {4, 1};
36939318fe57SMatthew G. Knepley     PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
36949318fe57SMatthew G. Knepley     PetscInt    cones[4]            = {1, 2, 3, 4};
36959318fe57SMatthew G. Knepley     PetscInt    coneOrientations[4] = {0, 0, 0, 0};
36969318fe57SMatthew G. Knepley     PetscScalar vertexCoords[8]     = {-1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0};
36979318fe57SMatthew G. Knepley 
36989566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 2));
36999566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37009371c9d4SSatish Balay   } break;
37019371c9d4SSatish Balay   case DM_POLYTOPE_SEG_PRISM_TENSOR: {
37029318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {4, 1};
37039318fe57SMatthew G. Knepley     PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
37049318fe57SMatthew G. Knepley     PetscInt    cones[4]            = {1, 2, 3, 4};
37059318fe57SMatthew G. Knepley     PetscInt    coneOrientations[4] = {0, 0, 0, 0};
37069318fe57SMatthew G. Knepley     PetscScalar vertexCoords[8]     = {-1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0};
37079318fe57SMatthew G. Knepley 
37089566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 2));
37099566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37109371c9d4SSatish Balay   } break;
37119371c9d4SSatish Balay   case DM_POLYTOPE_TETRAHEDRON: {
37129318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {4, 1};
37139318fe57SMatthew G. Knepley     PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
3714f0edb160SMatthew G. Knepley     PetscInt    cones[4]            = {1, 2, 3, 4};
37159318fe57SMatthew G. Knepley     PetscInt    coneOrientations[4] = {0, 0, 0, 0};
3716f0edb160SMatthew 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};
37179318fe57SMatthew G. Knepley 
37189566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
37199566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37209371c9d4SSatish Balay   } break;
37219371c9d4SSatish Balay   case DM_POLYTOPE_HEXAHEDRON: {
37229318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {8, 1};
37239318fe57SMatthew G. Knepley     PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
3724f0edb160SMatthew G. Knepley     PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
37259318fe57SMatthew G. Knepley     PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
37269371c9d4SSatish 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};
37279318fe57SMatthew G. Knepley 
37289566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
37299566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37309371c9d4SSatish Balay   } break;
37319371c9d4SSatish Balay   case DM_POLYTOPE_TRI_PRISM: {
37329318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {6, 1};
37339318fe57SMatthew G. Knepley     PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
3734f0edb160SMatthew G. Knepley     PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
37359318fe57SMatthew G. Knepley     PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
37369371c9d4SSatish 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};
37379318fe57SMatthew G. Knepley 
37389566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
37399566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37409371c9d4SSatish Balay   } break;
37419371c9d4SSatish Balay   case DM_POLYTOPE_TRI_PRISM_TENSOR: {
37429318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {6, 1};
37439318fe57SMatthew G. Knepley     PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
37449318fe57SMatthew G. Knepley     PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
37459318fe57SMatthew G. Knepley     PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
37469371c9d4SSatish 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};
37479318fe57SMatthew G. Knepley 
37489566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
37499566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37509371c9d4SSatish Balay   } break;
37519371c9d4SSatish Balay   case DM_POLYTOPE_QUAD_PRISM_TENSOR: {
37529318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {8, 1};
37539318fe57SMatthew G. Knepley     PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
37549318fe57SMatthew G. Knepley     PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
37559318fe57SMatthew G. Knepley     PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
37569371c9d4SSatish 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};
37579318fe57SMatthew G. Knepley 
37589566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
37599566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37609371c9d4SSatish Balay   } break;
37619371c9d4SSatish Balay   case DM_POLYTOPE_PYRAMID: {
37629318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {5, 1};
37639318fe57SMatthew G. Knepley     PetscInt    coneSize[6]         = {5, 0, 0, 0, 0, 0};
3764f0edb160SMatthew G. Knepley     PetscInt    cones[5]            = {1, 2, 3, 4, 5};
37659318fe57SMatthew G. Knepley     PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
37669371c9d4SSatish 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};
37679318fe57SMatthew G. Knepley 
37689566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
37699566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37709371c9d4SSatish Balay   } break;
3771d71ae5a4SJacob Faibussowitsch   default:
3772d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)rdm), PETSC_ERR_ARG_WRONG, "Cannot create reference cell for cell type %s", DMPolytopeTypes[ct]);
37739318fe57SMatthew G. Knepley   }
37749318fe57SMatthew G. Knepley   {
37759318fe57SMatthew G. Knepley     PetscInt Nv, v;
37769318fe57SMatthew G. Knepley 
37779318fe57SMatthew G. Knepley     /* Must create the celltype label here so that we do not automatically try to compute the types */
37789566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(rdm, "celltype"));
37799566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCellType(rdm, 0, ct));
37809566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(rdm, NULL, &Nv));
37819566063dSJacob Faibussowitsch     for (v = 1; v < Nv; ++v) PetscCall(DMPlexSetCellType(rdm, v, DM_POLYTOPE_POINT));
37829318fe57SMatthew G. Knepley   }
37839566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolateInPlace_Internal(rdm));
37849566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)rdm, DMPolytopeTypes[ct]));
37853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
37860a6ba040SMatthew G. Knepley }
37870a6ba040SMatthew G. Knepley 
37889318fe57SMatthew G. Knepley /*@
3789a1cb98faSBarry Smith   DMPlexCreateReferenceCell - Create a `DMPLEX` with the appropriate FEM reference cell
37909318fe57SMatthew G. Knepley 
37919318fe57SMatthew G. Knepley   Collective
37929318fe57SMatthew G. Knepley 
37939318fe57SMatthew G. Knepley   Input Parameters:
37949318fe57SMatthew G. Knepley + comm - The communicator
37959318fe57SMatthew G. Knepley - ct   - The cell type of the reference cell
37969318fe57SMatthew G. Knepley 
37979318fe57SMatthew G. Knepley   Output Parameter:
37989318fe57SMatthew G. Knepley . refdm - The reference cell
37999318fe57SMatthew G. Knepley 
38009318fe57SMatthew G. Knepley   Level: intermediate
38019318fe57SMatthew G. Knepley 
380242747ad1SJacob Faibussowitsch .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBoxMesh()`
38039318fe57SMatthew G. Knepley @*/
3804d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateReferenceCell(MPI_Comm comm, DMPolytopeType ct, DM *refdm)
3805d71ae5a4SJacob Faibussowitsch {
38060a6ba040SMatthew G. Knepley   PetscFunctionBegin;
38079566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, refdm));
38089566063dSJacob Faibussowitsch   PetscCall(DMSetType(*refdm, DMPLEX));
38099566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateReferenceCell_Internal(*refdm, ct));
38103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
38119318fe57SMatthew G. Knepley }
381279a015ccSMatthew G. Knepley 
3813d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoundaryLabel_Private(DM dm, const char name[])
3814d71ae5a4SJacob Faibussowitsch {
38159318fe57SMatthew G. Knepley   DM        plex;
38169318fe57SMatthew G. Knepley   DMLabel   label;
38179318fe57SMatthew G. Knepley   PetscBool hasLabel;
38180a6ba040SMatthew G. Knepley 
3819c22d3578SMatthew G. Knepley   PetscFunctionBegin;
38209566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &hasLabel));
38213ba16761SJacob Faibussowitsch   if (hasLabel) PetscFunctionReturn(PETSC_SUCCESS);
38229566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, name));
38239566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
38249566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
38259566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkBoundaryFaces(plex, 1, label));
38261c8afea9SMatthew G. Knepley   PetscCall(DMPlexLabelComplete(plex, label));
38279566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plex));
38283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
38299318fe57SMatthew G. Knepley }
3830acdc6f61SToby Isaac 
3831669647acSMatthew G. Knepley /*
3832669647acSMatthew 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.
3833669647acSMatthew G. Knepley 
3834669647acSMatthew G. Knepley     (x, y) -> (r, theta) = (x[1], (x[0] - lower[0]) * 2\pi/(upper[0] - lower[0]))
3835669647acSMatthew G. Knepley */
3836d71ae5a4SJacob 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[])
3837d71ae5a4SJacob Faibussowitsch {
3838669647acSMatthew G. Knepley   const PetscReal low = PetscRealPart(constants[0]);
3839669647acSMatthew G. Knepley   const PetscReal upp = PetscRealPart(constants[1]);
3840669647acSMatthew G. Knepley   const PetscReal r   = PetscRealPart(u[1]);
3841669647acSMatthew G. Knepley   const PetscReal th  = 2. * PETSC_PI * (PetscRealPart(u[0]) - low) / (upp - low);
3842669647acSMatthew G. Knepley 
3843669647acSMatthew G. Knepley   f0[0] = r * PetscCosReal(th);
3844669647acSMatthew G. Knepley   f0[1] = r * PetscSinReal(th);
3845669647acSMatthew G. Knepley }
3846669647acSMatthew G. Knepley 
38475dca41c3SJed Brown const char *const DMPlexShapes[] = {"box", "box_surface", "ball", "sphere", "cylinder", "schwarz_p", "gyroid", "doublet", "annulus", "hypercubic", "zbox", "unknown", "DMPlexShape", "DM_SHAPE_", NULL};
38489318fe57SMatthew G. Knepley 
3849d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateFromOptions_Internal(PetscOptionItems *PetscOptionsObject, PetscBool *useCoordSpace, DM dm)
3850d71ae5a4SJacob Faibussowitsch {
38519318fe57SMatthew G. Knepley   DMPlexShape    shape   = DM_SHAPE_BOX;
38529318fe57SMatthew G. Knepley   DMPolytopeType cell    = DM_POLYTOPE_TRIANGLE;
38539318fe57SMatthew G. Knepley   PetscInt       dim     = 2;
38549318fe57SMatthew G. Knepley   PetscBool      simplex = PETSC_TRUE, interpolate = PETSC_TRUE, adjCone = PETSC_FALSE, adjClosure = PETSC_TRUE, refDomain = PETSC_FALSE;
3855cd7e8a5eSksagiyam   PetscBool      flg, flg2, fflg, bdfflg, nameflg;
38569318fe57SMatthew G. Knepley   MPI_Comm       comm;
3857ed5e4e85SVaclav Hapla   char           filename[PETSC_MAX_PATH_LEN]   = "<unspecified>";
3858ed5e4e85SVaclav Hapla   char           bdFilename[PETSC_MAX_PATH_LEN] = "<unspecified>";
3859ed5e4e85SVaclav Hapla   char           plexname[PETSC_MAX_PATH_LEN]   = "";
38609318fe57SMatthew G. Knepley 
38619318fe57SMatthew G. Knepley   PetscFunctionBegin;
3862708be2fdSJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_CreateFromOptions, dm, 0, 0, 0));
38639566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
38649318fe57SMatthew G. Knepley   /* TODO Turn this into a registration interface */
38659566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_filename", "File containing a mesh", "DMPlexCreateFromFile", filename, filename, sizeof(filename), &fflg));
38669566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_boundary_filename", "File containing a mesh boundary", "DMPlexCreateFromFile", bdFilename, bdFilename, sizeof(bdFilename), &bdfflg));
38679566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_name", "Name of the mesh in the file", "DMPlexCreateFromFile", plexname, plexname, sizeof(plexname), &nameflg));
38689566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_plex_cell", "Cell shape", "", DMPolytopeTypes, (PetscEnum)cell, (PetscEnum *)&cell, NULL));
38699566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_reference_cell_domain", "Use a reference cell domain", "", refDomain, &refDomain, NULL));
38709566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_plex_shape", "Shape for built-in mesh", "", DMPlexShapes, (PetscEnum)shape, (PetscEnum *)&shape, &flg));
38719566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_dim", "Topological dimension of the mesh", "DMGetDimension", dim, &dim, &flg, 0));
3872cfb853baSMatthew G. Knepley   PetscCheck(dim >= 0, comm, PETSC_ERR_ARG_OUTOFRANGE, "Dimension %" PetscInt_FMT " should be in [0, infinity)", dim);
38739566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_simplex", "Mesh cell shape", "", simplex, &simplex, &flg));
38749566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_interpolate", "Flag to create edges and faces automatically", "", interpolate, &interpolate, &flg));
38759566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_adj_cone", "Set adjacency direction", "DMSetBasicAdjacency", adjCone, &adjCone, &flg));
38769566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_adj_closure", "Set adjacency size", "DMSetBasicAdjacency", adjClosure, &adjClosure, &flg2));
38779566063dSJacob Faibussowitsch   if (flg || flg2) PetscCall(DMSetBasicAdjacency(dm, adjCone, adjClosure));
38789318fe57SMatthew G. Knepley 
387961a622f3SMatthew G. Knepley   switch (cell) {
388061a622f3SMatthew G. Knepley   case DM_POLYTOPE_POINT:
388161a622f3SMatthew G. Knepley   case DM_POLYTOPE_SEGMENT:
388261a622f3SMatthew G. Knepley   case DM_POLYTOPE_POINT_PRISM_TENSOR:
388361a622f3SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
388461a622f3SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
388561a622f3SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
3886d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_HEXAHEDRON:
3887d71ae5a4SJacob Faibussowitsch     *useCoordSpace = PETSC_TRUE;
3888d71ae5a4SJacob Faibussowitsch     break;
3889d71ae5a4SJacob Faibussowitsch   default:
3890d71ae5a4SJacob Faibussowitsch     *useCoordSpace = PETSC_FALSE;
3891d71ae5a4SJacob Faibussowitsch     break;
389261a622f3SMatthew G. Knepley   }
389361a622f3SMatthew G. Knepley 
38949318fe57SMatthew G. Knepley   if (fflg) {
38959318fe57SMatthew G. Knepley     DM dmnew;
38969318fe57SMatthew G. Knepley 
38979566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), filename, plexname, interpolate, &dmnew));
38985de52c6dSVaclav Hapla     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
389969d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &dmnew));
39009318fe57SMatthew G. Knepley   } else if (refDomain) {
39019566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateReferenceCell_Internal(dm, cell));
39029318fe57SMatthew G. Knepley   } else if (bdfflg) {
39039318fe57SMatthew G. Knepley     DM bdm, dmnew;
39049318fe57SMatthew G. Knepley 
39059566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), bdFilename, plexname, interpolate, &bdm));
39069566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)bdm, "bd_"));
39079566063dSJacob Faibussowitsch     PetscCall(DMSetFromOptions(bdm));
39089566063dSJacob Faibussowitsch     PetscCall(DMPlexGenerate(bdm, NULL, interpolate, &dmnew));
39099566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&bdm));
39105de52c6dSVaclav Hapla     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
391169d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &dmnew));
39129318fe57SMatthew G. Knepley   } else {
39139566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)dm, DMPlexShapes[shape]));
39149318fe57SMatthew G. Knepley     switch (shape) {
3915669647acSMatthew G. Knepley     case DM_SHAPE_BOX:
39165dca41c3SJed Brown     case DM_SHAPE_ZBOX:
3917669647acSMatthew G. Knepley     case DM_SHAPE_ANNULUS: {
39189318fe57SMatthew G. Knepley       PetscInt       faces[3]  = {0, 0, 0};
39199318fe57SMatthew G. Knepley       PetscReal      lower[3]  = {0, 0, 0};
39209318fe57SMatthew G. Knepley       PetscReal      upper[3]  = {1, 1, 1};
39219318fe57SMatthew G. Knepley       DMBoundaryType bdt[3]    = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
3922669647acSMatthew G. Knepley       PetscBool      isAnnular = shape == DM_SHAPE_ANNULUS ? PETSC_TRUE : PETSC_FALSE;
39239318fe57SMatthew G. Knepley       PetscInt       i, n;
39249318fe57SMatthew G. Knepley 
39259318fe57SMatthew G. Knepley       n = dim;
39269318fe57SMatthew G. Knepley       for (i = 0; i < dim; ++i) faces[i] = (dim == 1 ? 1 : 4 - dim);
39279566063dSJacob Faibussowitsch       PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg));
39289318fe57SMatthew G. Knepley       n = 3;
39299566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
393063a3b9bcSJacob Faibussowitsch       PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Lower box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
39319318fe57SMatthew G. Knepley       n = 3;
39329566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
393363a3b9bcSJacob Faibussowitsch       PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Upper box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
39349318fe57SMatthew G. Knepley       n = 3;
39359566063dSJacob Faibussowitsch       PetscCall(PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *)bdt, &n, &flg));
393663a3b9bcSJacob Faibussowitsch       PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Box boundary types had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
3937669647acSMatthew G. Knepley 
3938669647acSMatthew G. Knepley       PetscCheck(!isAnnular || dim == 2, comm, PETSC_ERR_ARG_OUTOFRANGE, "Only two dimensional annuli have been implemented");
3939669647acSMatthew G. Knepley       if (isAnnular)
3940669647acSMatthew G. Knepley         for (i = 0; i < dim - 1; ++i) bdt[i] = DM_BOUNDARY_PERIODIC;
3941669647acSMatthew G. Knepley 
39429318fe57SMatthew G. Knepley       switch (cell) {
394361a622f3SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
39449566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateWedgeBoxMesh_Internal(dm, faces, lower, upper, bdt));
3945d410b0cfSMatthew G. Knepley         if (!interpolate) {
3946d410b0cfSMatthew G. Knepley           DM udm;
3947d410b0cfSMatthew G. Knepley 
39489566063dSJacob Faibussowitsch           PetscCall(DMPlexUninterpolate(dm, &udm));
394969d8a87bSksagiyam           PetscCall(DMPlexReplace_Internal(dm, &udm));
3950d410b0cfSMatthew G. Knepley         }
39519318fe57SMatthew G. Knepley         break;
3952d71ae5a4SJacob Faibussowitsch       default:
39535dca41c3SJed Brown         PetscCall(DMPlexCreateBoxMesh_Internal(dm, shape, dim, simplex, faces, lower, upper, bdt, interpolate));
3954d71ae5a4SJacob Faibussowitsch         break;
39559318fe57SMatthew G. Knepley       }
3956669647acSMatthew G. Knepley       if (isAnnular) {
3957669647acSMatthew G. Knepley         DM          cdm;
3958669647acSMatthew G. Knepley         PetscDS     cds;
3959669647acSMatthew G. Knepley         PetscScalar bounds[2] = {lower[0], upper[0]};
3960669647acSMatthew G. Knepley 
3961669647acSMatthew G. Knepley         // Fix coordinates for annular region
3962669647acSMatthew G. Knepley         PetscCall(DMSetPeriodicity(dm, NULL, NULL, NULL));
3963669647acSMatthew G. Knepley         PetscCall(DMSetCellCoordinatesLocal(dm, NULL));
3964669647acSMatthew G. Knepley         PetscCall(DMSetCellCoordinates(dm, NULL));
3965e44f6aebSMatthew G. Knepley         PetscCall(DMPlexCreateCoordinateSpace(dm, 1, PETSC_TRUE, NULL));
3966669647acSMatthew G. Knepley         PetscCall(DMGetCoordinateDM(dm, &cdm));
3967669647acSMatthew G. Knepley         PetscCall(DMGetDS(cdm, &cds));
3968669647acSMatthew G. Knepley         PetscCall(PetscDSSetConstants(cds, 2, bounds));
3969669647acSMatthew G. Knepley         PetscCall(DMPlexRemapGeometry(dm, 0.0, boxToAnnulus));
3970669647acSMatthew G. Knepley       }
39719371c9d4SSatish Balay     } break;
39729371c9d4SSatish Balay     case DM_SHAPE_BOX_SURFACE: {
39739318fe57SMatthew G. Knepley       PetscInt  faces[3] = {0, 0, 0};
39749318fe57SMatthew G. Knepley       PetscReal lower[3] = {0, 0, 0};
39759318fe57SMatthew G. Knepley       PetscReal upper[3] = {1, 1, 1};
39769318fe57SMatthew G. Knepley       PetscInt  i, n;
39779318fe57SMatthew G. Knepley 
39789318fe57SMatthew G. Knepley       n = dim + 1;
39799318fe57SMatthew G. Knepley       for (i = 0; i < dim + 1; ++i) faces[i] = (dim + 1 == 1 ? 1 : 4 - (dim + 1));
39809566063dSJacob Faibussowitsch       PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg));
39819318fe57SMatthew G. Knepley       n = 3;
39829566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
398363a3b9bcSJacob 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);
39849318fe57SMatthew G. Knepley       n = 3;
39859566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
398663a3b9bcSJacob 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);
39879566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(dm, dim + 1, faces, lower, upper, interpolate));
39889371c9d4SSatish Balay     } break;
39899371c9d4SSatish Balay     case DM_SHAPE_SPHERE: {
39909318fe57SMatthew G. Knepley       PetscReal R = 1.0;
39919318fe57SMatthew G. Knepley 
39929566063dSJacob Faibussowitsch       PetscCall(PetscOptionsReal("-dm_plex_sphere_radius", "Radius of the sphere", "", R, &R, &flg));
39939566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateSphereMesh_Internal(dm, dim, simplex, R));
39949371c9d4SSatish Balay     } break;
39959371c9d4SSatish Balay     case DM_SHAPE_BALL: {
39969318fe57SMatthew G. Knepley       PetscReal R = 1.0;
39979318fe57SMatthew G. Knepley 
39989566063dSJacob Faibussowitsch       PetscCall(PetscOptionsReal("-dm_plex_ball_radius", "Radius of the ball", "", R, &R, &flg));
39999566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateBallMesh_Internal(dm, dim, R));
40009371c9d4SSatish Balay     } break;
40019371c9d4SSatish Balay     case DM_SHAPE_CYLINDER: {
40029318fe57SMatthew G. Knepley       DMBoundaryType bdt = DM_BOUNDARY_NONE;
40039318fe57SMatthew G. Knepley       PetscInt       Nw  = 6;
40049318fe57SMatthew G. Knepley 
40059566063dSJacob Faibussowitsch       PetscCall(PetscOptionsEnum("-dm_plex_cylinder_bd", "Boundary type in the z direction", "", DMBoundaryTypes, (PetscEnum)bdt, (PetscEnum *)&bdt, NULL));
40069566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-dm_plex_cylinder_num_wedges", "Number of wedges around the cylinder", "", Nw, &Nw, NULL));
40079318fe57SMatthew G. Knepley       switch (cell) {
4008d71ae5a4SJacob Faibussowitsch       case DM_POLYTOPE_TRI_PRISM_TENSOR:
4009d71ae5a4SJacob Faibussowitsch         PetscCall(DMPlexCreateWedgeCylinderMesh_Internal(dm, Nw, interpolate));
4010d71ae5a4SJacob Faibussowitsch         break;
4011d71ae5a4SJacob Faibussowitsch       default:
4012d71ae5a4SJacob Faibussowitsch         PetscCall(DMPlexCreateHexCylinderMesh_Internal(dm, bdt));
4013d71ae5a4SJacob Faibussowitsch         break;
40149318fe57SMatthew G. Knepley       }
40159371c9d4SSatish Balay     } break;
4016b7f5c055SJed Brown     case DM_SHAPE_SCHWARZ_P: // fallthrough
40179371c9d4SSatish Balay     case DM_SHAPE_GYROID: {
4018b7f5c055SJed Brown       PetscInt       extent[3] = {1, 1, 1}, refine = 0, layers = 0, three;
4019b7f5c055SJed Brown       PetscReal      thickness   = 0.;
4020b7f5c055SJed Brown       DMBoundaryType periodic[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
4021b7f5c055SJed Brown       DMPlexTPSType  tps_type    = shape == DM_SHAPE_SCHWARZ_P ? DMPLEX_TPS_SCHWARZ_P : DMPLEX_TPS_GYROID;
40221436d7faSJed Brown       PetscBool      tps_distribute;
40239566063dSJacob Faibussowitsch       PetscCall(PetscOptionsIntArray("-dm_plex_tps_extent", "Number of replicas for each of three dimensions", NULL, extent, (three = 3, &three), NULL));
40249566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-dm_plex_tps_refine", "Number of refinements", NULL, refine, &refine, NULL));
40259566063dSJacob Faibussowitsch       PetscCall(PetscOptionsEnumArray("-dm_plex_tps_periodic", "Periodicity in each of three dimensions", NULL, DMBoundaryTypes, (PetscEnum *)periodic, (three = 3, &three), NULL));
40269566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-dm_plex_tps_layers", "Number of layers in volumetric extrusion (or zero to not extrude)", NULL, layers, &layers, NULL));
40279566063dSJacob Faibussowitsch       PetscCall(PetscOptionsReal("-dm_plex_tps_thickness", "Thickness of volumetric extrusion", NULL, thickness, &thickness, NULL));
40289566063dSJacob Faibussowitsch       PetscCall(DMPlexDistributeGetDefault(dm, &tps_distribute));
40299566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_tps_distribute", "Distribute the 2D mesh prior to refinement and extrusion", NULL, tps_distribute, &tps_distribute, NULL));
40309566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateTPSMesh_Internal(dm, tps_type, extent, periodic, tps_distribute, refine, layers, thickness));
40319371c9d4SSatish Balay     } break;
40329371c9d4SSatish Balay     case DM_SHAPE_DOUBLET: {
403305bd46c0SStefano Zampini       DM        dmnew;
403405bd46c0SStefano Zampini       PetscReal rl = 0.0;
403505bd46c0SStefano Zampini 
403605bd46c0SStefano Zampini       PetscCall(PetscOptionsReal("-dm_plex_doublet_refinementlimit", "Refinement limit", NULL, rl, &rl, NULL));
403705bd46c0SStefano Zampini       PetscCall(DMPlexCreateDoublet(PetscObjectComm((PetscObject)dm), dim, simplex, interpolate, rl, &dmnew));
40385de52c6dSVaclav Hapla       PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
403969d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &dmnew));
40409371c9d4SSatish Balay     } break;
4041cfb853baSMatthew G. Knepley     case DM_SHAPE_HYPERCUBIC: {
4042cfb853baSMatthew G. Knepley       PetscInt       *edges;
4043cfb853baSMatthew G. Knepley       PetscReal      *lower, *upper;
4044cfb853baSMatthew G. Knepley       DMBoundaryType *bdt;
4045cfb853baSMatthew G. Knepley       PetscInt        n, d;
4046cfb853baSMatthew G. Knepley 
4047cfb853baSMatthew G. Knepley       *useCoordSpace = PETSC_FALSE;
4048cfb853baSMatthew G. Knepley       PetscCall(PetscMalloc4(dim, &edges, dim, &lower, dim, &upper, dim, &bdt));
4049cfb853baSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
4050cfb853baSMatthew G. Knepley         edges[d] = 1;
4051cfb853baSMatthew G. Knepley         lower[d] = 0.;
4052cfb853baSMatthew G. Knepley         upper[d] = 1.;
4053cfb853baSMatthew G. Knepley         bdt[d]   = DM_BOUNDARY_PERIODIC;
4054cfb853baSMatthew G. Knepley       }
4055cfb853baSMatthew G. Knepley       n = dim;
4056cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", edges, &n, &flg));
4057cfb853baSMatthew G. Knepley       n = dim;
4058cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
4059cfb853baSMatthew 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);
4060cfb853baSMatthew G. Knepley       n = dim;
4061cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
4062cfb853baSMatthew 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);
4063cfb853baSMatthew G. Knepley       n = dim;
4064cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *)bdt, &n, &flg));
4065cfb853baSMatthew 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);
4066cfb853baSMatthew G. Knepley       PetscCall(DMPlexCreateHypercubicMesh_Internal(dm, dim, lower, upper, edges, bdt));
4067cfb853baSMatthew G. Knepley       PetscCall(PetscFree4(edges, lower, upper, bdt));
4068cfb853baSMatthew G. Knepley     } break;
4069d71ae5a4SJacob Faibussowitsch     default:
4070d71ae5a4SJacob Faibussowitsch       SETERRQ(comm, PETSC_ERR_SUP, "Domain shape %s is unsupported", DMPlexShapes[shape]);
40719318fe57SMatthew G. Knepley     }
40729318fe57SMatthew G. Knepley   }
40739566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
407448a46eb9SPierre Jolivet   if (!((PetscObject)dm)->name && nameflg) PetscCall(PetscObjectSetName((PetscObject)dm, plexname));
4075708be2fdSJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_CreateFromOptions, dm, 0, 0, 0));
40763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
40770a6ba040SMatthew G. Knepley }
40780a6ba040SMatthew G. Knepley 
4079d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions_NonRefinement_Plex(DM dm, PetscOptionItems *PetscOptionsObject)
4080d71ae5a4SJacob Faibussowitsch {
40810a6ba040SMatthew G. Knepley   DM_Plex  *mesh = (DM_Plex *)dm->data;
40827f9d8d6cSVaclav Hapla   PetscBool flg, flg2;
40839318fe57SMatthew G. Knepley   char      bdLabel[PETSC_MAX_PATH_LEN];
40840a6ba040SMatthew G. Knepley 
40850a6ba040SMatthew G. Knepley   PetscFunctionBegin;
40860a6ba040SMatthew G. Knepley   /* Handle viewing */
40879566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_print_set_values", "Output all set values info", "DMPlexMatSetClosure", PETSC_FALSE, &mesh->printSetValues, NULL));
40885962854dSMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_fem", "Debug output level for all fem computations", "DMPlexSNESComputeResidualFEM", 0, &mesh->printFEM, NULL, 0));
40895962854dSMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_fvm", "Debug output level for all fvm computations", "DMPlexSNESComputeResidualFVM", 0, &mesh->printFVM, NULL, 0));
40909566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-dm_plex_print_tol", "Tolerance for FEM output", "DMPlexSNESComputeResidualFEM", mesh->printTol, &mesh->printTol, NULL));
40919566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_l2", "Debug output level all L2 diff computations", "DMComputeL2Diff", 0, &mesh->printL2, NULL, 0));
4092f5867de0SMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_locate", "Debug output level all point location computations", "DMLocatePoints", 0, &mesh->printLocate, NULL, 0));
40939566063dSJacob Faibussowitsch   PetscCall(DMMonitorSetFromOptions(dm, "-dm_plex_monitor_throughput", "Monitor the simulation throughput", "DMPlexMonitorThroughput", DMPlexMonitorThroughput, NULL, &flg));
40949566063dSJacob Faibussowitsch   if (flg) PetscCall(PetscLogDefaultBegin());
40959318fe57SMatthew G. Knepley   /* Labeling */
40969566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_boundary_label", "Label to mark the mesh boundary", "", bdLabel, bdLabel, sizeof(bdLabel), &flg));
40979566063dSJacob Faibussowitsch   if (flg) PetscCall(DMPlexCreateBoundaryLabel_Private(dm, bdLabel));
4098953fc75cSMatthew G. Knepley   /* Point Location */
40999566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_hash_location", "Use grid hashing for point location", "DMInterpolate", PETSC_FALSE, &mesh->useHashLocation, NULL));
41000848f4b5SMatthew G. Knepley   /* Partitioning and distribution */
41019566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_partition_balance", "Attempt to evenly divide points on partition boundary between processes", "DMPlexSetPartitionBalance", PETSC_FALSE, &mesh->partitionBalance, NULL));
41022e62ab5aSMatthew G. Knepley   /* Generation and remeshing */
41039566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_remesh_bd", "Allow changes to the boundary on remeshing", "DMAdapt", PETSC_FALSE, &mesh->remeshBd, NULL));
4104b29cfa1cSToby Isaac   /* Projection behavior */
4105d5b43468SJose E. Roman   PetscCall(PetscOptionsBoundedInt("-dm_plex_max_projection_height", "Maximum mesh point height used to project locally", "DMPlexSetMaxProjectionHeight", 0, &mesh->maxProjectionHeight, NULL, 0));
41069566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_regular_refinement", "Use special nested projection algorithm for regular refinement", "DMPlexSetRegularRefinement", mesh->regularRefinement, &mesh->regularRefinement, NULL));
4107f12cf164SMatthew G. Knepley   /* Checking structure */
4108f12cf164SMatthew G. Knepley   {
41097f9d8d6cSVaclav Hapla     PetscBool all = PETSC_FALSE;
4110f12cf164SMatthew G. Knepley 
41117f9d8d6cSVaclav Hapla     PetscCall(PetscOptionsBool("-dm_plex_check_all", "Perform all basic checks", "DMPlexCheck", PETSC_FALSE, &all, NULL));
41127f9d8d6cSVaclav Hapla     if (all) {
41137f9d8d6cSVaclav Hapla       PetscCall(DMPlexCheck(dm));
41147f9d8d6cSVaclav Hapla     } else {
41159566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_symmetry", "Check that the adjacency information in the mesh is symmetric", "DMPlexCheckSymmetry", PETSC_FALSE, &flg, &flg2));
41167f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckSymmetry(dm));
41179566063dSJacob 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));
41187f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckSkeleton(dm, 0));
41199566063dSJacob 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));
41207f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckFaces(dm, 0));
41219566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_geometry", "Check that cells have positive volume", "DMPlexCheckGeometry", PETSC_FALSE, &flg, &flg2));
41227f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckGeometry(dm));
41239566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_pointsf", "Check some necessary conditions for PointSF", "DMPlexCheckPointSF", PETSC_FALSE, &flg, &flg2));
4124d7d32a9aSMatthew G. Knepley       if (flg && flg2) PetscCall(DMPlexCheckPointSF(dm, NULL, PETSC_FALSE));
41259566063dSJacob 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));
41267f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckInterfaceCones(dm));
41277f9d8d6cSVaclav Hapla     }
41289566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-dm_plex_check_cell_shape", "Check cell shape", "DMPlexCheckCellShape", PETSC_FALSE, &flg, &flg2));
41299566063dSJacob Faibussowitsch     if (flg && flg2) PetscCall(DMPlexCheckCellShape(dm, PETSC_TRUE, PETSC_DETERMINE));
4130f12cf164SMatthew G. Knepley   }
41319318fe57SMatthew G. Knepley   {
41329318fe57SMatthew G. Knepley     PetscReal scale = 1.0;
41334f3833eaSMatthew G. Knepley 
41349566063dSJacob Faibussowitsch     PetscCall(PetscOptionsReal("-dm_plex_scale", "Scale factor for mesh coordinates", "DMPlexScale", scale, &scale, &flg));
41359318fe57SMatthew G. Knepley     if (flg) {
41369318fe57SMatthew G. Knepley       Vec coordinates, coordinatesLocal;
41379318fe57SMatthew G. Knepley 
41389566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinates(dm, &coordinates));
41399566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinatesLocal(dm, &coordinatesLocal));
41409566063dSJacob Faibussowitsch       PetscCall(VecScale(coordinates, scale));
41419566063dSJacob Faibussowitsch       PetscCall(VecScale(coordinatesLocal, scale));
41429318fe57SMatthew G. Knepley     }
41439318fe57SMatthew G. Knepley   }
41449566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerSetFromOptions(mesh->partitioner));
41453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
414668d4fef7SMatthew G. Knepley }
414768d4fef7SMatthew G. Knepley 
4148d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions_Overlap_Plex(DM dm, PetscOptionItems *PetscOptionsObject, PetscInt *overlap)
4149d71ae5a4SJacob Faibussowitsch {
4150c506a872SMatthew G. Knepley   PetscInt  numOvLabels = 16, numOvExLabels = 16;
4151c506a872SMatthew G. Knepley   char     *ovLabelNames[16], *ovExLabelNames[16];
4152c506a872SMatthew G. Knepley   PetscInt  numOvValues = 16, numOvExValues = 16, l;
4153c506a872SMatthew G. Knepley   PetscBool flg;
4154c506a872SMatthew G. Knepley 
4155c506a872SMatthew G. Knepley   PetscFunctionBegin;
4156c506a872SMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_distribute_overlap", "The size of the overlap halo", "DMPlexDistribute", *overlap, overlap, NULL, 0));
4157c506a872SMatthew G. Knepley   PetscCall(PetscOptionsStringArray("-dm_distribute_overlap_labels", "List of overlap label names", "DMPlexDistribute", ovLabelNames, &numOvLabels, &flg));
4158c506a872SMatthew G. Knepley   if (!flg) numOvLabels = 0;
4159c506a872SMatthew G. Knepley   if (numOvLabels) {
4160c506a872SMatthew G. Knepley     ((DM_Plex *)dm->data)->numOvLabels = numOvLabels;
4161c506a872SMatthew G. Knepley     for (l = 0; l < numOvLabels; ++l) {
4162c506a872SMatthew G. Knepley       PetscCall(DMGetLabel(dm, ovLabelNames[l], &((DM_Plex *)dm->data)->ovLabels[l]));
4163c506a872SMatthew G. Knepley       PetscCheck(((DM_Plex *)dm->data)->ovLabels[l], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid label name %s", ovLabelNames[l]);
4164c506a872SMatthew G. Knepley       PetscCall(PetscFree(ovLabelNames[l]));
4165c506a872SMatthew G. Knepley     }
4166c506a872SMatthew G. Knepley     PetscCall(PetscOptionsIntArray("-dm_distribute_overlap_values", "List of overlap label values", "DMPlexDistribute", ((DM_Plex *)dm->data)->ovValues, &numOvValues, &flg));
4167c506a872SMatthew G. Knepley     if (!flg) numOvValues = 0;
4168c506a872SMatthew 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);
4169c506a872SMatthew G. Knepley 
4170c506a872SMatthew G. Knepley     PetscCall(PetscOptionsStringArray("-dm_distribute_overlap_exclude_labels", "List of overlap exclude label names", "DMPlexDistribute", ovExLabelNames, &numOvExLabels, &flg));
4171c506a872SMatthew G. Knepley     if (!flg) numOvExLabels = 0;
4172c506a872SMatthew G. Knepley     ((DM_Plex *)dm->data)->numOvExLabels = numOvExLabels;
4173c506a872SMatthew G. Knepley     for (l = 0; l < numOvExLabels; ++l) {
4174c506a872SMatthew G. Knepley       PetscCall(DMGetLabel(dm, ovExLabelNames[l], &((DM_Plex *)dm->data)->ovExLabels[l]));
4175c506a872SMatthew G. Knepley       PetscCheck(((DM_Plex *)dm->data)->ovExLabels[l], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid label name %s", ovExLabelNames[l]);
4176c506a872SMatthew G. Knepley       PetscCall(PetscFree(ovExLabelNames[l]));
4177c506a872SMatthew G. Knepley     }
4178c506a872SMatthew G. Knepley     PetscCall(PetscOptionsIntArray("-dm_distribute_overlap_exclude_values", "List of overlap exclude label values", "DMPlexDistribute", ((DM_Plex *)dm->data)->ovExValues, &numOvExValues, &flg));
4179c506a872SMatthew G. Knepley     if (!flg) numOvExValues = 0;
4180c506a872SMatthew 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);
4181c506a872SMatthew G. Knepley   }
41823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4183c506a872SMatthew G. Knepley }
4184c506a872SMatthew G. Knepley 
4185d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetFromOptions_Plex(DM dm, PetscOptionItems *PetscOptionsObject)
4186d71ae5a4SJacob Faibussowitsch {
4187bdf63967SMatthew G. Knepley   PetscFunctionList        ordlist;
4188bdf63967SMatthew G. Knepley   char                     oname[256];
4189a286e215SMatthew G. Knepley   DMPlexReorderDefaultFlag reorder;
4190d410b0cfSMatthew G. Knepley   PetscReal                volume    = -1.0;
41919318fe57SMatthew G. Knepley   PetscInt                 prerefine = 0, refine = 0, r, coarsen = 0, overlap = 0, extLayers = 0, dim;
4192a286e215SMatthew 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;
419368d4fef7SMatthew G. Knepley 
419468d4fef7SMatthew G. Knepley   PetscFunctionBegin;
4195d0609cedSBarry Smith   PetscOptionsHeadBegin(PetscOptionsObject, "DMPlex Options");
4196dd4c3f67SMatthew G. Knepley   if (dm->cloneOpts) goto non_refine;
41979318fe57SMatthew G. Knepley   /* Handle automatic creation */
41989566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
41996bc1bd01Sksagiyam   if (dim < 0) {
42006bc1bd01Sksagiyam     PetscCall(DMPlexCreateFromOptions_Internal(PetscOptionsObject, &coordSpace, dm));
42016bc1bd01Sksagiyam     created = PETSC_TRUE;
42026bc1bd01Sksagiyam   }
42036bc1bd01Sksagiyam   PetscCall(DMGetDimension(dm, &dim));
4204d89e6e46SMatthew G. Knepley   /* Handle interpolation before distribution */
42059566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_interpolate_pre", "Flag to interpolate mesh before distribution", "", interpolate, &interpolate, &flg));
4206d89e6e46SMatthew G. Knepley   if (flg) {
4207d89e6e46SMatthew G. Knepley     DMPlexInterpolatedFlag interpolated;
4208d89e6e46SMatthew G. Knepley 
42099566063dSJacob Faibussowitsch     PetscCall(DMPlexIsInterpolated(dm, &interpolated));
4210d89e6e46SMatthew G. Knepley     if (interpolated == DMPLEX_INTERPOLATED_FULL && !interpolate) {
4211d89e6e46SMatthew G. Knepley       DM udm;
4212d89e6e46SMatthew G. Knepley 
42139566063dSJacob Faibussowitsch       PetscCall(DMPlexUninterpolate(dm, &udm));
421469d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &udm));
4215d89e6e46SMatthew G. Knepley     } else if (interpolated != DMPLEX_INTERPOLATED_FULL && interpolate) {
4216d89e6e46SMatthew G. Knepley       DM idm;
4217d89e6e46SMatthew G. Knepley 
42189566063dSJacob Faibussowitsch       PetscCall(DMPlexInterpolate(dm, &idm));
421969d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &idm));
4220d89e6e46SMatthew G. Knepley     }
4221d89e6e46SMatthew G. Knepley   }
42229b44eab4SMatthew G. Knepley   /* Handle DMPlex refinement before distribution */
42239566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_ignore_model", "Flag to ignore the geometry model when refining", "DMCreate", ignoreModel, &ignoreModel, &flg));
4224ad540459SPierre Jolivet   if (flg) ((DM_Plex *)dm->data)->ignoreModel = ignoreModel;
42259566063dSJacob Faibussowitsch   PetscCall(DMPlexGetRefinementUniform(dm, &uniformOrig));
42269566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine_pre", "The number of refinements before distribution", "DMCreate", prerefine, &prerefine, NULL, 0));
42279566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_remap_pre", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL));
42289566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_uniform_pre", "Flag for uniform refinement before distribution", "DMCreate", uniform, &uniform, &flg));
42299566063dSJacob Faibussowitsch   if (flg) PetscCall(DMPlexSetRefinementUniform(dm, uniform));
42309566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-dm_refine_volume_limit_pre", "The maximum cell volume after refinement before distribution", "DMCreate", volume, &volume, &flg));
42319318fe57SMatthew G. Knepley   if (flg) {
42329566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementUniform(dm, PETSC_FALSE));
42339566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementLimit(dm, volume));
42349318fe57SMatthew G. Knepley     prerefine = PetscMax(prerefine, 1);
42359318fe57SMatthew G. Knepley   }
42369b44eab4SMatthew G. Knepley   for (r = 0; r < prerefine; ++r) {
42379b44eab4SMatthew G. Knepley     DM             rdm;
42389b44eab4SMatthew G. Knepley     PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc;
42399b44eab4SMatthew G. Knepley 
4240dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
42419566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &rdm));
424269d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &rdm));
4243dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
424461a622f3SMatthew G. Knepley     if (coordFunc && remap) {
42459566063dSJacob Faibussowitsch       PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
42469b44eab4SMatthew G. Knepley       ((DM_Plex *)dm->data)->coordFunc = coordFunc;
42479b44eab4SMatthew G. Knepley     }
42489b44eab4SMatthew G. Knepley   }
42499566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, uniformOrig));
42509318fe57SMatthew G. Knepley   /* Handle DMPlex extrusion before distribution */
42519566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_extrude", "The number of layers to extrude", "", extLayers, &extLayers, NULL, 0));
42529318fe57SMatthew G. Knepley   if (extLayers) {
42539318fe57SMatthew G. Knepley     DM edm;
42549318fe57SMatthew G. Knepley 
42559566063dSJacob Faibussowitsch     PetscCall(DMExtrude(dm, extLayers, &edm));
425669d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &edm));
425748d16a33SMatthew G. Knepley     ((DM_Plex *)dm->data)->coordFunc = NULL;
4258dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
4259d410b0cfSMatthew G. Knepley     extLayers = 0;
42605e17fc22SAidan Hamilton     PetscCall(DMGetDimension(dm, &dim));
42619318fe57SMatthew G. Knepley   }
4262bdf63967SMatthew G. Knepley   /* Handle DMPlex reordering before distribution */
42636bc1bd01Sksagiyam   PetscCall(DMPlexReorderGetDefault(dm, &reorder));
42649566063dSJacob Faibussowitsch   PetscCall(MatGetOrderingList(&ordlist));
42656bc1bd01Sksagiyam   PetscCall(PetscStrncpy(oname, MATORDERINGNATURAL, sizeof(oname)));
42669566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_plex_reorder", "Set mesh reordering type", "DMPlexGetOrdering", ordlist, MATORDERINGNATURAL, oname, sizeof(oname), &flg));
42676bc1bd01Sksagiyam   if (reorder == DMPLEX_REORDER_DEFAULT_TRUE || flg) {
4268bdf63967SMatthew G. Knepley     DM pdm;
4269bdf63967SMatthew G. Knepley     IS perm;
4270bdf63967SMatthew G. Knepley 
42719566063dSJacob Faibussowitsch     PetscCall(DMPlexGetOrdering(dm, oname, NULL, &perm));
42729566063dSJacob Faibussowitsch     PetscCall(DMPlexPermute(dm, perm, &pdm));
42739566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&perm));
427469d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &pdm));
4275dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
4276bdf63967SMatthew G. Knepley   }
42779b44eab4SMatthew G. Knepley   /* Handle DMPlex distribution */
42789566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeGetDefault(dm, &distribute));
4279c506a872SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_distribute", "Flag to redistribute a mesh among processes", "DMPlexDistribute", distribute, &distribute, NULL));
4280a286e215SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_distribute_save_sf", "Flag to save the migration SF", "DMPlexSetMigrationSF", saveSF, &saveSF, NULL));
4281dbbe0bcdSBarry Smith   PetscCall(DMSetFromOptions_Overlap_Plex(dm, PetscOptionsObject, &overlap));
42829b44eab4SMatthew G. Knepley   if (distribute) {
42839b44eab4SMatthew G. Knepley     DM               pdm = NULL;
42849b44eab4SMatthew G. Knepley     PetscPartitioner part;
4285a286e215SMatthew G. Knepley     PetscSF          sfMigration;
42869b44eab4SMatthew G. Knepley 
42879566063dSJacob Faibussowitsch     PetscCall(DMPlexGetPartitioner(dm, &part));
42889566063dSJacob Faibussowitsch     PetscCall(PetscPartitionerSetFromOptions(part));
4289a286e215SMatthew G. Knepley     PetscCall(DMPlexDistribute(dm, overlap, &sfMigration, &pdm));
429048a46eb9SPierre Jolivet     if (pdm) PetscCall(DMPlexReplace_Internal(dm, &pdm));
4291a286e215SMatthew G. Knepley     if (saveSF) PetscCall(DMPlexSetMigrationSF(dm, sfMigration));
4292a286e215SMatthew G. Knepley     PetscCall(PetscSFDestroy(&sfMigration));
42939b44eab4SMatthew G. Knepley   }
4294d2b2dc1eSMatthew G. Knepley   /* Must check CEED options before creating function space for coordinates */
4295d2b2dc1eSMatthew G. Knepley   {
4296d2b2dc1eSMatthew G. Knepley     PetscBool useCeed = PETSC_FALSE, flg;
4297d2b2dc1eSMatthew G. Knepley 
4298d2b2dc1eSMatthew G. Knepley     PetscCall(PetscOptionsBool("-dm_plex_use_ceed", "Use LibCEED as the FEM backend", "DMPlexSetUseCeed", useCeed, &useCeed, &flg));
4299d2b2dc1eSMatthew G. Knepley     if (flg) PetscCall(DMPlexSetUseCeed(dm, useCeed));
4300d2b2dc1eSMatthew G. Knepley   }
43019318fe57SMatthew G. Knepley   /* Create coordinate space */
43029318fe57SMatthew G. Knepley   if (created) {
430361a622f3SMatthew G. Knepley     DM_Plex  *mesh   = (DM_Plex *)dm->data;
4304e44f6aebSMatthew G. Knepley     PetscInt  degree = 1, deg;
43055515ebd3SMatthew G. Knepley     PetscInt  height = 0;
43065515ebd3SMatthew G. Knepley     DM        cdm;
43076858538eSMatthew G. Knepley     PetscBool flg;
43089318fe57SMatthew G. Knepley 
43099566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-dm_coord_space", "Use an FEM space for coordinates", "", coordSpace, &coordSpace, &flg));
43109566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-dm_coord_petscspace_degree", "FEM degree for coordinate space", "", degree, &degree, NULL));
4311e44f6aebSMatthew G. Knepley     PetscCall(DMGetCoordinateDegree_Internal(dm, &deg));
4312e44f6aebSMatthew G. Knepley     if (coordSpace && deg <= 1) PetscCall(DMPlexCreateCoordinateSpace(dm, degree, PETSC_TRUE, mesh->coordFunc));
43135515ebd3SMatthew G. Knepley     PetscCall(DMGetCoordinateDM(dm, &cdm));
431461a622f3SMatthew G. Knepley     if (flg && !coordSpace) {
431561a622f3SMatthew G. Knepley       PetscDS      cds;
431661a622f3SMatthew G. Knepley       PetscObject  obj;
431761a622f3SMatthew G. Knepley       PetscClassId id;
431861a622f3SMatthew G. Knepley 
43199566063dSJacob Faibussowitsch       PetscCall(DMGetDS(cdm, &cds));
43209566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
43219566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
432261a622f3SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
432361a622f3SMatthew G. Knepley         PetscContainer dummy;
432461a622f3SMatthew G. Knepley 
43259566063dSJacob Faibussowitsch         PetscCall(PetscContainerCreate(PETSC_COMM_SELF, &dummy));
43269566063dSJacob Faibussowitsch         PetscCall(PetscObjectSetName((PetscObject)dummy, "coordinates"));
43279566063dSJacob Faibussowitsch         PetscCall(DMSetField(cdm, 0, NULL, (PetscObject)dummy));
43289566063dSJacob Faibussowitsch         PetscCall(PetscContainerDestroy(&dummy));
43299566063dSJacob Faibussowitsch         PetscCall(DMClearDS(cdm));
433061a622f3SMatthew G. Knepley       }
433161a622f3SMatthew G. Knepley       mesh->coordFunc = NULL;
433261a622f3SMatthew G. Knepley     }
43336858538eSMatthew G. Knepley     PetscCall(PetscOptionsBool("-dm_sparse_localize", "Localize only necessary cells", "", dm->sparseLocalize, &dm->sparseLocalize, &flg));
43345515ebd3SMatthew G. Knepley     PetscCall(PetscOptionsInt("-dm_localize_height", "Localize edges and faces in addition to cells", "", height, &height, &flg));
43355515ebd3SMatthew G. Knepley     if (flg) PetscCall(DMPlexSetMaxProjectionHeight(cdm, height));
43369566063dSJacob Faibussowitsch     PetscCall(DMLocalizeCoordinates(dm));
43379318fe57SMatthew G. Knepley   }
433868d4fef7SMatthew G. Knepley   /* Handle DMPlex refinement */
433961a622f3SMatthew G. Knepley   remap = PETSC_TRUE;
43409566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine", "The number of uniform refinements", "DMCreate", refine, &refine, NULL, 0));
43419566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_remap", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL));
43429566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine_hierarchy", "The number of uniform refinements", "DMCreate", refine, &refine, &isHierarchy, 0));
43439566063dSJacob Faibussowitsch   if (refine) PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
434468d4fef7SMatthew G. Knepley   if (refine && isHierarchy) {
4345acdc6f61SToby Isaac     DM *dms, coarseDM;
434668d4fef7SMatthew G. Knepley 
43479566063dSJacob Faibussowitsch     PetscCall(DMGetCoarseDM(dm, &coarseDM));
43489566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)coarseDM));
43499566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(refine, &dms));
43509566063dSJacob Faibussowitsch     PetscCall(DMRefineHierarchy(dm, refine, dms));
435168d4fef7SMatthew G. Knepley     /* Total hack since we do not pass in a pointer */
43529566063dSJacob Faibussowitsch     PetscCall(DMPlexSwap_Static(dm, dms[refine - 1]));
435368d4fef7SMatthew G. Knepley     if (refine == 1) {
43549566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dm, dms[0]));
43559566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dm, PETSC_TRUE));
435668d4fef7SMatthew G. Knepley     } else {
43579566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dm, dms[refine - 2]));
43589566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dm, PETSC_TRUE));
43599566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dms[0], dms[refine - 1]));
43609566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dms[0], PETSC_TRUE));
436168d4fef7SMatthew G. Knepley     }
43629566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dms[refine - 1], coarseDM));
43639566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)coarseDM));
436468d4fef7SMatthew G. Knepley     /* Free DMs */
436568d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
4366dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dms[r], PetscOptionsObject));
43679566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dms[r]));
436868d4fef7SMatthew G. Knepley     }
43699566063dSJacob Faibussowitsch     PetscCall(PetscFree(dms));
437068d4fef7SMatthew G. Knepley   } else {
437168d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
43729318fe57SMatthew G. Knepley       DM             rdm;
437351a74b61SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc;
437468d4fef7SMatthew G. Knepley 
4375dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
43769566063dSJacob Faibussowitsch       PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &rdm));
437768d4fef7SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
437869d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &rdm));
4379dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
438061a622f3SMatthew G. Knepley       if (coordFunc && remap) {
43819566063dSJacob Faibussowitsch         PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
438251a74b61SMatthew G. Knepley         ((DM_Plex *)dm->data)->coordFunc = coordFunc;
438351a74b61SMatthew G. Knepley       }
438468d4fef7SMatthew G. Knepley     }
438568d4fef7SMatthew G. Knepley   }
43863cf6fe12SMatthew G. Knepley   /* Handle DMPlex coarsening */
43879566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_coarsen", "Coarsen the mesh", "DMCreate", coarsen, &coarsen, NULL, 0));
43889566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_coarsen_hierarchy", "The number of coarsenings", "DMCreate", coarsen, &coarsen, &isHierarchy, 0));
4389b653a561SMatthew G. Knepley   if (coarsen && isHierarchy) {
4390b653a561SMatthew G. Knepley     DM *dms;
4391b653a561SMatthew G. Knepley 
43929566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(coarsen, &dms));
43939566063dSJacob Faibussowitsch     PetscCall(DMCoarsenHierarchy(dm, coarsen, dms));
4394b653a561SMatthew G. Knepley     /* Free DMs */
4395b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
4396dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dms[r], PetscOptionsObject));
43979566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dms[r]));
4398b653a561SMatthew G. Knepley     }
43999566063dSJacob Faibussowitsch     PetscCall(PetscFree(dms));
4400b653a561SMatthew G. Knepley   } else {
4401b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
44029318fe57SMatthew G. Knepley       DM             cdm;
44039318fe57SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc;
44043cf6fe12SMatthew G. Knepley 
4405dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
44069566063dSJacob Faibussowitsch       PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &cdm));
44073cf6fe12SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
440869d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &cdm));
4409dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
44109318fe57SMatthew G. Knepley       if (coordFunc) {
44119566063dSJacob Faibussowitsch         PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
44129318fe57SMatthew G. Knepley         ((DM_Plex *)dm->data)->coordFunc = coordFunc;
44139318fe57SMatthew G. Knepley       }
44143cf6fe12SMatthew G. Knepley     }
4415b653a561SMatthew G. Knepley   }
4416909dfd52SMatthew G. Knepley   /* Handle ghost cells */
44179566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_create_fv_ghost_cells", "Flag to create finite volume ghost cells on the boundary", "DMCreate", ghostCells, &ghostCells, NULL));
4418909dfd52SMatthew G. Knepley   if (ghostCells) {
4419909dfd52SMatthew G. Knepley     DM   gdm;
4420909dfd52SMatthew G. Knepley     char lname[PETSC_MAX_PATH_LEN];
4421909dfd52SMatthew G. Knepley 
4422909dfd52SMatthew G. Knepley     lname[0] = '\0';
44239566063dSJacob Faibussowitsch     PetscCall(PetscOptionsString("-dm_plex_fv_ghost_cells_label", "Label name for ghost cells boundary", "DMCreate", lname, lname, sizeof(lname), &flg));
44249566063dSJacob Faibussowitsch     PetscCall(DMPlexConstructGhostCells(dm, flg ? lname : NULL, NULL, &gdm));
442569d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &gdm));
4426909dfd52SMatthew G. Knepley   }
44276913077dSMatthew G. Knepley   /* Handle 1D order */
44286bc1bd01Sksagiyam   if (reorder != DMPLEX_REORDER_DEFAULT_FALSE && dim == 1) {
44296913077dSMatthew G. Knepley     DM           cdm, rdm;
44306913077dSMatthew G. Knepley     PetscDS      cds;
44316913077dSMatthew G. Knepley     PetscObject  obj;
44326913077dSMatthew G. Knepley     PetscClassId id = PETSC_OBJECT_CLASSID;
44336913077dSMatthew G. Knepley     IS           perm;
44346bc1bd01Sksagiyam     PetscInt     Nf;
44356913077dSMatthew G. Knepley     PetscBool    distributed;
44366913077dSMatthew G. Knepley 
44379566063dSJacob Faibussowitsch     PetscCall(DMPlexIsDistributed(dm, &distributed));
44389566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
44399566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
44409566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(cds, &Nf));
44416913077dSMatthew G. Knepley     if (Nf) {
44429566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
44439566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
44446913077dSMatthew G. Knepley     }
44456bc1bd01Sksagiyam     if (!distributed && id != PETSCFE_CLASSID) {
44469566063dSJacob Faibussowitsch       PetscCall(DMPlexGetOrdering1D(dm, &perm));
44479566063dSJacob Faibussowitsch       PetscCall(DMPlexPermute(dm, perm, &rdm));
444869d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &rdm));
44499566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&perm));
44506913077dSMatthew G. Knepley     }
44516913077dSMatthew G. Knepley   }
44523cf6fe12SMatthew G. Knepley /* Handle */
4453dd4c3f67SMatthew G. Knepley non_refine:
4454dbbe0bcdSBarry Smith   PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
4455d0609cedSBarry Smith   PetscOptionsHeadEnd();
44563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44570a6ba040SMatthew G. Knepley }
44580a6ba040SMatthew G. Knepley 
4459d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMCreateGlobalVector_Plex(DM dm, Vec *vec)
4460d71ae5a4SJacob Faibussowitsch {
4461552f7358SJed Brown   PetscFunctionBegin;
44629566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector_Section_Private(dm, vec));
44639566063dSJacob Faibussowitsch   /* PetscCall(VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM)); */
44649566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEW, (void (*)(void))VecView_Plex));
44659566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEWNATIVE, (void (*)(void))VecView_Plex_Native));
44669566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOAD, (void (*)(void))VecLoad_Plex));
44679566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOADNATIVE, (void (*)(void))VecLoad_Plex_Native));
44683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4469552f7358SJed Brown }
4470552f7358SJed Brown 
4471d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMCreateLocalVector_Plex(DM dm, Vec *vec)
4472d71ae5a4SJacob Faibussowitsch {
4473552f7358SJed Brown   PetscFunctionBegin;
44749566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector_Section_Private(dm, vec));
44759566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEW, (void (*)(void))VecView_Plex_Local));
44769566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOAD, (void (*)(void))VecLoad_Plex_Local));
44773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4478552f7358SJed Brown }
4479552f7358SJed Brown 
4480d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGetDimPoints_Plex(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
4481d71ae5a4SJacob Faibussowitsch {
4482793f3fe5SMatthew G. Knepley   PetscInt depth, d;
4483793f3fe5SMatthew G. Knepley 
4484793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
44859566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
4486793f3fe5SMatthew G. Knepley   if (depth == 1) {
44879566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(dm, &d));
44889566063dSJacob Faibussowitsch     if (dim == 0) PetscCall(DMPlexGetDepthStratum(dm, dim, pStart, pEnd));
44899566063dSJacob Faibussowitsch     else if (dim == d) PetscCall(DMPlexGetDepthStratum(dm, 1, pStart, pEnd));
44909371c9d4SSatish Balay     else {
44919371c9d4SSatish Balay       *pStart = 0;
44929371c9d4SSatish Balay       *pEnd   = 0;
44939371c9d4SSatish Balay     }
4494793f3fe5SMatthew G. Knepley   } else {
44959566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, dim, pStart, pEnd));
4496793f3fe5SMatthew G. Knepley   }
44973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4498793f3fe5SMatthew G. Knepley }
4499793f3fe5SMatthew G. Knepley 
4500d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGetNeighbors_Plex(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
4501d71ae5a4SJacob Faibussowitsch {
4502502a2867SDave May   PetscSF            sf;
45030a19bb7dSprj-   PetscInt           niranks, njranks, n;
45040a19bb7dSprj-   const PetscMPIInt *iranks, *jranks;
45050a19bb7dSprj-   DM_Plex           *data = (DM_Plex *)dm->data;
4506502a2867SDave May 
45072f356facSMatthew G. Knepley   PetscFunctionBegin;
45089566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
45090a19bb7dSprj-   if (!data->neighbors) {
45109566063dSJacob Faibussowitsch     PetscCall(PetscSFSetUp(sf));
45119566063dSJacob Faibussowitsch     PetscCall(PetscSFGetRootRanks(sf, &njranks, &jranks, NULL, NULL, NULL));
45129566063dSJacob Faibussowitsch     PetscCall(PetscSFGetLeafRanks(sf, &niranks, &iranks, NULL, NULL));
45139566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(njranks + niranks + 1, &data->neighbors));
45149566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(data->neighbors + 1, jranks, njranks));
45159566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(data->neighbors + njranks + 1, iranks, niranks));
45160a19bb7dSprj-     n = njranks + niranks;
45179566063dSJacob Faibussowitsch     PetscCall(PetscSortRemoveDupsMPIInt(&n, data->neighbors + 1));
45180a19bb7dSprj-     /* The following cast should never fail: can't have more neighbors than PETSC_MPI_INT_MAX */
45199566063dSJacob Faibussowitsch     PetscCall(PetscMPIIntCast(n, data->neighbors));
45200a19bb7dSprj-   }
45210a19bb7dSprj-   if (nranks) *nranks = data->neighbors[0];
45220a19bb7dSprj-   if (ranks) {
45230a19bb7dSprj-     if (data->neighbors[0]) *ranks = data->neighbors + 1;
45240a19bb7dSprj-     else *ranks = NULL;
45250a19bb7dSprj-   }
45263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4527502a2867SDave May }
4528502a2867SDave May 
45291eb70e55SToby Isaac PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM, DM, Mat, Vec, Vec);
45301eb70e55SToby Isaac 
4531d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMInitialize_Plex(DM dm)
4532d71ae5a4SJacob Faibussowitsch {
4533552f7358SJed Brown   PetscFunctionBegin;
4534552f7358SJed Brown   dm->ops->view                      = DMView_Plex;
45352c40f234SMatthew G. Knepley   dm->ops->load                      = DMLoad_Plex;
4536552f7358SJed Brown   dm->ops->setfromoptions            = DMSetFromOptions_Plex;
453738221697SMatthew G. Knepley   dm->ops->clone                     = DMClone_Plex;
4538552f7358SJed Brown   dm->ops->setup                     = DMSetUp_Plex;
45391bb6d2a8SBarry Smith   dm->ops->createlocalsection        = DMCreateLocalSection_Plex;
454066ad2231SToby Isaac   dm->ops->createdefaultconstraints  = DMCreateDefaultConstraints_Plex;
4541552f7358SJed Brown   dm->ops->createglobalvector        = DMCreateGlobalVector_Plex;
4542552f7358SJed Brown   dm->ops->createlocalvector         = DMCreateLocalVector_Plex;
4543184d77edSJed Brown   dm->ops->getlocaltoglobalmapping   = NULL;
45440298fd71SBarry Smith   dm->ops->createfieldis             = NULL;
4545552f7358SJed Brown   dm->ops->createcoordinatedm        = DMCreateCoordinateDM_Plex;
4546f19dbd58SToby Isaac   dm->ops->createcoordinatefield     = DMCreateCoordinateField_Plex;
45470a6ba040SMatthew G. Knepley   dm->ops->getcoloring               = NULL;
4548552f7358SJed Brown   dm->ops->creatematrix              = DMCreateMatrix_Plex;
4549bceba477SMatthew G. Knepley   dm->ops->createinterpolation       = DMCreateInterpolation_Plex;
4550bd041c0cSMatthew G. Knepley   dm->ops->createmassmatrix          = DMCreateMassMatrix_Plex;
4551b4937a87SMatthew G. Knepley   dm->ops->createmassmatrixlumped    = DMCreateMassMatrixLumped_Plex;
45525a84ad33SLisandro Dalcin   dm->ops->createinjection           = DMCreateInjection_Plex;
4553552f7358SJed Brown   dm->ops->refine                    = DMRefine_Plex;
45540a6ba040SMatthew G. Knepley   dm->ops->coarsen                   = DMCoarsen_Plex;
45550a6ba040SMatthew G. Knepley   dm->ops->refinehierarchy           = DMRefineHierarchy_Plex;
4556b653a561SMatthew G. Knepley   dm->ops->coarsenhierarchy          = DMCoarsenHierarchy_Plex;
4557d410b0cfSMatthew G. Knepley   dm->ops->extrude                   = DMExtrude_Plex;
45580298fd71SBarry Smith   dm->ops->globaltolocalbegin        = NULL;
45590298fd71SBarry Smith   dm->ops->globaltolocalend          = NULL;
45600298fd71SBarry Smith   dm->ops->localtoglobalbegin        = NULL;
45610298fd71SBarry Smith   dm->ops->localtoglobalend          = NULL;
4562552f7358SJed Brown   dm->ops->destroy                   = DMDestroy_Plex;
4563552f7358SJed Brown   dm->ops->createsubdm               = DMCreateSubDM_Plex;
45642adcc780SMatthew G. Knepley   dm->ops->createsuperdm             = DMCreateSuperDM_Plex;
4565793f3fe5SMatthew G. Knepley   dm->ops->getdimpoints              = DMGetDimPoints_Plex;
4566552f7358SJed Brown   dm->ops->locatepoints              = DMLocatePoints_Plex;
45670709b2feSToby Isaac   dm->ops->projectfunctionlocal      = DMProjectFunctionLocal_Plex;
45680709b2feSToby Isaac   dm->ops->projectfunctionlabellocal = DMProjectFunctionLabelLocal_Plex;
4569bfc4295aSToby Isaac   dm->ops->projectfieldlocal         = DMProjectFieldLocal_Plex;
45708c6c5593SMatthew G. Knepley   dm->ops->projectfieldlabellocal    = DMProjectFieldLabelLocal_Plex;
4571ece3a9fcSMatthew G. Knepley   dm->ops->projectbdfieldlabellocal  = DMProjectBdFieldLabelLocal_Plex;
45720709b2feSToby Isaac   dm->ops->computel2diff             = DMComputeL2Diff_Plex;
4573b698f381SToby Isaac   dm->ops->computel2gradientdiff     = DMComputeL2GradientDiff_Plex;
45742a16baeaSToby Isaac   dm->ops->computel2fielddiff        = DMComputeL2FieldDiff_Plex;
457528d58a37SPierre Jolivet   dm->ops->getneighbors              = DMGetNeighbors_Plex;
45769566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertBoundaryValues_C", DMPlexInsertBoundaryValues_Plex));
45779566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertTimeDerviativeBoundaryValues_C", DMPlexInsertTimeDerivativeBoundaryValues_Plex));
45789566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMSetUpGLVisViewer_C", DMSetUpGLVisViewer_Plex));
45799566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMCreateNeumannOverlap_C", DMCreateNeumannOverlap_Plex));
45809566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetOverlap_C", DMPlexGetOverlap_Plex));
45819566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeGetDefault_C", DMPlexDistributeGetDefault_Plex));
45829566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeSetDefault_C", DMPlexDistributeSetDefault_Plex));
45836bc1bd01Sksagiyam   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderGetDefault_C", DMPlexReorderGetDefault_Plex));
45846bc1bd01Sksagiyam   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderSetDefault_C", DMPlexReorderSetDefault_Plex));
45859566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMInterpolateSolution_C", DMInterpolateSolution_Plex));
4586c506a872SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetOverlap_C", DMPlexGetOverlap_Plex));
4587c506a872SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexSetOverlap_C", DMPlexSetOverlap_Plex));
4588d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetUseCeed_C", DMPlexGetUseCeed_Plex));
4589d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexSetUseCeed_C", DMPlexSetUseCeed_Plex));
45903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4591552f7358SJed Brown }
4592552f7358SJed Brown 
4593d71ae5a4SJacob Faibussowitsch PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm)
4594d71ae5a4SJacob Faibussowitsch {
459563a16f15SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
45966725e60dSJed Brown   PetscSF  face_sf;
459763a16f15SMatthew G. Knepley 
459863a16f15SMatthew G. Knepley   PetscFunctionBegin;
459963a16f15SMatthew G. Knepley   mesh->refct++;
460063a16f15SMatthew G. Knepley   (*newdm)->data = mesh;
46015f06a3ddSJed Brown   PetscCall(DMPlexGetIsoperiodicFaceSF(dm, &face_sf));
46025f06a3ddSJed Brown   PetscCall(DMPlexSetIsoperiodicFaceSF(*newdm, face_sf));
46039566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)*newdm, DMPLEX));
46049566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(*newdm));
46053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
460663a16f15SMatthew G. Knepley }
460763a16f15SMatthew G. Knepley 
46088818961aSMatthew G Knepley /*MC
4609a1cb98faSBarry Smith   DMPLEX = "plex" - A `DM` object that encapsulates an unstructured mesh, or CW Complex, which can be expressed using a Hasse Diagram.
461020f4b53cSBarry Smith                     In the local representation, `Vec`s contain all unknowns in the interior and shared boundary. This is
46118818961aSMatthew G Knepley                     specified by a PetscSection object. Ownership in the global representation is determined by
4612a1cb98faSBarry Smith                     ownership of the underlying `DMPLEX` points. This is specified by another `PetscSection` object.
46138818961aSMatthew G Knepley 
4614e5893cccSMatthew G. Knepley   Options Database Keys:
4615250712c9SMatthew G. Knepley + -dm_refine_pre                     - Refine mesh before distribution
4616250712c9SMatthew G. Knepley + -dm_refine_uniform_pre             - Choose uniform or generator-based refinement
4617250712c9SMatthew G. Knepley + -dm_refine_volume_limit_pre        - Cell volume limit after pre-refinement using generator
4618250712c9SMatthew G. Knepley . -dm_distribute                     - Distribute mesh across processes
4619250712c9SMatthew G. Knepley . -dm_distribute_overlap             - Number of cells to overlap for distribution
4620250712c9SMatthew G. Knepley . -dm_refine                         - Refine mesh after distribution
4621250712c9SMatthew G. Knepley . -dm_plex_hash_location             - Use grid hashing for point location
4622ddce0771SMatthew G. Knepley . -dm_plex_hash_box_faces <n,m,p>    - The number of divisions in each direction of the grid hash
4623f12cf164SMatthew G. Knepley . -dm_plex_partition_balance         - Attempt to evenly divide points on partition boundary between processes
4624f12cf164SMatthew G. Knepley . -dm_plex_remesh_bd                 - Allow changes to the boundary on remeshing
4625d5b43468SJose E. Roman . -dm_plex_max_projection_height     - Maximum mesh point height used to project locally
4626f12cf164SMatthew G. Knepley . -dm_plex_regular_refinement        - Use special nested projection algorithm for regular refinement
4627aaa8cc7dSPierre Jolivet . -dm_plex_check_all                 - Perform all checks below
4628f12cf164SMatthew G. Knepley . -dm_plex_check_symmetry            - Check that the adjacency information in the mesh is symmetric
4629f12cf164SMatthew G. Knepley . -dm_plex_check_skeleton <celltype> - Check that each cell has the correct number of vertices
4630f12cf164SMatthew 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
4631f12cf164SMatthew G. Knepley . -dm_plex_check_geometry            - Check that cells have positive volume
4632f12cf164SMatthew G. Knepley . -dm_view :mesh.tex:ascii_latex     - View the mesh in LaTeX/TikZ
4633e5893cccSMatthew G. Knepley . -dm_plex_view_scale <num>          - Scale the TikZ
46345962854dSMatthew G. Knepley . -dm_plex_print_fem <num>           - View FEM assembly information, such as element vectors and matrices
46355962854dSMatthew G. Knepley - -dm_plex_print_fvm <num>           - View FVM assembly information, such as flux updates
4636e5893cccSMatthew G. Knepley 
46378818961aSMatthew G Knepley   Level: intermediate
46388818961aSMatthew G Knepley 
46391cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMType`, `DMPlexCreate()`, `DMCreate()`, `DMSetType()`, `PetscSection`
46408818961aSMatthew G Knepley M*/
46418818961aSMatthew G Knepley 
4642d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode DMCreate_Plex(DM dm)
4643d71ae5a4SJacob Faibussowitsch {
4644552f7358SJed Brown   DM_Plex *mesh;
4645412e9a14SMatthew G. Knepley   PetscInt unit;
4646552f7358SJed Brown 
4647552f7358SJed Brown   PetscFunctionBegin;
4648f39ec787SMatthew G. Knepley   PetscCall(PetscCitationsRegister(PlexCitation, &Plexcite));
4649552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
46504dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&mesh));
4651552f7358SJed Brown   dm->data = mesh;
4652552f7358SJed Brown 
4653552f7358SJed Brown   mesh->refct = 1;
46549566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->coneSection));
46559566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->supportSection));
4656552f7358SJed Brown   mesh->refinementUniform      = PETSC_TRUE;
4657552f7358SJed Brown   mesh->refinementLimit        = -1.0;
4658e600fa54SMatthew G. Knepley   mesh->distDefault            = PETSC_TRUE;
46596bc1bd01Sksagiyam   mesh->reorderDefault         = DMPLEX_REORDER_DEFAULT_NOTSET;
46601d1f2f2aSksagiyam   mesh->distributionName       = NULL;
46617d0f5628SVaclav Hapla   mesh->interpolated           = DMPLEX_INTERPOLATED_INVALID;
46627d0f5628SVaclav Hapla   mesh->interpolatedCollective = DMPLEX_INTERPOLATED_INVALID;
4663552f7358SJed Brown 
46649566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerCreate(PetscObjectComm((PetscObject)dm), &mesh->partitioner));
46652e62ab5aSMatthew G. Knepley   mesh->remeshBd = PETSC_FALSE;
4666d9deefdfSMatthew G. Knepley 
46678865f1eaSKarl Rupp   for (unit = 0; unit < NUM_PETSC_UNITS; ++unit) mesh->scale[unit] = 1.0;
4668552f7358SJed Brown 
4669df0420ecSMatthew G. Knepley   mesh->depthState    = -1;
4670ba2698f1SMatthew G. Knepley   mesh->celltypeState = -1;
46716113b454SMatthew G. Knepley   mesh->printTol      = 1.0e-10;
4672552f7358SJed Brown 
46739566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(dm));
46743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4675552f7358SJed Brown }
4676552f7358SJed Brown 
4677552f7358SJed Brown /*@
4678a1cb98faSBarry Smith   DMPlexCreate - Creates a `DMPLEX` object, which encapsulates an unstructured mesh, or CW complex, which can be expressed using a Hasse Diagram.
4679552f7358SJed Brown 
4680d083f849SBarry Smith   Collective
4681552f7358SJed Brown 
4682552f7358SJed Brown   Input Parameter:
4683a1cb98faSBarry Smith . comm - The communicator for the `DMPLEX` object
4684552f7358SJed Brown 
4685552f7358SJed Brown   Output Parameter:
4686a1cb98faSBarry Smith . mesh - The `DMPLEX` object
4687552f7358SJed Brown 
4688552f7358SJed Brown   Level: beginner
4689552f7358SJed Brown 
469042747ad1SJacob Faibussowitsch .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMType`, `DMCreate()`, `DMSetType()`
4691552f7358SJed Brown @*/
4692d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreate(MPI_Comm comm, DM *mesh)
4693d71ae5a4SJacob Faibussowitsch {
4694552f7358SJed Brown   PetscFunctionBegin;
46954f572ea9SToby Isaac   PetscAssertPointer(mesh, 2);
46969566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, mesh));
46979566063dSJacob Faibussowitsch   PetscCall(DMSetType(*mesh, DMPLEX));
46983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4699552f7358SJed Brown }
4700552f7358SJed Brown 
4701b09969d6SVaclav Hapla /*@C
4702a1cb98faSBarry Smith   DMPlexBuildFromCellListParallel - Build distributed `DMPLEX` topology from a list of vertices for each cell (common mesh generator output)
4703a1cb98faSBarry Smith 
470420f4b53cSBarry Smith   Collective; No Fortran Support
4705b09969d6SVaclav Hapla 
4706b09969d6SVaclav Hapla   Input Parameters:
4707a1cb98faSBarry Smith + dm          - The `DM`
4708b09969d6SVaclav Hapla . numCells    - The number of cells owned by this process
4709a1cb98faSBarry Smith . numVertices - The number of vertices to be owned by this process, or `PETSC_DECIDE`
4710a1cb98faSBarry Smith . NVertices   - The global number of vertices, or `PETSC_DETERMINE`
4711b09969d6SVaclav Hapla . numCorners  - The number of vertices for each cell
47125e488331SVaclav Hapla - cells       - An array of numCells*numCorners numbers, the global vertex numbers for each cell
4713b09969d6SVaclav Hapla 
4714be8c289dSNicolas Barral   Output Parameters:
4715a1cb98faSBarry Smith + vertexSF         - (Optional) `PetscSF` describing complete vertex ownership
4716be8c289dSNicolas Barral - verticesAdjSaved - (Optional) vertex adjacency array
4717b09969d6SVaclav Hapla 
4718b09969d6SVaclav Hapla   Level: advanced
4719b09969d6SVaclav Hapla 
4720a1cb98faSBarry Smith   Notes:
4721a1cb98faSBarry Smith   Two triangles sharing a face
4722a1cb98faSBarry Smith .vb
4723a1cb98faSBarry Smith 
4724a1cb98faSBarry Smith         2
4725a1cb98faSBarry Smith       / | \
4726a1cb98faSBarry Smith      /  |  \
4727a1cb98faSBarry Smith     /   |   \
4728a1cb98faSBarry Smith    0  0 | 1  3
4729a1cb98faSBarry Smith     \   |   /
4730a1cb98faSBarry Smith      \  |  /
4731a1cb98faSBarry Smith       \ | /
4732a1cb98faSBarry Smith         1
4733a1cb98faSBarry Smith .ve
4734a1cb98faSBarry Smith   would have input
4735a1cb98faSBarry Smith .vb
4736a1cb98faSBarry Smith   numCells = 2, numVertices = 4
4737a1cb98faSBarry Smith   cells = [0 1 2  1 3 2]
4738a1cb98faSBarry Smith .ve
4739a1cb98faSBarry Smith   which would result in the `DMPLEX`
4740a1cb98faSBarry Smith .vb
4741a1cb98faSBarry Smith 
4742a1cb98faSBarry Smith         4
4743a1cb98faSBarry Smith       / | \
4744a1cb98faSBarry Smith      /  |  \
4745a1cb98faSBarry Smith     /   |   \
4746a1cb98faSBarry Smith    2  0 | 1  5
4747a1cb98faSBarry Smith     \   |   /
4748a1cb98faSBarry Smith      \  |  /
4749a1cb98faSBarry Smith       \ | /
4750a1cb98faSBarry Smith         3
4751a1cb98faSBarry Smith .ve
4752a1cb98faSBarry Smith 
4753a1cb98faSBarry Smith   Vertices are implicitly numbered consecutively 0,...,NVertices.
4754a1cb98faSBarry Smith   Each rank owns a chunk of numVertices consecutive vertices.
4755a1cb98faSBarry Smith   If numVertices is `PETSC_DECIDE`, PETSc will distribute them as evenly as possible using PetscLayout.
4756a1cb98faSBarry Smith   If NVertices is `PETSC_DETERMINE` and numVertices is PETSC_DECIDE, NVertices is computed by PETSc as the maximum vertex index in cells + 1.
4757a1cb98faSBarry Smith   If only NVertices is `PETSC_DETERMINE`, it is computed as the sum of numVertices over all ranks.
4758a1cb98faSBarry Smith 
4759a1cb98faSBarry Smith   The cell distribution is arbitrary non-overlapping, independent of the vertex distribution.
4760a1cb98faSBarry Smith 
47611cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildFromCellList()`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildCoordinatesFromCellListParallel()`,
4762a1cb98faSBarry Smith           `PetscSF`
4763b09969d6SVaclav Hapla @*/
4764d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildFromCellListParallel(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt NVertices, PetscInt numCorners, const PetscInt cells[], PetscSF *vertexSF, PetscInt **verticesAdjSaved)
4765d71ae5a4SJacob Faibussowitsch {
47662464107aSksagiyam   PetscSF     sfPoint;
47672464107aSksagiyam   PetscLayout layout;
476882fb893eSVaclav Hapla   PetscInt    numVerticesAdj, *verticesAdj, *cones, c, p;
4769a47d0d45SMatthew G. Knepley 
4770a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
477125b6865aSVaclav Hapla   PetscValidLogicalCollectiveInt(dm, NVertices, 4);
47729566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
477325b6865aSVaclav Hapla   /* Get/check global number of vertices */
477425b6865aSVaclav Hapla   {
477525b6865aSVaclav Hapla     PetscInt       NVerticesInCells, i;
477625b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
477725b6865aSVaclav Hapla 
477825b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
477925b6865aSVaclav Hapla     NVerticesInCells = PETSC_MIN_INT;
47809371c9d4SSatish Balay     for (i = 0; i < len; i++)
47819371c9d4SSatish Balay       if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
478225b6865aSVaclav Hapla     ++NVerticesInCells;
4783712fec58SPierre Jolivet     PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &NVerticesInCells, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
478425b6865aSVaclav Hapla 
478525b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE && NVertices == PETSC_DECIDE) NVertices = NVerticesInCells;
47869371c9d4SSatish Balay     else
47879371c9d4SSatish 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);
478825b6865aSVaclav Hapla   }
47899079aca8SVaclav Hapla   /* Count locally unique vertices */
47909079aca8SVaclav Hapla   {
47919079aca8SVaclav Hapla     PetscHSetI vhash;
47929079aca8SVaclav Hapla     PetscInt   off = 0;
47939079aca8SVaclav Hapla 
47949566063dSJacob Faibussowitsch     PetscCall(PetscHSetICreate(&vhash));
4795a47d0d45SMatthew G. Knepley     for (c = 0; c < numCells; ++c) {
479648a46eb9SPierre Jolivet       for (p = 0; p < numCorners; ++p) PetscCall(PetscHSetIAdd(vhash, cells[c * numCorners + p]));
4797a47d0d45SMatthew G. Knepley     }
47989566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetSize(vhash, &numVerticesAdj));
47999566063dSJacob Faibussowitsch     if (!verticesAdjSaved) PetscCall(PetscMalloc1(numVerticesAdj, &verticesAdj));
4800ad540459SPierre Jolivet     else verticesAdj = *verticesAdjSaved;
48019566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetElems(vhash, &off, verticesAdj));
48029566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&vhash));
480363a3b9bcSJacob Faibussowitsch     PetscCheck(off == numVerticesAdj, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid number of local vertices %" PetscInt_FMT " should be %" PetscInt_FMT, off, numVerticesAdj);
4804a47d0d45SMatthew G. Knepley   }
48059566063dSJacob Faibussowitsch   PetscCall(PetscSortInt(numVerticesAdj, verticesAdj));
4806a47d0d45SMatthew G. Knepley   /* Create cones */
48079566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, 0, numCells + numVerticesAdj));
48089566063dSJacob Faibussowitsch   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, numCorners));
48099566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm));
48109566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCones(dm, &cones));
4811a47d0d45SMatthew G. Knepley   for (c = 0; c < numCells; ++c) {
4812a47d0d45SMatthew G. Knepley     for (p = 0; p < numCorners; ++p) {
4813a47d0d45SMatthew G. Knepley       const PetscInt gv = cells[c * numCorners + p];
4814a47d0d45SMatthew G. Knepley       PetscInt       lv;
4815a47d0d45SMatthew G. Knepley 
48169079aca8SVaclav Hapla       /* Positions within verticesAdj form 0-based local vertex numbering;
48179079aca8SVaclav Hapla          we need to shift it by numCells to get correct DAG points (cells go first) */
48189566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv));
481963a3b9bcSJacob Faibussowitsch       PetscCheck(lv >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not find global vertex %" PetscInt_FMT " in local connectivity", gv);
4820961cfab0SVaclav Hapla       cones[c * numCorners + p] = lv + numCells;
4821a47d0d45SMatthew G. Knepley     }
4822a47d0d45SMatthew G. Knepley   }
48232464107aSksagiyam   /* Build point sf */
48249566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(PetscObjectComm((PetscObject)dm), &layout));
48259566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetSize(layout, NVertices));
48269566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, numVertices));
48279566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
48289566063dSJacob Faibussowitsch   PetscCall(PetscSFCreateByMatchingIndices(layout, numVerticesAdj, verticesAdj, NULL, numCells, numVerticesAdj, verticesAdj, NULL, numCells, vertexSF, &sfPoint));
48299566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
48309566063dSJacob Faibussowitsch   if (!verticesAdjSaved) PetscCall(PetscFree(verticesAdj));
48319566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)sfPoint, "point SF"));
48322464107aSksagiyam   if (dm->sf) {
48332464107aSksagiyam     const char *prefix;
48342464107aSksagiyam 
48359566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm->sf, &prefix));
48369566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)sfPoint, prefix));
48372464107aSksagiyam   }
48389566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dm, sfPoint));
48399566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&sfPoint));
48409566063dSJacob Faibussowitsch   if (vertexSF) PetscCall(PetscObjectSetName((PetscObject)(*vertexSF), "Vertex Ownership SF"));
4841a47d0d45SMatthew G. Knepley   /* Fill in the rest of the topology structure */
48429566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
48439566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
48449566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
48453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4846a47d0d45SMatthew G. Knepley }
4847a47d0d45SMatthew G. Knepley 
4848b09969d6SVaclav Hapla /*@C
4849a1cb98faSBarry Smith   DMPlexBuildCoordinatesFromCellListParallel - Build `DM` coordinates from a list of coordinates for each owned vertex (common mesh generator output)
4850a1cb98faSBarry Smith 
485120f4b53cSBarry Smith   Collective; No Fortran Support
4852b09969d6SVaclav Hapla 
4853b09969d6SVaclav Hapla   Input Parameters:
4854a1cb98faSBarry Smith + dm           - The `DM`
4855b09969d6SVaclav Hapla . spaceDim     - The spatial dimension used for coordinates
4856a1cb98faSBarry Smith . sfVert       - `PetscSF` describing complete vertex ownership
4857b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
4858b09969d6SVaclav Hapla 
4859b09969d6SVaclav Hapla   Level: advanced
4860b09969d6SVaclav Hapla 
48611cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildFromCellListParallel()`
4862b09969d6SVaclav Hapla @*/
4863d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildCoordinatesFromCellListParallel(DM dm, PetscInt spaceDim, PetscSF sfVert, const PetscReal vertexCoords[])
4864d71ae5a4SJacob Faibussowitsch {
4865a47d0d45SMatthew G. Knepley   PetscSection coordSection;
4866a47d0d45SMatthew G. Knepley   Vec          coordinates;
4867a47d0d45SMatthew G. Knepley   PetscScalar *coords;
48681edcf0b2SVaclav Hapla   PetscInt     numVertices, numVerticesAdj, coordSize, v, vStart, vEnd;
4869a47d0d45SMatthew G. Knepley 
4870a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
48719566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
48729566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
48731dca8a05SBarry Smith   PetscCheck(vStart >= 0 && vEnd >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
48749566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, spaceDim));
48759566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfVert, &numVertices, &numVerticesAdj, NULL, NULL));
48761dca8a05SBarry 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);
48779566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
48789566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
48799566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, spaceDim));
48809566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, vStart, vEnd));
48811edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
48829566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, spaceDim));
48839566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, spaceDim));
4884a47d0d45SMatthew G. Knepley   }
48859566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
48869566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
48879566063dSJacob Faibussowitsch   PetscCall(VecCreate(PetscObjectComm((PetscObject)dm), &coordinates));
48889566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, spaceDim));
48899566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
48909566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
48919566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
48929566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
4893a47d0d45SMatthew G. Knepley   {
4894a47d0d45SMatthew G. Knepley     MPI_Datatype coordtype;
4895a47d0d45SMatthew G. Knepley 
4896a47d0d45SMatthew G. Knepley     /* Need a temp buffer for coords if we have complex/single */
48979566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_contiguous(spaceDim, MPIU_SCALAR, &coordtype));
48989566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_commit(&coordtype));
489921016a8bSBarry Smith #if defined(PETSC_USE_COMPLEX)
490021016a8bSBarry Smith     {
490121016a8bSBarry Smith       PetscScalar *svertexCoords;
490221016a8bSBarry Smith       PetscInt     i;
49039566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numVertices * spaceDim, &svertexCoords));
49043612f820SVaclav Hapla       for (i = 0; i < numVertices * spaceDim; i++) svertexCoords[i] = vertexCoords[i];
49059566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(sfVert, coordtype, svertexCoords, coords, MPI_REPLACE));
49069566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(sfVert, coordtype, svertexCoords, coords, MPI_REPLACE));
49079566063dSJacob Faibussowitsch       PetscCall(PetscFree(svertexCoords));
490821016a8bSBarry Smith     }
490921016a8bSBarry Smith #else
49109566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sfVert, coordtype, vertexCoords, coords, MPI_REPLACE));
49119566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sfVert, coordtype, vertexCoords, coords, MPI_REPLACE));
491221016a8bSBarry Smith #endif
49139566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_free(&coordtype));
4914a47d0d45SMatthew G. Knepley   }
49159566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
49169566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
49179566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
49189566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
49193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4920a47d0d45SMatthew G. Knepley }
4921a47d0d45SMatthew G. Knepley 
4922c3edce3dSSatish Balay /*@
4923a1cb98faSBarry Smith   DMPlexCreateFromCellListParallelPetsc - Create distributed `DMPLEX` from a list of vertices for each cell (common mesh generator output)
4924a1cb98faSBarry Smith 
4925a1cb98faSBarry Smith   Collective
4926a47d0d45SMatthew G. Knepley 
4927a47d0d45SMatthew G. Knepley   Input Parameters:
4928a47d0d45SMatthew G. Knepley + comm         - The communicator
4929a47d0d45SMatthew G. Knepley . dim          - The topological dimension of the mesh
4930a47d0d45SMatthew G. Knepley . numCells     - The number of cells owned by this process
4931a1cb98faSBarry Smith . numVertices  - The number of vertices owned by this process, or `PETSC_DECIDE`
4932a1cb98faSBarry Smith . NVertices    - The global number of vertices, or `PETSC_DECIDE`
4933a47d0d45SMatthew G. Knepley . numCorners   - The number of vertices for each cell
4934a47d0d45SMatthew G. Knepley . interpolate  - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
4935a47d0d45SMatthew G. Knepley . cells        - An array of numCells*numCorners numbers, the global vertex numbers for each cell
4936a47d0d45SMatthew G. Knepley . spaceDim     - The spatial dimension used for coordinates
4937a47d0d45SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
4938a47d0d45SMatthew G. Knepley 
4939d8d19677SJose E. Roman   Output Parameters:
4940a1cb98faSBarry Smith + dm          - The `DM`
4941a1cb98faSBarry Smith . vertexSF    - (Optional) `PetscSF` describing complete vertex ownership
494260225df5SJacob Faibussowitsch - verticesAdj - (Optional) vertex adjacency array
4943a47d0d45SMatthew G. Knepley 
4944b09969d6SVaclav Hapla   Level: intermediate
4945a47d0d45SMatthew G. Knepley 
4946a1cb98faSBarry Smith   Notes:
4947a1cb98faSBarry Smith   This function is just a convenient sequence of `DMCreate()`, `DMSetType()`, `DMSetDimension()`,
4948a1cb98faSBarry Smith   `DMPlexBuildFromCellListParallel()`, `DMPlexInterpolate()`, `DMPlexBuildCoordinatesFromCellListParallel()`
4949a1cb98faSBarry Smith 
4950a1cb98faSBarry Smith   See `DMPlexBuildFromCellListParallel()` for an example and details about the topology-related parameters.
4951a1cb98faSBarry Smith 
4952a1cb98faSBarry Smith   See `DMPlexBuildCoordinatesFromCellListParallel()` for details about the geometry-related parameters.
4953a1cb98faSBarry Smith 
49541cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()`
4955a47d0d45SMatthew G. Knepley @*/
4956d71ae5a4SJacob 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)
4957d71ae5a4SJacob Faibussowitsch {
4958a47d0d45SMatthew G. Knepley   PetscSF sfVert;
4959a47d0d45SMatthew G. Knepley 
4960a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
49619566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
49629566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
4963a47d0d45SMatthew G. Knepley   PetscValidLogicalCollectiveInt(*dm, dim, 2);
4964064a246eSJacob Faibussowitsch   PetscValidLogicalCollectiveInt(*dm, spaceDim, 9);
49659566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
49669566063dSJacob Faibussowitsch   PetscCall(DMPlexBuildFromCellListParallel(*dm, numCells, numVertices, NVertices, numCorners, cells, &sfVert, verticesAdj));
4967a47d0d45SMatthew G. Knepley   if (interpolate) {
49685fd9971aSMatthew G. Knepley     DM idm;
4969a47d0d45SMatthew G. Knepley 
49709566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
49719566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
4972a47d0d45SMatthew G. Knepley     *dm = idm;
4973a47d0d45SMatthew G. Knepley   }
49749566063dSJacob Faibussowitsch   PetscCall(DMPlexBuildCoordinatesFromCellListParallel(*dm, spaceDim, sfVert, vertexCoords));
497518d54ad4SMichael Lange   if (vertexSF) *vertexSF = sfVert;
49769566063dSJacob Faibussowitsch   else PetscCall(PetscSFDestroy(&sfVert));
49773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4978a47d0d45SMatthew G. Knepley }
4979a47d0d45SMatthew G. Knepley 
4980b09969d6SVaclav Hapla /*@C
4981a1cb98faSBarry Smith   DMPlexBuildFromCellList - Build `DMPLEX` topology from a list of vertices for each cell (common mesh generator output)
4982a1cb98faSBarry Smith 
498320f4b53cSBarry Smith   Collective; No Fortran Support
49849298eaa6SMatthew G Knepley 
49859298eaa6SMatthew G Knepley   Input Parameters:
4986a1cb98faSBarry Smith + dm          - The `DM`
4987b09969d6SVaclav Hapla . numCells    - The number of cells owned by this process
4988a1cb98faSBarry Smith . numVertices - The number of vertices owned by this process, or `PETSC_DETERMINE`
49899298eaa6SMatthew G Knepley . numCorners  - The number of vertices for each cell
49905e488331SVaclav Hapla - cells       - An array of numCells*numCorners numbers, the global vertex numbers for each cell
49919298eaa6SMatthew G Knepley 
4992b09969d6SVaclav Hapla   Level: advanced
49939298eaa6SMatthew G Knepley 
4994b09969d6SVaclav Hapla   Notes:
4995b09969d6SVaclav Hapla   Two triangles sharing a face
4996a1cb98faSBarry Smith .vb
49979298eaa6SMatthew G Knepley 
4998a1cb98faSBarry Smith         2
4999a1cb98faSBarry Smith       / | \
5000a1cb98faSBarry Smith      /  |  \
5001a1cb98faSBarry Smith     /   |   \
5002a1cb98faSBarry Smith    0  0 | 1  3
5003a1cb98faSBarry Smith     \   |   /
5004a1cb98faSBarry Smith      \  |  /
5005a1cb98faSBarry Smith       \ | /
5006a1cb98faSBarry Smith         1
5007a1cb98faSBarry Smith .ve
5008a1cb98faSBarry Smith   would have input
5009a1cb98faSBarry Smith .vb
5010a1cb98faSBarry Smith   numCells = 2, numVertices = 4
5011a1cb98faSBarry Smith   cells = [0 1 2  1 3 2]
5012a1cb98faSBarry Smith .ve
5013a1cb98faSBarry Smith   which would result in the `DMPLEX`
5014a1cb98faSBarry Smith .vb
5015a1cb98faSBarry Smith 
5016a1cb98faSBarry Smith         4
5017a1cb98faSBarry Smith       / | \
5018a1cb98faSBarry Smith      /  |  \
5019a1cb98faSBarry Smith     /   |   \
5020a1cb98faSBarry Smith    2  0 | 1  5
5021a1cb98faSBarry Smith     \   |   /
5022a1cb98faSBarry Smith      \  |  /
5023a1cb98faSBarry Smith       \ | /
5024a1cb98faSBarry Smith         3
5025a1cb98faSBarry Smith .ve
5026a1cb98faSBarry Smith 
5027a1cb98faSBarry Smith   If numVertices is `PETSC_DETERMINE`, it is computed by PETSc as the maximum vertex index in cells + 1.
502825b6865aSVaclav Hapla 
50291cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromCellListPetsc()`
5030b09969d6SVaclav Hapla @*/
5031d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildFromCellList(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const PetscInt cells[])
5032d71ae5a4SJacob Faibussowitsch {
5033961cfab0SVaclav Hapla   PetscInt *cones, c, p, dim;
5034b09969d6SVaclav Hapla 
5035b09969d6SVaclav Hapla   PetscFunctionBegin;
50369566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
50379566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
503825b6865aSVaclav Hapla   /* Get/check global number of vertices */
503925b6865aSVaclav Hapla   {
504025b6865aSVaclav Hapla     PetscInt       NVerticesInCells, i;
504125b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
504225b6865aSVaclav Hapla 
504325b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
504425b6865aSVaclav Hapla     NVerticesInCells = PETSC_MIN_INT;
50459371c9d4SSatish Balay     for (i = 0; i < len; i++)
50469371c9d4SSatish Balay       if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
504725b6865aSVaclav Hapla     ++NVerticesInCells;
504825b6865aSVaclav Hapla 
504925b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE) numVertices = NVerticesInCells;
50509371c9d4SSatish Balay     else
50519371c9d4SSatish 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);
505225b6865aSVaclav Hapla   }
50539566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
505448a46eb9SPierre Jolivet   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, numCorners));
50559566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm));
50569566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCones(dm, &cones));
5057b09969d6SVaclav Hapla   for (c = 0; c < numCells; ++c) {
5058ad540459SPierre Jolivet     for (p = 0; p < numCorners; ++p) cones[c * numCorners + p] = cells[c * numCorners + p] + numCells;
5059b09969d6SVaclav Hapla   }
50609566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
50619566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
50629566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
50633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5064b09969d6SVaclav Hapla }
5065b09969d6SVaclav Hapla 
5066b09969d6SVaclav Hapla /*@C
5067a1cb98faSBarry Smith   DMPlexBuildCoordinatesFromCellList - Build `DM` coordinates from a list of coordinates for each owned vertex (common mesh generator output)
5068a1cb98faSBarry Smith 
506920f4b53cSBarry Smith   Collective; No Fortran Support
5070b09969d6SVaclav Hapla 
5071b09969d6SVaclav Hapla   Input Parameters:
5072a1cb98faSBarry Smith + dm           - The `DM`
5073b09969d6SVaclav Hapla . spaceDim     - The spatial dimension used for coordinates
5074b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
5075b09969d6SVaclav Hapla 
5076b09969d6SVaclav Hapla   Level: advanced
5077b09969d6SVaclav Hapla 
50781cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellList()`
5079b09969d6SVaclav Hapla @*/
5080d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildCoordinatesFromCellList(DM dm, PetscInt spaceDim, const PetscReal vertexCoords[])
5081d71ae5a4SJacob Faibussowitsch {
5082b09969d6SVaclav Hapla   PetscSection coordSection;
5083b09969d6SVaclav Hapla   Vec          coordinates;
5084b09969d6SVaclav Hapla   DM           cdm;
5085b09969d6SVaclav Hapla   PetscScalar *coords;
50861edcf0b2SVaclav Hapla   PetscInt     v, vStart, vEnd, d;
5087b09969d6SVaclav Hapla 
5088b09969d6SVaclav Hapla   PetscFunctionBegin;
50899566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
50909566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
50911dca8a05SBarry Smith   PetscCheck(vStart >= 0 && vEnd >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
50929566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, spaceDim));
50939566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
50949566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
50959566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, spaceDim));
50969566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, vStart, vEnd));
50971edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
50989566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, spaceDim));
50999566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, spaceDim));
5100b09969d6SVaclav Hapla   }
51019566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
5102b09969d6SVaclav Hapla 
51039566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
51049566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(cdm, &coordinates));
51059566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, spaceDim));
51069566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
51079566063dSJacob Faibussowitsch   PetscCall(VecGetArrayWrite(coordinates, &coords));
51081edcf0b2SVaclav Hapla   for (v = 0; v < vEnd - vStart; ++v) {
5109ad540459SPierre Jolivet     for (d = 0; d < spaceDim; ++d) coords[v * spaceDim + d] = vertexCoords[v * spaceDim + d];
5110b09969d6SVaclav Hapla   }
51119566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayWrite(coordinates, &coords));
51129566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
51139566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
51149566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
51153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5116b09969d6SVaclav Hapla }
5117b09969d6SVaclav Hapla 
5118b09969d6SVaclav Hapla /*@
5119a1cb98faSBarry Smith   DMPlexCreateFromCellListPetsc - Create `DMPLEX` from a list of vertices for each cell (common mesh generator output), but only process 0 takes in the input
51203df08285SMatthew G. Knepley 
5121a1cb98faSBarry Smith   Collective
5122b09969d6SVaclav Hapla 
5123b09969d6SVaclav Hapla   Input Parameters:
5124b09969d6SVaclav Hapla + comm         - The communicator
5125b09969d6SVaclav Hapla . dim          - The topological dimension of the mesh
51263df08285SMatthew G. Knepley . numCells     - The number of cells, only on process 0
5127a1cb98faSBarry Smith . numVertices  - The number of vertices owned by this process, or `PETSC_DECIDE`, only on process 0
51283df08285SMatthew G. Knepley . numCorners   - The number of vertices for each cell, only on process 0
5129b09969d6SVaclav Hapla . interpolate  - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
51303df08285SMatthew G. Knepley . cells        - An array of numCells*numCorners numbers, the vertices for each cell, only on process 0
5131b09969d6SVaclav Hapla . spaceDim     - The spatial dimension used for coordinates
51323df08285SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex, only on process 0
5133b09969d6SVaclav Hapla 
5134b09969d6SVaclav Hapla   Output Parameter:
5135a1cb98faSBarry Smith . dm - The `DM`, which only has points on process 0
513625b6865aSVaclav Hapla 
5137b09969d6SVaclav Hapla   Level: intermediate
5138b09969d6SVaclav Hapla 
5139a1cb98faSBarry Smith   Notes:
5140a1cb98faSBarry Smith   This function is just a convenient sequence of `DMCreate()`, `DMSetType()`, `DMSetDimension()`, `DMPlexBuildFromCellList()`,
5141a1cb98faSBarry Smith   `DMPlexInterpolate()`, `DMPlexBuildCoordinatesFromCellList()`
5142a1cb98faSBarry Smith 
5143a1cb98faSBarry Smith   See `DMPlexBuildFromCellList()` for an example and details about the topology-related parameters.
5144a1cb98faSBarry Smith   See `DMPlexBuildCoordinatesFromCellList()` for details about the geometry-related parameters.
5145a1cb98faSBarry Smith   See `DMPlexCreateFromCellListParallelPetsc()` for parallel input
5146a1cb98faSBarry Smith 
51471cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildFromCellList()`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()`
51489298eaa6SMatthew G Knepley @*/
5149d71ae5a4SJacob 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)
5150d71ae5a4SJacob Faibussowitsch {
51513df08285SMatthew G. Knepley   PetscMPIInt rank;
51529298eaa6SMatthew G Knepley 
51539298eaa6SMatthew G Knepley   PetscFunctionBegin;
515428b400f6SJacob 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.");
51559566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
51569566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
51579566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
51589566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
5159c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildFromCellList(*dm, numCells, numVertices, numCorners, cells));
51609566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildFromCellList(*dm, 0, 0, 0, NULL));
51619298eaa6SMatthew G Knepley   if (interpolate) {
51625fd9971aSMatthew G. Knepley     DM idm;
51639298eaa6SMatthew G Knepley 
51649566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
51659566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
51669298eaa6SMatthew G Knepley     *dm = idm;
51679298eaa6SMatthew G Knepley   }
5168c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, vertexCoords));
51699566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, NULL));
51703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
51719298eaa6SMatthew G Knepley }
51729298eaa6SMatthew G Knepley 
5173939f6067SMatthew G. Knepley /*@
517420f4b53cSBarry Smith   DMPlexCreateFromDAG - This takes as input the adjacency-list representation of the Directed Acyclic Graph (Hasse Diagram) encoding a mesh, and produces a `DM`
5175939f6067SMatthew G. Knepley 
5176939f6067SMatthew G. Knepley   Input Parameters:
517720f4b53cSBarry Smith + dm               - The empty `DM` object, usually from `DMCreate()` and `DMSetDimension()`
5178939f6067SMatthew G. Knepley . depth            - The depth of the DAG
517920f4b53cSBarry Smith . numPoints        - Array of size depth + 1 containing the number of points at each `depth`
5180939f6067SMatthew G. Knepley . coneSize         - The cone size of each point
5181939f6067SMatthew G. Knepley . cones            - The concatenation of the cone points for each point, the cone list must be oriented correctly for each point
5182939f6067SMatthew G. Knepley . coneOrientations - The orientation of each cone point
518320f4b53cSBarry Smith - vertexCoords     - An array of `numPoints`[0]*spacedim numbers representing the coordinates of each vertex, with spacedim the value set via `DMSetCoordinateDim()`
5184939f6067SMatthew G. Knepley 
5185939f6067SMatthew G. Knepley   Output Parameter:
518620f4b53cSBarry Smith . dm - The `DM`
518720f4b53cSBarry Smith 
518820f4b53cSBarry Smith   Level: advanced
5189939f6067SMatthew G. Knepley 
5190a1cb98faSBarry Smith   Note:
5191a1cb98faSBarry Smith   Two triangles sharing a face would have input
5192a1cb98faSBarry Smith .vb
5193a1cb98faSBarry Smith   depth = 1, numPoints = [4 2], coneSize = [3 3 0 0 0 0]
5194a1cb98faSBarry Smith   cones = [2 3 4  3 5 4], coneOrientations = [0 0 0  0 0 0]
5195a1cb98faSBarry Smith  vertexCoords = [-1.0 0.0  0.0 -1.0  0.0 1.0  1.0 0.0]
5196a1cb98faSBarry Smith .ve
5197939f6067SMatthew G. Knepley   which would result in the DMPlex
5198a1cb98faSBarry Smith .vb
5199a1cb98faSBarry Smith         4
5200a1cb98faSBarry Smith       / | \
5201a1cb98faSBarry Smith      /  |  \
5202a1cb98faSBarry Smith     /   |   \
5203a1cb98faSBarry Smith    2  0 | 1  5
5204a1cb98faSBarry Smith     \   |   /
5205a1cb98faSBarry Smith      \  |  /
5206a1cb98faSBarry Smith       \ | /
5207a1cb98faSBarry Smith         3
5208a1cb98faSBarry Smith .ve
5209a1cb98faSBarry Smith   Notice that all points are numbered consecutively, unlike `DMPlexCreateFromCellListPetsc()`
5210939f6067SMatthew G. Knepley 
52111cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`
5212939f6067SMatthew G. Knepley @*/
5213d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateFromDAG(DM dm, PetscInt depth, const PetscInt numPoints[], const PetscInt coneSize[], const PetscInt cones[], const PetscInt coneOrientations[], const PetscScalar vertexCoords[])
5214d71ae5a4SJacob Faibussowitsch {
52159298eaa6SMatthew G Knepley   Vec          coordinates;
52169298eaa6SMatthew G Knepley   PetscSection coordSection;
52179298eaa6SMatthew G Knepley   PetscScalar *coords;
5218811e8653SToby Isaac   PetscInt     coordSize, firstVertex = -1, pStart = 0, pEnd = 0, p, v, dim, dimEmbed, d, off;
52199298eaa6SMatthew G Knepley 
52209298eaa6SMatthew G Knepley   PetscFunctionBegin;
52219566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
52229566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dimEmbed));
522363a3b9bcSJacob Faibussowitsch   PetscCheck(dimEmbed >= dim, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Embedding dimension %" PetscInt_FMT " cannot be less than intrinsic dimension %" PetscInt_FMT, dimEmbed, dim);
52249298eaa6SMatthew G Knepley   for (d = 0; d <= depth; ++d) pEnd += numPoints[d];
52259566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, pStart, pEnd));
52269298eaa6SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
52279566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeSize(dm, p, coneSize[p - pStart]));
5228ad540459SPierre Jolivet     if (firstVertex < 0 && !coneSize[p - pStart]) firstVertex = p - pStart;
522997e052ccSToby Isaac   }
52301dca8a05SBarry Smith   PetscCheck(firstVertex >= 0 || !numPoints[0], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Expected %" PetscInt_FMT " vertices but could not find any", numPoints[0]);
52319566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm)); /* Allocate space for cones */
52329298eaa6SMatthew G Knepley   for (p = pStart, off = 0; p < pEnd; off += coneSize[p - pStart], ++p) {
52339566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(dm, p, &cones[off]));
52349566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeOrientation(dm, p, &coneOrientations[off]));
52359298eaa6SMatthew G Knepley   }
52369566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
52379566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
52389298eaa6SMatthew G Knepley   /* Build coordinates */
52399566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
52409566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
52419566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dimEmbed));
52429566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numPoints[0]));
52439298eaa6SMatthew G Knepley   for (v = firstVertex; v < firstVertex + numPoints[0]; ++v) {
52449566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, dimEmbed));
52459566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dimEmbed));
52469298eaa6SMatthew G Knepley   }
52479566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
52489566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
52499566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
52509566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
52519566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
52529566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, dimEmbed));
52539566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
52549318fe57SMatthew G. Knepley   if (vertexCoords) {
52559566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
52569298eaa6SMatthew G Knepley     for (v = 0; v < numPoints[0]; ++v) {
52579298eaa6SMatthew G Knepley       PetscInt off;
52589298eaa6SMatthew G Knepley 
52599566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, v + firstVertex, &off));
5260ad540459SPierre Jolivet       for (d = 0; d < dimEmbed; ++d) coords[off + d] = vertexCoords[v * dimEmbed + d];
52619298eaa6SMatthew G Knepley     }
52629318fe57SMatthew G. Knepley   }
52639566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
52649566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
52659566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
52663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
52679298eaa6SMatthew G Knepley }
52688415267dSToby Isaac 
5269a4e35b19SJacob Faibussowitsch /*
5270a1cb98faSBarry Smith   DMPlexCreateCellVertexFromFile - Create a `DMPLEX` mesh from a simple cell-vertex file.
5271a1cb98faSBarry Smith 
5272a1cb98faSBarry Smith   Collective
52738ca92349SMatthew G. Knepley 
52748ca92349SMatthew G. Knepley + comm        - The MPI communicator
52758ca92349SMatthew G. Knepley . filename    - Name of the .dat file
52768ca92349SMatthew G. Knepley - interpolate - Create faces and edges in the mesh
52778ca92349SMatthew G. Knepley 
52788ca92349SMatthew G. Knepley   Output Parameter:
5279a1cb98faSBarry Smith . dm  - The `DM` object representing the mesh
52808ca92349SMatthew G. Knepley 
52818ca92349SMatthew G. Knepley   Level: beginner
52828ca92349SMatthew G. Knepley 
5283a1cb98faSBarry Smith   Note:
5284a1cb98faSBarry Smith   The format is the simplest possible:
5285a1cb98faSBarry Smith .vb
5286a1cb98faSBarry Smith   Ne
5287a1cb98faSBarry Smith   v0 v1 ... vk
5288a1cb98faSBarry Smith   Nv
5289a1cb98faSBarry Smith   x y z marker
5290a1cb98faSBarry Smith .ve
5291a1cb98faSBarry Smith 
5292a1cb98faSBarry Smith   Developer Note:
5293a1cb98faSBarry Smith   Should use a `PetscViewer` not a filename
5294a1cb98faSBarry Smith 
52951cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromFile()`, `DMPlexCreateMedFromFile()`, `DMPlexCreateGmsh()`, `DMPlexCreate()`
5296a4e35b19SJacob Faibussowitsch */
5297ff6a9541SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCellVertexFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
5298d71ae5a4SJacob Faibussowitsch {
52998ca92349SMatthew G. Knepley   DMLabel      marker;
53008ca92349SMatthew G. Knepley   PetscViewer  viewer;
53018ca92349SMatthew G. Knepley   Vec          coordinates;
53028ca92349SMatthew G. Knepley   PetscSection coordSection;
53038ca92349SMatthew G. Knepley   PetscScalar *coords;
53048ca92349SMatthew G. Knepley   char         line[PETSC_MAX_PATH_LEN];
53058ca92349SMatthew G. Knepley   PetscInt     dim = 3, cdim = 3, coordSize, v, c, d;
53068ca92349SMatthew G. Knepley   PetscMPIInt  rank;
5307f8d5e320SMatthew G. Knepley   int          snum, Nv, Nc, Ncn, Nl;
53088ca92349SMatthew G. Knepley 
53098ca92349SMatthew G. Knepley   PetscFunctionBegin;
53109566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
53119566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, &viewer));
53129566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(viewer, PETSCVIEWERASCII));
53139566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetMode(viewer, FILE_MODE_READ));
53149566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetName(viewer, filename));
5315dd400576SPatrick Sanan   if (rank == 0) {
53169566063dSJacob Faibussowitsch     PetscCall(PetscViewerRead(viewer, line, 4, NULL, PETSC_STRING));
5317f8d5e320SMatthew G. Knepley     snum = sscanf(line, "%d %d %d %d", &Nc, &Nv, &Ncn, &Nl);
531808401ef6SPierre Jolivet     PetscCheck(snum == 4, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
531925ce1634SJed Brown   } else {
5320f8d5e320SMatthew G. Knepley     Nc = Nv = Ncn = Nl = 0;
53218ca92349SMatthew G. Knepley   }
53229566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
53239566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
53249566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(*dm, 0, Nc + Nv));
53259566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
53269566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*dm, cdim));
53278ca92349SMatthew G. Knepley   /* Read topology */
5328dd400576SPatrick Sanan   if (rank == 0) {
5329f8d5e320SMatthew G. Knepley     char     format[PETSC_MAX_PATH_LEN];
5330f8d5e320SMatthew G. Knepley     PetscInt cone[8];
53318ca92349SMatthew G. Knepley     int      vbuf[8], v;
53328ca92349SMatthew G. Knepley 
53339371c9d4SSatish Balay     for (c = 0; c < Ncn; ++c) {
53349371c9d4SSatish Balay       format[c * 3 + 0] = '%';
53359371c9d4SSatish Balay       format[c * 3 + 1] = 'd';
53369371c9d4SSatish Balay       format[c * 3 + 2] = ' ';
53379371c9d4SSatish Balay     }
5338f8d5e320SMatthew G. Knepley     format[Ncn * 3 - 1] = '\0';
53399566063dSJacob Faibussowitsch     for (c = 0; c < Nc; ++c) PetscCall(DMPlexSetConeSize(*dm, c, Ncn));
53409566063dSJacob Faibussowitsch     PetscCall(DMSetUp(*dm));
53418ca92349SMatthew G. Knepley     for (c = 0; c < Nc; ++c) {
53429566063dSJacob Faibussowitsch       PetscCall(PetscViewerRead(viewer, line, Ncn, NULL, PETSC_STRING));
5343f8d5e320SMatthew G. Knepley       switch (Ncn) {
5344d71ae5a4SJacob Faibussowitsch       case 2:
5345d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1]);
5346d71ae5a4SJacob Faibussowitsch         break;
5347d71ae5a4SJacob Faibussowitsch       case 3:
5348d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2]);
5349d71ae5a4SJacob Faibussowitsch         break;
5350d71ae5a4SJacob Faibussowitsch       case 4:
5351d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3]);
5352d71ae5a4SJacob Faibussowitsch         break;
5353d71ae5a4SJacob Faibussowitsch       case 6:
5354d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5]);
5355d71ae5a4SJacob Faibussowitsch         break;
5356d71ae5a4SJacob Faibussowitsch       case 8:
5357d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5], &vbuf[6], &vbuf[7]);
5358d71ae5a4SJacob Faibussowitsch         break;
5359d71ae5a4SJacob Faibussowitsch       default:
5360d71ae5a4SJacob Faibussowitsch         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No cell shape with %d vertices", Ncn);
5361f8d5e320SMatthew G. Knepley       }
536208401ef6SPierre Jolivet       PetscCheck(snum == Ncn, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
5363f8d5e320SMatthew G. Knepley       for (v = 0; v < Ncn; ++v) cone[v] = vbuf[v] + Nc;
53648ca92349SMatthew G. Knepley       /* Hexahedra are inverted */
5365f8d5e320SMatthew G. Knepley       if (Ncn == 8) {
53668ca92349SMatthew G. Knepley         PetscInt tmp = cone[1];
53678ca92349SMatthew G. Knepley         cone[1]      = cone[3];
53688ca92349SMatthew G. Knepley         cone[3]      = tmp;
53698ca92349SMatthew G. Knepley       }
53709566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(*dm, c, cone));
53718ca92349SMatthew G. Knepley     }
53728ca92349SMatthew G. Knepley   }
53739566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(*dm));
53749566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(*dm));
53758ca92349SMatthew G. Knepley   /* Read coordinates */
53769566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(*dm, &coordSection));
53779566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
53789566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, cdim));
53799566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, Nc, Nc + Nv));
53808ca92349SMatthew G. Knepley   for (v = Nc; v < Nc + Nv; ++v) {
53819566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, cdim));
53829566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, cdim));
53838ca92349SMatthew G. Knepley   }
53849566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
53859566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
53869566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
53879566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
53889566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
53899566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, cdim));
53909566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
53919566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
5392dd400576SPatrick Sanan   if (rank == 0) {
5393f8d5e320SMatthew G. Knepley     char   format[PETSC_MAX_PATH_LEN];
53948ca92349SMatthew G. Knepley     double x[3];
5395f8d5e320SMatthew G. Knepley     int    l, val[3];
53968ca92349SMatthew G. Knepley 
5397f8d5e320SMatthew G. Knepley     if (Nl) {
53989371c9d4SSatish Balay       for (l = 0; l < Nl; ++l) {
53999371c9d4SSatish Balay         format[l * 3 + 0] = '%';
54009371c9d4SSatish Balay         format[l * 3 + 1] = 'd';
54019371c9d4SSatish Balay         format[l * 3 + 2] = ' ';
54029371c9d4SSatish Balay       }
5403f8d5e320SMatthew G. Knepley       format[Nl * 3 - 1] = '\0';
54049566063dSJacob Faibussowitsch       PetscCall(DMCreateLabel(*dm, "marker"));
54059566063dSJacob Faibussowitsch       PetscCall(DMGetLabel(*dm, "marker", &marker));
5406f8d5e320SMatthew G. Knepley     }
54078ca92349SMatthew G. Knepley     for (v = 0; v < Nv; ++v) {
54089566063dSJacob Faibussowitsch       PetscCall(PetscViewerRead(viewer, line, 3 + Nl, NULL, PETSC_STRING));
5409f8d5e320SMatthew G. Knepley       snum = sscanf(line, "%lg %lg %lg", &x[0], &x[1], &x[2]);
541008401ef6SPierre Jolivet       PetscCheck(snum == 3, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
5411f8d5e320SMatthew G. Knepley       switch (Nl) {
5412d71ae5a4SJacob Faibussowitsch       case 0:
5413d71ae5a4SJacob Faibussowitsch         snum = 0;
5414d71ae5a4SJacob Faibussowitsch         break;
5415d71ae5a4SJacob Faibussowitsch       case 1:
5416d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &val[0]);
5417d71ae5a4SJacob Faibussowitsch         break;
5418d71ae5a4SJacob Faibussowitsch       case 2:
5419d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &val[0], &val[1]);
5420d71ae5a4SJacob Faibussowitsch         break;
5421d71ae5a4SJacob Faibussowitsch       case 3:
5422d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &val[0], &val[1], &val[2]);
5423d71ae5a4SJacob Faibussowitsch         break;
5424d71ae5a4SJacob Faibussowitsch       default:
5425d71ae5a4SJacob Faibussowitsch         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Request support for %d labels", Nl);
5426f8d5e320SMatthew G. Knepley       }
542708401ef6SPierre Jolivet       PetscCheck(snum == Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
54288ca92349SMatthew G. Knepley       for (d = 0; d < cdim; ++d) coords[v * cdim + d] = x[d];
54299566063dSJacob Faibussowitsch       for (l = 0; l < Nl; ++l) PetscCall(DMLabelSetValue(marker, v + Nc, val[l]));
54308ca92349SMatthew G. Knepley     }
54318ca92349SMatthew G. Knepley   }
54329566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
54339566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(*dm, coordinates));
54349566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
54359566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(&viewer));
54368ca92349SMatthew G. Knepley   if (interpolate) {
54378ca92349SMatthew G. Knepley     DM      idm;
54388ca92349SMatthew G. Knepley     DMLabel bdlabel;
54398ca92349SMatthew G. Knepley 
54409566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
54419566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
54428ca92349SMatthew G. Knepley     *dm = idm;
54438ca92349SMatthew G. Knepley 
5444f8d5e320SMatthew G. Knepley     if (!Nl) {
54459566063dSJacob Faibussowitsch       PetscCall(DMCreateLabel(*dm, "marker"));
54469566063dSJacob Faibussowitsch       PetscCall(DMGetLabel(*dm, "marker", &bdlabel));
54479566063dSJacob Faibussowitsch       PetscCall(DMPlexMarkBoundaryFaces(*dm, PETSC_DETERMINE, bdlabel));
54489566063dSJacob Faibussowitsch       PetscCall(DMPlexLabelComplete(*dm, bdlabel));
54498ca92349SMatthew G. Knepley     }
5450f8d5e320SMatthew G. Knepley   }
54513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
54528ca92349SMatthew G. Knepley }
54538ca92349SMatthew G. Knepley 
54548ca92349SMatthew G. Knepley /*@C
5455a1cb98faSBarry Smith   DMPlexCreateFromFile - This takes a filename and produces a `DM`
5456a1cb98faSBarry Smith 
5457a1cb98faSBarry Smith   Collective
5458ca522641SMatthew G. Knepley 
5459ca522641SMatthew G. Knepley   Input Parameters:
5460ca522641SMatthew G. Knepley + comm        - The communicator
5461ca522641SMatthew G. Knepley . filename    - A file name
5462a1cb98faSBarry Smith . plexname    - The object name of the resulting `DM`, also used for intra-datafile lookup by some formats
5463ca522641SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
5464ca522641SMatthew G. Knepley 
5465ca522641SMatthew G. Knepley   Output Parameter:
5466a1cb98faSBarry Smith . dm - The `DM`
5467ca522641SMatthew G. Knepley 
5468a1cb98faSBarry Smith   Options Database Key:
5469a1cb98faSBarry Smith . -dm_plex_create_from_hdf5_xdmf - use the `PETSC_VIEWER_HDF5_XDMF` format for reading HDF5
547002ef0d99SVaclav Hapla 
547137fdd005SBarry Smith   Use `-dm_plex_create_ prefix` to pass options to the internal `PetscViewer`, e.g.
5472bca97951SVaclav Hapla $ -dm_plex_create_viewer_hdf5_collective
5473bca97951SVaclav Hapla 
5474ca522641SMatthew G. Knepley   Level: beginner
5475ca522641SMatthew G. Knepley 
5476a1cb98faSBarry Smith   Notes:
5477a1cb98faSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
5478a1cb98faSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
5479a1cb98faSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
5480a1cb98faSBarry Smith   The input parameter name is thus used to name the `DMPLEX` object when `DMPlexCreateFromFile()` internally
5481a1cb98faSBarry Smith   calls `DMLoad()`. Currently, name is ignored for other viewer types and/or formats.
5482a1cb98faSBarry Smith 
54831cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromDAG()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`, `PetscObjectSetName()`, `DMView()`, `DMLoad()`
5484ca522641SMatthew G. Knepley @*/
5485d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateFromFile(MPI_Comm comm, const char filename[], const char plexname[], PetscBool interpolate, DM *dm)
5486d71ae5a4SJacob Faibussowitsch {
5487ef3a5affSJacob Faibussowitsch   const char  extGmsh[]      = ".msh";
5488ef3a5affSJacob Faibussowitsch   const char  extGmsh2[]     = ".msh2";
5489ef3a5affSJacob Faibussowitsch   const char  extGmsh4[]     = ".msh4";
5490ef3a5affSJacob Faibussowitsch   const char  extCGNS[]      = ".cgns";
5491ef3a5affSJacob Faibussowitsch   const char  extExodus[]    = ".exo";
5492ef3a5affSJacob Faibussowitsch   const char  extExodus_e[]  = ".e";
5493ef3a5affSJacob Faibussowitsch   const char  extGenesis[]   = ".gen";
5494ef3a5affSJacob Faibussowitsch   const char  extFluent[]    = ".cas";
5495ef3a5affSJacob Faibussowitsch   const char  extHDF5[]      = ".h5";
54966f2c871aSStefano Zampini   const char  extXDMFHDF5[]  = ".xdmf.h5";
5497ef3a5affSJacob Faibussowitsch   const char  extMed[]       = ".med";
5498ef3a5affSJacob Faibussowitsch   const char  extPLY[]       = ".ply";
5499ef3a5affSJacob Faibussowitsch   const char  extEGADSLite[] = ".egadslite";
5500ef3a5affSJacob Faibussowitsch   const char  extEGADS[]     = ".egads";
5501ef3a5affSJacob Faibussowitsch   const char  extIGES[]      = ".igs";
5502ef3a5affSJacob Faibussowitsch   const char  extSTEP[]      = ".stp";
5503ef3a5affSJacob Faibussowitsch   const char  extCV[]        = ".dat";
5504ca522641SMatthew G. Knepley   size_t      len;
55056f2c871aSStefano Zampini   PetscBool   isGmsh, isGmsh2, isGmsh4, isCGNS, isExodus, isGenesis, isFluent, isHDF5, isMed, isPLY, isEGADSLite, isEGADS, isIGES, isSTEP, isCV, isXDMFHDF5;
5506ca522641SMatthew G. Knepley   PetscMPIInt rank;
5507ca522641SMatthew G. Knepley 
5508ca522641SMatthew G. Knepley   PetscFunctionBegin;
55094f572ea9SToby Isaac   PetscAssertPointer(filename, 2);
55104f572ea9SToby Isaac   if (plexname) PetscAssertPointer(plexname, 3);
55114f572ea9SToby Isaac   PetscAssertPointer(dm, 5);
55129566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
55139566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_CreateFromFile, 0, 0, 0, 0));
55149566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
55159566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(filename, &len));
551628b400f6SJacob Faibussowitsch   PetscCheck(len, comm, PETSC_ERR_ARG_WRONG, "Filename must be a valid path");
5517ef3a5affSJacob Faibussowitsch 
55189371c9d4SSatish Balay #define CheckExtension(extension__, is_extension__) \
55199371c9d4SSatish Balay   do { \
5520274aaeaaSJacob Faibussowitsch     PetscAssert(sizeof(extension__), comm, PETSC_ERR_PLIB, "Zero-size extension: %s", extension__); \
5521274aaeaaSJacob Faibussowitsch     /* don't count the null-terminator at the end */ \
5522274aaeaaSJacob Faibussowitsch     const size_t ext_len = sizeof(extension__) - 1; \
5523274aaeaaSJacob Faibussowitsch     if (len < ext_len) { \
5524ef3a5affSJacob Faibussowitsch       is_extension__ = PETSC_FALSE; \
5525ef3a5affSJacob Faibussowitsch     } else { \
5526274aaeaaSJacob Faibussowitsch       PetscCall(PetscStrncmp(filename + len - ext_len, extension__, ext_len, &is_extension__)); \
5527ef3a5affSJacob Faibussowitsch     } \
5528ef3a5affSJacob Faibussowitsch   } while (0)
5529ef3a5affSJacob Faibussowitsch 
5530ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh, isGmsh);
5531ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh2, isGmsh2);
5532ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh4, isGmsh4);
5533ef3a5affSJacob Faibussowitsch   CheckExtension(extCGNS, isCGNS);
5534ef3a5affSJacob Faibussowitsch   CheckExtension(extExodus, isExodus);
5535ef3a5affSJacob Faibussowitsch   if (!isExodus) CheckExtension(extExodus_e, isExodus);
5536ef3a5affSJacob Faibussowitsch   CheckExtension(extGenesis, isGenesis);
5537ef3a5affSJacob Faibussowitsch   CheckExtension(extFluent, isFluent);
5538ef3a5affSJacob Faibussowitsch   CheckExtension(extHDF5, isHDF5);
5539ef3a5affSJacob Faibussowitsch   CheckExtension(extMed, isMed);
5540ef3a5affSJacob Faibussowitsch   CheckExtension(extPLY, isPLY);
5541ef3a5affSJacob Faibussowitsch   CheckExtension(extEGADSLite, isEGADSLite);
5542ef3a5affSJacob Faibussowitsch   CheckExtension(extEGADS, isEGADS);
5543ef3a5affSJacob Faibussowitsch   CheckExtension(extIGES, isIGES);
5544ef3a5affSJacob Faibussowitsch   CheckExtension(extSTEP, isSTEP);
5545ef3a5affSJacob Faibussowitsch   CheckExtension(extCV, isCV);
55466f2c871aSStefano Zampini   CheckExtension(extXDMFHDF5, isXDMFHDF5);
5547ef3a5affSJacob Faibussowitsch 
5548ef3a5affSJacob Faibussowitsch #undef CheckExtension
5549ef3a5affSJacob Faibussowitsch 
5550de78e4feSLisandro Dalcin   if (isGmsh || isGmsh2 || isGmsh4) {
55519566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateGmshFromFile(comm, filename, interpolate, dm));
5552ca522641SMatthew G. Knepley   } else if (isCGNS) {
55539566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCGNSFromFile(comm, filename, interpolate, dm));
555490c68965SMatthew G. Knepley   } else if (isExodus || isGenesis) {
55559566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateExodusFromFile(comm, filename, interpolate, dm));
55562f0bd6dcSMichael Lange   } else if (isFluent) {
55579566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFluentFromFile(comm, filename, interpolate, dm));
5558cc2f8f65SMatthew G. Knepley   } else if (isHDF5) {
5559cc2f8f65SMatthew G. Knepley     PetscViewer viewer;
5560cc2f8f65SMatthew G. Knepley 
556143b242b4SVaclav 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 */
55626f2c871aSStefano Zampini     PetscCall(PetscOptionsGetBool(NULL, NULL, "-dm_plex_create_from_hdf5_xdmf", &isXDMFHDF5, NULL));
55639566063dSJacob Faibussowitsch     PetscCall(PetscViewerCreate(comm, &viewer));
55649566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetType(viewer, PETSCVIEWERHDF5));
55659566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetOptionsPrefix(viewer, "dm_plex_create_"));
55669566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetFromOptions(viewer));
55679566063dSJacob Faibussowitsch     PetscCall(PetscViewerFileSetMode(viewer, FILE_MODE_READ));
55689566063dSJacob Faibussowitsch     PetscCall(PetscViewerFileSetName(viewer, filename));
5569cd7e8a5eSksagiyam 
55709566063dSJacob Faibussowitsch     PetscCall(DMCreate(comm, dm));
55719566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)(*dm), plexname));
55729566063dSJacob Faibussowitsch     PetscCall(DMSetType(*dm, DMPLEX));
55736f2c871aSStefano Zampini     if (isXDMFHDF5) PetscCall(PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_XDMF));
55749566063dSJacob Faibussowitsch     PetscCall(DMLoad(*dm, viewer));
55756f2c871aSStefano Zampini     if (isXDMFHDF5) PetscCall(PetscViewerPopFormat(viewer));
55769566063dSJacob Faibussowitsch     PetscCall(PetscViewerDestroy(&viewer));
55775fd9971aSMatthew G. Knepley 
55785fd9971aSMatthew G. Knepley     if (interpolate) {
55795fd9971aSMatthew G. Knepley       DM idm;
55805fd9971aSMatthew G. Knepley 
55819566063dSJacob Faibussowitsch       PetscCall(DMPlexInterpolate(*dm, &idm));
55829566063dSJacob Faibussowitsch       PetscCall(DMDestroy(dm));
55835fd9971aSMatthew G. Knepley       *dm = idm;
55845fd9971aSMatthew G. Knepley     }
5585707dd687SMichael Lange   } else if (isMed) {
55869566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateMedFromFile(comm, filename, interpolate, dm));
5587f2801cd6SMatthew G. Knepley   } else if (isPLY) {
55889566063dSJacob Faibussowitsch     PetscCall(DMPlexCreatePLYFromFile(comm, filename, interpolate, dm));
5589c1cad2e7SMatthew G. Knepley   } else if (isEGADSLite || isEGADS || isIGES || isSTEP) {
55909566063dSJacob Faibussowitsch     if (isEGADSLite) PetscCall(DMPlexCreateEGADSLiteFromFile(comm, filename, dm));
55919566063dSJacob Faibussowitsch     else PetscCall(DMPlexCreateEGADSFromFile(comm, filename, dm));
55927bee2925SMatthew Knepley     if (!interpolate) {
55937bee2925SMatthew Knepley       DM udm;
55947bee2925SMatthew Knepley 
55959566063dSJacob Faibussowitsch       PetscCall(DMPlexUninterpolate(*dm, &udm));
55969566063dSJacob Faibussowitsch       PetscCall(DMDestroy(dm));
55977bee2925SMatthew Knepley       *dm = udm;
55987bee2925SMatthew Knepley     }
55998ca92349SMatthew G. Knepley   } else if (isCV) {
56009566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCellVertexFromFile(comm, filename, interpolate, dm));
560198921bdaSJacob Faibussowitsch   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot load file %s: unrecognized extension", filename);
56029566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(plexname, &len));
56039566063dSJacob Faibussowitsch   if (len) PetscCall(PetscObjectSetName((PetscObject)(*dm), plexname));
56049566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_CreateFromFile, 0, 0, 0, 0));
56053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5606ca522641SMatthew G. Knepley }
560712b8a6daSStefano Zampini 
56089f6c5813SMatthew G. Knepley /*@C
56099f6c5813SMatthew 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.
56109f6c5813SMatthew G. Knepley 
56110528010dSStefano Zampini   Input Parameters:
56120528010dSStefano Zampini + tr     - The `DMPlexTransform`
56130528010dSStefano Zampini - prefix - An options prefix, or NULL
56149f6c5813SMatthew G. Knepley 
56159f6c5813SMatthew G. Knepley   Output Parameter:
56169f6c5813SMatthew G. Knepley . dm - The `DM`
56179f6c5813SMatthew G. Knepley 
56189f6c5813SMatthew G. Knepley   Level: beginner
56199f6c5813SMatthew G. Knepley 
562020f4b53cSBarry Smith   Notes:
562120f4b53cSBarry 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.
562220f4b53cSBarry Smith 
56239f6c5813SMatthew G. Knepley .seealso: `DMPlexCreateFromFile`, `DMPlexCreateFromDAG()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`
56249f6c5813SMatthew G. Knepley @*/
56250528010dSStefano Zampini PetscErrorCode DMPlexCreateEphemeral(DMPlexTransform tr, const char prefix[], DM *dm)
56269f6c5813SMatthew G. Knepley {
56270528010dSStefano Zampini   DM           bdm, bcdm, cdm;
56280528010dSStefano Zampini   Vec          coordinates, coordinatesNew;
56290528010dSStefano Zampini   PetscSection cs;
56300528010dSStefano Zampini   PetscInt     dim, cdim, Nl;
56319f6c5813SMatthew G. Knepley 
56329f6c5813SMatthew G. Knepley   PetscFunctionBegin;
56339f6c5813SMatthew G. Knepley   PetscCall(DMCreate(PetscObjectComm((PetscObject)tr), dm));
56349f6c5813SMatthew G. Knepley   PetscCall(DMSetType(*dm, DMPLEX));
56350528010dSStefano Zampini   ((DM_Plex *)(*dm)->data)->interpolated = DMPLEX_INTERPOLATED_FULL;
56360528010dSStefano Zampini   // Handle coordinates
56370528010dSStefano Zampini   PetscCall(DMPlexTransformGetDM(tr, &bdm));
56380528010dSStefano Zampini   PetscCall(DMGetCoordinateDim(bdm, &cdim));
56390528010dSStefano Zampini   PetscCall(DMSetCoordinateDim(*dm, cdim));
56400528010dSStefano Zampini   PetscCall(DMGetDimension(bdm, &dim));
56410528010dSStefano Zampini   PetscCall(DMSetDimension(*dm, dim));
56420528010dSStefano Zampini   PetscCall(DMGetCoordinateDM(bdm, &bcdm));
56430528010dSStefano Zampini   PetscCall(DMGetCoordinateDM(*dm, &cdm));
56440528010dSStefano Zampini   PetscCall(DMCopyDisc(bcdm, cdm));
56450528010dSStefano Zampini   PetscCall(DMGetLocalSection(cdm, &cs));
56460528010dSStefano Zampini   PetscCall(PetscSectionSetNumFields(cs, 1));
56470528010dSStefano Zampini   PetscCall(PetscSectionSetFieldComponents(cs, 0, cdim));
56480528010dSStefano Zampini   PetscCall(DMGetCoordinatesLocal(bdm, &coordinates));
56490528010dSStefano Zampini   PetscCall(VecDuplicate(coordinates, &coordinatesNew));
56500528010dSStefano Zampini   PetscCall(VecCopy(coordinates, coordinatesNew));
56510528010dSStefano Zampini   PetscCall(DMSetCoordinatesLocal(*dm, coordinatesNew));
56520528010dSStefano Zampini   PetscCall(VecDestroy(&coordinatesNew));
56539f6c5813SMatthew G. Knepley 
56549f6c5813SMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)tr));
56559f6c5813SMatthew G. Knepley   PetscCall(DMPlexTransformDestroy(&((DM_Plex *)(*dm)->data)->tr));
56569f6c5813SMatthew G. Knepley   ((DM_Plex *)(*dm)->data)->tr = tr;
56570528010dSStefano Zampini   PetscCall(DMPlexDistributeSetDefault(*dm, PETSC_FALSE));
56580528010dSStefano Zampini   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)*dm, prefix));
56590528010dSStefano Zampini   PetscCall(DMSetFromOptions(*dm));
56609f6c5813SMatthew G. Knepley 
56619f6c5813SMatthew G. Knepley   PetscCall(DMGetNumLabels(bdm, &Nl));
56629f6c5813SMatthew G. Knepley   for (PetscInt l = 0; l < Nl; ++l) {
56639f6c5813SMatthew G. Knepley     DMLabel     label, labelNew;
56649f6c5813SMatthew G. Knepley     const char *lname;
56659f6c5813SMatthew G. Knepley     PetscBool   isDepth, isCellType;
56669f6c5813SMatthew G. Knepley 
56679f6c5813SMatthew G. Knepley     PetscCall(DMGetLabelName(bdm, l, &lname));
56689f6c5813SMatthew G. Knepley     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
56699f6c5813SMatthew G. Knepley     if (isDepth) continue;
56709f6c5813SMatthew G. Knepley     PetscCall(PetscStrcmp(lname, "celltype", &isCellType));
56719f6c5813SMatthew G. Knepley     if (isCellType) continue;
56729f6c5813SMatthew G. Knepley     PetscCall(DMCreateLabel(*dm, lname));
56739f6c5813SMatthew G. Knepley     PetscCall(DMGetLabel(bdm, lname, &label));
56749f6c5813SMatthew G. Knepley     PetscCall(DMGetLabel(*dm, lname, &labelNew));
56759f6c5813SMatthew G. Knepley     PetscCall(DMLabelSetType(labelNew, DMLABELEPHEMERAL));
56769f6c5813SMatthew G. Knepley     PetscCall(DMLabelEphemeralSetLabel(labelNew, label));
56779f6c5813SMatthew G. Knepley     PetscCall(DMLabelEphemeralSetTransform(labelNew, tr));
56789f6c5813SMatthew G. Knepley     PetscCall(DMLabelSetUp(labelNew));
56799f6c5813SMatthew G. Knepley   }
56803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
56819f6c5813SMatthew G. Knepley }
5682