xref: /petsc/src/dm/impls/plex/plexcreate.c (revision dd2b43eb7749f3575ad19a32eda4dcdf85755a4c)
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;
1912a88998SMatthew G. Knepley   VecType                  vecType;
2012a88998SMatthew G. Knepley   MatType                  matType;
215962854dSMatthew G. Knepley   PetscBool                dist, useCeed;
226bc1bd01Sksagiyam   DMPlexReorderDefaultFlag reorder;
23e600fa54SMatthew G. Knepley 
24e600fa54SMatthew G. Knepley   PetscFunctionBegin;
2512a88998SMatthew G. Knepley   PetscCall(DMGetVecType(dmin, &vecType));
2612a88998SMatthew G. Knepley   PetscCall(DMSetVecType(dmout, vecType));
2712a88998SMatthew G. Knepley   PetscCall(DMGetMatType(dmin, &matType));
2812a88998SMatthew G. Knepley   PetscCall(DMSetMatType(dmout, matType));
29e600fa54SMatthew G. Knepley   if (copyPeriodicity) {
304fb89dddSMatthew G. Knepley     PetscCall(DMGetPeriodicity(dmin, &maxCell, &Lstart, &L));
314fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dmout, maxCell, Lstart, L));
32e600fa54SMatthew G. Knepley   }
339566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeGetDefault(dmin, &dist));
349566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeSetDefault(dmout, dist));
356bc1bd01Sksagiyam   PetscCall(DMPlexReorderGetDefault(dmin, &reorder));
366bc1bd01Sksagiyam   PetscCall(DMPlexReorderSetDefault(dmout, reorder));
375962854dSMatthew G. Knepley   PetscCall(DMPlexGetUseCeed(dmin, &useCeed));
385962854dSMatthew G. Knepley   PetscCall(DMPlexSetUseCeed(dmout, useCeed));
39e600fa54SMatthew G. Knepley   ((DM_Plex *)dmout->data)->useHashLocation = ((DM_Plex *)dmin->data)->useHashLocation;
405962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printSetValues  = ((DM_Plex *)dmin->data)->printSetValues;
415962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printFEM        = ((DM_Plex *)dmin->data)->printFEM;
425962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printFVM        = ((DM_Plex *)dmin->data)->printFVM;
435962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printL2         = ((DM_Plex *)dmin->data)->printL2;
445962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printLocate     = ((DM_Plex *)dmin->data)->printLocate;
455962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printTol        = ((DM_Plex *)dmin->data)->printTol;
461baa6e33SBarry Smith   if (copyOverlap) PetscCall(DMPlexSetOverlap_Plex(dmout, dmin, 0));
473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48e600fa54SMatthew G. Knepley }
49e600fa54SMatthew G. Knepley 
509318fe57SMatthew G. Knepley /* Replace dm with the contents of ndm, and then destroy ndm
519318fe57SMatthew G. Knepley    - Share the DM_Plex structure
529318fe57SMatthew G. Knepley    - Share the coordinates
539318fe57SMatthew G. Knepley    - Share the SF
549318fe57SMatthew G. Knepley */
55d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexReplace_Internal(DM dm, DM *ndm)
56d71ae5a4SJacob Faibussowitsch {
579318fe57SMatthew G. Knepley   PetscSF          sf;
589318fe57SMatthew G. Knepley   DM               dmNew = *ndm, coordDM, coarseDM;
599318fe57SMatthew G. Knepley   Vec              coords;
604fb89dddSMatthew G. Knepley   const PetscReal *maxCell, *Lstart, *L;
619318fe57SMatthew G. Knepley   PetscInt         dim, cdim;
629318fe57SMatthew G. Knepley 
639318fe57SMatthew G. Knepley   PetscFunctionBegin;
649318fe57SMatthew G. Knepley   if (dm == dmNew) {
659566063dSJacob Faibussowitsch     PetscCall(DMDestroy(ndm));
663ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
679318fe57SMatthew G. Knepley   }
689318fe57SMatthew G. Knepley   dm->setupcalled = dmNew->setupcalled;
699566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dmNew, &dim));
709566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
719566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dmNew, &cdim));
729566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, cdim));
739566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmNew, &sf));
749566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dm, sf));
759566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmNew, &coordDM));
769566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmNew, &coords));
779566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dm, coordDM));
789566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coords));
796858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dmNew, &coordDM));
806858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dmNew, &coords));
816858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinateDM(dm, coordDM));
826858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinatesLocal(dm, coords));
839318fe57SMatthew G. Knepley   /* Do not want to create the coordinate field if it does not already exist, so do not call DMGetCoordinateField() */
846858538eSMatthew G. Knepley   PetscCall(DMFieldDestroy(&dm->coordinates[0].field));
856858538eSMatthew G. Knepley   dm->coordinates[0].field            = dmNew->coordinates[0].field;
8661a622f3SMatthew G. Knepley   ((DM_Plex *)dmNew->data)->coordFunc = ((DM_Plex *)dm->data)->coordFunc;
874fb89dddSMatthew G. Knepley   PetscCall(DMGetPeriodicity(dmNew, &maxCell, &Lstart, &L));
884fb89dddSMatthew G. Knepley   PetscCall(DMSetPeriodicity(dm, maxCell, Lstart, L));
896925d1c4SMatthew G. Knepley   PetscCall(DMPlexGetGlobalToNaturalSF(dmNew, &sf));
906925d1c4SMatthew G. Knepley   PetscCall(DMPlexSetGlobalToNaturalSF(dm, sf));
919566063dSJacob Faibussowitsch   PetscCall(DMDestroy_Plex(dm));
929566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(dm));
939318fe57SMatthew G. Knepley   dm->data = dmNew->data;
949318fe57SMatthew G. Knepley   ((DM_Plex *)dmNew->data)->refct++;
955f06a3ddSJed Brown   PetscCall(DMPlexGetIsoperiodicFaceSF(dm, &sf));
965f06a3ddSJed Brown   PetscCall(DMPlexSetIsoperiodicFaceSF(dm, sf)); // for the compose function effect on dm
979566063dSJacob Faibussowitsch   PetscCall(DMDestroyLabelLinkList_Internal(dm));
989566063dSJacob Faibussowitsch   PetscCall(DMCopyLabels(dmNew, dm, PETSC_OWN_POINTER, PETSC_TRUE, DM_COPY_LABELS_FAIL));
999566063dSJacob Faibussowitsch   PetscCall(DMGetCoarseDM(dmNew, &coarseDM));
1009566063dSJacob Faibussowitsch   PetscCall(DMSetCoarseDM(dm, coarseDM));
1019566063dSJacob Faibussowitsch   PetscCall(DMDestroy(ndm));
1023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1039318fe57SMatthew G. Knepley }
1049318fe57SMatthew G. Knepley 
1059318fe57SMatthew G. Knepley /* Swap dm with the contents of dmNew
1069318fe57SMatthew G. Knepley    - Swap the DM_Plex structure
1079318fe57SMatthew G. Knepley    - Swap the coordinates
1089318fe57SMatthew G. Knepley    - Swap the point PetscSF
1099318fe57SMatthew G. Knepley */
110d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexSwap_Static(DM dmA, DM dmB)
111d71ae5a4SJacob Faibussowitsch {
1129318fe57SMatthew G. Knepley   DM          coordDMA, coordDMB;
1139318fe57SMatthew G. Knepley   Vec         coordsA, coordsB;
1149318fe57SMatthew G. Knepley   PetscSF     sfA, sfB;
1159318fe57SMatthew G. Knepley   DMField     fieldTmp;
1169318fe57SMatthew G. Knepley   void       *tmp;
1179318fe57SMatthew G. Knepley   DMLabelLink listTmp;
1189318fe57SMatthew G. Knepley   DMLabel     depthTmp;
1199318fe57SMatthew G. Knepley   PetscInt    tmpI;
1209318fe57SMatthew G. Knepley 
1219318fe57SMatthew G. Knepley   PetscFunctionBegin;
1223ba16761SJacob Faibussowitsch   if (dmA == dmB) PetscFunctionReturn(PETSC_SUCCESS);
1239566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmA, &sfA));
1249566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmB, &sfB));
1259566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sfA));
1269566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dmA, sfB));
1279566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dmB, sfA));
1289566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)sfA));
1299318fe57SMatthew G. Knepley 
1309566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmA, &coordDMA));
1319566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmB, &coordDMB));
1329566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)coordDMA));
1339566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dmA, coordDMB));
1349566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dmB, coordDMA));
1359566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)coordDMA));
1369318fe57SMatthew G. Knepley 
1379566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmA, &coordsA));
1389566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmB, &coordsB));
1399566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)coordsA));
1409566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmA, coordsB));
1419566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmB, coordsA));
1429566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)coordsA));
1439318fe57SMatthew G. Knepley 
1446858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dmA, &coordDMA));
1456858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dmB, &coordDMB));
1466858538eSMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)coordDMA));
1476858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinateDM(dmA, coordDMB));
1486858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinateDM(dmB, coordDMA));
1496858538eSMatthew G. Knepley   PetscCall(PetscObjectDereference((PetscObject)coordDMA));
1506858538eSMatthew G. Knepley 
1516858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dmA, &coordsA));
1526858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dmB, &coordsB));
1536858538eSMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)coordsA));
1546858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinatesLocal(dmA, coordsB));
1556858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinatesLocal(dmB, coordsA));
1566858538eSMatthew G. Knepley   PetscCall(PetscObjectDereference((PetscObject)coordsA));
1576858538eSMatthew G. Knepley 
1586858538eSMatthew G. Knepley   fieldTmp                  = dmA->coordinates[0].field;
1596858538eSMatthew G. Knepley   dmA->coordinates[0].field = dmB->coordinates[0].field;
1606858538eSMatthew G. Knepley   dmB->coordinates[0].field = fieldTmp;
1616858538eSMatthew G. Knepley   fieldTmp                  = dmA->coordinates[1].field;
1626858538eSMatthew G. Knepley   dmA->coordinates[1].field = dmB->coordinates[1].field;
1636858538eSMatthew G. Knepley   dmB->coordinates[1].field = fieldTmp;
1649318fe57SMatthew G. Knepley   tmp                       = dmA->data;
1659318fe57SMatthew G. Knepley   dmA->data                 = dmB->data;
1669318fe57SMatthew G. Knepley   dmB->data                 = tmp;
1679318fe57SMatthew G. Knepley   listTmp                   = dmA->labels;
1689318fe57SMatthew G. Knepley   dmA->labels               = dmB->labels;
1699318fe57SMatthew G. Knepley   dmB->labels               = listTmp;
1709318fe57SMatthew G. Knepley   depthTmp                  = dmA->depthLabel;
1719318fe57SMatthew G. Knepley   dmA->depthLabel           = dmB->depthLabel;
1729318fe57SMatthew G. Knepley   dmB->depthLabel           = depthTmp;
1739318fe57SMatthew G. Knepley   depthTmp                  = dmA->celltypeLabel;
1749318fe57SMatthew G. Knepley   dmA->celltypeLabel        = dmB->celltypeLabel;
1759318fe57SMatthew G. Knepley   dmB->celltypeLabel        = depthTmp;
1769318fe57SMatthew G. Knepley   tmpI                      = dmA->levelup;
1779318fe57SMatthew G. Knepley   dmA->levelup              = dmB->levelup;
1789318fe57SMatthew G. Knepley   dmB->levelup              = tmpI;
1793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1809318fe57SMatthew G. Knepley }
1819318fe57SMatthew G. Knepley 
1823431e603SJed Brown PetscErrorCode DMPlexInterpolateInPlace_Internal(DM dm)
183d71ae5a4SJacob Faibussowitsch {
1849318fe57SMatthew G. Knepley   DM idm;
1859318fe57SMatthew G. Knepley 
1869318fe57SMatthew G. Knepley   PetscFunctionBegin;
1879566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolate(dm, &idm));
1889566063dSJacob Faibussowitsch   PetscCall(DMPlexCopyCoordinates(dm, idm));
18969d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &idm));
1903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1919318fe57SMatthew G. Knepley }
1929318fe57SMatthew G. Knepley 
1939318fe57SMatthew G. Knepley /*@C
1949318fe57SMatthew G. Knepley   DMPlexCreateCoordinateSpace - Creates a finite element space for the coordinates
1959318fe57SMatthew G. Knepley 
19620f4b53cSBarry Smith   Collective
1979318fe57SMatthew G. Knepley 
1989318fe57SMatthew G. Knepley   Input Parameters:
19960225df5SJacob Faibussowitsch + dm        - The `DMPLEX`
20020f4b53cSBarry Smith . degree    - The degree of the finite element or `PETSC_DECIDE`
201e44f6aebSMatthew G. Knepley . project   - Flag to project current coordinates into the space
2029318fe57SMatthew G. Knepley - coordFunc - An optional function to map new points from refinement to the surface
2039318fe57SMatthew G. Knepley 
2049318fe57SMatthew G. Knepley   Level: advanced
2059318fe57SMatthew G. Knepley 
2061cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `PetscPointFunc`, `PetscFECreateLagrange()`, `DMGetCoordinateDM()`
2079318fe57SMatthew G. Knepley @*/
208e44f6aebSMatthew G. Knepley PetscErrorCode DMPlexCreateCoordinateSpace(DM dm, PetscInt degree, PetscBool project, PetscPointFunc coordFunc)
209d71ae5a4SJacob Faibussowitsch {
2109318fe57SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
211e44f6aebSMatthew G. Knepley   PetscFE  fe   = NULL;
2129318fe57SMatthew G. Knepley   DM       cdm;
2131df12153SMatthew G. Knepley   PetscInt dim, dE, qorder, height;
2149318fe57SMatthew G. Knepley 
215e44f6aebSMatthew G. Knepley   PetscFunctionBegin;
2169566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
2179566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
2189318fe57SMatthew G. Knepley   qorder = degree;
219e44f6aebSMatthew G. Knepley   PetscCall(DMGetCoordinateDM(dm, &cdm));
220d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)cdm);
221dc431b0cSMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-default_quadrature_order", "Quadrature order is one less than quadrature points per edge", "DMPlexCreateCoordinateSpace", qorder, &qorder, NULL, 0));
222d0609cedSBarry Smith   PetscOptionsEnd();
2231df12153SMatthew G. Knepley   PetscCall(DMPlexGetVTKCellHeight(dm, &height));
224e44f6aebSMatthew G. Knepley   if (degree >= 0) {
225e44f6aebSMatthew G. Knepley     DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
226e44f6aebSMatthew G. Knepley     PetscInt       cStart, cEnd, gct;
227dc431b0cSMatthew G. Knepley 
2281df12153SMatthew G. Knepley     PetscCall(DMPlexGetHeightStratum(dm, height, &cStart, &cEnd));
229dc431b0cSMatthew G. Knepley     if (cEnd > cStart) PetscCall(DMPlexGetCellType(dm, cStart, &ct));
230e44f6aebSMatthew G. Knepley     gct = (PetscInt)ct;
231e44f6aebSMatthew G. Knepley     PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &gct, 1, MPIU_INT, MPI_MIN, PetscObjectComm((PetscObject)dm)));
232e44f6aebSMatthew G. Knepley     ct = (DMPolytopeType)gct;
233e44f6aebSMatthew G. Knepley     // Work around current bug in PetscDualSpaceSetUp_Lagrange()
234e44f6aebSMatthew G. Knepley     //   Can be seen in plex_tutorials-ex10_1
235e44f6aebSMatthew 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));
2364f9ab2b4SJed Brown   }
237e44f6aebSMatthew G. Knepley   PetscCall(DMSetCoordinateDisc(dm, fe, project));
2389566063dSJacob Faibussowitsch   PetscCall(PetscFEDestroy(&fe));
2399318fe57SMatthew G. Knepley   mesh->coordFunc = coordFunc;
2403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2419318fe57SMatthew G. Knepley }
2429318fe57SMatthew G. Knepley 
2431df5d5c5SMatthew G. Knepley /*@
2441df5d5c5SMatthew G. Knepley   DMPlexCreateDoublet - Creates a mesh of two cells of the specified type, optionally with later refinement.
2451df5d5c5SMatthew G. Knepley 
246d083f849SBarry Smith   Collective
2471df5d5c5SMatthew G. Knepley 
2481df5d5c5SMatthew G. Knepley   Input Parameters:
249a1cb98faSBarry Smith + comm            - The communicator for the `DM` object
2501df5d5c5SMatthew G. Knepley . dim             - The spatial dimension
2511df5d5c5SMatthew G. Knepley . simplex         - Flag for simplicial cells, otherwise they are tensor product cells
2521df5d5c5SMatthew G. Knepley . interpolate     - Flag to create intermediate mesh pieces (edges, faces)
2531df5d5c5SMatthew G. Knepley - refinementLimit - A nonzero number indicates the largest admissible volume for a refined cell
2541df5d5c5SMatthew G. Knepley 
2551df5d5c5SMatthew G. Knepley   Output Parameter:
25660225df5SJacob Faibussowitsch . newdm - The `DM` object
2571df5d5c5SMatthew G. Knepley 
2581df5d5c5SMatthew G. Knepley   Level: beginner
2591df5d5c5SMatthew G. Knepley 
2601cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMSetType()`, `DMCreate()`
2611df5d5c5SMatthew G. Knepley @*/
262d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateDoublet(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscBool interpolate, PetscReal refinementLimit, DM *newdm)
263d71ae5a4SJacob Faibussowitsch {
2641df5d5c5SMatthew G. Knepley   DM          dm;
2651df5d5c5SMatthew G. Knepley   PetscMPIInt rank;
2661df5d5c5SMatthew G. Knepley 
2671df5d5c5SMatthew G. Knepley   PetscFunctionBegin;
2689566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, &dm));
2699566063dSJacob Faibussowitsch   PetscCall(DMSetType(dm, DMPLEX));
2709566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
27146139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
2729566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
273ce78fa2fSMatthew G. Knepley   switch (dim) {
274ce78fa2fSMatthew G. Knepley   case 2:
2759566063dSJacob Faibussowitsch     if (simplex) PetscCall(PetscObjectSetName((PetscObject)dm, "triangular"));
2769566063dSJacob Faibussowitsch     else PetscCall(PetscObjectSetName((PetscObject)dm, "quadrilateral"));
277ce78fa2fSMatthew G. Knepley     break;
278ce78fa2fSMatthew G. Knepley   case 3:
2799566063dSJacob Faibussowitsch     if (simplex) PetscCall(PetscObjectSetName((PetscObject)dm, "tetrahedral"));
2809566063dSJacob Faibussowitsch     else PetscCall(PetscObjectSetName((PetscObject)dm, "hexahedral"));
281ce78fa2fSMatthew G. Knepley     break;
282d71ae5a4SJacob Faibussowitsch   default:
283d71ae5a4SJacob Faibussowitsch     SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %" PetscInt_FMT, dim);
284ce78fa2fSMatthew G. Knepley   }
2851df5d5c5SMatthew G. Knepley   if (rank) {
2861df5d5c5SMatthew G. Knepley     PetscInt numPoints[2] = {0, 0};
2879566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, NULL, NULL, NULL, NULL));
2881df5d5c5SMatthew G. Knepley   } else {
2891df5d5c5SMatthew G. Knepley     switch (dim) {
2901df5d5c5SMatthew G. Knepley     case 2:
2911df5d5c5SMatthew G. Knepley       if (simplex) {
2921df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {4, 2};
2931df5d5c5SMatthew G. Knepley         PetscInt    coneSize[6]         = {3, 3, 0, 0, 0, 0};
2941df5d5c5SMatthew G. Knepley         PetscInt    cones[6]            = {2, 3, 4, 5, 4, 3};
2951df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
2961df5d5c5SMatthew G. Knepley         PetscScalar vertexCoords[8]     = {-0.5, 0.5, 0.0, 0.0, 0.0, 1.0, 0.5, 0.5};
2971df5d5c5SMatthew G. Knepley 
2989566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
2991df5d5c5SMatthew G. Knepley       } else {
3001df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {6, 2};
3011df5d5c5SMatthew G. Knepley         PetscInt    coneSize[8]         = {4, 4, 0, 0, 0, 0, 0, 0};
3021df5d5c5SMatthew G. Knepley         PetscInt    cones[8]            = {2, 3, 4, 5, 3, 6, 7, 4};
3031df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
3041df5d5c5SMatthew 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};
3051df5d5c5SMatthew G. Knepley 
3069566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
3071df5d5c5SMatthew G. Knepley       }
3081df5d5c5SMatthew G. Knepley       break;
3091df5d5c5SMatthew G. Knepley     case 3:
3101df5d5c5SMatthew G. Knepley       if (simplex) {
3111df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {5, 2};
3121df5d5c5SMatthew G. Knepley         PetscInt    coneSize[7]         = {4, 4, 0, 0, 0, 0, 0};
3131df5d5c5SMatthew G. Knepley         PetscInt    cones[8]            = {4, 3, 5, 2, 5, 3, 4, 6};
3141df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
3151df5d5c5SMatthew 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};
3161df5d5c5SMatthew G. Knepley 
3179566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
3181df5d5c5SMatthew G. Knepley       } else {
3191df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]         = {12, 2};
3201df5d5c5SMatthew G. Knepley         PetscInt    coneSize[14]         = {8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
3211df5d5c5SMatthew G. Knepley         PetscInt    cones[16]            = {2, 3, 4, 5, 6, 7, 8, 9, 5, 4, 10, 11, 7, 12, 13, 8};
3221df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
3239371c9d4SSatish 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};
3241df5d5c5SMatthew G. Knepley 
3259566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
3261df5d5c5SMatthew G. Knepley       }
3271df5d5c5SMatthew G. Knepley       break;
328d71ae5a4SJacob Faibussowitsch     default:
329d71ae5a4SJacob Faibussowitsch       SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %" PetscInt_FMT, dim);
3301df5d5c5SMatthew G. Knepley     }
3311df5d5c5SMatthew G. Knepley   }
33246139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
3331df5d5c5SMatthew G. Knepley   *newdm = dm;
3341df5d5c5SMatthew G. Knepley   if (refinementLimit > 0.0) {
3351df5d5c5SMatthew G. Knepley     DM          rdm;
3361df5d5c5SMatthew G. Knepley     const char *name;
3371df5d5c5SMatthew G. Knepley 
3389566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementUniform(*newdm, PETSC_FALSE));
3399566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementLimit(*newdm, refinementLimit));
3409566063dSJacob Faibussowitsch     PetscCall(DMRefine(*newdm, comm, &rdm));
3419566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)*newdm, &name));
3429566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)rdm, name));
3439566063dSJacob Faibussowitsch     PetscCall(DMDestroy(newdm));
3441df5d5c5SMatthew G. Knepley     *newdm = rdm;
3451df5d5c5SMatthew G. Knepley   }
3461df5d5c5SMatthew G. Knepley   if (interpolate) {
3475fd9971aSMatthew G. Knepley     DM idm;
3481df5d5c5SMatthew G. Knepley 
3499566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*newdm, &idm));
3509566063dSJacob Faibussowitsch     PetscCall(DMDestroy(newdm));
3511df5d5c5SMatthew G. Knepley     *newdm = idm;
3521df5d5c5SMatthew G. Knepley   }
3533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3541df5d5c5SMatthew G. Knepley }
3551df5d5c5SMatthew G. Knepley 
356d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
357d71ae5a4SJacob Faibussowitsch {
3589318fe57SMatthew G. Knepley   const PetscInt numVertices    = 2;
3599318fe57SMatthew G. Knepley   PetscInt       markerRight    = 1;
3609318fe57SMatthew G. Knepley   PetscInt       markerLeft     = 1;
3619318fe57SMatthew G. Knepley   PetscBool      markerSeparate = PETSC_FALSE;
3629318fe57SMatthew G. Knepley   Vec            coordinates;
3639318fe57SMatthew G. Knepley   PetscSection   coordSection;
3649318fe57SMatthew G. Knepley   PetscScalar   *coords;
3659318fe57SMatthew G. Knepley   PetscInt       coordSize;
3669318fe57SMatthew G. Knepley   PetscMPIInt    rank;
3679318fe57SMatthew G. Knepley   PetscInt       cdim = 1, v;
368552f7358SJed Brown 
3699318fe57SMatthew G. Knepley   PetscFunctionBegin;
3709566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
3719318fe57SMatthew G. Knepley   if (markerSeparate) {
3729318fe57SMatthew G. Knepley     markerRight = 2;
3739318fe57SMatthew G. Knepley     markerLeft  = 1;
3749318fe57SMatthew G. Knepley   }
3759566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
376c5853193SPierre Jolivet   if (rank == 0) {
3779566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numVertices));
3789566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
3799566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", 0, markerLeft));
3809566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", 1, markerRight));
3819318fe57SMatthew G. Knepley   }
3829566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
3839566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
3849318fe57SMatthew G. Knepley   /* Build coordinates */
3859566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, cdim));
3869566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
3879566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
3889566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, 0, numVertices));
3899566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, cdim));
3909318fe57SMatthew G. Knepley   for (v = 0; v < numVertices; ++v) {
3919566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, cdim));
3929566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, cdim));
3939318fe57SMatthew G. Knepley   }
3949566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
3959566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
3969566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
3979566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
3989566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
3999566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, cdim));
4009566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
4019566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
4029318fe57SMatthew G. Knepley   coords[0] = lower[0];
4039318fe57SMatthew G. Knepley   coords[1] = upper[0];
4049566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
4059566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
4069566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
4073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4089318fe57SMatthew G. Knepley }
40926492d91SMatthew G. Knepley 
410d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
411d71ae5a4SJacob Faibussowitsch {
4121df21d24SMatthew G. Knepley   const PetscInt numVertices    = (edges[0] + 1) * (edges[1] + 1);
4131df21d24SMatthew G. Knepley   const PetscInt numEdges       = edges[0] * (edges[1] + 1) + (edges[0] + 1) * edges[1];
414552f7358SJed Brown   PetscInt       markerTop      = 1;
415552f7358SJed Brown   PetscInt       markerBottom   = 1;
416552f7358SJed Brown   PetscInt       markerRight    = 1;
417552f7358SJed Brown   PetscInt       markerLeft     = 1;
418552f7358SJed Brown   PetscBool      markerSeparate = PETSC_FALSE;
419552f7358SJed Brown   Vec            coordinates;
420552f7358SJed Brown   PetscSection   coordSection;
421552f7358SJed Brown   PetscScalar   *coords;
422552f7358SJed Brown   PetscInt       coordSize;
423552f7358SJed Brown   PetscMPIInt    rank;
424552f7358SJed Brown   PetscInt       v, vx, vy;
425552f7358SJed Brown 
426552f7358SJed Brown   PetscFunctionBegin;
4279566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
428552f7358SJed Brown   if (markerSeparate) {
4291df21d24SMatthew G. Knepley     markerTop    = 3;
4301df21d24SMatthew G. Knepley     markerBottom = 1;
4311df21d24SMatthew G. Knepley     markerRight  = 2;
4321df21d24SMatthew G. Knepley     markerLeft   = 4;
433552f7358SJed Brown   }
4349566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
435dd400576SPatrick Sanan   if (rank == 0) {
436552f7358SJed Brown     PetscInt e, ex, ey;
437552f7358SJed Brown 
4389566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numEdges + numVertices));
43948a46eb9SPierre Jolivet     for (e = 0; e < numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2));
4409566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
441552f7358SJed Brown     for (vx = 0; vx <= edges[0]; vx++) {
442552f7358SJed Brown       for (ey = 0; ey < edges[1]; ey++) {
443552f7358SJed Brown         PetscInt edge   = vx * edges[1] + ey + edges[0] * (edges[1] + 1);
444552f7358SJed Brown         PetscInt vertex = ey * (edges[0] + 1) + vx + numEdges;
445da80777bSKarl Rupp         PetscInt cone[2];
446552f7358SJed Brown 
4479371c9d4SSatish Balay         cone[0] = vertex;
4489371c9d4SSatish Balay         cone[1] = vertex + edges[0] + 1;
4499566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, edge, cone));
450552f7358SJed Brown         if (vx == edges[0]) {
4519566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
4529566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
453552f7358SJed Brown           if (ey == edges[1] - 1) {
4549566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
4559566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerRight));
456552f7358SJed Brown           }
457552f7358SJed Brown         } else if (vx == 0) {
4589566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
4599566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
460552f7358SJed Brown           if (ey == edges[1] - 1) {
4619566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
4629566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerLeft));
463552f7358SJed Brown           }
464552f7358SJed Brown         }
465552f7358SJed Brown       }
466552f7358SJed Brown     }
467552f7358SJed Brown     for (vy = 0; vy <= edges[1]; vy++) {
468552f7358SJed Brown       for (ex = 0; ex < edges[0]; ex++) {
469552f7358SJed Brown         PetscInt edge   = vy * edges[0] + ex;
470552f7358SJed Brown         PetscInt vertex = vy * (edges[0] + 1) + ex + numEdges;
471da80777bSKarl Rupp         PetscInt cone[2];
472552f7358SJed Brown 
4739371c9d4SSatish Balay         cone[0] = vertex;
4749371c9d4SSatish Balay         cone[1] = vertex + 1;
4759566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, edge, cone));
476552f7358SJed Brown         if (vy == edges[1]) {
4779566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
4789566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
479552f7358SJed Brown           if (ex == edges[0] - 1) {
4809566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
4819566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerTop));
482552f7358SJed Brown           }
483552f7358SJed Brown         } else if (vy == 0) {
4849566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
4859566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
486552f7358SJed Brown           if (ex == edges[0] - 1) {
4879566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
4889566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerBottom));
489552f7358SJed Brown           }
490552f7358SJed Brown         }
491552f7358SJed Brown       }
492552f7358SJed Brown     }
493552f7358SJed Brown   }
4949566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
4959566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
496552f7358SJed Brown   /* Build coordinates */
4979566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, 2));
4989566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
4999566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
5009566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, numEdges, numEdges + numVertices));
5019566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, 2));
502552f7358SJed Brown   for (v = numEdges; v < numEdges + numVertices; ++v) {
5039566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, 2));
5049566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, 2));
505552f7358SJed Brown   }
5069566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
5079566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
5089566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
5099566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
5109566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
5119566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, 2));
5129566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
5139566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
514552f7358SJed Brown   for (vy = 0; vy <= edges[1]; ++vy) {
515552f7358SJed Brown     for (vx = 0; vx <= edges[0]; ++vx) {
516552f7358SJed Brown       coords[(vy * (edges[0] + 1) + vx) * 2 + 0] = lower[0] + ((upper[0] - lower[0]) / edges[0]) * vx;
517552f7358SJed Brown       coords[(vy * (edges[0] + 1) + vx) * 2 + 1] = lower[1] + ((upper[1] - lower[1]) / edges[1]) * vy;
518552f7358SJed Brown     }
519552f7358SJed Brown   }
5209566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
5219566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
5229566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
5233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
524552f7358SJed Brown }
525552f7358SJed Brown 
526d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt faces[])
527d71ae5a4SJacob Faibussowitsch {
5289e8abbc3SMichael Lange   PetscInt     vertices[3], numVertices;
5297b59f5a9SMichael Lange   PetscInt     numFaces       = 2 * faces[0] * faces[1] + 2 * faces[1] * faces[2] + 2 * faces[0] * faces[2];
530c2df9bbfSMatthew G. Knepley   PetscInt     markerTop      = 1;
531c2df9bbfSMatthew G. Knepley   PetscInt     markerBottom   = 1;
532c2df9bbfSMatthew G. Knepley   PetscInt     markerFront    = 1;
533c2df9bbfSMatthew G. Knepley   PetscInt     markerBack     = 1;
534c2df9bbfSMatthew G. Knepley   PetscInt     markerRight    = 1;
535c2df9bbfSMatthew G. Knepley   PetscInt     markerLeft     = 1;
536c2df9bbfSMatthew G. Knepley   PetscBool    markerSeparate = PETSC_FALSE;
537552f7358SJed Brown   Vec          coordinates;
538552f7358SJed Brown   PetscSection coordSection;
539552f7358SJed Brown   PetscScalar *coords;
540552f7358SJed Brown   PetscInt     coordSize;
541552f7358SJed Brown   PetscMPIInt  rank;
542552f7358SJed Brown   PetscInt     v, vx, vy, vz;
5437b59f5a9SMichael Lange   PetscInt     voffset, iface = 0, cone[4];
544552f7358SJed Brown 
545552f7358SJed Brown   PetscFunctionBegin;
5461dca8a05SBarry 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");
5479566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
548c2df9bbfSMatthew G. Knepley   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
549c2df9bbfSMatthew G. Knepley   if (markerSeparate) {
550c2df9bbfSMatthew G. Knepley     markerBottom = 1;
551c2df9bbfSMatthew G. Knepley     markerTop    = 2;
552c2df9bbfSMatthew G. Knepley     markerFront  = 3;
553c2df9bbfSMatthew G. Knepley     markerBack   = 4;
554c2df9bbfSMatthew G. Knepley     markerRight  = 5;
555c2df9bbfSMatthew G. Knepley     markerLeft   = 6;
556c2df9bbfSMatthew G. Knepley   }
5579371c9d4SSatish Balay   vertices[0] = faces[0] + 1;
5589371c9d4SSatish Balay   vertices[1] = faces[1] + 1;
5599371c9d4SSatish Balay   vertices[2] = faces[2] + 1;
5609e8abbc3SMichael Lange   numVertices = vertices[0] * vertices[1] * vertices[2];
561dd400576SPatrick Sanan   if (rank == 0) {
562552f7358SJed Brown     PetscInt f;
563552f7358SJed Brown 
5649566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numFaces + numVertices));
56548a46eb9SPierre Jolivet     for (f = 0; f < numFaces; ++f) PetscCall(DMPlexSetConeSize(dm, f, 4));
5669566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
5677b59f5a9SMichael Lange 
5687b59f5a9SMichael Lange     /* Side 0 (Top) */
5697b59f5a9SMichael Lange     for (vy = 0; vy < faces[1]; vy++) {
5707b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5717b59f5a9SMichael Lange         voffset = numFaces + vertices[0] * vertices[1] * (vertices[2] - 1) + vy * vertices[0] + vx;
5729371c9d4SSatish Balay         cone[0] = voffset;
5739371c9d4SSatish Balay         cone[1] = voffset + 1;
5749371c9d4SSatish Balay         cone[2] = voffset + vertices[0] + 1;
5759371c9d4SSatish Balay         cone[3] = voffset + vertices[0];
5769566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
577c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerTop));
578c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerTop));
579c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerTop));
580c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerTop));
581c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 1, markerTop));
5827b59f5a9SMichael Lange         iface++;
583552f7358SJed Brown       }
584552f7358SJed Brown     }
5857b59f5a9SMichael Lange 
5867b59f5a9SMichael Lange     /* Side 1 (Bottom) */
5877b59f5a9SMichael Lange     for (vy = 0; vy < faces[1]; vy++) {
5887b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5897b59f5a9SMichael Lange         voffset = numFaces + vy * (faces[0] + 1) + vx;
5909371c9d4SSatish Balay         cone[0] = voffset + 1;
5919371c9d4SSatish Balay         cone[1] = voffset;
5929371c9d4SSatish Balay         cone[2] = voffset + vertices[0];
5939371c9d4SSatish Balay         cone[3] = voffset + vertices[0] + 1;
5949566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
595c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerBottom));
596c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerBottom));
597c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerBottom));
598c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerBottom));
599c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 1, markerBottom));
6007b59f5a9SMichael Lange         iface++;
601552f7358SJed Brown       }
602552f7358SJed Brown     }
6037b59f5a9SMichael Lange 
6047b59f5a9SMichael Lange     /* Side 2 (Front) */
6057b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
6067b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
6077b59f5a9SMichael Lange         voffset = numFaces + vz * vertices[0] * vertices[1] + vx;
6089371c9d4SSatish Balay         cone[0] = voffset;
6099371c9d4SSatish Balay         cone[1] = voffset + 1;
6109371c9d4SSatish Balay         cone[2] = voffset + vertices[0] * vertices[1] + 1;
6119371c9d4SSatish Balay         cone[3] = voffset + vertices[0] * vertices[1];
6129566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
613c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerFront));
614c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerFront));
615c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerFront));
616c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 0, markerFront));
617c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 1, markerFront));
6187b59f5a9SMichael Lange         iface++;
619552f7358SJed Brown       }
6207b59f5a9SMichael Lange     }
6217b59f5a9SMichael Lange 
6227b59f5a9SMichael Lange     /* Side 3 (Back) */
6237b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
6247b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
6257b59f5a9SMichael Lange         voffset = numFaces + vz * vertices[0] * vertices[1] + vertices[0] * (vertices[1] - 1) + vx;
6269371c9d4SSatish Balay         cone[0] = voffset + vertices[0] * vertices[1];
6279371c9d4SSatish Balay         cone[1] = voffset + vertices[0] * vertices[1] + 1;
6289371c9d4SSatish Balay         cone[2] = voffset + 1;
6299371c9d4SSatish Balay         cone[3] = voffset;
6309566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
631c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerBack));
632c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerBack));
633c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerBack));
634c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 0, markerBack));
635c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 1, markerBack));
6367b59f5a9SMichael Lange         iface++;
6377b59f5a9SMichael Lange       }
6387b59f5a9SMichael Lange     }
6397b59f5a9SMichael Lange 
6407b59f5a9SMichael Lange     /* Side 4 (Left) */
6417b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
6427b59f5a9SMichael Lange       for (vy = 0; vy < faces[1]; vy++) {
6437b59f5a9SMichael Lange         voffset = numFaces + vz * vertices[0] * vertices[1] + vy * vertices[0];
6449371c9d4SSatish Balay         cone[0] = voffset;
6459371c9d4SSatish Balay         cone[1] = voffset + vertices[0] * vertices[1];
6469371c9d4SSatish Balay         cone[2] = voffset + vertices[0] * vertices[1] + vertices[0];
6479371c9d4SSatish Balay         cone[3] = voffset + vertices[0];
6489566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
649c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerLeft));
650c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerLeft));
651c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerLeft));
652c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[1] + 0, markerLeft));
653c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + vertices[0], markerLeft));
6547b59f5a9SMichael Lange         iface++;
6557b59f5a9SMichael Lange       }
6567b59f5a9SMichael Lange     }
6577b59f5a9SMichael Lange 
6587b59f5a9SMichael Lange     /* Side 5 (Right) */
6597b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
6607b59f5a9SMichael Lange       for (vy = 0; vy < faces[1]; vy++) {
661aab5bcd8SJed Brown         voffset = numFaces + vz * vertices[0] * vertices[1] + vy * vertices[0] + faces[0];
6629371c9d4SSatish Balay         cone[0] = voffset + vertices[0] * vertices[1];
6639371c9d4SSatish Balay         cone[1] = voffset;
6649371c9d4SSatish Balay         cone[2] = voffset + vertices[0];
6659371c9d4SSatish Balay         cone[3] = voffset + vertices[0] * vertices[1] + vertices[0];
6669566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
667c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerRight));
668c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerRight));
669c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerRight));
670c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 0, markerRight));
671c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + vertices[0], markerRight));
6727b59f5a9SMichael Lange         iface++;
6737b59f5a9SMichael Lange       }
674552f7358SJed Brown     }
675552f7358SJed Brown   }
6769566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
6779566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
678552f7358SJed Brown   /* Build coordinates */
6799566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, 3));
6809566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
6819566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
6829566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, numFaces, numFaces + numVertices));
6839566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, 3));
684552f7358SJed Brown   for (v = numFaces; v < numFaces + numVertices; ++v) {
6859566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, 3));
6869566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, 3));
687552f7358SJed Brown   }
6889566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
6899566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
6909566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
6919566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
6929566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
6939566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, 3));
6949566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
6959566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
696552f7358SJed Brown   for (vz = 0; vz <= faces[2]; ++vz) {
697552f7358SJed Brown     for (vy = 0; vy <= faces[1]; ++vy) {
698552f7358SJed Brown       for (vx = 0; vx <= faces[0]; ++vx) {
699552f7358SJed Brown         coords[((vz * (faces[1] + 1) + vy) * (faces[0] + 1) + vx) * 3 + 0] = lower[0] + ((upper[0] - lower[0]) / faces[0]) * vx;
700552f7358SJed Brown         coords[((vz * (faces[1] + 1) + vy) * (faces[0] + 1) + vx) * 3 + 1] = lower[1] + ((upper[1] - lower[1]) / faces[1]) * vy;
701552f7358SJed Brown         coords[((vz * (faces[1] + 1) + vy) * (faces[0] + 1) + vx) * 3 + 2] = lower[2] + ((upper[2] - lower[2]) / faces[2]) * vz;
702552f7358SJed Brown       }
703552f7358SJed Brown     }
704552f7358SJed Brown   }
7059566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
7069566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
7079566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
7083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
709552f7358SJed Brown }
710552f7358SJed Brown 
711d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate)
712d71ae5a4SJacob Faibussowitsch {
7139318fe57SMatthew G. Knepley   PetscFunctionBegin;
7149318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
71546139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
7169566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim - 1));
7179566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, dim));
7189318fe57SMatthew G. Knepley   switch (dim) {
719d71ae5a4SJacob Faibussowitsch   case 1:
720d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(dm, lower, upper, faces));
721d71ae5a4SJacob Faibussowitsch     break;
722d71ae5a4SJacob Faibussowitsch   case 2:
723d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(dm, lower, upper, faces));
724d71ae5a4SJacob Faibussowitsch     break;
725d71ae5a4SJacob Faibussowitsch   case 3:
726d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(dm, lower, upper, faces));
727d71ae5a4SJacob Faibussowitsch     break;
728d71ae5a4SJacob Faibussowitsch   default:
729d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Dimension not supported: %" PetscInt_FMT, dim);
7309318fe57SMatthew G. Knepley   }
73146139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
7329566063dSJacob Faibussowitsch   if (interpolate) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
7333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7349318fe57SMatthew G. Knepley }
7359318fe57SMatthew G. Knepley 
7369318fe57SMatthew G. Knepley /*@C
7379318fe57SMatthew G. Knepley   DMPlexCreateBoxSurfaceMesh - Creates a mesh on the surface of the tensor product of unit intervals (box) using tensor cells (hexahedra).
7389318fe57SMatthew G. Knepley 
7399318fe57SMatthew G. Knepley   Collective
7409318fe57SMatthew G. Knepley 
7419318fe57SMatthew G. Knepley   Input Parameters:
742a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
74320f4b53cSBarry Smith . dim         - The spatial dimension of the box, so the resulting mesh is has dimension `dim`-1
74420f4b53cSBarry Smith . faces       - Number of faces per dimension, or `NULL` for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
74520f4b53cSBarry Smith . lower       - The lower left corner, or `NULL` for (0, 0, 0)
74620f4b53cSBarry Smith . upper       - The upper right corner, or `NULL` for (1, 1, 1)
7479318fe57SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
7489318fe57SMatthew G. Knepley 
7499318fe57SMatthew G. Knepley   Output Parameter:
750a1cb98faSBarry Smith . dm - The `DM` object
7519318fe57SMatthew G. Knepley 
7529318fe57SMatthew G. Knepley   Level: beginner
7539318fe57SMatthew G. Knepley 
7541cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMSetFromOptions()`, `DMPlexCreateBoxMesh()`, `DMPlexCreateFromFile()`, `DMSetType()`, `DMCreate()`
7559318fe57SMatthew G. Knepley @*/
756d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateBoxSurfaceMesh(MPI_Comm comm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate, DM *dm)
757d71ae5a4SJacob Faibussowitsch {
7589318fe57SMatthew G. Knepley   PetscInt  fac[3] = {1, 1, 1};
7599318fe57SMatthew G. Knepley   PetscReal low[3] = {0, 0, 0};
7609318fe57SMatthew G. Knepley   PetscReal upp[3] = {1, 1, 1};
7619318fe57SMatthew G. Knepley 
7629318fe57SMatthew G. Knepley   PetscFunctionBegin;
7639566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
7649566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
7659566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(*dm, dim, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, interpolate));
7663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7679318fe57SMatthew G. Knepley }
7689318fe57SMatthew G. Knepley 
769d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateLineMesh_Internal(DM dm, PetscInt segments, PetscReal lower, PetscReal upper, DMBoundaryType bd)
770d71ae5a4SJacob Faibussowitsch {
771fdbf62faSLisandro Dalcin   PetscInt     i, fStart, fEnd, numCells = 0, numVerts = 0;
772fdbf62faSLisandro Dalcin   PetscInt     numPoints[2], *coneSize, *cones, *coneOrientations;
773fdbf62faSLisandro Dalcin   PetscScalar *vertexCoords;
774fdbf62faSLisandro Dalcin   PetscReal    L, maxCell;
775fdbf62faSLisandro Dalcin   PetscBool    markerSeparate = PETSC_FALSE;
776fdbf62faSLisandro Dalcin   PetscInt     markerLeft = 1, faceMarkerLeft = 1;
777fdbf62faSLisandro Dalcin   PetscInt     markerRight = 1, faceMarkerRight = 2;
778fdbf62faSLisandro Dalcin   PetscBool    wrap = (bd == DM_BOUNDARY_PERIODIC || bd == DM_BOUNDARY_TWIST) ? PETSC_TRUE : PETSC_FALSE;
779fdbf62faSLisandro Dalcin   PetscMPIInt  rank;
780fdbf62faSLisandro Dalcin 
781fdbf62faSLisandro Dalcin   PetscFunctionBegin;
7824f572ea9SToby Isaac   PetscAssertPointer(dm, 1);
783fdbf62faSLisandro Dalcin 
7849566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, 1));
7859566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
7869566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
787fdbf62faSLisandro Dalcin 
7889566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
789dd400576SPatrick Sanan   if (rank == 0) numCells = segments;
790dd400576SPatrick Sanan   if (rank == 0) numVerts = segments + (wrap ? 0 : 1);
791fdbf62faSLisandro Dalcin 
7929371c9d4SSatish Balay   numPoints[0] = numVerts;
7939371c9d4SSatish Balay   numPoints[1] = numCells;
7949566063dSJacob Faibussowitsch   PetscCall(PetscMalloc4(numCells + numVerts, &coneSize, numCells * 2, &cones, numCells + numVerts, &coneOrientations, numVerts, &vertexCoords));
7959566063dSJacob Faibussowitsch   PetscCall(PetscArrayzero(coneOrientations, numCells + numVerts));
796ad540459SPierre Jolivet   for (i = 0; i < numCells; ++i) coneSize[i] = 2;
797ad540459SPierre Jolivet   for (i = 0; i < numVerts; ++i) coneSize[numCells + i] = 0;
7989371c9d4SSatish Balay   for (i = 0; i < numCells; ++i) {
7999371c9d4SSatish Balay     cones[2 * i]     = numCells + i % numVerts;
8009371c9d4SSatish Balay     cones[2 * i + 1] = numCells + (i + 1) % numVerts;
8019371c9d4SSatish Balay   }
802ad540459SPierre Jolivet   for (i = 0; i < numVerts; ++i) vertexCoords[i] = lower + (upper - lower) * ((PetscReal)i / (PetscReal)numCells);
8039566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
8049566063dSJacob Faibussowitsch   PetscCall(PetscFree4(coneSize, cones, coneOrientations, vertexCoords));
805fdbf62faSLisandro Dalcin 
8069566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
8079371c9d4SSatish Balay   if (markerSeparate) {
8089371c9d4SSatish Balay     markerLeft  = faceMarkerLeft;
8099371c9d4SSatish Balay     markerRight = faceMarkerRight;
8109371c9d4SSatish Balay   }
811dd400576SPatrick Sanan   if (!wrap && rank == 0) {
8129566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
8139566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", fStart, markerLeft));
8149566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", fEnd - 1, markerRight));
8159566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "Face Sets", fStart, faceMarkerLeft));
8169566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "Face Sets", fEnd - 1, faceMarkerRight));
817fdbf62faSLisandro Dalcin   }
818fdbf62faSLisandro Dalcin   if (wrap) {
819fdbf62faSLisandro Dalcin     L       = upper - lower;
820fdbf62faSLisandro Dalcin     maxCell = (PetscReal)1.1 * (L / (PetscReal)PetscMax(1, segments));
8214fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, &maxCell, &lower, &L));
822fdbf62faSLisandro Dalcin   }
8239566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
8243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
825fdbf62faSLisandro Dalcin }
826fdbf62faSLisandro Dalcin 
827d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxMesh_Simplex_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate)
828d71ae5a4SJacob Faibussowitsch {
8299318fe57SMatthew G. Knepley   DM      boundary, vol;
830c22d3578SMatthew G. Knepley   DMLabel bdlabel;
831d6218766SMatthew G. Knepley 
832d6218766SMatthew G. Knepley   PetscFunctionBegin;
8334f572ea9SToby Isaac   PetscAssertPointer(dm, 1);
834c22d3578SMatthew 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");
8359566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &boundary));
8369566063dSJacob Faibussowitsch   PetscCall(DMSetType(boundary, DMPLEX));
8379566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(boundary, dim, faces, lower, upper, PETSC_FALSE));
8389566063dSJacob Faibussowitsch   PetscCall(DMPlexGenerate(boundary, NULL, interpolate, &vol));
839c22d3578SMatthew G. Knepley   PetscCall(DMGetLabel(vol, "marker", &bdlabel));
840c22d3578SMatthew G. Knepley   if (bdlabel) PetscCall(DMPlexLabelComplete(vol, bdlabel));
8415de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_FALSE, vol));
84269d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &vol));
8439566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&boundary));
8443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
845d6218766SMatthew G. Knepley }
846d6218766SMatthew G. Knepley 
847d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCubeMesh_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], DMBoundaryType bdX, DMBoundaryType bdY, DMBoundaryType bdZ)
848d71ae5a4SJacob Faibussowitsch {
849ed0e4b50SMatthew G. Knepley   DMLabel     cutLabel  = NULL;
850f4eb4c5dSMatthew G. Knepley   PetscInt    markerTop = 1, faceMarkerTop = 1;
851f4eb4c5dSMatthew G. Knepley   PetscInt    markerBottom = 1, faceMarkerBottom = 1;
852f4eb4c5dSMatthew G. Knepley   PetscInt    markerFront = 1, faceMarkerFront = 1;
853f4eb4c5dSMatthew G. Knepley   PetscInt    markerBack = 1, faceMarkerBack = 1;
854f4eb4c5dSMatthew G. Knepley   PetscInt    markerRight = 1, faceMarkerRight = 1;
855f4eb4c5dSMatthew G. Knepley   PetscInt    markerLeft = 1, faceMarkerLeft = 1;
8563dfda0b1SToby Isaac   PetscInt    dim;
857d8211ee3SMatthew G. Knepley   PetscBool   markerSeparate = PETSC_FALSE, cutMarker = PETSC_FALSE;
8583dfda0b1SToby Isaac   PetscMPIInt rank;
8593dfda0b1SToby Isaac 
8603dfda0b1SToby Isaac   PetscFunctionBegin;
8619566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
8629566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
8639566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
8649566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
8659566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL));
8669371c9d4SSatish 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) {
8679371c9d4SSatish Balay     if (cutMarker) {
8689371c9d4SSatish Balay       PetscCall(DMCreateLabel(dm, "periodic_cut"));
8699371c9d4SSatish Balay       PetscCall(DMGetLabel(dm, "periodic_cut", &cutLabel));
8709371c9d4SSatish Balay     }
871d8211ee3SMatthew G. Knepley   }
8723dfda0b1SToby Isaac   switch (dim) {
8733dfda0b1SToby Isaac   case 2:
874f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 3;
875f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
876f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 2;
877f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 4;
8783dfda0b1SToby Isaac     break;
8793dfda0b1SToby Isaac   case 3:
880f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
881f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 2;
882f4eb4c5dSMatthew G. Knepley     faceMarkerFront  = 3;
883f4eb4c5dSMatthew G. Knepley     faceMarkerBack   = 4;
884f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 5;
885f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 6;
8863dfda0b1SToby Isaac     break;
887d71ae5a4SJacob Faibussowitsch   default:
888d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Dimension %" PetscInt_FMT " not supported", dim);
8893dfda0b1SToby Isaac   }
8909566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
891f4eb4c5dSMatthew G. Knepley   if (markerSeparate) {
892f4eb4c5dSMatthew G. Knepley     markerBottom = faceMarkerBottom;
893f4eb4c5dSMatthew G. Knepley     markerTop    = faceMarkerTop;
894f4eb4c5dSMatthew G. Knepley     markerFront  = faceMarkerFront;
895f4eb4c5dSMatthew G. Knepley     markerBack   = faceMarkerBack;
896f4eb4c5dSMatthew G. Knepley     markerRight  = faceMarkerRight;
897f4eb4c5dSMatthew G. Knepley     markerLeft   = faceMarkerLeft;
8983dfda0b1SToby Isaac   }
8993dfda0b1SToby Isaac   {
900dd400576SPatrick Sanan     const PetscInt numXEdges    = rank == 0 ? edges[0] : 0;
901dd400576SPatrick Sanan     const PetscInt numYEdges    = rank == 0 ? edges[1] : 0;
902dd400576SPatrick Sanan     const PetscInt numZEdges    = rank == 0 ? edges[2] : 0;
903dd400576SPatrick Sanan     const PetscInt numXVertices = rank == 0 ? (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ? edges[0] : edges[0] + 1) : 0;
904dd400576SPatrick Sanan     const PetscInt numYVertices = rank == 0 ? (bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ? edges[1] : edges[1] + 1) : 0;
905dd400576SPatrick Sanan     const PetscInt numZVertices = rank == 0 ? (bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST ? edges[2] : edges[2] + 1) : 0;
9063dfda0b1SToby Isaac     const PetscInt numCells     = numXEdges * numYEdges * numZEdges;
9073dfda0b1SToby Isaac     const PetscInt numXFaces    = numYEdges * numZEdges;
9083dfda0b1SToby Isaac     const PetscInt numYFaces    = numXEdges * numZEdges;
9093dfda0b1SToby Isaac     const PetscInt numZFaces    = numXEdges * numYEdges;
9103dfda0b1SToby Isaac     const PetscInt numTotXFaces = numXVertices * numXFaces;
9113dfda0b1SToby Isaac     const PetscInt numTotYFaces = numYVertices * numYFaces;
9123dfda0b1SToby Isaac     const PetscInt numTotZFaces = numZVertices * numZFaces;
9133dfda0b1SToby Isaac     const PetscInt numFaces     = numTotXFaces + numTotYFaces + numTotZFaces;
9143dfda0b1SToby Isaac     const PetscInt numTotXEdges = numXEdges * numYVertices * numZVertices;
9153dfda0b1SToby Isaac     const PetscInt numTotYEdges = numYEdges * numXVertices * numZVertices;
9163dfda0b1SToby Isaac     const PetscInt numTotZEdges = numZEdges * numXVertices * numYVertices;
9173dfda0b1SToby Isaac     const PetscInt numVertices  = numXVertices * numYVertices * numZVertices;
9183dfda0b1SToby Isaac     const PetscInt numEdges     = numTotXEdges + numTotYEdges + numTotZEdges;
9193dfda0b1SToby Isaac     const PetscInt firstVertex  = (dim == 2) ? numFaces : numCells;
9203dfda0b1SToby Isaac     const PetscInt firstXFace   = (dim == 2) ? 0 : numCells + numVertices;
9213dfda0b1SToby Isaac     const PetscInt firstYFace   = firstXFace + numTotXFaces;
9223dfda0b1SToby Isaac     const PetscInt firstZFace   = firstYFace + numTotYFaces;
9233dfda0b1SToby Isaac     const PetscInt firstXEdge   = numCells + numFaces + numVertices;
9243dfda0b1SToby Isaac     const PetscInt firstYEdge   = firstXEdge + numTotXEdges;
9253dfda0b1SToby Isaac     const PetscInt firstZEdge   = firstYEdge + numTotYEdges;
9263dfda0b1SToby Isaac     Vec            coordinates;
9273dfda0b1SToby Isaac     PetscSection   coordSection;
9283dfda0b1SToby Isaac     PetscScalar   *coords;
9293dfda0b1SToby Isaac     PetscInt       coordSize;
9303dfda0b1SToby Isaac     PetscInt       v, vx, vy, vz;
9313dfda0b1SToby Isaac     PetscInt       c, f, fx, fy, fz, e, ex, ey, ez;
9323dfda0b1SToby Isaac 
9339566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells + numFaces + numEdges + numVertices));
93448a46eb9SPierre Jolivet     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 6));
93548a46eb9SPierre Jolivet     for (f = firstXFace; f < firstXFace + numFaces; ++f) PetscCall(DMPlexSetConeSize(dm, f, 4));
93648a46eb9SPierre Jolivet     for (e = firstXEdge; e < firstXEdge + numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2));
9379566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
9383dfda0b1SToby Isaac     /* Build cells */
9393dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
9403dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
9413dfda0b1SToby Isaac         for (fx = 0; fx < numXEdges; ++fx) {
9423dfda0b1SToby Isaac           PetscInt cell  = (fz * numYEdges + fy) * numXEdges + fx;
9433dfda0b1SToby Isaac           PetscInt faceB = firstZFace + (fy * numXEdges + fx) * numZVertices + fz;
9443dfda0b1SToby Isaac           PetscInt faceT = firstZFace + (fy * numXEdges + fx) * numZVertices + ((fz + 1) % numZVertices);
9453dfda0b1SToby Isaac           PetscInt faceF = firstYFace + (fz * numXEdges + fx) * numYVertices + fy;
9463dfda0b1SToby Isaac           PetscInt faceK = firstYFace + (fz * numXEdges + fx) * numYVertices + ((fy + 1) % numYVertices);
9473dfda0b1SToby Isaac           PetscInt faceL = firstXFace + (fz * numYEdges + fy) * numXVertices + fx;
9483dfda0b1SToby Isaac           PetscInt faceR = firstXFace + (fz * numYEdges + fy) * numXVertices + ((fx + 1) % numXVertices);
9493dfda0b1SToby Isaac           /* B,  T,  F,  K,  R,  L */
950b5a892a1SMatthew G. Knepley           PetscInt ornt[6] = {-2, 0, 0, -3, 0, -2}; /* ??? */
95142206facSLisandro Dalcin           PetscInt cone[6];
9523dfda0b1SToby Isaac 
9533dfda0b1SToby Isaac           /* no boundary twisting in 3D */
9549371c9d4SSatish Balay           cone[0] = faceB;
9559371c9d4SSatish Balay           cone[1] = faceT;
9569371c9d4SSatish Balay           cone[2] = faceF;
9579371c9d4SSatish Balay           cone[3] = faceK;
9589371c9d4SSatish Balay           cone[4] = faceR;
9599371c9d4SSatish Balay           cone[5] = faceL;
9609566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, cell, cone));
9619566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, cell, ornt));
9629566063dSJacob Faibussowitsch           if (bdX != DM_BOUNDARY_NONE && fx == numXEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
9639566063dSJacob Faibussowitsch           if (bdY != DM_BOUNDARY_NONE && fy == numYEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
9649566063dSJacob Faibussowitsch           if (bdZ != DM_BOUNDARY_NONE && fz == numZEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
9653dfda0b1SToby Isaac         }
9663dfda0b1SToby Isaac       }
9673dfda0b1SToby Isaac     }
9683dfda0b1SToby Isaac     /* Build x faces */
9693dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
9703dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
9713dfda0b1SToby Isaac         for (fx = 0; fx < numXVertices; ++fx) {
9723dfda0b1SToby Isaac           PetscInt face    = firstXFace + (fz * numYEdges + fy) * numXVertices + fx;
9733dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy * numXVertices + fx) * numZEdges + fz;
9743dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (((fy + 1) % numYVertices) * numXVertices + fx) * numZEdges + fz;
9753dfda0b1SToby Isaac           PetscInt edgeB   = firstYEdge + (fz * numXVertices + fx) * numYEdges + fy;
9763dfda0b1SToby Isaac           PetscInt edgeT   = firstYEdge + (((fz + 1) % numZVertices) * numXVertices + fx) * numYEdges + fy;
977b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
9783dfda0b1SToby Isaac           PetscInt cone[4];
9793dfda0b1SToby Isaac 
9803dfda0b1SToby Isaac           if (dim == 3) {
9813dfda0b1SToby Isaac             /* markers */
9823dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
9833dfda0b1SToby Isaac               if (fx == numXVertices - 1) {
9849566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerRight));
9859566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerRight));
9869371c9d4SSatish Balay               } else if (fx == 0) {
9879566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerLeft));
9889566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerLeft));
9893dfda0b1SToby Isaac               }
9903dfda0b1SToby Isaac             }
9913dfda0b1SToby Isaac           }
9929371c9d4SSatish Balay           cone[0] = edgeB;
9939371c9d4SSatish Balay           cone[1] = edgeR;
9949371c9d4SSatish Balay           cone[2] = edgeT;
9959371c9d4SSatish Balay           cone[3] = edgeL;
9969566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
9979566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
9983dfda0b1SToby Isaac         }
9993dfda0b1SToby Isaac       }
10003dfda0b1SToby Isaac     }
10013dfda0b1SToby Isaac     /* Build y faces */
10023dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
100342206facSLisandro Dalcin       for (fx = 0; fx < numXEdges; ++fx) {
10043dfda0b1SToby Isaac         for (fy = 0; fy < numYVertices; ++fy) {
10053dfda0b1SToby Isaac           PetscInt face    = firstYFace + (fz * numXEdges + fx) * numYVertices + fy;
10063dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy * numXVertices + fx) * numZEdges + fz;
10073dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (fy * numXVertices + ((fx + 1) % numXVertices)) * numZEdges + fz;
10083dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz * numYVertices + fy) * numXEdges + fx;
10093dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (((fz + 1) % numZVertices) * numYVertices + fy) * numXEdges + fx;
1010b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
10113dfda0b1SToby Isaac           PetscInt cone[4];
10123dfda0b1SToby Isaac 
10133dfda0b1SToby Isaac           if (dim == 3) {
10143dfda0b1SToby Isaac             /* markers */
10153dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
10163dfda0b1SToby Isaac               if (fy == numYVertices - 1) {
10179566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerBack));
10189566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerBack));
10199371c9d4SSatish Balay               } else if (fy == 0) {
10209566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerFront));
10219566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerFront));
10223dfda0b1SToby Isaac               }
10233dfda0b1SToby Isaac             }
10243dfda0b1SToby Isaac           }
10259371c9d4SSatish Balay           cone[0] = edgeB;
10269371c9d4SSatish Balay           cone[1] = edgeR;
10279371c9d4SSatish Balay           cone[2] = edgeT;
10289371c9d4SSatish Balay           cone[3] = edgeL;
10299566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
10309566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
10313dfda0b1SToby Isaac         }
10323dfda0b1SToby Isaac       }
10333dfda0b1SToby Isaac     }
10343dfda0b1SToby Isaac     /* Build z faces */
10353dfda0b1SToby Isaac     for (fy = 0; fy < numYEdges; ++fy) {
10363dfda0b1SToby Isaac       for (fx = 0; fx < numXEdges; ++fx) {
10373dfda0b1SToby Isaac         for (fz = 0; fz < numZVertices; fz++) {
10383dfda0b1SToby Isaac           PetscInt face    = firstZFace + (fy * numXEdges + fx) * numZVertices + fz;
10393dfda0b1SToby Isaac           PetscInt edgeL   = firstYEdge + (fz * numXVertices + fx) * numYEdges + fy;
10403dfda0b1SToby Isaac           PetscInt edgeR   = firstYEdge + (fz * numXVertices + ((fx + 1) % numXVertices)) * numYEdges + fy;
10413dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz * numYVertices + fy) * numXEdges + fx;
10423dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (fz * numYVertices + ((fy + 1) % numYVertices)) * numXEdges + fx;
1043b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
10443dfda0b1SToby Isaac           PetscInt cone[4];
10453dfda0b1SToby Isaac 
10463dfda0b1SToby Isaac           if (dim == 2) {
10479371c9d4SSatish Balay             if (bdX == DM_BOUNDARY_TWIST && fx == numXEdges - 1) {
10489371c9d4SSatish Balay               edgeR += numYEdges - 1 - 2 * fy;
10499371c9d4SSatish Balay               ornt[1] = -1;
10509371c9d4SSatish Balay             }
10519371c9d4SSatish Balay             if (bdY == DM_BOUNDARY_TWIST && fy == numYEdges - 1) {
10529371c9d4SSatish Balay               edgeT += numXEdges - 1 - 2 * fx;
10539371c9d4SSatish Balay               ornt[2] = 0;
10549371c9d4SSatish Balay             }
10559566063dSJacob Faibussowitsch             if (bdX != DM_BOUNDARY_NONE && fx == numXEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, face, 2));
10569566063dSJacob Faibussowitsch             if (bdY != DM_BOUNDARY_NONE && fy == numYEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, face, 2));
1057d1c88043SMatthew G. Knepley           } else {
10583dfda0b1SToby Isaac             /* markers */
10593dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
10603dfda0b1SToby Isaac               if (fz == numZVertices - 1) {
10619566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerTop));
10629566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerTop));
10639371c9d4SSatish Balay               } else if (fz == 0) {
10649566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerBottom));
10659566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerBottom));
10663dfda0b1SToby Isaac               }
10673dfda0b1SToby Isaac             }
10683dfda0b1SToby Isaac           }
10699371c9d4SSatish Balay           cone[0] = edgeB;
10709371c9d4SSatish Balay           cone[1] = edgeR;
10719371c9d4SSatish Balay           cone[2] = edgeT;
10729371c9d4SSatish Balay           cone[3] = edgeL;
10739566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
10749566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
10753dfda0b1SToby Isaac         }
10763dfda0b1SToby Isaac       }
10773dfda0b1SToby Isaac     }
10783dfda0b1SToby Isaac     /* Build Z edges*/
10793dfda0b1SToby Isaac     for (vy = 0; vy < numYVertices; vy++) {
10803dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
10813dfda0b1SToby Isaac         for (ez = 0; ez < numZEdges; ez++) {
10823dfda0b1SToby Isaac           const PetscInt edge    = firstZEdge + (vy * numXVertices + vx) * numZEdges + ez;
10833dfda0b1SToby Isaac           const PetscInt vertexB = firstVertex + (ez * numYVertices + vy) * numXVertices + vx;
10843dfda0b1SToby Isaac           const PetscInt vertexT = firstVertex + (((ez + 1) % numZVertices) * numYVertices + vy) * numXVertices + vx;
10853dfda0b1SToby Isaac           PetscInt       cone[2];
10863dfda0b1SToby Isaac 
10879371c9d4SSatish Balay           cone[0] = vertexB;
10889371c9d4SSatish Balay           cone[1] = vertexT;
1089c2df9bbfSMatthew G. Knepley           PetscCall(DMPlexSetCone(dm, edge, cone));
10903dfda0b1SToby Isaac           if (dim == 3) {
10913dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
10923dfda0b1SToby Isaac               if (vx == numXVertices - 1) {
10939566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
1094c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1095c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1096c2df9bbfSMatthew G. Knepley               } else if (vx == 0) {
10979566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
1098c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1099c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
11003dfda0b1SToby Isaac               }
11013dfda0b1SToby Isaac             }
11023dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
11033dfda0b1SToby Isaac               if (vy == numYVertices - 1) {
11049566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBack));
1105c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBack));
1106c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBack));
1107c2df9bbfSMatthew G. Knepley               } else if (vy == 0) {
11089566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerFront));
1109c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerFront));
1110c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerFront));
11113dfda0b1SToby Isaac               }
11123dfda0b1SToby Isaac             }
11133dfda0b1SToby Isaac           }
11143dfda0b1SToby Isaac         }
11153dfda0b1SToby Isaac       }
11163dfda0b1SToby Isaac     }
11173dfda0b1SToby Isaac     /* Build Y edges*/
11183dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
11193dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
11203dfda0b1SToby Isaac         for (ey = 0; ey < numYEdges; ey++) {
11213dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdY == DM_BOUNDARY_TWIST && ey == numYEdges - 1) ? (numXVertices - vx - 1) : (vz * numYVertices + ((ey + 1) % numYVertices)) * numXVertices + vx;
11223dfda0b1SToby Isaac           const PetscInt edge    = firstYEdge + (vz * numXVertices + vx) * numYEdges + ey;
11233dfda0b1SToby Isaac           const PetscInt vertexF = firstVertex + (vz * numYVertices + ey) * numXVertices + vx;
11243dfda0b1SToby Isaac           const PetscInt vertexK = firstVertex + nextv;
11253dfda0b1SToby Isaac           PetscInt       cone[2];
11263dfda0b1SToby Isaac 
11279371c9d4SSatish Balay           cone[0] = vertexF;
11289371c9d4SSatish Balay           cone[1] = vertexK;
11299566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, edge, cone));
11303dfda0b1SToby Isaac           if (dim == 2) {
11313dfda0b1SToby Isaac             if ((bdX != DM_BOUNDARY_PERIODIC) && (bdX != DM_BOUNDARY_TWIST)) {
11323dfda0b1SToby Isaac               if (vx == numXVertices - 1) {
11339566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerRight));
11349566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
11359566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1136c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1137d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
11389566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerLeft));
11399566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
11409566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1141c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
11423dfda0b1SToby Isaac               }
1143d8211ee3SMatthew G. Knepley             } else {
11444c67ea77SStefano Zampini               if (vx == 0 && cutLabel) {
11459566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, edge, 1));
11469566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, cone[0], 1));
1147c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMLabelSetValue(cutLabel, cone[1], 1));
11483dfda0b1SToby Isaac               }
1149d8211ee3SMatthew G. Knepley             }
1150d8211ee3SMatthew G. Knepley           } else {
11513dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
11523dfda0b1SToby Isaac               if (vx == numXVertices - 1) {
11539566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
1154c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1155c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1156d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
11579566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
1158c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1159c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
11603dfda0b1SToby Isaac               }
11613dfda0b1SToby Isaac             }
11623dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
11633dfda0b1SToby Isaac               if (vz == numZVertices - 1) {
11649566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
1165c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1166c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1167d8211ee3SMatthew G. Knepley               } else if (vz == 0) {
11689566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
1169c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1170c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
11713dfda0b1SToby Isaac               }
11723dfda0b1SToby Isaac             }
11733dfda0b1SToby Isaac           }
11743dfda0b1SToby Isaac         }
11753dfda0b1SToby Isaac       }
11763dfda0b1SToby Isaac     }
11773dfda0b1SToby Isaac     /* Build X edges*/
11783dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
11793dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; vy++) {
11803dfda0b1SToby Isaac         for (ex = 0; ex < numXEdges; ex++) {
11813dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdX == DM_BOUNDARY_TWIST && ex == numXEdges - 1) ? (numYVertices - vy - 1) * numXVertices : (vz * numYVertices + vy) * numXVertices + (ex + 1) % numXVertices;
11823dfda0b1SToby Isaac           const PetscInt edge    = firstXEdge + (vz * numYVertices + vy) * numXEdges + ex;
11833dfda0b1SToby Isaac           const PetscInt vertexL = firstVertex + (vz * numYVertices + vy) * numXVertices + ex;
11843dfda0b1SToby Isaac           const PetscInt vertexR = firstVertex + nextv;
11853dfda0b1SToby Isaac           PetscInt       cone[2];
11863dfda0b1SToby Isaac 
11879371c9d4SSatish Balay           cone[0] = vertexL;
11889371c9d4SSatish Balay           cone[1] = vertexR;
11899566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, edge, cone));
11903dfda0b1SToby Isaac           if (dim == 2) {
11913dfda0b1SToby Isaac             if ((bdY != DM_BOUNDARY_PERIODIC) && (bdY != DM_BOUNDARY_TWIST)) {
11923dfda0b1SToby Isaac               if (vy == numYVertices - 1) {
11939566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerTop));
11949566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
11959566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1196c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1197d8211ee3SMatthew G. Knepley               } else if (vy == 0) {
11989566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerBottom));
11999566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
12009566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1201c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
12023dfda0b1SToby Isaac               }
1203d8211ee3SMatthew G. Knepley             } else {
12044c67ea77SStefano Zampini               if (vy == 0 && cutLabel) {
12059566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, edge, 1));
12069566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, cone[0], 1));
1207c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMLabelSetValue(cutLabel, cone[1], 1));
12083dfda0b1SToby Isaac               }
1209d8211ee3SMatthew G. Knepley             }
1210d8211ee3SMatthew G. Knepley           } else {
12113dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
12123dfda0b1SToby Isaac               if (vy == numYVertices - 1) {
12139566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBack));
1214c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBack));
1215c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBack));
1216c2df9bbfSMatthew G. Knepley               } else if (vy == 0) {
12179566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerFront));
1218c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerFront));
1219c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerFront));
12203dfda0b1SToby Isaac               }
12213dfda0b1SToby Isaac             }
12223dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
12233dfda0b1SToby Isaac               if (vz == numZVertices - 1) {
12249566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
1225c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1226c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1227c2df9bbfSMatthew G. Knepley               } else if (vz == 0) {
12289566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
1229c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1230c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
12313dfda0b1SToby Isaac               }
12323dfda0b1SToby Isaac             }
12333dfda0b1SToby Isaac           }
12343dfda0b1SToby Isaac         }
12353dfda0b1SToby Isaac       }
12363dfda0b1SToby Isaac     }
12379566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
12389566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
12393dfda0b1SToby Isaac     /* Build coordinates */
12409566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
12419566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
12429566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
12439566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numVertices));
12443dfda0b1SToby Isaac     for (v = firstVertex; v < firstVertex + numVertices; ++v) {
12459566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
12469566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
12473dfda0b1SToby Isaac     }
12489566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
12499566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
12509566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
12519566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
12529566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
12539566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
12549566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates, VECSTANDARD));
12559566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
12563dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; ++vz) {
12573dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; ++vy) {
12583dfda0b1SToby Isaac         for (vx = 0; vx < numXVertices; ++vx) {
12593dfda0b1SToby Isaac           coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 0] = lower[0] + ((upper[0] - lower[0]) / numXEdges) * vx;
12603dfda0b1SToby Isaac           coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 1] = lower[1] + ((upper[1] - lower[1]) / numYEdges) * vy;
1261ad540459SPierre Jolivet           if (dim == 3) coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 2] = lower[2] + ((upper[2] - lower[2]) / numZEdges) * vz;
12623dfda0b1SToby Isaac         }
12633dfda0b1SToby Isaac       }
12643dfda0b1SToby Isaac     }
12659566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
12669566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
12679566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
12683dfda0b1SToby Isaac   }
12693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12703dfda0b1SToby Isaac }
12713dfda0b1SToby Isaac 
1272d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxMesh_Tensor_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
1273d71ae5a4SJacob Faibussowitsch {
12749318fe57SMatthew G. Knepley   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
12759318fe57SMatthew G. Knepley   PetscInt       fac[3] = {0, 0, 0}, d;
1276552f7358SJed Brown 
1277552f7358SJed Brown   PetscFunctionBegin;
12784f572ea9SToby Isaac   PetscAssertPointer(dm, 1);
12799318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
12809566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
12819371c9d4SSatish Balay   for (d = 0; d < dim; ++d) {
12829371c9d4SSatish Balay     fac[d] = faces[d];
12839371c9d4SSatish Balay     bdt[d] = periodicity[d];
12849371c9d4SSatish Balay   }
12859566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateCubeMesh_Internal(dm, lower, upper, fac, bdt[0], bdt[1], bdt[2]));
12869371c9d4SSatish 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))) {
12876858538eSMatthew G. Knepley     PetscReal L[3]       = {-1., -1., 0.};
12886858538eSMatthew G. Knepley     PetscReal maxCell[3] = {-1., -1., 0.};
1289552f7358SJed Brown 
12909318fe57SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
12916858538eSMatthew G. Knepley       if (periodicity[d] != DM_BOUNDARY_NONE) {
12929318fe57SMatthew G. Knepley         L[d]       = upper[d] - lower[d];
12939318fe57SMatthew G. Knepley         maxCell[d] = 1.1 * (L[d] / PetscMax(1, faces[d]));
1294768d5fceSMatthew G. Knepley       }
12956858538eSMatthew G. Knepley     }
12964fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, maxCell, lower, L));
1297768d5fceSMatthew G. Knepley   }
12989566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
12993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13009318fe57SMatthew G. Knepley }
13019318fe57SMatthew G. Knepley 
13025dca41c3SJed 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)
1303d71ae5a4SJacob Faibussowitsch {
13049318fe57SMatthew G. Knepley   PetscFunctionBegin;
130546139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
13065dca41c3SJed Brown   if (shape == DM_SHAPE_ZBOX) PetscCall(DMPlexCreateBoxMesh_Tensor_SFC_Internal(dm, dim, faces, lower, upper, periodicity, interpolate));
13076725e60dSJed Brown   else if (dim == 1) PetscCall(DMPlexCreateLineMesh_Internal(dm, faces[0], lower[0], upper[0], periodicity[0]));
13089566063dSJacob Faibussowitsch   else if (simplex) PetscCall(DMPlexCreateBoxMesh_Simplex_Internal(dm, dim, faces, lower, upper, periodicity, interpolate));
13099566063dSJacob Faibussowitsch   else PetscCall(DMPlexCreateBoxMesh_Tensor_Internal(dm, dim, faces, lower, upper, periodicity));
13109318fe57SMatthew G. Knepley   if (!interpolate && dim > 1 && !simplex) {
1311768d5fceSMatthew G. Knepley     DM udm;
1312768d5fceSMatthew G. Knepley 
13139566063dSJacob Faibussowitsch     PetscCall(DMPlexUninterpolate(dm, &udm));
13149566063dSJacob Faibussowitsch     PetscCall(DMPlexCopyCoordinates(dm, udm));
131569d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &udm));
1316768d5fceSMatthew G. Knepley   }
131746139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
13183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1319c8c68bd8SToby Isaac }
1320c8c68bd8SToby Isaac 
1321768d5fceSMatthew G. Knepley /*@C
1322768d5fceSMatthew G. Knepley   DMPlexCreateBoxMesh - Creates a mesh on the tensor product of unit intervals (box) using simplices or tensor cells (hexahedra).
1323768d5fceSMatthew G. Knepley 
1324d083f849SBarry Smith   Collective
1325768d5fceSMatthew G. Knepley 
1326768d5fceSMatthew G. Knepley   Input Parameters:
1327a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
1328768d5fceSMatthew G. Knepley . dim         - The spatial dimension
1329a1cb98faSBarry Smith . simplex     - `PETSC_TRUE` for simplices, `PETSC_FALSE` for tensor cells
133020f4b53cSBarry Smith . faces       - Number of faces per dimension, or `NULL` for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
133120f4b53cSBarry Smith . lower       - The lower left corner, or `NULL` for (0, 0, 0)
133220f4b53cSBarry Smith . upper       - The upper right corner, or `NULL` for (1, 1, 1)
133320f4b53cSBarry Smith . periodicity - The boundary type for the X,Y,Z direction, or `NULL` for `DM_BOUNDARY_NONE`
1334768d5fceSMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
1335768d5fceSMatthew G. Knepley 
1336768d5fceSMatthew G. Knepley   Output Parameter:
1337a1cb98faSBarry Smith . dm - The `DM` object
1338768d5fceSMatthew G. Knepley 
1339768d5fceSMatthew G. Knepley   Level: beginner
1340768d5fceSMatthew G. Knepley 
1341a1cb98faSBarry Smith   Note:
1342a1cb98faSBarry Smith   To customize this mesh using options, use
1343a1cb98faSBarry Smith .vb
1344a1cb98faSBarry Smith   DMCreate(comm, &dm);
1345a1cb98faSBarry Smith   DMSetType(dm, DMPLEX);
1346a1cb98faSBarry Smith   DMSetFromOptions(dm);
1347a1cb98faSBarry Smith .ve
1348a1cb98faSBarry Smith   and use the options in `DMSetFromOptions()`.
1349a1cb98faSBarry Smith 
1350a4e35b19SJacob Faibussowitsch   Here is the numbering returned for 2 faces in each direction for tensor cells\:
1351a1cb98faSBarry Smith .vb
1352a1cb98faSBarry Smith  10---17---11---18----12
1353a1cb98faSBarry Smith   |         |         |
1354a1cb98faSBarry Smith   |         |         |
1355a1cb98faSBarry Smith  20    2   22    3    24
1356a1cb98faSBarry Smith   |         |         |
1357a1cb98faSBarry Smith   |         |         |
1358a1cb98faSBarry Smith   7---15----8---16----9
1359a1cb98faSBarry Smith   |         |         |
1360a1cb98faSBarry Smith   |         |         |
1361a1cb98faSBarry Smith  19    0   21    1   23
1362a1cb98faSBarry Smith   |         |         |
1363a1cb98faSBarry Smith   |         |         |
1364a1cb98faSBarry Smith   4---13----5---14----6
1365a1cb98faSBarry Smith .ve
1366a1cb98faSBarry Smith   and for simplicial cells
1367a1cb98faSBarry Smith .vb
1368a1cb98faSBarry Smith  14----8---15----9----16
1369a1cb98faSBarry Smith   |\     5  |\      7 |
1370a1cb98faSBarry Smith   | \       | \       |
1371a1cb98faSBarry Smith  13   2    14    3    15
1372a1cb98faSBarry Smith   | 4   \   | 6   \   |
1373a1cb98faSBarry Smith   |       \ |       \ |
1374a1cb98faSBarry Smith  11----6---12----7----13
1375a1cb98faSBarry Smith   |\        |\        |
1376a1cb98faSBarry Smith   | \    1  | \     3 |
1377a1cb98faSBarry Smith  10   0    11    1    12
1378a1cb98faSBarry Smith   | 0   \   | 2   \   |
1379a1cb98faSBarry Smith   |       \ |       \ |
1380a1cb98faSBarry Smith   8----4----9----5----10
1381a1cb98faSBarry Smith .ve
1382a1cb98faSBarry Smith 
13831cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMSetFromOptions()`, `DMPlexCreateFromFile()`, `DMPlexCreateHexCylinderMesh()`, `DMSetType()`, `DMCreate()`
1384768d5fceSMatthew G. Knepley @*/
1385d71ae5a4SJacob 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)
1386d71ae5a4SJacob Faibussowitsch {
13879318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
1388fdbf62faSLisandro Dalcin   PetscReal      low[3] = {0, 0, 0};
1389fdbf62faSLisandro Dalcin   PetscReal      upp[3] = {1, 1, 1};
1390fdbf62faSLisandro Dalcin   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
1391552f7358SJed Brown 
1392768d5fceSMatthew G. Knepley   PetscFunctionBegin;
13939566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
13949566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
13955dca41c3SJed Brown   PetscCall(DMPlexCreateBoxMesh_Internal(*dm, DM_SHAPE_BOX, dim, simplex, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt, interpolate));
13967ff04441SMatthew G. Knepley   if (periodicity) PetscCall(DMLocalizeCoordinates(*dm));
13973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13989318fe57SMatthew G. Knepley }
1399fdbf62faSLisandro Dalcin 
1400d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateWedgeBoxMesh_Internal(DM dm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
1401d71ae5a4SJacob Faibussowitsch {
14029318fe57SMatthew G. Knepley   DM       bdm, vol;
14039318fe57SMatthew G. Knepley   PetscInt i;
14049318fe57SMatthew G. Knepley 
14059318fe57SMatthew G. Knepley   PetscFunctionBegin;
14061fcf445aSMatthew G. Knepley   // TODO Now we can support periodicity
140708401ef6SPierre Jolivet   for (i = 0; i < 3; ++i) PetscCheck(periodicity[i] == DM_BOUNDARY_NONE, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Periodicity not yet supported");
14089566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &bdm));
14099566063dSJacob Faibussowitsch   PetscCall(DMSetType(bdm, DMPLEX));
14109566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(bdm, 2));
141146139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, bdm, 0, 0, 0));
14129566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxMesh_Simplex_Internal(bdm, 2, faces, lower, upper, periodicity, PETSC_TRUE));
14131fcf445aSMatthew G. Knepley   PetscCall(DMPlexExtrude(bdm, faces[2], upper[2] - lower[2], PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, NULL, NULL, &vol));
141446139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, bdm, 0, 0, 0));
14159566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&bdm));
141669d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &vol));
14179318fe57SMatthew G. Knepley   if (lower[2] != 0.0) {
14189318fe57SMatthew G. Knepley     Vec          v;
14199318fe57SMatthew G. Knepley     PetscScalar *x;
14209318fe57SMatthew G. Knepley     PetscInt     cDim, n;
14219318fe57SMatthew G. Knepley 
14229566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &v));
14239566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(v, &cDim));
14249566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(v, &n));
14259566063dSJacob Faibussowitsch     PetscCall(VecGetArray(v, &x));
14269318fe57SMatthew G. Knepley     x += cDim;
14279318fe57SMatthew G. Knepley     for (i = 0; i < n; i += cDim) x[i] += lower[2];
14289566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(v, &x));
14299566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, v));
14309318fe57SMatthew G. Knepley   }
14313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1432552f7358SJed Brown }
1433552f7358SJed Brown 
143400dabe28SStefano Zampini /*@
143500dabe28SStefano Zampini   DMPlexCreateWedgeBoxMesh - Creates a 3-D mesh tesselating the (x,y) plane and extruding in the third direction using wedge cells.
143600dabe28SStefano Zampini 
1437d083f849SBarry Smith   Collective
143800dabe28SStefano Zampini 
143900dabe28SStefano Zampini   Input Parameters:
1440a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
144120f4b53cSBarry Smith . faces       - Number of faces per dimension, or `NULL` for (1, 1, 1)
144220f4b53cSBarry Smith . lower       - The lower left corner, or `NULL` for (0, 0, 0)
144320f4b53cSBarry Smith . upper       - The upper right corner, or `NULL` for (1, 1, 1)
144420f4b53cSBarry Smith . periodicity - The boundary type for the X,Y,Z direction, or `NULL` for `DM_BOUNDARY_NONE`
1445a1cb98faSBarry Smith . orderHeight - If `PETSC_TRUE`, orders the extruded cells in the height first. Otherwise, orders the cell on the layers first
144600dabe28SStefano Zampini - interpolate - Flag to create intermediate mesh pieces (edges, faces)
144700dabe28SStefano Zampini 
144800dabe28SStefano Zampini   Output Parameter:
1449a1cb98faSBarry Smith . dm - The `DM` object
145000dabe28SStefano Zampini 
145100dabe28SStefano Zampini   Level: beginner
145200dabe28SStefano Zampini 
14531cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateHexCylinderMesh()`, `DMPlexCreateWedgeCylinderMesh()`, `DMExtrude()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
145400dabe28SStefano Zampini @*/
1455d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateWedgeBoxMesh(MPI_Comm comm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool orderHeight, PetscBool interpolate, DM *dm)
1456d71ae5a4SJacob Faibussowitsch {
14579318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
145800dabe28SStefano Zampini   PetscReal      low[3] = {0, 0, 0};
145900dabe28SStefano Zampini   PetscReal      upp[3] = {1, 1, 1};
146000dabe28SStefano Zampini   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
146100dabe28SStefano Zampini 
146200dabe28SStefano Zampini   PetscFunctionBegin;
14639566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
14649566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
14659566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateWedgeBoxMesh_Internal(*dm, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt));
1466d410b0cfSMatthew G. Knepley   if (!interpolate) {
1467d410b0cfSMatthew G. Knepley     DM udm;
146800dabe28SStefano Zampini 
14699566063dSJacob Faibussowitsch     PetscCall(DMPlexUninterpolate(*dm, &udm));
147069d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(*dm, &udm));
147100dabe28SStefano Zampini   }
14727ff04441SMatthew G. Knepley   if (periodicity) PetscCall(DMLocalizeCoordinates(*dm));
14733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
147400dabe28SStefano Zampini }
147500dabe28SStefano Zampini 
1476cfb853baSMatthew G. Knepley /*
1477cfb853baSMatthew 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.
1478cfb853baSMatthew G. Knepley 
1479cfb853baSMatthew G. Knepley   Input Parameters:
1480cfb853baSMatthew G. Knepley + len - The length of the tuple
1481cfb853baSMatthew G. Knepley . max - The maximum for each dimension, so values are in [0, max)
1482cfb853baSMatthew G. Knepley - tup - A tuple of length len+1: tup[len] > 0 indicates a stopping condition
1483cfb853baSMatthew G. Knepley 
1484cfb853baSMatthew G. Knepley   Output Parameter:
148520f4b53cSBarry Smith . tup - A tuple of `len` integers whose entries are at most `max`
1486cfb853baSMatthew G. Knepley 
1487cfb853baSMatthew G. Knepley   Level: developer
1488cfb853baSMatthew G. Knepley 
148920f4b53cSBarry Smith   Note:
149020f4b53cSBarry Smith   Ordering is lexicographic with lowest index as least significant in ordering.
149120f4b53cSBarry 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}.
149220f4b53cSBarry Smith 
1493cfb853baSMatthew G. Knepley .seealso: PetscDualSpaceTensorPointLexicographic_Internal(), PetscDualSpaceLatticePointLexicographic_Internal()
1494cfb853baSMatthew G. Knepley */
1495cfb853baSMatthew G. Knepley static PetscErrorCode DMPlexTensorPointLexicographic_Private(PetscInt len, const PetscInt max[], PetscInt tup[])
1496cfb853baSMatthew G. Knepley {
1497cfb853baSMatthew G. Knepley   PetscInt i;
1498cfb853baSMatthew G. Knepley 
1499cfb853baSMatthew G. Knepley   PetscFunctionBegin;
1500cfb853baSMatthew G. Knepley   for (i = 0; i < len; ++i) {
1501cfb853baSMatthew G. Knepley     if (tup[i] < max[i] - 1) {
1502cfb853baSMatthew G. Knepley       break;
1503cfb853baSMatthew G. Knepley     } else {
1504cfb853baSMatthew G. Knepley       tup[i] = 0;
1505cfb853baSMatthew G. Knepley     }
1506cfb853baSMatthew G. Knepley   }
1507cfb853baSMatthew G. Knepley   if (i == len) tup[i - 1] = max[i - 1];
1508cfb853baSMatthew G. Knepley   else ++tup[i];
15093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1510cfb853baSMatthew G. Knepley }
1511cfb853baSMatthew G. Knepley 
1512cfb853baSMatthew G. Knepley static PetscInt TupleToIndex_Private(PetscInt len, const PetscInt max[], const PetscInt tup[])
1513cfb853baSMatthew G. Knepley {
1514cfb853baSMatthew G. Knepley   PetscInt i, idx = tup[len - 1];
1515cfb853baSMatthew G. Knepley 
1516cfb853baSMatthew G. Knepley   for (i = len - 2; i >= 0; --i) {
1517cfb853baSMatthew G. Knepley     idx *= max[i];
1518cfb853baSMatthew G. Knepley     idx += tup[i];
1519cfb853baSMatthew G. Knepley   }
1520cfb853baSMatthew G. Knepley   return idx;
1521cfb853baSMatthew G. Knepley }
1522cfb853baSMatthew G. Knepley 
1523cfb853baSMatthew G. Knepley static PetscErrorCode DestroyExtent_Private(void *extent)
1524cfb853baSMatthew G. Knepley {
1525cfb853baSMatthew G. Knepley   return PetscFree(extent);
1526cfb853baSMatthew G. Knepley }
1527cfb853baSMatthew G. Knepley 
1528cfb853baSMatthew G. Knepley static PetscErrorCode DMPlexCreateHypercubicMesh_Internal(DM dm, PetscInt dim, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], const DMBoundaryType bd[])
1529cfb853baSMatthew G. Knepley {
1530cfb853baSMatthew G. Knepley   Vec          coordinates;
1531cfb853baSMatthew G. Knepley   PetscSection coordSection;
1532cfb853baSMatthew G. Knepley   DMLabel      cutLabel    = NULL;
1533cfb853baSMatthew G. Knepley   PetscBool    cutMarker   = PETSC_FALSE;
1534cfb853baSMatthew G. Knepley   PetscBool    periodic    = PETSC_FALSE;
1535cfb853baSMatthew G. Knepley   PetscInt     numCells    = 1, c;
1536cfb853baSMatthew G. Knepley   PetscInt     numVertices = 1, v;
1537cfb853baSMatthew G. Knepley   PetscScalar *coords;
1538cfb853baSMatthew G. Knepley   PetscInt    *vertices, *vert, *vtmp, *supp, cone[2];
1539cfb853baSMatthew G. Knepley   PetscInt     d, e, cell = 0, coordSize;
1540cfb853baSMatthew G. Knepley   PetscMPIInt  rank;
1541cfb853baSMatthew G. Knepley 
1542cfb853baSMatthew G. Knepley   PetscFunctionBegin;
1543cfb853baSMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
1544cfb853baSMatthew G. Knepley   PetscCall(DMSetDimension(dm, dim));
1545cfb853baSMatthew G. Knepley   PetscCall(PetscCalloc4(dim, &vertices, dim, &vert, dim, &vtmp, 2 * dim, &supp));
1546cfb853baSMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "marker"));
1547cfb853baSMatthew G. Knepley   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL));
1548cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) periodic = (periodic || bd[d] == DM_BOUNDARY_PERIODIC) ? PETSC_TRUE : PETSC_FALSE;
1549cfb853baSMatthew G. Knepley   if (periodic && cutMarker) {
1550cfb853baSMatthew G. Knepley     PetscCall(DMCreateLabel(dm, "periodic_cut"));
1551cfb853baSMatthew G. Knepley     PetscCall(DMGetLabel(dm, "periodic_cut", &cutLabel));
1552cfb853baSMatthew G. Knepley   }
1553cfb853baSMatthew 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");
1554cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) {
1555cfb853baSMatthew G. Knepley     vertices[d] = edges[d];
1556cfb853baSMatthew G. Knepley     numVertices *= vertices[d];
1557cfb853baSMatthew G. Knepley   }
1558cfb853baSMatthew G. Knepley   numCells = numVertices * dim;
1559cfb853baSMatthew G. Knepley   PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
1560cfb853baSMatthew G. Knepley   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, 2));
1561cfb853baSMatthew G. Knepley   for (v = numCells; v < numCells + numVertices; ++v) PetscCall(DMPlexSetSupportSize(dm, v, 2 * dim));
1562cfb853baSMatthew G. Knepley   /* TODO Loop over boundary and reset support sizes */
1563cfb853baSMatthew G. Knepley   PetscCall(DMSetUp(dm)); /* Allocate space for cones and supports */
1564cfb853baSMatthew G. Knepley   /* Build cell cones and vertex supports */
1565cfb853baSMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "celltype"));
1566cfb853baSMatthew G. Knepley   while (vert[dim - 1] < vertices[dim - 1]) {
1567cfb853baSMatthew G. Knepley     const PetscInt vertex = TupleToIndex_Private(dim, vertices, vert) + numCells;
1568cfb853baSMatthew G. Knepley     PetscInt       s      = 0;
1569cfb853baSMatthew G. Knepley 
15703ba16761SJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "Vertex %" PetscInt_FMT ":", vertex));
15713ba16761SJacob Faibussowitsch     for (d = 0; d < dim; ++d) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, vert[d]));
15723ba16761SJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
1573cfb853baSMatthew G. Knepley     PetscCall(DMPlexSetCellType(dm, vertex, DM_POLYTOPE_POINT));
1574cfb853baSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
1575cfb853baSMatthew G. Knepley       for (e = 0; e < dim; ++e) vtmp[e] = vert[e];
1576cfb853baSMatthew G. Knepley       vtmp[d] = (vert[d] + 1) % vertices[d];
1577cfb853baSMatthew G. Knepley       cone[0] = vertex;
1578cfb853baSMatthew G. Knepley       cone[1] = TupleToIndex_Private(dim, vertices, vtmp) + numCells;
15793ba16761SJacob Faibussowitsch       PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Vertex %" PetscInt_FMT ":", cone[1]));
15803ba16761SJacob Faibussowitsch       for (e = 0; e < dim; ++e) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, vtmp[e]));
15813ba16761SJacob Faibussowitsch       PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
1582cfb853baSMatthew G. Knepley       PetscCall(DMPlexSetCone(dm, cell, cone));
1583cfb853baSMatthew G. Knepley       PetscCall(DMPlexSetCellType(dm, cell, DM_POLYTOPE_SEGMENT));
15843ba16761SJacob Faibussowitsch       PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Edge %" PetscInt_FMT " (%" PetscInt_FMT " %" PetscInt_FMT ")\n", cell, cone[0], cone[1]));
1585cfb853baSMatthew G. Knepley       ++cell;
1586cfb853baSMatthew G. Knepley     }
1587cfb853baSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
1588cfb853baSMatthew G. Knepley       for (e = 0; e < dim; ++e) vtmp[e] = vert[e];
1589cfb853baSMatthew G. Knepley       vtmp[d]   = (vert[d] + vertices[d] - 1) % vertices[d];
1590cfb853baSMatthew G. Knepley       supp[s++] = TupleToIndex_Private(dim, vertices, vtmp) * dim + d;
1591cfb853baSMatthew G. Knepley       supp[s++] = (vertex - numCells) * dim + d;
1592cfb853baSMatthew G. Knepley       PetscCall(DMPlexSetSupport(dm, vertex, supp));
1593cfb853baSMatthew G. Knepley     }
1594cfb853baSMatthew G. Knepley     PetscCall(DMPlexTensorPointLexicographic_Private(dim, vertices, vert));
1595cfb853baSMatthew G. Knepley   }
1596cfb853baSMatthew G. Knepley   PetscCall(DMPlexStratify(dm));
1597cfb853baSMatthew G. Knepley   /* Build coordinates */
1598cfb853baSMatthew G. Knepley   PetscCall(DMGetCoordinateSection(dm, &coordSection));
1599cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetNumFields(coordSection, 1));
1600cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
1601cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices));
1602cfb853baSMatthew G. Knepley   for (v = numCells; v < numCells + numVertices; ++v) {
1603cfb853baSMatthew G. Knepley     PetscCall(PetscSectionSetDof(coordSection, v, dim));
1604cfb853baSMatthew G. Knepley     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
1605cfb853baSMatthew G. Knepley   }
1606cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetUp(coordSection));
1607cfb853baSMatthew G. Knepley   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
1608cfb853baSMatthew G. Knepley   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
1609cfb853baSMatthew G. Knepley   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
1610cfb853baSMatthew G. Knepley   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
1611cfb853baSMatthew G. Knepley   PetscCall(VecSetBlockSize(coordinates, dim));
1612cfb853baSMatthew G. Knepley   PetscCall(VecSetType(coordinates, VECSTANDARD));
1613cfb853baSMatthew G. Knepley   PetscCall(VecGetArray(coordinates, &coords));
1614cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) vert[d] = 0;
1615cfb853baSMatthew G. Knepley   while (vert[dim - 1] < vertices[dim - 1]) {
1616cfb853baSMatthew G. Knepley     const PetscInt vertex = TupleToIndex_Private(dim, vertices, vert);
1617cfb853baSMatthew G. Knepley 
1618cfb853baSMatthew G. Knepley     for (d = 0; d < dim; ++d) coords[vertex * dim + d] = lower[d] + ((upper[d] - lower[d]) / vertices[d]) * vert[d];
16193ba16761SJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "Vertex %" PetscInt_FMT ":", vertex));
16203ba16761SJacob Faibussowitsch     for (d = 0; d < dim; ++d) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, vert[d]));
16213ba16761SJacob Faibussowitsch     for (d = 0; d < dim; ++d) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %g", (double)PetscRealPart(coords[vertex * dim + d])));
16223ba16761SJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
1623cfb853baSMatthew G. Knepley     PetscCall(DMPlexTensorPointLexicographic_Private(dim, vertices, vert));
1624cfb853baSMatthew G. Knepley   }
1625cfb853baSMatthew G. Knepley   PetscCall(VecRestoreArray(coordinates, &coords));
1626cfb853baSMatthew G. Knepley   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
1627cfb853baSMatthew G. Knepley   PetscCall(VecDestroy(&coordinates));
1628cfb853baSMatthew G. Knepley   PetscCall(PetscFree4(vertices, vert, vtmp, supp));
1629cfb853baSMatthew G. Knepley   //PetscCall(DMSetPeriodicity(dm, NULL, lower, upper));
1630cfb853baSMatthew G. Knepley   // Attach the extent
1631cfb853baSMatthew G. Knepley   {
1632cfb853baSMatthew G. Knepley     PetscContainer c;
1633cfb853baSMatthew G. Knepley     PetscInt      *extent;
1634cfb853baSMatthew G. Knepley 
1635cfb853baSMatthew G. Knepley     PetscCall(PetscMalloc1(dim, &extent));
1636cfb853baSMatthew G. Knepley     for (PetscInt d = 0; d < dim; ++d) extent[d] = edges[d];
1637cfb853baSMatthew G. Knepley     PetscCall(PetscContainerCreate(PETSC_COMM_SELF, &c));
1638cfb853baSMatthew G. Knepley     PetscCall(PetscContainerSetUserDestroy(c, DestroyExtent_Private));
1639cfb853baSMatthew G. Knepley     PetscCall(PetscContainerSetPointer(c, extent));
1640cfb853baSMatthew G. Knepley     PetscCall(PetscObjectCompose((PetscObject)dm, "_extent", (PetscObject)c));
1641cfb853baSMatthew G. Knepley     PetscCall(PetscContainerDestroy(&c));
1642cfb853baSMatthew G. Knepley   }
16433ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1644cfb853baSMatthew G. Knepley }
1645cfb853baSMatthew G. Knepley 
1646cfb853baSMatthew G. Knepley /*@C
1647aaa8cc7dSPierre Jolivet   DMPlexCreateHypercubicMesh - Creates a periodic mesh on the tensor product of unit intervals using only vertices and edges.
1648cfb853baSMatthew G. Knepley 
1649cfb853baSMatthew G. Knepley   Collective
1650cfb853baSMatthew G. Knepley 
1651cfb853baSMatthew G. Knepley   Input Parameters:
1652cfb853baSMatthew G. Knepley + comm  - The communicator for the DM object
1653cfb853baSMatthew G. Knepley . dim   - The spatial dimension
165420f4b53cSBarry Smith . edges - Number of edges per dimension, or `NULL` for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
165520f4b53cSBarry Smith . lower - The lower left corner, or `NULL` for (0, 0, 0)
165620f4b53cSBarry Smith - upper - The upper right corner, or `NULL` for (1, 1, 1)
1657cfb853baSMatthew G. Knepley 
1658cfb853baSMatthew G. Knepley   Output Parameter:
1659cfb853baSMatthew G. Knepley . dm - The DM object
1660cfb853baSMatthew G. Knepley 
166120f4b53cSBarry Smith   Level: beginner
166220f4b53cSBarry Smith 
166320f4b53cSBarry Smith   Note:
166420f4b53cSBarry Smith   If you want to customize this mesh using options, you just need to
166520f4b53cSBarry Smith .vb
166620f4b53cSBarry Smith   DMCreate(comm, &dm);
166720f4b53cSBarry Smith   DMSetType(dm, DMPLEX);
166820f4b53cSBarry Smith   DMSetFromOptions(dm);
166920f4b53cSBarry Smith .ve
167020f4b53cSBarry Smith   and use the options on the `DMSetFromOptions()` page.
1671cfb853baSMatthew G. Knepley 
1672cfb853baSMatthew G. Knepley   The vertices are numbered is lexicographic order, and the dim edges exiting a vertex in the positive orthant are number consecutively,
167320f4b53cSBarry Smith .vb
167420f4b53cSBarry Smith  18--0-19--2-20--4-18
167520f4b53cSBarry Smith   |     |     |     |
167620f4b53cSBarry Smith  13    15    17    13
167720f4b53cSBarry Smith   |     |     |     |
167820f4b53cSBarry Smith  24-12-25-14-26-16-24
167920f4b53cSBarry Smith   |     |     |     |
168020f4b53cSBarry Smith   7     9    11     7
168120f4b53cSBarry Smith   |     |     |     |
168220f4b53cSBarry Smith  21--6-22--8-23-10-21
168320f4b53cSBarry Smith   |     |     |     |
168420f4b53cSBarry Smith   1     3     5     1
168520f4b53cSBarry Smith   |     |     |     |
168620f4b53cSBarry Smith  18--0-19--2-20--4-18
168720f4b53cSBarry Smith .ve
1688cfb853baSMatthew G. Knepley 
168976fbde31SPierre Jolivet .seealso: `DMSetFromOptions()`, `DMPlexCreateFromFile()`, `DMPlexCreateHexCylinderMesh()`, `DMSetType()`, `DMCreate()`
1690cfb853baSMatthew G. Knepley @*/
1691cfb853baSMatthew G. Knepley PetscErrorCode DMPlexCreateHypercubicMesh(MPI_Comm comm, PetscInt dim, const PetscInt edges[], const PetscReal lower[], const PetscReal upper[], DM *dm)
1692cfb853baSMatthew G. Knepley {
1693cfb853baSMatthew G. Knepley   PetscInt       *edg;
1694cfb853baSMatthew G. Knepley   PetscReal      *low, *upp;
1695cfb853baSMatthew G. Knepley   DMBoundaryType *bdt;
1696cfb853baSMatthew G. Knepley   PetscInt        d;
1697cfb853baSMatthew G. Knepley 
1698cfb853baSMatthew G. Knepley   PetscFunctionBegin;
1699cfb853baSMatthew G. Knepley   PetscCall(DMCreate(comm, dm));
1700cfb853baSMatthew G. Knepley   PetscCall(DMSetType(*dm, DMPLEX));
1701cfb853baSMatthew G. Knepley   PetscCall(PetscMalloc4(dim, &edg, dim, &low, dim, &upp, dim, &bdt));
1702cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) {
1703cfb853baSMatthew G. Knepley     edg[d] = edges ? edges[d] : 1;
1704cfb853baSMatthew G. Knepley     low[d] = lower ? lower[d] : 0.;
1705cfb853baSMatthew G. Knepley     upp[d] = upper ? upper[d] : 1.;
1706cfb853baSMatthew G. Knepley     bdt[d] = DM_BOUNDARY_PERIODIC;
1707cfb853baSMatthew G. Knepley   }
1708cfb853baSMatthew G. Knepley   PetscCall(DMPlexCreateHypercubicMesh_Internal(*dm, dim, low, upp, edg, bdt));
1709cfb853baSMatthew G. Knepley   PetscCall(PetscFree4(edg, low, upp, bdt));
17103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1711cfb853baSMatthew G. Knepley }
1712cfb853baSMatthew G. Knepley 
1713a9074c1eSMatthew G. Knepley /*@C
1714a1cb98faSBarry Smith   DMPlexSetOptionsPrefix - Sets the prefix used for searching for all `DM` options in the database.
1715a9074c1eSMatthew G. Knepley 
171620f4b53cSBarry Smith   Logically Collective
1717a9074c1eSMatthew G. Knepley 
1718a9074c1eSMatthew G. Knepley   Input Parameters:
171920f4b53cSBarry Smith + dm     - the `DM` context
1720a9074c1eSMatthew G. Knepley - prefix - the prefix to prepend to all option names
1721a9074c1eSMatthew G. Knepley 
1722a1cb98faSBarry Smith   Level: advanced
1723a1cb98faSBarry Smith 
1724a1cb98faSBarry Smith   Note:
1725a9074c1eSMatthew G. Knepley   A hyphen (-) must NOT be given at the beginning of the prefix name.
1726a9074c1eSMatthew G. Knepley   The first character of all runtime options is AUTOMATICALLY the hyphen.
1727a9074c1eSMatthew G. Knepley 
17281cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `SNESSetFromOptions()`
1729a9074c1eSMatthew G. Knepley @*/
1730d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetOptionsPrefix(DM dm, const char prefix[])
1731d71ae5a4SJacob Faibussowitsch {
1732a9074c1eSMatthew G. Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
1733a9074c1eSMatthew G. Knepley 
1734a9074c1eSMatthew G. Knepley   PetscFunctionBegin;
1735a9074c1eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17369566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, prefix));
17379566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)mesh->partitioner, prefix));
17383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1739a9074c1eSMatthew G. Knepley }
1740a9074c1eSMatthew G. Knepley 
17419318fe57SMatthew G. Knepley /* Remap geometry to cylinder
174261a622f3SMatthew G. Knepley    TODO: This only works for a single refinement, then it is broken
174361a622f3SMatthew G. Knepley 
17449318fe57SMatthew G. Knepley      Interior square: Linear interpolation is correct
17459318fe57SMatthew G. Knepley      The other cells all have vertices on rays from the origin. We want to uniformly expand the spacing
17469318fe57SMatthew G. Knepley      such that the last vertex is on the unit circle. So the closest and farthest vertices are at distance
17470510c589SMatthew G. Knepley 
17489318fe57SMatthew G. Knepley        phi     = arctan(y/x)
17499318fe57SMatthew G. Knepley        d_close = sqrt(1/8 + 1/4 sin^2(phi))
17509318fe57SMatthew G. Knepley        d_far   = sqrt(1/2 + sin^2(phi))
17510510c589SMatthew G. Knepley 
17529318fe57SMatthew G. Knepley      so we remap them using
17530510c589SMatthew G. Knepley 
17549318fe57SMatthew G. Knepley        x_new = x_close + (x - x_close) (1 - d_close) / (d_far - d_close)
17559318fe57SMatthew G. Knepley        y_new = y_close + (y - y_close) (1 - d_close) / (d_far - d_close)
17560510c589SMatthew G. Knepley 
17579318fe57SMatthew G. Knepley      If pi/4 < phi < 3pi/4 or -3pi/4 < phi < -pi/4, then we switch x and y.
17589318fe57SMatthew G. Knepley */
1759d71ae5a4SJacob 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[])
1760d71ae5a4SJacob Faibussowitsch {
17619318fe57SMatthew G. Knepley   const PetscReal dis = 1.0 / PetscSqrtReal(2.0);
17629318fe57SMatthew G. Knepley   const PetscReal ds2 = 0.5 * dis;
176322cc497dSMatthew G. Knepley 
17649318fe57SMatthew G. Knepley   if ((PetscAbsScalar(u[0]) <= ds2) && (PetscAbsScalar(u[1]) <= ds2)) {
17659318fe57SMatthew G. Knepley     f0[0] = u[0];
17669318fe57SMatthew G. Knepley     f0[1] = u[1];
17679318fe57SMatthew G. Knepley   } else {
17689318fe57SMatthew G. Knepley     PetscReal phi, sinp, cosp, dc, df, x, y, xc, yc;
17690510c589SMatthew G. Knepley 
17709318fe57SMatthew G. Knepley     x    = PetscRealPart(u[0]);
17719318fe57SMatthew G. Knepley     y    = PetscRealPart(u[1]);
17729318fe57SMatthew G. Knepley     phi  = PetscAtan2Real(y, x);
17739318fe57SMatthew G. Knepley     sinp = PetscSinReal(phi);
17749318fe57SMatthew G. Knepley     cosp = PetscCosReal(phi);
17759318fe57SMatthew G. Knepley     if ((PetscAbsReal(phi) > PETSC_PI / 4.0) && (PetscAbsReal(phi) < 3.0 * PETSC_PI / 4.0)) {
17769318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2 / sinp);
17779318fe57SMatthew G. Knepley       df = PetscAbsReal(dis / sinp);
17789318fe57SMatthew G. Knepley       xc = ds2 * x / PetscAbsReal(y);
17799318fe57SMatthew G. Knepley       yc = ds2 * PetscSignReal(y);
17809318fe57SMatthew G. Knepley     } else {
17819318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2 / cosp);
17829318fe57SMatthew G. Knepley       df = PetscAbsReal(dis / cosp);
17839318fe57SMatthew G. Knepley       xc = ds2 * PetscSignReal(x);
17849318fe57SMatthew G. Knepley       yc = ds2 * y / PetscAbsReal(x);
17859318fe57SMatthew G. Knepley     }
17869318fe57SMatthew G. Knepley     f0[0] = xc + (u[0] - xc) * (1.0 - dc) / (df - dc);
17879318fe57SMatthew G. Knepley     f0[1] = yc + (u[1] - yc) * (1.0 - dc) / (df - dc);
17889318fe57SMatthew G. Knepley   }
17899318fe57SMatthew G. Knepley   f0[2] = u[2];
17909318fe57SMatthew G. Knepley }
17910510c589SMatthew G. Knepley 
1792d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateHexCylinderMesh_Internal(DM dm, DMBoundaryType periodicZ)
1793d71ae5a4SJacob Faibussowitsch {
17940510c589SMatthew G. Knepley   const PetscInt dim = 3;
17959318fe57SMatthew G. Knepley   PetscInt       numCells, numVertices;
1796d8c47e87SMatthew G. Knepley   PetscMPIInt    rank;
17970510c589SMatthew G. Knepley 
17980510c589SMatthew G. Knepley   PetscFunctionBegin;
179946139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
18009566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
18019566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
18020510c589SMatthew G. Knepley   /* Create topology */
18030510c589SMatthew G. Knepley   {
18040510c589SMatthew G. Knepley     PetscInt cone[8], c;
18050510c589SMatthew G. Knepley 
1806dd400576SPatrick Sanan     numCells    = rank == 0 ? 5 : 0;
1807dd400576SPatrick Sanan     numVertices = rank == 0 ? 16 : 0;
1808006a8963SMatthew G. Knepley     if (periodicZ == DM_BOUNDARY_PERIODIC) {
1809ae8bcbbbSMatthew G. Knepley       numCells *= 3;
1810dd400576SPatrick Sanan       numVertices = rank == 0 ? 24 : 0;
1811006a8963SMatthew G. Knepley     }
18129566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
18139566063dSJacob Faibussowitsch     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 8));
18149566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm));
1815dd400576SPatrick Sanan     if (rank == 0) {
1816006a8963SMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
18179371c9d4SSatish Balay         cone[0] = 15;
18189371c9d4SSatish Balay         cone[1] = 18;
18199371c9d4SSatish Balay         cone[2] = 17;
18209371c9d4SSatish Balay         cone[3] = 16;
18219371c9d4SSatish Balay         cone[4] = 31;
18229371c9d4SSatish Balay         cone[5] = 32;
18239371c9d4SSatish Balay         cone[6] = 33;
18249371c9d4SSatish Balay         cone[7] = 34;
18259566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
18269371c9d4SSatish Balay         cone[0] = 16;
18279371c9d4SSatish Balay         cone[1] = 17;
18289371c9d4SSatish Balay         cone[2] = 24;
18299371c9d4SSatish Balay         cone[3] = 23;
18309371c9d4SSatish Balay         cone[4] = 32;
18319371c9d4SSatish Balay         cone[5] = 36;
18329371c9d4SSatish Balay         cone[6] = 37;
18339371c9d4SSatish Balay         cone[7] = 33; /* 22 25 26 21 */
18349566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
18359371c9d4SSatish Balay         cone[0] = 18;
18369371c9d4SSatish Balay         cone[1] = 27;
18379371c9d4SSatish Balay         cone[2] = 24;
18389371c9d4SSatish Balay         cone[3] = 17;
18399371c9d4SSatish Balay         cone[4] = 34;
18409371c9d4SSatish Balay         cone[5] = 33;
18419371c9d4SSatish Balay         cone[6] = 37;
18429371c9d4SSatish Balay         cone[7] = 38;
18439566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
18449371c9d4SSatish Balay         cone[0] = 29;
18459371c9d4SSatish Balay         cone[1] = 27;
18469371c9d4SSatish Balay         cone[2] = 18;
18479371c9d4SSatish Balay         cone[3] = 15;
18489371c9d4SSatish Balay         cone[4] = 35;
18499371c9d4SSatish Balay         cone[5] = 31;
18509371c9d4SSatish Balay         cone[6] = 34;
18519371c9d4SSatish Balay         cone[7] = 38;
18529566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
18539371c9d4SSatish Balay         cone[0] = 29;
18549371c9d4SSatish Balay         cone[1] = 15;
18559371c9d4SSatish Balay         cone[2] = 16;
18569371c9d4SSatish Balay         cone[3] = 23;
18579371c9d4SSatish Balay         cone[4] = 35;
18589371c9d4SSatish Balay         cone[5] = 36;
18599371c9d4SSatish Balay         cone[6] = 32;
18609371c9d4SSatish Balay         cone[7] = 31;
18619566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
1862006a8963SMatthew G. Knepley 
18639371c9d4SSatish Balay         cone[0] = 31;
18649371c9d4SSatish Balay         cone[1] = 34;
18659371c9d4SSatish Balay         cone[2] = 33;
18669371c9d4SSatish Balay         cone[3] = 32;
18679371c9d4SSatish Balay         cone[4] = 19;
18689371c9d4SSatish Balay         cone[5] = 22;
18699371c9d4SSatish Balay         cone[6] = 21;
18709371c9d4SSatish Balay         cone[7] = 20;
18719566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 5, cone));
18729371c9d4SSatish Balay         cone[0] = 32;
18739371c9d4SSatish Balay         cone[1] = 33;
18749371c9d4SSatish Balay         cone[2] = 37;
18759371c9d4SSatish Balay         cone[3] = 36;
18769371c9d4SSatish Balay         cone[4] = 22;
18779371c9d4SSatish Balay         cone[5] = 25;
18789371c9d4SSatish Balay         cone[6] = 26;
18799371c9d4SSatish Balay         cone[7] = 21;
18809566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 6, cone));
18819371c9d4SSatish Balay         cone[0] = 34;
18829371c9d4SSatish Balay         cone[1] = 38;
18839371c9d4SSatish Balay         cone[2] = 37;
18849371c9d4SSatish Balay         cone[3] = 33;
18859371c9d4SSatish Balay         cone[4] = 20;
18869371c9d4SSatish Balay         cone[5] = 21;
18879371c9d4SSatish Balay         cone[6] = 26;
18889371c9d4SSatish Balay         cone[7] = 28;
18899566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 7, cone));
18909371c9d4SSatish Balay         cone[0] = 35;
18919371c9d4SSatish Balay         cone[1] = 38;
18929371c9d4SSatish Balay         cone[2] = 34;
18939371c9d4SSatish Balay         cone[3] = 31;
18949371c9d4SSatish Balay         cone[4] = 30;
18959371c9d4SSatish Balay         cone[5] = 19;
18969371c9d4SSatish Balay         cone[6] = 20;
18979371c9d4SSatish Balay         cone[7] = 28;
18989566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 8, cone));
18999371c9d4SSatish Balay         cone[0] = 35;
19009371c9d4SSatish Balay         cone[1] = 31;
19019371c9d4SSatish Balay         cone[2] = 32;
19029371c9d4SSatish Balay         cone[3] = 36;
19039371c9d4SSatish Balay         cone[4] = 30;
19049371c9d4SSatish Balay         cone[5] = 25;
19059371c9d4SSatish Balay         cone[6] = 22;
19069371c9d4SSatish Balay         cone[7] = 19;
19079566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 9, cone));
1908ae8bcbbbSMatthew G. Knepley 
19099371c9d4SSatish Balay         cone[0] = 19;
19109371c9d4SSatish Balay         cone[1] = 20;
19119371c9d4SSatish Balay         cone[2] = 21;
19129371c9d4SSatish Balay         cone[3] = 22;
19139371c9d4SSatish Balay         cone[4] = 15;
19149371c9d4SSatish Balay         cone[5] = 16;
19159371c9d4SSatish Balay         cone[6] = 17;
19169371c9d4SSatish Balay         cone[7] = 18;
19179566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 10, cone));
19189371c9d4SSatish Balay         cone[0] = 22;
19199371c9d4SSatish Balay         cone[1] = 21;
19209371c9d4SSatish Balay         cone[2] = 26;
19219371c9d4SSatish Balay         cone[3] = 25;
19229371c9d4SSatish Balay         cone[4] = 16;
19239371c9d4SSatish Balay         cone[5] = 23;
19249371c9d4SSatish Balay         cone[6] = 24;
19259371c9d4SSatish Balay         cone[7] = 17;
19269566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 11, cone));
19279371c9d4SSatish Balay         cone[0] = 20;
19289371c9d4SSatish Balay         cone[1] = 28;
19299371c9d4SSatish Balay         cone[2] = 26;
19309371c9d4SSatish Balay         cone[3] = 21;
19319371c9d4SSatish Balay         cone[4] = 18;
19329371c9d4SSatish Balay         cone[5] = 17;
19339371c9d4SSatish Balay         cone[6] = 24;
19349371c9d4SSatish Balay         cone[7] = 27;
19359566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 12, cone));
19369371c9d4SSatish Balay         cone[0] = 30;
19379371c9d4SSatish Balay         cone[1] = 28;
19389371c9d4SSatish Balay         cone[2] = 20;
19399371c9d4SSatish Balay         cone[3] = 19;
19409371c9d4SSatish Balay         cone[4] = 29;
19419371c9d4SSatish Balay         cone[5] = 15;
19429371c9d4SSatish Balay         cone[6] = 18;
19439371c9d4SSatish Balay         cone[7] = 27;
19449566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 13, cone));
19459371c9d4SSatish Balay         cone[0] = 30;
19469371c9d4SSatish Balay         cone[1] = 19;
19479371c9d4SSatish Balay         cone[2] = 22;
19489371c9d4SSatish Balay         cone[3] = 25;
19499371c9d4SSatish Balay         cone[4] = 29;
19509371c9d4SSatish Balay         cone[5] = 23;
19519371c9d4SSatish Balay         cone[6] = 16;
19529371c9d4SSatish Balay         cone[7] = 15;
19539566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 14, cone));
1954006a8963SMatthew G. Knepley       } else {
19559371c9d4SSatish Balay         cone[0] = 5;
19569371c9d4SSatish Balay         cone[1] = 8;
19579371c9d4SSatish Balay         cone[2] = 7;
19589371c9d4SSatish Balay         cone[3] = 6;
19599371c9d4SSatish Balay         cone[4] = 9;
19609371c9d4SSatish Balay         cone[5] = 12;
19619371c9d4SSatish Balay         cone[6] = 11;
19629371c9d4SSatish Balay         cone[7] = 10;
19639566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
19649371c9d4SSatish Balay         cone[0] = 6;
19659371c9d4SSatish Balay         cone[1] = 7;
19669371c9d4SSatish Balay         cone[2] = 14;
19679371c9d4SSatish Balay         cone[3] = 13;
19689371c9d4SSatish Balay         cone[4] = 12;
19699371c9d4SSatish Balay         cone[5] = 15;
19709371c9d4SSatish Balay         cone[6] = 16;
19719371c9d4SSatish Balay         cone[7] = 11;
19729566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
19739371c9d4SSatish Balay         cone[0] = 8;
19749371c9d4SSatish Balay         cone[1] = 17;
19759371c9d4SSatish Balay         cone[2] = 14;
19769371c9d4SSatish Balay         cone[3] = 7;
19779371c9d4SSatish Balay         cone[4] = 10;
19789371c9d4SSatish Balay         cone[5] = 11;
19799371c9d4SSatish Balay         cone[6] = 16;
19809371c9d4SSatish Balay         cone[7] = 18;
19819566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
19829371c9d4SSatish Balay         cone[0] = 19;
19839371c9d4SSatish Balay         cone[1] = 17;
19849371c9d4SSatish Balay         cone[2] = 8;
19859371c9d4SSatish Balay         cone[3] = 5;
19869371c9d4SSatish Balay         cone[4] = 20;
19879371c9d4SSatish Balay         cone[5] = 9;
19889371c9d4SSatish Balay         cone[6] = 10;
19899371c9d4SSatish Balay         cone[7] = 18;
19909566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
19919371c9d4SSatish Balay         cone[0] = 19;
19929371c9d4SSatish Balay         cone[1] = 5;
19939371c9d4SSatish Balay         cone[2] = 6;
19949371c9d4SSatish Balay         cone[3] = 13;
19959371c9d4SSatish Balay         cone[4] = 20;
19969371c9d4SSatish Balay         cone[5] = 15;
19979371c9d4SSatish Balay         cone[6] = 12;
19989371c9d4SSatish Balay         cone[7] = 9;
19999566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
2000006a8963SMatthew G. Knepley       }
2001d8c47e87SMatthew G. Knepley     }
20029566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
20039566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
20040510c589SMatthew G. Knepley   }
2005dbc1dc17SMatthew G. Knepley   /* Create cube geometry */
20060510c589SMatthew G. Knepley   {
20070510c589SMatthew G. Knepley     Vec             coordinates;
20080510c589SMatthew G. Knepley     PetscSection    coordSection;
20090510c589SMatthew G. Knepley     PetscScalar    *coords;
20100510c589SMatthew G. Knepley     PetscInt        coordSize, v;
20110510c589SMatthew G. Knepley     const PetscReal dis = 1.0 / PetscSqrtReal(2.0);
20120510c589SMatthew G. Knepley     const PetscReal ds2 = dis / 2.0;
20130510c589SMatthew G. Knepley 
20140510c589SMatthew G. Knepley     /* Build coordinates */
20159566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
20169566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
20179566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
20189566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices));
20190510c589SMatthew G. Knepley     for (v = numCells; v < numCells + numVertices; ++v) {
20209566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
20219566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
20220510c589SMatthew G. Knepley     }
20239566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
20249566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
20259566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
20269566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
20279566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
20289566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
20299566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates, VECSTANDARD));
20309566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
2031dd400576SPatrick Sanan     if (rank == 0) {
20329371c9d4SSatish Balay       coords[0 * dim + 0]  = -ds2;
20339371c9d4SSatish Balay       coords[0 * dim + 1]  = -ds2;
20349371c9d4SSatish Balay       coords[0 * dim + 2]  = 0.0;
20359371c9d4SSatish Balay       coords[1 * dim + 0]  = ds2;
20369371c9d4SSatish Balay       coords[1 * dim + 1]  = -ds2;
20379371c9d4SSatish Balay       coords[1 * dim + 2]  = 0.0;
20389371c9d4SSatish Balay       coords[2 * dim + 0]  = ds2;
20399371c9d4SSatish Balay       coords[2 * dim + 1]  = ds2;
20409371c9d4SSatish Balay       coords[2 * dim + 2]  = 0.0;
20419371c9d4SSatish Balay       coords[3 * dim + 0]  = -ds2;
20429371c9d4SSatish Balay       coords[3 * dim + 1]  = ds2;
20439371c9d4SSatish Balay       coords[3 * dim + 2]  = 0.0;
20449371c9d4SSatish Balay       coords[4 * dim + 0]  = -ds2;
20459371c9d4SSatish Balay       coords[4 * dim + 1]  = -ds2;
20469371c9d4SSatish Balay       coords[4 * dim + 2]  = 1.0;
20479371c9d4SSatish Balay       coords[5 * dim + 0]  = -ds2;
20489371c9d4SSatish Balay       coords[5 * dim + 1]  = ds2;
20499371c9d4SSatish Balay       coords[5 * dim + 2]  = 1.0;
20509371c9d4SSatish Balay       coords[6 * dim + 0]  = ds2;
20519371c9d4SSatish Balay       coords[6 * dim + 1]  = ds2;
20529371c9d4SSatish Balay       coords[6 * dim + 2]  = 1.0;
20539371c9d4SSatish Balay       coords[7 * dim + 0]  = ds2;
20549371c9d4SSatish Balay       coords[7 * dim + 1]  = -ds2;
20559371c9d4SSatish Balay       coords[7 * dim + 2]  = 1.0;
20569371c9d4SSatish Balay       coords[8 * dim + 0]  = dis;
20579371c9d4SSatish Balay       coords[8 * dim + 1]  = -dis;
20589371c9d4SSatish Balay       coords[8 * dim + 2]  = 0.0;
20599371c9d4SSatish Balay       coords[9 * dim + 0]  = dis;
20609371c9d4SSatish Balay       coords[9 * dim + 1]  = dis;
20619371c9d4SSatish Balay       coords[9 * dim + 2]  = 0.0;
20629371c9d4SSatish Balay       coords[10 * dim + 0] = dis;
20639371c9d4SSatish Balay       coords[10 * dim + 1] = -dis;
20649371c9d4SSatish Balay       coords[10 * dim + 2] = 1.0;
20659371c9d4SSatish Balay       coords[11 * dim + 0] = dis;
20669371c9d4SSatish Balay       coords[11 * dim + 1] = dis;
20679371c9d4SSatish Balay       coords[11 * dim + 2] = 1.0;
20689371c9d4SSatish Balay       coords[12 * dim + 0] = -dis;
20699371c9d4SSatish Balay       coords[12 * dim + 1] = dis;
20709371c9d4SSatish Balay       coords[12 * dim + 2] = 0.0;
20719371c9d4SSatish Balay       coords[13 * dim + 0] = -dis;
20729371c9d4SSatish Balay       coords[13 * dim + 1] = dis;
20739371c9d4SSatish Balay       coords[13 * dim + 2] = 1.0;
20749371c9d4SSatish Balay       coords[14 * dim + 0] = -dis;
20759371c9d4SSatish Balay       coords[14 * dim + 1] = -dis;
20769371c9d4SSatish Balay       coords[14 * dim + 2] = 0.0;
20779371c9d4SSatish Balay       coords[15 * dim + 0] = -dis;
20789371c9d4SSatish Balay       coords[15 * dim + 1] = -dis;
20799371c9d4SSatish Balay       coords[15 * dim + 2] = 1.0;
2080ae8bcbbbSMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
20819371c9d4SSatish Balay         /* 15 31 19 */ coords[16 * dim + 0] = -ds2;
20829371c9d4SSatish Balay         coords[16 * dim + 1]                = -ds2;
20839371c9d4SSatish Balay         coords[16 * dim + 2]                = 0.5;
20849371c9d4SSatish Balay         /* 16 32 22 */ coords[17 * dim + 0] = ds2;
20859371c9d4SSatish Balay         coords[17 * dim + 1]                = -ds2;
20869371c9d4SSatish Balay         coords[17 * dim + 2]                = 0.5;
20879371c9d4SSatish Balay         /* 17 33 21 */ coords[18 * dim + 0] = ds2;
20889371c9d4SSatish Balay         coords[18 * dim + 1]                = ds2;
20899371c9d4SSatish Balay         coords[18 * dim + 2]                = 0.5;
20909371c9d4SSatish Balay         /* 18 34 20 */ coords[19 * dim + 0] = -ds2;
20919371c9d4SSatish Balay         coords[19 * dim + 1]                = ds2;
20929371c9d4SSatish Balay         coords[19 * dim + 2]                = 0.5;
20939371c9d4SSatish Balay         /* 29 35 30 */ coords[20 * dim + 0] = -dis;
20949371c9d4SSatish Balay         coords[20 * dim + 1]                = -dis;
20959371c9d4SSatish Balay         coords[20 * dim + 2]                = 0.5;
20969371c9d4SSatish Balay         /* 23 36 25 */ coords[21 * dim + 0] = dis;
20979371c9d4SSatish Balay         coords[21 * dim + 1]                = -dis;
20989371c9d4SSatish Balay         coords[21 * dim + 2]                = 0.5;
20999371c9d4SSatish Balay         /* 24 37 26 */ coords[22 * dim + 0] = dis;
21009371c9d4SSatish Balay         coords[22 * dim + 1]                = dis;
21019371c9d4SSatish Balay         coords[22 * dim + 2]                = 0.5;
21029371c9d4SSatish Balay         /* 27 38 28 */ coords[23 * dim + 0] = -dis;
21039371c9d4SSatish Balay         coords[23 * dim + 1]                = dis;
21049371c9d4SSatish Balay         coords[23 * dim + 2]                = 0.5;
2105ae8bcbbbSMatthew G. Knepley       }
2106d8c47e87SMatthew G. Knepley     }
21079566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
21089566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
21099566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
21100510c589SMatthew G. Knepley   }
2111006a8963SMatthew G. Knepley   /* Create periodicity */
2112006a8963SMatthew G. Knepley   if (periodicZ == DM_BOUNDARY_PERIODIC || periodicZ == DM_BOUNDARY_TWIST) {
21136858538eSMatthew G. Knepley     PetscReal L[3]       = {-1., -1., 0.};
21146858538eSMatthew G. Knepley     PetscReal maxCell[3] = {-1., -1., 0.};
2115006a8963SMatthew G. Knepley     PetscReal lower[3]   = {0.0, 0.0, 0.0};
2116ae8bcbbbSMatthew G. Knepley     PetscReal upper[3]   = {1.0, 1.0, 1.5};
21176858538eSMatthew G. Knepley     PetscInt  numZCells  = 3;
2118006a8963SMatthew G. Knepley 
21196858538eSMatthew G. Knepley     L[2]       = upper[2] - lower[2];
21206858538eSMatthew G. Knepley     maxCell[2] = 1.1 * (L[2] / numZCells);
21214fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, maxCell, lower, L));
2122006a8963SMatthew G. Knepley   }
2123dbc1dc17SMatthew G. Knepley   {
21249318fe57SMatthew G. Knepley     DM          cdm;
21259318fe57SMatthew G. Knepley     PetscDS     cds;
21269318fe57SMatthew G. Knepley     PetscScalar c[2] = {1.0, 1.0};
2127dbc1dc17SMatthew G. Knepley 
2128e44f6aebSMatthew G. Knepley     PetscCall(DMPlexCreateCoordinateSpace(dm, 1, PETSC_TRUE, snapToCylinder));
21299566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
21309566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
21319566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, 2, c));
2132dbc1dc17SMatthew G. Knepley   }
213346139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
213446139095SJed Brown 
21359318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
21369566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolateInPlace_Internal(dm));
21373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21380510c589SMatthew G. Knepley }
21390510c589SMatthew G. Knepley 
214024119c2aSMatthew G. Knepley /*@
21419318fe57SMatthew G. Knepley   DMPlexCreateHexCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using hexahedra.
214224119c2aSMatthew G. Knepley 
2143d083f849SBarry Smith   Collective
214424119c2aSMatthew G. Knepley 
214524119c2aSMatthew G. Knepley   Input Parameters:
2146a1cb98faSBarry Smith + comm      - The communicator for the `DM` object
21479318fe57SMatthew G. Knepley - periodicZ - The boundary type for the Z direction
214824119c2aSMatthew G. Knepley 
214924119c2aSMatthew G. Knepley   Output Parameter:
215020f4b53cSBarry Smith . dm - The `DM` object
215124119c2aSMatthew G. Knepley 
215224119c2aSMatthew G. Knepley   Level: beginner
215324119c2aSMatthew G. Knepley 
2154a1cb98faSBarry Smith   Note:
2155a4e35b19SJacob Faibussowitsch   Here is the output numbering looking from the bottom of the cylinder\:
2156a1cb98faSBarry Smith .vb
2157a1cb98faSBarry Smith        17-----14
2158a1cb98faSBarry Smith         |     |
2159a1cb98faSBarry Smith         |  2  |
2160a1cb98faSBarry Smith         |     |
2161a1cb98faSBarry Smith  17-----8-----7-----14
2162a1cb98faSBarry Smith   |     |     |     |
2163a1cb98faSBarry Smith   |  3  |  0  |  1  |
2164a1cb98faSBarry Smith   |     |     |     |
2165a1cb98faSBarry Smith  19-----5-----6-----13
2166a1cb98faSBarry Smith         |     |
2167a1cb98faSBarry Smith         |  4  |
2168a1cb98faSBarry Smith         |     |
2169a1cb98faSBarry Smith        19-----13
2170a1cb98faSBarry Smith 
2171a1cb98faSBarry Smith  and up through the top
2172a1cb98faSBarry Smith 
2173a1cb98faSBarry Smith        18-----16
2174a1cb98faSBarry Smith         |     |
2175a1cb98faSBarry Smith         |  2  |
2176a1cb98faSBarry Smith         |     |
2177a1cb98faSBarry Smith  18----10----11-----16
2178a1cb98faSBarry Smith   |     |     |     |
2179a1cb98faSBarry Smith   |  3  |  0  |  1  |
2180a1cb98faSBarry Smith   |     |     |     |
2181a1cb98faSBarry Smith  20-----9----12-----15
2182a1cb98faSBarry Smith         |     |
2183a1cb98faSBarry Smith         |  4  |
2184a1cb98faSBarry Smith         |     |
2185a1cb98faSBarry Smith        20-----15
2186a1cb98faSBarry Smith .ve
2187a1cb98faSBarry Smith 
21881cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
218924119c2aSMatthew G. Knepley @*/
2190d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateHexCylinderMesh(MPI_Comm comm, DMBoundaryType periodicZ, DM *dm)
2191d71ae5a4SJacob Faibussowitsch {
21929318fe57SMatthew G. Knepley   PetscFunctionBegin;
21934f572ea9SToby Isaac   PetscAssertPointer(dm, 3);
21949566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
21959566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
21969566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateHexCylinderMesh_Internal(*dm, periodicZ));
21973ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21989318fe57SMatthew G. Knepley }
21999318fe57SMatthew G. Knepley 
2200d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateWedgeCylinderMesh_Internal(DM dm, PetscInt n, PetscBool interpolate)
2201d71ae5a4SJacob Faibussowitsch {
220224119c2aSMatthew G. Knepley   const PetscInt dim = 3;
2203412e9a14SMatthew G. Knepley   PetscInt       numCells, numVertices, v;
22049fe9f049SMatthew G. Knepley   PetscMPIInt    rank;
220524119c2aSMatthew G. Knepley 
220624119c2aSMatthew G. Knepley   PetscFunctionBegin;
220763a3b9bcSJacob Faibussowitsch   PetscCheck(n >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of wedges %" PetscInt_FMT " cannot be negative", n);
220846139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
22099566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
22109566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
2211412e9a14SMatthew G. Knepley   /* Must create the celltype label here so that we do not automatically try to compute the types */
22129566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "celltype"));
221324119c2aSMatthew G. Knepley   /* Create topology */
221424119c2aSMatthew G. Knepley   {
221524119c2aSMatthew G. Knepley     PetscInt cone[6], c;
221624119c2aSMatthew G. Knepley 
2217dd400576SPatrick Sanan     numCells    = rank == 0 ? n : 0;
2218dd400576SPatrick Sanan     numVertices = rank == 0 ? 2 * (n + 1) : 0;
22199566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
22209566063dSJacob Faibussowitsch     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 6));
22219566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm));
222224119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
22239371c9d4SSatish Balay       cone[0] = c + n * 1;
22249371c9d4SSatish Balay       cone[1] = (c + 1) % n + n * 1;
22259371c9d4SSatish Balay       cone[2] = 0 + 3 * n;
22269371c9d4SSatish Balay       cone[3] = c + n * 2;
22279371c9d4SSatish Balay       cone[4] = (c + 1) % n + n * 2;
22289371c9d4SSatish Balay       cone[5] = 1 + 3 * n;
22299566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(dm, c, cone));
22309566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(dm, c, DM_POLYTOPE_TRI_PRISM_TENSOR));
223124119c2aSMatthew G. Knepley     }
22329566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
22339566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
223424119c2aSMatthew G. Knepley   }
223548a46eb9SPierre Jolivet   for (v = numCells; v < numCells + numVertices; ++v) PetscCall(DMPlexSetCellType(dm, v, DM_POLYTOPE_POINT));
223624119c2aSMatthew G. Knepley   /* Create cylinder geometry */
223724119c2aSMatthew G. Knepley   {
223824119c2aSMatthew G. Knepley     Vec          coordinates;
223924119c2aSMatthew G. Knepley     PetscSection coordSection;
224024119c2aSMatthew G. Knepley     PetscScalar *coords;
2241412e9a14SMatthew G. Knepley     PetscInt     coordSize, c;
224224119c2aSMatthew G. Knepley 
224324119c2aSMatthew G. Knepley     /* Build coordinates */
22449566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
22459566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
22469566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
22479566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices));
224824119c2aSMatthew G. Knepley     for (v = numCells; v < numCells + numVertices; ++v) {
22499566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
22509566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
225124119c2aSMatthew G. Knepley     }
22529566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
22539566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
22549566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
22559566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
22569566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
22579566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
22589566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates, VECSTANDARD));
22599566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
226024119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
22619371c9d4SSatish Balay       coords[(c + 0 * n) * dim + 0] = PetscCosReal(2.0 * c * PETSC_PI / n);
22629371c9d4SSatish Balay       coords[(c + 0 * n) * dim + 1] = PetscSinReal(2.0 * c * PETSC_PI / n);
22639371c9d4SSatish Balay       coords[(c + 0 * n) * dim + 2] = 1.0;
22649371c9d4SSatish Balay       coords[(c + 1 * n) * dim + 0] = PetscCosReal(2.0 * c * PETSC_PI / n);
22659371c9d4SSatish Balay       coords[(c + 1 * n) * dim + 1] = PetscSinReal(2.0 * c * PETSC_PI / n);
22669371c9d4SSatish Balay       coords[(c + 1 * n) * dim + 2] = 0.0;
226724119c2aSMatthew G. Knepley     }
2268dd400576SPatrick Sanan     if (rank == 0) {
22699371c9d4SSatish Balay       coords[(2 * n + 0) * dim + 0] = 0.0;
22709371c9d4SSatish Balay       coords[(2 * n + 0) * dim + 1] = 0.0;
22719371c9d4SSatish Balay       coords[(2 * n + 0) * dim + 2] = 1.0;
22729371c9d4SSatish Balay       coords[(2 * n + 1) * dim + 0] = 0.0;
22739371c9d4SSatish Balay       coords[(2 * n + 1) * dim + 1] = 0.0;
22749371c9d4SSatish Balay       coords[(2 * n + 1) * dim + 2] = 0.0;
22759fe9f049SMatthew G. Knepley     }
22769566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
22779566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
22789566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
227924119c2aSMatthew G. Knepley   }
228046139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
22819318fe57SMatthew G. Knepley   /* Interpolate */
22829566063dSJacob Faibussowitsch   if (interpolate) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
22833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
22849318fe57SMatthew G. Knepley }
22859318fe57SMatthew G. Knepley 
22869318fe57SMatthew G. Knepley /*@
22879318fe57SMatthew G. Knepley   DMPlexCreateWedgeCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using wedges.
22889318fe57SMatthew G. Knepley 
22899318fe57SMatthew G. Knepley   Collective
22909318fe57SMatthew G. Knepley 
22919318fe57SMatthew G. Knepley   Input Parameters:
2292a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
22939318fe57SMatthew G. Knepley . n           - The number of wedges around the origin
22949318fe57SMatthew G. Knepley - interpolate - Create edges and faces
22959318fe57SMatthew G. Knepley 
22969318fe57SMatthew G. Knepley   Output Parameter:
2297a1cb98faSBarry Smith . dm - The `DM` object
22989318fe57SMatthew G. Knepley 
22999318fe57SMatthew G. Knepley   Level: beginner
23009318fe57SMatthew G. Knepley 
23011cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateHexCylinderMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
23029318fe57SMatthew G. Knepley @*/
2303d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateWedgeCylinderMesh(MPI_Comm comm, PetscInt n, PetscBool interpolate, DM *dm)
2304d71ae5a4SJacob Faibussowitsch {
23059318fe57SMatthew G. Knepley   PetscFunctionBegin;
23064f572ea9SToby Isaac   PetscAssertPointer(dm, 4);
23079566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
23089566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
23099566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateWedgeCylinderMesh_Internal(*dm, n, interpolate));
23103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
231124119c2aSMatthew G. Knepley }
231224119c2aSMatthew G. Knepley 
2313d71ae5a4SJacob Faibussowitsch static inline PetscReal DiffNormReal(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 += PetscSqr(x[i] - y[i]);
231865a81367SMatthew G. Knepley   return PetscSqrtReal(prod);
231965a81367SMatthew G. Knepley }
2320*dd2b43ebSStefano Zampini 
2321d71ae5a4SJacob Faibussowitsch static inline PetscReal DotReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
2322d71ae5a4SJacob Faibussowitsch {
232365a81367SMatthew G. Knepley   PetscReal prod = 0.0;
232465a81367SMatthew G. Knepley   PetscInt  i;
232565a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += x[i] * y[i];
232665a81367SMatthew G. Knepley   return prod;
232765a81367SMatthew G. Knepley }
232865a81367SMatthew G. Knepley 
232951a74b61SMatthew G. Knepley /* The first constant is the sphere radius */
2330d71ae5a4SJacob 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[])
2331d71ae5a4SJacob Faibussowitsch {
233251a74b61SMatthew G. Knepley   PetscReal r     = PetscRealPart(constants[0]);
233351a74b61SMatthew G. Knepley   PetscReal norm2 = 0.0, fac;
233451a74b61SMatthew G. Knepley   PetscInt  n     = uOff[1] - uOff[0], d;
233551a74b61SMatthew G. Knepley 
233651a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) norm2 += PetscSqr(PetscRealPart(u[d]));
233751a74b61SMatthew G. Knepley   fac = r / PetscSqrtReal(norm2);
233851a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) f0[d] = u[d] * fac;
233951a74b61SMatthew G. Knepley }
234051a74b61SMatthew G. Knepley 
2341d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSphereMesh_Internal(DM dm, PetscInt dim, PetscBool simplex, PetscReal R)
2342d71ae5a4SJacob Faibussowitsch {
234365a81367SMatthew G. Knepley   const PetscInt embedDim = dim + 1;
234465a81367SMatthew G. Knepley   PetscSection   coordSection;
234565a81367SMatthew G. Knepley   Vec            coordinates;
234665a81367SMatthew G. Knepley   PetscScalar   *coords;
234765a81367SMatthew G. Knepley   PetscReal     *coordsIn;
234807c565c5SJose E. Roman   PetscInt       numCells, numEdges, numVerts = 0, firstVertex = 0, v, firstEdge, coordSize, d, e;
234965a81367SMatthew G. Knepley   PetscMPIInt    rank;
235065a81367SMatthew G. Knepley 
235165a81367SMatthew G. Knepley   PetscFunctionBegin;
23529318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveBool(dm, simplex, 3);
235346139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
23549566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
23559566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, dim + 1));
23569566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
235765a81367SMatthew G. Knepley   switch (dim) {
23585c344501SMatthew G. Knepley   case 1:
23595c344501SMatthew G. Knepley     numCells = 16;
23605c344501SMatthew G. Knepley     numVerts = numCells;
23615c344501SMatthew G. Knepley 
23625c344501SMatthew G. Knepley     // Build Topology
23635c344501SMatthew G. Knepley     PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts));
23645c344501SMatthew G. Knepley     for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim));
23655c344501SMatthew G. Knepley     PetscCall(DMSetUp(dm));
23665c344501SMatthew G. Knepley     for (PetscInt c = 0; c < numCells; ++c) {
23675c344501SMatthew G. Knepley       PetscInt cone[2];
23685c344501SMatthew G. Knepley 
23695c344501SMatthew G. Knepley       cone[0] = c + numCells;
23705c344501SMatthew G. Knepley       cone[1] = (c + 1) % numVerts + numCells;
23715c344501SMatthew G. Knepley       PetscCall(DMPlexSetCone(dm, c, cone));
23725c344501SMatthew G. Knepley     }
23735c344501SMatthew G. Knepley     PetscCall(DMPlexSymmetrize(dm));
23745c344501SMatthew G. Knepley     PetscCall(DMPlexStratify(dm));
23755c344501SMatthew G. Knepley     PetscCall(PetscMalloc1(numVerts * embedDim, &coordsIn));
23765c344501SMatthew G. Knepley     for (PetscInt v = 0; v < numVerts; ++v) {
23775c344501SMatthew G. Knepley       const PetscReal rad = 2. * PETSC_PI * v / numVerts;
23785c344501SMatthew G. Knepley 
23795c344501SMatthew G. Knepley       coordsIn[v * embedDim + 0] = PetscCosReal(rad);
23805c344501SMatthew G. Knepley       coordsIn[v * embedDim + 1] = PetscSinReal(rad);
23815c344501SMatthew G. Knepley     }
23825c344501SMatthew G. Knepley     break;
238365a81367SMatthew G. Knepley   case 2:
238465a81367SMatthew G. Knepley     if (simplex) {
238551a74b61SMatthew G. Knepley       const PetscReal radius    = PetscSqrtReal(1 + PETSC_PHI * PETSC_PHI) / (1.0 + PETSC_PHI);
238651a74b61SMatthew G. Knepley       const PetscReal edgeLen   = 2.0 / (1.0 + PETSC_PHI) * (R / radius);
238765a81367SMatthew G. Knepley       const PetscInt  degree    = 5;
238851a74b61SMatthew G. Knepley       PetscReal       vertex[3] = {0.0, 1.0 / (1.0 + PETSC_PHI), PETSC_PHI / (1.0 + PETSC_PHI)};
238965a81367SMatthew G. Knepley       PetscInt        s[3]      = {1, 1, 1};
239065a81367SMatthew G. Knepley       PetscInt        cone[3];
239107c565c5SJose E. Roman       PetscInt       *graph;
239265a81367SMatthew G. Knepley 
23939371c9d4SSatish Balay       vertex[0] *= R / radius;
23949371c9d4SSatish Balay       vertex[1] *= R / radius;
23959371c9d4SSatish Balay       vertex[2] *= R / radius;
2396dd400576SPatrick Sanan       numCells    = rank == 0 ? 20 : 0;
2397dd400576SPatrick Sanan       numVerts    = rank == 0 ? 12 : 0;
239865a81367SMatthew G. Knepley       firstVertex = numCells;
239951a74b61SMatthew G. Knepley       /* Use icosahedron, which for a R-sphere has coordinates which are all cyclic permutations of
240065a81367SMatthew G. Knepley 
240165a81367SMatthew G. Knepley            (0, \pm 1/\phi+1, \pm \phi/\phi+1)
240265a81367SMatthew G. Knepley 
240365a81367SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
240451a74b61SMatthew G. Knepley          length is then given by 2/(1+\phi) = 2 * 0.38197 = 0.76393.
240565a81367SMatthew G. Knepley       */
240665a81367SMatthew G. Knepley       /* Construct vertices */
24079566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
2408dd400576SPatrick Sanan       if (rank == 0) {
240907c565c5SJose E. Roman         for (PetscInt p = 0, i = 0; p < embedDim; ++p) {
241065a81367SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
241165a81367SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
241265a81367SMatthew G. Knepley               for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[(d + p) % embedDim] * vertex[(d + p) % embedDim];
241365a81367SMatthew G. Knepley               ++i;
241465a81367SMatthew G. Knepley             }
241565a81367SMatthew G. Knepley           }
241665a81367SMatthew G. Knepley         }
241745da822fSValeria Barra       }
241865a81367SMatthew G. Knepley       /* Construct graph */
24199566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * numVerts, &graph));
242007c565c5SJose E. Roman       for (PetscInt i = 0; i < numVerts; ++i) {
242107c565c5SJose E. Roman         PetscInt k = 0;
242207c565c5SJose E. Roman         for (PetscInt j = 0; j < numVerts; ++j) {
24239371c9d4SSatish Balay           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i * embedDim], &coordsIn[j * embedDim]) - edgeLen) < PETSC_SMALL) {
24249371c9d4SSatish Balay             graph[i * numVerts + j] = 1;
24259371c9d4SSatish Balay             ++k;
24269371c9d4SSatish Balay           }
242765a81367SMatthew G. Knepley         }
242863a3b9bcSJacob Faibussowitsch         PetscCheck(k == degree, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid icosahedron, vertex %" PetscInt_FMT " degree %" PetscInt_FMT " != %" PetscInt_FMT, i, k, degree);
242965a81367SMatthew G. Knepley       }
243065a81367SMatthew G. Knepley       /* Build Topology */
24319566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts));
243207c565c5SJose E. Roman       for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim));
24339566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
243465a81367SMatthew G. Knepley       /* Cells */
243507c565c5SJose E. Roman       for (PetscInt i = 0, c = 0; i < numVerts; ++i) {
243607c565c5SJose E. Roman         for (PetscInt j = 0; j < i; ++j) {
243707c565c5SJose E. Roman           for (PetscInt k = 0; k < j; ++k) {
243865a81367SMatthew G. Knepley             if (graph[i * numVerts + j] && graph[j * numVerts + k] && graph[k * numVerts + i]) {
24399371c9d4SSatish Balay               cone[0] = firstVertex + i;
24409371c9d4SSatish Balay               cone[1] = firstVertex + j;
24419371c9d4SSatish Balay               cone[2] = firstVertex + k;
244265a81367SMatthew G. Knepley               /* Check orientation */
244365a81367SMatthew G. Knepley               {
24449371c9d4SSatish Balay                 const PetscInt epsilon[3][3][3] = {
24459371c9d4SSatish Balay                   {{0, 0, 0},  {0, 0, 1},  {0, -1, 0}},
24469371c9d4SSatish Balay                   {{0, 0, -1}, {0, 0, 0},  {1, 0, 0} },
24479371c9d4SSatish Balay                   {{0, 1, 0},  {-1, 0, 0}, {0, 0, 0} }
24489371c9d4SSatish Balay                 };
244965a81367SMatthew G. Knepley                 PetscReal normal[3];
245065a81367SMatthew G. Knepley                 PetscInt  e, f;
245165a81367SMatthew G. Knepley 
245265a81367SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) {
245365a81367SMatthew G. Knepley                   normal[d] = 0.0;
245465a81367SMatthew G. Knepley                   for (e = 0; e < embedDim; ++e) {
2455ad540459SPierre 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]);
245665a81367SMatthew G. Knepley                   }
245765a81367SMatthew G. Knepley                 }
24589371c9d4SSatish Balay                 if (DotReal(embedDim, normal, &coordsIn[i * embedDim]) < 0) {
24599371c9d4SSatish Balay                   PetscInt tmp = cone[1];
24609371c9d4SSatish Balay                   cone[1]      = cone[2];
24619371c9d4SSatish Balay                   cone[2]      = tmp;
246265a81367SMatthew G. Knepley                 }
246365a81367SMatthew G. Knepley               }
24649566063dSJacob Faibussowitsch               PetscCall(DMPlexSetCone(dm, c++, cone));
246565a81367SMatthew G. Knepley             }
246665a81367SMatthew G. Knepley           }
246765a81367SMatthew G. Knepley         }
246865a81367SMatthew G. Knepley       }
24699566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
24709566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
24719566063dSJacob Faibussowitsch       PetscCall(PetscFree(graph));
247265a81367SMatthew G. Knepley     } else {
24732829fed8SMatthew G. Knepley       /*
24742829fed8SMatthew G. Knepley         12-21--13
24752829fed8SMatthew G. Knepley          |     |
24762829fed8SMatthew G. Knepley         25  4  24
24772829fed8SMatthew G. Knepley          |     |
24782829fed8SMatthew G. Knepley   12-25--9-16--8-24--13
24792829fed8SMatthew G. Knepley    |     |     |     |
24802829fed8SMatthew G. Knepley   23  5 17  0 15  3  22
24812829fed8SMatthew G. Knepley    |     |     |     |
24822829fed8SMatthew G. Knepley   10-20--6-14--7-19--11
24832829fed8SMatthew G. Knepley          |     |
24842829fed8SMatthew G. Knepley         20  1  19
24852829fed8SMatthew G. Knepley          |     |
24862829fed8SMatthew G. Knepley         10-18--11
24872829fed8SMatthew G. Knepley          |     |
24882829fed8SMatthew G. Knepley         23  2  22
24892829fed8SMatthew G. Knepley          |     |
24902829fed8SMatthew G. Knepley         12-21--13
24912829fed8SMatthew G. Knepley        */
24922829fed8SMatthew G. Knepley       PetscInt cone[4], ornt[4];
24932829fed8SMatthew G. Knepley 
2494dd400576SPatrick Sanan       numCells    = rank == 0 ? 6 : 0;
2495dd400576SPatrick Sanan       numEdges    = rank == 0 ? 12 : 0;
2496dd400576SPatrick Sanan       numVerts    = rank == 0 ? 8 : 0;
249765a81367SMatthew G. Knepley       firstVertex = numCells;
249865a81367SMatthew G. Knepley       firstEdge   = numCells + numVerts;
24992829fed8SMatthew G. Knepley       /* Build Topology */
25009566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells + numEdges + numVerts));
250107c565c5SJose E. Roman       for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 4));
250248a46eb9SPierre Jolivet       for (e = firstEdge; e < firstEdge + numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2));
25039566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
2504dd400576SPatrick Sanan       if (rank == 0) {
25052829fed8SMatthew G. Knepley         /* Cell 0 */
25069371c9d4SSatish Balay         cone[0] = 14;
25079371c9d4SSatish Balay         cone[1] = 15;
25089371c9d4SSatish Balay         cone[2] = 16;
25099371c9d4SSatish Balay         cone[3] = 17;
25109566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
25119371c9d4SSatish Balay         ornt[0] = 0;
25129371c9d4SSatish Balay         ornt[1] = 0;
25139371c9d4SSatish Balay         ornt[2] = 0;
25149371c9d4SSatish Balay         ornt[3] = 0;
25159566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 0, ornt));
25162829fed8SMatthew G. Knepley         /* Cell 1 */
25179371c9d4SSatish Balay         cone[0] = 18;
25189371c9d4SSatish Balay         cone[1] = 19;
25199371c9d4SSatish Balay         cone[2] = 14;
25209371c9d4SSatish Balay         cone[3] = 20;
25219566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
25229371c9d4SSatish Balay         ornt[0] = 0;
25239371c9d4SSatish Balay         ornt[1] = 0;
25249371c9d4SSatish Balay         ornt[2] = -1;
25259371c9d4SSatish Balay         ornt[3] = 0;
25269566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 1, ornt));
25272829fed8SMatthew G. Knepley         /* Cell 2 */
25289371c9d4SSatish Balay         cone[0] = 21;
25299371c9d4SSatish Balay         cone[1] = 22;
25309371c9d4SSatish Balay         cone[2] = 18;
25319371c9d4SSatish Balay         cone[3] = 23;
25329566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
25339371c9d4SSatish Balay         ornt[0] = 0;
25349371c9d4SSatish Balay         ornt[1] = 0;
25359371c9d4SSatish Balay         ornt[2] = -1;
25369371c9d4SSatish Balay         ornt[3] = 0;
25379566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 2, ornt));
25382829fed8SMatthew G. Knepley         /* Cell 3 */
25399371c9d4SSatish Balay         cone[0] = 19;
25409371c9d4SSatish Balay         cone[1] = 22;
25419371c9d4SSatish Balay         cone[2] = 24;
25429371c9d4SSatish Balay         cone[3] = 15;
25439566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
25449371c9d4SSatish Balay         ornt[0] = -1;
25459371c9d4SSatish Balay         ornt[1] = -1;
25469371c9d4SSatish Balay         ornt[2] = 0;
25479371c9d4SSatish Balay         ornt[3] = -1;
25489566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 3, ornt));
25492829fed8SMatthew G. Knepley         /* Cell 4 */
25509371c9d4SSatish Balay         cone[0] = 16;
25519371c9d4SSatish Balay         cone[1] = 24;
25529371c9d4SSatish Balay         cone[2] = 21;
25539371c9d4SSatish Balay         cone[3] = 25;
25549566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
25559371c9d4SSatish Balay         ornt[0] = -1;
25569371c9d4SSatish Balay         ornt[1] = -1;
25579371c9d4SSatish Balay         ornt[2] = -1;
25589371c9d4SSatish Balay         ornt[3] = 0;
25599566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 4, ornt));
25602829fed8SMatthew G. Knepley         /* Cell 5 */
25619371c9d4SSatish Balay         cone[0] = 20;
25629371c9d4SSatish Balay         cone[1] = 17;
25639371c9d4SSatish Balay         cone[2] = 25;
25649371c9d4SSatish Balay         cone[3] = 23;
25659566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 5, cone));
25669371c9d4SSatish Balay         ornt[0] = -1;
25679371c9d4SSatish Balay         ornt[1] = -1;
25689371c9d4SSatish Balay         ornt[2] = -1;
25699371c9d4SSatish Balay         ornt[3] = -1;
25709566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 5, ornt));
25712829fed8SMatthew G. Knepley         /* Edges */
25729371c9d4SSatish Balay         cone[0] = 6;
25739371c9d4SSatish Balay         cone[1] = 7;
25749566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 14, cone));
25759371c9d4SSatish Balay         cone[0] = 7;
25769371c9d4SSatish Balay         cone[1] = 8;
25779566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 15, cone));
25789371c9d4SSatish Balay         cone[0] = 8;
25799371c9d4SSatish Balay         cone[1] = 9;
25809566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 16, cone));
25819371c9d4SSatish Balay         cone[0] = 9;
25829371c9d4SSatish Balay         cone[1] = 6;
25839566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 17, cone));
25849371c9d4SSatish Balay         cone[0] = 10;
25859371c9d4SSatish Balay         cone[1] = 11;
25869566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 18, cone));
25879371c9d4SSatish Balay         cone[0] = 11;
25889371c9d4SSatish Balay         cone[1] = 7;
25899566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 19, cone));
25909371c9d4SSatish Balay         cone[0] = 6;
25919371c9d4SSatish Balay         cone[1] = 10;
25929566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 20, cone));
25939371c9d4SSatish Balay         cone[0] = 12;
25949371c9d4SSatish Balay         cone[1] = 13;
25959566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 21, cone));
25969371c9d4SSatish Balay         cone[0] = 13;
25979371c9d4SSatish Balay         cone[1] = 11;
25989566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 22, cone));
25999371c9d4SSatish Balay         cone[0] = 10;
26009371c9d4SSatish Balay         cone[1] = 12;
26019566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 23, cone));
26029371c9d4SSatish Balay         cone[0] = 13;
26039371c9d4SSatish Balay         cone[1] = 8;
26049566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 24, cone));
26059371c9d4SSatish Balay         cone[0] = 12;
26069371c9d4SSatish Balay         cone[1] = 9;
26079566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 25, cone));
260845da822fSValeria Barra       }
26099566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
26109566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
26112829fed8SMatthew G. Knepley       /* Build coordinates */
26129566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
2613dd400576SPatrick Sanan       if (rank == 0) {
26149371c9d4SSatish Balay         coordsIn[0 * embedDim + 0] = -R;
26159371c9d4SSatish Balay         coordsIn[0 * embedDim + 1] = R;
26169371c9d4SSatish Balay         coordsIn[0 * embedDim + 2] = -R;
26179371c9d4SSatish Balay         coordsIn[1 * embedDim + 0] = R;
26189371c9d4SSatish Balay         coordsIn[1 * embedDim + 1] = R;
26199371c9d4SSatish Balay         coordsIn[1 * embedDim + 2] = -R;
26209371c9d4SSatish Balay         coordsIn[2 * embedDim + 0] = R;
26219371c9d4SSatish Balay         coordsIn[2 * embedDim + 1] = -R;
26229371c9d4SSatish Balay         coordsIn[2 * embedDim + 2] = -R;
26239371c9d4SSatish Balay         coordsIn[3 * embedDim + 0] = -R;
26249371c9d4SSatish Balay         coordsIn[3 * embedDim + 1] = -R;
26259371c9d4SSatish Balay         coordsIn[3 * embedDim + 2] = -R;
26269371c9d4SSatish Balay         coordsIn[4 * embedDim + 0] = -R;
26279371c9d4SSatish Balay         coordsIn[4 * embedDim + 1] = R;
26289371c9d4SSatish Balay         coordsIn[4 * embedDim + 2] = R;
26299371c9d4SSatish Balay         coordsIn[5 * embedDim + 0] = R;
26309371c9d4SSatish Balay         coordsIn[5 * embedDim + 1] = R;
26319371c9d4SSatish Balay         coordsIn[5 * embedDim + 2] = R;
26329371c9d4SSatish Balay         coordsIn[6 * embedDim + 0] = -R;
26339371c9d4SSatish Balay         coordsIn[6 * embedDim + 1] = -R;
26349371c9d4SSatish Balay         coordsIn[6 * embedDim + 2] = R;
26359371c9d4SSatish Balay         coordsIn[7 * embedDim + 0] = R;
26369371c9d4SSatish Balay         coordsIn[7 * embedDim + 1] = -R;
26379371c9d4SSatish Balay         coordsIn[7 * embedDim + 2] = R;
263865a81367SMatthew G. Knepley       }
263945da822fSValeria Barra     }
264065a81367SMatthew G. Knepley     break;
264165a81367SMatthew G. Knepley   case 3:
2642116ded15SMatthew G. Knepley     if (simplex) {
2643116ded15SMatthew G. Knepley       const PetscReal edgeLen         = 1.0 / PETSC_PHI;
264451a74b61SMatthew G. Knepley       PetscReal       vertexA[4]      = {0.5, 0.5, 0.5, 0.5};
264551a74b61SMatthew G. Knepley       PetscReal       vertexB[4]      = {1.0, 0.0, 0.0, 0.0};
264651a74b61SMatthew G. Knepley       PetscReal       vertexC[4]      = {0.5, 0.5 * PETSC_PHI, 0.5 / PETSC_PHI, 0.0};
2647116ded15SMatthew G. Knepley       const PetscInt  degree          = 12;
2648116ded15SMatthew G. Knepley       PetscInt        s[4]            = {1, 1, 1};
26499371c9d4SSatish Balay       PetscInt        evenPerm[12][4] = {
26509371c9d4SSatish Balay         {0, 1, 2, 3},
26519371c9d4SSatish Balay         {0, 2, 3, 1},
26529371c9d4SSatish Balay         {0, 3, 1, 2},
26539371c9d4SSatish Balay         {1, 0, 3, 2},
26549371c9d4SSatish Balay         {1, 2, 0, 3},
26559371c9d4SSatish Balay         {1, 3, 2, 0},
26569371c9d4SSatish Balay         {2, 0, 1, 3},
26579371c9d4SSatish Balay         {2, 1, 3, 0},
26589371c9d4SSatish Balay         {2, 3, 0, 1},
26599371c9d4SSatish Balay         {3, 0, 2, 1},
26609371c9d4SSatish Balay         {3, 1, 0, 2},
26619371c9d4SSatish Balay         {3, 2, 1, 0}
26629371c9d4SSatish Balay       };
2663116ded15SMatthew G. Knepley       PetscInt  cone[4];
2664116ded15SMatthew G. Knepley       PetscInt *graph, p, i, j, k, l;
2665116ded15SMatthew G. Knepley 
26669371c9d4SSatish Balay       vertexA[0] *= R;
26679371c9d4SSatish Balay       vertexA[1] *= R;
26689371c9d4SSatish Balay       vertexA[2] *= R;
26699371c9d4SSatish Balay       vertexA[3] *= R;
26709371c9d4SSatish Balay       vertexB[0] *= R;
26719371c9d4SSatish Balay       vertexB[1] *= R;
26729371c9d4SSatish Balay       vertexB[2] *= R;
26739371c9d4SSatish Balay       vertexB[3] *= R;
26749371c9d4SSatish Balay       vertexC[0] *= R;
26759371c9d4SSatish Balay       vertexC[1] *= R;
26769371c9d4SSatish Balay       vertexC[2] *= R;
26779371c9d4SSatish Balay       vertexC[3] *= R;
2678dd400576SPatrick Sanan       numCells    = rank == 0 ? 600 : 0;
2679dd400576SPatrick Sanan       numVerts    = rank == 0 ? 120 : 0;
2680116ded15SMatthew G. Knepley       firstVertex = numCells;
2681116ded15SMatthew G. Knepley       /* Use the 600-cell, which for a unit sphere has coordinates which are
2682116ded15SMatthew G. Knepley 
2683116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm 1,    \pm 1, \pm 1)                          16
2684116ded15SMatthew G. Knepley                (\pm 1,    0,       0,      0)  all cyclic permutations   8
2685116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm phi, \pm 1/phi, 0)  all even permutations    96
2686116ded15SMatthew G. Knepley 
2687116ded15SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
26886333ae4fSvaleriabarra          length is then given by 1/\phi = 0.61803.
2689116ded15SMatthew G. Knepley 
2690116ded15SMatthew G. Knepley          http://buzzard.pugetsound.edu/sage-practice/ch03s03.html
2691116ded15SMatthew G. Knepley          http://mathworld.wolfram.com/600-Cell.html
2692116ded15SMatthew G. Knepley       */
2693116ded15SMatthew G. Knepley       /* Construct vertices */
26949566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
2695116ded15SMatthew G. Knepley       i = 0;
2696dd400576SPatrick Sanan       if (rank == 0) {
2697116ded15SMatthew G. Knepley         for (s[0] = -1; s[0] < 2; s[0] += 2) {
2698116ded15SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
2699116ded15SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
2700116ded15SMatthew G. Knepley               for (s[3] = -1; s[3] < 2; s[3] += 2) {
2701116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[d] * vertexA[d];
2702116ded15SMatthew G. Knepley                 ++i;
2703116ded15SMatthew G. Knepley               }
2704116ded15SMatthew G. Knepley             }
2705116ded15SMatthew G. Knepley           }
2706116ded15SMatthew G. Knepley         }
2707116ded15SMatthew G. Knepley         for (p = 0; p < embedDim; ++p) {
2708116ded15SMatthew G. Knepley           s[1] = s[2] = s[3] = 1;
2709116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
2710116ded15SMatthew G. Knepley             for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[(d + p) % embedDim] * vertexB[(d + p) % embedDim];
2711116ded15SMatthew G. Knepley             ++i;
2712116ded15SMatthew G. Knepley           }
2713116ded15SMatthew G. Knepley         }
2714116ded15SMatthew G. Knepley         for (p = 0; p < 12; ++p) {
2715116ded15SMatthew G. Knepley           s[3] = 1;
2716116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
2717116ded15SMatthew G. Knepley             for (s[1] = -1; s[1] < 2; s[1] += 2) {
2718116ded15SMatthew G. Knepley               for (s[2] = -1; s[2] < 2; s[2] += 2) {
2719116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[evenPerm[p][d]] * vertexC[evenPerm[p][d]];
2720116ded15SMatthew G. Knepley                 ++i;
2721116ded15SMatthew G. Knepley               }
2722116ded15SMatthew G. Knepley             }
2723116ded15SMatthew G. Knepley           }
2724116ded15SMatthew G. Knepley         }
272545da822fSValeria Barra       }
272663a3b9bcSJacob Faibussowitsch       PetscCheck(i == numVerts, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertices %" PetscInt_FMT " != %" PetscInt_FMT, i, numVerts);
2727116ded15SMatthew G. Knepley       /* Construct graph */
27289566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * numVerts, &graph));
2729116ded15SMatthew G. Knepley       for (i = 0; i < numVerts; ++i) {
2730116ded15SMatthew G. Knepley         for (j = 0, k = 0; j < numVerts; ++j) {
27319371c9d4SSatish Balay           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i * embedDim], &coordsIn[j * embedDim]) - edgeLen) < PETSC_SMALL) {
27329371c9d4SSatish Balay             graph[i * numVerts + j] = 1;
27339371c9d4SSatish Balay             ++k;
27349371c9d4SSatish Balay           }
2735116ded15SMatthew G. Knepley         }
273663a3b9bcSJacob Faibussowitsch         PetscCheck(k == degree, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertex %" PetscInt_FMT " degree %" PetscInt_FMT " != %" PetscInt_FMT, i, k, degree);
2737116ded15SMatthew G. Knepley       }
2738116ded15SMatthew G. Knepley       /* Build Topology */
27399566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts));
274007c565c5SJose E. Roman       for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim));
27419566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
2742116ded15SMatthew G. Knepley       /* Cells */
2743dd400576SPatrick Sanan       if (rank == 0) {
274407c565c5SJose E. Roman         for (PetscInt i = 0, c = 0; i < numVerts; ++i) {
2745116ded15SMatthew G. Knepley           for (j = 0; j < i; ++j) {
2746116ded15SMatthew G. Knepley             for (k = 0; k < j; ++k) {
2747116ded15SMatthew G. Knepley               for (l = 0; l < k; ++l) {
27489371c9d4SSatish 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]) {
27499371c9d4SSatish Balay                   cone[0] = firstVertex + i;
27509371c9d4SSatish Balay                   cone[1] = firstVertex + j;
27519371c9d4SSatish Balay                   cone[2] = firstVertex + k;
27529371c9d4SSatish Balay                   cone[3] = firstVertex + l;
2753116ded15SMatthew G. Knepley                   /* Check orientation: https://ef.gy/linear-algebra:normal-vectors-in-higher-dimensional-spaces */
2754116ded15SMatthew G. Knepley                   {
27559371c9d4SSatish Balay                     const PetscInt epsilon[4][4][4][4] = {
27569371c9d4SSatish 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}}},
2757116ded15SMatthew G. Knepley 
27589371c9d4SSatish 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}}},
2759116ded15SMatthew G. Knepley 
27609371c9d4SSatish 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}}},
2761116ded15SMatthew G. Knepley 
27629371c9d4SSatish 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}} }
27639371c9d4SSatish Balay                     };
2764116ded15SMatthew G. Knepley                     PetscReal normal[4];
2765116ded15SMatthew G. Knepley                     PetscInt  e, f, g;
2766116ded15SMatthew G. Knepley 
2767116ded15SMatthew G. Knepley                     for (d = 0; d < embedDim; ++d) {
2768116ded15SMatthew G. Knepley                       normal[d] = 0.0;
2769116ded15SMatthew G. Knepley                       for (e = 0; e < embedDim; ++e) {
2770116ded15SMatthew G. Knepley                         for (f = 0; f < embedDim; ++f) {
2771116ded15SMatthew G. Knepley                           for (g = 0; g < embedDim; ++g) {
2772116ded15SMatthew 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]);
2773116ded15SMatthew G. Knepley                           }
2774116ded15SMatthew G. Knepley                         }
2775116ded15SMatthew G. Knepley                       }
2776116ded15SMatthew G. Knepley                     }
27779371c9d4SSatish Balay                     if (DotReal(embedDim, normal, &coordsIn[i * embedDim]) < 0) {
27789371c9d4SSatish Balay                       PetscInt tmp = cone[1];
27799371c9d4SSatish Balay                       cone[1]      = cone[2];
27809371c9d4SSatish Balay                       cone[2]      = tmp;
27819371c9d4SSatish Balay                     }
2782116ded15SMatthew G. Knepley                   }
27839566063dSJacob Faibussowitsch                   PetscCall(DMPlexSetCone(dm, c++, cone));
2784116ded15SMatthew G. Knepley                 }
2785116ded15SMatthew G. Knepley               }
2786116ded15SMatthew G. Knepley             }
2787116ded15SMatthew G. Knepley           }
2788116ded15SMatthew G. Knepley         }
278945da822fSValeria Barra       }
27909566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
27919566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
27929566063dSJacob Faibussowitsch       PetscCall(PetscFree(graph));
2793116ded15SMatthew G. Knepley     }
2794f4d061e9SPierre Jolivet     break;
2795d71ae5a4SJacob Faibussowitsch   default:
2796d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension for sphere: %" PetscInt_FMT, dim);
279765a81367SMatthew G. Knepley   }
279865a81367SMatthew G. Knepley   /* Create coordinates */
27999566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
28009566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
28019566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, embedDim));
28029566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numVerts));
28032829fed8SMatthew G. Knepley   for (v = firstVertex; v < firstVertex + numVerts; ++v) {
28049566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, embedDim));
28059566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, embedDim));
28062829fed8SMatthew G. Knepley   }
28079566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
28089566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
28099566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
28109566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, embedDim));
28119566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
28129566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
28139566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
28149566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
28159371c9d4SSatish Balay   for (v = 0; v < numVerts; ++v)
2816ad540459SPierre Jolivet     for (d = 0; d < embedDim; ++d) coords[v * embedDim + d] = coordsIn[v * embedDim + d];
28179566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
28189566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
28199566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
28209566063dSJacob Faibussowitsch   PetscCall(PetscFree(coordsIn));
282151a74b61SMatthew G. Knepley   {
282251a74b61SMatthew G. Knepley     DM          cdm;
282351a74b61SMatthew G. Knepley     PetscDS     cds;
28249318fe57SMatthew G. Knepley     PetscScalar c = R;
282551a74b61SMatthew G. Knepley 
2826e44f6aebSMatthew G. Knepley     PetscCall(DMPlexCreateCoordinateSpace(dm, 1, PETSC_TRUE, snapToSphere));
28279566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
28289566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
28299566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, 1, &c));
283051a74b61SMatthew G. Knepley   }
283146139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
28329318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
28339566063dSJacob Faibussowitsch   if (simplex) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
28343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
28359318fe57SMatthew G. Knepley }
28369318fe57SMatthew G. Knepley 
2837b7f5c055SJed Brown typedef void (*TPSEvaluateFunc)(const PetscReal[], PetscReal *, PetscReal[], PetscReal (*)[3]);
2838b7f5c055SJed Brown 
2839b7f5c055SJed Brown /*
2840b7f5c055SJed Brown  The Schwarz P implicit surface is
2841b7f5c055SJed Brown 
2842b7f5c055SJed Brown      f(x) = cos(x0) + cos(x1) + cos(x2) = 0
2843b7f5c055SJed Brown */
2844d71ae5a4SJacob Faibussowitsch static void TPSEvaluate_SchwarzP(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3])
2845d71ae5a4SJacob Faibussowitsch {
2846b7f5c055SJed Brown   PetscReal c[3] = {PetscCosReal(y[0] * PETSC_PI), PetscCosReal(y[1] * PETSC_PI), PetscCosReal(y[2] * PETSC_PI)};
2847b7f5c055SJed Brown   PetscReal g[3] = {-PetscSinReal(y[0] * PETSC_PI), -PetscSinReal(y[1] * PETSC_PI), -PetscSinReal(y[2] * PETSC_PI)};
2848b7f5c055SJed Brown   f[0]           = c[0] + c[1] + c[2];
2849b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) {
2850b7f5c055SJed Brown     grad[i] = PETSC_PI * g[i];
2851ad540459SPierre Jolivet     for (PetscInt j = 0; j < 3; j++) hess[i][j] = (i == j) ? -PetscSqr(PETSC_PI) * c[i] : 0.;
2852b7f5c055SJed Brown   }
2853b7f5c055SJed Brown }
2854b7f5c055SJed Brown 
28554663dae6SJed Brown // u[] is a tentative normal on input. Replace with the implicit function gradient in the same direction
2856d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSExtrudeNormalFunc_SchwarzP(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt r, PetscScalar u[], void *ctx)
2857d71ae5a4SJacob Faibussowitsch {
2858ad540459SPierre Jolivet   for (PetscInt i = 0; i < 3; i++) u[i] = -PETSC_PI * PetscSinReal(x[i] * PETSC_PI);
28593ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
28604663dae6SJed Brown }
28614663dae6SJed Brown 
2862b7f5c055SJed Brown /*
2863b7f5c055SJed Brown  The Gyroid implicit surface is
2864b7f5c055SJed Brown 
2865b7f5c055SJed 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)
2866b7f5c055SJed Brown 
2867b7f5c055SJed Brown */
2868d71ae5a4SJacob Faibussowitsch static void TPSEvaluate_Gyroid(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3])
2869d71ae5a4SJacob Faibussowitsch {
2870b7f5c055SJed Brown   PetscReal s[3] = {PetscSinReal(PETSC_PI * y[0]), PetscSinReal(PETSC_PI * (y[1] + .5)), PetscSinReal(PETSC_PI * (y[2] + .25))};
2871b7f5c055SJed Brown   PetscReal c[3] = {PetscCosReal(PETSC_PI * y[0]), PetscCosReal(PETSC_PI * (y[1] + .5)), PetscCosReal(PETSC_PI * (y[2] + .25))};
2872b7f5c055SJed Brown   f[0]           = s[0] * c[1] + s[1] * c[2] + s[2] * c[0];
2873b7f5c055SJed Brown   grad[0]        = PETSC_PI * (c[0] * c[1] - s[2] * s[0]);
2874b7f5c055SJed Brown   grad[1]        = PETSC_PI * (c[1] * c[2] - s[0] * s[1]);
2875b7f5c055SJed Brown   grad[2]        = PETSC_PI * (c[2] * c[0] - s[1] * s[2]);
2876b7f5c055SJed Brown   hess[0][0]     = -PetscSqr(PETSC_PI) * (s[0] * c[1] + s[2] * c[0]);
2877b7f5c055SJed Brown   hess[0][1]     = -PetscSqr(PETSC_PI) * (c[0] * s[1]);
2878b7f5c055SJed Brown   hess[0][2]     = -PetscSqr(PETSC_PI) * (c[2] * s[0]);
2879b7f5c055SJed Brown   hess[1][0]     = -PetscSqr(PETSC_PI) * (s[1] * c[2] + s[0] * c[1]);
2880b7f5c055SJed Brown   hess[1][1]     = -PetscSqr(PETSC_PI) * (c[1] * s[2]);
2881b7f5c055SJed Brown   hess[2][2]     = -PetscSqr(PETSC_PI) * (c[0] * s[1]);
2882b7f5c055SJed Brown   hess[2][0]     = -PetscSqr(PETSC_PI) * (s[2] * c[0] + s[1] * c[2]);
2883b7f5c055SJed Brown   hess[2][1]     = -PetscSqr(PETSC_PI) * (c[2] * s[0]);
2884b7f5c055SJed Brown   hess[2][2]     = -PetscSqr(PETSC_PI) * (c[1] * s[2]);
2885b7f5c055SJed Brown }
2886b7f5c055SJed Brown 
28874663dae6SJed Brown // u[] is a tentative normal on input. Replace with the implicit function gradient in the same direction
2888d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSExtrudeNormalFunc_Gyroid(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt r, PetscScalar u[], void *ctx)
2889d71ae5a4SJacob Faibussowitsch {
28904663dae6SJed Brown   PetscReal s[3] = {PetscSinReal(PETSC_PI * x[0]), PetscSinReal(PETSC_PI * (x[1] + .5)), PetscSinReal(PETSC_PI * (x[2] + .25))};
28914663dae6SJed Brown   PetscReal c[3] = {PetscCosReal(PETSC_PI * x[0]), PetscCosReal(PETSC_PI * (x[1] + .5)), PetscCosReal(PETSC_PI * (x[2] + .25))};
28924663dae6SJed Brown   u[0]           = PETSC_PI * (c[0] * c[1] - s[2] * s[0]);
28934663dae6SJed Brown   u[1]           = PETSC_PI * (c[1] * c[2] - s[0] * s[1]);
28944663dae6SJed Brown   u[2]           = PETSC_PI * (c[2] * c[0] - s[1] * s[2]);
28953ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
28964663dae6SJed Brown }
28974663dae6SJed Brown 
2898b7f5c055SJed Brown /*
2899b7f5c055SJed Brown    We wish to solve
2900b7f5c055SJed Brown 
2901b7f5c055SJed Brown          min_y || y - x ||^2  subject to f(y) = 0
2902b7f5c055SJed Brown 
2903b7f5c055SJed Brown    Let g(y) = grad(f).  The minimization problem is equivalent to asking to satisfy
2904b7f5c055SJed 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
2905b7f5c055SJed Brown    tangent space and ask for both components in the tangent space to be zero.
2906b7f5c055SJed Brown 
2907b7f5c055SJed Brown    Take g to be a column vector and compute the "full QR" factorization Q R = g,
2908b7f5c055SJed Brown    where Q = I - 2 n n^T is a symmetric orthogonal matrix.
2909b7f5c055SJed Brown    The first column of Q is parallel to g so the remaining two columns span the null space.
2910b7f5c055SJed Brown    Let Qn = Q[:,1:] be those remaining columns.  Then Qn Qn^T is an orthogonal projector into the tangent space.
2911da81f932SPierre Jolivet    Since Q is symmetric, this is equivalent to multiplying by Q and taking the last two entries.
2912b7f5c055SJed Brown    In total, we have a system of 3 equations in 3 unknowns:
2913b7f5c055SJed Brown 
2914b7f5c055SJed Brown      f(y) = 0                       1 equation
2915b7f5c055SJed Brown      Qn^T (y - x) = 0               2 equations
2916b7f5c055SJed Brown 
2917b7f5c055SJed Brown    Here, we compute the residual and Jacobian of this system.
2918b7f5c055SJed Brown */
2919d71ae5a4SJacob Faibussowitsch static void TPSNearestPointResJac(TPSEvaluateFunc feval, const PetscScalar x[], const PetscScalar y[], PetscScalar res[], PetscScalar J[])
2920d71ae5a4SJacob Faibussowitsch {
2921b7f5c055SJed Brown   PetscReal yreal[3] = {PetscRealPart(y[0]), PetscRealPart(y[1]), PetscRealPart(y[2])};
2922b7f5c055SJed Brown   PetscReal d[3]     = {PetscRealPart(y[0] - x[0]), PetscRealPart(y[1] - x[1]), PetscRealPart(y[2] - x[2])};
29232f0490c0SSatish Balay   PetscReal f, grad[3], n[3], norm, norm_y[3], nd, nd_y[3], sign;
29249371c9d4SSatish Balay   PetscReal n_y[3][3] = {
29259371c9d4SSatish Balay     {0, 0, 0},
29269371c9d4SSatish Balay     {0, 0, 0},
29279371c9d4SSatish Balay     {0, 0, 0}
29289371c9d4SSatish Balay   };
2929b7f5c055SJed Brown 
2930b7f5c055SJed Brown   feval(yreal, &f, grad, n_y);
2931b7f5c055SJed Brown 
2932b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) n[i] = grad[i];
2933b7f5c055SJed Brown   norm = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2]));
2934ad540459SPierre Jolivet   for (PetscInt i = 0; i < 3; i++) norm_y[i] = 1. / norm * n[i] * n_y[i][i];
2935b7f5c055SJed Brown 
2936b7f5c055SJed Brown   // Define the Householder reflector
2937b7f5c055SJed Brown   sign = n[0] >= 0 ? 1. : -1.;
2938b7f5c055SJed Brown   n[0] += norm * sign;
2939b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) n_y[0][i] += norm_y[i] * sign;
2940b7f5c055SJed Brown 
2941b7f5c055SJed Brown   norm      = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2]));
2942b7f5c055SJed Brown   norm_y[0] = 1. / norm * (n[0] * n_y[0][0]);
2943b7f5c055SJed Brown   norm_y[1] = 1. / norm * (n[0] * n_y[0][1] + n[1] * n_y[1][1]);
2944b7f5c055SJed Brown   norm_y[2] = 1. / norm * (n[0] * n_y[0][2] + n[2] * n_y[2][2]);
2945b7f5c055SJed Brown 
2946b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) {
2947b7f5c055SJed Brown     n[i] /= norm;
2948b7f5c055SJed Brown     for (PetscInt j = 0; j < 3; j++) {
2949b7f5c055SJed Brown       // note that n[i] is n_old[i]/norm when executing the code below
2950b7f5c055SJed Brown       n_y[i][j] = n_y[i][j] / norm - n[i] / norm * norm_y[j];
2951b7f5c055SJed Brown     }
2952b7f5c055SJed Brown   }
2953b7f5c055SJed Brown 
2954b7f5c055SJed Brown   nd = n[0] * d[0] + n[1] * d[1] + n[2] * d[2];
2955b7f5c055SJed 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];
2956b7f5c055SJed Brown 
2957b7f5c055SJed Brown   res[0] = f;
2958b7f5c055SJed Brown   res[1] = d[1] - 2 * n[1] * nd;
2959b7f5c055SJed Brown   res[2] = d[2] - 2 * n[2] * nd;
2960b7f5c055SJed Brown   // J[j][i] is J_{ij} (column major)
2961b7f5c055SJed Brown   for (PetscInt j = 0; j < 3; j++) {
2962b7f5c055SJed Brown     J[0 + j * 3] = grad[j];
2963b7f5c055SJed Brown     J[1 + j * 3] = (j == 1) * 1. - 2 * (n_y[1][j] * nd + n[1] * nd_y[j]);
2964b7f5c055SJed Brown     J[2 + j * 3] = (j == 2) * 1. - 2 * (n_y[2][j] * nd + n[2] * nd_y[j]);
2965b7f5c055SJed Brown   }
2966b7f5c055SJed Brown }
2967b7f5c055SJed Brown 
2968b7f5c055SJed Brown /*
2969b7f5c055SJed Brown    Project x to the nearest point on the implicit surface using Newton's method.
2970b7f5c055SJed Brown */
2971d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSNearestPoint(TPSEvaluateFunc feval, PetscScalar x[])
2972d71ae5a4SJacob Faibussowitsch {
2973b7f5c055SJed Brown   PetscScalar y[3] = {x[0], x[1], x[2]}; // Initial guess
2974b7f5c055SJed Brown 
2975b7f5c055SJed Brown   PetscFunctionBegin;
2976b7f5c055SJed Brown   for (PetscInt iter = 0; iter < 10; iter++) {
2977b7f5c055SJed Brown     PetscScalar res[3], J[9];
2978b7f5c055SJed Brown     PetscReal   resnorm;
2979b7f5c055SJed Brown     TPSNearestPointResJac(feval, x, y, res, J);
2980b7f5c055SJed Brown     resnorm = PetscSqrtReal(PetscSqr(PetscRealPart(res[0])) + PetscSqr(PetscRealPart(res[1])) + PetscSqr(PetscRealPart(res[2])));
2981b7f5c055SJed Brown     if (0) { // Turn on this monitor if you need to confirm quadratic convergence
298263a3b9bcSJacob 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])));
2983b7f5c055SJed Brown     }
2984b7f5c055SJed Brown     if (resnorm < PETSC_SMALL) break;
2985b7f5c055SJed Brown 
2986b7f5c055SJed Brown     // Take the Newton step
29879566063dSJacob Faibussowitsch     PetscCall(PetscKernel_A_gets_inverse_A_3(J, 0., PETSC_FALSE, NULL));
2988b7f5c055SJed Brown     PetscKernel_v_gets_v_minus_A_times_w_3(y, J, res);
2989b7f5c055SJed Brown   }
2990b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) x[i] = y[i];
29913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2992b7f5c055SJed Brown }
2993b7f5c055SJed Brown 
2994b7f5c055SJed Brown const char *const DMPlexTPSTypes[] = {"SCHWARZ_P", "GYROID", "DMPlexTPSType", "DMPLEX_TPS_", NULL};
2995b7f5c055SJed Brown 
2996d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateTPSMesh_Internal(DM dm, DMPlexTPSType tpstype, const PetscInt extent[], const DMBoundaryType periodic[], PetscBool tps_distribute, PetscInt refinements, PetscInt layers, PetscReal thickness)
2997d71ae5a4SJacob Faibussowitsch {
2998b7f5c055SJed Brown   PetscMPIInt rank;
2999b7f5c055SJed Brown   PetscInt    topoDim = 2, spaceDim = 3, numFaces = 0, numVertices = 0, numEdges = 0;
3000b7f5c055SJed Brown   PetscInt(*edges)[2] = NULL, *edgeSets = NULL;
3001b7f5c055SJed Brown   PetscInt            *cells_flat = NULL;
3002b7f5c055SJed Brown   PetscReal           *vtxCoords  = NULL;
3003b7f5c055SJed Brown   TPSEvaluateFunc      evalFunc   = NULL;
30044663dae6SJed Brown   PetscSimplePointFunc normalFunc = NULL;
3005b7f5c055SJed Brown   DMLabel              label;
3006b7f5c055SJed Brown 
3007b7f5c055SJed Brown   PetscFunctionBegin;
300846139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
30099566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
301063a3b9bcSJacob 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);
3011b7f5c055SJed Brown   switch (tpstype) {
3012b7f5c055SJed Brown   case DMPLEX_TPS_SCHWARZ_P:
3013b7f5c055SJed 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");
3014c5853193SPierre Jolivet     if (rank == 0) {
3015b7f5c055SJed Brown       PetscInt(*cells)[6][4][4] = NULL; // [junction, junction-face, cell, conn]
3016b7f5c055SJed Brown       PetscInt  Njunctions = 0, Ncuts = 0, Npipes[3], vcount;
3017b7f5c055SJed Brown       PetscReal L = 1;
3018b7f5c055SJed Brown 
3019b7f5c055SJed Brown       Npipes[0]   = (extent[0] + 1) * extent[1] * extent[2];
3020b7f5c055SJed Brown       Npipes[1]   = extent[0] * (extent[1] + 1) * extent[2];
3021b7f5c055SJed Brown       Npipes[2]   = extent[0] * extent[1] * (extent[2] + 1);
3022b7f5c055SJed Brown       Njunctions  = extent[0] * extent[1] * extent[2];
3023b7f5c055SJed Brown       Ncuts       = 2 * (extent[0] * extent[1] + extent[1] * extent[2] + extent[2] * extent[0]);
3024b7f5c055SJed Brown       numVertices = 4 * (Npipes[0] + Npipes[1] + Npipes[2]) + 8 * Njunctions;
30259566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(3 * numVertices, &vtxCoords));
30269566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Njunctions, &cells));
30279566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Ncuts * 4, &edges));
30289566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Ncuts * 4, &edgeSets));
3029b7f5c055SJed Brown       // x-normal pipes
3030b7f5c055SJed Brown       vcount = 0;
3031b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0] + 1; i++) {
3032b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3033b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2]; k++) {
3034b7f5c055SJed Brown             for (PetscInt l = 0; l < 4; l++) {
3035b7f5c055SJed Brown               vtxCoords[vcount++] = (2 * i - 1) * L;
3036b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * j * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3037b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * k * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3038b7f5c055SJed Brown             }
3039b7f5c055SJed Brown           }
3040b7f5c055SJed Brown         }
3041b7f5c055SJed Brown       }
3042b7f5c055SJed Brown       // y-normal pipes
3043b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0]; i++) {
3044b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1] + 1; j++) {
3045b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2]; k++) {
3046b7f5c055SJed Brown             for (PetscInt l = 0; l < 4; l++) {
3047b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * i * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3048b7f5c055SJed Brown               vtxCoords[vcount++] = (2 * j - 1) * L;
3049b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * k * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3050b7f5c055SJed Brown             }
3051b7f5c055SJed Brown           }
3052b7f5c055SJed Brown         }
3053b7f5c055SJed Brown       }
3054b7f5c055SJed Brown       // z-normal pipes
3055b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0]; i++) {
3056b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3057b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2] + 1; k++) {
3058b7f5c055SJed Brown             for (PetscInt l = 0; l < 4; l++) {
3059b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * i * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3060b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * j * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3061b7f5c055SJed Brown               vtxCoords[vcount++] = (2 * k - 1) * L;
3062b7f5c055SJed Brown             }
3063b7f5c055SJed Brown           }
3064b7f5c055SJed Brown         }
3065b7f5c055SJed Brown       }
3066b7f5c055SJed Brown       // junctions
3067b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0]; i++) {
3068b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3069b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2]; k++) {
3070b7f5c055SJed Brown             const PetscInt J = (i * extent[1] + j) * extent[2] + k, Jvoff = (Npipes[0] + Npipes[1] + Npipes[2]) * 4 + J * 8;
3071b7f5c055SJed Brown             PetscCheck(vcount / 3 == Jvoff, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unexpected vertex count");
3072b7f5c055SJed Brown             for (PetscInt ii = 0; ii < 2; ii++) {
3073b7f5c055SJed Brown               for (PetscInt jj = 0; jj < 2; jj++) {
3074b7f5c055SJed Brown                 for (PetscInt kk = 0; kk < 2; kk++) {
3075b7f5c055SJed Brown                   double Ls           = (1 - sqrt(2) / 4) * L;
3076b7f5c055SJed Brown                   vtxCoords[vcount++] = 2 * i * L + (2 * ii - 1) * Ls;
3077b7f5c055SJed Brown                   vtxCoords[vcount++] = 2 * j * L + (2 * jj - 1) * Ls;
3078b7f5c055SJed Brown                   vtxCoords[vcount++] = 2 * k * L + (2 * kk - 1) * Ls;
3079b7f5c055SJed Brown                 }
3080b7f5c055SJed Brown               }
3081b7f5c055SJed Brown             }
3082b7f5c055SJed Brown             const PetscInt jfaces[3][2][4] = {
3083b7f5c055SJed Brown               {{3, 1, 0, 2}, {7, 5, 4, 6}}, // x-aligned
3084b7f5c055SJed Brown               {{5, 4, 0, 1}, {7, 6, 2, 3}}, // y-aligned
3085b7f5c055SJed Brown               {{6, 2, 0, 4}, {7, 3, 1, 5}}  // z-aligned
3086b7f5c055SJed Brown             };
3087b7f5c055SJed Brown             const PetscInt pipe_lo[3] = {// vertex numbers of pipes
30889371c9d4SSatish 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};
3089b7f5c055SJed Brown             const PetscInt pipe_hi[3] = {// vertex numbers of pipes
30909371c9d4SSatish 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};
3091b7f5c055SJed Brown             for (PetscInt dir = 0; dir < 3; dir++) { // x,y,z
3092b7f5c055SJed Brown               const PetscInt ijk[3] = {i, j, k};
3093b7f5c055SJed Brown               for (PetscInt l = 0; l < 4; l++) { // rotations
3094b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][0] = pipe_lo[dir] + l;
3095b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][1] = Jvoff + jfaces[dir][0][l];
3096b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][2] = Jvoff + jfaces[dir][0][(l - 1 + 4) % 4];
3097b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][3] = pipe_lo[dir] + (l - 1 + 4) % 4;
3098b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][0] = Jvoff + jfaces[dir][1][l];
3099b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][1] = pipe_hi[dir] + l;
3100b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][2] = pipe_hi[dir] + (l - 1 + 4) % 4;
3101b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][3] = Jvoff + jfaces[dir][1][(l - 1 + 4) % 4];
3102b7f5c055SJed Brown                 if (ijk[dir] == 0) {
3103b7f5c055SJed Brown                   edges[numEdges][0] = pipe_lo[dir] + l;
3104b7f5c055SJed Brown                   edges[numEdges][1] = pipe_lo[dir] + (l + 1) % 4;
3105b7f5c055SJed Brown                   edgeSets[numEdges] = dir * 2 + 1;
3106b7f5c055SJed Brown                   numEdges++;
3107b7f5c055SJed Brown                 }
3108b7f5c055SJed Brown                 if (ijk[dir] + 1 == extent[dir]) {
3109b7f5c055SJed Brown                   edges[numEdges][0] = pipe_hi[dir] + l;
3110b7f5c055SJed Brown                   edges[numEdges][1] = pipe_hi[dir] + (l + 1) % 4;
3111b7f5c055SJed Brown                   edgeSets[numEdges] = dir * 2 + 2;
3112b7f5c055SJed Brown                   numEdges++;
3113b7f5c055SJed Brown                 }
3114b7f5c055SJed Brown               }
3115b7f5c055SJed Brown             }
3116b7f5c055SJed Brown           }
3117b7f5c055SJed Brown         }
3118b7f5c055SJed Brown       }
311963a3b9bcSJacob Faibussowitsch       PetscCheck(numEdges == Ncuts * 4, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Edge count %" PetscInt_FMT " incompatible with number of cuts %" PetscInt_FMT, numEdges, Ncuts);
3120b7f5c055SJed Brown       numFaces   = 24 * Njunctions;
3121b7f5c055SJed Brown       cells_flat = cells[0][0][0];
3122b7f5c055SJed Brown     }
3123b7f5c055SJed Brown     evalFunc   = TPSEvaluate_SchwarzP;
31244663dae6SJed Brown     normalFunc = TPSExtrudeNormalFunc_SchwarzP;
3125b7f5c055SJed Brown     break;
3126b7f5c055SJed Brown   case DMPLEX_TPS_GYROID:
3127c5853193SPierre Jolivet     if (rank == 0) {
3128b7f5c055SJed Brown       // This is a coarse mesh approximation of the gyroid shifted to being the zero of the level set
3129b7f5c055SJed Brown       //
3130b7f5c055SJed 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)
3131b7f5c055SJed Brown       //
3132b7f5c055SJed Brown       // on the cell [0,2]^3.
3133b7f5c055SJed Brown       //
3134b7f5c055SJed Brown       // Think about dividing that cell into four columns, and focus on the column [0,1]x[0,1]x[0,2].
3135b7f5c055SJed Brown       // If you looked at the gyroid in that column at different slices of z you would see that it kind of spins
3136b7f5c055SJed Brown       // like a boomerang:
3137b7f5c055SJed Brown       //
3138b7f5c055SJed Brown       //     z = 0          z = 1/4        z = 1/2        z = 3/4     //
3139b7f5c055SJed Brown       //     -----          -------        -------        -------     //
3140b7f5c055SJed Brown       //                                                              //
3141b7f5c055SJed Brown       //     +       +      +       +      +       +      +   \   +   //
3142b7f5c055SJed Brown       //      \                                   /            \      //
3143b7f5c055SJed Brown       //       \            `-_   _-'            /              }     //
3144b7f5c055SJed Brown       //        *-_            `-'            _-'              /      //
3145b7f5c055SJed Brown       //     +     `-+      +       +      +-'     +      +   /   +   //
3146b7f5c055SJed Brown       //                                                              //
3147b7f5c055SJed Brown       //                                                              //
3148b7f5c055SJed Brown       //     z = 1          z = 5/4        z = 3/2        z = 7/4     //
3149b7f5c055SJed Brown       //     -----          -------        -------        -------     //
3150b7f5c055SJed Brown       //                                                              //
3151b7f5c055SJed Brown       //     +-_     +      +       +      +     _-+      +   /   +   //
3152b7f5c055SJed Brown       //        `-_            _-_            _-`            /        //
3153b7f5c055SJed Brown       //           \        _-'   `-_        /              {         //
3154b7f5c055SJed Brown       //            \                       /                \        //
3155b7f5c055SJed Brown       //     +       +      +       +      +       +      +   \   +   //
3156b7f5c055SJed Brown       //
3157b7f5c055SJed Brown       //
3158b7f5c055SJed Brown       // This course mesh approximates each of these slices by two line segments,
3159b7f5c055SJed Brown       // and then connects the segments in consecutive layers with quadrilateral faces.
3160b7f5c055SJed Brown       // All of the end points of the segments are multiples of 1/4 except for the
3161b7f5c055SJed Brown       // point * in the picture for z = 0 above and the similar points in other layers.
3162b7f5c055SJed Brown       // That point is at (gamma, gamma, 0), where gamma is calculated below.
3163b7f5c055SJed Brown       //
3164b7f5c055SJed Brown       // The column  [1,2]x[1,2]x[0,2] looks the same as this column;
3165b7f5c055SJed Brown       // The columns [1,2]x[0,1]x[0,2] and [0,1]x[1,2]x[0,2] are mirror images.
3166b7f5c055SJed Brown       //
3167b7f5c055SJed Brown       // As for how this method turned into the names given to the vertices:
3168b7f5c055SJed Brown       // that was not systematic, it was just the way it worked out in my handwritten notes.
3169b7f5c055SJed Brown 
3170b7f5c055SJed Brown       PetscInt facesPerBlock = 64;
3171b7f5c055SJed Brown       PetscInt vertsPerBlock = 56;
3172b7f5c055SJed Brown       PetscInt extentPlus[3];
3173b7f5c055SJed Brown       PetscInt numBlocks, numBlocksPlus;
31749371c9d4SSatish 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;
31759371c9d4SSatish Balay       const PetscInt pattern[64][4] = {
31769371c9d4SSatish Balay   /* face to vertex within the coarse discretization of a single gyroid block */
3177b7f5c055SJed Brown   /* layer 0 */
31789371c9d4SSatish Balay         {A,           C,           K,           G          },
31799371c9d4SSatish Balay         {C,           B,           II,          K          },
31809371c9d4SSatish Balay         {D,           A,           H,           L          },
31819371c9d4SSatish Balay         {B + 56 * 1,  D,           L,           J          },
31829371c9d4SSatish Balay         {E,           B + 56 * 1,  J,           N          },
31839371c9d4SSatish Balay         {A + 56 * 2,  E,           N,           H + 56 * 2 },
31849371c9d4SSatish Balay         {F,           A + 56 * 2,  G + 56 * 2,  M          },
31859371c9d4SSatish Balay         {B,           F,           M,           II         },
3186b7f5c055SJed Brown  /* layer 1 */
31879371c9d4SSatish Balay         {G,           K,           Q,           O          },
31889371c9d4SSatish Balay         {K,           II,          P,           Q          },
31899371c9d4SSatish Balay         {L,           H,           O + 56 * 1,  R          },
31909371c9d4SSatish Balay         {J,           L,           R,           P          },
31919371c9d4SSatish Balay         {N,           J,           P,           S          },
31929371c9d4SSatish Balay         {H + 56 * 2,  N,           S,           O + 56 * 3 },
31939371c9d4SSatish Balay         {M,           G + 56 * 2,  O + 56 * 2,  T          },
31949371c9d4SSatish Balay         {II,          M,           T,           P          },
3195b7f5c055SJed Brown  /* layer 2 */
31969371c9d4SSatish Balay         {O,           Q,           Y,           U          },
31979371c9d4SSatish Balay         {Q,           P,           W,           Y          },
31989371c9d4SSatish Balay         {R,           O + 56 * 1,  U + 56 * 1,  Ap         },
31999371c9d4SSatish Balay         {P,           R,           Ap,          W          },
32009371c9d4SSatish Balay         {S,           P,           X,           Bp         },
32019371c9d4SSatish Balay         {O + 56 * 3,  S,           Bp,          V + 56 * 1 },
32029371c9d4SSatish Balay         {T,           O + 56 * 2,  V,           Z          },
32039371c9d4SSatish Balay         {P,           T,           Z,           X          },
3204b7f5c055SJed Brown  /* layer 3 */
32059371c9d4SSatish Balay         {U,           Y,           Ep,          Dp         },
32069371c9d4SSatish Balay         {Y,           W,           Cp,          Ep         },
32079371c9d4SSatish Balay         {Ap,          U + 56 * 1,  Dp + 56 * 1, Gp         },
32089371c9d4SSatish Balay         {W,           Ap,          Gp,          Cp         },
32099371c9d4SSatish Balay         {Bp,          X,           Cp + 56 * 2, Fp         },
32109371c9d4SSatish Balay         {V + 56 * 1,  Bp,          Fp,          Dp + 56 * 1},
32119371c9d4SSatish Balay         {Z,           V,           Dp,          Hp         },
32129371c9d4SSatish Balay         {X,           Z,           Hp,          Cp + 56 * 2},
3213b7f5c055SJed Brown  /* layer 4 */
32149371c9d4SSatish Balay         {Dp,          Ep,          Mp,          Kp         },
32159371c9d4SSatish Balay         {Ep,          Cp,          Ip,          Mp         },
32169371c9d4SSatish Balay         {Gp,          Dp + 56 * 1, Lp,          Np         },
32179371c9d4SSatish Balay         {Cp,          Gp,          Np,          Jp         },
32189371c9d4SSatish Balay         {Fp,          Cp + 56 * 2, Jp + 56 * 2, Pp         },
32199371c9d4SSatish Balay         {Dp + 56 * 1, Fp,          Pp,          Lp         },
32209371c9d4SSatish Balay         {Hp,          Dp,          Kp,          Op         },
32219371c9d4SSatish Balay         {Cp + 56 * 2, Hp,          Op,          Ip + 56 * 2},
3222b7f5c055SJed Brown  /* layer 5 */
32239371c9d4SSatish Balay         {Kp,          Mp,          Sp,          Rp         },
32249371c9d4SSatish Balay         {Mp,          Ip,          Qp,          Sp         },
32259371c9d4SSatish Balay         {Np,          Lp,          Rp,          Tp         },
32269371c9d4SSatish Balay         {Jp,          Np,          Tp,          Qp + 56 * 1},
32279371c9d4SSatish Balay         {Pp,          Jp + 56 * 2, Qp + 56 * 3, Up         },
32289371c9d4SSatish Balay         {Lp,          Pp,          Up,          Rp         },
32299371c9d4SSatish Balay         {Op,          Kp,          Rp,          Vp         },
32309371c9d4SSatish Balay         {Ip + 56 * 2, Op,          Vp,          Qp + 56 * 2},
3231b7f5c055SJed Brown  /* layer 6 */
32329371c9d4SSatish Balay         {Rp,          Sp,          Aq,          Yp         },
32339371c9d4SSatish Balay         {Sp,          Qp,          Wp,          Aq         },
32349371c9d4SSatish Balay         {Tp,          Rp,          Yp,          Cq         },
32359371c9d4SSatish Balay         {Qp + 56 * 1, Tp,          Cq,          Wp + 56 * 1},
32369371c9d4SSatish Balay         {Up,          Qp + 56 * 3, Xp + 56 * 1, Dq         },
32379371c9d4SSatish Balay         {Rp,          Up,          Dq,          Zp         },
32389371c9d4SSatish Balay         {Vp,          Rp,          Zp,          Bq         },
32399371c9d4SSatish Balay         {Qp + 56 * 2, Vp,          Bq,          Xp         },
3240b7f5c055SJed Brown  /* layer 7 (the top is the periodic image of the bottom of layer 0) */
32419371c9d4SSatish Balay         {Yp,          Aq,          C + 56 * 4,  A + 56 * 4 },
32429371c9d4SSatish Balay         {Aq,          Wp,          B + 56 * 4,  C + 56 * 4 },
32439371c9d4SSatish Balay         {Cq,          Yp,          A + 56 * 4,  D + 56 * 4 },
32449371c9d4SSatish Balay         {Wp + 56 * 1, Cq,          D + 56 * 4,  B + 56 * 5 },
32459371c9d4SSatish Balay         {Dq,          Xp + 56 * 1, B + 56 * 5,  E + 56 * 4 },
32469371c9d4SSatish Balay         {Zp,          Dq,          E + 56 * 4,  A + 56 * 6 },
32479371c9d4SSatish Balay         {Bq,          Zp,          A + 56 * 6,  F + 56 * 4 },
32489371c9d4SSatish Balay         {Xp,          Bq,          F + 56 * 4,  B + 56 * 4 }
3249b7f5c055SJed Brown       };
3250b7f5c055SJed Brown       const PetscReal gamma                = PetscAcosReal((PetscSqrtReal(3.) - 1.) / PetscSqrtReal(2.)) / PETSC_PI;
32519371c9d4SSatish Balay       const PetscReal patternCoords[56][3] = {
3252bee3fc89SBarry Smith         {1.,        0.,        0.  }, /* A  */
3253bee3fc89SBarry Smith         {0.,        1.,        0.  }, /* B  */
3254bee3fc89SBarry Smith         {gamma,     gamma,     0.  }, /* C  */
3255bee3fc89SBarry Smith         {1 + gamma, 1 - gamma, 0.  }, /* D  */
3256bee3fc89SBarry Smith         {2 - gamma, 2 - gamma, 0.  }, /* E  */
3257bee3fc89SBarry Smith         {1 - gamma, 1 + gamma, 0.  }, /* F  */
3258b7f5c055SJed Brown 
3259bee3fc89SBarry Smith         {.5,        0,         .25 }, /* G  */
3260bee3fc89SBarry Smith         {1.5,       0.,        .25 }, /* H  */
3261bee3fc89SBarry Smith         {.5,        1.,        .25 }, /* II */
3262bee3fc89SBarry Smith         {1.5,       1.,        .25 }, /* J  */
3263bee3fc89SBarry Smith         {.25,       .5,        .25 }, /* K  */
3264bee3fc89SBarry Smith         {1.25,      .5,        .25 }, /* L  */
3265bee3fc89SBarry Smith         {.75,       1.5,       .25 }, /* M  */
3266bee3fc89SBarry Smith         {1.75,      1.5,       .25 }, /* N  */
3267b7f5c055SJed Brown 
3268bee3fc89SBarry Smith         {0.,        0.,        .5  }, /* O  */
3269bee3fc89SBarry Smith         {1.,        1.,        .5  }, /* P  */
3270bee3fc89SBarry Smith         {gamma,     1 - gamma, .5  }, /* Q  */
3271bee3fc89SBarry Smith         {1 + gamma, gamma,     .5  }, /* R  */
3272bee3fc89SBarry Smith         {2 - gamma, 1 + gamma, .5  }, /* S  */
3273bee3fc89SBarry Smith         {1 - gamma, 2 - gamma, .5  }, /* T  */
3274b7f5c055SJed Brown 
3275bee3fc89SBarry Smith         {0.,        .5,        .75 }, /* U  */
3276bee3fc89SBarry Smith         {0.,        1.5,       .75 }, /* V  */
3277bee3fc89SBarry Smith         {1.,        .5,        .75 }, /* W  */
3278bee3fc89SBarry Smith         {1.,        1.5,       .75 }, /* X  */
3279bee3fc89SBarry Smith         {.5,        .75,       .75 }, /* Y  */
3280bee3fc89SBarry Smith         {.5,        1.75,      .75 }, /* Z  */
3281bee3fc89SBarry Smith         {1.5,       .25,       .75 }, /* Ap */
3282bee3fc89SBarry Smith         {1.5,       1.25,      .75 }, /* Bp */
3283b7f5c055SJed Brown 
3284bee3fc89SBarry Smith         {1.,        0.,        1.  }, /* Cp */
3285bee3fc89SBarry Smith         {0.,        1.,        1.  }, /* Dp */
3286bee3fc89SBarry Smith         {1 - gamma, 1 - gamma, 1.  }, /* Ep */
3287bee3fc89SBarry Smith         {1 + gamma, 1 + gamma, 1.  }, /* Fp */
3288bee3fc89SBarry Smith         {2 - gamma, gamma,     1.  }, /* Gp */
3289bee3fc89SBarry Smith         {gamma,     2 - gamma, 1.  }, /* Hp */
3290b7f5c055SJed Brown 
3291bee3fc89SBarry Smith         {.5,        0.,        1.25}, /* Ip */
3292bee3fc89SBarry Smith         {1.5,       0.,        1.25}, /* Jp */
3293bee3fc89SBarry Smith         {.5,        1.,        1.25}, /* Kp */
3294bee3fc89SBarry Smith         {1.5,       1.,        1.25}, /* Lp */
3295bee3fc89SBarry Smith         {.75,       .5,        1.25}, /* Mp */
3296bee3fc89SBarry Smith         {1.75,      .5,        1.25}, /* Np */
3297bee3fc89SBarry Smith         {.25,       1.5,       1.25}, /* Op */
3298bee3fc89SBarry Smith         {1.25,      1.5,       1.25}, /* Pp */
3299b7f5c055SJed Brown 
3300bee3fc89SBarry Smith         {0.,        0.,        1.5 }, /* Qp */
3301bee3fc89SBarry Smith         {1.,        1.,        1.5 }, /* Rp */
3302bee3fc89SBarry Smith         {1 - gamma, gamma,     1.5 }, /* Sp */
3303bee3fc89SBarry Smith         {2 - gamma, 1 - gamma, 1.5 }, /* Tp */
3304bee3fc89SBarry Smith         {1 + gamma, 2 - gamma, 1.5 }, /* Up */
3305bee3fc89SBarry Smith         {gamma,     1 + gamma, 1.5 }, /* Vp */
3306b7f5c055SJed Brown 
3307bee3fc89SBarry Smith         {0.,        .5,        1.75}, /* Wp */
3308bee3fc89SBarry Smith         {0.,        1.5,       1.75}, /* Xp */
3309bee3fc89SBarry Smith         {1.,        .5,        1.75}, /* Yp */
3310bee3fc89SBarry Smith         {1.,        1.5,       1.75}, /* Zp */
3311bee3fc89SBarry Smith         {.5,        .25,       1.75}, /* Aq */
3312bee3fc89SBarry Smith         {.5,        1.25,      1.75}, /* Bq */
3313bee3fc89SBarry Smith         {1.5,       .75,       1.75}, /* Cq */
3314bee3fc89SBarry Smith         {1.5,       1.75,      1.75}, /* Dq */
3315b7f5c055SJed Brown       };
3316b7f5c055SJed Brown       PetscInt(*cells)[64][4] = NULL;
3317b7f5c055SJed Brown       PetscBool *seen;
3318b7f5c055SJed Brown       PetscInt  *vertToTrueVert;
3319b7f5c055SJed Brown       PetscInt   count;
3320b7f5c055SJed Brown 
3321b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) extentPlus[i] = extent[i] + 1;
3322b7f5c055SJed Brown       numBlocks = 1;
3323b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) numBlocks *= extent[i];
3324b7f5c055SJed Brown       numBlocksPlus = 1;
3325b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) numBlocksPlus *= extentPlus[i];
3326b7f5c055SJed Brown       numFaces = numBlocks * facesPerBlock;
33279566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numBlocks, &cells));
33289566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numBlocksPlus * vertsPerBlock, &seen));
3329b7f5c055SJed Brown       for (PetscInt k = 0; k < extent[2]; k++) {
3330b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3331b7f5c055SJed Brown           for (PetscInt i = 0; i < extent[0]; i++) {
3332b7f5c055SJed Brown             for (PetscInt f = 0; f < facesPerBlock; f++) {
3333b7f5c055SJed Brown               for (PetscInt v = 0; v < 4; v++) {
3334b7f5c055SJed Brown                 PetscInt vertRaw     = pattern[f][v];
3335b7f5c055SJed Brown                 PetscInt blockidx    = vertRaw / 56;
3336b7f5c055SJed Brown                 PetscInt patternvert = vertRaw % 56;
3337b7f5c055SJed Brown                 PetscInt xplus       = (blockidx & 1);
3338b7f5c055SJed Brown                 PetscInt yplus       = (blockidx & 2) >> 1;
3339b7f5c055SJed Brown                 PetscInt zplus       = (blockidx & 4) >> 2;
3340b7f5c055SJed Brown                 PetscInt zcoord      = (periodic && periodic[2] == DM_BOUNDARY_PERIODIC) ? ((k + zplus) % extent[2]) : (k + zplus);
3341b7f5c055SJed Brown                 PetscInt ycoord      = (periodic && periodic[1] == DM_BOUNDARY_PERIODIC) ? ((j + yplus) % extent[1]) : (j + yplus);
3342b7f5c055SJed Brown                 PetscInt xcoord      = (periodic && periodic[0] == DM_BOUNDARY_PERIODIC) ? ((i + xplus) % extent[0]) : (i + xplus);
3343b7f5c055SJed Brown                 PetscInt vert        = ((zcoord * extentPlus[1] + ycoord) * extentPlus[0] + xcoord) * 56 + patternvert;
3344b7f5c055SJed Brown 
3345b7f5c055SJed Brown                 cells[(k * extent[1] + j) * extent[0] + i][f][v] = vert;
3346b7f5c055SJed Brown                 seen[vert]                                       = PETSC_TRUE;
3347b7f5c055SJed Brown               }
3348b7f5c055SJed Brown             }
3349b7f5c055SJed Brown           }
3350b7f5c055SJed Brown         }
3351b7f5c055SJed Brown       }
33529371c9d4SSatish Balay       for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++)
33539371c9d4SSatish Balay         if (seen[i]) numVertices++;
3354b7f5c055SJed Brown       count = 0;
33559566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numBlocksPlus * vertsPerBlock, &vertToTrueVert));
33569566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numVertices * 3, &vtxCoords));
3357b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++) vertToTrueVert[i] = -1;
3358b7f5c055SJed Brown       for (PetscInt k = 0; k < extentPlus[2]; k++) {
3359b7f5c055SJed Brown         for (PetscInt j = 0; j < extentPlus[1]; j++) {
3360b7f5c055SJed Brown           for (PetscInt i = 0; i < extentPlus[0]; i++) {
3361b7f5c055SJed Brown             for (PetscInt v = 0; v < vertsPerBlock; v++) {
3362b7f5c055SJed Brown               PetscInt vIdx = ((k * extentPlus[1] + j) * extentPlus[0] + i) * vertsPerBlock + v;
3363b7f5c055SJed Brown 
3364b7f5c055SJed Brown               if (seen[vIdx]) {
3365b7f5c055SJed Brown                 PetscInt thisVert;
3366b7f5c055SJed Brown 
3367b7f5c055SJed Brown                 vertToTrueVert[vIdx] = thisVert = count++;
3368b7f5c055SJed Brown 
3369b7f5c055SJed Brown                 for (PetscInt d = 0; d < 3; d++) vtxCoords[3 * thisVert + d] = patternCoords[v][d];
3370b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 0] += i * 2;
3371b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 1] += j * 2;
3372b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 2] += k * 2;
3373b7f5c055SJed Brown               }
3374b7f5c055SJed Brown             }
3375b7f5c055SJed Brown           }
3376b7f5c055SJed Brown         }
3377b7f5c055SJed Brown       }
3378b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocks; i++) {
3379b7f5c055SJed Brown         for (PetscInt f = 0; f < facesPerBlock; f++) {
3380ad540459SPierre Jolivet           for (PetscInt v = 0; v < 4; v++) cells[i][f][v] = vertToTrueVert[cells[i][f][v]];
3381b7f5c055SJed Brown         }
3382b7f5c055SJed Brown       }
33839566063dSJacob Faibussowitsch       PetscCall(PetscFree(vertToTrueVert));
33849566063dSJacob Faibussowitsch       PetscCall(PetscFree(seen));
3385b7f5c055SJed Brown       cells_flat = cells[0][0];
3386b7f5c055SJed Brown       numEdges   = 0;
3387b7f5c055SJed Brown       for (PetscInt i = 0; i < numFaces; i++) {
3388b7f5c055SJed Brown         for (PetscInt e = 0; e < 4; e++) {
3389b7f5c055SJed Brown           PetscInt         ev[]       = {cells_flat[i * 4 + e], cells_flat[i * 4 + ((e + 1) % 4)]};
3390b7f5c055SJed Brown           const PetscReal *evCoords[] = {&vtxCoords[3 * ev[0]], &vtxCoords[3 * ev[1]]};
3391b7f5c055SJed Brown 
3392b7f5c055SJed Brown           for (PetscInt d = 0; d < 3; d++) {
3393b7f5c055SJed Brown             if (!periodic || periodic[0] != DM_BOUNDARY_PERIODIC) {
3394b7f5c055SJed Brown               if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) numEdges++;
3395b7f5c055SJed Brown               if (evCoords[0][d] == 2. * extent[d] && evCoords[1][d] == 2. * extent[d]) numEdges++;
3396b7f5c055SJed Brown             }
3397b7f5c055SJed Brown           }
3398b7f5c055SJed Brown         }
3399b7f5c055SJed Brown       }
34009566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numEdges, &edges));
34019566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numEdges, &edgeSets));
3402b7f5c055SJed Brown       for (PetscInt edge = 0, i = 0; i < numFaces; i++) {
3403b7f5c055SJed Brown         for (PetscInt e = 0; e < 4; e++) {
3404b7f5c055SJed Brown           PetscInt         ev[]       = {cells_flat[i * 4 + e], cells_flat[i * 4 + ((e + 1) % 4)]};
3405b7f5c055SJed Brown           const PetscReal *evCoords[] = {&vtxCoords[3 * ev[0]], &vtxCoords[3 * ev[1]]};
3406b7f5c055SJed Brown 
3407b7f5c055SJed Brown           for (PetscInt d = 0; d < 3; d++) {
3408b7f5c055SJed Brown             if (!periodic || periodic[d] != DM_BOUNDARY_PERIODIC) {
3409b7f5c055SJed Brown               if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) {
3410b7f5c055SJed Brown                 edges[edge][0]   = ev[0];
3411b7f5c055SJed Brown                 edges[edge][1]   = ev[1];
3412b7f5c055SJed Brown                 edgeSets[edge++] = 2 * d;
3413b7f5c055SJed Brown               }
3414b7f5c055SJed Brown               if (evCoords[0][d] == 2. * extent[d] && evCoords[1][d] == 2. * extent[d]) {
3415b7f5c055SJed Brown                 edges[edge][0]   = ev[0];
3416b7f5c055SJed Brown                 edges[edge][1]   = ev[1];
3417b7f5c055SJed Brown                 edgeSets[edge++] = 2 * d + 1;
3418b7f5c055SJed Brown               }
3419b7f5c055SJed Brown             }
3420b7f5c055SJed Brown           }
3421b7f5c055SJed Brown         }
3422b7f5c055SJed Brown       }
3423b7f5c055SJed Brown     }
3424b7f5c055SJed Brown     evalFunc   = TPSEvaluate_Gyroid;
34254663dae6SJed Brown     normalFunc = TPSExtrudeNormalFunc_Gyroid;
3426b7f5c055SJed Brown     break;
3427b7f5c055SJed Brown   }
3428b7f5c055SJed Brown 
34299566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, topoDim));
3430c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildFromCellList(dm, numFaces, numVertices, 4, cells_flat));
34319566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildFromCellList(dm, 0, 0, 0, NULL));
34329566063dSJacob Faibussowitsch   PetscCall(PetscFree(cells_flat));
3433b7f5c055SJed Brown   {
3434b7f5c055SJed Brown     DM idm;
34359566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(dm, &idm));
343669d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &idm));
3437b7f5c055SJed Brown   }
3438c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildCoordinatesFromCellList(dm, spaceDim, vtxCoords));
34399566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildCoordinatesFromCellList(dm, spaceDim, NULL));
34409566063dSJacob Faibussowitsch   PetscCall(PetscFree(vtxCoords));
3441b7f5c055SJed Brown 
34429566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
34439566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "Face Sets", &label));
3444b7f5c055SJed Brown   for (PetscInt e = 0; e < numEdges; e++) {
3445b7f5c055SJed Brown     PetscInt        njoin;
3446b7f5c055SJed Brown     const PetscInt *join, verts[] = {numFaces + edges[e][0], numFaces + edges[e][1]};
34479566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, 2, verts, &njoin, &join));
344863a3b9bcSJacob 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]);
34499566063dSJacob Faibussowitsch     PetscCall(DMLabelSetValue(label, join[0], edgeSets[e]));
34509566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, 2, verts, &njoin, &join));
3451b7f5c055SJed Brown   }
34529566063dSJacob Faibussowitsch   PetscCall(PetscFree(edges));
34539566063dSJacob Faibussowitsch   PetscCall(PetscFree(edgeSets));
34541436d7faSJed Brown   if (tps_distribute) {
34551436d7faSJed Brown     DM               pdm = NULL;
34561436d7faSJed Brown     PetscPartitioner part;
34571436d7faSJed Brown 
34589566063dSJacob Faibussowitsch     PetscCall(DMPlexGetPartitioner(dm, &part));
34599566063dSJacob Faibussowitsch     PetscCall(PetscPartitionerSetFromOptions(part));
34609566063dSJacob Faibussowitsch     PetscCall(DMPlexDistribute(dm, 0, NULL, &pdm));
346148a46eb9SPierre Jolivet     if (pdm) PetscCall(DMPlexReplace_Internal(dm, &pdm));
34621436d7faSJed Brown     // Do not auto-distribute again
34639566063dSJacob Faibussowitsch     PetscCall(DMPlexDistributeSetDefault(dm, PETSC_FALSE));
34641436d7faSJed Brown   }
3465b7f5c055SJed Brown 
34669566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
3467b7f5c055SJed Brown   for (PetscInt refine = 0; refine < refinements; refine++) {
3468b7f5c055SJed Brown     PetscInt     m;
3469b7f5c055SJed Brown     DM           dmf;
3470b7f5c055SJed Brown     Vec          X;
3471b7f5c055SJed Brown     PetscScalar *x;
34729566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, MPI_COMM_NULL, &dmf));
347369d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &dmf));
3474b7f5c055SJed Brown 
34759566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &X));
34769566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(X, &m));
34779566063dSJacob Faibussowitsch     PetscCall(VecGetArray(X, &x));
347848a46eb9SPierre Jolivet     for (PetscInt i = 0; i < m; i += 3) PetscCall(TPSNearestPoint(evalFunc, &x[i]));
34799566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(X, &x));
3480b7f5c055SJed Brown   }
3481b7f5c055SJed Brown 
3482b7f5c055SJed Brown   // Face Sets has already been propagated to new vertices during refinement; this propagates to the initial vertices.
34839566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "Face Sets", &label));
34849566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete(dm, label));
3485b7f5c055SJed Brown 
348646139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
348746139095SJed Brown 
3488b7f5c055SJed Brown   if (thickness > 0) {
34894663dae6SJed Brown     DM              edm, cdm, ecdm;
34904663dae6SJed Brown     DMPlexTransform tr;
34914663dae6SJed Brown     const char     *prefix;
34924663dae6SJed Brown     PetscOptions    options;
34934663dae6SJed Brown     // Code from DMPlexExtrude
34944663dae6SJed Brown     PetscCall(DMPlexTransformCreate(PetscObjectComm((PetscObject)dm), &tr));
34954663dae6SJed Brown     PetscCall(DMPlexTransformSetDM(tr, dm));
34964663dae6SJed Brown     PetscCall(DMPlexTransformSetType(tr, DMPLEXEXTRUDE));
34974663dae6SJed Brown     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, &prefix));
34984663dae6SJed Brown     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)tr, prefix));
34994663dae6SJed Brown     PetscCall(PetscObjectGetOptions((PetscObject)dm, &options));
35004663dae6SJed Brown     PetscCall(PetscObjectSetOptions((PetscObject)tr, options));
35014663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetLayers(tr, layers));
35024663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetThickness(tr, thickness));
35034663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetTensor(tr, PETSC_FALSE));
35044663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetSymmetric(tr, PETSC_TRUE));
35054663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetNormalFunction(tr, normalFunc));
35064663dae6SJed Brown     PetscCall(DMPlexTransformSetFromOptions(tr));
35074663dae6SJed Brown     PetscCall(PetscObjectSetOptions((PetscObject)tr, NULL));
35084663dae6SJed Brown     PetscCall(DMPlexTransformSetUp(tr));
35094663dae6SJed Brown     PetscCall(PetscObjectViewFromOptions((PetscObject)tr, NULL, "-dm_plex_tps_transform_view"));
35104663dae6SJed Brown     PetscCall(DMPlexTransformApply(tr, dm, &edm));
35114663dae6SJed Brown     PetscCall(DMCopyDisc(dm, edm));
35124663dae6SJed Brown     PetscCall(DMGetCoordinateDM(dm, &cdm));
35134663dae6SJed Brown     PetscCall(DMGetCoordinateDM(edm, &ecdm));
35144663dae6SJed Brown     PetscCall(DMCopyDisc(cdm, ecdm));
35154663dae6SJed Brown     PetscCall(DMPlexTransformCreateDiscLabels(tr, edm));
35164663dae6SJed Brown     PetscCall(DMPlexTransformDestroy(&tr));
35174663dae6SJed Brown     if (edm) {
35184663dae6SJed Brown       ((DM_Plex *)edm->data)->printFEM    = ((DM_Plex *)dm->data)->printFEM;
35194663dae6SJed Brown       ((DM_Plex *)edm->data)->printL2     = ((DM_Plex *)dm->data)->printL2;
3520f5867de0SMatthew G. Knepley       ((DM_Plex *)edm->data)->printLocate = ((DM_Plex *)dm->data)->printLocate;
35214663dae6SJed Brown     }
352269d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &edm));
3523b7f5c055SJed Brown   }
35243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3525b7f5c055SJed Brown }
3526b7f5c055SJed Brown 
3527b7f5c055SJed Brown /*@
3528b7f5c055SJed Brown   DMPlexCreateTPSMesh - Create a distributed, interpolated mesh of a triply-periodic surface
3529b7f5c055SJed Brown 
3530b7f5c055SJed Brown   Collective
3531b7f5c055SJed Brown 
3532b7f5c055SJed Brown   Input Parameters:
3533a1cb98faSBarry Smith + comm           - The communicator for the `DM` object
3534b7f5c055SJed Brown . tpstype        - Type of triply-periodic surface
3535b7f5c055SJed Brown . extent         - Array of length 3 containing number of periods in each direction
353620f4b53cSBarry Smith . periodic       - array of length 3 with periodicity, or `NULL` for non-periodic
35371436d7faSJed Brown . tps_distribute - Distribute 2D manifold mesh prior to refinement and extrusion (more scalable)
3538817da375SSatish Balay . refinements    - Number of factor-of-2 refinements of 2D manifold mesh
35391436d7faSJed Brown . layers         - Number of cell layers extruded in normal direction
3540817da375SSatish Balay - thickness      - Thickness in normal direction
3541b7f5c055SJed Brown 
3542b7f5c055SJed Brown   Output Parameter:
3543a1cb98faSBarry Smith . dm - The `DM` object
3544a1cb98faSBarry Smith 
3545a1cb98faSBarry Smith   Level: beginner
3546b7f5c055SJed Brown 
3547b7f5c055SJed Brown   Notes:
3548b7f5c055SJed 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.
35491d27aa22SBarry Smith   <https://en.wikipedia.org/wiki/Schwarz_minimal_surface#Schwarz_P_(%22Primitive%22)> and can be cut with "clean" boundaries.
35501d27aa22SBarry Smith   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.
3551b7f5c055SJed Brown   Our implementation creates a very coarse mesh of the surface and refines (by 4-way splitting) as many times as requested.
3552b7f5c055SJed Brown   On each refinement, all vertices are projected to their nearest point on the surface.
3553b7f5c055SJed Brown   This projection could readily be extended to related surfaces.
3554b7f5c055SJed Brown 
35551d27aa22SBarry Smith   See {cite}`maskery2018insights`
35561d27aa22SBarry Smith 
35571d27aa22SBarry Smith   The face (edge) sets for the Schwarz P surface are numbered $1(-x), 2(+x), 3(-y), 4(+y), 5(-z), 6(+z)$.
35581d27aa22SBarry Smith   When the mesh is refined, "Face Sets" contain the new vertices (created during refinement).
35591d27aa22SBarry Smith   Use `DMPlexLabelComplete()` to propagate to coarse-level vertices.
3560b7f5c055SJed Brown 
356160225df5SJacob Faibussowitsch   Developer Notes:
3562b7f5c055SJed Brown   The Gyroid mesh does not currently mark boundary sets.
3563b7f5c055SJed Brown 
35641cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSphereMesh()`, `DMSetType()`, `DMCreate()`
3565b7f5c055SJed Brown @*/
3566d71ae5a4SJacob 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)
3567d71ae5a4SJacob Faibussowitsch {
3568b7f5c055SJed Brown   PetscFunctionBegin;
35699566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
35709566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
35719566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateTPSMesh_Internal(*dm, tpstype, extent, periodic, tps_distribute, refinements, layers, thickness));
35723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3573b7f5c055SJed Brown }
3574b7f5c055SJed Brown 
35759318fe57SMatthew G. Knepley /*@
35769318fe57SMatthew G. Knepley   DMPlexCreateSphereMesh - Creates a mesh on the d-dimensional sphere, S^d.
35779318fe57SMatthew G. Knepley 
35789318fe57SMatthew G. Knepley   Collective
35799318fe57SMatthew G. Knepley 
35809318fe57SMatthew G. Knepley   Input Parameters:
3581a1cb98faSBarry Smith + comm    - The communicator for the `DM` object
35829318fe57SMatthew G. Knepley . dim     - The dimension
35839318fe57SMatthew G. Knepley . simplex - Use simplices, or tensor product cells
35849318fe57SMatthew G. Knepley - R       - The radius
35859318fe57SMatthew G. Knepley 
35869318fe57SMatthew G. Knepley   Output Parameter:
3587a1cb98faSBarry Smith . dm - The `DM` object
35889318fe57SMatthew G. Knepley 
35899318fe57SMatthew G. Knepley   Level: beginner
35909318fe57SMatthew G. Knepley 
35911cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBallMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
35929318fe57SMatthew G. Knepley @*/
3593d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateSphereMesh(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscReal R, DM *dm)
3594d71ae5a4SJacob Faibussowitsch {
35959318fe57SMatthew G. Knepley   PetscFunctionBegin;
35964f572ea9SToby Isaac   PetscAssertPointer(dm, 5);
35979566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
35989566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
35999566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSphereMesh_Internal(*dm, dim, simplex, R));
36003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36019318fe57SMatthew G. Knepley }
36029318fe57SMatthew G. Knepley 
3603d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBallMesh_Internal(DM dm, PetscInt dim, PetscReal R)
3604d71ae5a4SJacob Faibussowitsch {
36059318fe57SMatthew G. Knepley   DM          sdm, vol;
36069318fe57SMatthew G. Knepley   DMLabel     bdlabel;
3607*dd2b43ebSStefano Zampini   const char *prefix;
36089318fe57SMatthew G. Knepley 
36099318fe57SMatthew G. Knepley   PetscFunctionBegin;
36109566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &sdm));
36119566063dSJacob Faibussowitsch   PetscCall(DMSetType(sdm, DMPLEX));
3612*dd2b43ebSStefano Zampini   PetscCall(DMGetOptionsPrefix(dm, &prefix));
3613*dd2b43ebSStefano Zampini   PetscCall(DMSetOptionsPrefix(sdm, prefix));
3614*dd2b43ebSStefano Zampini   PetscCall(DMAppendOptionsPrefix(sdm, "bd_"));
3615*dd2b43ebSStefano Zampini   PetscCall(DMPlexDistributeSetDefault(sdm, PETSC_FALSE));
36169566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSphereMesh_Internal(sdm, dim - 1, PETSC_TRUE, R));
36179566063dSJacob Faibussowitsch   PetscCall(DMSetFromOptions(sdm));
36189566063dSJacob Faibussowitsch   PetscCall(DMViewFromOptions(sdm, NULL, "-dm_view"));
36199566063dSJacob Faibussowitsch   PetscCall(DMPlexGenerate(sdm, NULL, PETSC_TRUE, &vol));
36209566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&sdm));
362169d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &vol));
36229566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
36239566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "marker", &bdlabel));
36249566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkBoundaryFaces(dm, PETSC_DETERMINE, bdlabel));
36259566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete(dm, bdlabel));
36263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
362751a74b61SMatthew G. Knepley }
362851a74b61SMatthew G. Knepley 
362951a74b61SMatthew G. Knepley /*@
363051a74b61SMatthew G. Knepley   DMPlexCreateBallMesh - Creates a simplex mesh on the d-dimensional ball, B^d.
363151a74b61SMatthew G. Knepley 
363251a74b61SMatthew G. Knepley   Collective
363351a74b61SMatthew G. Knepley 
363451a74b61SMatthew G. Knepley   Input Parameters:
3635a1cb98faSBarry Smith + comm - The communicator for the `DM` object
363651a74b61SMatthew G. Knepley . dim  - The dimension
363751a74b61SMatthew G. Knepley - R    - The radius
363851a74b61SMatthew G. Knepley 
363951a74b61SMatthew G. Knepley   Output Parameter:
3640a1cb98faSBarry Smith . dm - The `DM` object
364151a74b61SMatthew G. Knepley 
3642a1cb98faSBarry Smith   Options Database Key:
364360225df5SJacob Faibussowitsch . bd_dm_refine - This will refine the surface mesh preserving the sphere geometry
364451a74b61SMatthew G. Knepley 
364551a74b61SMatthew G. Knepley   Level: beginner
364651a74b61SMatthew G. Knepley 
36471cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSphereMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
364851a74b61SMatthew G. Knepley @*/
3649d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateBallMesh(MPI_Comm comm, PetscInt dim, PetscReal R, DM *dm)
3650d71ae5a4SJacob Faibussowitsch {
365151a74b61SMatthew G. Knepley   PetscFunctionBegin;
36529566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
36539566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
36549566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBallMesh_Internal(*dm, dim, R));
36553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36562829fed8SMatthew G. Knepley }
36572829fed8SMatthew G. Knepley 
3658d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateReferenceCell_Internal(DM rdm, DMPolytopeType ct)
3659d71ae5a4SJacob Faibussowitsch {
36600a6ba040SMatthew G. Knepley   PetscFunctionBegin;
36619318fe57SMatthew G. Knepley   switch (ct) {
36629371c9d4SSatish Balay   case DM_POLYTOPE_POINT: {
36639318fe57SMatthew G. Knepley     PetscInt    numPoints[1]        = {1};
36649318fe57SMatthew G. Knepley     PetscInt    coneSize[1]         = {0};
36659318fe57SMatthew G. Knepley     PetscInt    cones[1]            = {0};
36669318fe57SMatthew G. Knepley     PetscInt    coneOrientations[1] = {0};
36679318fe57SMatthew G. Knepley     PetscScalar vertexCoords[1]     = {0.0};
36689318fe57SMatthew G. Knepley 
36699566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 0));
36709566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 0, numPoints, coneSize, cones, coneOrientations, vertexCoords));
36719371c9d4SSatish Balay   } break;
36729371c9d4SSatish Balay   case DM_POLYTOPE_SEGMENT: {
36739318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {2, 1};
36749318fe57SMatthew G. Knepley     PetscInt    coneSize[3]         = {2, 0, 0};
36759318fe57SMatthew G. Knepley     PetscInt    cones[2]            = {1, 2};
36769318fe57SMatthew G. Knepley     PetscInt    coneOrientations[2] = {0, 0};
36779318fe57SMatthew G. Knepley     PetscScalar vertexCoords[2]     = {-1.0, 1.0};
36789318fe57SMatthew G. Knepley 
36799566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 1));
36809566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
36819371c9d4SSatish Balay   } break;
36829371c9d4SSatish Balay   case DM_POLYTOPE_POINT_PRISM_TENSOR: {
3683b5a892a1SMatthew G. Knepley     PetscInt    numPoints[2]        = {2, 1};
3684b5a892a1SMatthew G. Knepley     PetscInt    coneSize[3]         = {2, 0, 0};
3685b5a892a1SMatthew G. Knepley     PetscInt    cones[2]            = {1, 2};
3686b5a892a1SMatthew G. Knepley     PetscInt    coneOrientations[2] = {0, 0};
3687b5a892a1SMatthew G. Knepley     PetscScalar vertexCoords[2]     = {-1.0, 1.0};
3688b5a892a1SMatthew G. Knepley 
36899566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 1));
36909566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
36919371c9d4SSatish Balay   } break;
36929371c9d4SSatish Balay   case DM_POLYTOPE_TRIANGLE: {
36939318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {3, 1};
36949318fe57SMatthew G. Knepley     PetscInt    coneSize[4]         = {3, 0, 0, 0};
36959318fe57SMatthew G. Knepley     PetscInt    cones[3]            = {1, 2, 3};
36969318fe57SMatthew G. Knepley     PetscInt    coneOrientations[3] = {0, 0, 0};
36979318fe57SMatthew G. Knepley     PetscScalar vertexCoords[6]     = {-1.0, -1.0, 1.0, -1.0, -1.0, 1.0};
36989318fe57SMatthew G. Knepley 
36999566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 2));
37009566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37019371c9d4SSatish Balay   } break;
37029371c9d4SSatish Balay   case DM_POLYTOPE_QUADRILATERAL: {
37039318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {4, 1};
37049318fe57SMatthew G. Knepley     PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
37059318fe57SMatthew G. Knepley     PetscInt    cones[4]            = {1, 2, 3, 4};
37069318fe57SMatthew G. Knepley     PetscInt    coneOrientations[4] = {0, 0, 0, 0};
37079318fe57SMatthew G. Knepley     PetscScalar vertexCoords[8]     = {-1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0};
37089318fe57SMatthew G. Knepley 
37099566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 2));
37109566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37119371c9d4SSatish Balay   } break;
37129371c9d4SSatish Balay   case DM_POLYTOPE_SEG_PRISM_TENSOR: {
37139318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {4, 1};
37149318fe57SMatthew G. Knepley     PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
37159318fe57SMatthew G. Knepley     PetscInt    cones[4]            = {1, 2, 3, 4};
37169318fe57SMatthew G. Knepley     PetscInt    coneOrientations[4] = {0, 0, 0, 0};
37179318fe57SMatthew G. Knepley     PetscScalar vertexCoords[8]     = {-1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0};
37189318fe57SMatthew G. Knepley 
37199566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 2));
37209566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37219371c9d4SSatish Balay   } break;
37229371c9d4SSatish Balay   case DM_POLYTOPE_TETRAHEDRON: {
37239318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {4, 1};
37249318fe57SMatthew G. Knepley     PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
3725f0edb160SMatthew G. Knepley     PetscInt    cones[4]            = {1, 2, 3, 4};
37269318fe57SMatthew G. Knepley     PetscInt    coneOrientations[4] = {0, 0, 0, 0};
3727f0edb160SMatthew 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};
37289318fe57SMatthew G. Knepley 
37299566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
37309566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37319371c9d4SSatish Balay   } break;
37329371c9d4SSatish Balay   case DM_POLYTOPE_HEXAHEDRON: {
37339318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {8, 1};
37349318fe57SMatthew G. Knepley     PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
3735f0edb160SMatthew G. Knepley     PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
37369318fe57SMatthew G. Knepley     PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
37379371c9d4SSatish 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};
37389318fe57SMatthew G. Knepley 
37399566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
37409566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37419371c9d4SSatish Balay   } break;
37429371c9d4SSatish Balay   case DM_POLYTOPE_TRI_PRISM: {
37439318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {6, 1};
37449318fe57SMatthew G. Knepley     PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
3745f0edb160SMatthew G. Knepley     PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
37469318fe57SMatthew G. Knepley     PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
37479371c9d4SSatish 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};
37489318fe57SMatthew G. Knepley 
37499566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
37509566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37519371c9d4SSatish Balay   } break;
37529371c9d4SSatish Balay   case DM_POLYTOPE_TRI_PRISM_TENSOR: {
37539318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {6, 1};
37549318fe57SMatthew G. Knepley     PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
37559318fe57SMatthew G. Knepley     PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
37569318fe57SMatthew G. Knepley     PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
37579371c9d4SSatish 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};
37589318fe57SMatthew G. Knepley 
37599566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
37609566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37619371c9d4SSatish Balay   } break;
37629371c9d4SSatish Balay   case DM_POLYTOPE_QUAD_PRISM_TENSOR: {
37639318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {8, 1};
37649318fe57SMatthew G. Knepley     PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
37659318fe57SMatthew G. Knepley     PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
37669318fe57SMatthew G. Knepley     PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
37679371c9d4SSatish 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};
37689318fe57SMatthew G. Knepley 
37699566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
37709566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37719371c9d4SSatish Balay   } break;
37729371c9d4SSatish Balay   case DM_POLYTOPE_PYRAMID: {
37739318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {5, 1};
37749318fe57SMatthew G. Knepley     PetscInt    coneSize[6]         = {5, 0, 0, 0, 0, 0};
3775f0edb160SMatthew G. Knepley     PetscInt    cones[5]            = {1, 2, 3, 4, 5};
37769318fe57SMatthew G. Knepley     PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
37779371c9d4SSatish 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};
37789318fe57SMatthew G. Knepley 
37799566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
37809566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37819371c9d4SSatish Balay   } break;
3782d71ae5a4SJacob Faibussowitsch   default:
3783d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)rdm), PETSC_ERR_ARG_WRONG, "Cannot create reference cell for cell type %s", DMPolytopeTypes[ct]);
37849318fe57SMatthew G. Knepley   }
37859318fe57SMatthew G. Knepley   {
37869318fe57SMatthew G. Knepley     PetscInt Nv, v;
37879318fe57SMatthew G. Knepley 
37889318fe57SMatthew G. Knepley     /* Must create the celltype label here so that we do not automatically try to compute the types */
37899566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(rdm, "celltype"));
37909566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCellType(rdm, 0, ct));
37919566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(rdm, NULL, &Nv));
37929566063dSJacob Faibussowitsch     for (v = 1; v < Nv; ++v) PetscCall(DMPlexSetCellType(rdm, v, DM_POLYTOPE_POINT));
37939318fe57SMatthew G. Knepley   }
37949566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolateInPlace_Internal(rdm));
37959566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)rdm, DMPolytopeTypes[ct]));
37963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
37970a6ba040SMatthew G. Knepley }
37980a6ba040SMatthew G. Knepley 
37999318fe57SMatthew G. Knepley /*@
3800a1cb98faSBarry Smith   DMPlexCreateReferenceCell - Create a `DMPLEX` with the appropriate FEM reference cell
38019318fe57SMatthew G. Knepley 
38029318fe57SMatthew G. Knepley   Collective
38039318fe57SMatthew G. Knepley 
38049318fe57SMatthew G. Knepley   Input Parameters:
38059318fe57SMatthew G. Knepley + comm - The communicator
38069318fe57SMatthew G. Knepley - ct   - The cell type of the reference cell
38079318fe57SMatthew G. Knepley 
38089318fe57SMatthew G. Knepley   Output Parameter:
38099318fe57SMatthew G. Knepley . refdm - The reference cell
38109318fe57SMatthew G. Knepley 
38119318fe57SMatthew G. Knepley   Level: intermediate
38129318fe57SMatthew G. Knepley 
381342747ad1SJacob Faibussowitsch .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBoxMesh()`
38149318fe57SMatthew G. Knepley @*/
3815d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateReferenceCell(MPI_Comm comm, DMPolytopeType ct, DM *refdm)
3816d71ae5a4SJacob Faibussowitsch {
38170a6ba040SMatthew G. Knepley   PetscFunctionBegin;
38189566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, refdm));
38199566063dSJacob Faibussowitsch   PetscCall(DMSetType(*refdm, DMPLEX));
38209566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateReferenceCell_Internal(*refdm, ct));
38213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
38229318fe57SMatthew G. Knepley }
382379a015ccSMatthew G. Knepley 
3824d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoundaryLabel_Private(DM dm, const char name[])
3825d71ae5a4SJacob Faibussowitsch {
38269318fe57SMatthew G. Knepley   DM        plex;
38279318fe57SMatthew G. Knepley   DMLabel   label;
38289318fe57SMatthew G. Knepley   PetscBool hasLabel;
38290a6ba040SMatthew G. Knepley 
3830c22d3578SMatthew G. Knepley   PetscFunctionBegin;
38319566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &hasLabel));
38323ba16761SJacob Faibussowitsch   if (hasLabel) PetscFunctionReturn(PETSC_SUCCESS);
38339566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, name));
38349566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
38359566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
38369566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkBoundaryFaces(plex, 1, label));
38371c8afea9SMatthew G. Knepley   PetscCall(DMPlexLabelComplete(plex, label));
38389566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plex));
38393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
38409318fe57SMatthew G. Knepley }
3841acdc6f61SToby Isaac 
3842669647acSMatthew G. Knepley /*
3843669647acSMatthew 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.
3844669647acSMatthew G. Knepley 
3845669647acSMatthew G. Knepley     (x, y) -> (r, theta) = (x[1], (x[0] - lower[0]) * 2\pi/(upper[0] - lower[0]))
3846669647acSMatthew G. Knepley */
3847d71ae5a4SJacob 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[])
3848d71ae5a4SJacob Faibussowitsch {
3849669647acSMatthew G. Knepley   const PetscReal low = PetscRealPart(constants[0]);
3850669647acSMatthew G. Knepley   const PetscReal upp = PetscRealPart(constants[1]);
3851669647acSMatthew G. Knepley   const PetscReal r   = PetscRealPart(u[1]);
3852669647acSMatthew G. Knepley   const PetscReal th  = 2. * PETSC_PI * (PetscRealPart(u[0]) - low) / (upp - low);
3853669647acSMatthew G. Knepley 
3854669647acSMatthew G. Knepley   f0[0] = r * PetscCosReal(th);
3855669647acSMatthew G. Knepley   f0[1] = r * PetscSinReal(th);
3856669647acSMatthew G. Knepley }
3857669647acSMatthew G. Knepley 
38584e22dd4cSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscOptionsFindPairPrefix_Private(PetscOptions, const char pre[], const char name[], const char *option[], const char *value[], PetscBool *flg);
38594e22dd4cSMatthew G. Knepley 
38605dca41c3SJed Brown const char *const DMPlexShapes[] = {"box", "box_surface", "ball", "sphere", "cylinder", "schwarz_p", "gyroid", "doublet", "annulus", "hypercubic", "zbox", "unknown", "DMPlexShape", "DM_SHAPE_", NULL};
38619318fe57SMatthew G. Knepley 
3862d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateFromOptions_Internal(PetscOptionItems *PetscOptionsObject, PetscBool *useCoordSpace, DM dm)
3863d71ae5a4SJacob Faibussowitsch {
38649318fe57SMatthew G. Knepley   DMPlexShape    shape   = DM_SHAPE_BOX;
38659318fe57SMatthew G. Knepley   DMPolytopeType cell    = DM_POLYTOPE_TRIANGLE;
38669318fe57SMatthew G. Knepley   PetscInt       dim     = 2;
38679318fe57SMatthew G. Knepley   PetscBool      simplex = PETSC_TRUE, interpolate = PETSC_TRUE, adjCone = PETSC_FALSE, adjClosure = PETSC_TRUE, refDomain = PETSC_FALSE;
3868cd7e8a5eSksagiyam   PetscBool      flg, flg2, fflg, bdfflg, nameflg;
38699318fe57SMatthew G. Knepley   MPI_Comm       comm;
3870ed5e4e85SVaclav Hapla   char           filename[PETSC_MAX_PATH_LEN]   = "<unspecified>";
3871ed5e4e85SVaclav Hapla   char           bdFilename[PETSC_MAX_PATH_LEN] = "<unspecified>";
3872ed5e4e85SVaclav Hapla   char           plexname[PETSC_MAX_PATH_LEN]   = "";
38734e22dd4cSMatthew G. Knepley   const char    *option;
38749318fe57SMatthew G. Knepley 
38759318fe57SMatthew G. Knepley   PetscFunctionBegin;
3876708be2fdSJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_CreateFromOptions, dm, 0, 0, 0));
38779566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
38789318fe57SMatthew G. Knepley   /* TODO Turn this into a registration interface */
38799566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_filename", "File containing a mesh", "DMPlexCreateFromFile", filename, filename, sizeof(filename), &fflg));
38809566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_boundary_filename", "File containing a mesh boundary", "DMPlexCreateFromFile", bdFilename, bdFilename, sizeof(bdFilename), &bdfflg));
38819566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_name", "Name of the mesh in the file", "DMPlexCreateFromFile", plexname, plexname, sizeof(plexname), &nameflg));
38829566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_plex_cell", "Cell shape", "", DMPolytopeTypes, (PetscEnum)cell, (PetscEnum *)&cell, NULL));
38839566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_reference_cell_domain", "Use a reference cell domain", "", refDomain, &refDomain, NULL));
38849566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_plex_shape", "Shape for built-in mesh", "", DMPlexShapes, (PetscEnum)shape, (PetscEnum *)&shape, &flg));
38859566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_dim", "Topological dimension of the mesh", "DMGetDimension", dim, &dim, &flg, 0));
3886cfb853baSMatthew G. Knepley   PetscCheck(dim >= 0, comm, PETSC_ERR_ARG_OUTOFRANGE, "Dimension %" PetscInt_FMT " should be in [0, infinity)", dim);
38879566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_simplex", "Mesh cell shape", "", simplex, &simplex, &flg));
38889566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_interpolate", "Flag to create edges and faces automatically", "", interpolate, &interpolate, &flg));
38899566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_adj_cone", "Set adjacency direction", "DMSetBasicAdjacency", adjCone, &adjCone, &flg));
38909566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_adj_closure", "Set adjacency size", "DMSetBasicAdjacency", adjClosure, &adjClosure, &flg2));
38919566063dSJacob Faibussowitsch   if (flg || flg2) PetscCall(DMSetBasicAdjacency(dm, adjCone, adjClosure));
38929318fe57SMatthew G. Knepley 
389361a622f3SMatthew G. Knepley   switch (cell) {
389461a622f3SMatthew G. Knepley   case DM_POLYTOPE_POINT:
389561a622f3SMatthew G. Knepley   case DM_POLYTOPE_SEGMENT:
389661a622f3SMatthew G. Knepley   case DM_POLYTOPE_POINT_PRISM_TENSOR:
389761a622f3SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
389861a622f3SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
389961a622f3SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
3900d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_HEXAHEDRON:
3901d71ae5a4SJacob Faibussowitsch     *useCoordSpace = PETSC_TRUE;
3902d71ae5a4SJacob Faibussowitsch     break;
3903d71ae5a4SJacob Faibussowitsch   default:
3904d71ae5a4SJacob Faibussowitsch     *useCoordSpace = PETSC_FALSE;
3905d71ae5a4SJacob Faibussowitsch     break;
390661a622f3SMatthew G. Knepley   }
390761a622f3SMatthew G. Knepley 
39089318fe57SMatthew G. Knepley   if (fflg) {
39099318fe57SMatthew G. Knepley     DM dmnew;
39109318fe57SMatthew G. Knepley 
39119566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), filename, plexname, interpolate, &dmnew));
39125de52c6dSVaclav Hapla     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
391369d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &dmnew));
39149318fe57SMatthew G. Knepley   } else if (refDomain) {
39159566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateReferenceCell_Internal(dm, cell));
39169318fe57SMatthew G. Knepley   } else if (bdfflg) {
39179318fe57SMatthew G. Knepley     DM bdm, dmnew;
39189318fe57SMatthew G. Knepley 
39199566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), bdFilename, plexname, interpolate, &bdm));
39209566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)bdm, "bd_"));
39219566063dSJacob Faibussowitsch     PetscCall(DMSetFromOptions(bdm));
39229566063dSJacob Faibussowitsch     PetscCall(DMPlexGenerate(bdm, NULL, interpolate, &dmnew));
39239566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&bdm));
39245de52c6dSVaclav Hapla     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
392569d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &dmnew));
39269318fe57SMatthew G. Knepley   } else {
39279566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)dm, DMPlexShapes[shape]));
39289318fe57SMatthew G. Knepley     switch (shape) {
3929669647acSMatthew G. Knepley     case DM_SHAPE_BOX:
39305dca41c3SJed Brown     case DM_SHAPE_ZBOX:
3931669647acSMatthew G. Knepley     case DM_SHAPE_ANNULUS: {
39329318fe57SMatthew G. Knepley       PetscInt       faces[3]  = {0, 0, 0};
39339318fe57SMatthew G. Knepley       PetscReal      lower[3]  = {0, 0, 0};
39349318fe57SMatthew G. Knepley       PetscReal      upper[3]  = {1, 1, 1};
39359318fe57SMatthew G. Knepley       DMBoundaryType bdt[3]    = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
3936669647acSMatthew G. Knepley       PetscBool      isAnnular = shape == DM_SHAPE_ANNULUS ? PETSC_TRUE : PETSC_FALSE;
39379318fe57SMatthew G. Knepley       PetscInt       i, n;
39389318fe57SMatthew G. Knepley 
39399318fe57SMatthew G. Knepley       n = dim;
39409318fe57SMatthew G. Knepley       for (i = 0; i < dim; ++i) faces[i] = (dim == 1 ? 1 : 4 - dim);
39419566063dSJacob Faibussowitsch       PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg));
39429318fe57SMatthew G. Knepley       n = 3;
39439566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
394463a3b9bcSJacob Faibussowitsch       PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Lower box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
39459318fe57SMatthew G. Knepley       n = 3;
39469566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
394763a3b9bcSJacob Faibussowitsch       PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Upper box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
39489318fe57SMatthew G. Knepley       n = 3;
39499566063dSJacob Faibussowitsch       PetscCall(PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *)bdt, &n, &flg));
395063a3b9bcSJacob Faibussowitsch       PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Box boundary types had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
3951669647acSMatthew G. Knepley 
3952669647acSMatthew G. Knepley       PetscCheck(!isAnnular || dim == 2, comm, PETSC_ERR_ARG_OUTOFRANGE, "Only two dimensional annuli have been implemented");
3953669647acSMatthew G. Knepley       if (isAnnular)
3954669647acSMatthew G. Knepley         for (i = 0; i < dim - 1; ++i) bdt[i] = DM_BOUNDARY_PERIODIC;
3955669647acSMatthew G. Knepley 
39569318fe57SMatthew G. Knepley       switch (cell) {
395761a622f3SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
39589566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateWedgeBoxMesh_Internal(dm, faces, lower, upper, bdt));
3959d410b0cfSMatthew G. Knepley         if (!interpolate) {
3960d410b0cfSMatthew G. Knepley           DM udm;
3961d410b0cfSMatthew G. Knepley 
39629566063dSJacob Faibussowitsch           PetscCall(DMPlexUninterpolate(dm, &udm));
396369d8a87bSksagiyam           PetscCall(DMPlexReplace_Internal(dm, &udm));
3964d410b0cfSMatthew G. Knepley         }
39659318fe57SMatthew G. Knepley         break;
3966d71ae5a4SJacob Faibussowitsch       default:
39675dca41c3SJed Brown         PetscCall(DMPlexCreateBoxMesh_Internal(dm, shape, dim, simplex, faces, lower, upper, bdt, interpolate));
3968d71ae5a4SJacob Faibussowitsch         break;
39699318fe57SMatthew G. Knepley       }
3970669647acSMatthew G. Knepley       if (isAnnular) {
3971669647acSMatthew G. Knepley         DM          cdm;
3972669647acSMatthew G. Knepley         PetscDS     cds;
3973669647acSMatthew G. Knepley         PetscScalar bounds[2] = {lower[0], upper[0]};
3974669647acSMatthew G. Knepley 
3975669647acSMatthew G. Knepley         // Fix coordinates for annular region
3976669647acSMatthew G. Knepley         PetscCall(DMSetPeriodicity(dm, NULL, NULL, NULL));
3977669647acSMatthew G. Knepley         PetscCall(DMSetCellCoordinatesLocal(dm, NULL));
3978669647acSMatthew G. Knepley         PetscCall(DMSetCellCoordinates(dm, NULL));
3979e44f6aebSMatthew G. Knepley         PetscCall(DMPlexCreateCoordinateSpace(dm, 1, PETSC_TRUE, NULL));
3980669647acSMatthew G. Knepley         PetscCall(DMGetCoordinateDM(dm, &cdm));
3981669647acSMatthew G. Knepley         PetscCall(DMGetDS(cdm, &cds));
3982669647acSMatthew G. Knepley         PetscCall(PetscDSSetConstants(cds, 2, bounds));
3983669647acSMatthew G. Knepley         PetscCall(DMPlexRemapGeometry(dm, 0.0, boxToAnnulus));
3984669647acSMatthew G. Knepley       }
39859371c9d4SSatish Balay     } break;
39869371c9d4SSatish Balay     case DM_SHAPE_BOX_SURFACE: {
39879318fe57SMatthew G. Knepley       PetscInt  faces[3] = {0, 0, 0};
39889318fe57SMatthew G. Knepley       PetscReal lower[3] = {0, 0, 0};
39899318fe57SMatthew G. Knepley       PetscReal upper[3] = {1, 1, 1};
39909318fe57SMatthew G. Knepley       PetscInt  i, n;
39919318fe57SMatthew G. Knepley 
39929318fe57SMatthew G. Knepley       n = dim + 1;
39939318fe57SMatthew G. Knepley       for (i = 0; i < dim + 1; ++i) faces[i] = (dim + 1 == 1 ? 1 : 4 - (dim + 1));
39949566063dSJacob Faibussowitsch       PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg));
39959318fe57SMatthew G. Knepley       n = 3;
39969566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
399763a3b9bcSJacob 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);
39989318fe57SMatthew G. Knepley       n = 3;
39999566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
400063a3b9bcSJacob 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);
40019566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(dm, dim + 1, faces, lower, upper, interpolate));
40029371c9d4SSatish Balay     } break;
40039371c9d4SSatish Balay     case DM_SHAPE_SPHERE: {
40049318fe57SMatthew G. Knepley       PetscReal R = 1.0;
40059318fe57SMatthew G. Knepley 
40069566063dSJacob Faibussowitsch       PetscCall(PetscOptionsReal("-dm_plex_sphere_radius", "Radius of the sphere", "", R, &R, &flg));
40079566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateSphereMesh_Internal(dm, dim, simplex, R));
40089371c9d4SSatish Balay     } break;
40099371c9d4SSatish Balay     case DM_SHAPE_BALL: {
40109318fe57SMatthew G. Knepley       PetscReal R = 1.0;
40119318fe57SMatthew G. Knepley 
40129566063dSJacob Faibussowitsch       PetscCall(PetscOptionsReal("-dm_plex_ball_radius", "Radius of the ball", "", R, &R, &flg));
40139566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateBallMesh_Internal(dm, dim, R));
40149371c9d4SSatish Balay     } break;
40159371c9d4SSatish Balay     case DM_SHAPE_CYLINDER: {
40169318fe57SMatthew G. Knepley       DMBoundaryType bdt = DM_BOUNDARY_NONE;
40179318fe57SMatthew G. Knepley       PetscInt       Nw  = 6;
40189318fe57SMatthew G. Knepley 
40199566063dSJacob Faibussowitsch       PetscCall(PetscOptionsEnum("-dm_plex_cylinder_bd", "Boundary type in the z direction", "", DMBoundaryTypes, (PetscEnum)bdt, (PetscEnum *)&bdt, NULL));
40209566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-dm_plex_cylinder_num_wedges", "Number of wedges around the cylinder", "", Nw, &Nw, NULL));
40219318fe57SMatthew G. Knepley       switch (cell) {
4022d71ae5a4SJacob Faibussowitsch       case DM_POLYTOPE_TRI_PRISM_TENSOR:
4023d71ae5a4SJacob Faibussowitsch         PetscCall(DMPlexCreateWedgeCylinderMesh_Internal(dm, Nw, interpolate));
4024d71ae5a4SJacob Faibussowitsch         break;
4025d71ae5a4SJacob Faibussowitsch       default:
4026d71ae5a4SJacob Faibussowitsch         PetscCall(DMPlexCreateHexCylinderMesh_Internal(dm, bdt));
4027d71ae5a4SJacob Faibussowitsch         break;
40289318fe57SMatthew G. Knepley       }
40299371c9d4SSatish Balay     } break;
4030b7f5c055SJed Brown     case DM_SHAPE_SCHWARZ_P: // fallthrough
40319371c9d4SSatish Balay     case DM_SHAPE_GYROID: {
4032b7f5c055SJed Brown       PetscInt       extent[3] = {1, 1, 1}, refine = 0, layers = 0, three;
4033b7f5c055SJed Brown       PetscReal      thickness   = 0.;
4034b7f5c055SJed Brown       DMBoundaryType periodic[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
4035b7f5c055SJed Brown       DMPlexTPSType  tps_type    = shape == DM_SHAPE_SCHWARZ_P ? DMPLEX_TPS_SCHWARZ_P : DMPLEX_TPS_GYROID;
40361436d7faSJed Brown       PetscBool      tps_distribute;
40379566063dSJacob Faibussowitsch       PetscCall(PetscOptionsIntArray("-dm_plex_tps_extent", "Number of replicas for each of three dimensions", NULL, extent, (three = 3, &three), NULL));
40389566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-dm_plex_tps_refine", "Number of refinements", NULL, refine, &refine, NULL));
40399566063dSJacob Faibussowitsch       PetscCall(PetscOptionsEnumArray("-dm_plex_tps_periodic", "Periodicity in each of three dimensions", NULL, DMBoundaryTypes, (PetscEnum *)periodic, (three = 3, &three), NULL));
40409566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-dm_plex_tps_layers", "Number of layers in volumetric extrusion (or zero to not extrude)", NULL, layers, &layers, NULL));
40419566063dSJacob Faibussowitsch       PetscCall(PetscOptionsReal("-dm_plex_tps_thickness", "Thickness of volumetric extrusion", NULL, thickness, &thickness, NULL));
40429566063dSJacob Faibussowitsch       PetscCall(DMPlexDistributeGetDefault(dm, &tps_distribute));
40439566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_tps_distribute", "Distribute the 2D mesh prior to refinement and extrusion", NULL, tps_distribute, &tps_distribute, NULL));
40449566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateTPSMesh_Internal(dm, tps_type, extent, periodic, tps_distribute, refine, layers, thickness));
40459371c9d4SSatish Balay     } break;
40469371c9d4SSatish Balay     case DM_SHAPE_DOUBLET: {
404705bd46c0SStefano Zampini       DM        dmnew;
404805bd46c0SStefano Zampini       PetscReal rl = 0.0;
404905bd46c0SStefano Zampini 
405005bd46c0SStefano Zampini       PetscCall(PetscOptionsReal("-dm_plex_doublet_refinementlimit", "Refinement limit", NULL, rl, &rl, NULL));
405105bd46c0SStefano Zampini       PetscCall(DMPlexCreateDoublet(PetscObjectComm((PetscObject)dm), dim, simplex, interpolate, rl, &dmnew));
40525de52c6dSVaclav Hapla       PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
405369d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &dmnew));
40549371c9d4SSatish Balay     } break;
4055cfb853baSMatthew G. Knepley     case DM_SHAPE_HYPERCUBIC: {
4056cfb853baSMatthew G. Knepley       PetscInt       *edges;
4057cfb853baSMatthew G. Knepley       PetscReal      *lower, *upper;
4058cfb853baSMatthew G. Knepley       DMBoundaryType *bdt;
4059cfb853baSMatthew G. Knepley       PetscInt        n, d;
4060cfb853baSMatthew G. Knepley 
4061cfb853baSMatthew G. Knepley       *useCoordSpace = PETSC_FALSE;
4062cfb853baSMatthew G. Knepley       PetscCall(PetscMalloc4(dim, &edges, dim, &lower, dim, &upper, dim, &bdt));
4063cfb853baSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
4064cfb853baSMatthew G. Knepley         edges[d] = 1;
4065cfb853baSMatthew G. Knepley         lower[d] = 0.;
4066cfb853baSMatthew G. Knepley         upper[d] = 1.;
4067cfb853baSMatthew G. Knepley         bdt[d]   = DM_BOUNDARY_PERIODIC;
4068cfb853baSMatthew G. Knepley       }
4069cfb853baSMatthew G. Knepley       n = dim;
4070cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", edges, &n, &flg));
4071cfb853baSMatthew G. Knepley       n = dim;
4072cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
4073cfb853baSMatthew 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);
4074cfb853baSMatthew G. Knepley       n = dim;
4075cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
4076cfb853baSMatthew 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);
4077cfb853baSMatthew G. Knepley       n = dim;
4078cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *)bdt, &n, &flg));
4079cfb853baSMatthew 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);
4080cfb853baSMatthew G. Knepley       PetscCall(DMPlexCreateHypercubicMesh_Internal(dm, dim, lower, upper, edges, bdt));
4081cfb853baSMatthew G. Knepley       PetscCall(PetscFree4(edges, lower, upper, bdt));
4082cfb853baSMatthew G. Knepley     } break;
4083d71ae5a4SJacob Faibussowitsch     default:
4084d71ae5a4SJacob Faibussowitsch       SETERRQ(comm, PETSC_ERR_SUP, "Domain shape %s is unsupported", DMPlexShapes[shape]);
40859318fe57SMatthew G. Knepley     }
40869318fe57SMatthew G. Knepley   }
40879566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
408848a46eb9SPierre Jolivet   if (!((PetscObject)dm)->name && nameflg) PetscCall(PetscObjectSetName((PetscObject)dm, plexname));
40894e22dd4cSMatthew G. Knepley   // Allow label creation
40904e22dd4cSMatthew G. Knepley   PetscCall(PetscOptionsFindPairPrefix_Private(NULL, ((PetscObject)dm)->prefix, "-dm_plex_label_", &option, NULL, &flg));
40914e22dd4cSMatthew G. Knepley   if (flg) {
40924e22dd4cSMatthew G. Knepley     DMLabel     label;
40934e22dd4cSMatthew G. Knepley     PetscInt    points[1024], n = 1024;
40944e22dd4cSMatthew G. Knepley     char        fulloption[PETSC_MAX_PATH_LEN];
40954e22dd4cSMatthew G. Knepley     const char *name = &option[14];
40964e22dd4cSMatthew G. Knepley 
40974e22dd4cSMatthew G. Knepley     PetscCall(DMCreateLabel(dm, name));
40984e22dd4cSMatthew G. Knepley     PetscCall(DMGetLabel(dm, name, &label));
40994e22dd4cSMatthew G. Knepley     fulloption[0] = '-';
41004e22dd4cSMatthew G. Knepley     fulloption[1] = 0;
41014e22dd4cSMatthew G. Knepley     PetscCall(PetscStrlcat(fulloption, option, PETSC_MAX_PATH_LEN));
41024e22dd4cSMatthew G. Knepley     PetscCall(PetscOptionsGetIntArray(NULL, ((PetscObject)dm)->prefix, fulloption, points, &n, NULL));
41034e22dd4cSMatthew G. Knepley     for (PetscInt p = 0; p < n; ++p) PetscCall(DMLabelSetValue(label, points[p], 1));
41044e22dd4cSMatthew G. Knepley   }
4105708be2fdSJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_CreateFromOptions, dm, 0, 0, 0));
41063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
41070a6ba040SMatthew G. Knepley }
41080a6ba040SMatthew G. Knepley 
4109d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions_NonRefinement_Plex(DM dm, PetscOptionItems *PetscOptionsObject)
4110d71ae5a4SJacob Faibussowitsch {
41110a6ba040SMatthew G. Knepley   DM_Plex  *mesh = (DM_Plex *)dm->data;
41127f9d8d6cSVaclav Hapla   PetscBool flg, flg2;
41139318fe57SMatthew G. Knepley   char      bdLabel[PETSC_MAX_PATH_LEN];
41140a6ba040SMatthew G. Knepley 
41150a6ba040SMatthew G. Knepley   PetscFunctionBegin;
41160a6ba040SMatthew G. Knepley   /* Handle viewing */
41179566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_print_set_values", "Output all set values info", "DMPlexMatSetClosure", PETSC_FALSE, &mesh->printSetValues, NULL));
41185962854dSMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_fem", "Debug output level for all fem computations", "DMPlexSNESComputeResidualFEM", 0, &mesh->printFEM, NULL, 0));
41195962854dSMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_fvm", "Debug output level for all fvm computations", "DMPlexSNESComputeResidualFVM", 0, &mesh->printFVM, NULL, 0));
41209566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-dm_plex_print_tol", "Tolerance for FEM output", "DMPlexSNESComputeResidualFEM", mesh->printTol, &mesh->printTol, NULL));
41219566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_l2", "Debug output level all L2 diff computations", "DMComputeL2Diff", 0, &mesh->printL2, NULL, 0));
4122f5867de0SMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_locate", "Debug output level all point location computations", "DMLocatePoints", 0, &mesh->printLocate, NULL, 0));
41239566063dSJacob Faibussowitsch   PetscCall(DMMonitorSetFromOptions(dm, "-dm_plex_monitor_throughput", "Monitor the simulation throughput", "DMPlexMonitorThroughput", DMPlexMonitorThroughput, NULL, &flg));
41249566063dSJacob Faibussowitsch   if (flg) PetscCall(PetscLogDefaultBegin());
41259318fe57SMatthew G. Knepley   /* Labeling */
41269566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_boundary_label", "Label to mark the mesh boundary", "", bdLabel, bdLabel, sizeof(bdLabel), &flg));
41279566063dSJacob Faibussowitsch   if (flg) PetscCall(DMPlexCreateBoundaryLabel_Private(dm, bdLabel));
4128953fc75cSMatthew G. Knepley   /* Point Location */
41299566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_hash_location", "Use grid hashing for point location", "DMInterpolate", PETSC_FALSE, &mesh->useHashLocation, NULL));
41300848f4b5SMatthew G. Knepley   /* Partitioning and distribution */
41319566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_partition_balance", "Attempt to evenly divide points on partition boundary between processes", "DMPlexSetPartitionBalance", PETSC_FALSE, &mesh->partitionBalance, NULL));
4132d02c7345SMatthew G. Knepley   /* Reordering */
4133d02c7345SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_plex_reorder_section", "Compute point permutation for local section", "DMPlexReorderSectionSetDefault", PETSC_FALSE, &flg2, &flg));
4134d02c7345SMatthew G. Knepley   if (flg) PetscCall(DMPlexReorderSectionSetDefault(dm, flg2 ? DMPLEX_REORDER_DEFAULT_TRUE : DMPLEX_REORDER_DEFAULT_FALSE));
41352e62ab5aSMatthew G. Knepley   /* Generation and remeshing */
41369566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_remesh_bd", "Allow changes to the boundary on remeshing", "DMAdapt", PETSC_FALSE, &mesh->remeshBd, NULL));
4137b29cfa1cSToby Isaac   /* Projection behavior */
4138d5b43468SJose E. Roman   PetscCall(PetscOptionsBoundedInt("-dm_plex_max_projection_height", "Maximum mesh point height used to project locally", "DMPlexSetMaxProjectionHeight", 0, &mesh->maxProjectionHeight, NULL, 0));
41399566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_regular_refinement", "Use special nested projection algorithm for regular refinement", "DMPlexSetRegularRefinement", mesh->regularRefinement, &mesh->regularRefinement, NULL));
4140f12cf164SMatthew G. Knepley   /* Checking structure */
4141f12cf164SMatthew G. Knepley   {
41427f9d8d6cSVaclav Hapla     PetscBool all = PETSC_FALSE;
4143f12cf164SMatthew G. Knepley 
41447f9d8d6cSVaclav Hapla     PetscCall(PetscOptionsBool("-dm_plex_check_all", "Perform all basic checks", "DMPlexCheck", PETSC_FALSE, &all, NULL));
41457f9d8d6cSVaclav Hapla     if (all) {
41467f9d8d6cSVaclav Hapla       PetscCall(DMPlexCheck(dm));
41477f9d8d6cSVaclav Hapla     } else {
41489566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_symmetry", "Check that the adjacency information in the mesh is symmetric", "DMPlexCheckSymmetry", PETSC_FALSE, &flg, &flg2));
41497f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckSymmetry(dm));
41509566063dSJacob 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));
41517f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckSkeleton(dm, 0));
41529566063dSJacob 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));
41537f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckFaces(dm, 0));
41549566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_geometry", "Check that cells have positive volume", "DMPlexCheckGeometry", PETSC_FALSE, &flg, &flg2));
41557f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckGeometry(dm));
41569566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_pointsf", "Check some necessary conditions for PointSF", "DMPlexCheckPointSF", PETSC_FALSE, &flg, &flg2));
4157d7d32a9aSMatthew G. Knepley       if (flg && flg2) PetscCall(DMPlexCheckPointSF(dm, NULL, PETSC_FALSE));
41589566063dSJacob 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));
41597f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckInterfaceCones(dm));
41607f9d8d6cSVaclav Hapla     }
41619566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-dm_plex_check_cell_shape", "Check cell shape", "DMPlexCheckCellShape", PETSC_FALSE, &flg, &flg2));
41629566063dSJacob Faibussowitsch     if (flg && flg2) PetscCall(DMPlexCheckCellShape(dm, PETSC_TRUE, PETSC_DETERMINE));
4163f12cf164SMatthew G. Knepley   }
41649318fe57SMatthew G. Knepley   {
41659318fe57SMatthew G. Knepley     PetscReal scale = 1.0;
41664f3833eaSMatthew G. Knepley 
41679566063dSJacob Faibussowitsch     PetscCall(PetscOptionsReal("-dm_plex_scale", "Scale factor for mesh coordinates", "DMPlexScale", scale, &scale, &flg));
41689318fe57SMatthew G. Knepley     if (flg) {
41699318fe57SMatthew G. Knepley       Vec coordinates, coordinatesLocal;
41709318fe57SMatthew G. Knepley 
41719566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinates(dm, &coordinates));
41729566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinatesLocal(dm, &coordinatesLocal));
41739566063dSJacob Faibussowitsch       PetscCall(VecScale(coordinates, scale));
41749566063dSJacob Faibussowitsch       PetscCall(VecScale(coordinatesLocal, scale));
41759318fe57SMatthew G. Knepley     }
41769318fe57SMatthew G. Knepley   }
41779566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerSetFromOptions(mesh->partitioner));
41783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
417968d4fef7SMatthew G. Knepley }
418068d4fef7SMatthew G. Knepley 
4181d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions_Overlap_Plex(DM dm, PetscOptionItems *PetscOptionsObject, PetscInt *overlap)
4182d71ae5a4SJacob Faibussowitsch {
4183c506a872SMatthew G. Knepley   PetscInt  numOvLabels = 16, numOvExLabels = 16;
4184c506a872SMatthew G. Knepley   char     *ovLabelNames[16], *ovExLabelNames[16];
4185c506a872SMatthew G. Knepley   PetscInt  numOvValues = 16, numOvExValues = 16, l;
4186c506a872SMatthew G. Knepley   PetscBool flg;
4187c506a872SMatthew G. Knepley 
4188c506a872SMatthew G. Knepley   PetscFunctionBegin;
4189c506a872SMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_distribute_overlap", "The size of the overlap halo", "DMPlexDistribute", *overlap, overlap, NULL, 0));
4190c506a872SMatthew G. Knepley   PetscCall(PetscOptionsStringArray("-dm_distribute_overlap_labels", "List of overlap label names", "DMPlexDistribute", ovLabelNames, &numOvLabels, &flg));
4191c506a872SMatthew G. Knepley   if (!flg) numOvLabels = 0;
4192c506a872SMatthew G. Knepley   if (numOvLabels) {
4193c506a872SMatthew G. Knepley     ((DM_Plex *)dm->data)->numOvLabels = numOvLabels;
4194c506a872SMatthew G. Knepley     for (l = 0; l < numOvLabels; ++l) {
4195c506a872SMatthew G. Knepley       PetscCall(DMGetLabel(dm, ovLabelNames[l], &((DM_Plex *)dm->data)->ovLabels[l]));
4196c506a872SMatthew G. Knepley       PetscCheck(((DM_Plex *)dm->data)->ovLabels[l], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid label name %s", ovLabelNames[l]);
4197c506a872SMatthew G. Knepley       PetscCall(PetscFree(ovLabelNames[l]));
4198c506a872SMatthew G. Knepley     }
4199c506a872SMatthew G. Knepley     PetscCall(PetscOptionsIntArray("-dm_distribute_overlap_values", "List of overlap label values", "DMPlexDistribute", ((DM_Plex *)dm->data)->ovValues, &numOvValues, &flg));
4200c506a872SMatthew G. Knepley     if (!flg) numOvValues = 0;
4201c506a872SMatthew 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);
4202c506a872SMatthew G. Knepley 
4203c506a872SMatthew G. Knepley     PetscCall(PetscOptionsStringArray("-dm_distribute_overlap_exclude_labels", "List of overlap exclude label names", "DMPlexDistribute", ovExLabelNames, &numOvExLabels, &flg));
4204c506a872SMatthew G. Knepley     if (!flg) numOvExLabels = 0;
4205c506a872SMatthew G. Knepley     ((DM_Plex *)dm->data)->numOvExLabels = numOvExLabels;
4206c506a872SMatthew G. Knepley     for (l = 0; l < numOvExLabels; ++l) {
4207c506a872SMatthew G. Knepley       PetscCall(DMGetLabel(dm, ovExLabelNames[l], &((DM_Plex *)dm->data)->ovExLabels[l]));
4208c506a872SMatthew G. Knepley       PetscCheck(((DM_Plex *)dm->data)->ovExLabels[l], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid label name %s", ovExLabelNames[l]);
4209c506a872SMatthew G. Knepley       PetscCall(PetscFree(ovExLabelNames[l]));
4210c506a872SMatthew G. Knepley     }
4211c506a872SMatthew G. Knepley     PetscCall(PetscOptionsIntArray("-dm_distribute_overlap_exclude_values", "List of overlap exclude label values", "DMPlexDistribute", ((DM_Plex *)dm->data)->ovExValues, &numOvExValues, &flg));
4212c506a872SMatthew G. Knepley     if (!flg) numOvExValues = 0;
4213c506a872SMatthew 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);
4214c506a872SMatthew G. Knepley   }
42153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4216c506a872SMatthew G. Knepley }
4217c506a872SMatthew G. Knepley 
4218d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetFromOptions_Plex(DM dm, PetscOptionItems *PetscOptionsObject)
4219d71ae5a4SJacob Faibussowitsch {
4220bdf63967SMatthew G. Knepley   PetscFunctionList        ordlist;
4221bdf63967SMatthew G. Knepley   char                     oname[256];
42224e22dd4cSMatthew G. Knepley   char                     sublabelname[PETSC_MAX_PATH_LEN] = "";
4223a286e215SMatthew G. Knepley   DMPlexReorderDefaultFlag reorder;
4224d410b0cfSMatthew G. Knepley   PetscReal                volume    = -1.0;
42259318fe57SMatthew G. Knepley   PetscInt                 prerefine = 0, refine = 0, r, coarsen = 0, overlap = 0, extLayers = 0, dim;
4226a286e215SMatthew 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;
422768d4fef7SMatthew G. Knepley 
422868d4fef7SMatthew G. Knepley   PetscFunctionBegin;
4229d0609cedSBarry Smith   PetscOptionsHeadBegin(PetscOptionsObject, "DMPlex Options");
4230dd4c3f67SMatthew G. Knepley   if (dm->cloneOpts) goto non_refine;
42319318fe57SMatthew G. Knepley   /* Handle automatic creation */
42329566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
42336bc1bd01Sksagiyam   if (dim < 0) {
42346bc1bd01Sksagiyam     PetscCall(DMPlexCreateFromOptions_Internal(PetscOptionsObject, &coordSpace, dm));
42356bc1bd01Sksagiyam     created = PETSC_TRUE;
42366bc1bd01Sksagiyam   }
42376bc1bd01Sksagiyam   PetscCall(DMGetDimension(dm, &dim));
4238d89e6e46SMatthew G. Knepley   /* Handle interpolation before distribution */
42399566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_interpolate_pre", "Flag to interpolate mesh before distribution", "", interpolate, &interpolate, &flg));
4240d89e6e46SMatthew G. Knepley   if (flg) {
4241d89e6e46SMatthew G. Knepley     DMPlexInterpolatedFlag interpolated;
4242d89e6e46SMatthew G. Knepley 
42439566063dSJacob Faibussowitsch     PetscCall(DMPlexIsInterpolated(dm, &interpolated));
4244d89e6e46SMatthew G. Knepley     if (interpolated == DMPLEX_INTERPOLATED_FULL && !interpolate) {
4245d89e6e46SMatthew G. Knepley       DM udm;
4246d89e6e46SMatthew G. Knepley 
42479566063dSJacob Faibussowitsch       PetscCall(DMPlexUninterpolate(dm, &udm));
424869d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &udm));
4249d89e6e46SMatthew G. Knepley     } else if (interpolated != DMPLEX_INTERPOLATED_FULL && interpolate) {
4250d89e6e46SMatthew G. Knepley       DM idm;
4251d89e6e46SMatthew G. Knepley 
42529566063dSJacob Faibussowitsch       PetscCall(DMPlexInterpolate(dm, &idm));
425369d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &idm));
4254d89e6e46SMatthew G. Knepley     }
4255d89e6e46SMatthew G. Knepley   }
42564e22dd4cSMatthew G. Knepley   // Handle submesh selection before distribution
42574e22dd4cSMatthew G. Knepley   PetscCall(PetscOptionsString("-dm_plex_submesh", "Label to use for submesh selection", "", sublabelname, sublabelname, PETSC_MAX_PATH_LEN, &flg));
42584e22dd4cSMatthew G. Knepley   if (flg) {
42594e22dd4cSMatthew G. Knepley     DM              subdm;
42604e22dd4cSMatthew G. Knepley     DMLabel         label;
42614e22dd4cSMatthew G. Knepley     IS              valueIS, pointIS;
42624e22dd4cSMatthew G. Knepley     const PetscInt *values, *points;
42634e22dd4cSMatthew G. Knepley     PetscBool       markedFaces = PETSC_FALSE;
42644e22dd4cSMatthew G. Knepley     PetscInt        Nv, value, Np;
42654e22dd4cSMatthew G. Knepley 
42664e22dd4cSMatthew G. Knepley     PetscCall(DMGetLabel(dm, sublabelname, &label));
42674e22dd4cSMatthew G. Knepley     PetscCall(DMLabelGetNumValues(label, &Nv));
42684e22dd4cSMatthew G. Knepley     PetscCheck(Nv == 1, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Only a single label value is currently supported for submesh selection, not %" PetscInt_FMT, Nv);
42694e22dd4cSMatthew G. Knepley     PetscCall(DMLabelGetValueIS(label, &valueIS));
42704e22dd4cSMatthew G. Knepley     PetscCall(ISGetIndices(valueIS, &values));
42714e22dd4cSMatthew G. Knepley     value = values[0];
42724e22dd4cSMatthew G. Knepley     PetscCall(ISRestoreIndices(valueIS, &values));
42734e22dd4cSMatthew G. Knepley     PetscCall(ISDestroy(&valueIS));
42744e22dd4cSMatthew G. Knepley     PetscCall(DMLabelGetStratumSize(label, value, &Np));
42754e22dd4cSMatthew G. Knepley     PetscCall(DMLabelGetStratumIS(label, value, &pointIS));
42764e22dd4cSMatthew G. Knepley     PetscCall(ISGetIndices(pointIS, &points));
42774e22dd4cSMatthew G. Knepley     for (PetscInt p = 0; p < Np; ++p) {
42784e22dd4cSMatthew G. Knepley       PetscInt pdepth;
42794e22dd4cSMatthew G. Knepley 
42804e22dd4cSMatthew G. Knepley       PetscCall(DMPlexGetPointDepth(dm, points[p], &pdepth));
42814e22dd4cSMatthew G. Knepley       if (pdepth) {
42824e22dd4cSMatthew G. Knepley         markedFaces = PETSC_TRUE;
42834e22dd4cSMatthew G. Knepley         break;
42844e22dd4cSMatthew G. Knepley       }
42854e22dd4cSMatthew G. Knepley     }
42864e22dd4cSMatthew G. Knepley     PetscCall(ISRestoreIndices(pointIS, &points));
42874e22dd4cSMatthew G. Knepley     PetscCall(ISDestroy(&pointIS));
42884e22dd4cSMatthew G. Knepley     PetscCall(DMPlexCreateSubmesh(dm, label, value, markedFaces, &subdm));
42894e22dd4cSMatthew G. Knepley     PetscCall(DMPlexReplace_Internal(dm, &subdm));
42904e22dd4cSMatthew G. Knepley     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
42914e22dd4cSMatthew G. Knepley   }
42929b44eab4SMatthew G. Knepley   /* Handle DMPlex refinement before distribution */
42939566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_ignore_model", "Flag to ignore the geometry model when refining", "DMCreate", ignoreModel, &ignoreModel, &flg));
4294ad540459SPierre Jolivet   if (flg) ((DM_Plex *)dm->data)->ignoreModel = ignoreModel;
42959566063dSJacob Faibussowitsch   PetscCall(DMPlexGetRefinementUniform(dm, &uniformOrig));
42969566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine_pre", "The number of refinements before distribution", "DMCreate", prerefine, &prerefine, NULL, 0));
42979566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_remap_pre", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL));
42989566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_uniform_pre", "Flag for uniform refinement before distribution", "DMCreate", uniform, &uniform, &flg));
42999566063dSJacob Faibussowitsch   if (flg) PetscCall(DMPlexSetRefinementUniform(dm, uniform));
43009566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-dm_refine_volume_limit_pre", "The maximum cell volume after refinement before distribution", "DMCreate", volume, &volume, &flg));
43019318fe57SMatthew G. Knepley   if (flg) {
43029566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementUniform(dm, PETSC_FALSE));
43039566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementLimit(dm, volume));
43049318fe57SMatthew G. Knepley     prerefine = PetscMax(prerefine, 1);
43059318fe57SMatthew G. Knepley   }
4306b23db253SStefano Zampini   if (prerefine) PetscCall(DMLocalizeCoordinates(dm));
43079b44eab4SMatthew G. Knepley   for (r = 0; r < prerefine; ++r) {
43089b44eab4SMatthew G. Knepley     DM             rdm;
43099b44eab4SMatthew G. Knepley     PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc;
43109b44eab4SMatthew G. Knepley 
4311dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
43129566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &rdm));
431369d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &rdm));
4314dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
431561a622f3SMatthew G. Knepley     if (coordFunc && remap) {
43169566063dSJacob Faibussowitsch       PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
43179b44eab4SMatthew G. Knepley       ((DM_Plex *)dm->data)->coordFunc = coordFunc;
43189b44eab4SMatthew G. Knepley     }
43199b44eab4SMatthew G. Knepley   }
43209566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, uniformOrig));
43219318fe57SMatthew G. Knepley   /* Handle DMPlex extrusion before distribution */
43229566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_extrude", "The number of layers to extrude", "", extLayers, &extLayers, NULL, 0));
43239318fe57SMatthew G. Knepley   if (extLayers) {
43249318fe57SMatthew G. Knepley     DM edm;
43259318fe57SMatthew G. Knepley 
43269566063dSJacob Faibussowitsch     PetscCall(DMExtrude(dm, extLayers, &edm));
432769d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &edm));
432848d16a33SMatthew G. Knepley     ((DM_Plex *)dm->data)->coordFunc = NULL;
4329dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
4330d410b0cfSMatthew G. Knepley     extLayers = 0;
43315e17fc22SAidan Hamilton     PetscCall(DMGetDimension(dm, &dim));
43329318fe57SMatthew G. Knepley   }
4333bdf63967SMatthew G. Knepley   /* Handle DMPlex reordering before distribution */
43346bc1bd01Sksagiyam   PetscCall(DMPlexReorderGetDefault(dm, &reorder));
43359566063dSJacob Faibussowitsch   PetscCall(MatGetOrderingList(&ordlist));
43366bc1bd01Sksagiyam   PetscCall(PetscStrncpy(oname, MATORDERINGNATURAL, sizeof(oname)));
43379566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_plex_reorder", "Set mesh reordering type", "DMPlexGetOrdering", ordlist, MATORDERINGNATURAL, oname, sizeof(oname), &flg));
43386bc1bd01Sksagiyam   if (reorder == DMPLEX_REORDER_DEFAULT_TRUE || flg) {
4339bdf63967SMatthew G. Knepley     DM pdm;
4340bdf63967SMatthew G. Knepley     IS perm;
4341bdf63967SMatthew G. Knepley 
43429566063dSJacob Faibussowitsch     PetscCall(DMPlexGetOrdering(dm, oname, NULL, &perm));
43439566063dSJacob Faibussowitsch     PetscCall(DMPlexPermute(dm, perm, &pdm));
43449566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&perm));
434569d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &pdm));
4346dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
4347bdf63967SMatthew G. Knepley   }
43489b44eab4SMatthew G. Knepley   /* Handle DMPlex distribution */
43499566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeGetDefault(dm, &distribute));
4350c506a872SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_distribute", "Flag to redistribute a mesh among processes", "DMPlexDistribute", distribute, &distribute, NULL));
4351a286e215SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_distribute_save_sf", "Flag to save the migration SF", "DMPlexSetMigrationSF", saveSF, &saveSF, NULL));
4352dbbe0bcdSBarry Smith   PetscCall(DMSetFromOptions_Overlap_Plex(dm, PetscOptionsObject, &overlap));
43539b44eab4SMatthew G. Knepley   if (distribute) {
43549b44eab4SMatthew G. Knepley     DM               pdm = NULL;
43559b44eab4SMatthew G. Knepley     PetscPartitioner part;
4356a286e215SMatthew G. Knepley     PetscSF          sfMigration;
43579b44eab4SMatthew G. Knepley 
43589566063dSJacob Faibussowitsch     PetscCall(DMPlexGetPartitioner(dm, &part));
43599566063dSJacob Faibussowitsch     PetscCall(PetscPartitionerSetFromOptions(part));
4360a286e215SMatthew G. Knepley     PetscCall(DMPlexDistribute(dm, overlap, &sfMigration, &pdm));
436148a46eb9SPierre Jolivet     if (pdm) PetscCall(DMPlexReplace_Internal(dm, &pdm));
4362a286e215SMatthew G. Knepley     if (saveSF) PetscCall(DMPlexSetMigrationSF(dm, sfMigration));
4363a286e215SMatthew G. Knepley     PetscCall(PetscSFDestroy(&sfMigration));
43649b44eab4SMatthew G. Knepley   }
4365d2b2dc1eSMatthew G. Knepley   /* Must check CEED options before creating function space for coordinates */
4366d2b2dc1eSMatthew G. Knepley   {
4367d2b2dc1eSMatthew G. Knepley     PetscBool useCeed = PETSC_FALSE, flg;
4368d2b2dc1eSMatthew G. Knepley 
4369d2b2dc1eSMatthew G. Knepley     PetscCall(PetscOptionsBool("-dm_plex_use_ceed", "Use LibCEED as the FEM backend", "DMPlexSetUseCeed", useCeed, &useCeed, &flg));
4370d2b2dc1eSMatthew G. Knepley     if (flg) PetscCall(DMPlexSetUseCeed(dm, useCeed));
4371d2b2dc1eSMatthew G. Knepley   }
43729318fe57SMatthew G. Knepley   /* Create coordinate space */
43739318fe57SMatthew G. Knepley   if (created) {
437461a622f3SMatthew G. Knepley     DM_Plex  *mesh   = (DM_Plex *)dm->data;
4375e44f6aebSMatthew G. Knepley     PetscInt  degree = 1, deg;
43765515ebd3SMatthew G. Knepley     PetscInt  height = 0;
43775515ebd3SMatthew G. Knepley     DM        cdm;
43786858538eSMatthew G. Knepley     PetscBool flg;
43799318fe57SMatthew G. Knepley 
43809566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-dm_coord_space", "Use an FEM space for coordinates", "", coordSpace, &coordSpace, &flg));
43819566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-dm_coord_petscspace_degree", "FEM degree for coordinate space", "", degree, &degree, NULL));
4382e44f6aebSMatthew G. Knepley     PetscCall(DMGetCoordinateDegree_Internal(dm, &deg));
4383e44f6aebSMatthew G. Knepley     if (coordSpace && deg <= 1) PetscCall(DMPlexCreateCoordinateSpace(dm, degree, PETSC_TRUE, mesh->coordFunc));
43845515ebd3SMatthew G. Knepley     PetscCall(DMGetCoordinateDM(dm, &cdm));
438561a622f3SMatthew G. Knepley     if (flg && !coordSpace) {
438661a622f3SMatthew G. Knepley       PetscDS      cds;
438761a622f3SMatthew G. Knepley       PetscObject  obj;
438861a622f3SMatthew G. Knepley       PetscClassId id;
438961a622f3SMatthew G. Knepley 
43909566063dSJacob Faibussowitsch       PetscCall(DMGetDS(cdm, &cds));
43919566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
43929566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
439361a622f3SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
439461a622f3SMatthew G. Knepley         PetscContainer dummy;
439561a622f3SMatthew G. Knepley 
43969566063dSJacob Faibussowitsch         PetscCall(PetscContainerCreate(PETSC_COMM_SELF, &dummy));
43979566063dSJacob Faibussowitsch         PetscCall(PetscObjectSetName((PetscObject)dummy, "coordinates"));
43989566063dSJacob Faibussowitsch         PetscCall(DMSetField(cdm, 0, NULL, (PetscObject)dummy));
43999566063dSJacob Faibussowitsch         PetscCall(PetscContainerDestroy(&dummy));
44009566063dSJacob Faibussowitsch         PetscCall(DMClearDS(cdm));
440161a622f3SMatthew G. Knepley       }
440261a622f3SMatthew G. Knepley       mesh->coordFunc = NULL;
440361a622f3SMatthew G. Knepley     }
44046858538eSMatthew G. Knepley     PetscCall(PetscOptionsBool("-dm_sparse_localize", "Localize only necessary cells", "", dm->sparseLocalize, &dm->sparseLocalize, &flg));
44055515ebd3SMatthew G. Knepley     PetscCall(PetscOptionsInt("-dm_localize_height", "Localize edges and faces in addition to cells", "", height, &height, &flg));
44065515ebd3SMatthew G. Knepley     if (flg) PetscCall(DMPlexSetMaxProjectionHeight(cdm, height));
44079566063dSJacob Faibussowitsch     PetscCall(DMLocalizeCoordinates(dm));
44089318fe57SMatthew G. Knepley   }
440968d4fef7SMatthew G. Knepley   /* Handle DMPlex refinement */
441061a622f3SMatthew G. Knepley   remap = PETSC_TRUE;
44119566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine", "The number of uniform refinements", "DMCreate", refine, &refine, NULL, 0));
44129566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_remap", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL));
44139566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine_hierarchy", "The number of uniform refinements", "DMCreate", refine, &refine, &isHierarchy, 0));
44149566063dSJacob Faibussowitsch   if (refine) PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
441568d4fef7SMatthew G. Knepley   if (refine && isHierarchy) {
4416acdc6f61SToby Isaac     DM *dms, coarseDM;
441768d4fef7SMatthew G. Knepley 
44189566063dSJacob Faibussowitsch     PetscCall(DMGetCoarseDM(dm, &coarseDM));
44199566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)coarseDM));
44209566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(refine, &dms));
44219566063dSJacob Faibussowitsch     PetscCall(DMRefineHierarchy(dm, refine, dms));
442268d4fef7SMatthew G. Knepley     /* Total hack since we do not pass in a pointer */
44239566063dSJacob Faibussowitsch     PetscCall(DMPlexSwap_Static(dm, dms[refine - 1]));
442468d4fef7SMatthew G. Knepley     if (refine == 1) {
44259566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dm, dms[0]));
44269566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dm, PETSC_TRUE));
442768d4fef7SMatthew G. Knepley     } else {
44289566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dm, dms[refine - 2]));
44299566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dm, PETSC_TRUE));
44309566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dms[0], dms[refine - 1]));
44319566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dms[0], PETSC_TRUE));
443268d4fef7SMatthew G. Knepley     }
44339566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dms[refine - 1], coarseDM));
44349566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)coarseDM));
443568d4fef7SMatthew G. Knepley     /* Free DMs */
443668d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
4437dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dms[r], PetscOptionsObject));
44389566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dms[r]));
443968d4fef7SMatthew G. Knepley     }
44409566063dSJacob Faibussowitsch     PetscCall(PetscFree(dms));
444168d4fef7SMatthew G. Knepley   } else {
444268d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
44439318fe57SMatthew G. Knepley       DM             rdm;
444451a74b61SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc;
444568d4fef7SMatthew G. Knepley 
4446dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
44479566063dSJacob Faibussowitsch       PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &rdm));
444868d4fef7SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
444969d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &rdm));
4450dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
445161a622f3SMatthew G. Knepley       if (coordFunc && remap) {
44529566063dSJacob Faibussowitsch         PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
445351a74b61SMatthew G. Knepley         ((DM_Plex *)dm->data)->coordFunc = coordFunc;
445451a74b61SMatthew G. Knepley       }
445568d4fef7SMatthew G. Knepley     }
445668d4fef7SMatthew G. Knepley   }
44573cf6fe12SMatthew G. Knepley   /* Handle DMPlex coarsening */
44589566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_coarsen", "Coarsen the mesh", "DMCreate", coarsen, &coarsen, NULL, 0));
44599566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_coarsen_hierarchy", "The number of coarsenings", "DMCreate", coarsen, &coarsen, &isHierarchy, 0));
4460b653a561SMatthew G. Knepley   if (coarsen && isHierarchy) {
4461b653a561SMatthew G. Knepley     DM *dms;
4462b653a561SMatthew G. Knepley 
44639566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(coarsen, &dms));
44649566063dSJacob Faibussowitsch     PetscCall(DMCoarsenHierarchy(dm, coarsen, dms));
4465b653a561SMatthew G. Knepley     /* Free DMs */
4466b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
4467dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dms[r], PetscOptionsObject));
44689566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dms[r]));
4469b653a561SMatthew G. Knepley     }
44709566063dSJacob Faibussowitsch     PetscCall(PetscFree(dms));
4471b653a561SMatthew G. Knepley   } else {
4472b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
44739318fe57SMatthew G. Knepley       DM             cdm;
44749318fe57SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc;
44753cf6fe12SMatthew G. Knepley 
4476dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
44779566063dSJacob Faibussowitsch       PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &cdm));
44783cf6fe12SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
447969d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &cdm));
4480dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
44819318fe57SMatthew G. Knepley       if (coordFunc) {
44829566063dSJacob Faibussowitsch         PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
44839318fe57SMatthew G. Knepley         ((DM_Plex *)dm->data)->coordFunc = coordFunc;
44849318fe57SMatthew G. Knepley       }
44853cf6fe12SMatthew G. Knepley     }
4486b653a561SMatthew G. Knepley   }
4487be664eb1SMatthew G. Knepley   // Handle coordinate remapping
4488be664eb1SMatthew G. Knepley   remap = PETSC_FALSE;
4489be664eb1SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_coord_remap", "Flag to control coordinate remapping", "", remap, &remap, NULL));
4490be664eb1SMatthew G. Knepley   if (remap) {
4491be664eb1SMatthew G. Knepley     DMPlexCoordMap map     = DM_COORD_MAP_NONE;
4492be664eb1SMatthew G. Knepley     PetscPointFunc mapFunc = NULL;
4493be664eb1SMatthew G. Knepley     PetscScalar    params[16];
4494be664eb1SMatthew G. Knepley     PetscInt       Np = sizeof(params) / sizeof(params[0]), cdim;
4495be664eb1SMatthew G. Knepley     MPI_Comm       comm;
4496be664eb1SMatthew G. Knepley 
4497be664eb1SMatthew G. Knepley     PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4498be664eb1SMatthew G. Knepley     PetscCall(DMGetCoordinateDim(dm, &cdim));
4499be664eb1SMatthew G. Knepley     PetscCall(PetscOptionsScalarArray("-dm_coord_map_params", "Parameters for the coordinate remapping", "", params, &Np, &flg));
4500be664eb1SMatthew G. Knepley     if (!flg) Np = 0;
4501be664eb1SMatthew G. Knepley     // TODO Allow user to pass a map function by name
4502be664eb1SMatthew G. Knepley     PetscCall(PetscOptionsEnum("-dm_coord_map", "Coordinate mapping for built-in mesh", "", DMPlexCoordMaps, (PetscEnum)map, (PetscEnum *)&map, &flg));
4503be664eb1SMatthew G. Knepley     if (flg) {
4504be664eb1SMatthew G. Knepley       switch (map) {
4505be664eb1SMatthew G. Knepley       case DM_COORD_MAP_NONE:
4506be664eb1SMatthew G. Knepley         mapFunc = coordMap_identity;
4507be664eb1SMatthew G. Knepley         break;
4508be664eb1SMatthew G. Knepley       case DM_COORD_MAP_SHEAR:
4509be664eb1SMatthew G. Knepley         mapFunc = coordMap_shear;
4510be664eb1SMatthew G. Knepley         if (!Np) {
4511be664eb1SMatthew G. Knepley           Np        = cdim + 1;
4512be664eb1SMatthew G. Knepley           params[0] = 0;
4513be664eb1SMatthew G. Knepley           for (PetscInt d = 1; d <= cdim; ++d) params[d] = 1.0;
4514be664eb1SMatthew G. Knepley         }
4515be664eb1SMatthew G. Knepley         PetscCheck(Np == cdim + 1, comm, PETSC_ERR_ARG_WRONG, "The shear coordinate map must have cdim + 1 = %" PetscInt_FMT " parameters, not %" PetscInt_FMT, cdim + 1, Np);
4516be664eb1SMatthew G. Knepley         break;
4517be664eb1SMatthew G. Knepley       case DM_COORD_MAP_FLARE:
4518be664eb1SMatthew G. Knepley         mapFunc = coordMap_flare;
4519be664eb1SMatthew G. Knepley         if (!Np) {
4520be664eb1SMatthew G. Knepley           Np        = cdim + 1;
4521be664eb1SMatthew G. Knepley           params[0] = 0;
4522be664eb1SMatthew G. Knepley           for (PetscInt d = 1; d <= cdim; ++d) params[d] = 1.0;
4523be664eb1SMatthew G. Knepley         }
4524be664eb1SMatthew G. Knepley         PetscCheck(Np == cdim + 1, comm, PETSC_ERR_ARG_WRONG, "The flare coordinate map must have cdim + 1 = %" PetscInt_FMT " parameters, not %" PetscInt_FMT, cdim + 1, Np);
4525be664eb1SMatthew G. Knepley         break;
4526be664eb1SMatthew G. Knepley       case DM_COORD_MAP_ANNULUS:
4527be664eb1SMatthew G. Knepley         mapFunc = coordMap_annulus;
4528be664eb1SMatthew G. Knepley         if (!Np) {
4529be664eb1SMatthew G. Knepley           Np        = 2;
4530be664eb1SMatthew G. Knepley           params[0] = 1.;
4531be664eb1SMatthew G. Knepley           params[1] = 2.;
4532be664eb1SMatthew G. Knepley         }
4533be664eb1SMatthew G. Knepley         PetscCheck(Np == 2, comm, PETSC_ERR_ARG_WRONG, "The annulus coordinate map must have 2 parameters, not %" PetscInt_FMT, Np);
4534be664eb1SMatthew G. Knepley         break;
4535be664eb1SMatthew G. Knepley       case DM_COORD_MAP_SHELL:
4536be664eb1SMatthew G. Knepley         mapFunc = coordMap_shell;
4537be664eb1SMatthew G. Knepley         if (!Np) {
4538be664eb1SMatthew G. Knepley           Np        = 2;
4539be664eb1SMatthew G. Knepley           params[0] = 1.;
4540be664eb1SMatthew G. Knepley           params[1] = 2.;
4541be664eb1SMatthew G. Knepley         }
4542be664eb1SMatthew G. Knepley         PetscCheck(Np == 2, comm, PETSC_ERR_ARG_WRONG, "The spherical shell coordinate map must have 2 parameters, not %" PetscInt_FMT, Np);
4543be664eb1SMatthew G. Knepley         break;
4544be664eb1SMatthew G. Knepley       default:
4545be664eb1SMatthew G. Knepley         mapFunc = coordMap_identity;
4546be664eb1SMatthew G. Knepley       }
4547be664eb1SMatthew G. Knepley     }
4548be664eb1SMatthew G. Knepley     if (Np) {
4549be664eb1SMatthew G. Knepley       DM      cdm;
4550be664eb1SMatthew G. Knepley       PetscDS cds;
4551be664eb1SMatthew G. Knepley 
4552be664eb1SMatthew G. Knepley       PetscCall(DMGetCoordinateDM(dm, &cdm));
4553be664eb1SMatthew G. Knepley       PetscCall(DMGetDS(cdm, &cds));
4554be664eb1SMatthew G. Knepley       PetscCall(PetscDSSetConstants(cds, Np, params));
4555be664eb1SMatthew G. Knepley     }
4556be664eb1SMatthew G. Knepley     PetscCall(DMPlexRemapGeometry(dm, 0.0, mapFunc));
4557be664eb1SMatthew G. Knepley   }
4558909dfd52SMatthew G. Knepley   /* Handle ghost cells */
45599566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_create_fv_ghost_cells", "Flag to create finite volume ghost cells on the boundary", "DMCreate", ghostCells, &ghostCells, NULL));
4560909dfd52SMatthew G. Knepley   if (ghostCells) {
4561909dfd52SMatthew G. Knepley     DM   gdm;
4562909dfd52SMatthew G. Knepley     char lname[PETSC_MAX_PATH_LEN];
4563909dfd52SMatthew G. Knepley 
4564909dfd52SMatthew G. Knepley     lname[0] = '\0';
45659566063dSJacob Faibussowitsch     PetscCall(PetscOptionsString("-dm_plex_fv_ghost_cells_label", "Label name for ghost cells boundary", "DMCreate", lname, lname, sizeof(lname), &flg));
45669566063dSJacob Faibussowitsch     PetscCall(DMPlexConstructGhostCells(dm, flg ? lname : NULL, NULL, &gdm));
456769d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &gdm));
4568909dfd52SMatthew G. Knepley   }
45696913077dSMatthew G. Knepley   /* Handle 1D order */
45706bc1bd01Sksagiyam   if (reorder != DMPLEX_REORDER_DEFAULT_FALSE && dim == 1) {
45716913077dSMatthew G. Knepley     DM           cdm, rdm;
45726913077dSMatthew G. Knepley     PetscDS      cds;
45736913077dSMatthew G. Knepley     PetscObject  obj;
45746913077dSMatthew G. Knepley     PetscClassId id = PETSC_OBJECT_CLASSID;
45756913077dSMatthew G. Knepley     IS           perm;
45766bc1bd01Sksagiyam     PetscInt     Nf;
45776913077dSMatthew G. Knepley     PetscBool    distributed;
45786913077dSMatthew G. Knepley 
45799566063dSJacob Faibussowitsch     PetscCall(DMPlexIsDistributed(dm, &distributed));
45809566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
45819566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
45829566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(cds, &Nf));
45836913077dSMatthew G. Knepley     if (Nf) {
45849566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
45859566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
45866913077dSMatthew G. Knepley     }
45876bc1bd01Sksagiyam     if (!distributed && id != PETSCFE_CLASSID) {
45889566063dSJacob Faibussowitsch       PetscCall(DMPlexGetOrdering1D(dm, &perm));
45899566063dSJacob Faibussowitsch       PetscCall(DMPlexPermute(dm, perm, &rdm));
459069d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &rdm));
45919566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&perm));
45926913077dSMatthew G. Knepley     }
45936913077dSMatthew G. Knepley   }
45943cf6fe12SMatthew G. Knepley /* Handle */
4595dd4c3f67SMatthew G. Knepley non_refine:
4596dbbe0bcdSBarry Smith   PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
4597d0609cedSBarry Smith   PetscOptionsHeadEnd();
45983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
45990a6ba040SMatthew G. Knepley }
46000a6ba040SMatthew G. Knepley 
4601d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMCreateGlobalVector_Plex(DM dm, Vec *vec)
4602d71ae5a4SJacob Faibussowitsch {
4603552f7358SJed Brown   PetscFunctionBegin;
46049566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector_Section_Private(dm, vec));
46059566063dSJacob Faibussowitsch   /* PetscCall(VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM)); */
46069566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEW, (void (*)(void))VecView_Plex));
46079566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEWNATIVE, (void (*)(void))VecView_Plex_Native));
46089566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOAD, (void (*)(void))VecLoad_Plex));
46099566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOADNATIVE, (void (*)(void))VecLoad_Plex_Native));
46103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4611552f7358SJed Brown }
4612552f7358SJed Brown 
4613d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMCreateLocalVector_Plex(DM dm, Vec *vec)
4614d71ae5a4SJacob Faibussowitsch {
4615552f7358SJed Brown   PetscFunctionBegin;
46169566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector_Section_Private(dm, vec));
46179566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEW, (void (*)(void))VecView_Plex_Local));
46189566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOAD, (void (*)(void))VecLoad_Plex_Local));
46193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4620552f7358SJed Brown }
4621552f7358SJed Brown 
4622d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGetDimPoints_Plex(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
4623d71ae5a4SJacob Faibussowitsch {
4624793f3fe5SMatthew G. Knepley   PetscInt depth, d;
4625793f3fe5SMatthew G. Knepley 
4626793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
46279566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
4628793f3fe5SMatthew G. Knepley   if (depth == 1) {
46299566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(dm, &d));
46309566063dSJacob Faibussowitsch     if (dim == 0) PetscCall(DMPlexGetDepthStratum(dm, dim, pStart, pEnd));
46319566063dSJacob Faibussowitsch     else if (dim == d) PetscCall(DMPlexGetDepthStratum(dm, 1, pStart, pEnd));
46329371c9d4SSatish Balay     else {
46339371c9d4SSatish Balay       *pStart = 0;
46349371c9d4SSatish Balay       *pEnd   = 0;
46359371c9d4SSatish Balay     }
4636793f3fe5SMatthew G. Knepley   } else {
46379566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, dim, pStart, pEnd));
4638793f3fe5SMatthew G. Knepley   }
46393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4640793f3fe5SMatthew G. Knepley }
4641793f3fe5SMatthew G. Knepley 
4642d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGetNeighbors_Plex(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
4643d71ae5a4SJacob Faibussowitsch {
4644502a2867SDave May   PetscSF            sf;
46450a19bb7dSprj-   PetscInt           niranks, njranks, n;
46460a19bb7dSprj-   const PetscMPIInt *iranks, *jranks;
46470a19bb7dSprj-   DM_Plex           *data = (DM_Plex *)dm->data;
4648502a2867SDave May 
46492f356facSMatthew G. Knepley   PetscFunctionBegin;
46509566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
46510a19bb7dSprj-   if (!data->neighbors) {
46529566063dSJacob Faibussowitsch     PetscCall(PetscSFSetUp(sf));
46539566063dSJacob Faibussowitsch     PetscCall(PetscSFGetRootRanks(sf, &njranks, &jranks, NULL, NULL, NULL));
46549566063dSJacob Faibussowitsch     PetscCall(PetscSFGetLeafRanks(sf, &niranks, &iranks, NULL, NULL));
46559566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(njranks + niranks + 1, &data->neighbors));
46569566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(data->neighbors + 1, jranks, njranks));
46579566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(data->neighbors + njranks + 1, iranks, niranks));
46580a19bb7dSprj-     n = njranks + niranks;
46599566063dSJacob Faibussowitsch     PetscCall(PetscSortRemoveDupsMPIInt(&n, data->neighbors + 1));
46600a19bb7dSprj-     /* The following cast should never fail: can't have more neighbors than PETSC_MPI_INT_MAX */
46619566063dSJacob Faibussowitsch     PetscCall(PetscMPIIntCast(n, data->neighbors));
46620a19bb7dSprj-   }
46630a19bb7dSprj-   if (nranks) *nranks = data->neighbors[0];
46640a19bb7dSprj-   if (ranks) {
46650a19bb7dSprj-     if (data->neighbors[0]) *ranks = data->neighbors + 1;
46660a19bb7dSprj-     else *ranks = NULL;
46670a19bb7dSprj-   }
46683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4669502a2867SDave May }
4670502a2867SDave May 
46711eb70e55SToby Isaac PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM, DM, Mat, Vec, Vec);
46721eb70e55SToby Isaac 
4673d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMInitialize_Plex(DM dm)
4674d71ae5a4SJacob Faibussowitsch {
4675552f7358SJed Brown   PetscFunctionBegin;
4676552f7358SJed Brown   dm->ops->view                      = DMView_Plex;
46772c40f234SMatthew G. Knepley   dm->ops->load                      = DMLoad_Plex;
4678552f7358SJed Brown   dm->ops->setfromoptions            = DMSetFromOptions_Plex;
467938221697SMatthew G. Knepley   dm->ops->clone                     = DMClone_Plex;
4680552f7358SJed Brown   dm->ops->setup                     = DMSetUp_Plex;
46811bb6d2a8SBarry Smith   dm->ops->createlocalsection        = DMCreateLocalSection_Plex;
468266ad2231SToby Isaac   dm->ops->createdefaultconstraints  = DMCreateDefaultConstraints_Plex;
4683552f7358SJed Brown   dm->ops->createglobalvector        = DMCreateGlobalVector_Plex;
4684552f7358SJed Brown   dm->ops->createlocalvector         = DMCreateLocalVector_Plex;
4685184d77edSJed Brown   dm->ops->getlocaltoglobalmapping   = NULL;
46860298fd71SBarry Smith   dm->ops->createfieldis             = NULL;
4687552f7358SJed Brown   dm->ops->createcoordinatedm        = DMCreateCoordinateDM_Plex;
4688f19dbd58SToby Isaac   dm->ops->createcoordinatefield     = DMCreateCoordinateField_Plex;
46890a6ba040SMatthew G. Knepley   dm->ops->getcoloring               = NULL;
4690552f7358SJed Brown   dm->ops->creatematrix              = DMCreateMatrix_Plex;
4691bceba477SMatthew G. Knepley   dm->ops->createinterpolation       = DMCreateInterpolation_Plex;
4692bd041c0cSMatthew G. Knepley   dm->ops->createmassmatrix          = DMCreateMassMatrix_Plex;
4693b4937a87SMatthew G. Knepley   dm->ops->createmassmatrixlumped    = DMCreateMassMatrixLumped_Plex;
46945a84ad33SLisandro Dalcin   dm->ops->createinjection           = DMCreateInjection_Plex;
4695552f7358SJed Brown   dm->ops->refine                    = DMRefine_Plex;
46960a6ba040SMatthew G. Knepley   dm->ops->coarsen                   = DMCoarsen_Plex;
46970a6ba040SMatthew G. Knepley   dm->ops->refinehierarchy           = DMRefineHierarchy_Plex;
4698b653a561SMatthew G. Knepley   dm->ops->coarsenhierarchy          = DMCoarsenHierarchy_Plex;
4699d410b0cfSMatthew G. Knepley   dm->ops->extrude                   = DMExtrude_Plex;
47000298fd71SBarry Smith   dm->ops->globaltolocalbegin        = NULL;
47010298fd71SBarry Smith   dm->ops->globaltolocalend          = NULL;
47020298fd71SBarry Smith   dm->ops->localtoglobalbegin        = NULL;
47030298fd71SBarry Smith   dm->ops->localtoglobalend          = NULL;
4704552f7358SJed Brown   dm->ops->destroy                   = DMDestroy_Plex;
4705552f7358SJed Brown   dm->ops->createsubdm               = DMCreateSubDM_Plex;
47062adcc780SMatthew G. Knepley   dm->ops->createsuperdm             = DMCreateSuperDM_Plex;
4707793f3fe5SMatthew G. Knepley   dm->ops->getdimpoints              = DMGetDimPoints_Plex;
4708552f7358SJed Brown   dm->ops->locatepoints              = DMLocatePoints_Plex;
47090709b2feSToby Isaac   dm->ops->projectfunctionlocal      = DMProjectFunctionLocal_Plex;
47100709b2feSToby Isaac   dm->ops->projectfunctionlabellocal = DMProjectFunctionLabelLocal_Plex;
4711bfc4295aSToby Isaac   dm->ops->projectfieldlocal         = DMProjectFieldLocal_Plex;
47128c6c5593SMatthew G. Knepley   dm->ops->projectfieldlabellocal    = DMProjectFieldLabelLocal_Plex;
4713ece3a9fcSMatthew G. Knepley   dm->ops->projectbdfieldlabellocal  = DMProjectBdFieldLabelLocal_Plex;
47140709b2feSToby Isaac   dm->ops->computel2diff             = DMComputeL2Diff_Plex;
4715b698f381SToby Isaac   dm->ops->computel2gradientdiff     = DMComputeL2GradientDiff_Plex;
47162a16baeaSToby Isaac   dm->ops->computel2fielddiff        = DMComputeL2FieldDiff_Plex;
471728d58a37SPierre Jolivet   dm->ops->getneighbors              = DMGetNeighbors_Plex;
4718907a3e9cSStefano Zampini   dm->ops->createdomaindecomposition = DMCreateDomainDecomposition_Plex;
4719907a3e9cSStefano Zampini   dm->ops->createddscatters          = DMCreateDomainDecompositionScatters_Plex;
47209566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertBoundaryValues_C", DMPlexInsertBoundaryValues_Plex));
47216c51210dSStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertTimeDerivativeBoundaryValues_C", DMPlexInsertTimeDerivativeBoundaryValues_Plex));
47229566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMSetUpGLVisViewer_C", DMSetUpGLVisViewer_Plex));
47239566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMCreateNeumannOverlap_C", DMCreateNeumannOverlap_Plex));
47249566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeGetDefault_C", DMPlexDistributeGetDefault_Plex));
47259566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeSetDefault_C", DMPlexDistributeSetDefault_Plex));
47266bc1bd01Sksagiyam   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderGetDefault_C", DMPlexReorderGetDefault_Plex));
47276bc1bd01Sksagiyam   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderSetDefault_C", DMPlexReorderSetDefault_Plex));
4728d02c7345SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderSectionGetDefault_C", DMPlexReorderSectionGetDefault_Plex));
4729d02c7345SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderSectionSetDefault_C", DMPlexReorderSectionSetDefault_Plex));
47309566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMInterpolateSolution_C", DMInterpolateSolution_Plex));
4731c506a872SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetOverlap_C", DMPlexGetOverlap_Plex));
4732c506a872SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexSetOverlap_C", DMPlexSetOverlap_Plex));
4733d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetUseCeed_C", DMPlexGetUseCeed_Plex));
4734d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexSetUseCeed_C", DMPlexSetUseCeed_Plex));
47353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4736552f7358SJed Brown }
4737552f7358SJed Brown 
4738d71ae5a4SJacob Faibussowitsch PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm)
4739d71ae5a4SJacob Faibussowitsch {
474063a16f15SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
47416725e60dSJed Brown   PetscSF  face_sf;
474263a16f15SMatthew G. Knepley 
474363a16f15SMatthew G. Knepley   PetscFunctionBegin;
474463a16f15SMatthew G. Knepley   mesh->refct++;
474563a16f15SMatthew G. Knepley   (*newdm)->data = mesh;
47465f06a3ddSJed Brown   PetscCall(DMPlexGetIsoperiodicFaceSF(dm, &face_sf));
47475f06a3ddSJed Brown   PetscCall(DMPlexSetIsoperiodicFaceSF(*newdm, face_sf));
47489566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)*newdm, DMPLEX));
47499566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(*newdm));
47503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
475163a16f15SMatthew G. Knepley }
475263a16f15SMatthew G. Knepley 
47538818961aSMatthew G Knepley /*MC
4754a1cb98faSBarry Smith   DMPLEX = "plex" - A `DM` object that encapsulates an unstructured mesh, or CW Complex, which can be expressed using a Hasse Diagram.
475520f4b53cSBarry Smith                     In the local representation, `Vec`s contain all unknowns in the interior and shared boundary. This is
47568818961aSMatthew G Knepley                     specified by a PetscSection object. Ownership in the global representation is determined by
4757a1cb98faSBarry Smith                     ownership of the underlying `DMPLEX` points. This is specified by another `PetscSection` object.
47588818961aSMatthew G Knepley 
4759e5893cccSMatthew G. Knepley   Options Database Keys:
4760250712c9SMatthew G. Knepley + -dm_refine_pre                     - Refine mesh before distribution
4761250712c9SMatthew G. Knepley + -dm_refine_uniform_pre             - Choose uniform or generator-based refinement
4762250712c9SMatthew G. Knepley + -dm_refine_volume_limit_pre        - Cell volume limit after pre-refinement using generator
4763250712c9SMatthew G. Knepley . -dm_distribute                     - Distribute mesh across processes
4764250712c9SMatthew G. Knepley . -dm_distribute_overlap             - Number of cells to overlap for distribution
4765250712c9SMatthew G. Knepley . -dm_refine                         - Refine mesh after distribution
4766250712c9SMatthew G. Knepley . -dm_plex_hash_location             - Use grid hashing for point location
4767ddce0771SMatthew G. Knepley . -dm_plex_hash_box_faces <n,m,p>    - The number of divisions in each direction of the grid hash
4768f12cf164SMatthew G. Knepley . -dm_plex_partition_balance         - Attempt to evenly divide points on partition boundary between processes
4769f12cf164SMatthew G. Knepley . -dm_plex_remesh_bd                 - Allow changes to the boundary on remeshing
4770d5b43468SJose E. Roman . -dm_plex_max_projection_height     - Maximum mesh point height used to project locally
4771f12cf164SMatthew G. Knepley . -dm_plex_regular_refinement        - Use special nested projection algorithm for regular refinement
4772d02c7345SMatthew G. Knepley . -dm_plex_reorder_section           - Use specialized blocking if available
4773aaa8cc7dSPierre Jolivet . -dm_plex_check_all                 - Perform all checks below
4774f12cf164SMatthew G. Knepley . -dm_plex_check_symmetry            - Check that the adjacency information in the mesh is symmetric
4775f12cf164SMatthew G. Knepley . -dm_plex_check_skeleton <celltype> - Check that each cell has the correct number of vertices
4776f12cf164SMatthew 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
4777f12cf164SMatthew G. Knepley . -dm_plex_check_geometry            - Check that cells have positive volume
4778f12cf164SMatthew G. Knepley . -dm_view :mesh.tex:ascii_latex     - View the mesh in LaTeX/TikZ
4779e5893cccSMatthew G. Knepley . -dm_plex_view_scale <num>          - Scale the TikZ
47805962854dSMatthew G. Knepley . -dm_plex_print_fem <num>           - View FEM assembly information, such as element vectors and matrices
47815962854dSMatthew G. Knepley - -dm_plex_print_fvm <num>           - View FVM assembly information, such as flux updates
4782e5893cccSMatthew G. Knepley 
47838818961aSMatthew G Knepley   Level: intermediate
47848818961aSMatthew G Knepley 
47851cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMType`, `DMPlexCreate()`, `DMCreate()`, `DMSetType()`, `PetscSection`
47868818961aSMatthew G Knepley M*/
47878818961aSMatthew G Knepley 
4788d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode DMCreate_Plex(DM dm)
4789d71ae5a4SJacob Faibussowitsch {
4790552f7358SJed Brown   DM_Plex *mesh;
4791412e9a14SMatthew G. Knepley   PetscInt unit;
4792552f7358SJed Brown 
4793552f7358SJed Brown   PetscFunctionBegin;
4794f39ec787SMatthew G. Knepley   PetscCall(PetscCitationsRegister(PlexCitation, &Plexcite));
4795552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47964dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&mesh));
4797552f7358SJed Brown   dm->data = mesh;
4798552f7358SJed Brown 
4799552f7358SJed Brown   mesh->refct = 1;
48009566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->coneSection));
48019566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->supportSection));
4802552f7358SJed Brown   mesh->refinementUniform      = PETSC_TRUE;
4803552f7358SJed Brown   mesh->refinementLimit        = -1.0;
4804e600fa54SMatthew G. Knepley   mesh->distDefault            = PETSC_TRUE;
48056bc1bd01Sksagiyam   mesh->reorderDefault         = DMPLEX_REORDER_DEFAULT_NOTSET;
4806d02c7345SMatthew G. Knepley   mesh->reorderSection         = DMPLEX_REORDER_DEFAULT_NOTSET;
48071d1f2f2aSksagiyam   mesh->distributionName       = NULL;
48087d0f5628SVaclav Hapla   mesh->interpolated           = DMPLEX_INTERPOLATED_INVALID;
48097d0f5628SVaclav Hapla   mesh->interpolatedCollective = DMPLEX_INTERPOLATED_INVALID;
4810552f7358SJed Brown 
48119566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerCreate(PetscObjectComm((PetscObject)dm), &mesh->partitioner));
48122e62ab5aSMatthew G. Knepley   mesh->remeshBd = PETSC_FALSE;
4813d9deefdfSMatthew G. Knepley 
48148865f1eaSKarl Rupp   for (unit = 0; unit < NUM_PETSC_UNITS; ++unit) mesh->scale[unit] = 1.0;
4815552f7358SJed Brown 
4816df0420ecSMatthew G. Knepley   mesh->depthState    = -1;
4817ba2698f1SMatthew G. Knepley   mesh->celltypeState = -1;
48186113b454SMatthew G. Knepley   mesh->printTol      = 1.0e-10;
4819552f7358SJed Brown 
48209566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(dm));
48213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4822552f7358SJed Brown }
4823552f7358SJed Brown 
4824552f7358SJed Brown /*@
4825a1cb98faSBarry Smith   DMPlexCreate - Creates a `DMPLEX` object, which encapsulates an unstructured mesh, or CW complex, which can be expressed using a Hasse Diagram.
4826552f7358SJed Brown 
4827d083f849SBarry Smith   Collective
4828552f7358SJed Brown 
4829552f7358SJed Brown   Input Parameter:
4830a1cb98faSBarry Smith . comm - The communicator for the `DMPLEX` object
4831552f7358SJed Brown 
4832552f7358SJed Brown   Output Parameter:
4833a1cb98faSBarry Smith . mesh - The `DMPLEX` object
4834552f7358SJed Brown 
4835552f7358SJed Brown   Level: beginner
4836552f7358SJed Brown 
483742747ad1SJacob Faibussowitsch .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMType`, `DMCreate()`, `DMSetType()`
4838552f7358SJed Brown @*/
4839d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreate(MPI_Comm comm, DM *mesh)
4840d71ae5a4SJacob Faibussowitsch {
4841552f7358SJed Brown   PetscFunctionBegin;
48424f572ea9SToby Isaac   PetscAssertPointer(mesh, 2);
48439566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, mesh));
48449566063dSJacob Faibussowitsch   PetscCall(DMSetType(*mesh, DMPLEX));
48453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4846552f7358SJed Brown }
4847552f7358SJed Brown 
4848b09969d6SVaclav Hapla /*@C
4849a1cb98faSBarry Smith   DMPlexBuildFromCellListParallel - Build distributed `DMPLEX` topology from a list of vertices for each cell (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 . numCells    - The number of cells owned by this process
4856a1cb98faSBarry Smith . numVertices - The number of vertices to be owned by this process, or `PETSC_DECIDE`
4857a1cb98faSBarry Smith . NVertices   - The global number of vertices, or `PETSC_DETERMINE`
4858b09969d6SVaclav Hapla . numCorners  - The number of vertices for each cell
48595e488331SVaclav Hapla - cells       - An array of numCells*numCorners numbers, the global vertex numbers for each cell
4860b09969d6SVaclav Hapla 
4861be8c289dSNicolas Barral   Output Parameters:
4862a1cb98faSBarry Smith + vertexSF         - (Optional) `PetscSF` describing complete vertex ownership
4863be8c289dSNicolas Barral - verticesAdjSaved - (Optional) vertex adjacency array
4864b09969d6SVaclav Hapla 
4865b09969d6SVaclav Hapla   Level: advanced
4866b09969d6SVaclav Hapla 
4867a1cb98faSBarry Smith   Notes:
4868a1cb98faSBarry Smith   Two triangles sharing a face
4869a1cb98faSBarry Smith .vb
4870a1cb98faSBarry Smith 
4871a1cb98faSBarry Smith         2
4872a1cb98faSBarry Smith       / | \
4873a1cb98faSBarry Smith      /  |  \
4874a1cb98faSBarry Smith     /   |   \
4875a1cb98faSBarry Smith    0  0 | 1  3
4876a1cb98faSBarry Smith     \   |   /
4877a1cb98faSBarry Smith      \  |  /
4878a1cb98faSBarry Smith       \ | /
4879a1cb98faSBarry Smith         1
4880a1cb98faSBarry Smith .ve
4881a1cb98faSBarry Smith   would have input
4882a1cb98faSBarry Smith .vb
4883a1cb98faSBarry Smith   numCells = 2, numVertices = 4
4884a1cb98faSBarry Smith   cells = [0 1 2  1 3 2]
4885a1cb98faSBarry Smith .ve
4886a1cb98faSBarry Smith   which would result in the `DMPLEX`
4887a1cb98faSBarry Smith .vb
4888a1cb98faSBarry Smith 
4889a1cb98faSBarry Smith         4
4890a1cb98faSBarry Smith       / | \
4891a1cb98faSBarry Smith      /  |  \
4892a1cb98faSBarry Smith     /   |   \
4893a1cb98faSBarry Smith    2  0 | 1  5
4894a1cb98faSBarry Smith     \   |   /
4895a1cb98faSBarry Smith      \  |  /
4896a1cb98faSBarry Smith       \ | /
4897a1cb98faSBarry Smith         3
4898a1cb98faSBarry Smith .ve
4899a1cb98faSBarry Smith 
4900a1cb98faSBarry Smith   Vertices are implicitly numbered consecutively 0,...,NVertices.
4901a1cb98faSBarry Smith   Each rank owns a chunk of numVertices consecutive vertices.
4902a1cb98faSBarry Smith   If numVertices is `PETSC_DECIDE`, PETSc will distribute them as evenly as possible using PetscLayout.
4903a1cb98faSBarry Smith   If NVertices is `PETSC_DETERMINE` and numVertices is PETSC_DECIDE, NVertices is computed by PETSc as the maximum vertex index in cells + 1.
4904a1cb98faSBarry Smith   If only NVertices is `PETSC_DETERMINE`, it is computed as the sum of numVertices over all ranks.
4905a1cb98faSBarry Smith 
4906a1cb98faSBarry Smith   The cell distribution is arbitrary non-overlapping, independent of the vertex distribution.
4907a1cb98faSBarry Smith 
49081cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildFromCellList()`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildCoordinatesFromCellListParallel()`,
4909a1cb98faSBarry Smith           `PetscSF`
4910b09969d6SVaclav Hapla @*/
4911d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildFromCellListParallel(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt NVertices, PetscInt numCorners, const PetscInt cells[], PetscSF *vertexSF, PetscInt **verticesAdjSaved)
4912d71ae5a4SJacob Faibussowitsch {
49132464107aSksagiyam   PetscSF     sfPoint;
49142464107aSksagiyam   PetscLayout layout;
491582fb893eSVaclav Hapla   PetscInt    numVerticesAdj, *verticesAdj, *cones, c, p;
4916a47d0d45SMatthew G. Knepley 
4917a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
491825b6865aSVaclav Hapla   PetscValidLogicalCollectiveInt(dm, NVertices, 4);
49199566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
492025b6865aSVaclav Hapla   /* Get/check global number of vertices */
492125b6865aSVaclav Hapla   {
492225b6865aSVaclav Hapla     PetscInt       NVerticesInCells, i;
492325b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
492425b6865aSVaclav Hapla 
492525b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
492625b6865aSVaclav Hapla     NVerticesInCells = PETSC_MIN_INT;
49279371c9d4SSatish Balay     for (i = 0; i < len; i++)
49289371c9d4SSatish Balay       if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
492925b6865aSVaclav Hapla     ++NVerticesInCells;
4930712fec58SPierre Jolivet     PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &NVerticesInCells, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
493125b6865aSVaclav Hapla 
493225b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE && NVertices == PETSC_DECIDE) NVertices = NVerticesInCells;
49339371c9d4SSatish Balay     else
49349371c9d4SSatish 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);
493525b6865aSVaclav Hapla   }
49369079aca8SVaclav Hapla   /* Count locally unique vertices */
49379079aca8SVaclav Hapla   {
49389079aca8SVaclav Hapla     PetscHSetI vhash;
49399079aca8SVaclav Hapla     PetscInt   off = 0;
49409079aca8SVaclav Hapla 
49419566063dSJacob Faibussowitsch     PetscCall(PetscHSetICreate(&vhash));
4942a47d0d45SMatthew G. Knepley     for (c = 0; c < numCells; ++c) {
494348a46eb9SPierre Jolivet       for (p = 0; p < numCorners; ++p) PetscCall(PetscHSetIAdd(vhash, cells[c * numCorners + p]));
4944a47d0d45SMatthew G. Knepley     }
49459566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetSize(vhash, &numVerticesAdj));
49469566063dSJacob Faibussowitsch     if (!verticesAdjSaved) PetscCall(PetscMalloc1(numVerticesAdj, &verticesAdj));
4947ad540459SPierre Jolivet     else verticesAdj = *verticesAdjSaved;
49489566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetElems(vhash, &off, verticesAdj));
49499566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&vhash));
495063a3b9bcSJacob Faibussowitsch     PetscCheck(off == numVerticesAdj, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid number of local vertices %" PetscInt_FMT " should be %" PetscInt_FMT, off, numVerticesAdj);
4951a47d0d45SMatthew G. Knepley   }
49529566063dSJacob Faibussowitsch   PetscCall(PetscSortInt(numVerticesAdj, verticesAdj));
4953a47d0d45SMatthew G. Knepley   /* Create cones */
49549566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, 0, numCells + numVerticesAdj));
49559566063dSJacob Faibussowitsch   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, numCorners));
49569566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm));
49579566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCones(dm, &cones));
4958a47d0d45SMatthew G. Knepley   for (c = 0; c < numCells; ++c) {
4959a47d0d45SMatthew G. Knepley     for (p = 0; p < numCorners; ++p) {
4960a47d0d45SMatthew G. Knepley       const PetscInt gv = cells[c * numCorners + p];
4961a47d0d45SMatthew G. Knepley       PetscInt       lv;
4962a47d0d45SMatthew G. Knepley 
49639079aca8SVaclav Hapla       /* Positions within verticesAdj form 0-based local vertex numbering;
49649079aca8SVaclav Hapla          we need to shift it by numCells to get correct DAG points (cells go first) */
49659566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv));
496663a3b9bcSJacob Faibussowitsch       PetscCheck(lv >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not find global vertex %" PetscInt_FMT " in local connectivity", gv);
4967961cfab0SVaclav Hapla       cones[c * numCorners + p] = lv + numCells;
4968a47d0d45SMatthew G. Knepley     }
4969a47d0d45SMatthew G. Knepley   }
49702464107aSksagiyam   /* Build point sf */
49719566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(PetscObjectComm((PetscObject)dm), &layout));
49729566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetSize(layout, NVertices));
49739566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, numVertices));
49749566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
49759566063dSJacob Faibussowitsch   PetscCall(PetscSFCreateByMatchingIndices(layout, numVerticesAdj, verticesAdj, NULL, numCells, numVerticesAdj, verticesAdj, NULL, numCells, vertexSF, &sfPoint));
49769566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
49779566063dSJacob Faibussowitsch   if (!verticesAdjSaved) PetscCall(PetscFree(verticesAdj));
49789566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)sfPoint, "point SF"));
49792464107aSksagiyam   if (dm->sf) {
49802464107aSksagiyam     const char *prefix;
49812464107aSksagiyam 
49829566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm->sf, &prefix));
49839566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)sfPoint, prefix));
49842464107aSksagiyam   }
49859566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dm, sfPoint));
49869566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&sfPoint));
49879566063dSJacob Faibussowitsch   if (vertexSF) PetscCall(PetscObjectSetName((PetscObject)(*vertexSF), "Vertex Ownership SF"));
4988a47d0d45SMatthew G. Knepley   /* Fill in the rest of the topology structure */
49899566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
49909566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
49919566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
49923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4993a47d0d45SMatthew G. Knepley }
4994a47d0d45SMatthew G. Knepley 
4995b09969d6SVaclav Hapla /*@C
4996a1cb98faSBarry Smith   DMPlexBuildCoordinatesFromCellListParallel - Build `DM` coordinates from a list of coordinates for each owned vertex (common mesh generator output)
4997a1cb98faSBarry Smith 
499820f4b53cSBarry Smith   Collective; No Fortran Support
4999b09969d6SVaclav Hapla 
5000b09969d6SVaclav Hapla   Input Parameters:
5001a1cb98faSBarry Smith + dm           - The `DM`
5002b09969d6SVaclav Hapla . spaceDim     - The spatial dimension used for coordinates
5003a1cb98faSBarry Smith . sfVert       - `PetscSF` describing complete vertex ownership
5004b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
5005b09969d6SVaclav Hapla 
5006b09969d6SVaclav Hapla   Level: advanced
5007b09969d6SVaclav Hapla 
50081cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildFromCellListParallel()`
5009b09969d6SVaclav Hapla @*/
5010d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildCoordinatesFromCellListParallel(DM dm, PetscInt spaceDim, PetscSF sfVert, const PetscReal vertexCoords[])
5011d71ae5a4SJacob Faibussowitsch {
5012a47d0d45SMatthew G. Knepley   PetscSection coordSection;
5013a47d0d45SMatthew G. Knepley   Vec          coordinates;
5014a47d0d45SMatthew G. Knepley   PetscScalar *coords;
50151edcf0b2SVaclav Hapla   PetscInt     numVertices, numVerticesAdj, coordSize, v, vStart, vEnd;
5016a47d0d45SMatthew G. Knepley 
5017a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
50189566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
50199566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
50201dca8a05SBarry Smith   PetscCheck(vStart >= 0 && vEnd >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
50219566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, spaceDim));
50229566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfVert, &numVertices, &numVerticesAdj, NULL, NULL));
50231dca8a05SBarry 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);
50249566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
50259566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
50269566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, spaceDim));
50279566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, vStart, vEnd));
50281edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
50299566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, spaceDim));
50309566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, spaceDim));
5031a47d0d45SMatthew G. Knepley   }
50329566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
50339566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
50349566063dSJacob Faibussowitsch   PetscCall(VecCreate(PetscObjectComm((PetscObject)dm), &coordinates));
50359566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, spaceDim));
50369566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
50379566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
50389566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
50399566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
5040a47d0d45SMatthew G. Knepley   {
5041a47d0d45SMatthew G. Knepley     MPI_Datatype coordtype;
5042a47d0d45SMatthew G. Knepley 
5043a47d0d45SMatthew G. Knepley     /* Need a temp buffer for coords if we have complex/single */
50449566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_contiguous(spaceDim, MPIU_SCALAR, &coordtype));
50459566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_commit(&coordtype));
504621016a8bSBarry Smith #if defined(PETSC_USE_COMPLEX)
504721016a8bSBarry Smith     {
504821016a8bSBarry Smith       PetscScalar *svertexCoords;
504921016a8bSBarry Smith       PetscInt     i;
50509566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numVertices * spaceDim, &svertexCoords));
50513612f820SVaclav Hapla       for (i = 0; i < numVertices * spaceDim; i++) svertexCoords[i] = vertexCoords[i];
50529566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(sfVert, coordtype, svertexCoords, coords, MPI_REPLACE));
50539566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(sfVert, coordtype, svertexCoords, coords, MPI_REPLACE));
50549566063dSJacob Faibussowitsch       PetscCall(PetscFree(svertexCoords));
505521016a8bSBarry Smith     }
505621016a8bSBarry Smith #else
50579566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sfVert, coordtype, vertexCoords, coords, MPI_REPLACE));
50589566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sfVert, coordtype, vertexCoords, coords, MPI_REPLACE));
505921016a8bSBarry Smith #endif
50609566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_free(&coordtype));
5061a47d0d45SMatthew G. Knepley   }
50629566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
50639566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
50649566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
50659566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
50663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5067a47d0d45SMatthew G. Knepley }
5068a47d0d45SMatthew G. Knepley 
5069c3edce3dSSatish Balay /*@
5070a1cb98faSBarry Smith   DMPlexCreateFromCellListParallelPetsc - Create distributed `DMPLEX` from a list of vertices for each cell (common mesh generator output)
5071a1cb98faSBarry Smith 
5072a1cb98faSBarry Smith   Collective
5073a47d0d45SMatthew G. Knepley 
5074a47d0d45SMatthew G. Knepley   Input Parameters:
5075a47d0d45SMatthew G. Knepley + comm         - The communicator
5076a47d0d45SMatthew G. Knepley . dim          - The topological dimension of the mesh
5077a47d0d45SMatthew G. Knepley . numCells     - The number of cells owned by this process
5078a1cb98faSBarry Smith . numVertices  - The number of vertices owned by this process, or `PETSC_DECIDE`
5079a1cb98faSBarry Smith . NVertices    - The global number of vertices, or `PETSC_DECIDE`
5080a47d0d45SMatthew G. Knepley . numCorners   - The number of vertices for each cell
5081a47d0d45SMatthew G. Knepley . interpolate  - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
5082a47d0d45SMatthew G. Knepley . cells        - An array of numCells*numCorners numbers, the global vertex numbers for each cell
5083a47d0d45SMatthew G. Knepley . spaceDim     - The spatial dimension used for coordinates
5084a47d0d45SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
5085a47d0d45SMatthew G. Knepley 
5086d8d19677SJose E. Roman   Output Parameters:
5087a1cb98faSBarry Smith + dm          - The `DM`
5088a1cb98faSBarry Smith . vertexSF    - (Optional) `PetscSF` describing complete vertex ownership
508960225df5SJacob Faibussowitsch - verticesAdj - (Optional) vertex adjacency array
5090a47d0d45SMatthew G. Knepley 
5091b09969d6SVaclav Hapla   Level: intermediate
5092a47d0d45SMatthew G. Knepley 
5093a1cb98faSBarry Smith   Notes:
5094a1cb98faSBarry Smith   This function is just a convenient sequence of `DMCreate()`, `DMSetType()`, `DMSetDimension()`,
5095a1cb98faSBarry Smith   `DMPlexBuildFromCellListParallel()`, `DMPlexInterpolate()`, `DMPlexBuildCoordinatesFromCellListParallel()`
5096a1cb98faSBarry Smith 
5097a1cb98faSBarry Smith   See `DMPlexBuildFromCellListParallel()` for an example and details about the topology-related parameters.
5098a1cb98faSBarry Smith 
5099a1cb98faSBarry Smith   See `DMPlexBuildCoordinatesFromCellListParallel()` for details about the geometry-related parameters.
5100a1cb98faSBarry Smith 
51011cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()`
5102a47d0d45SMatthew G. Knepley @*/
5103d71ae5a4SJacob 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)
5104d71ae5a4SJacob Faibussowitsch {
5105a47d0d45SMatthew G. Knepley   PetscSF sfVert;
5106a47d0d45SMatthew G. Knepley 
5107a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
51089566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
51099566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
5110a47d0d45SMatthew G. Knepley   PetscValidLogicalCollectiveInt(*dm, dim, 2);
5111064a246eSJacob Faibussowitsch   PetscValidLogicalCollectiveInt(*dm, spaceDim, 9);
51129566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
51139566063dSJacob Faibussowitsch   PetscCall(DMPlexBuildFromCellListParallel(*dm, numCells, numVertices, NVertices, numCorners, cells, &sfVert, verticesAdj));
5114a47d0d45SMatthew G. Knepley   if (interpolate) {
51155fd9971aSMatthew G. Knepley     DM idm;
5116a47d0d45SMatthew G. Knepley 
51179566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
51189566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
5119a47d0d45SMatthew G. Knepley     *dm = idm;
5120a47d0d45SMatthew G. Knepley   }
51219566063dSJacob Faibussowitsch   PetscCall(DMPlexBuildCoordinatesFromCellListParallel(*dm, spaceDim, sfVert, vertexCoords));
512218d54ad4SMichael Lange   if (vertexSF) *vertexSF = sfVert;
51239566063dSJacob Faibussowitsch   else PetscCall(PetscSFDestroy(&sfVert));
51243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5125a47d0d45SMatthew G. Knepley }
5126a47d0d45SMatthew G. Knepley 
5127b09969d6SVaclav Hapla /*@C
5128a1cb98faSBarry Smith   DMPlexBuildFromCellList - Build `DMPLEX` topology from a list of vertices for each cell (common mesh generator output)
5129a1cb98faSBarry Smith 
513020f4b53cSBarry Smith   Collective; No Fortran Support
51319298eaa6SMatthew G Knepley 
51329298eaa6SMatthew G Knepley   Input Parameters:
5133a1cb98faSBarry Smith + dm          - The `DM`
5134b09969d6SVaclav Hapla . numCells    - The number of cells owned by this process
5135a1cb98faSBarry Smith . numVertices - The number of vertices owned by this process, or `PETSC_DETERMINE`
51369298eaa6SMatthew G Knepley . numCorners  - The number of vertices for each cell
51375e488331SVaclav Hapla - cells       - An array of numCells*numCorners numbers, the global vertex numbers for each cell
51389298eaa6SMatthew G Knepley 
5139b09969d6SVaclav Hapla   Level: advanced
51409298eaa6SMatthew G Knepley 
5141b09969d6SVaclav Hapla   Notes:
5142b09969d6SVaclav Hapla   Two triangles sharing a face
5143a1cb98faSBarry Smith .vb
51449298eaa6SMatthew G Knepley 
5145a1cb98faSBarry Smith         2
5146a1cb98faSBarry Smith       / | \
5147a1cb98faSBarry Smith      /  |  \
5148a1cb98faSBarry Smith     /   |   \
5149a1cb98faSBarry Smith    0  0 | 1  3
5150a1cb98faSBarry Smith     \   |   /
5151a1cb98faSBarry Smith      \  |  /
5152a1cb98faSBarry Smith       \ | /
5153a1cb98faSBarry Smith         1
5154a1cb98faSBarry Smith .ve
5155a1cb98faSBarry Smith   would have input
5156a1cb98faSBarry Smith .vb
5157a1cb98faSBarry Smith   numCells = 2, numVertices = 4
5158a1cb98faSBarry Smith   cells = [0 1 2  1 3 2]
5159a1cb98faSBarry Smith .ve
5160a1cb98faSBarry Smith   which would result in the `DMPLEX`
5161a1cb98faSBarry Smith .vb
5162a1cb98faSBarry Smith 
5163a1cb98faSBarry Smith         4
5164a1cb98faSBarry Smith       / | \
5165a1cb98faSBarry Smith      /  |  \
5166a1cb98faSBarry Smith     /   |   \
5167a1cb98faSBarry Smith    2  0 | 1  5
5168a1cb98faSBarry Smith     \   |   /
5169a1cb98faSBarry Smith      \  |  /
5170a1cb98faSBarry Smith       \ | /
5171a1cb98faSBarry Smith         3
5172a1cb98faSBarry Smith .ve
5173a1cb98faSBarry Smith 
5174a1cb98faSBarry Smith   If numVertices is `PETSC_DETERMINE`, it is computed by PETSc as the maximum vertex index in cells + 1.
517525b6865aSVaclav Hapla 
51761cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromCellListPetsc()`
5177b09969d6SVaclav Hapla @*/
5178d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildFromCellList(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const PetscInt cells[])
5179d71ae5a4SJacob Faibussowitsch {
5180961cfab0SVaclav Hapla   PetscInt *cones, c, p, dim;
5181b09969d6SVaclav Hapla 
5182b09969d6SVaclav Hapla   PetscFunctionBegin;
51839566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
51849566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
518525b6865aSVaclav Hapla   /* Get/check global number of vertices */
518625b6865aSVaclav Hapla   {
518725b6865aSVaclav Hapla     PetscInt       NVerticesInCells, i;
518825b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
518925b6865aSVaclav Hapla 
519025b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
519125b6865aSVaclav Hapla     NVerticesInCells = PETSC_MIN_INT;
51929371c9d4SSatish Balay     for (i = 0; i < len; i++)
51939371c9d4SSatish Balay       if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
519425b6865aSVaclav Hapla     ++NVerticesInCells;
519525b6865aSVaclav Hapla 
519625b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE) numVertices = NVerticesInCells;
51979371c9d4SSatish Balay     else
51989371c9d4SSatish 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);
519925b6865aSVaclav Hapla   }
52009566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
520148a46eb9SPierre Jolivet   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, numCorners));
52029566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm));
52039566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCones(dm, &cones));
5204b09969d6SVaclav Hapla   for (c = 0; c < numCells; ++c) {
5205ad540459SPierre Jolivet     for (p = 0; p < numCorners; ++p) cones[c * numCorners + p] = cells[c * numCorners + p] + numCells;
5206b09969d6SVaclav Hapla   }
52079566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
52089566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
52099566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
52103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5211b09969d6SVaclav Hapla }
5212b09969d6SVaclav Hapla 
5213b09969d6SVaclav Hapla /*@C
5214a1cb98faSBarry Smith   DMPlexBuildCoordinatesFromCellList - Build `DM` coordinates from a list of coordinates for each owned vertex (common mesh generator output)
5215a1cb98faSBarry Smith 
521620f4b53cSBarry Smith   Collective; No Fortran Support
5217b09969d6SVaclav Hapla 
5218b09969d6SVaclav Hapla   Input Parameters:
5219a1cb98faSBarry Smith + dm           - The `DM`
5220b09969d6SVaclav Hapla . spaceDim     - The spatial dimension used for coordinates
5221b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
5222b09969d6SVaclav Hapla 
5223b09969d6SVaclav Hapla   Level: advanced
5224b09969d6SVaclav Hapla 
52251cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellList()`
5226b09969d6SVaclav Hapla @*/
5227d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildCoordinatesFromCellList(DM dm, PetscInt spaceDim, const PetscReal vertexCoords[])
5228d71ae5a4SJacob Faibussowitsch {
5229b09969d6SVaclav Hapla   PetscSection coordSection;
5230b09969d6SVaclav Hapla   Vec          coordinates;
5231b09969d6SVaclav Hapla   DM           cdm;
5232b09969d6SVaclav Hapla   PetscScalar *coords;
52331edcf0b2SVaclav Hapla   PetscInt     v, vStart, vEnd, d;
5234b09969d6SVaclav Hapla 
5235b09969d6SVaclav Hapla   PetscFunctionBegin;
52369566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
52379566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
52381dca8a05SBarry Smith   PetscCheck(vStart >= 0 && vEnd >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
52399566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, spaceDim));
52409566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
52419566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
52429566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, spaceDim));
52439566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, vStart, vEnd));
52441edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
52459566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, spaceDim));
52469566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, spaceDim));
5247b09969d6SVaclav Hapla   }
52489566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
5249b09969d6SVaclav Hapla 
52509566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
52519566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(cdm, &coordinates));
52529566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, spaceDim));
52539566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
52549566063dSJacob Faibussowitsch   PetscCall(VecGetArrayWrite(coordinates, &coords));
52551edcf0b2SVaclav Hapla   for (v = 0; v < vEnd - vStart; ++v) {
5256ad540459SPierre Jolivet     for (d = 0; d < spaceDim; ++d) coords[v * spaceDim + d] = vertexCoords[v * spaceDim + d];
5257b09969d6SVaclav Hapla   }
52589566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayWrite(coordinates, &coords));
52599566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
52609566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
52619566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
52623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5263b09969d6SVaclav Hapla }
5264b09969d6SVaclav Hapla 
5265b09969d6SVaclav Hapla /*@
5266a1cb98faSBarry Smith   DMPlexCreateFromCellListPetsc - Create `DMPLEX` from a list of vertices for each cell (common mesh generator output), but only process 0 takes in the input
52673df08285SMatthew G. Knepley 
5268a1cb98faSBarry Smith   Collective
5269b09969d6SVaclav Hapla 
5270b09969d6SVaclav Hapla   Input Parameters:
5271b09969d6SVaclav Hapla + comm         - The communicator
5272b09969d6SVaclav Hapla . dim          - The topological dimension of the mesh
52733df08285SMatthew G. Knepley . numCells     - The number of cells, only on process 0
5274a1cb98faSBarry Smith . numVertices  - The number of vertices owned by this process, or `PETSC_DECIDE`, only on process 0
52753df08285SMatthew G. Knepley . numCorners   - The number of vertices for each cell, only on process 0
5276b09969d6SVaclav Hapla . interpolate  - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
52773df08285SMatthew G. Knepley . cells        - An array of numCells*numCorners numbers, the vertices for each cell, only on process 0
5278b09969d6SVaclav Hapla . spaceDim     - The spatial dimension used for coordinates
52793df08285SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex, only on process 0
5280b09969d6SVaclav Hapla 
5281b09969d6SVaclav Hapla   Output Parameter:
5282a1cb98faSBarry Smith . dm - The `DM`, which only has points on process 0
528325b6865aSVaclav Hapla 
5284b09969d6SVaclav Hapla   Level: intermediate
5285b09969d6SVaclav Hapla 
5286a1cb98faSBarry Smith   Notes:
5287a1cb98faSBarry Smith   This function is just a convenient sequence of `DMCreate()`, `DMSetType()`, `DMSetDimension()`, `DMPlexBuildFromCellList()`,
5288a1cb98faSBarry Smith   `DMPlexInterpolate()`, `DMPlexBuildCoordinatesFromCellList()`
5289a1cb98faSBarry Smith 
5290a1cb98faSBarry Smith   See `DMPlexBuildFromCellList()` for an example and details about the topology-related parameters.
5291a1cb98faSBarry Smith   See `DMPlexBuildCoordinatesFromCellList()` for details about the geometry-related parameters.
5292a1cb98faSBarry Smith   See `DMPlexCreateFromCellListParallelPetsc()` for parallel input
5293a1cb98faSBarry Smith 
52941cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildFromCellList()`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()`
52959298eaa6SMatthew G Knepley @*/
5296d71ae5a4SJacob 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)
5297d71ae5a4SJacob Faibussowitsch {
52983df08285SMatthew G. Knepley   PetscMPIInt rank;
52999298eaa6SMatthew G Knepley 
53009298eaa6SMatthew G Knepley   PetscFunctionBegin;
530128b400f6SJacob 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.");
53029566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
53039566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
53049566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
53059566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
5306c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildFromCellList(*dm, numCells, numVertices, numCorners, cells));
53079566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildFromCellList(*dm, 0, 0, 0, NULL));
53089298eaa6SMatthew G Knepley   if (interpolate) {
53095fd9971aSMatthew G. Knepley     DM idm;
53109298eaa6SMatthew G Knepley 
53119566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
53129566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
53139298eaa6SMatthew G Knepley     *dm = idm;
53149298eaa6SMatthew G Knepley   }
5315c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, vertexCoords));
53169566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, NULL));
53173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
53189298eaa6SMatthew G Knepley }
53199298eaa6SMatthew G Knepley 
5320939f6067SMatthew G. Knepley /*@
532120f4b53cSBarry Smith   DMPlexCreateFromDAG - This takes as input the adjacency-list representation of the Directed Acyclic Graph (Hasse Diagram) encoding a mesh, and produces a `DM`
5322939f6067SMatthew G. Knepley 
5323939f6067SMatthew G. Knepley   Input Parameters:
532420f4b53cSBarry Smith + dm               - The empty `DM` object, usually from `DMCreate()` and `DMSetDimension()`
5325939f6067SMatthew G. Knepley . depth            - The depth of the DAG
532620f4b53cSBarry Smith . numPoints        - Array of size depth + 1 containing the number of points at each `depth`
5327939f6067SMatthew G. Knepley . coneSize         - The cone size of each point
5328939f6067SMatthew G. Knepley . cones            - The concatenation of the cone points for each point, the cone list must be oriented correctly for each point
5329939f6067SMatthew G. Knepley . coneOrientations - The orientation of each cone point
533020f4b53cSBarry Smith - vertexCoords     - An array of `numPoints`[0]*spacedim numbers representing the coordinates of each vertex, with spacedim the value set via `DMSetCoordinateDim()`
5331939f6067SMatthew G. Knepley 
5332939f6067SMatthew G. Knepley   Output Parameter:
533320f4b53cSBarry Smith . dm - The `DM`
533420f4b53cSBarry Smith 
533520f4b53cSBarry Smith   Level: advanced
5336939f6067SMatthew G. Knepley 
5337a1cb98faSBarry Smith   Note:
5338a1cb98faSBarry Smith   Two triangles sharing a face would have input
5339a1cb98faSBarry Smith .vb
5340a1cb98faSBarry Smith   depth = 1, numPoints = [4 2], coneSize = [3 3 0 0 0 0]
5341a1cb98faSBarry Smith   cones = [2 3 4  3 5 4], coneOrientations = [0 0 0  0 0 0]
5342a1cb98faSBarry Smith  vertexCoords = [-1.0 0.0  0.0 -1.0  0.0 1.0  1.0 0.0]
5343a1cb98faSBarry Smith .ve
5344939f6067SMatthew G. Knepley   which would result in the DMPlex
5345a1cb98faSBarry Smith .vb
5346a1cb98faSBarry Smith         4
5347a1cb98faSBarry Smith       / | \
5348a1cb98faSBarry Smith      /  |  \
5349a1cb98faSBarry Smith     /   |   \
5350a1cb98faSBarry Smith    2  0 | 1  5
5351a1cb98faSBarry Smith     \   |   /
5352a1cb98faSBarry Smith      \  |  /
5353a1cb98faSBarry Smith       \ | /
5354a1cb98faSBarry Smith         3
5355a1cb98faSBarry Smith .ve
5356a1cb98faSBarry Smith   Notice that all points are numbered consecutively, unlike `DMPlexCreateFromCellListPetsc()`
5357939f6067SMatthew G. Knepley 
53581cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`
5359939f6067SMatthew G. Knepley @*/
5360d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateFromDAG(DM dm, PetscInt depth, const PetscInt numPoints[], const PetscInt coneSize[], const PetscInt cones[], const PetscInt coneOrientations[], const PetscScalar vertexCoords[])
5361d71ae5a4SJacob Faibussowitsch {
53629298eaa6SMatthew G Knepley   Vec          coordinates;
53639298eaa6SMatthew G Knepley   PetscSection coordSection;
53649298eaa6SMatthew G Knepley   PetscScalar *coords;
5365811e8653SToby Isaac   PetscInt     coordSize, firstVertex = -1, pStart = 0, pEnd = 0, p, v, dim, dimEmbed, d, off;
53669298eaa6SMatthew G Knepley 
53679298eaa6SMatthew G Knepley   PetscFunctionBegin;
53689566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
53699566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dimEmbed));
537063a3b9bcSJacob Faibussowitsch   PetscCheck(dimEmbed >= dim, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Embedding dimension %" PetscInt_FMT " cannot be less than intrinsic dimension %" PetscInt_FMT, dimEmbed, dim);
53719298eaa6SMatthew G Knepley   for (d = 0; d <= depth; ++d) pEnd += numPoints[d];
53729566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, pStart, pEnd));
53739298eaa6SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
53749566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeSize(dm, p, coneSize[p - pStart]));
5375ad540459SPierre Jolivet     if (firstVertex < 0 && !coneSize[p - pStart]) firstVertex = p - pStart;
537697e052ccSToby Isaac   }
53771dca8a05SBarry Smith   PetscCheck(firstVertex >= 0 || !numPoints[0], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Expected %" PetscInt_FMT " vertices but could not find any", numPoints[0]);
53789566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm)); /* Allocate space for cones */
53799298eaa6SMatthew G Knepley   for (p = pStart, off = 0; p < pEnd; off += coneSize[p - pStart], ++p) {
53809566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(dm, p, &cones[off]));
53819566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeOrientation(dm, p, &coneOrientations[off]));
53829298eaa6SMatthew G Knepley   }
53839566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
53849566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
53859298eaa6SMatthew G Knepley   /* Build coordinates */
53869566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
53879566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
53889566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dimEmbed));
53899566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numPoints[0]));
53909298eaa6SMatthew G Knepley   for (v = firstVertex; v < firstVertex + numPoints[0]; ++v) {
53919566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, dimEmbed));
53929566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dimEmbed));
53939298eaa6SMatthew G Knepley   }
53949566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
53959566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
53969566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
53979566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
53989566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
53999566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, dimEmbed));
54009566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
54019318fe57SMatthew G. Knepley   if (vertexCoords) {
54029566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
54039298eaa6SMatthew G Knepley     for (v = 0; v < numPoints[0]; ++v) {
54049298eaa6SMatthew G Knepley       PetscInt off;
54059298eaa6SMatthew G Knepley 
54069566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, v + firstVertex, &off));
5407ad540459SPierre Jolivet       for (d = 0; d < dimEmbed; ++d) coords[off + d] = vertexCoords[v * dimEmbed + d];
54089298eaa6SMatthew G Knepley     }
54099318fe57SMatthew G. Knepley   }
54109566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
54119566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
54129566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
54133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
54149298eaa6SMatthew G Knepley }
54158415267dSToby Isaac 
5416a4e35b19SJacob Faibussowitsch /*
5417a1cb98faSBarry Smith   DMPlexCreateCellVertexFromFile - Create a `DMPLEX` mesh from a simple cell-vertex file.
5418a1cb98faSBarry Smith 
5419a1cb98faSBarry Smith   Collective
54208ca92349SMatthew G. Knepley 
54218ca92349SMatthew G. Knepley + comm        - The MPI communicator
54228ca92349SMatthew G. Knepley . filename    - Name of the .dat file
54238ca92349SMatthew G. Knepley - interpolate - Create faces and edges in the mesh
54248ca92349SMatthew G. Knepley 
54258ca92349SMatthew G. Knepley   Output Parameter:
5426a1cb98faSBarry Smith . dm  - The `DM` object representing the mesh
54278ca92349SMatthew G. Knepley 
54288ca92349SMatthew G. Knepley   Level: beginner
54298ca92349SMatthew G. Knepley 
5430a1cb98faSBarry Smith   Note:
5431a1cb98faSBarry Smith   The format is the simplest possible:
5432a1cb98faSBarry Smith .vb
5433a1cb98faSBarry Smith   Ne
5434a1cb98faSBarry Smith   v0 v1 ... vk
5435a1cb98faSBarry Smith   Nv
5436a1cb98faSBarry Smith   x y z marker
5437a1cb98faSBarry Smith .ve
5438a1cb98faSBarry Smith 
5439a1cb98faSBarry Smith   Developer Note:
5440a1cb98faSBarry Smith   Should use a `PetscViewer` not a filename
5441a1cb98faSBarry Smith 
54426afe31f6SMartin Diehl .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromFile()`, `DMPlexCreateGmsh()`, `DMPlexCreate()`
5443a4e35b19SJacob Faibussowitsch */
5444ff6a9541SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCellVertexFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
5445d71ae5a4SJacob Faibussowitsch {
54468ca92349SMatthew G. Knepley   DMLabel      marker;
54478ca92349SMatthew G. Knepley   PetscViewer  viewer;
54488ca92349SMatthew G. Knepley   Vec          coordinates;
54498ca92349SMatthew G. Knepley   PetscSection coordSection;
54508ca92349SMatthew G. Knepley   PetscScalar *coords;
54518ca92349SMatthew G. Knepley   char         line[PETSC_MAX_PATH_LEN];
54528ca92349SMatthew G. Knepley   PetscInt     dim = 3, cdim = 3, coordSize, v, c, d;
54538ca92349SMatthew G. Knepley   PetscMPIInt  rank;
5454f8d5e320SMatthew G. Knepley   int          snum, Nv, Nc, Ncn, Nl;
54558ca92349SMatthew G. Knepley 
54568ca92349SMatthew G. Knepley   PetscFunctionBegin;
54579566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
54589566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, &viewer));
54599566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(viewer, PETSCVIEWERASCII));
54609566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetMode(viewer, FILE_MODE_READ));
54619566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetName(viewer, filename));
5462dd400576SPatrick Sanan   if (rank == 0) {
54639566063dSJacob Faibussowitsch     PetscCall(PetscViewerRead(viewer, line, 4, NULL, PETSC_STRING));
5464f8d5e320SMatthew G. Knepley     snum = sscanf(line, "%d %d %d %d", &Nc, &Nv, &Ncn, &Nl);
546508401ef6SPierre Jolivet     PetscCheck(snum == 4, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
546625ce1634SJed Brown   } else {
5467f8d5e320SMatthew G. Knepley     Nc = Nv = Ncn = Nl = 0;
54688ca92349SMatthew G. Knepley   }
54699566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
54709566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
54719566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(*dm, 0, Nc + Nv));
54729566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
54739566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*dm, cdim));
54748ca92349SMatthew G. Knepley   /* Read topology */
5475dd400576SPatrick Sanan   if (rank == 0) {
5476f8d5e320SMatthew G. Knepley     char     format[PETSC_MAX_PATH_LEN];
5477f8d5e320SMatthew G. Knepley     PetscInt cone[8];
54788ca92349SMatthew G. Knepley     int      vbuf[8], v;
54798ca92349SMatthew G. Knepley 
54809371c9d4SSatish Balay     for (c = 0; c < Ncn; ++c) {
54819371c9d4SSatish Balay       format[c * 3 + 0] = '%';
54829371c9d4SSatish Balay       format[c * 3 + 1] = 'd';
54839371c9d4SSatish Balay       format[c * 3 + 2] = ' ';
54849371c9d4SSatish Balay     }
5485f8d5e320SMatthew G. Knepley     format[Ncn * 3 - 1] = '\0';
54869566063dSJacob Faibussowitsch     for (c = 0; c < Nc; ++c) PetscCall(DMPlexSetConeSize(*dm, c, Ncn));
54879566063dSJacob Faibussowitsch     PetscCall(DMSetUp(*dm));
54888ca92349SMatthew G. Knepley     for (c = 0; c < Nc; ++c) {
54899566063dSJacob Faibussowitsch       PetscCall(PetscViewerRead(viewer, line, Ncn, NULL, PETSC_STRING));
5490f8d5e320SMatthew G. Knepley       switch (Ncn) {
5491d71ae5a4SJacob Faibussowitsch       case 2:
5492d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1]);
5493d71ae5a4SJacob Faibussowitsch         break;
5494d71ae5a4SJacob Faibussowitsch       case 3:
5495d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2]);
5496d71ae5a4SJacob Faibussowitsch         break;
5497d71ae5a4SJacob Faibussowitsch       case 4:
5498d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3]);
5499d71ae5a4SJacob Faibussowitsch         break;
5500d71ae5a4SJacob Faibussowitsch       case 6:
5501d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5]);
5502d71ae5a4SJacob Faibussowitsch         break;
5503d71ae5a4SJacob Faibussowitsch       case 8:
5504d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5], &vbuf[6], &vbuf[7]);
5505d71ae5a4SJacob Faibussowitsch         break;
5506d71ae5a4SJacob Faibussowitsch       default:
5507d71ae5a4SJacob Faibussowitsch         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No cell shape with %d vertices", Ncn);
5508f8d5e320SMatthew G. Knepley       }
550908401ef6SPierre Jolivet       PetscCheck(snum == Ncn, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
5510f8d5e320SMatthew G. Knepley       for (v = 0; v < Ncn; ++v) cone[v] = vbuf[v] + Nc;
55118ca92349SMatthew G. Knepley       /* Hexahedra are inverted */
5512f8d5e320SMatthew G. Knepley       if (Ncn == 8) {
55138ca92349SMatthew G. Knepley         PetscInt tmp = cone[1];
55148ca92349SMatthew G. Knepley         cone[1]      = cone[3];
55158ca92349SMatthew G. Knepley         cone[3]      = tmp;
55168ca92349SMatthew G. Knepley       }
55179566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(*dm, c, cone));
55188ca92349SMatthew G. Knepley     }
55198ca92349SMatthew G. Knepley   }
55209566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(*dm));
55219566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(*dm));
55228ca92349SMatthew G. Knepley   /* Read coordinates */
55239566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(*dm, &coordSection));
55249566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
55259566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, cdim));
55269566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, Nc, Nc + Nv));
55278ca92349SMatthew G. Knepley   for (v = Nc; v < Nc + Nv; ++v) {
55289566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, cdim));
55299566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, cdim));
55308ca92349SMatthew G. Knepley   }
55319566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
55329566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
55339566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
55349566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
55359566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
55369566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, cdim));
55379566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
55389566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
5539dd400576SPatrick Sanan   if (rank == 0) {
5540f8d5e320SMatthew G. Knepley     char   format[PETSC_MAX_PATH_LEN];
55418ca92349SMatthew G. Knepley     double x[3];
5542f8d5e320SMatthew G. Knepley     int    l, val[3];
55438ca92349SMatthew G. Knepley 
5544f8d5e320SMatthew G. Knepley     if (Nl) {
55459371c9d4SSatish Balay       for (l = 0; l < Nl; ++l) {
55469371c9d4SSatish Balay         format[l * 3 + 0] = '%';
55479371c9d4SSatish Balay         format[l * 3 + 1] = 'd';
55489371c9d4SSatish Balay         format[l * 3 + 2] = ' ';
55499371c9d4SSatish Balay       }
5550f8d5e320SMatthew G. Knepley       format[Nl * 3 - 1] = '\0';
55519566063dSJacob Faibussowitsch       PetscCall(DMCreateLabel(*dm, "marker"));
55529566063dSJacob Faibussowitsch       PetscCall(DMGetLabel(*dm, "marker", &marker));
5553f8d5e320SMatthew G. Knepley     }
55548ca92349SMatthew G. Knepley     for (v = 0; v < Nv; ++v) {
55559566063dSJacob Faibussowitsch       PetscCall(PetscViewerRead(viewer, line, 3 + Nl, NULL, PETSC_STRING));
5556f8d5e320SMatthew G. Knepley       snum = sscanf(line, "%lg %lg %lg", &x[0], &x[1], &x[2]);
555708401ef6SPierre Jolivet       PetscCheck(snum == 3, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
5558f8d5e320SMatthew G. Knepley       switch (Nl) {
5559d71ae5a4SJacob Faibussowitsch       case 0:
5560d71ae5a4SJacob Faibussowitsch         snum = 0;
5561d71ae5a4SJacob Faibussowitsch         break;
5562d71ae5a4SJacob Faibussowitsch       case 1:
5563d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &val[0]);
5564d71ae5a4SJacob Faibussowitsch         break;
5565d71ae5a4SJacob Faibussowitsch       case 2:
5566d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &val[0], &val[1]);
5567d71ae5a4SJacob Faibussowitsch         break;
5568d71ae5a4SJacob Faibussowitsch       case 3:
5569d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &val[0], &val[1], &val[2]);
5570d71ae5a4SJacob Faibussowitsch         break;
5571d71ae5a4SJacob Faibussowitsch       default:
5572d71ae5a4SJacob Faibussowitsch         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Request support for %d labels", Nl);
5573f8d5e320SMatthew G. Knepley       }
557408401ef6SPierre Jolivet       PetscCheck(snum == Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
55758ca92349SMatthew G. Knepley       for (d = 0; d < cdim; ++d) coords[v * cdim + d] = x[d];
55769566063dSJacob Faibussowitsch       for (l = 0; l < Nl; ++l) PetscCall(DMLabelSetValue(marker, v + Nc, val[l]));
55778ca92349SMatthew G. Knepley     }
55788ca92349SMatthew G. Knepley   }
55799566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
55809566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(*dm, coordinates));
55819566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
55829566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(&viewer));
55838ca92349SMatthew G. Knepley   if (interpolate) {
55848ca92349SMatthew G. Knepley     DM      idm;
55858ca92349SMatthew G. Knepley     DMLabel bdlabel;
55868ca92349SMatthew G. Knepley 
55879566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
55889566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
55898ca92349SMatthew G. Knepley     *dm = idm;
55908ca92349SMatthew G. Knepley 
5591f8d5e320SMatthew G. Knepley     if (!Nl) {
55929566063dSJacob Faibussowitsch       PetscCall(DMCreateLabel(*dm, "marker"));
55939566063dSJacob Faibussowitsch       PetscCall(DMGetLabel(*dm, "marker", &bdlabel));
55949566063dSJacob Faibussowitsch       PetscCall(DMPlexMarkBoundaryFaces(*dm, PETSC_DETERMINE, bdlabel));
55959566063dSJacob Faibussowitsch       PetscCall(DMPlexLabelComplete(*dm, bdlabel));
55968ca92349SMatthew G. Knepley     }
5597f8d5e320SMatthew G. Knepley   }
55983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
55998ca92349SMatthew G. Knepley }
56008ca92349SMatthew G. Knepley 
56018ca92349SMatthew G. Knepley /*@C
5602a1cb98faSBarry Smith   DMPlexCreateFromFile - This takes a filename and produces a `DM`
5603a1cb98faSBarry Smith 
5604a1cb98faSBarry Smith   Collective
5605ca522641SMatthew G. Knepley 
5606ca522641SMatthew G. Knepley   Input Parameters:
5607ca522641SMatthew G. Knepley + comm        - The communicator
5608ca522641SMatthew G. Knepley . filename    - A file name
5609a1cb98faSBarry Smith . plexname    - The object name of the resulting `DM`, also used for intra-datafile lookup by some formats
5610ca522641SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
5611ca522641SMatthew G. Knepley 
5612ca522641SMatthew G. Knepley   Output Parameter:
5613a1cb98faSBarry Smith . dm - The `DM`
5614ca522641SMatthew G. Knepley 
5615a1cb98faSBarry Smith   Options Database Key:
5616a1cb98faSBarry Smith . -dm_plex_create_from_hdf5_xdmf - use the `PETSC_VIEWER_HDF5_XDMF` format for reading HDF5
561702ef0d99SVaclav Hapla 
561837fdd005SBarry Smith   Use `-dm_plex_create_ prefix` to pass options to the internal `PetscViewer`, e.g.
5619bca97951SVaclav Hapla $ -dm_plex_create_viewer_hdf5_collective
5620bca97951SVaclav Hapla 
5621ca522641SMatthew G. Knepley   Level: beginner
5622ca522641SMatthew G. Knepley 
5623a1cb98faSBarry Smith   Notes:
5624a1cb98faSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
5625a1cb98faSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
5626a1cb98faSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
5627a1cb98faSBarry Smith   The input parameter name is thus used to name the `DMPLEX` object when `DMPlexCreateFromFile()` internally
5628a1cb98faSBarry Smith   calls `DMLoad()`. Currently, name is ignored for other viewer types and/or formats.
5629a1cb98faSBarry Smith 
56301cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromDAG()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`, `PetscObjectSetName()`, `DMView()`, `DMLoad()`
5631ca522641SMatthew G. Knepley @*/
5632d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateFromFile(MPI_Comm comm, const char filename[], const char plexname[], PetscBool interpolate, DM *dm)
5633d71ae5a4SJacob Faibussowitsch {
5634ef3a5affSJacob Faibussowitsch   const char  extGmsh[]      = ".msh";
5635ef3a5affSJacob Faibussowitsch   const char  extGmsh2[]     = ".msh2";
5636ef3a5affSJacob Faibussowitsch   const char  extGmsh4[]     = ".msh4";
5637ef3a5affSJacob Faibussowitsch   const char  extCGNS[]      = ".cgns";
5638ef3a5affSJacob Faibussowitsch   const char  extExodus[]    = ".exo";
5639ef3a5affSJacob Faibussowitsch   const char  extExodus_e[]  = ".e";
5640ef3a5affSJacob Faibussowitsch   const char  extGenesis[]   = ".gen";
5641ef3a5affSJacob Faibussowitsch   const char  extFluent[]    = ".cas";
5642ef3a5affSJacob Faibussowitsch   const char  extHDF5[]      = ".h5";
56436f2c871aSStefano Zampini   const char  extXDMFHDF5[]  = ".xdmf.h5";
5644ef3a5affSJacob Faibussowitsch   const char  extPLY[]       = ".ply";
5645ef3a5affSJacob Faibussowitsch   const char  extEGADSLite[] = ".egadslite";
5646ef3a5affSJacob Faibussowitsch   const char  extEGADS[]     = ".egads";
5647ef3a5affSJacob Faibussowitsch   const char  extIGES[]      = ".igs";
5648ef3a5affSJacob Faibussowitsch   const char  extSTEP[]      = ".stp";
5649ef3a5affSJacob Faibussowitsch   const char  extCV[]        = ".dat";
5650ca522641SMatthew G. Knepley   size_t      len;
56516afe31f6SMartin Diehl   PetscBool   isGmsh, isGmsh2, isGmsh4, isCGNS, isExodus, isGenesis, isFluent, isHDF5, isPLY, isEGADSLite, isEGADS, isIGES, isSTEP, isCV, isXDMFHDF5;
5652ca522641SMatthew G. Knepley   PetscMPIInt rank;
5653ca522641SMatthew G. Knepley 
5654ca522641SMatthew G. Knepley   PetscFunctionBegin;
56554f572ea9SToby Isaac   PetscAssertPointer(filename, 2);
56564f572ea9SToby Isaac   if (plexname) PetscAssertPointer(plexname, 3);
56574f572ea9SToby Isaac   PetscAssertPointer(dm, 5);
56589566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
56599566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_CreateFromFile, 0, 0, 0, 0));
56609566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
56619566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(filename, &len));
566228b400f6SJacob Faibussowitsch   PetscCheck(len, comm, PETSC_ERR_ARG_WRONG, "Filename must be a valid path");
5663ef3a5affSJacob Faibussowitsch 
56649371c9d4SSatish Balay #define CheckExtension(extension__, is_extension__) \
56659371c9d4SSatish Balay   do { \
5666274aaeaaSJacob Faibussowitsch     PetscAssert(sizeof(extension__), comm, PETSC_ERR_PLIB, "Zero-size extension: %s", extension__); \
5667274aaeaaSJacob Faibussowitsch     /* don't count the null-terminator at the end */ \
5668274aaeaaSJacob Faibussowitsch     const size_t ext_len = sizeof(extension__) - 1; \
5669274aaeaaSJacob Faibussowitsch     if (len < ext_len) { \
5670ef3a5affSJacob Faibussowitsch       is_extension__ = PETSC_FALSE; \
5671ef3a5affSJacob Faibussowitsch     } else { \
5672274aaeaaSJacob Faibussowitsch       PetscCall(PetscStrncmp(filename + len - ext_len, extension__, ext_len, &is_extension__)); \
5673ef3a5affSJacob Faibussowitsch     } \
5674ef3a5affSJacob Faibussowitsch   } while (0)
5675ef3a5affSJacob Faibussowitsch 
5676ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh, isGmsh);
5677ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh2, isGmsh2);
5678ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh4, isGmsh4);
5679ef3a5affSJacob Faibussowitsch   CheckExtension(extCGNS, isCGNS);
5680ef3a5affSJacob Faibussowitsch   CheckExtension(extExodus, isExodus);
5681ef3a5affSJacob Faibussowitsch   if (!isExodus) CheckExtension(extExodus_e, isExodus);
5682ef3a5affSJacob Faibussowitsch   CheckExtension(extGenesis, isGenesis);
5683ef3a5affSJacob Faibussowitsch   CheckExtension(extFluent, isFluent);
5684ef3a5affSJacob Faibussowitsch   CheckExtension(extHDF5, isHDF5);
5685ef3a5affSJacob Faibussowitsch   CheckExtension(extPLY, isPLY);
5686ef3a5affSJacob Faibussowitsch   CheckExtension(extEGADSLite, isEGADSLite);
5687ef3a5affSJacob Faibussowitsch   CheckExtension(extEGADS, isEGADS);
5688ef3a5affSJacob Faibussowitsch   CheckExtension(extIGES, isIGES);
5689ef3a5affSJacob Faibussowitsch   CheckExtension(extSTEP, isSTEP);
5690ef3a5affSJacob Faibussowitsch   CheckExtension(extCV, isCV);
56916f2c871aSStefano Zampini   CheckExtension(extXDMFHDF5, isXDMFHDF5);
5692ef3a5affSJacob Faibussowitsch 
5693ef3a5affSJacob Faibussowitsch #undef CheckExtension
5694ef3a5affSJacob Faibussowitsch 
5695de78e4feSLisandro Dalcin   if (isGmsh || isGmsh2 || isGmsh4) {
56969566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateGmshFromFile(comm, filename, interpolate, dm));
5697ca522641SMatthew G. Knepley   } else if (isCGNS) {
56989566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCGNSFromFile(comm, filename, interpolate, dm));
569990c68965SMatthew G. Knepley   } else if (isExodus || isGenesis) {
57009566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateExodusFromFile(comm, filename, interpolate, dm));
57012f0bd6dcSMichael Lange   } else if (isFluent) {
57029566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFluentFromFile(comm, filename, interpolate, dm));
5703cc2f8f65SMatthew G. Knepley   } else if (isHDF5) {
5704cc2f8f65SMatthew G. Knepley     PetscViewer viewer;
5705cc2f8f65SMatthew G. Knepley 
570643b242b4SVaclav 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 */
57076f2c871aSStefano Zampini     PetscCall(PetscOptionsGetBool(NULL, NULL, "-dm_plex_create_from_hdf5_xdmf", &isXDMFHDF5, NULL));
57089566063dSJacob Faibussowitsch     PetscCall(PetscViewerCreate(comm, &viewer));
57099566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetType(viewer, PETSCVIEWERHDF5));
57109566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetOptionsPrefix(viewer, "dm_plex_create_"));
57119566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetFromOptions(viewer));
57129566063dSJacob Faibussowitsch     PetscCall(PetscViewerFileSetMode(viewer, FILE_MODE_READ));
57139566063dSJacob Faibussowitsch     PetscCall(PetscViewerFileSetName(viewer, filename));
5714cd7e8a5eSksagiyam 
57159566063dSJacob Faibussowitsch     PetscCall(DMCreate(comm, dm));
57169566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)(*dm), plexname));
57179566063dSJacob Faibussowitsch     PetscCall(DMSetType(*dm, DMPLEX));
57186f2c871aSStefano Zampini     if (isXDMFHDF5) PetscCall(PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_XDMF));
57199566063dSJacob Faibussowitsch     PetscCall(DMLoad(*dm, viewer));
57206f2c871aSStefano Zampini     if (isXDMFHDF5) PetscCall(PetscViewerPopFormat(viewer));
57219566063dSJacob Faibussowitsch     PetscCall(PetscViewerDestroy(&viewer));
57225fd9971aSMatthew G. Knepley 
57235fd9971aSMatthew G. Knepley     if (interpolate) {
57245fd9971aSMatthew G. Knepley       DM idm;
57255fd9971aSMatthew G. Knepley 
57269566063dSJacob Faibussowitsch       PetscCall(DMPlexInterpolate(*dm, &idm));
57279566063dSJacob Faibussowitsch       PetscCall(DMDestroy(dm));
57285fd9971aSMatthew G. Knepley       *dm = idm;
57295fd9971aSMatthew G. Knepley     }
5730f2801cd6SMatthew G. Knepley   } else if (isPLY) {
57319566063dSJacob Faibussowitsch     PetscCall(DMPlexCreatePLYFromFile(comm, filename, interpolate, dm));
5732c1cad2e7SMatthew G. Knepley   } else if (isEGADSLite || isEGADS || isIGES || isSTEP) {
57339566063dSJacob Faibussowitsch     if (isEGADSLite) PetscCall(DMPlexCreateEGADSLiteFromFile(comm, filename, dm));
57349566063dSJacob Faibussowitsch     else PetscCall(DMPlexCreateEGADSFromFile(comm, filename, dm));
57357bee2925SMatthew Knepley     if (!interpolate) {
57367bee2925SMatthew Knepley       DM udm;
57377bee2925SMatthew Knepley 
57389566063dSJacob Faibussowitsch       PetscCall(DMPlexUninterpolate(*dm, &udm));
57399566063dSJacob Faibussowitsch       PetscCall(DMDestroy(dm));
57407bee2925SMatthew Knepley       *dm = udm;
57417bee2925SMatthew Knepley     }
57428ca92349SMatthew G. Knepley   } else if (isCV) {
57439566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCellVertexFromFile(comm, filename, interpolate, dm));
574498921bdaSJacob Faibussowitsch   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot load file %s: unrecognized extension", filename);
57459566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(plexname, &len));
57469566063dSJacob Faibussowitsch   if (len) PetscCall(PetscObjectSetName((PetscObject)(*dm), plexname));
57479566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_CreateFromFile, 0, 0, 0, 0));
57483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5749ca522641SMatthew G. Knepley }
575012b8a6daSStefano Zampini 
57519f6c5813SMatthew G. Knepley /*@C
57529f6c5813SMatthew 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.
57539f6c5813SMatthew G. Knepley 
57540528010dSStefano Zampini   Input Parameters:
57550528010dSStefano Zampini + tr     - The `DMPlexTransform`
57560528010dSStefano Zampini - prefix - An options prefix, or NULL
57579f6c5813SMatthew G. Knepley 
57589f6c5813SMatthew G. Knepley   Output Parameter:
57599f6c5813SMatthew G. Knepley . dm - The `DM`
57609f6c5813SMatthew G. Knepley 
57619f6c5813SMatthew G. Knepley   Level: beginner
57629f6c5813SMatthew G. Knepley 
576320f4b53cSBarry Smith   Notes:
576420f4b53cSBarry 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.
576520f4b53cSBarry Smith 
57669f6c5813SMatthew G. Knepley .seealso: `DMPlexCreateFromFile`, `DMPlexCreateFromDAG()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`
57679f6c5813SMatthew G. Knepley @*/
57680528010dSStefano Zampini PetscErrorCode DMPlexCreateEphemeral(DMPlexTransform tr, const char prefix[], DM *dm)
57699f6c5813SMatthew G. Knepley {
57700528010dSStefano Zampini   DM           bdm, bcdm, cdm;
57710528010dSStefano Zampini   Vec          coordinates, coordinatesNew;
57720528010dSStefano Zampini   PetscSection cs;
57730528010dSStefano Zampini   PetscInt     dim, cdim, Nl;
57749f6c5813SMatthew G. Knepley 
57759f6c5813SMatthew G. Knepley   PetscFunctionBegin;
57769f6c5813SMatthew G. Knepley   PetscCall(DMCreate(PetscObjectComm((PetscObject)tr), dm));
57779f6c5813SMatthew G. Knepley   PetscCall(DMSetType(*dm, DMPLEX));
57780528010dSStefano Zampini   ((DM_Plex *)(*dm)->data)->interpolated = DMPLEX_INTERPOLATED_FULL;
57790528010dSStefano Zampini   // Handle coordinates
57800528010dSStefano Zampini   PetscCall(DMPlexTransformGetDM(tr, &bdm));
57810528010dSStefano Zampini   PetscCall(DMGetCoordinateDim(bdm, &cdim));
57820528010dSStefano Zampini   PetscCall(DMSetCoordinateDim(*dm, cdim));
57830528010dSStefano Zampini   PetscCall(DMGetDimension(bdm, &dim));
57840528010dSStefano Zampini   PetscCall(DMSetDimension(*dm, dim));
57850528010dSStefano Zampini   PetscCall(DMGetCoordinateDM(bdm, &bcdm));
57860528010dSStefano Zampini   PetscCall(DMGetCoordinateDM(*dm, &cdm));
57870528010dSStefano Zampini   PetscCall(DMCopyDisc(bcdm, cdm));
57880528010dSStefano Zampini   PetscCall(DMGetLocalSection(cdm, &cs));
57890528010dSStefano Zampini   PetscCall(PetscSectionSetNumFields(cs, 1));
57900528010dSStefano Zampini   PetscCall(PetscSectionSetFieldComponents(cs, 0, cdim));
57910528010dSStefano Zampini   PetscCall(DMGetCoordinatesLocal(bdm, &coordinates));
57920528010dSStefano Zampini   PetscCall(VecDuplicate(coordinates, &coordinatesNew));
57930528010dSStefano Zampini   PetscCall(VecCopy(coordinates, coordinatesNew));
57940528010dSStefano Zampini   PetscCall(DMSetCoordinatesLocal(*dm, coordinatesNew));
57950528010dSStefano Zampini   PetscCall(VecDestroy(&coordinatesNew));
57969f6c5813SMatthew G. Knepley 
57979f6c5813SMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)tr));
57989f6c5813SMatthew G. Knepley   PetscCall(DMPlexTransformDestroy(&((DM_Plex *)(*dm)->data)->tr));
57999f6c5813SMatthew G. Knepley   ((DM_Plex *)(*dm)->data)->tr = tr;
58000528010dSStefano Zampini   PetscCall(DMPlexDistributeSetDefault(*dm, PETSC_FALSE));
58010528010dSStefano Zampini   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)*dm, prefix));
58020528010dSStefano Zampini   PetscCall(DMSetFromOptions(*dm));
58039f6c5813SMatthew G. Knepley 
58049f6c5813SMatthew G. Knepley   PetscCall(DMGetNumLabels(bdm, &Nl));
58059f6c5813SMatthew G. Knepley   for (PetscInt l = 0; l < Nl; ++l) {
58069f6c5813SMatthew G. Knepley     DMLabel     label, labelNew;
58079f6c5813SMatthew G. Knepley     const char *lname;
58089f6c5813SMatthew G. Knepley     PetscBool   isDepth, isCellType;
58099f6c5813SMatthew G. Knepley 
58109f6c5813SMatthew G. Knepley     PetscCall(DMGetLabelName(bdm, l, &lname));
58119f6c5813SMatthew G. Knepley     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
58129f6c5813SMatthew G. Knepley     if (isDepth) continue;
58139f6c5813SMatthew G. Knepley     PetscCall(PetscStrcmp(lname, "celltype", &isCellType));
58149f6c5813SMatthew G. Knepley     if (isCellType) continue;
58159f6c5813SMatthew G. Knepley     PetscCall(DMCreateLabel(*dm, lname));
58169f6c5813SMatthew G. Knepley     PetscCall(DMGetLabel(bdm, lname, &label));
58179f6c5813SMatthew G. Knepley     PetscCall(DMGetLabel(*dm, lname, &labelNew));
58189f6c5813SMatthew G. Knepley     PetscCall(DMLabelSetType(labelNew, DMLABELEPHEMERAL));
58199f6c5813SMatthew G. Knepley     PetscCall(DMLabelEphemeralSetLabel(labelNew, label));
58209f6c5813SMatthew G. Knepley     PetscCall(DMLabelEphemeralSetTransform(labelNew, tr));
58219f6c5813SMatthew G. Knepley     PetscCall(DMLabelSetUp(labelNew));
58229f6c5813SMatthew G. Knepley   }
58233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58249f6c5813SMatthew G. Knepley }
5825