xref: /petsc/src/dm/impls/plex/plexcreate.c (revision 49704ca50b493c2e85d363bc78de18338d2b5f1c)
1552f7358SJed Brown #define PETSCDM_DLL
2af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h> /*I   "petscdmplex.h"   I*/
3cc4c1da9SBarry Smith #include <petsc/private/hashseti.h>
40c312b8eSJed Brown #include <petscsf.h>
5cc4c1da9SBarry Smith #include <petscdmplextransform.h> /*I   "petscdmplextransform.h"   I*/
69f6c5813SMatthew G. Knepley #include <petscdmlabelephemeral.h>
7b7f5c055SJed Brown #include <petsc/private/kernels/blockmatmult.h>
8b7f5c055SJed Brown #include <petsc/private/kernels/blockinvert.h>
9552f7358SJed Brown 
10d0812dedSMatthew G. Knepley #ifdef PETSC_HAVE_UNISTD_H
11d0812dedSMatthew G. Knepley   #include <unistd.h>
12d0812dedSMatthew G. Knepley #endif
13d0812dedSMatthew G. Knepley #include <errno.h>
14d0812dedSMatthew G. Knepley 
15708be2fdSJed Brown PetscLogEvent DMPLEX_CreateFromFile, DMPLEX_CreateFromOptions, DMPLEX_BuildFromCellList, DMPLEX_BuildCoordinatesFromCellList;
1658cd63d5SVaclav Hapla 
179318fe57SMatthew G. Knepley /* External function declarations here */
189318fe57SMatthew G. Knepley static PetscErrorCode DMInitialize_Plex(DM dm);
199318fe57SMatthew G. Knepley 
20e600fa54SMatthew G. Knepley /* This copies internal things in the Plex structure that we generally want when making a new, related Plex */
21d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCopy_Internal(DM dmin, PetscBool copyPeriodicity, PetscBool copyOverlap, DM dmout)
22d71ae5a4SJacob Faibussowitsch {
234fb89dddSMatthew G. Knepley   const PetscReal     *maxCell, *Lstart, *L;
2412a88998SMatthew G. Knepley   VecType              vecType;
2512a88998SMatthew G. Knepley   MatType              matType;
265962854dSMatthew G. Knepley   PetscBool            dist, useCeed;
27adc21957SMatthew G. Knepley   DMReorderDefaultFlag reorder;
28e600fa54SMatthew G. Knepley 
29e600fa54SMatthew G. Knepley   PetscFunctionBegin;
3012a88998SMatthew G. Knepley   PetscCall(DMGetVecType(dmin, &vecType));
3112a88998SMatthew G. Knepley   PetscCall(DMSetVecType(dmout, vecType));
3212a88998SMatthew G. Knepley   PetscCall(DMGetMatType(dmin, &matType));
3312a88998SMatthew G. Knepley   PetscCall(DMSetMatType(dmout, matType));
34e600fa54SMatthew G. Knepley   if (copyPeriodicity) {
354fb89dddSMatthew G. Knepley     PetscCall(DMGetPeriodicity(dmin, &maxCell, &Lstart, &L));
364fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dmout, maxCell, Lstart, L));
373d0e8ed9SDavid Salac     PetscCall(DMLocalizeCoordinates(dmout));
38e600fa54SMatthew G. Knepley   }
399566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeGetDefault(dmin, &dist));
409566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeSetDefault(dmout, dist));
416bc1bd01Sksagiyam   PetscCall(DMPlexReorderGetDefault(dmin, &reorder));
426bc1bd01Sksagiyam   PetscCall(DMPlexReorderSetDefault(dmout, reorder));
435962854dSMatthew G. Knepley   PetscCall(DMPlexGetUseCeed(dmin, &useCeed));
445962854dSMatthew G. Knepley   PetscCall(DMPlexSetUseCeed(dmout, useCeed));
45e600fa54SMatthew G. Knepley   ((DM_Plex *)dmout->data)->useHashLocation = ((DM_Plex *)dmin->data)->useHashLocation;
465962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printSetValues  = ((DM_Plex *)dmin->data)->printSetValues;
475962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printFEM        = ((DM_Plex *)dmin->data)->printFEM;
485962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printFVM        = ((DM_Plex *)dmin->data)->printFVM;
495962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printL2         = ((DM_Plex *)dmin->data)->printL2;
505962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printLocate     = ((DM_Plex *)dmin->data)->printLocate;
515962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printTol        = ((DM_Plex *)dmin->data)->printTol;
521baa6e33SBarry Smith   if (copyOverlap) PetscCall(DMPlexSetOverlap_Plex(dmout, dmin, 0));
533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
54e600fa54SMatthew G. Knepley }
55e600fa54SMatthew G. Knepley 
569318fe57SMatthew G. Knepley /* Replace dm with the contents of ndm, and then destroy ndm
579318fe57SMatthew G. Knepley    - Share the DM_Plex structure
589318fe57SMatthew G. Knepley    - Share the coordinates
599318fe57SMatthew G. Knepley    - Share the SF
609318fe57SMatthew G. Knepley */
61d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexReplace_Internal(DM dm, DM *ndm)
62d71ae5a4SJacob Faibussowitsch {
639318fe57SMatthew G. Knepley   PetscSF          sf;
649318fe57SMatthew G. Knepley   DM               dmNew = *ndm, coordDM, coarseDM;
659318fe57SMatthew G. Knepley   Vec              coords;
664fb89dddSMatthew G. Knepley   const PetscReal *maxCell, *Lstart, *L;
679318fe57SMatthew G. Knepley   PetscInt         dim, cdim;
689318fe57SMatthew G. Knepley 
699318fe57SMatthew G. Knepley   PetscFunctionBegin;
709318fe57SMatthew G. Knepley   if (dm == dmNew) {
719566063dSJacob Faibussowitsch     PetscCall(DMDestroy(ndm));
723ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
739318fe57SMatthew G. Knepley   }
749318fe57SMatthew G. Knepley   dm->setupcalled = dmNew->setupcalled;
75d0812dedSMatthew G. Knepley   if (!dm->hdr.name) {
76d0812dedSMatthew G. Knepley     const char *name;
77d0812dedSMatthew G. Knepley 
78d0812dedSMatthew G. Knepley     PetscCall(PetscObjectGetName((PetscObject)*ndm, &name));
79d0812dedSMatthew G. Knepley     PetscCall(PetscObjectSetName((PetscObject)dm, name));
80d0812dedSMatthew G. Knepley   }
819566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dmNew, &dim));
829566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
839566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dmNew, &cdim));
849566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, cdim));
859566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmNew, &sf));
869566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dm, sf));
879566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmNew, &coordDM));
889566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmNew, &coords));
899566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dm, coordDM));
909566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coords));
916858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dmNew, &coordDM));
926858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dmNew, &coords));
936858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinateDM(dm, coordDM));
946858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinatesLocal(dm, coords));
959318fe57SMatthew G. Knepley   /* Do not want to create the coordinate field if it does not already exist, so do not call DMGetCoordinateField() */
966858538eSMatthew G. Knepley   PetscCall(DMFieldDestroy(&dm->coordinates[0].field));
976858538eSMatthew G. Knepley   dm->coordinates[0].field            = dmNew->coordinates[0].field;
9861a622f3SMatthew G. Knepley   ((DM_Plex *)dmNew->data)->coordFunc = ((DM_Plex *)dm->data)->coordFunc;
994fb89dddSMatthew G. Knepley   PetscCall(DMGetPeriodicity(dmNew, &maxCell, &Lstart, &L));
1004fb89dddSMatthew G. Knepley   PetscCall(DMSetPeriodicity(dm, maxCell, Lstart, L));
1016925d1c4SMatthew G. Knepley   PetscCall(DMPlexGetGlobalToNaturalSF(dmNew, &sf));
1026925d1c4SMatthew G. Knepley   PetscCall(DMPlexSetGlobalToNaturalSF(dm, sf));
1039566063dSJacob Faibussowitsch   PetscCall(DMDestroy_Plex(dm));
1049566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(dm));
1059318fe57SMatthew G. Knepley   dm->data = dmNew->data;
1069318fe57SMatthew G. Knepley   ((DM_Plex *)dmNew->data)->refct++;
1071fca310dSJames Wright   {
1081fca310dSJames Wright     PetscInt       num_face_sfs;
1091fca310dSJames Wright     const PetscSF *sfs;
1101fca310dSJames Wright     PetscCall(DMPlexGetIsoperiodicFaceSF(dm, &num_face_sfs, &sfs));
1111fca310dSJames Wright     PetscCall(DMPlexSetIsoperiodicFaceSF(dm, num_face_sfs, (PetscSF *)sfs)); // for the compose function effect on dm
1121fca310dSJames Wright   }
1139566063dSJacob Faibussowitsch   PetscCall(DMDestroyLabelLinkList_Internal(dm));
1149566063dSJacob Faibussowitsch   PetscCall(DMCopyLabels(dmNew, dm, PETSC_OWN_POINTER, PETSC_TRUE, DM_COPY_LABELS_FAIL));
1159566063dSJacob Faibussowitsch   PetscCall(DMGetCoarseDM(dmNew, &coarseDM));
1169566063dSJacob Faibussowitsch   PetscCall(DMSetCoarseDM(dm, coarseDM));
1179566063dSJacob Faibussowitsch   PetscCall(DMDestroy(ndm));
1183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1199318fe57SMatthew G. Knepley }
1209318fe57SMatthew G. Knepley 
1219318fe57SMatthew G. Knepley /* Swap dm with the contents of dmNew
1229318fe57SMatthew G. Knepley    - Swap the DM_Plex structure
1239318fe57SMatthew G. Knepley    - Swap the coordinates
1249318fe57SMatthew G. Knepley    - Swap the point PetscSF
1259318fe57SMatthew G. Knepley */
126d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexSwap_Static(DM dmA, DM dmB)
127d71ae5a4SJacob Faibussowitsch {
1289318fe57SMatthew G. Knepley   DM          coordDMA, coordDMB;
1299318fe57SMatthew G. Knepley   Vec         coordsA, coordsB;
1309318fe57SMatthew G. Knepley   PetscSF     sfA, sfB;
1319318fe57SMatthew G. Knepley   DMField     fieldTmp;
1329318fe57SMatthew G. Knepley   void       *tmp;
1339318fe57SMatthew G. Knepley   DMLabelLink listTmp;
1349318fe57SMatthew G. Knepley   DMLabel     depthTmp;
1359318fe57SMatthew G. Knepley   PetscInt    tmpI;
1369318fe57SMatthew G. Knepley 
1379318fe57SMatthew G. Knepley   PetscFunctionBegin;
1383ba16761SJacob Faibussowitsch   if (dmA == dmB) PetscFunctionReturn(PETSC_SUCCESS);
1399566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmA, &sfA));
1409566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmB, &sfB));
1419566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sfA));
1429566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dmA, sfB));
1439566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dmB, sfA));
1449566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)sfA));
1459318fe57SMatthew G. Knepley 
1469566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmA, &coordDMA));
1479566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmB, &coordDMB));
1489566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)coordDMA));
1499566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dmA, coordDMB));
1509566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dmB, coordDMA));
1519566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)coordDMA));
1529318fe57SMatthew G. Knepley 
1539566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmA, &coordsA));
1549566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmB, &coordsB));
1559566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)coordsA));
1569566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmA, coordsB));
1579566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmB, coordsA));
1589566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)coordsA));
1599318fe57SMatthew G. Knepley 
1606858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dmA, &coordDMA));
1616858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dmB, &coordDMB));
1626858538eSMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)coordDMA));
1636858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinateDM(dmA, coordDMB));
1646858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinateDM(dmB, coordDMA));
1656858538eSMatthew G. Knepley   PetscCall(PetscObjectDereference((PetscObject)coordDMA));
1666858538eSMatthew G. Knepley 
1676858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dmA, &coordsA));
1686858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dmB, &coordsB));
1696858538eSMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)coordsA));
1706858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinatesLocal(dmA, coordsB));
1716858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinatesLocal(dmB, coordsA));
1726858538eSMatthew G. Knepley   PetscCall(PetscObjectDereference((PetscObject)coordsA));
1736858538eSMatthew G. Knepley 
1746858538eSMatthew G. Knepley   fieldTmp                  = dmA->coordinates[0].field;
1756858538eSMatthew G. Knepley   dmA->coordinates[0].field = dmB->coordinates[0].field;
1766858538eSMatthew G. Knepley   dmB->coordinates[0].field = fieldTmp;
1776858538eSMatthew G. Knepley   fieldTmp                  = dmA->coordinates[1].field;
1786858538eSMatthew G. Knepley   dmA->coordinates[1].field = dmB->coordinates[1].field;
1796858538eSMatthew G. Knepley   dmB->coordinates[1].field = fieldTmp;
1809318fe57SMatthew G. Knepley   tmp                       = dmA->data;
1819318fe57SMatthew G. Knepley   dmA->data                 = dmB->data;
1829318fe57SMatthew G. Knepley   dmB->data                 = tmp;
1839318fe57SMatthew G. Knepley   listTmp                   = dmA->labels;
1849318fe57SMatthew G. Knepley   dmA->labels               = dmB->labels;
1859318fe57SMatthew G. Knepley   dmB->labels               = listTmp;
1869318fe57SMatthew G. Knepley   depthTmp                  = dmA->depthLabel;
1879318fe57SMatthew G. Knepley   dmA->depthLabel           = dmB->depthLabel;
1889318fe57SMatthew G. Knepley   dmB->depthLabel           = depthTmp;
1899318fe57SMatthew G. Knepley   depthTmp                  = dmA->celltypeLabel;
1909318fe57SMatthew G. Knepley   dmA->celltypeLabel        = dmB->celltypeLabel;
1919318fe57SMatthew G. Knepley   dmB->celltypeLabel        = depthTmp;
1929318fe57SMatthew G. Knepley   tmpI                      = dmA->levelup;
1939318fe57SMatthew G. Knepley   dmA->levelup              = dmB->levelup;
1949318fe57SMatthew G. Knepley   dmB->levelup              = tmpI;
1953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1969318fe57SMatthew G. Knepley }
1979318fe57SMatthew G. Knepley 
1983431e603SJed Brown PetscErrorCode DMPlexInterpolateInPlace_Internal(DM dm)
199d71ae5a4SJacob Faibussowitsch {
2009318fe57SMatthew G. Knepley   DM idm;
2019318fe57SMatthew G. Knepley 
2029318fe57SMatthew G. Knepley   PetscFunctionBegin;
2039566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolate(dm, &idm));
2049566063dSJacob Faibussowitsch   PetscCall(DMPlexCopyCoordinates(dm, idm));
20569d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &idm));
2063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2079318fe57SMatthew G. Knepley }
2089318fe57SMatthew G. Knepley 
2099318fe57SMatthew G. Knepley /*@C
2109318fe57SMatthew G. Knepley   DMPlexCreateCoordinateSpace - Creates a finite element space for the coordinates
2119318fe57SMatthew G. Knepley 
21220f4b53cSBarry Smith   Collective
2139318fe57SMatthew G. Knepley 
2149318fe57SMatthew G. Knepley   Input Parameters:
21560225df5SJacob Faibussowitsch + dm        - The `DMPLEX`
21620f4b53cSBarry Smith . degree    - The degree of the finite element or `PETSC_DECIDE`
217e44f6aebSMatthew G. Knepley . project   - Flag to project current coordinates into the space
2189318fe57SMatthew G. Knepley - coordFunc - An optional function to map new points from refinement to the surface
2199318fe57SMatthew G. Knepley 
2209318fe57SMatthew G. Knepley   Level: advanced
2219318fe57SMatthew G. Knepley 
2221cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `PetscPointFunc`, `PetscFECreateLagrange()`, `DMGetCoordinateDM()`
2239318fe57SMatthew G. Knepley @*/
224e44f6aebSMatthew G. Knepley PetscErrorCode DMPlexCreateCoordinateSpace(DM dm, PetscInt degree, PetscBool project, PetscPointFunc coordFunc)
225d71ae5a4SJacob Faibussowitsch {
2269318fe57SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
227e44f6aebSMatthew G. Knepley   PetscFE  fe   = NULL;
2289318fe57SMatthew G. Knepley   DM       cdm;
2291df12153SMatthew G. Knepley   PetscInt dim, dE, qorder, height;
2309318fe57SMatthew G. Knepley 
231e44f6aebSMatthew G. Knepley   PetscFunctionBegin;
2329566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
2339566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
2349318fe57SMatthew G. Knepley   qorder = degree;
235e44f6aebSMatthew G. Knepley   PetscCall(DMGetCoordinateDM(dm, &cdm));
236d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)cdm);
237dc431b0cSMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-default_quadrature_order", "Quadrature order is one less than quadrature points per edge", "DMPlexCreateCoordinateSpace", qorder, &qorder, NULL, 0));
238d0609cedSBarry Smith   PetscOptionsEnd();
2391df12153SMatthew G. Knepley   PetscCall(DMPlexGetVTKCellHeight(dm, &height));
240e44f6aebSMatthew G. Knepley   if (degree >= 0) {
241e44f6aebSMatthew G. Knepley     DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
242e44f6aebSMatthew G. Knepley     PetscInt       cStart, cEnd, gct;
243dc431b0cSMatthew G. Knepley 
2441df12153SMatthew G. Knepley     PetscCall(DMPlexGetHeightStratum(dm, height, &cStart, &cEnd));
245dc431b0cSMatthew G. Knepley     if (cEnd > cStart) PetscCall(DMPlexGetCellType(dm, cStart, &ct));
246e44f6aebSMatthew G. Knepley     gct = (PetscInt)ct;
247e44f6aebSMatthew G. Knepley     PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &gct, 1, MPIU_INT, MPI_MIN, PetscObjectComm((PetscObject)dm)));
248e44f6aebSMatthew G. Knepley     ct = (DMPolytopeType)gct;
249e44f6aebSMatthew G. Knepley     // Work around current bug in PetscDualSpaceSetUp_Lagrange()
250e44f6aebSMatthew G. Knepley     //   Can be seen in plex_tutorials-ex10_1
251e44f6aebSMatthew 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));
2524f9ab2b4SJed Brown   }
253e44f6aebSMatthew G. Knepley   PetscCall(DMSetCoordinateDisc(dm, fe, project));
2549566063dSJacob Faibussowitsch   PetscCall(PetscFEDestroy(&fe));
2559318fe57SMatthew G. Knepley   mesh->coordFunc = coordFunc;
2563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2579318fe57SMatthew G. Knepley }
2589318fe57SMatthew G. Knepley 
2591df5d5c5SMatthew G. Knepley /*@
2601df5d5c5SMatthew G. Knepley   DMPlexCreateDoublet - Creates a mesh of two cells of the specified type, optionally with later refinement.
2611df5d5c5SMatthew G. Knepley 
262d083f849SBarry Smith   Collective
2631df5d5c5SMatthew G. Knepley 
2641df5d5c5SMatthew G. Knepley   Input Parameters:
265a1cb98faSBarry Smith + comm            - The communicator for the `DM` object
2661df5d5c5SMatthew G. Knepley . dim             - The spatial dimension
2671df5d5c5SMatthew G. Knepley . simplex         - Flag for simplicial cells, otherwise they are tensor product cells
2681df5d5c5SMatthew G. Knepley . interpolate     - Flag to create intermediate mesh pieces (edges, faces)
2691df5d5c5SMatthew G. Knepley - refinementLimit - A nonzero number indicates the largest admissible volume for a refined cell
2701df5d5c5SMatthew G. Knepley 
2711df5d5c5SMatthew G. Knepley   Output Parameter:
27260225df5SJacob Faibussowitsch . newdm - The `DM` object
2731df5d5c5SMatthew G. Knepley 
2741df5d5c5SMatthew G. Knepley   Level: beginner
2751df5d5c5SMatthew G. Knepley 
2761cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMSetType()`, `DMCreate()`
2771df5d5c5SMatthew G. Knepley @*/
278d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateDoublet(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscBool interpolate, PetscReal refinementLimit, DM *newdm)
279d71ae5a4SJacob Faibussowitsch {
2801df5d5c5SMatthew G. Knepley   DM          dm;
2811df5d5c5SMatthew G. Knepley   PetscMPIInt rank;
2821df5d5c5SMatthew G. Knepley 
2831df5d5c5SMatthew G. Knepley   PetscFunctionBegin;
2849566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, &dm));
2859566063dSJacob Faibussowitsch   PetscCall(DMSetType(dm, DMPLEX));
2869566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
28746139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
2889566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
289ce78fa2fSMatthew G. Knepley   switch (dim) {
290ce78fa2fSMatthew G. Knepley   case 2:
2919566063dSJacob Faibussowitsch     if (simplex) PetscCall(PetscObjectSetName((PetscObject)dm, "triangular"));
2929566063dSJacob Faibussowitsch     else PetscCall(PetscObjectSetName((PetscObject)dm, "quadrilateral"));
293ce78fa2fSMatthew G. Knepley     break;
294ce78fa2fSMatthew G. Knepley   case 3:
2959566063dSJacob Faibussowitsch     if (simplex) PetscCall(PetscObjectSetName((PetscObject)dm, "tetrahedral"));
2969566063dSJacob Faibussowitsch     else PetscCall(PetscObjectSetName((PetscObject)dm, "hexahedral"));
297ce78fa2fSMatthew G. Knepley     break;
298d71ae5a4SJacob Faibussowitsch   default:
299d71ae5a4SJacob Faibussowitsch     SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %" PetscInt_FMT, dim);
300ce78fa2fSMatthew G. Knepley   }
3011df5d5c5SMatthew G. Knepley   if (rank) {
3021df5d5c5SMatthew G. Knepley     PetscInt numPoints[2] = {0, 0};
3039566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, NULL, NULL, NULL, NULL));
3041df5d5c5SMatthew G. Knepley   } else {
3051df5d5c5SMatthew G. Knepley     switch (dim) {
3061df5d5c5SMatthew G. Knepley     case 2:
3071df5d5c5SMatthew G. Knepley       if (simplex) {
3081df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {4, 2};
3091df5d5c5SMatthew G. Knepley         PetscInt    coneSize[6]         = {3, 3, 0, 0, 0, 0};
3101df5d5c5SMatthew G. Knepley         PetscInt    cones[6]            = {2, 3, 4, 5, 4, 3};
3111df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
3121df5d5c5SMatthew G. Knepley         PetscScalar vertexCoords[8]     = {-0.5, 0.5, 0.0, 0.0, 0.0, 1.0, 0.5, 0.5};
3131df5d5c5SMatthew G. Knepley 
3149566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
3151df5d5c5SMatthew G. Knepley       } else {
3161df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {6, 2};
3171df5d5c5SMatthew G. Knepley         PetscInt    coneSize[8]         = {4, 4, 0, 0, 0, 0, 0, 0};
3181df5d5c5SMatthew G. Knepley         PetscInt    cones[8]            = {2, 3, 4, 5, 3, 6, 7, 4};
3191df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
3201df5d5c5SMatthew 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};
3211df5d5c5SMatthew G. Knepley 
3229566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
3231df5d5c5SMatthew G. Knepley       }
3241df5d5c5SMatthew G. Knepley       break;
3251df5d5c5SMatthew G. Knepley     case 3:
3261df5d5c5SMatthew G. Knepley       if (simplex) {
3271df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {5, 2};
3281df5d5c5SMatthew G. Knepley         PetscInt    coneSize[7]         = {4, 4, 0, 0, 0, 0, 0};
3291df5d5c5SMatthew G. Knepley         PetscInt    cones[8]            = {4, 3, 5, 2, 5, 3, 4, 6};
3301df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
3311df5d5c5SMatthew 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};
3321df5d5c5SMatthew G. Knepley 
3339566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
3341df5d5c5SMatthew G. Knepley       } else {
3351df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]         = {12, 2};
3361df5d5c5SMatthew G. Knepley         PetscInt    coneSize[14]         = {8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
3371df5d5c5SMatthew G. Knepley         PetscInt    cones[16]            = {2, 3, 4, 5, 6, 7, 8, 9, 5, 4, 10, 11, 7, 12, 13, 8};
3381df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
3399371c9d4SSatish 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};
3401df5d5c5SMatthew G. Knepley 
3419566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
3421df5d5c5SMatthew G. Knepley       }
3431df5d5c5SMatthew G. Knepley       break;
344d71ae5a4SJacob Faibussowitsch     default:
345d71ae5a4SJacob Faibussowitsch       SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %" PetscInt_FMT, dim);
3461df5d5c5SMatthew G. Knepley     }
3471df5d5c5SMatthew G. Knepley   }
34846139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
3491df5d5c5SMatthew G. Knepley   *newdm = dm;
3501df5d5c5SMatthew G. Knepley   if (refinementLimit > 0.0) {
3511df5d5c5SMatthew G. Knepley     DM          rdm;
3521df5d5c5SMatthew G. Knepley     const char *name;
3531df5d5c5SMatthew G. Knepley 
3549566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementUniform(*newdm, PETSC_FALSE));
3559566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementLimit(*newdm, refinementLimit));
3569566063dSJacob Faibussowitsch     PetscCall(DMRefine(*newdm, comm, &rdm));
3579566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)*newdm, &name));
3589566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)rdm, name));
3599566063dSJacob Faibussowitsch     PetscCall(DMDestroy(newdm));
3601df5d5c5SMatthew G. Knepley     *newdm = rdm;
3611df5d5c5SMatthew G. Knepley   }
3621df5d5c5SMatthew G. Knepley   if (interpolate) {
3635fd9971aSMatthew G. Knepley     DM idm;
3641df5d5c5SMatthew G. Knepley 
3659566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*newdm, &idm));
3669566063dSJacob Faibussowitsch     PetscCall(DMDestroy(newdm));
3671df5d5c5SMatthew G. Knepley     *newdm = idm;
3681df5d5c5SMatthew G. Knepley   }
3693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3701df5d5c5SMatthew G. Knepley }
3711df5d5c5SMatthew G. Knepley 
372d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
373d71ae5a4SJacob Faibussowitsch {
3749318fe57SMatthew G. Knepley   const PetscInt numVertices    = 2;
3759318fe57SMatthew G. Knepley   PetscInt       markerRight    = 1;
3769318fe57SMatthew G. Knepley   PetscInt       markerLeft     = 1;
3779318fe57SMatthew G. Knepley   PetscBool      markerSeparate = PETSC_FALSE;
3789318fe57SMatthew G. Knepley   Vec            coordinates;
3799318fe57SMatthew G. Knepley   PetscSection   coordSection;
3809318fe57SMatthew G. Knepley   PetscScalar   *coords;
3819318fe57SMatthew G. Knepley   PetscInt       coordSize;
3829318fe57SMatthew G. Knepley   PetscMPIInt    rank;
3839318fe57SMatthew G. Knepley   PetscInt       cdim = 1, v;
384552f7358SJed Brown 
3859318fe57SMatthew G. Knepley   PetscFunctionBegin;
3869566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
3879318fe57SMatthew G. Knepley   if (markerSeparate) {
3889318fe57SMatthew G. Knepley     markerRight = 2;
3899318fe57SMatthew G. Knepley     markerLeft  = 1;
3909318fe57SMatthew G. Knepley   }
3919566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
392c5853193SPierre Jolivet   if (rank == 0) {
3939566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numVertices));
3949566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
3959566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", 0, markerLeft));
3969566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", 1, markerRight));
3979318fe57SMatthew G. Knepley   }
3989566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
3999566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
4009318fe57SMatthew G. Knepley   /* Build coordinates */
4019566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, cdim));
4029566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
4039566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
4049566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, 0, numVertices));
4059566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, cdim));
4069318fe57SMatthew G. Knepley   for (v = 0; v < numVertices; ++v) {
4079566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, cdim));
4089566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, cdim));
4099318fe57SMatthew G. Knepley   }
4109566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
4119566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
4129566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
4139566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
4149566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
4159566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, cdim));
4169566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
4179566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
4189318fe57SMatthew G. Knepley   coords[0] = lower[0];
4199318fe57SMatthew G. Knepley   coords[1] = upper[0];
4209566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
4219566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
4229566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
4233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4249318fe57SMatthew G. Knepley }
42526492d91SMatthew G. Knepley 
426d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
427d71ae5a4SJacob Faibussowitsch {
4281df21d24SMatthew G. Knepley   const PetscInt numVertices    = (edges[0] + 1) * (edges[1] + 1);
4291df21d24SMatthew G. Knepley   const PetscInt numEdges       = edges[0] * (edges[1] + 1) + (edges[0] + 1) * edges[1];
430552f7358SJed Brown   PetscInt       markerTop      = 1;
431552f7358SJed Brown   PetscInt       markerBottom   = 1;
432552f7358SJed Brown   PetscInt       markerRight    = 1;
433552f7358SJed Brown   PetscInt       markerLeft     = 1;
434552f7358SJed Brown   PetscBool      markerSeparate = PETSC_FALSE;
435552f7358SJed Brown   Vec            coordinates;
436552f7358SJed Brown   PetscSection   coordSection;
437552f7358SJed Brown   PetscScalar   *coords;
438552f7358SJed Brown   PetscInt       coordSize;
439552f7358SJed Brown   PetscMPIInt    rank;
440552f7358SJed Brown   PetscInt       v, vx, vy;
441552f7358SJed Brown 
442552f7358SJed Brown   PetscFunctionBegin;
4439566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
444552f7358SJed Brown   if (markerSeparate) {
4451df21d24SMatthew G. Knepley     markerTop    = 3;
4461df21d24SMatthew G. Knepley     markerBottom = 1;
4471df21d24SMatthew G. Knepley     markerRight  = 2;
4481df21d24SMatthew G. Knepley     markerLeft   = 4;
449552f7358SJed Brown   }
4509566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
451dd400576SPatrick Sanan   if (rank == 0) {
452552f7358SJed Brown     PetscInt e, ex, ey;
453552f7358SJed Brown 
4549566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numEdges + numVertices));
45548a46eb9SPierre Jolivet     for (e = 0; e < numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2));
4569566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
457552f7358SJed Brown     for (vx = 0; vx <= edges[0]; vx++) {
458552f7358SJed Brown       for (ey = 0; ey < edges[1]; ey++) {
459552f7358SJed Brown         PetscInt edge   = vx * edges[1] + ey + edges[0] * (edges[1] + 1);
460552f7358SJed Brown         PetscInt vertex = ey * (edges[0] + 1) + vx + numEdges;
461da80777bSKarl Rupp         PetscInt cone[2];
462552f7358SJed Brown 
4639371c9d4SSatish Balay         cone[0] = vertex;
4649371c9d4SSatish Balay         cone[1] = vertex + edges[0] + 1;
4659566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, edge, cone));
466552f7358SJed Brown         if (vx == edges[0]) {
4679566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
4689566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
469552f7358SJed Brown           if (ey == edges[1] - 1) {
4709566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
4719566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerRight));
472552f7358SJed Brown           }
473552f7358SJed Brown         } else if (vx == 0) {
4749566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
4759566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
476552f7358SJed Brown           if (ey == edges[1] - 1) {
4779566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
4789566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerLeft));
479552f7358SJed Brown           }
480552f7358SJed Brown         }
481552f7358SJed Brown       }
482552f7358SJed Brown     }
483552f7358SJed Brown     for (vy = 0; vy <= edges[1]; vy++) {
484552f7358SJed Brown       for (ex = 0; ex < edges[0]; ex++) {
485552f7358SJed Brown         PetscInt edge   = vy * edges[0] + ex;
486552f7358SJed Brown         PetscInt vertex = vy * (edges[0] + 1) + ex + numEdges;
487da80777bSKarl Rupp         PetscInt cone[2];
488552f7358SJed Brown 
4899371c9d4SSatish Balay         cone[0] = vertex;
4909371c9d4SSatish Balay         cone[1] = vertex + 1;
4919566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, edge, cone));
492552f7358SJed Brown         if (vy == edges[1]) {
4939566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
4949566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
495552f7358SJed Brown           if (ex == edges[0] - 1) {
4969566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
4979566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerTop));
498552f7358SJed Brown           }
499552f7358SJed Brown         } else if (vy == 0) {
5009566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
5019566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
502552f7358SJed Brown           if (ex == edges[0] - 1) {
5039566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
5049566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerBottom));
505552f7358SJed Brown           }
506552f7358SJed Brown         }
507552f7358SJed Brown       }
508552f7358SJed Brown     }
509552f7358SJed Brown   }
5109566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
5119566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
512552f7358SJed Brown   /* Build coordinates */
5139566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, 2));
5149566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
5159566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
5169566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, numEdges, numEdges + numVertices));
5179566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, 2));
518552f7358SJed Brown   for (v = numEdges; v < numEdges + numVertices; ++v) {
5199566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, 2));
5209566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, 2));
521552f7358SJed Brown   }
5229566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
5239566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
5249566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
5259566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
5269566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
5279566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, 2));
5289566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
5299566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
530552f7358SJed Brown   for (vy = 0; vy <= edges[1]; ++vy) {
531552f7358SJed Brown     for (vx = 0; vx <= edges[0]; ++vx) {
532552f7358SJed Brown       coords[(vy * (edges[0] + 1) + vx) * 2 + 0] = lower[0] + ((upper[0] - lower[0]) / edges[0]) * vx;
533552f7358SJed Brown       coords[(vy * (edges[0] + 1) + vx) * 2 + 1] = lower[1] + ((upper[1] - lower[1]) / edges[1]) * vy;
534552f7358SJed Brown     }
535552f7358SJed Brown   }
5369566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
5379566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
5389566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
5393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
540552f7358SJed Brown }
541552f7358SJed Brown 
542d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt faces[])
543d71ae5a4SJacob Faibussowitsch {
5449e8abbc3SMichael Lange   PetscInt     vertices[3], numVertices;
5457b59f5a9SMichael Lange   PetscInt     numFaces       = 2 * faces[0] * faces[1] + 2 * faces[1] * faces[2] + 2 * faces[0] * faces[2];
546c2df9bbfSMatthew G. Knepley   PetscInt     markerTop      = 1;
547c2df9bbfSMatthew G. Knepley   PetscInt     markerBottom   = 1;
548c2df9bbfSMatthew G. Knepley   PetscInt     markerFront    = 1;
549c2df9bbfSMatthew G. Knepley   PetscInt     markerBack     = 1;
550c2df9bbfSMatthew G. Knepley   PetscInt     markerRight    = 1;
551c2df9bbfSMatthew G. Knepley   PetscInt     markerLeft     = 1;
552c2df9bbfSMatthew G. Knepley   PetscBool    markerSeparate = PETSC_FALSE;
553552f7358SJed Brown   Vec          coordinates;
554552f7358SJed Brown   PetscSection coordSection;
555552f7358SJed Brown   PetscScalar *coords;
556552f7358SJed Brown   PetscInt     coordSize;
557552f7358SJed Brown   PetscMPIInt  rank;
558552f7358SJed Brown   PetscInt     v, vx, vy, vz;
5597b59f5a9SMichael Lange   PetscInt     voffset, iface = 0, cone[4];
560552f7358SJed Brown 
561552f7358SJed Brown   PetscFunctionBegin;
5621dca8a05SBarry 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");
5639566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
564c2df9bbfSMatthew G. Knepley   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
565c2df9bbfSMatthew G. Knepley   if (markerSeparate) {
566c2df9bbfSMatthew G. Knepley     markerBottom = 1;
567c2df9bbfSMatthew G. Knepley     markerTop    = 2;
568c2df9bbfSMatthew G. Knepley     markerFront  = 3;
569c2df9bbfSMatthew G. Knepley     markerBack   = 4;
570c2df9bbfSMatthew G. Knepley     markerRight  = 5;
571c2df9bbfSMatthew G. Knepley     markerLeft   = 6;
572c2df9bbfSMatthew G. Knepley   }
5739371c9d4SSatish Balay   vertices[0] = faces[0] + 1;
5749371c9d4SSatish Balay   vertices[1] = faces[1] + 1;
5759371c9d4SSatish Balay   vertices[2] = faces[2] + 1;
5769e8abbc3SMichael Lange   numVertices = vertices[0] * vertices[1] * vertices[2];
577dd400576SPatrick Sanan   if (rank == 0) {
578552f7358SJed Brown     PetscInt f;
579552f7358SJed Brown 
5809566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numFaces + numVertices));
58148a46eb9SPierre Jolivet     for (f = 0; f < numFaces; ++f) PetscCall(DMPlexSetConeSize(dm, f, 4));
5829566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
5837b59f5a9SMichael Lange 
5847b59f5a9SMichael Lange     /* Side 0 (Top) */
5857b59f5a9SMichael Lange     for (vy = 0; vy < faces[1]; vy++) {
5867b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5877b59f5a9SMichael Lange         voffset = numFaces + vertices[0] * vertices[1] * (vertices[2] - 1) + vy * vertices[0] + vx;
5889371c9d4SSatish Balay         cone[0] = voffset;
5899371c9d4SSatish Balay         cone[1] = voffset + 1;
5909371c9d4SSatish Balay         cone[2] = voffset + vertices[0] + 1;
5919371c9d4SSatish Balay         cone[3] = voffset + vertices[0];
5929566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
593c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerTop));
594c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerTop));
595c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerTop));
596c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerTop));
597c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 1, markerTop));
5987b59f5a9SMichael Lange         iface++;
599552f7358SJed Brown       }
600552f7358SJed Brown     }
6017b59f5a9SMichael Lange 
6027b59f5a9SMichael Lange     /* Side 1 (Bottom) */
6037b59f5a9SMichael Lange     for (vy = 0; vy < faces[1]; vy++) {
6047b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
6057b59f5a9SMichael Lange         voffset = numFaces + vy * (faces[0] + 1) + vx;
6069371c9d4SSatish Balay         cone[0] = voffset + 1;
6079371c9d4SSatish Balay         cone[1] = voffset;
6089371c9d4SSatish Balay         cone[2] = voffset + vertices[0];
6099371c9d4SSatish Balay         cone[3] = voffset + vertices[0] + 1;
6109566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
611c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerBottom));
612c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerBottom));
613c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerBottom));
614c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerBottom));
615c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 1, markerBottom));
6167b59f5a9SMichael Lange         iface++;
617552f7358SJed Brown       }
618552f7358SJed Brown     }
6197b59f5a9SMichael Lange 
6207b59f5a9SMichael Lange     /* Side 2 (Front) */
6217b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
6227b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
6237b59f5a9SMichael Lange         voffset = numFaces + vz * vertices[0] * vertices[1] + vx;
6249371c9d4SSatish Balay         cone[0] = voffset;
6259371c9d4SSatish Balay         cone[1] = voffset + 1;
6269371c9d4SSatish Balay         cone[2] = voffset + vertices[0] * vertices[1] + 1;
6279371c9d4SSatish Balay         cone[3] = voffset + vertices[0] * vertices[1];
6289566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
629c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerFront));
630c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerFront));
631c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerFront));
632c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 0, markerFront));
633c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 1, markerFront));
6347b59f5a9SMichael Lange         iface++;
635552f7358SJed Brown       }
6367b59f5a9SMichael Lange     }
6377b59f5a9SMichael Lange 
6387b59f5a9SMichael Lange     /* Side 3 (Back) */
6397b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
6407b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
6417b59f5a9SMichael Lange         voffset = numFaces + vz * vertices[0] * vertices[1] + vertices[0] * (vertices[1] - 1) + vx;
6429371c9d4SSatish Balay         cone[0] = voffset + vertices[0] * vertices[1];
6439371c9d4SSatish Balay         cone[1] = voffset + vertices[0] * vertices[1] + 1;
6449371c9d4SSatish Balay         cone[2] = voffset + 1;
6459371c9d4SSatish Balay         cone[3] = voffset;
6469566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
647c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerBack));
648c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerBack));
649c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerBack));
650c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 0, markerBack));
651c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 1, markerBack));
6527b59f5a9SMichael Lange         iface++;
6537b59f5a9SMichael Lange       }
6547b59f5a9SMichael Lange     }
6557b59f5a9SMichael Lange 
6567b59f5a9SMichael Lange     /* Side 4 (Left) */
6577b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
6587b59f5a9SMichael Lange       for (vy = 0; vy < faces[1]; vy++) {
6597b59f5a9SMichael Lange         voffset = numFaces + vz * vertices[0] * vertices[1] + vy * vertices[0];
6609371c9d4SSatish Balay         cone[0] = voffset;
6619371c9d4SSatish Balay         cone[1] = voffset + vertices[0] * vertices[1];
6629371c9d4SSatish Balay         cone[2] = voffset + vertices[0] * vertices[1] + vertices[0];
6639371c9d4SSatish Balay         cone[3] = voffset + vertices[0];
6649566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
665c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerLeft));
666c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerLeft));
667c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerLeft));
668c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[1] + 0, markerLeft));
669c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + vertices[0], markerLeft));
6707b59f5a9SMichael Lange         iface++;
6717b59f5a9SMichael Lange       }
6727b59f5a9SMichael Lange     }
6737b59f5a9SMichael Lange 
6747b59f5a9SMichael Lange     /* Side 5 (Right) */
6757b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
6767b59f5a9SMichael Lange       for (vy = 0; vy < faces[1]; vy++) {
677aab5bcd8SJed Brown         voffset = numFaces + vz * vertices[0] * vertices[1] + vy * vertices[0] + faces[0];
6789371c9d4SSatish Balay         cone[0] = voffset + vertices[0] * vertices[1];
6799371c9d4SSatish Balay         cone[1] = voffset;
6809371c9d4SSatish Balay         cone[2] = voffset + vertices[0];
6819371c9d4SSatish Balay         cone[3] = voffset + vertices[0] * vertices[1] + vertices[0];
6829566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
683c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerRight));
684c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerRight));
685c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerRight));
686c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 0, markerRight));
687c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + vertices[0], markerRight));
6887b59f5a9SMichael Lange         iface++;
6897b59f5a9SMichael Lange       }
690552f7358SJed Brown     }
691552f7358SJed Brown   }
6929566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
6939566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
694552f7358SJed Brown   /* Build coordinates */
6959566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, 3));
6969566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
6979566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
6989566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, numFaces, numFaces + numVertices));
6999566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, 3));
700552f7358SJed Brown   for (v = numFaces; v < numFaces + numVertices; ++v) {
7019566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, 3));
7029566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, 3));
703552f7358SJed Brown   }
7049566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
7059566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
7069566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
7079566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
7089566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
7099566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, 3));
7109566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
7119566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
712552f7358SJed Brown   for (vz = 0; vz <= faces[2]; ++vz) {
713552f7358SJed Brown     for (vy = 0; vy <= faces[1]; ++vy) {
714552f7358SJed Brown       for (vx = 0; vx <= faces[0]; ++vx) {
715552f7358SJed Brown         coords[((vz * (faces[1] + 1) + vy) * (faces[0] + 1) + vx) * 3 + 0] = lower[0] + ((upper[0] - lower[0]) / faces[0]) * vx;
716552f7358SJed Brown         coords[((vz * (faces[1] + 1) + vy) * (faces[0] + 1) + vx) * 3 + 1] = lower[1] + ((upper[1] - lower[1]) / faces[1]) * vy;
717552f7358SJed Brown         coords[((vz * (faces[1] + 1) + vy) * (faces[0] + 1) + vx) * 3 + 2] = lower[2] + ((upper[2] - lower[2]) / faces[2]) * vz;
718552f7358SJed Brown       }
719552f7358SJed Brown     }
720552f7358SJed Brown   }
7219566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
7229566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
7239566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
7243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
725552f7358SJed Brown }
726552f7358SJed Brown 
727d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate)
728d71ae5a4SJacob Faibussowitsch {
7299318fe57SMatthew G. Knepley   PetscFunctionBegin;
7309318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
73146139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
7329566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim - 1));
7339566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, dim));
7349318fe57SMatthew G. Knepley   switch (dim) {
735d71ae5a4SJacob Faibussowitsch   case 1:
736d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(dm, lower, upper, faces));
737d71ae5a4SJacob Faibussowitsch     break;
738d71ae5a4SJacob Faibussowitsch   case 2:
739d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(dm, lower, upper, faces));
740d71ae5a4SJacob Faibussowitsch     break;
741d71ae5a4SJacob Faibussowitsch   case 3:
742d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(dm, lower, upper, faces));
743d71ae5a4SJacob Faibussowitsch     break;
744d71ae5a4SJacob Faibussowitsch   default:
745d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Dimension not supported: %" PetscInt_FMT, dim);
7469318fe57SMatthew G. Knepley   }
74746139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
7489566063dSJacob Faibussowitsch   if (interpolate) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
7493ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7509318fe57SMatthew G. Knepley }
7519318fe57SMatthew G. Knepley 
7529318fe57SMatthew G. Knepley /*@C
7539318fe57SMatthew G. Knepley   DMPlexCreateBoxSurfaceMesh - Creates a mesh on the surface of the tensor product of unit intervals (box) using tensor cells (hexahedra).
7549318fe57SMatthew G. Knepley 
7559318fe57SMatthew G. Knepley   Collective
7569318fe57SMatthew G. Knepley 
7579318fe57SMatthew G. Knepley   Input Parameters:
758a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
75920f4b53cSBarry Smith . dim         - The spatial dimension of the box, so the resulting mesh is has dimension `dim`-1
76020f4b53cSBarry Smith . faces       - Number of faces per dimension, or `NULL` for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
76120f4b53cSBarry Smith . lower       - The lower left corner, or `NULL` for (0, 0, 0)
76220f4b53cSBarry Smith . upper       - The upper right corner, or `NULL` for (1, 1, 1)
7639318fe57SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
7649318fe57SMatthew G. Knepley 
7659318fe57SMatthew G. Knepley   Output Parameter:
766a1cb98faSBarry Smith . dm - The `DM` object
7679318fe57SMatthew G. Knepley 
7689318fe57SMatthew G. Knepley   Level: beginner
7699318fe57SMatthew G. Knepley 
7701cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMSetFromOptions()`, `DMPlexCreateBoxMesh()`, `DMPlexCreateFromFile()`, `DMSetType()`, `DMCreate()`
7719318fe57SMatthew G. Knepley @*/
772d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateBoxSurfaceMesh(MPI_Comm comm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate, DM *dm)
773d71ae5a4SJacob Faibussowitsch {
7749318fe57SMatthew G. Knepley   PetscInt  fac[3] = {1, 1, 1};
7759318fe57SMatthew G. Knepley   PetscReal low[3] = {0, 0, 0};
7769318fe57SMatthew G. Knepley   PetscReal upp[3] = {1, 1, 1};
7779318fe57SMatthew G. Knepley 
7789318fe57SMatthew G. Knepley   PetscFunctionBegin;
7799566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
7809566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
7819566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(*dm, dim, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, interpolate));
7823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7839318fe57SMatthew G. Knepley }
7849318fe57SMatthew G. Knepley 
785d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateLineMesh_Internal(DM dm, PetscInt segments, PetscReal lower, PetscReal upper, DMBoundaryType bd)
786d71ae5a4SJacob Faibussowitsch {
787fdbf62faSLisandro Dalcin   PetscInt     i, fStart, fEnd, numCells = 0, numVerts = 0;
788fdbf62faSLisandro Dalcin   PetscInt     numPoints[2], *coneSize, *cones, *coneOrientations;
789fdbf62faSLisandro Dalcin   PetscScalar *vertexCoords;
790fdbf62faSLisandro Dalcin   PetscReal    L, maxCell;
791fdbf62faSLisandro Dalcin   PetscBool    markerSeparate = PETSC_FALSE;
792fdbf62faSLisandro Dalcin   PetscInt     markerLeft = 1, faceMarkerLeft = 1;
793fdbf62faSLisandro Dalcin   PetscInt     markerRight = 1, faceMarkerRight = 2;
794fdbf62faSLisandro Dalcin   PetscBool    wrap = (bd == DM_BOUNDARY_PERIODIC || bd == DM_BOUNDARY_TWIST) ? PETSC_TRUE : PETSC_FALSE;
795fdbf62faSLisandro Dalcin   PetscMPIInt  rank;
796fdbf62faSLisandro Dalcin 
797fdbf62faSLisandro Dalcin   PetscFunctionBegin;
7984f572ea9SToby Isaac   PetscAssertPointer(dm, 1);
799fdbf62faSLisandro Dalcin 
8009566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, 1));
8019566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
8029566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
803fdbf62faSLisandro Dalcin 
8049566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
805dd400576SPatrick Sanan   if (rank == 0) numCells = segments;
806dd400576SPatrick Sanan   if (rank == 0) numVerts = segments + (wrap ? 0 : 1);
807fdbf62faSLisandro Dalcin 
8089371c9d4SSatish Balay   numPoints[0] = numVerts;
8099371c9d4SSatish Balay   numPoints[1] = numCells;
8109566063dSJacob Faibussowitsch   PetscCall(PetscMalloc4(numCells + numVerts, &coneSize, numCells * 2, &cones, numCells + numVerts, &coneOrientations, numVerts, &vertexCoords));
8119566063dSJacob Faibussowitsch   PetscCall(PetscArrayzero(coneOrientations, numCells + numVerts));
812ad540459SPierre Jolivet   for (i = 0; i < numCells; ++i) coneSize[i] = 2;
813ad540459SPierre Jolivet   for (i = 0; i < numVerts; ++i) coneSize[numCells + i] = 0;
8149371c9d4SSatish Balay   for (i = 0; i < numCells; ++i) {
8159371c9d4SSatish Balay     cones[2 * i]     = numCells + i % numVerts;
8169371c9d4SSatish Balay     cones[2 * i + 1] = numCells + (i + 1) % numVerts;
8179371c9d4SSatish Balay   }
818ad540459SPierre Jolivet   for (i = 0; i < numVerts; ++i) vertexCoords[i] = lower + (upper - lower) * ((PetscReal)i / (PetscReal)numCells);
8199566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
8209566063dSJacob Faibussowitsch   PetscCall(PetscFree4(coneSize, cones, coneOrientations, vertexCoords));
821fdbf62faSLisandro Dalcin 
8229566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
8239371c9d4SSatish Balay   if (markerSeparate) {
8249371c9d4SSatish Balay     markerLeft  = faceMarkerLeft;
8259371c9d4SSatish Balay     markerRight = faceMarkerRight;
8269371c9d4SSatish Balay   }
827dd400576SPatrick Sanan   if (!wrap && rank == 0) {
8289566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
8299566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", fStart, markerLeft));
8309566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", fEnd - 1, markerRight));
8319566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "Face Sets", fStart, faceMarkerLeft));
8329566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "Face Sets", fEnd - 1, faceMarkerRight));
833fdbf62faSLisandro Dalcin   }
834fdbf62faSLisandro Dalcin   if (wrap) {
835fdbf62faSLisandro Dalcin     L       = upper - lower;
836fdbf62faSLisandro Dalcin     maxCell = (PetscReal)1.1 * (L / (PetscReal)PetscMax(1, segments));
8374fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, &maxCell, &lower, &L));
838fdbf62faSLisandro Dalcin   }
8399566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
8403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
841fdbf62faSLisandro Dalcin }
842fdbf62faSLisandro Dalcin 
843d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxMesh_Simplex_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate)
844d71ae5a4SJacob Faibussowitsch {
8459318fe57SMatthew G. Knepley   DM      boundary, vol;
846c22d3578SMatthew G. Knepley   DMLabel bdlabel;
847d6218766SMatthew G. Knepley 
848d6218766SMatthew G. Knepley   PetscFunctionBegin;
8494f572ea9SToby Isaac   PetscAssertPointer(dm, 1);
850c22d3578SMatthew 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");
8519566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &boundary));
8529566063dSJacob Faibussowitsch   PetscCall(DMSetType(boundary, DMPLEX));
8539566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(boundary, dim, faces, lower, upper, PETSC_FALSE));
8549566063dSJacob Faibussowitsch   PetscCall(DMPlexGenerate(boundary, NULL, interpolate, &vol));
855c22d3578SMatthew G. Knepley   PetscCall(DMGetLabel(vol, "marker", &bdlabel));
856c22d3578SMatthew G. Knepley   if (bdlabel) PetscCall(DMPlexLabelComplete(vol, bdlabel));
8575de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_FALSE, vol));
85869d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &vol));
8599566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&boundary));
8603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
861d6218766SMatthew G. Knepley }
862d6218766SMatthew G. Knepley 
863d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCubeMesh_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], DMBoundaryType bdX, DMBoundaryType bdY, DMBoundaryType bdZ)
864d71ae5a4SJacob Faibussowitsch {
865ed0e4b50SMatthew G. Knepley   DMLabel     cutLabel  = NULL;
866f4eb4c5dSMatthew G. Knepley   PetscInt    markerTop = 1, faceMarkerTop = 1;
867f4eb4c5dSMatthew G. Knepley   PetscInt    markerBottom = 1, faceMarkerBottom = 1;
868f4eb4c5dSMatthew G. Knepley   PetscInt    markerFront = 1, faceMarkerFront = 1;
869f4eb4c5dSMatthew G. Knepley   PetscInt    markerBack = 1, faceMarkerBack = 1;
870f4eb4c5dSMatthew G. Knepley   PetscInt    markerRight = 1, faceMarkerRight = 1;
871f4eb4c5dSMatthew G. Knepley   PetscInt    markerLeft = 1, faceMarkerLeft = 1;
8723dfda0b1SToby Isaac   PetscInt    dim;
873d8211ee3SMatthew G. Knepley   PetscBool   markerSeparate = PETSC_FALSE, cutMarker = PETSC_FALSE;
8743dfda0b1SToby Isaac   PetscMPIInt rank;
8753dfda0b1SToby Isaac 
8763dfda0b1SToby Isaac   PetscFunctionBegin;
8779566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
8789566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
8799566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
8809566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
8819566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL));
8829371c9d4SSatish 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) {
8839371c9d4SSatish Balay     if (cutMarker) {
8849371c9d4SSatish Balay       PetscCall(DMCreateLabel(dm, "periodic_cut"));
8859371c9d4SSatish Balay       PetscCall(DMGetLabel(dm, "periodic_cut", &cutLabel));
8869371c9d4SSatish Balay     }
887d8211ee3SMatthew G. Knepley   }
8883dfda0b1SToby Isaac   switch (dim) {
8893dfda0b1SToby Isaac   case 2:
890f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 3;
891f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
892f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 2;
893f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 4;
8943dfda0b1SToby Isaac     break;
8953dfda0b1SToby Isaac   case 3:
896f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
897f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 2;
898f4eb4c5dSMatthew G. Knepley     faceMarkerFront  = 3;
899f4eb4c5dSMatthew G. Knepley     faceMarkerBack   = 4;
900f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 5;
901f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 6;
9023dfda0b1SToby Isaac     break;
903d71ae5a4SJacob Faibussowitsch   default:
904d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Dimension %" PetscInt_FMT " not supported", dim);
9053dfda0b1SToby Isaac   }
9069566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
907f4eb4c5dSMatthew G. Knepley   if (markerSeparate) {
908f4eb4c5dSMatthew G. Knepley     markerBottom = faceMarkerBottom;
909f4eb4c5dSMatthew G. Knepley     markerTop    = faceMarkerTop;
910f4eb4c5dSMatthew G. Knepley     markerFront  = faceMarkerFront;
911f4eb4c5dSMatthew G. Knepley     markerBack   = faceMarkerBack;
912f4eb4c5dSMatthew G. Knepley     markerRight  = faceMarkerRight;
913f4eb4c5dSMatthew G. Knepley     markerLeft   = faceMarkerLeft;
9143dfda0b1SToby Isaac   }
9153dfda0b1SToby Isaac   {
916dd400576SPatrick Sanan     const PetscInt numXEdges    = rank == 0 ? edges[0] : 0;
917dd400576SPatrick Sanan     const PetscInt numYEdges    = rank == 0 ? edges[1] : 0;
918dd400576SPatrick Sanan     const PetscInt numZEdges    = rank == 0 ? edges[2] : 0;
919dd400576SPatrick Sanan     const PetscInt numXVertices = rank == 0 ? (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ? edges[0] : edges[0] + 1) : 0;
920dd400576SPatrick Sanan     const PetscInt numYVertices = rank == 0 ? (bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ? edges[1] : edges[1] + 1) : 0;
921dd400576SPatrick Sanan     const PetscInt numZVertices = rank == 0 ? (bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST ? edges[2] : edges[2] + 1) : 0;
9223dfda0b1SToby Isaac     const PetscInt numCells     = numXEdges * numYEdges * numZEdges;
9233dfda0b1SToby Isaac     const PetscInt numXFaces    = numYEdges * numZEdges;
9243dfda0b1SToby Isaac     const PetscInt numYFaces    = numXEdges * numZEdges;
9253dfda0b1SToby Isaac     const PetscInt numZFaces    = numXEdges * numYEdges;
9263dfda0b1SToby Isaac     const PetscInt numTotXFaces = numXVertices * numXFaces;
9273dfda0b1SToby Isaac     const PetscInt numTotYFaces = numYVertices * numYFaces;
9283dfda0b1SToby Isaac     const PetscInt numTotZFaces = numZVertices * numZFaces;
9293dfda0b1SToby Isaac     const PetscInt numFaces     = numTotXFaces + numTotYFaces + numTotZFaces;
9303dfda0b1SToby Isaac     const PetscInt numTotXEdges = numXEdges * numYVertices * numZVertices;
9313dfda0b1SToby Isaac     const PetscInt numTotYEdges = numYEdges * numXVertices * numZVertices;
9323dfda0b1SToby Isaac     const PetscInt numTotZEdges = numZEdges * numXVertices * numYVertices;
9333dfda0b1SToby Isaac     const PetscInt numVertices  = numXVertices * numYVertices * numZVertices;
9343dfda0b1SToby Isaac     const PetscInt numEdges     = numTotXEdges + numTotYEdges + numTotZEdges;
9353dfda0b1SToby Isaac     const PetscInt firstVertex  = (dim == 2) ? numFaces : numCells;
9363dfda0b1SToby Isaac     const PetscInt firstXFace   = (dim == 2) ? 0 : numCells + numVertices;
9373dfda0b1SToby Isaac     const PetscInt firstYFace   = firstXFace + numTotXFaces;
9383dfda0b1SToby Isaac     const PetscInt firstZFace   = firstYFace + numTotYFaces;
9393dfda0b1SToby Isaac     const PetscInt firstXEdge   = numCells + numFaces + numVertices;
9403dfda0b1SToby Isaac     const PetscInt firstYEdge   = firstXEdge + numTotXEdges;
9413dfda0b1SToby Isaac     const PetscInt firstZEdge   = firstYEdge + numTotYEdges;
9423dfda0b1SToby Isaac     Vec            coordinates;
9433dfda0b1SToby Isaac     PetscSection   coordSection;
9443dfda0b1SToby Isaac     PetscScalar   *coords;
9453dfda0b1SToby Isaac     PetscInt       coordSize;
9463dfda0b1SToby Isaac     PetscInt       v, vx, vy, vz;
9473dfda0b1SToby Isaac     PetscInt       c, f, fx, fy, fz, e, ex, ey, ez;
9483dfda0b1SToby Isaac 
9499566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells + numFaces + numEdges + numVertices));
95048a46eb9SPierre Jolivet     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 6));
95148a46eb9SPierre Jolivet     for (f = firstXFace; f < firstXFace + numFaces; ++f) PetscCall(DMPlexSetConeSize(dm, f, 4));
95248a46eb9SPierre Jolivet     for (e = firstXEdge; e < firstXEdge + numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2));
9539566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
9543dfda0b1SToby Isaac     /* Build cells */
9553dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
9563dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
9573dfda0b1SToby Isaac         for (fx = 0; fx < numXEdges; ++fx) {
9583dfda0b1SToby Isaac           PetscInt cell  = (fz * numYEdges + fy) * numXEdges + fx;
9593dfda0b1SToby Isaac           PetscInt faceB = firstZFace + (fy * numXEdges + fx) * numZVertices + fz;
9603dfda0b1SToby Isaac           PetscInt faceT = firstZFace + (fy * numXEdges + fx) * numZVertices + ((fz + 1) % numZVertices);
9613dfda0b1SToby Isaac           PetscInt faceF = firstYFace + (fz * numXEdges + fx) * numYVertices + fy;
9623dfda0b1SToby Isaac           PetscInt faceK = firstYFace + (fz * numXEdges + fx) * numYVertices + ((fy + 1) % numYVertices);
9633dfda0b1SToby Isaac           PetscInt faceL = firstXFace + (fz * numYEdges + fy) * numXVertices + fx;
9643dfda0b1SToby Isaac           PetscInt faceR = firstXFace + (fz * numYEdges + fy) * numXVertices + ((fx + 1) % numXVertices);
9653dfda0b1SToby Isaac           /* B,  T,  F,  K,  R,  L */
966b5a892a1SMatthew G. Knepley           PetscInt ornt[6] = {-2, 0, 0, -3, 0, -2}; /* ??? */
96742206facSLisandro Dalcin           PetscInt cone[6];
9683dfda0b1SToby Isaac 
9693dfda0b1SToby Isaac           /* no boundary twisting in 3D */
9709371c9d4SSatish Balay           cone[0] = faceB;
9719371c9d4SSatish Balay           cone[1] = faceT;
9729371c9d4SSatish Balay           cone[2] = faceF;
9739371c9d4SSatish Balay           cone[3] = faceK;
9749371c9d4SSatish Balay           cone[4] = faceR;
9759371c9d4SSatish Balay           cone[5] = faceL;
9769566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, cell, cone));
9779566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, cell, ornt));
9789566063dSJacob Faibussowitsch           if (bdX != DM_BOUNDARY_NONE && fx == numXEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
9799566063dSJacob Faibussowitsch           if (bdY != DM_BOUNDARY_NONE && fy == numYEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
9809566063dSJacob Faibussowitsch           if (bdZ != DM_BOUNDARY_NONE && fz == numZEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
9813dfda0b1SToby Isaac         }
9823dfda0b1SToby Isaac       }
9833dfda0b1SToby Isaac     }
9843dfda0b1SToby Isaac     /* Build x faces */
9853dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
9863dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
9873dfda0b1SToby Isaac         for (fx = 0; fx < numXVertices; ++fx) {
9883dfda0b1SToby Isaac           PetscInt face    = firstXFace + (fz * numYEdges + fy) * numXVertices + fx;
9893dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy * numXVertices + fx) * numZEdges + fz;
9903dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (((fy + 1) % numYVertices) * numXVertices + fx) * numZEdges + fz;
9913dfda0b1SToby Isaac           PetscInt edgeB   = firstYEdge + (fz * numXVertices + fx) * numYEdges + fy;
9923dfda0b1SToby Isaac           PetscInt edgeT   = firstYEdge + (((fz + 1) % numZVertices) * numXVertices + fx) * numYEdges + fy;
993b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
9943dfda0b1SToby Isaac           PetscInt cone[4];
9953dfda0b1SToby Isaac 
9963dfda0b1SToby Isaac           if (dim == 3) {
9973dfda0b1SToby Isaac             /* markers */
9983dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
9993dfda0b1SToby Isaac               if (fx == numXVertices - 1) {
10009566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerRight));
10019566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerRight));
10029371c9d4SSatish Balay               } else if (fx == 0) {
10039566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerLeft));
10049566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerLeft));
10053dfda0b1SToby Isaac               }
10063dfda0b1SToby Isaac             }
10073dfda0b1SToby Isaac           }
10089371c9d4SSatish Balay           cone[0] = edgeB;
10099371c9d4SSatish Balay           cone[1] = edgeR;
10109371c9d4SSatish Balay           cone[2] = edgeT;
10119371c9d4SSatish Balay           cone[3] = edgeL;
10129566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
10139566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
10143dfda0b1SToby Isaac         }
10153dfda0b1SToby Isaac       }
10163dfda0b1SToby Isaac     }
10173dfda0b1SToby Isaac     /* Build y faces */
10183dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
101942206facSLisandro Dalcin       for (fx = 0; fx < numXEdges; ++fx) {
10203dfda0b1SToby Isaac         for (fy = 0; fy < numYVertices; ++fy) {
10213dfda0b1SToby Isaac           PetscInt face    = firstYFace + (fz * numXEdges + fx) * numYVertices + fy;
10223dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy * numXVertices + fx) * numZEdges + fz;
10233dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (fy * numXVertices + ((fx + 1) % numXVertices)) * numZEdges + fz;
10243dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz * numYVertices + fy) * numXEdges + fx;
10253dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (((fz + 1) % numZVertices) * numYVertices + fy) * numXEdges + fx;
1026b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
10273dfda0b1SToby Isaac           PetscInt cone[4];
10283dfda0b1SToby Isaac 
10293dfda0b1SToby Isaac           if (dim == 3) {
10303dfda0b1SToby Isaac             /* markers */
10313dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
10323dfda0b1SToby Isaac               if (fy == numYVertices - 1) {
10339566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerBack));
10349566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerBack));
10359371c9d4SSatish Balay               } else if (fy == 0) {
10369566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerFront));
10379566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerFront));
10383dfda0b1SToby Isaac               }
10393dfda0b1SToby Isaac             }
10403dfda0b1SToby Isaac           }
10419371c9d4SSatish Balay           cone[0] = edgeB;
10429371c9d4SSatish Balay           cone[1] = edgeR;
10439371c9d4SSatish Balay           cone[2] = edgeT;
10449371c9d4SSatish Balay           cone[3] = edgeL;
10459566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
10469566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
10473dfda0b1SToby Isaac         }
10483dfda0b1SToby Isaac       }
10493dfda0b1SToby Isaac     }
10503dfda0b1SToby Isaac     /* Build z faces */
10513dfda0b1SToby Isaac     for (fy = 0; fy < numYEdges; ++fy) {
10523dfda0b1SToby Isaac       for (fx = 0; fx < numXEdges; ++fx) {
10533dfda0b1SToby Isaac         for (fz = 0; fz < numZVertices; fz++) {
10543dfda0b1SToby Isaac           PetscInt face    = firstZFace + (fy * numXEdges + fx) * numZVertices + fz;
10553dfda0b1SToby Isaac           PetscInt edgeL   = firstYEdge + (fz * numXVertices + fx) * numYEdges + fy;
10563dfda0b1SToby Isaac           PetscInt edgeR   = firstYEdge + (fz * numXVertices + ((fx + 1) % numXVertices)) * numYEdges + fy;
10573dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz * numYVertices + fy) * numXEdges + fx;
10583dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (fz * numYVertices + ((fy + 1) % numYVertices)) * numXEdges + fx;
1059b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
10603dfda0b1SToby Isaac           PetscInt cone[4];
10613dfda0b1SToby Isaac 
10623dfda0b1SToby Isaac           if (dim == 2) {
10639371c9d4SSatish Balay             if (bdX == DM_BOUNDARY_TWIST && fx == numXEdges - 1) {
10649371c9d4SSatish Balay               edgeR += numYEdges - 1 - 2 * fy;
10659371c9d4SSatish Balay               ornt[1] = -1;
10669371c9d4SSatish Balay             }
10679371c9d4SSatish Balay             if (bdY == DM_BOUNDARY_TWIST && fy == numYEdges - 1) {
10689371c9d4SSatish Balay               edgeT += numXEdges - 1 - 2 * fx;
10699371c9d4SSatish Balay               ornt[2] = 0;
10709371c9d4SSatish Balay             }
10719566063dSJacob Faibussowitsch             if (bdX != DM_BOUNDARY_NONE && fx == numXEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, face, 2));
10729566063dSJacob Faibussowitsch             if (bdY != DM_BOUNDARY_NONE && fy == numYEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, face, 2));
1073d1c88043SMatthew G. Knepley           } else {
10743dfda0b1SToby Isaac             /* markers */
10753dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
10763dfda0b1SToby Isaac               if (fz == numZVertices - 1) {
10779566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerTop));
10789566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerTop));
10799371c9d4SSatish Balay               } else if (fz == 0) {
10809566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerBottom));
10819566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerBottom));
10823dfda0b1SToby Isaac               }
10833dfda0b1SToby Isaac             }
10843dfda0b1SToby Isaac           }
10859371c9d4SSatish Balay           cone[0] = edgeB;
10869371c9d4SSatish Balay           cone[1] = edgeR;
10879371c9d4SSatish Balay           cone[2] = edgeT;
10889371c9d4SSatish Balay           cone[3] = edgeL;
10899566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
10909566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
10913dfda0b1SToby Isaac         }
10923dfda0b1SToby Isaac       }
10933dfda0b1SToby Isaac     }
10943dfda0b1SToby Isaac     /* Build Z edges*/
10953dfda0b1SToby Isaac     for (vy = 0; vy < numYVertices; vy++) {
10963dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
10973dfda0b1SToby Isaac         for (ez = 0; ez < numZEdges; ez++) {
10983dfda0b1SToby Isaac           const PetscInt edge    = firstZEdge + (vy * numXVertices + vx) * numZEdges + ez;
10993dfda0b1SToby Isaac           const PetscInt vertexB = firstVertex + (ez * numYVertices + vy) * numXVertices + vx;
11003dfda0b1SToby Isaac           const PetscInt vertexT = firstVertex + (((ez + 1) % numZVertices) * numYVertices + vy) * numXVertices + vx;
11013dfda0b1SToby Isaac           PetscInt       cone[2];
11023dfda0b1SToby Isaac 
11039371c9d4SSatish Balay           cone[0] = vertexB;
11049371c9d4SSatish Balay           cone[1] = vertexT;
1105c2df9bbfSMatthew G. Knepley           PetscCall(DMPlexSetCone(dm, edge, cone));
11063dfda0b1SToby Isaac           if (dim == 3) {
11073dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
11083dfda0b1SToby Isaac               if (vx == numXVertices - 1) {
11099566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
1110c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1111c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1112c2df9bbfSMatthew G. Knepley               } else if (vx == 0) {
11139566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
1114c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1115c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
11163dfda0b1SToby Isaac               }
11173dfda0b1SToby Isaac             }
11183dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
11193dfda0b1SToby Isaac               if (vy == numYVertices - 1) {
11209566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBack));
1121c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBack));
1122c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBack));
1123c2df9bbfSMatthew G. Knepley               } else if (vy == 0) {
11249566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerFront));
1125c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerFront));
1126c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerFront));
11273dfda0b1SToby Isaac               }
11283dfda0b1SToby Isaac             }
11293dfda0b1SToby Isaac           }
11303dfda0b1SToby Isaac         }
11313dfda0b1SToby Isaac       }
11323dfda0b1SToby Isaac     }
11333dfda0b1SToby Isaac     /* Build Y edges*/
11343dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
11353dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
11363dfda0b1SToby Isaac         for (ey = 0; ey < numYEdges; ey++) {
11373dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdY == DM_BOUNDARY_TWIST && ey == numYEdges - 1) ? (numXVertices - vx - 1) : (vz * numYVertices + ((ey + 1) % numYVertices)) * numXVertices + vx;
11383dfda0b1SToby Isaac           const PetscInt edge    = firstYEdge + (vz * numXVertices + vx) * numYEdges + ey;
11393dfda0b1SToby Isaac           const PetscInt vertexF = firstVertex + (vz * numYVertices + ey) * numXVertices + vx;
11403dfda0b1SToby Isaac           const PetscInt vertexK = firstVertex + nextv;
11413dfda0b1SToby Isaac           PetscInt       cone[2];
11423dfda0b1SToby Isaac 
11439371c9d4SSatish Balay           cone[0] = vertexF;
11449371c9d4SSatish Balay           cone[1] = vertexK;
11459566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, edge, cone));
11463dfda0b1SToby Isaac           if (dim == 2) {
11473dfda0b1SToby Isaac             if ((bdX != DM_BOUNDARY_PERIODIC) && (bdX != DM_BOUNDARY_TWIST)) {
11483dfda0b1SToby Isaac               if (vx == numXVertices - 1) {
11499566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerRight));
11509566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
11519566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1152c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1153d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
11549566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerLeft));
11559566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
11569566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1157c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
11583dfda0b1SToby Isaac               }
1159d8211ee3SMatthew G. Knepley             } else {
11604c67ea77SStefano Zampini               if (vx == 0 && cutLabel) {
11619566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, edge, 1));
11629566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, cone[0], 1));
1163c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMLabelSetValue(cutLabel, cone[1], 1));
11643dfda0b1SToby Isaac               }
1165d8211ee3SMatthew G. Knepley             }
1166d8211ee3SMatthew G. Knepley           } else {
11673dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
11683dfda0b1SToby Isaac               if (vx == numXVertices - 1) {
11699566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
1170c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1171c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1172d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
11739566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
1174c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1175c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
11763dfda0b1SToby Isaac               }
11773dfda0b1SToby Isaac             }
11783dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
11793dfda0b1SToby Isaac               if (vz == numZVertices - 1) {
11809566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
1181c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1182c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1183d8211ee3SMatthew G. Knepley               } else if (vz == 0) {
11849566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
1185c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1186c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
11873dfda0b1SToby Isaac               }
11883dfda0b1SToby Isaac             }
11893dfda0b1SToby Isaac           }
11903dfda0b1SToby Isaac         }
11913dfda0b1SToby Isaac       }
11923dfda0b1SToby Isaac     }
11933dfda0b1SToby Isaac     /* Build X edges*/
11943dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
11953dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; vy++) {
11963dfda0b1SToby Isaac         for (ex = 0; ex < numXEdges; ex++) {
11973dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdX == DM_BOUNDARY_TWIST && ex == numXEdges - 1) ? (numYVertices - vy - 1) * numXVertices : (vz * numYVertices + vy) * numXVertices + (ex + 1) % numXVertices;
11983dfda0b1SToby Isaac           const PetscInt edge    = firstXEdge + (vz * numYVertices + vy) * numXEdges + ex;
11993dfda0b1SToby Isaac           const PetscInt vertexL = firstVertex + (vz * numYVertices + vy) * numXVertices + ex;
12003dfda0b1SToby Isaac           const PetscInt vertexR = firstVertex + nextv;
12013dfda0b1SToby Isaac           PetscInt       cone[2];
12023dfda0b1SToby Isaac 
12039371c9d4SSatish Balay           cone[0] = vertexL;
12049371c9d4SSatish Balay           cone[1] = vertexR;
12059566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, edge, cone));
12063dfda0b1SToby Isaac           if (dim == 2) {
12073dfda0b1SToby Isaac             if ((bdY != DM_BOUNDARY_PERIODIC) && (bdY != DM_BOUNDARY_TWIST)) {
12083dfda0b1SToby Isaac               if (vy == numYVertices - 1) {
12099566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerTop));
12109566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
12119566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1212c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1213d8211ee3SMatthew G. Knepley               } else if (vy == 0) {
12149566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerBottom));
12159566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
12169566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1217c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
12183dfda0b1SToby Isaac               }
1219d8211ee3SMatthew G. Knepley             } else {
12204c67ea77SStefano Zampini               if (vy == 0 && cutLabel) {
12219566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, edge, 1));
12229566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, cone[0], 1));
1223c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMLabelSetValue(cutLabel, cone[1], 1));
12243dfda0b1SToby Isaac               }
1225d8211ee3SMatthew G. Knepley             }
1226d8211ee3SMatthew G. Knepley           } else {
12273dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
12283dfda0b1SToby Isaac               if (vy == numYVertices - 1) {
12299566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBack));
1230c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBack));
1231c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBack));
1232c2df9bbfSMatthew G. Knepley               } else if (vy == 0) {
12339566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerFront));
1234c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerFront));
1235c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerFront));
12363dfda0b1SToby Isaac               }
12373dfda0b1SToby Isaac             }
12383dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
12393dfda0b1SToby Isaac               if (vz == numZVertices - 1) {
12409566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
1241c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1242c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1243c2df9bbfSMatthew G. Knepley               } else if (vz == 0) {
12449566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
1245c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1246c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
12473dfda0b1SToby Isaac               }
12483dfda0b1SToby Isaac             }
12493dfda0b1SToby Isaac           }
12503dfda0b1SToby Isaac         }
12513dfda0b1SToby Isaac       }
12523dfda0b1SToby Isaac     }
12539566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
12549566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
12553dfda0b1SToby Isaac     /* Build coordinates */
12569566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
12579566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
12589566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
12599566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numVertices));
12603dfda0b1SToby Isaac     for (v = firstVertex; v < firstVertex + numVertices; ++v) {
12619566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
12629566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
12633dfda0b1SToby Isaac     }
12649566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
12659566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
12669566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
12679566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
12689566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
12699566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
12709566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates, VECSTANDARD));
12719566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
12723dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; ++vz) {
12733dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; ++vy) {
12743dfda0b1SToby Isaac         for (vx = 0; vx < numXVertices; ++vx) {
12753dfda0b1SToby Isaac           coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 0] = lower[0] + ((upper[0] - lower[0]) / numXEdges) * vx;
12763dfda0b1SToby Isaac           coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 1] = lower[1] + ((upper[1] - lower[1]) / numYEdges) * vy;
1277ad540459SPierre Jolivet           if (dim == 3) coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 2] = lower[2] + ((upper[2] - lower[2]) / numZEdges) * vz;
12783dfda0b1SToby Isaac         }
12793dfda0b1SToby Isaac       }
12803dfda0b1SToby Isaac     }
12819566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
12829566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
12839566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
12843dfda0b1SToby Isaac   }
12853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
12863dfda0b1SToby Isaac }
12873dfda0b1SToby Isaac 
1288d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxMesh_Tensor_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
1289d71ae5a4SJacob Faibussowitsch {
12909318fe57SMatthew G. Knepley   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
12919318fe57SMatthew G. Knepley   PetscInt       fac[3] = {0, 0, 0}, d;
1292552f7358SJed Brown 
1293552f7358SJed Brown   PetscFunctionBegin;
12944f572ea9SToby Isaac   PetscAssertPointer(dm, 1);
12959318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
12969566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
12979371c9d4SSatish Balay   for (d = 0; d < dim; ++d) {
12989371c9d4SSatish Balay     fac[d] = faces[d];
12999371c9d4SSatish Balay     bdt[d] = periodicity[d];
13009371c9d4SSatish Balay   }
13019566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateCubeMesh_Internal(dm, lower, upper, fac, bdt[0], bdt[1], bdt[2]));
13029371c9d4SSatish 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))) {
13036858538eSMatthew G. Knepley     PetscReal L[3]       = {-1., -1., 0.};
13046858538eSMatthew G. Knepley     PetscReal maxCell[3] = {-1., -1., 0.};
1305552f7358SJed Brown 
13069318fe57SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
13076858538eSMatthew G. Knepley       if (periodicity[d] != DM_BOUNDARY_NONE) {
13089318fe57SMatthew G. Knepley         L[d]       = upper[d] - lower[d];
13099318fe57SMatthew G. Knepley         maxCell[d] = 1.1 * (L[d] / PetscMax(1, faces[d]));
1310768d5fceSMatthew G. Knepley       }
13116858538eSMatthew G. Knepley     }
13124fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, maxCell, lower, L));
1313768d5fceSMatthew G. Knepley   }
13149566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
13153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13169318fe57SMatthew G. Knepley }
13179318fe57SMatthew G. Knepley 
13185dca41c3SJed 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)
1319d71ae5a4SJacob Faibussowitsch {
13209318fe57SMatthew G. Knepley   PetscFunctionBegin;
132146139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
13225dca41c3SJed Brown   if (shape == DM_SHAPE_ZBOX) PetscCall(DMPlexCreateBoxMesh_Tensor_SFC_Internal(dm, dim, faces, lower, upper, periodicity, interpolate));
13236725e60dSJed Brown   else if (dim == 1) PetscCall(DMPlexCreateLineMesh_Internal(dm, faces[0], lower[0], upper[0], periodicity[0]));
13249566063dSJacob Faibussowitsch   else if (simplex) PetscCall(DMPlexCreateBoxMesh_Simplex_Internal(dm, dim, faces, lower, upper, periodicity, interpolate));
13259566063dSJacob Faibussowitsch   else PetscCall(DMPlexCreateBoxMesh_Tensor_Internal(dm, dim, faces, lower, upper, periodicity));
13269318fe57SMatthew G. Knepley   if (!interpolate && dim > 1 && !simplex) {
1327768d5fceSMatthew G. Knepley     DM udm;
1328768d5fceSMatthew G. Knepley 
13299566063dSJacob Faibussowitsch     PetscCall(DMPlexUninterpolate(dm, &udm));
13309566063dSJacob Faibussowitsch     PetscCall(DMPlexCopyCoordinates(dm, udm));
133169d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &udm));
1332768d5fceSMatthew G. Knepley   }
133346139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
13343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1335c8c68bd8SToby Isaac }
1336c8c68bd8SToby Isaac 
1337768d5fceSMatthew G. Knepley /*@C
1338768d5fceSMatthew G. Knepley   DMPlexCreateBoxMesh - Creates a mesh on the tensor product of unit intervals (box) using simplices or tensor cells (hexahedra).
1339768d5fceSMatthew G. Knepley 
1340d083f849SBarry Smith   Collective
1341768d5fceSMatthew G. Knepley 
1342768d5fceSMatthew G. Knepley   Input Parameters:
1343a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
1344768d5fceSMatthew G. Knepley . dim         - The spatial dimension
1345a1cb98faSBarry Smith . simplex     - `PETSC_TRUE` for simplices, `PETSC_FALSE` for tensor cells
134620f4b53cSBarry Smith . faces       - Number of faces per dimension, or `NULL` for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
134720f4b53cSBarry Smith . lower       - The lower left corner, or `NULL` for (0, 0, 0)
134820f4b53cSBarry Smith . upper       - The upper right corner, or `NULL` for (1, 1, 1)
134920f4b53cSBarry Smith . periodicity - The boundary type for the X,Y,Z direction, or `NULL` for `DM_BOUNDARY_NONE`
1350768d5fceSMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
1351768d5fceSMatthew G. Knepley 
1352768d5fceSMatthew G. Knepley   Output Parameter:
1353a1cb98faSBarry Smith . dm - The `DM` object
1354768d5fceSMatthew G. Knepley 
1355768d5fceSMatthew G. Knepley   Level: beginner
1356768d5fceSMatthew G. Knepley 
1357a1cb98faSBarry Smith   Note:
1358a1cb98faSBarry Smith   To customize this mesh using options, use
1359a1cb98faSBarry Smith .vb
1360a1cb98faSBarry Smith   DMCreate(comm, &dm);
1361a1cb98faSBarry Smith   DMSetType(dm, DMPLEX);
1362a1cb98faSBarry Smith   DMSetFromOptions(dm);
1363a1cb98faSBarry Smith .ve
1364a1cb98faSBarry Smith   and use the options in `DMSetFromOptions()`.
1365a1cb98faSBarry Smith 
1366a4e35b19SJacob Faibussowitsch   Here is the numbering returned for 2 faces in each direction for tensor cells\:
1367a1cb98faSBarry Smith .vb
1368a1cb98faSBarry Smith  10---17---11---18----12
1369a1cb98faSBarry Smith   |         |         |
1370a1cb98faSBarry Smith   |         |         |
1371a1cb98faSBarry Smith  20    2   22    3    24
1372a1cb98faSBarry Smith   |         |         |
1373a1cb98faSBarry Smith   |         |         |
1374a1cb98faSBarry Smith   7---15----8---16----9
1375a1cb98faSBarry Smith   |         |         |
1376a1cb98faSBarry Smith   |         |         |
1377a1cb98faSBarry Smith  19    0   21    1   23
1378a1cb98faSBarry Smith   |         |         |
1379a1cb98faSBarry Smith   |         |         |
1380a1cb98faSBarry Smith   4---13----5---14----6
1381a1cb98faSBarry Smith .ve
1382a1cb98faSBarry Smith   and for simplicial cells
1383a1cb98faSBarry Smith .vb
1384a1cb98faSBarry Smith  14----8---15----9----16
1385a1cb98faSBarry Smith   |\     5  |\      7 |
1386a1cb98faSBarry Smith   | \       | \       |
1387a1cb98faSBarry Smith  13   2    14    3    15
1388a1cb98faSBarry Smith   | 4   \   | 6   \   |
1389a1cb98faSBarry Smith   |       \ |       \ |
1390a1cb98faSBarry Smith  11----6---12----7----13
1391a1cb98faSBarry Smith   |\        |\        |
1392a1cb98faSBarry Smith   | \    1  | \     3 |
1393a1cb98faSBarry Smith  10   0    11    1    12
1394a1cb98faSBarry Smith   | 0   \   | 2   \   |
1395a1cb98faSBarry Smith   |       \ |       \ |
1396a1cb98faSBarry Smith   8----4----9----5----10
1397a1cb98faSBarry Smith .ve
1398a1cb98faSBarry Smith 
13991cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMSetFromOptions()`, `DMPlexCreateFromFile()`, `DMPlexCreateHexCylinderMesh()`, `DMSetType()`, `DMCreate()`
1400768d5fceSMatthew G. Knepley @*/
1401d71ae5a4SJacob 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)
1402d71ae5a4SJacob Faibussowitsch {
14039318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
1404fdbf62faSLisandro Dalcin   PetscReal      low[3] = {0, 0, 0};
1405fdbf62faSLisandro Dalcin   PetscReal      upp[3] = {1, 1, 1};
1406fdbf62faSLisandro Dalcin   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
1407552f7358SJed Brown 
1408768d5fceSMatthew G. Knepley   PetscFunctionBegin;
14099566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
14109566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
14115dca41c3SJed Brown   PetscCall(DMPlexCreateBoxMesh_Internal(*dm, DM_SHAPE_BOX, dim, simplex, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt, interpolate));
14127ff04441SMatthew G. Knepley   if (periodicity) PetscCall(DMLocalizeCoordinates(*dm));
14133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
14149318fe57SMatthew G. Knepley }
1415fdbf62faSLisandro Dalcin 
1416d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateWedgeBoxMesh_Internal(DM dm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
1417d71ae5a4SJacob Faibussowitsch {
14189318fe57SMatthew G. Knepley   DM       bdm, vol;
14199318fe57SMatthew G. Knepley   PetscInt i;
14209318fe57SMatthew G. Knepley 
14219318fe57SMatthew G. Knepley   PetscFunctionBegin;
14221fcf445aSMatthew G. Knepley   // TODO Now we can support periodicity
142308401ef6SPierre Jolivet   for (i = 0; i < 3; ++i) PetscCheck(periodicity[i] == DM_BOUNDARY_NONE, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Periodicity not yet supported");
14249566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &bdm));
14259566063dSJacob Faibussowitsch   PetscCall(DMSetType(bdm, DMPLEX));
14269566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(bdm, 2));
142746139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, bdm, 0, 0, 0));
14289566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxMesh_Simplex_Internal(bdm, 2, faces, lower, upper, periodicity, PETSC_TRUE));
14291fcf445aSMatthew G. Knepley   PetscCall(DMPlexExtrude(bdm, faces[2], upper[2] - lower[2], PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, NULL, NULL, &vol));
143046139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, bdm, 0, 0, 0));
14319566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&bdm));
143269d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &vol));
14339318fe57SMatthew G. Knepley   if (lower[2] != 0.0) {
14349318fe57SMatthew G. Knepley     Vec          v;
14359318fe57SMatthew G. Knepley     PetscScalar *x;
14369318fe57SMatthew G. Knepley     PetscInt     cDim, n;
14379318fe57SMatthew G. Knepley 
14389566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &v));
14399566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(v, &cDim));
14409566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(v, &n));
14419566063dSJacob Faibussowitsch     PetscCall(VecGetArray(v, &x));
14429318fe57SMatthew G. Knepley     x += cDim;
14439318fe57SMatthew G. Knepley     for (i = 0; i < n; i += cDim) x[i] += lower[2];
14449566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(v, &x));
14459566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, v));
14469318fe57SMatthew G. Knepley   }
14473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1448552f7358SJed Brown }
1449552f7358SJed Brown 
145000dabe28SStefano Zampini /*@
145139f4f5dbSPierre Jolivet   DMPlexCreateWedgeBoxMesh - Creates a 3-D mesh tessellating the (x,y) plane and extruding in the third direction using wedge cells.
145200dabe28SStefano Zampini 
1453d083f849SBarry Smith   Collective
145400dabe28SStefano Zampini 
145500dabe28SStefano Zampini   Input Parameters:
1456a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
145720f4b53cSBarry Smith . faces       - Number of faces per dimension, or `NULL` for (1, 1, 1)
145820f4b53cSBarry Smith . lower       - The lower left corner, or `NULL` for (0, 0, 0)
145920f4b53cSBarry Smith . upper       - The upper right corner, or `NULL` for (1, 1, 1)
146020f4b53cSBarry Smith . periodicity - The boundary type for the X,Y,Z direction, or `NULL` for `DM_BOUNDARY_NONE`
1461a1cb98faSBarry Smith . orderHeight - If `PETSC_TRUE`, orders the extruded cells in the height first. Otherwise, orders the cell on the layers first
146200dabe28SStefano Zampini - interpolate - Flag to create intermediate mesh pieces (edges, faces)
146300dabe28SStefano Zampini 
146400dabe28SStefano Zampini   Output Parameter:
1465a1cb98faSBarry Smith . dm - The `DM` object
146600dabe28SStefano Zampini 
146700dabe28SStefano Zampini   Level: beginner
146800dabe28SStefano Zampini 
14691cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateHexCylinderMesh()`, `DMPlexCreateWedgeCylinderMesh()`, `DMExtrude()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
147000dabe28SStefano Zampini @*/
1471d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateWedgeBoxMesh(MPI_Comm comm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool orderHeight, PetscBool interpolate, DM *dm)
1472d71ae5a4SJacob Faibussowitsch {
14739318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
147400dabe28SStefano Zampini   PetscReal      low[3] = {0, 0, 0};
147500dabe28SStefano Zampini   PetscReal      upp[3] = {1, 1, 1};
147600dabe28SStefano Zampini   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
147700dabe28SStefano Zampini 
147800dabe28SStefano Zampini   PetscFunctionBegin;
14799566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
14809566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
14819566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateWedgeBoxMesh_Internal(*dm, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt));
1482d410b0cfSMatthew G. Knepley   if (!interpolate) {
1483d410b0cfSMatthew G. Knepley     DM udm;
148400dabe28SStefano Zampini 
14859566063dSJacob Faibussowitsch     PetscCall(DMPlexUninterpolate(*dm, &udm));
148669d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(*dm, &udm));
148700dabe28SStefano Zampini   }
14887ff04441SMatthew G. Knepley   if (periodicity) PetscCall(DMLocalizeCoordinates(*dm));
14893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
149000dabe28SStefano Zampini }
149100dabe28SStefano Zampini 
1492cfb853baSMatthew G. Knepley /*
1493cfb853baSMatthew 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.
1494cfb853baSMatthew G. Knepley 
1495cfb853baSMatthew G. Knepley   Input Parameters:
1496cfb853baSMatthew G. Knepley + len - The length of the tuple
1497cfb853baSMatthew G. Knepley . max - The maximum for each dimension, so values are in [0, max)
1498cfb853baSMatthew G. Knepley - tup - A tuple of length len+1: tup[len] > 0 indicates a stopping condition
1499cfb853baSMatthew G. Knepley 
1500cfb853baSMatthew G. Knepley   Output Parameter:
150120f4b53cSBarry Smith . tup - A tuple of `len` integers whose entries are at most `max`
1502cfb853baSMatthew G. Knepley 
1503cfb853baSMatthew G. Knepley   Level: developer
1504cfb853baSMatthew G. Knepley 
150520f4b53cSBarry Smith   Note:
150620f4b53cSBarry Smith   Ordering is lexicographic with lowest index as least significant in ordering.
150720f4b53cSBarry 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}.
150820f4b53cSBarry Smith 
1509cfb853baSMatthew G. Knepley .seealso: PetscDualSpaceTensorPointLexicographic_Internal(), PetscDualSpaceLatticePointLexicographic_Internal()
1510cfb853baSMatthew G. Knepley */
1511cfb853baSMatthew G. Knepley static PetscErrorCode DMPlexTensorPointLexicographic_Private(PetscInt len, const PetscInt max[], PetscInt tup[])
1512cfb853baSMatthew G. Knepley {
1513cfb853baSMatthew G. Knepley   PetscInt i;
1514cfb853baSMatthew G. Knepley 
1515cfb853baSMatthew G. Knepley   PetscFunctionBegin;
1516cfb853baSMatthew G. Knepley   for (i = 0; i < len; ++i) {
1517cfb853baSMatthew G. Knepley     if (tup[i] < max[i] - 1) {
1518cfb853baSMatthew G. Knepley       break;
1519cfb853baSMatthew G. Knepley     } else {
1520cfb853baSMatthew G. Knepley       tup[i] = 0;
1521cfb853baSMatthew G. Knepley     }
1522cfb853baSMatthew G. Knepley   }
1523cfb853baSMatthew G. Knepley   if (i == len) tup[i - 1] = max[i - 1];
1524cfb853baSMatthew G. Knepley   else ++tup[i];
15253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1526cfb853baSMatthew G. Knepley }
1527cfb853baSMatthew G. Knepley 
1528cfb853baSMatthew G. Knepley static PetscInt TupleToIndex_Private(PetscInt len, const PetscInt max[], const PetscInt tup[])
1529cfb853baSMatthew G. Knepley {
1530cfb853baSMatthew G. Knepley   PetscInt i, idx = tup[len - 1];
1531cfb853baSMatthew G. Knepley 
1532cfb853baSMatthew G. Knepley   for (i = len - 2; i >= 0; --i) {
1533cfb853baSMatthew G. Knepley     idx *= max[i];
1534cfb853baSMatthew G. Knepley     idx += tup[i];
1535cfb853baSMatthew G. Knepley   }
1536cfb853baSMatthew G. Knepley   return idx;
1537cfb853baSMatthew G. Knepley }
1538cfb853baSMatthew G. Knepley 
1539cfb853baSMatthew G. Knepley static PetscErrorCode DestroyExtent_Private(void *extent)
1540cfb853baSMatthew G. Knepley {
1541cfb853baSMatthew G. Knepley   return PetscFree(extent);
1542cfb853baSMatthew G. Knepley }
1543cfb853baSMatthew G. Knepley 
1544cfb853baSMatthew G. Knepley static PetscErrorCode DMPlexCreateHypercubicMesh_Internal(DM dm, PetscInt dim, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], const DMBoundaryType bd[])
1545cfb853baSMatthew G. Knepley {
1546cfb853baSMatthew G. Knepley   Vec          coordinates;
1547cfb853baSMatthew G. Knepley   PetscSection coordSection;
1548cfb853baSMatthew G. Knepley   DMLabel      cutLabel    = NULL;
1549cfb853baSMatthew G. Knepley   PetscBool    cutMarker   = PETSC_FALSE;
1550cfb853baSMatthew G. Knepley   PetscBool    periodic    = PETSC_FALSE;
1551cfb853baSMatthew G. Knepley   PetscInt     numCells    = 1, c;
1552cfb853baSMatthew G. Knepley   PetscInt     numVertices = 1, v;
1553cfb853baSMatthew G. Knepley   PetscScalar *coords;
1554cfb853baSMatthew G. Knepley   PetscInt    *vertices, *vert, *vtmp, *supp, cone[2];
1555cfb853baSMatthew G. Knepley   PetscInt     d, e, cell = 0, coordSize;
1556cfb853baSMatthew G. Knepley   PetscMPIInt  rank;
1557cfb853baSMatthew G. Knepley 
1558cfb853baSMatthew G. Knepley   PetscFunctionBegin;
1559cfb853baSMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
1560cfb853baSMatthew G. Knepley   PetscCall(DMSetDimension(dm, dim));
1561cfb853baSMatthew G. Knepley   PetscCall(PetscCalloc4(dim, &vertices, dim, &vert, dim, &vtmp, 2 * dim, &supp));
1562cfb853baSMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "marker"));
1563cfb853baSMatthew G. Knepley   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL));
1564cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) periodic = (periodic || bd[d] == DM_BOUNDARY_PERIODIC) ? PETSC_TRUE : PETSC_FALSE;
1565cfb853baSMatthew G. Knepley   if (periodic && cutMarker) {
1566cfb853baSMatthew G. Knepley     PetscCall(DMCreateLabel(dm, "periodic_cut"));
1567cfb853baSMatthew G. Knepley     PetscCall(DMGetLabel(dm, "periodic_cut", &cutLabel));
1568cfb853baSMatthew G. Knepley   }
1569cfb853baSMatthew 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");
1570cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) {
1571cfb853baSMatthew G. Knepley     vertices[d] = edges[d];
1572cfb853baSMatthew G. Knepley     numVertices *= vertices[d];
1573cfb853baSMatthew G. Knepley   }
1574cfb853baSMatthew G. Knepley   numCells = numVertices * dim;
1575cfb853baSMatthew G. Knepley   PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
1576cfb853baSMatthew G. Knepley   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, 2));
1577cfb853baSMatthew G. Knepley   for (v = numCells; v < numCells + numVertices; ++v) PetscCall(DMPlexSetSupportSize(dm, v, 2 * dim));
1578cfb853baSMatthew G. Knepley   /* TODO Loop over boundary and reset support sizes */
1579cfb853baSMatthew G. Knepley   PetscCall(DMSetUp(dm)); /* Allocate space for cones and supports */
1580cfb853baSMatthew G. Knepley   /* Build cell cones and vertex supports */
1581cfb853baSMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "celltype"));
1582cfb853baSMatthew G. Knepley   while (vert[dim - 1] < vertices[dim - 1]) {
1583cfb853baSMatthew G. Knepley     const PetscInt vertex = TupleToIndex_Private(dim, vertices, vert) + numCells;
1584cfb853baSMatthew G. Knepley     PetscInt       s      = 0;
1585cfb853baSMatthew G. Knepley 
15863ba16761SJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "Vertex %" PetscInt_FMT ":", vertex));
15873ba16761SJacob Faibussowitsch     for (d = 0; d < dim; ++d) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, vert[d]));
15883ba16761SJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
1589cfb853baSMatthew G. Knepley     PetscCall(DMPlexSetCellType(dm, vertex, DM_POLYTOPE_POINT));
1590cfb853baSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
1591cfb853baSMatthew G. Knepley       for (e = 0; e < dim; ++e) vtmp[e] = vert[e];
1592cfb853baSMatthew G. Knepley       vtmp[d] = (vert[d] + 1) % vertices[d];
1593cfb853baSMatthew G. Knepley       cone[0] = vertex;
1594cfb853baSMatthew G. Knepley       cone[1] = TupleToIndex_Private(dim, vertices, vtmp) + numCells;
15953ba16761SJacob Faibussowitsch       PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Vertex %" PetscInt_FMT ":", cone[1]));
15963ba16761SJacob Faibussowitsch       for (e = 0; e < dim; ++e) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, vtmp[e]));
15973ba16761SJacob Faibussowitsch       PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
1598cfb853baSMatthew G. Knepley       PetscCall(DMPlexSetCone(dm, cell, cone));
1599cfb853baSMatthew G. Knepley       PetscCall(DMPlexSetCellType(dm, cell, DM_POLYTOPE_SEGMENT));
16003ba16761SJacob Faibussowitsch       PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Edge %" PetscInt_FMT " (%" PetscInt_FMT " %" PetscInt_FMT ")\n", cell, cone[0], cone[1]));
1601cfb853baSMatthew G. Knepley       ++cell;
1602cfb853baSMatthew G. Knepley     }
1603cfb853baSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
1604cfb853baSMatthew G. Knepley       for (e = 0; e < dim; ++e) vtmp[e] = vert[e];
1605cfb853baSMatthew G. Knepley       vtmp[d]   = (vert[d] + vertices[d] - 1) % vertices[d];
1606cfb853baSMatthew G. Knepley       supp[s++] = TupleToIndex_Private(dim, vertices, vtmp) * dim + d;
1607cfb853baSMatthew G. Knepley       supp[s++] = (vertex - numCells) * dim + d;
1608cfb853baSMatthew G. Knepley       PetscCall(DMPlexSetSupport(dm, vertex, supp));
1609cfb853baSMatthew G. Knepley     }
1610cfb853baSMatthew G. Knepley     PetscCall(DMPlexTensorPointLexicographic_Private(dim, vertices, vert));
1611cfb853baSMatthew G. Knepley   }
1612cfb853baSMatthew G. Knepley   PetscCall(DMPlexStratify(dm));
1613cfb853baSMatthew G. Knepley   /* Build coordinates */
1614cfb853baSMatthew G. Knepley   PetscCall(DMGetCoordinateSection(dm, &coordSection));
1615cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetNumFields(coordSection, 1));
1616cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
1617cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices));
1618cfb853baSMatthew G. Knepley   for (v = numCells; v < numCells + numVertices; ++v) {
1619cfb853baSMatthew G. Knepley     PetscCall(PetscSectionSetDof(coordSection, v, dim));
1620cfb853baSMatthew G. Knepley     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
1621cfb853baSMatthew G. Knepley   }
1622cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetUp(coordSection));
1623cfb853baSMatthew G. Knepley   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
1624cfb853baSMatthew G. Knepley   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
1625cfb853baSMatthew G. Knepley   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
1626cfb853baSMatthew G. Knepley   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
1627cfb853baSMatthew G. Knepley   PetscCall(VecSetBlockSize(coordinates, dim));
1628cfb853baSMatthew G. Knepley   PetscCall(VecSetType(coordinates, VECSTANDARD));
1629cfb853baSMatthew G. Knepley   PetscCall(VecGetArray(coordinates, &coords));
1630cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) vert[d] = 0;
1631cfb853baSMatthew G. Knepley   while (vert[dim - 1] < vertices[dim - 1]) {
1632cfb853baSMatthew G. Knepley     const PetscInt vertex = TupleToIndex_Private(dim, vertices, vert);
1633cfb853baSMatthew G. Knepley 
1634cfb853baSMatthew G. Knepley     for (d = 0; d < dim; ++d) coords[vertex * dim + d] = lower[d] + ((upper[d] - lower[d]) / vertices[d]) * vert[d];
16353ba16761SJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "Vertex %" PetscInt_FMT ":", vertex));
16363ba16761SJacob Faibussowitsch     for (d = 0; d < dim; ++d) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, vert[d]));
16373ba16761SJacob Faibussowitsch     for (d = 0; d < dim; ++d) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %g", (double)PetscRealPart(coords[vertex * dim + d])));
16383ba16761SJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
1639cfb853baSMatthew G. Knepley     PetscCall(DMPlexTensorPointLexicographic_Private(dim, vertices, vert));
1640cfb853baSMatthew G. Knepley   }
1641cfb853baSMatthew G. Knepley   PetscCall(VecRestoreArray(coordinates, &coords));
1642cfb853baSMatthew G. Knepley   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
1643cfb853baSMatthew G. Knepley   PetscCall(VecDestroy(&coordinates));
1644cfb853baSMatthew G. Knepley   PetscCall(PetscFree4(vertices, vert, vtmp, supp));
1645cfb853baSMatthew G. Knepley   //PetscCall(DMSetPeriodicity(dm, NULL, lower, upper));
1646cfb853baSMatthew G. Knepley   // Attach the extent
1647cfb853baSMatthew G. Knepley   {
1648cfb853baSMatthew G. Knepley     PetscContainer c;
1649cfb853baSMatthew G. Knepley     PetscInt      *extent;
1650cfb853baSMatthew G. Knepley 
1651cfb853baSMatthew G. Knepley     PetscCall(PetscMalloc1(dim, &extent));
1652cfb853baSMatthew G. Knepley     for (PetscInt d = 0; d < dim; ++d) extent[d] = edges[d];
1653cfb853baSMatthew G. Knepley     PetscCall(PetscContainerCreate(PETSC_COMM_SELF, &c));
1654cfb853baSMatthew G. Knepley     PetscCall(PetscContainerSetUserDestroy(c, DestroyExtent_Private));
1655cfb853baSMatthew G. Knepley     PetscCall(PetscContainerSetPointer(c, extent));
1656cfb853baSMatthew G. Knepley     PetscCall(PetscObjectCompose((PetscObject)dm, "_extent", (PetscObject)c));
1657cfb853baSMatthew G. Knepley     PetscCall(PetscContainerDestroy(&c));
1658cfb853baSMatthew G. Knepley   }
16593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1660cfb853baSMatthew G. Knepley }
1661cfb853baSMatthew G. Knepley 
1662cfb853baSMatthew G. Knepley /*@C
1663aaa8cc7dSPierre Jolivet   DMPlexCreateHypercubicMesh - Creates a periodic mesh on the tensor product of unit intervals using only vertices and edges.
1664cfb853baSMatthew G. Knepley 
1665cfb853baSMatthew G. Knepley   Collective
1666cfb853baSMatthew G. Knepley 
1667cfb853baSMatthew G. Knepley   Input Parameters:
1668cfb853baSMatthew G. Knepley + comm  - The communicator for the DM object
1669cfb853baSMatthew G. Knepley . dim   - The spatial dimension
167020f4b53cSBarry Smith . edges - Number of edges per dimension, or `NULL` for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
167120f4b53cSBarry Smith . lower - The lower left corner, or `NULL` for (0, 0, 0)
167220f4b53cSBarry Smith - upper - The upper right corner, or `NULL` for (1, 1, 1)
1673cfb853baSMatthew G. Knepley 
1674cfb853baSMatthew G. Knepley   Output Parameter:
1675cfb853baSMatthew G. Knepley . dm - The DM object
1676cfb853baSMatthew G. Knepley 
167720f4b53cSBarry Smith   Level: beginner
167820f4b53cSBarry Smith 
167920f4b53cSBarry Smith   Note:
168020f4b53cSBarry Smith   If you want to customize this mesh using options, you just need to
168120f4b53cSBarry Smith .vb
168220f4b53cSBarry Smith   DMCreate(comm, &dm);
168320f4b53cSBarry Smith   DMSetType(dm, DMPLEX);
168420f4b53cSBarry Smith   DMSetFromOptions(dm);
168520f4b53cSBarry Smith .ve
168620f4b53cSBarry Smith   and use the options on the `DMSetFromOptions()` page.
1687cfb853baSMatthew G. Knepley 
1688cfb853baSMatthew G. Knepley   The vertices are numbered is lexicographic order, and the dim edges exiting a vertex in the positive orthant are number consecutively,
168920f4b53cSBarry Smith .vb
169020f4b53cSBarry Smith  18--0-19--2-20--4-18
169120f4b53cSBarry Smith   |     |     |     |
169220f4b53cSBarry Smith  13    15    17    13
169320f4b53cSBarry Smith   |     |     |     |
169420f4b53cSBarry Smith  24-12-25-14-26-16-24
169520f4b53cSBarry Smith   |     |     |     |
169620f4b53cSBarry Smith   7     9    11     7
169720f4b53cSBarry Smith   |     |     |     |
169820f4b53cSBarry Smith  21--6-22--8-23-10-21
169920f4b53cSBarry Smith   |     |     |     |
170020f4b53cSBarry Smith   1     3     5     1
170120f4b53cSBarry Smith   |     |     |     |
170220f4b53cSBarry Smith  18--0-19--2-20--4-18
170320f4b53cSBarry Smith .ve
1704cfb853baSMatthew G. Knepley 
170576fbde31SPierre Jolivet .seealso: `DMSetFromOptions()`, `DMPlexCreateFromFile()`, `DMPlexCreateHexCylinderMesh()`, `DMSetType()`, `DMCreate()`
1706cfb853baSMatthew G. Knepley @*/
1707cfb853baSMatthew G. Knepley PetscErrorCode DMPlexCreateHypercubicMesh(MPI_Comm comm, PetscInt dim, const PetscInt edges[], const PetscReal lower[], const PetscReal upper[], DM *dm)
1708cfb853baSMatthew G. Knepley {
1709cfb853baSMatthew G. Knepley   PetscInt       *edg;
1710cfb853baSMatthew G. Knepley   PetscReal      *low, *upp;
1711cfb853baSMatthew G. Knepley   DMBoundaryType *bdt;
1712cfb853baSMatthew G. Knepley   PetscInt        d;
1713cfb853baSMatthew G. Knepley 
1714cfb853baSMatthew G. Knepley   PetscFunctionBegin;
1715cfb853baSMatthew G. Knepley   PetscCall(DMCreate(comm, dm));
1716cfb853baSMatthew G. Knepley   PetscCall(DMSetType(*dm, DMPLEX));
1717cfb853baSMatthew G. Knepley   PetscCall(PetscMalloc4(dim, &edg, dim, &low, dim, &upp, dim, &bdt));
1718cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) {
1719cfb853baSMatthew G. Knepley     edg[d] = edges ? edges[d] : 1;
1720cfb853baSMatthew G. Knepley     low[d] = lower ? lower[d] : 0.;
1721cfb853baSMatthew G. Knepley     upp[d] = upper ? upper[d] : 1.;
1722cfb853baSMatthew G. Knepley     bdt[d] = DM_BOUNDARY_PERIODIC;
1723cfb853baSMatthew G. Knepley   }
1724cfb853baSMatthew G. Knepley   PetscCall(DMPlexCreateHypercubicMesh_Internal(*dm, dim, low, upp, edg, bdt));
1725cfb853baSMatthew G. Knepley   PetscCall(PetscFree4(edg, low, upp, bdt));
17263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1727cfb853baSMatthew G. Knepley }
1728cfb853baSMatthew G. Knepley 
1729cc4c1da9SBarry Smith /*@
1730a1cb98faSBarry Smith   DMPlexSetOptionsPrefix - Sets the prefix used for searching for all `DM` options in the database.
1731a9074c1eSMatthew G. Knepley 
173220f4b53cSBarry Smith   Logically Collective
1733a9074c1eSMatthew G. Knepley 
1734a9074c1eSMatthew G. Knepley   Input Parameters:
173520f4b53cSBarry Smith + dm     - the `DM` context
1736a9074c1eSMatthew G. Knepley - prefix - the prefix to prepend to all option names
1737a9074c1eSMatthew G. Knepley 
1738a1cb98faSBarry Smith   Level: advanced
1739a1cb98faSBarry Smith 
1740a1cb98faSBarry Smith   Note:
1741a9074c1eSMatthew G. Knepley   A hyphen (-) must NOT be given at the beginning of the prefix name.
1742a9074c1eSMatthew G. Knepley   The first character of all runtime options is AUTOMATICALLY the hyphen.
1743a9074c1eSMatthew G. Knepley 
17441cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `SNESSetFromOptions()`
1745a9074c1eSMatthew G. Knepley @*/
1746d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetOptionsPrefix(DM dm, const char prefix[])
1747d71ae5a4SJacob Faibussowitsch {
1748a9074c1eSMatthew G. Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
1749a9074c1eSMatthew G. Knepley 
1750a9074c1eSMatthew G. Knepley   PetscFunctionBegin;
1751a9074c1eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17529566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, prefix));
17539566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)mesh->partitioner, prefix));
17543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1755a9074c1eSMatthew G. Knepley }
1756a9074c1eSMatthew G. Knepley 
17579318fe57SMatthew G. Knepley /* Remap geometry to cylinder
175861a622f3SMatthew G. Knepley    TODO: This only works for a single refinement, then it is broken
175961a622f3SMatthew G. Knepley 
17609318fe57SMatthew G. Knepley      Interior square: Linear interpolation is correct
17619318fe57SMatthew G. Knepley      The other cells all have vertices on rays from the origin. We want to uniformly expand the spacing
17629318fe57SMatthew G. Knepley      such that the last vertex is on the unit circle. So the closest and farthest vertices are at distance
17630510c589SMatthew G. Knepley 
17649318fe57SMatthew G. Knepley        phi     = arctan(y/x)
17659318fe57SMatthew G. Knepley        d_close = sqrt(1/8 + 1/4 sin^2(phi))
17669318fe57SMatthew G. Knepley        d_far   = sqrt(1/2 + sin^2(phi))
17670510c589SMatthew G. Knepley 
17689318fe57SMatthew G. Knepley      so we remap them using
17690510c589SMatthew G. Knepley 
17709318fe57SMatthew G. Knepley        x_new = x_close + (x - x_close) (1 - d_close) / (d_far - d_close)
17719318fe57SMatthew G. Knepley        y_new = y_close + (y - y_close) (1 - d_close) / (d_far - d_close)
17720510c589SMatthew G. Knepley 
17739318fe57SMatthew G. Knepley      If pi/4 < phi < 3pi/4 or -3pi/4 < phi < -pi/4, then we switch x and y.
17749318fe57SMatthew G. Knepley */
1775d71ae5a4SJacob 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[])
1776d71ae5a4SJacob Faibussowitsch {
17779318fe57SMatthew G. Knepley   const PetscReal dis = 1.0 / PetscSqrtReal(2.0);
17789318fe57SMatthew G. Knepley   const PetscReal ds2 = 0.5 * dis;
177922cc497dSMatthew G. Knepley 
17809318fe57SMatthew G. Knepley   if ((PetscAbsScalar(u[0]) <= ds2) && (PetscAbsScalar(u[1]) <= ds2)) {
17819318fe57SMatthew G. Knepley     f0[0] = u[0];
17829318fe57SMatthew G. Knepley     f0[1] = u[1];
17839318fe57SMatthew G. Knepley   } else {
17849318fe57SMatthew G. Knepley     PetscReal phi, sinp, cosp, dc, df, x, y, xc, yc;
17850510c589SMatthew G. Knepley 
17869318fe57SMatthew G. Knepley     x    = PetscRealPart(u[0]);
17879318fe57SMatthew G. Knepley     y    = PetscRealPart(u[1]);
17889318fe57SMatthew G. Knepley     phi  = PetscAtan2Real(y, x);
17899318fe57SMatthew G. Knepley     sinp = PetscSinReal(phi);
17909318fe57SMatthew G. Knepley     cosp = PetscCosReal(phi);
17919318fe57SMatthew G. Knepley     if ((PetscAbsReal(phi) > PETSC_PI / 4.0) && (PetscAbsReal(phi) < 3.0 * PETSC_PI / 4.0)) {
17929318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2 / sinp);
17939318fe57SMatthew G. Knepley       df = PetscAbsReal(dis / sinp);
17949318fe57SMatthew G. Knepley       xc = ds2 * x / PetscAbsReal(y);
17959318fe57SMatthew G. Knepley       yc = ds2 * PetscSignReal(y);
17969318fe57SMatthew G. Knepley     } else {
17979318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2 / cosp);
17989318fe57SMatthew G. Knepley       df = PetscAbsReal(dis / cosp);
17999318fe57SMatthew G. Knepley       xc = ds2 * PetscSignReal(x);
18009318fe57SMatthew G. Knepley       yc = ds2 * y / PetscAbsReal(x);
18019318fe57SMatthew G. Knepley     }
18029318fe57SMatthew G. Knepley     f0[0] = xc + (u[0] - xc) * (1.0 - dc) / (df - dc);
18039318fe57SMatthew G. Knepley     f0[1] = yc + (u[1] - yc) * (1.0 - dc) / (df - dc);
18049318fe57SMatthew G. Knepley   }
18059318fe57SMatthew G. Knepley   f0[2] = u[2];
18069318fe57SMatthew G. Knepley }
18070510c589SMatthew G. Knepley 
1808*49704ca5SMatthew G. Knepley static PetscErrorCode DMPlexCreateHexCylinderMesh_Internal(DM dm, DMBoundaryType periodicZ, PetscInt Nr)
1809d71ae5a4SJacob Faibussowitsch {
18100510c589SMatthew G. Knepley   const PetscInt dim = 3;
18119318fe57SMatthew G. Knepley   PetscInt       numCells, numVertices;
1812d8c47e87SMatthew G. Knepley   PetscMPIInt    rank;
18130510c589SMatthew G. Knepley 
18140510c589SMatthew G. Knepley   PetscFunctionBegin;
181546139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
18169566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
18179566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
18180510c589SMatthew G. Knepley   /* Create topology */
18190510c589SMatthew G. Knepley   {
18200510c589SMatthew G. Knepley     PetscInt cone[8], c;
18210510c589SMatthew G. Knepley 
1822dd400576SPatrick Sanan     numCells    = rank == 0 ? 5 : 0;
1823dd400576SPatrick Sanan     numVertices = rank == 0 ? 16 : 0;
1824006a8963SMatthew G. Knepley     if (periodicZ == DM_BOUNDARY_PERIODIC) {
1825ae8bcbbbSMatthew G. Knepley       numCells *= 3;
1826dd400576SPatrick Sanan       numVertices = rank == 0 ? 24 : 0;
1827006a8963SMatthew G. Knepley     }
18289566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
18299566063dSJacob Faibussowitsch     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 8));
18309566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm));
1831dd400576SPatrick Sanan     if (rank == 0) {
1832006a8963SMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
18339371c9d4SSatish Balay         cone[0] = 15;
18349371c9d4SSatish Balay         cone[1] = 18;
18359371c9d4SSatish Balay         cone[2] = 17;
18369371c9d4SSatish Balay         cone[3] = 16;
18379371c9d4SSatish Balay         cone[4] = 31;
18389371c9d4SSatish Balay         cone[5] = 32;
18399371c9d4SSatish Balay         cone[6] = 33;
18409371c9d4SSatish Balay         cone[7] = 34;
18419566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
18429371c9d4SSatish Balay         cone[0] = 16;
18439371c9d4SSatish Balay         cone[1] = 17;
18449371c9d4SSatish Balay         cone[2] = 24;
18459371c9d4SSatish Balay         cone[3] = 23;
18469371c9d4SSatish Balay         cone[4] = 32;
18479371c9d4SSatish Balay         cone[5] = 36;
18489371c9d4SSatish Balay         cone[6] = 37;
18499371c9d4SSatish Balay         cone[7] = 33; /* 22 25 26 21 */
18509566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
18519371c9d4SSatish Balay         cone[0] = 18;
18529371c9d4SSatish Balay         cone[1] = 27;
18539371c9d4SSatish Balay         cone[2] = 24;
18549371c9d4SSatish Balay         cone[3] = 17;
18559371c9d4SSatish Balay         cone[4] = 34;
18569371c9d4SSatish Balay         cone[5] = 33;
18579371c9d4SSatish Balay         cone[6] = 37;
18589371c9d4SSatish Balay         cone[7] = 38;
18599566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
18609371c9d4SSatish Balay         cone[0] = 29;
18619371c9d4SSatish Balay         cone[1] = 27;
18629371c9d4SSatish Balay         cone[2] = 18;
18639371c9d4SSatish Balay         cone[3] = 15;
18649371c9d4SSatish Balay         cone[4] = 35;
18659371c9d4SSatish Balay         cone[5] = 31;
18669371c9d4SSatish Balay         cone[6] = 34;
18679371c9d4SSatish Balay         cone[7] = 38;
18689566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
18699371c9d4SSatish Balay         cone[0] = 29;
18709371c9d4SSatish Balay         cone[1] = 15;
18719371c9d4SSatish Balay         cone[2] = 16;
18729371c9d4SSatish Balay         cone[3] = 23;
18739371c9d4SSatish Balay         cone[4] = 35;
18749371c9d4SSatish Balay         cone[5] = 36;
18759371c9d4SSatish Balay         cone[6] = 32;
18769371c9d4SSatish Balay         cone[7] = 31;
18779566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
1878006a8963SMatthew G. Knepley 
18799371c9d4SSatish Balay         cone[0] = 31;
18809371c9d4SSatish Balay         cone[1] = 34;
18819371c9d4SSatish Balay         cone[2] = 33;
18829371c9d4SSatish Balay         cone[3] = 32;
18839371c9d4SSatish Balay         cone[4] = 19;
18849371c9d4SSatish Balay         cone[5] = 22;
18859371c9d4SSatish Balay         cone[6] = 21;
18869371c9d4SSatish Balay         cone[7] = 20;
18879566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 5, cone));
18889371c9d4SSatish Balay         cone[0] = 32;
18899371c9d4SSatish Balay         cone[1] = 33;
18909371c9d4SSatish Balay         cone[2] = 37;
18919371c9d4SSatish Balay         cone[3] = 36;
18929371c9d4SSatish Balay         cone[4] = 22;
18939371c9d4SSatish Balay         cone[5] = 25;
18949371c9d4SSatish Balay         cone[6] = 26;
18959371c9d4SSatish Balay         cone[7] = 21;
18969566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 6, cone));
18979371c9d4SSatish Balay         cone[0] = 34;
18989371c9d4SSatish Balay         cone[1] = 38;
18999371c9d4SSatish Balay         cone[2] = 37;
19009371c9d4SSatish Balay         cone[3] = 33;
19019371c9d4SSatish Balay         cone[4] = 20;
19029371c9d4SSatish Balay         cone[5] = 21;
19039371c9d4SSatish Balay         cone[6] = 26;
19049371c9d4SSatish Balay         cone[7] = 28;
19059566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 7, cone));
19069371c9d4SSatish Balay         cone[0] = 35;
19079371c9d4SSatish Balay         cone[1] = 38;
19089371c9d4SSatish Balay         cone[2] = 34;
19099371c9d4SSatish Balay         cone[3] = 31;
19109371c9d4SSatish Balay         cone[4] = 30;
19119371c9d4SSatish Balay         cone[5] = 19;
19129371c9d4SSatish Balay         cone[6] = 20;
19139371c9d4SSatish Balay         cone[7] = 28;
19149566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 8, cone));
19159371c9d4SSatish Balay         cone[0] = 35;
19169371c9d4SSatish Balay         cone[1] = 31;
19179371c9d4SSatish Balay         cone[2] = 32;
19189371c9d4SSatish Balay         cone[3] = 36;
19199371c9d4SSatish Balay         cone[4] = 30;
19209371c9d4SSatish Balay         cone[5] = 25;
19219371c9d4SSatish Balay         cone[6] = 22;
19229371c9d4SSatish Balay         cone[7] = 19;
19239566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 9, cone));
1924ae8bcbbbSMatthew G. Knepley 
19259371c9d4SSatish Balay         cone[0] = 19;
19269371c9d4SSatish Balay         cone[1] = 20;
19279371c9d4SSatish Balay         cone[2] = 21;
19289371c9d4SSatish Balay         cone[3] = 22;
19299371c9d4SSatish Balay         cone[4] = 15;
19309371c9d4SSatish Balay         cone[5] = 16;
19319371c9d4SSatish Balay         cone[6] = 17;
19329371c9d4SSatish Balay         cone[7] = 18;
19339566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 10, cone));
19349371c9d4SSatish Balay         cone[0] = 22;
19359371c9d4SSatish Balay         cone[1] = 21;
19369371c9d4SSatish Balay         cone[2] = 26;
19379371c9d4SSatish Balay         cone[3] = 25;
19389371c9d4SSatish Balay         cone[4] = 16;
19399371c9d4SSatish Balay         cone[5] = 23;
19409371c9d4SSatish Balay         cone[6] = 24;
19419371c9d4SSatish Balay         cone[7] = 17;
19429566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 11, cone));
19439371c9d4SSatish Balay         cone[0] = 20;
19449371c9d4SSatish Balay         cone[1] = 28;
19459371c9d4SSatish Balay         cone[2] = 26;
19469371c9d4SSatish Balay         cone[3] = 21;
19479371c9d4SSatish Balay         cone[4] = 18;
19489371c9d4SSatish Balay         cone[5] = 17;
19499371c9d4SSatish Balay         cone[6] = 24;
19509371c9d4SSatish Balay         cone[7] = 27;
19519566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 12, cone));
19529371c9d4SSatish Balay         cone[0] = 30;
19539371c9d4SSatish Balay         cone[1] = 28;
19549371c9d4SSatish Balay         cone[2] = 20;
19559371c9d4SSatish Balay         cone[3] = 19;
19569371c9d4SSatish Balay         cone[4] = 29;
19579371c9d4SSatish Balay         cone[5] = 15;
19589371c9d4SSatish Balay         cone[6] = 18;
19599371c9d4SSatish Balay         cone[7] = 27;
19609566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 13, cone));
19619371c9d4SSatish Balay         cone[0] = 30;
19629371c9d4SSatish Balay         cone[1] = 19;
19639371c9d4SSatish Balay         cone[2] = 22;
19649371c9d4SSatish Balay         cone[3] = 25;
19659371c9d4SSatish Balay         cone[4] = 29;
19669371c9d4SSatish Balay         cone[5] = 23;
19679371c9d4SSatish Balay         cone[6] = 16;
19689371c9d4SSatish Balay         cone[7] = 15;
19699566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 14, cone));
1970006a8963SMatthew G. Knepley       } else {
19719371c9d4SSatish Balay         cone[0] = 5;
19729371c9d4SSatish Balay         cone[1] = 8;
19739371c9d4SSatish Balay         cone[2] = 7;
19749371c9d4SSatish Balay         cone[3] = 6;
19759371c9d4SSatish Balay         cone[4] = 9;
19769371c9d4SSatish Balay         cone[5] = 12;
19779371c9d4SSatish Balay         cone[6] = 11;
19789371c9d4SSatish Balay         cone[7] = 10;
19799566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
19809371c9d4SSatish Balay         cone[0] = 6;
19819371c9d4SSatish Balay         cone[1] = 7;
19829371c9d4SSatish Balay         cone[2] = 14;
19839371c9d4SSatish Balay         cone[3] = 13;
19849371c9d4SSatish Balay         cone[4] = 12;
19859371c9d4SSatish Balay         cone[5] = 15;
19869371c9d4SSatish Balay         cone[6] = 16;
19879371c9d4SSatish Balay         cone[7] = 11;
19889566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
19899371c9d4SSatish Balay         cone[0] = 8;
19909371c9d4SSatish Balay         cone[1] = 17;
19919371c9d4SSatish Balay         cone[2] = 14;
19929371c9d4SSatish Balay         cone[3] = 7;
19939371c9d4SSatish Balay         cone[4] = 10;
19949371c9d4SSatish Balay         cone[5] = 11;
19959371c9d4SSatish Balay         cone[6] = 16;
19969371c9d4SSatish Balay         cone[7] = 18;
19979566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
19989371c9d4SSatish Balay         cone[0] = 19;
19999371c9d4SSatish Balay         cone[1] = 17;
20009371c9d4SSatish Balay         cone[2] = 8;
20019371c9d4SSatish Balay         cone[3] = 5;
20029371c9d4SSatish Balay         cone[4] = 20;
20039371c9d4SSatish Balay         cone[5] = 9;
20049371c9d4SSatish Balay         cone[6] = 10;
20059371c9d4SSatish Balay         cone[7] = 18;
20069566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
20079371c9d4SSatish Balay         cone[0] = 19;
20089371c9d4SSatish Balay         cone[1] = 5;
20099371c9d4SSatish Balay         cone[2] = 6;
20109371c9d4SSatish Balay         cone[3] = 13;
20119371c9d4SSatish Balay         cone[4] = 20;
20129371c9d4SSatish Balay         cone[5] = 15;
20139371c9d4SSatish Balay         cone[6] = 12;
20149371c9d4SSatish Balay         cone[7] = 9;
20159566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
2016006a8963SMatthew G. Knepley       }
2017d8c47e87SMatthew G. Knepley     }
20189566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
20199566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
20200510c589SMatthew G. Knepley   }
2021dbc1dc17SMatthew G. Knepley   /* Create cube geometry */
20220510c589SMatthew G. Knepley   {
20230510c589SMatthew G. Knepley     Vec             coordinates;
20240510c589SMatthew G. Knepley     PetscSection    coordSection;
20250510c589SMatthew G. Knepley     PetscScalar    *coords;
20260510c589SMatthew G. Knepley     PetscInt        coordSize, v;
20270510c589SMatthew G. Knepley     const PetscReal dis = 1.0 / PetscSqrtReal(2.0);
20280510c589SMatthew G. Knepley     const PetscReal ds2 = dis / 2.0;
20290510c589SMatthew G. Knepley 
20300510c589SMatthew G. Knepley     /* Build coordinates */
20319566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
20329566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
20339566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
20349566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices));
20350510c589SMatthew G. Knepley     for (v = numCells; v < numCells + numVertices; ++v) {
20369566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
20379566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
20380510c589SMatthew G. Knepley     }
20399566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
20409566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
20419566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
20429566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
20439566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
20449566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
20459566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates, VECSTANDARD));
20469566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
2047dd400576SPatrick Sanan     if (rank == 0) {
20489371c9d4SSatish Balay       coords[0 * dim + 0]  = -ds2;
20499371c9d4SSatish Balay       coords[0 * dim + 1]  = -ds2;
20509371c9d4SSatish Balay       coords[0 * dim + 2]  = 0.0;
20519371c9d4SSatish Balay       coords[1 * dim + 0]  = ds2;
20529371c9d4SSatish Balay       coords[1 * dim + 1]  = -ds2;
20539371c9d4SSatish Balay       coords[1 * dim + 2]  = 0.0;
20549371c9d4SSatish Balay       coords[2 * dim + 0]  = ds2;
20559371c9d4SSatish Balay       coords[2 * dim + 1]  = ds2;
20569371c9d4SSatish Balay       coords[2 * dim + 2]  = 0.0;
20579371c9d4SSatish Balay       coords[3 * dim + 0]  = -ds2;
20589371c9d4SSatish Balay       coords[3 * dim + 1]  = ds2;
20599371c9d4SSatish Balay       coords[3 * dim + 2]  = 0.0;
20609371c9d4SSatish Balay       coords[4 * dim + 0]  = -ds2;
20619371c9d4SSatish Balay       coords[4 * dim + 1]  = -ds2;
20629371c9d4SSatish Balay       coords[4 * dim + 2]  = 1.0;
20639371c9d4SSatish Balay       coords[5 * dim + 0]  = -ds2;
20649371c9d4SSatish Balay       coords[5 * dim + 1]  = ds2;
20659371c9d4SSatish Balay       coords[5 * dim + 2]  = 1.0;
20669371c9d4SSatish Balay       coords[6 * dim + 0]  = ds2;
20679371c9d4SSatish Balay       coords[6 * dim + 1]  = ds2;
20689371c9d4SSatish Balay       coords[6 * dim + 2]  = 1.0;
20699371c9d4SSatish Balay       coords[7 * dim + 0]  = ds2;
20709371c9d4SSatish Balay       coords[7 * dim + 1]  = -ds2;
20719371c9d4SSatish Balay       coords[7 * dim + 2]  = 1.0;
20729371c9d4SSatish Balay       coords[8 * dim + 0]  = dis;
20739371c9d4SSatish Balay       coords[8 * dim + 1]  = -dis;
20749371c9d4SSatish Balay       coords[8 * dim + 2]  = 0.0;
20759371c9d4SSatish Balay       coords[9 * dim + 0]  = dis;
20769371c9d4SSatish Balay       coords[9 * dim + 1]  = dis;
20779371c9d4SSatish Balay       coords[9 * dim + 2]  = 0.0;
20789371c9d4SSatish Balay       coords[10 * dim + 0] = dis;
20799371c9d4SSatish Balay       coords[10 * dim + 1] = -dis;
20809371c9d4SSatish Balay       coords[10 * dim + 2] = 1.0;
20819371c9d4SSatish Balay       coords[11 * dim + 0] = dis;
20829371c9d4SSatish Balay       coords[11 * dim + 1] = dis;
20839371c9d4SSatish Balay       coords[11 * dim + 2] = 1.0;
20849371c9d4SSatish Balay       coords[12 * dim + 0] = -dis;
20859371c9d4SSatish Balay       coords[12 * dim + 1] = dis;
20869371c9d4SSatish Balay       coords[12 * dim + 2] = 0.0;
20879371c9d4SSatish Balay       coords[13 * dim + 0] = -dis;
20889371c9d4SSatish Balay       coords[13 * dim + 1] = dis;
20899371c9d4SSatish Balay       coords[13 * dim + 2] = 1.0;
20909371c9d4SSatish Balay       coords[14 * dim + 0] = -dis;
20919371c9d4SSatish Balay       coords[14 * dim + 1] = -dis;
20929371c9d4SSatish Balay       coords[14 * dim + 2] = 0.0;
20939371c9d4SSatish Balay       coords[15 * dim + 0] = -dis;
20949371c9d4SSatish Balay       coords[15 * dim + 1] = -dis;
20959371c9d4SSatish Balay       coords[15 * dim + 2] = 1.0;
2096ae8bcbbbSMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
20979371c9d4SSatish Balay         /* 15 31 19 */ coords[16 * dim + 0] = -ds2;
20989371c9d4SSatish Balay         coords[16 * dim + 1]                = -ds2;
20999371c9d4SSatish Balay         coords[16 * dim + 2]                = 0.5;
21009371c9d4SSatish Balay         /* 16 32 22 */ coords[17 * dim + 0] = ds2;
21019371c9d4SSatish Balay         coords[17 * dim + 1]                = -ds2;
21029371c9d4SSatish Balay         coords[17 * dim + 2]                = 0.5;
21039371c9d4SSatish Balay         /* 17 33 21 */ coords[18 * dim + 0] = ds2;
21049371c9d4SSatish Balay         coords[18 * dim + 1]                = ds2;
21059371c9d4SSatish Balay         coords[18 * dim + 2]                = 0.5;
21069371c9d4SSatish Balay         /* 18 34 20 */ coords[19 * dim + 0] = -ds2;
21079371c9d4SSatish Balay         coords[19 * dim + 1]                = ds2;
21089371c9d4SSatish Balay         coords[19 * dim + 2]                = 0.5;
21099371c9d4SSatish Balay         /* 29 35 30 */ coords[20 * dim + 0] = -dis;
21109371c9d4SSatish Balay         coords[20 * dim + 1]                = -dis;
21119371c9d4SSatish Balay         coords[20 * dim + 2]                = 0.5;
21129371c9d4SSatish Balay         /* 23 36 25 */ coords[21 * dim + 0] = dis;
21139371c9d4SSatish Balay         coords[21 * dim + 1]                = -dis;
21149371c9d4SSatish Balay         coords[21 * dim + 2]                = 0.5;
21159371c9d4SSatish Balay         /* 24 37 26 */ coords[22 * dim + 0] = dis;
21169371c9d4SSatish Balay         coords[22 * dim + 1]                = dis;
21179371c9d4SSatish Balay         coords[22 * dim + 2]                = 0.5;
21189371c9d4SSatish Balay         /* 27 38 28 */ coords[23 * dim + 0] = -dis;
21199371c9d4SSatish Balay         coords[23 * dim + 1]                = dis;
21209371c9d4SSatish Balay         coords[23 * dim + 2]                = 0.5;
2121ae8bcbbbSMatthew G. Knepley       }
2122d8c47e87SMatthew G. Knepley     }
21239566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
21249566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
21259566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
21260510c589SMatthew G. Knepley   }
2127006a8963SMatthew G. Knepley   /* Create periodicity */
2128006a8963SMatthew G. Knepley   if (periodicZ == DM_BOUNDARY_PERIODIC || periodicZ == DM_BOUNDARY_TWIST) {
21296858538eSMatthew G. Knepley     PetscReal L[3]       = {-1., -1., 0.};
21306858538eSMatthew G. Knepley     PetscReal maxCell[3] = {-1., -1., 0.};
2131006a8963SMatthew G. Knepley     PetscReal lower[3]   = {0.0, 0.0, 0.0};
2132ae8bcbbbSMatthew G. Knepley     PetscReal upper[3]   = {1.0, 1.0, 1.5};
21336858538eSMatthew G. Knepley     PetscInt  numZCells  = 3;
2134006a8963SMatthew G. Knepley 
21356858538eSMatthew G. Knepley     L[2]       = upper[2] - lower[2];
21366858538eSMatthew G. Knepley     maxCell[2] = 1.1 * (L[2] / numZCells);
21374fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, maxCell, lower, L));
2138006a8963SMatthew G. Knepley   }
2139dbc1dc17SMatthew G. Knepley   {
21409318fe57SMatthew G. Knepley     DM          cdm;
21419318fe57SMatthew G. Knepley     PetscDS     cds;
21429318fe57SMatthew G. Knepley     PetscScalar c[2] = {1.0, 1.0};
2143dbc1dc17SMatthew G. Knepley 
2144*49704ca5SMatthew G. Knepley     PetscCall(DMPlexCreateCoordinateSpace(dm, 1, PETSC_TRUE, NULL));
21459566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
21469566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
21479566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, 2, c));
2148dbc1dc17SMatthew G. Knepley   }
214946139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
215046139095SJed Brown 
21519318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
21529566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolateInPlace_Internal(dm));
2153*49704ca5SMatthew G. Knepley 
2154*49704ca5SMatthew G. Knepley   char        oldprefix[PETSC_MAX_PATH_LEN];
2155*49704ca5SMatthew G. Knepley   const char *prefix;
2156*49704ca5SMatthew G. Knepley 
2157*49704ca5SMatthew G. Knepley   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, &prefix));
2158*49704ca5SMatthew G. Knepley   PetscCall(PetscStrncpy(oldprefix, prefix, PETSC_MAX_PATH_LEN));
2159*49704ca5SMatthew G. Knepley   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, "petsc_cyl_ref_"));
2160*49704ca5SMatthew G. Knepley   for (PetscInt r = 0; r < PetscMax(0, Nr); ++r) {
2161*49704ca5SMatthew G. Knepley     DM rdm;
2162*49704ca5SMatthew G. Knepley 
2163*49704ca5SMatthew G. Knepley     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &rdm));
2164*49704ca5SMatthew G. Knepley     PetscCall(DMPlexReplace_Internal(dm, &rdm));
2165*49704ca5SMatthew G. Knepley   }
2166*49704ca5SMatthew G. Knepley   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, oldprefix));
2167*49704ca5SMatthew G. Knepley   PetscCall(DMPlexRemapGeometry(dm, 0.0, snapToCylinder));
2168*49704ca5SMatthew G. Knepley 
2169*49704ca5SMatthew G. Knepley   DMLabel         bdlabel, edgelabel;
2170*49704ca5SMatthew G. Knepley   IS              faceIS;
2171*49704ca5SMatthew G. Knepley   const PetscInt *faces;
2172*49704ca5SMatthew G. Knepley   PetscInt        Nf;
2173*49704ca5SMatthew G. Knepley 
2174*49704ca5SMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "marker"));
2175*49704ca5SMatthew G. Knepley   PetscCall(DMGetLabel(dm, "marker", &bdlabel));
2176*49704ca5SMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "generatrix"));
2177*49704ca5SMatthew G. Knepley   PetscCall(DMGetLabel(dm, "generatrix", &edgelabel));
2178*49704ca5SMatthew G. Knepley   PetscCall(DMPlexMarkBoundaryFaces(dm, PETSC_DETERMINE, bdlabel));
2179*49704ca5SMatthew G. Knepley   // Remove faces on top and bottom
2180*49704ca5SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(bdlabel, 1, &faceIS));
2181*49704ca5SMatthew G. Knepley   PetscCall(ISGetLocalSize(faceIS, &Nf));
2182*49704ca5SMatthew G. Knepley   PetscCall(ISGetIndices(faceIS, &faces));
2183*49704ca5SMatthew G. Knepley   for (PetscInt f = 0; f < Nf; ++f) {
2184*49704ca5SMatthew G. Knepley     PetscReal vol, normal[3];
2185*49704ca5SMatthew G. Knepley 
2186*49704ca5SMatthew G. Knepley     PetscCall(DMPlexComputeCellGeometryFVM(dm, faces[f], &vol, NULL, normal));
2187*49704ca5SMatthew G. Knepley     if (PetscAbsReal(normal[2]) < PETSC_SMALL) PetscCall(DMLabelSetValue(edgelabel, faces[f], 1));
2188*49704ca5SMatthew G. Knepley   }
2189*49704ca5SMatthew G. Knepley   PetscCall(ISRestoreIndices(faceIS, &faces));
2190*49704ca5SMatthew G. Knepley   PetscCall(ISDestroy(&faceIS));
2191*49704ca5SMatthew G. Knepley   PetscCall(DMPlexLabelComplete(dm, bdlabel));
2192*49704ca5SMatthew G. Knepley   PetscCall(DMPlexLabelComplete(dm, edgelabel));
21933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
21940510c589SMatthew G. Knepley }
21950510c589SMatthew G. Knepley 
219624119c2aSMatthew G. Knepley /*@
21979318fe57SMatthew G. Knepley   DMPlexCreateHexCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using hexahedra.
219824119c2aSMatthew G. Knepley 
2199d083f849SBarry Smith   Collective
220024119c2aSMatthew G. Knepley 
220124119c2aSMatthew G. Knepley   Input Parameters:
2202a1cb98faSBarry Smith + comm      - The communicator for the `DM` object
2203*49704ca5SMatthew G. Knepley . periodicZ - The boundary type for the Z direction
2204*49704ca5SMatthew G. Knepley - Nr        - The number of refinements to carry out
220524119c2aSMatthew G. Knepley 
220624119c2aSMatthew G. Knepley   Output Parameter:
220720f4b53cSBarry Smith . dm - The `DM` object
220824119c2aSMatthew G. Knepley 
220924119c2aSMatthew G. Knepley   Level: beginner
221024119c2aSMatthew G. Knepley 
2211a1cb98faSBarry Smith   Note:
2212a4e35b19SJacob Faibussowitsch   Here is the output numbering looking from the bottom of the cylinder\:
2213a1cb98faSBarry Smith .vb
2214a1cb98faSBarry Smith        17-----14
2215a1cb98faSBarry Smith         |     |
2216a1cb98faSBarry Smith         |  2  |
2217a1cb98faSBarry Smith         |     |
2218a1cb98faSBarry Smith  17-----8-----7-----14
2219a1cb98faSBarry Smith   |     |     |     |
2220a1cb98faSBarry Smith   |  3  |  0  |  1  |
2221a1cb98faSBarry Smith   |     |     |     |
2222a1cb98faSBarry Smith  19-----5-----6-----13
2223a1cb98faSBarry Smith         |     |
2224a1cb98faSBarry Smith         |  4  |
2225a1cb98faSBarry Smith         |     |
2226a1cb98faSBarry Smith        19-----13
2227a1cb98faSBarry Smith 
2228a1cb98faSBarry Smith  and up through the top
2229a1cb98faSBarry Smith 
2230a1cb98faSBarry Smith        18-----16
2231a1cb98faSBarry Smith         |     |
2232a1cb98faSBarry Smith         |  2  |
2233a1cb98faSBarry Smith         |     |
2234a1cb98faSBarry Smith  18----10----11-----16
2235a1cb98faSBarry Smith   |     |     |     |
2236a1cb98faSBarry Smith   |  3  |  0  |  1  |
2237a1cb98faSBarry Smith   |     |     |     |
2238a1cb98faSBarry Smith  20-----9----12-----15
2239a1cb98faSBarry Smith         |     |
2240a1cb98faSBarry Smith         |  4  |
2241a1cb98faSBarry Smith         |     |
2242a1cb98faSBarry Smith        20-----15
2243a1cb98faSBarry Smith .ve
2244a1cb98faSBarry Smith 
22451cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
224624119c2aSMatthew G. Knepley @*/
2247*49704ca5SMatthew G. Knepley PetscErrorCode DMPlexCreateHexCylinderMesh(MPI_Comm comm, DMBoundaryType periodicZ, PetscInt Nr, DM *dm)
2248d71ae5a4SJacob Faibussowitsch {
22499318fe57SMatthew G. Knepley   PetscFunctionBegin;
2250*49704ca5SMatthew G. Knepley   PetscAssertPointer(dm, 4);
22519566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
22529566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
2253*49704ca5SMatthew G. Knepley   PetscCall(DMPlexCreateHexCylinderMesh_Internal(*dm, periodicZ, Nr));
22543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
22559318fe57SMatthew G. Knepley }
22569318fe57SMatthew G. Knepley 
2257d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateWedgeCylinderMesh_Internal(DM dm, PetscInt n, PetscBool interpolate)
2258d71ae5a4SJacob Faibussowitsch {
225924119c2aSMatthew G. Knepley   const PetscInt dim = 3;
2260412e9a14SMatthew G. Knepley   PetscInt       numCells, numVertices, v;
22619fe9f049SMatthew G. Knepley   PetscMPIInt    rank;
226224119c2aSMatthew G. Knepley 
226324119c2aSMatthew G. Knepley   PetscFunctionBegin;
226463a3b9bcSJacob Faibussowitsch   PetscCheck(n >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of wedges %" PetscInt_FMT " cannot be negative", n);
226546139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
22669566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
22679566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
2268412e9a14SMatthew G. Knepley   /* Must create the celltype label here so that we do not automatically try to compute the types */
22699566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "celltype"));
227024119c2aSMatthew G. Knepley   /* Create topology */
227124119c2aSMatthew G. Knepley   {
227224119c2aSMatthew G. Knepley     PetscInt cone[6], c;
227324119c2aSMatthew G. Knepley 
2274dd400576SPatrick Sanan     numCells    = rank == 0 ? n : 0;
2275dd400576SPatrick Sanan     numVertices = rank == 0 ? 2 * (n + 1) : 0;
22769566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
22779566063dSJacob Faibussowitsch     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 6));
22789566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm));
227924119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
22809371c9d4SSatish Balay       cone[0] = c + n * 1;
22819371c9d4SSatish Balay       cone[1] = (c + 1) % n + n * 1;
22829371c9d4SSatish Balay       cone[2] = 0 + 3 * n;
22839371c9d4SSatish Balay       cone[3] = c + n * 2;
22849371c9d4SSatish Balay       cone[4] = (c + 1) % n + n * 2;
22859371c9d4SSatish Balay       cone[5] = 1 + 3 * n;
22869566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(dm, c, cone));
22879566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(dm, c, DM_POLYTOPE_TRI_PRISM_TENSOR));
228824119c2aSMatthew G. Knepley     }
22899566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
22909566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
229124119c2aSMatthew G. Knepley   }
229248a46eb9SPierre Jolivet   for (v = numCells; v < numCells + numVertices; ++v) PetscCall(DMPlexSetCellType(dm, v, DM_POLYTOPE_POINT));
229324119c2aSMatthew G. Knepley   /* Create cylinder geometry */
229424119c2aSMatthew G. Knepley   {
229524119c2aSMatthew G. Knepley     Vec          coordinates;
229624119c2aSMatthew G. Knepley     PetscSection coordSection;
229724119c2aSMatthew G. Knepley     PetscScalar *coords;
2298412e9a14SMatthew G. Knepley     PetscInt     coordSize, c;
229924119c2aSMatthew G. Knepley 
230024119c2aSMatthew G. Knepley     /* Build coordinates */
23019566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
23029566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
23039566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
23049566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices));
230524119c2aSMatthew G. Knepley     for (v = numCells; v < numCells + numVertices; ++v) {
23069566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
23079566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
230824119c2aSMatthew G. Knepley     }
23099566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
23109566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
23119566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
23129566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
23139566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
23149566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
23159566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates, VECSTANDARD));
23169566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
231724119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
23189371c9d4SSatish Balay       coords[(c + 0 * n) * dim + 0] = PetscCosReal(2.0 * c * PETSC_PI / n);
23199371c9d4SSatish Balay       coords[(c + 0 * n) * dim + 1] = PetscSinReal(2.0 * c * PETSC_PI / n);
23209371c9d4SSatish Balay       coords[(c + 0 * n) * dim + 2] = 1.0;
23219371c9d4SSatish Balay       coords[(c + 1 * n) * dim + 0] = PetscCosReal(2.0 * c * PETSC_PI / n);
23229371c9d4SSatish Balay       coords[(c + 1 * n) * dim + 1] = PetscSinReal(2.0 * c * PETSC_PI / n);
23239371c9d4SSatish Balay       coords[(c + 1 * n) * dim + 2] = 0.0;
232424119c2aSMatthew G. Knepley     }
2325dd400576SPatrick Sanan     if (rank == 0) {
23269371c9d4SSatish Balay       coords[(2 * n + 0) * dim + 0] = 0.0;
23279371c9d4SSatish Balay       coords[(2 * n + 0) * dim + 1] = 0.0;
23289371c9d4SSatish Balay       coords[(2 * n + 0) * dim + 2] = 1.0;
23299371c9d4SSatish Balay       coords[(2 * n + 1) * dim + 0] = 0.0;
23309371c9d4SSatish Balay       coords[(2 * n + 1) * dim + 1] = 0.0;
23319371c9d4SSatish Balay       coords[(2 * n + 1) * dim + 2] = 0.0;
23329fe9f049SMatthew G. Knepley     }
23339566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
23349566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
23359566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
233624119c2aSMatthew G. Knepley   }
233746139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
23389318fe57SMatthew G. Knepley   /* Interpolate */
23399566063dSJacob Faibussowitsch   if (interpolate) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
23403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
23419318fe57SMatthew G. Knepley }
23429318fe57SMatthew G. Knepley 
23439318fe57SMatthew G. Knepley /*@
23449318fe57SMatthew G. Knepley   DMPlexCreateWedgeCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using wedges.
23459318fe57SMatthew G. Knepley 
23469318fe57SMatthew G. Knepley   Collective
23479318fe57SMatthew G. Knepley 
23489318fe57SMatthew G. Knepley   Input Parameters:
2349a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
23509318fe57SMatthew G. Knepley . n           - The number of wedges around the origin
23519318fe57SMatthew G. Knepley - interpolate - Create edges and faces
23529318fe57SMatthew G. Knepley 
23539318fe57SMatthew G. Knepley   Output Parameter:
2354a1cb98faSBarry Smith . dm - The `DM` object
23559318fe57SMatthew G. Knepley 
23569318fe57SMatthew G. Knepley   Level: beginner
23579318fe57SMatthew G. Knepley 
23581cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateHexCylinderMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
23599318fe57SMatthew G. Knepley @*/
2360d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateWedgeCylinderMesh(MPI_Comm comm, PetscInt n, PetscBool interpolate, DM *dm)
2361d71ae5a4SJacob Faibussowitsch {
23629318fe57SMatthew G. Knepley   PetscFunctionBegin;
23634f572ea9SToby Isaac   PetscAssertPointer(dm, 4);
23649566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
23659566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
23669566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateWedgeCylinderMesh_Internal(*dm, n, interpolate));
23673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
236824119c2aSMatthew G. Knepley }
236924119c2aSMatthew G. Knepley 
2370d71ae5a4SJacob Faibussowitsch static inline PetscReal DiffNormReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
2371d71ae5a4SJacob Faibussowitsch {
237265a81367SMatthew G. Knepley   PetscReal prod = 0.0;
237365a81367SMatthew G. Knepley   PetscInt  i;
237465a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += PetscSqr(x[i] - y[i]);
237565a81367SMatthew G. Knepley   return PetscSqrtReal(prod);
237665a81367SMatthew G. Knepley }
2377dd2b43ebSStefano Zampini 
2378d71ae5a4SJacob Faibussowitsch static inline PetscReal DotReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
2379d71ae5a4SJacob Faibussowitsch {
238065a81367SMatthew G. Knepley   PetscReal prod = 0.0;
238165a81367SMatthew G. Knepley   PetscInt  i;
238265a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += x[i] * y[i];
238365a81367SMatthew G. Knepley   return prod;
238465a81367SMatthew G. Knepley }
238565a81367SMatthew G. Knepley 
238651a74b61SMatthew G. Knepley /* The first constant is the sphere radius */
2387d71ae5a4SJacob 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[])
2388d71ae5a4SJacob Faibussowitsch {
238951a74b61SMatthew G. Knepley   PetscReal r     = PetscRealPart(constants[0]);
239051a74b61SMatthew G. Knepley   PetscReal norm2 = 0.0, fac;
239151a74b61SMatthew G. Knepley   PetscInt  n     = uOff[1] - uOff[0], d;
239251a74b61SMatthew G. Knepley 
239351a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) norm2 += PetscSqr(PetscRealPart(u[d]));
239451a74b61SMatthew G. Knepley   fac = r / PetscSqrtReal(norm2);
239551a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) f0[d] = u[d] * fac;
239651a74b61SMatthew G. Knepley }
239751a74b61SMatthew G. Knepley 
2398d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSphereMesh_Internal(DM dm, PetscInt dim, PetscBool simplex, PetscReal R)
2399d71ae5a4SJacob Faibussowitsch {
240065a81367SMatthew G. Knepley   const PetscInt embedDim = dim + 1;
240165a81367SMatthew G. Knepley   PetscSection   coordSection;
240265a81367SMatthew G. Knepley   Vec            coordinates;
240365a81367SMatthew G. Knepley   PetscScalar   *coords;
240465a81367SMatthew G. Knepley   PetscReal     *coordsIn;
240507c565c5SJose E. Roman   PetscInt       numCells, numEdges, numVerts = 0, firstVertex = 0, v, firstEdge, coordSize, d, e;
240665a81367SMatthew G. Knepley   PetscMPIInt    rank;
240765a81367SMatthew G. Knepley 
240865a81367SMatthew G. Knepley   PetscFunctionBegin;
24099318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveBool(dm, simplex, 3);
241046139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
24119566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
24129566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, dim + 1));
24139566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
241465a81367SMatthew G. Knepley   switch (dim) {
24155c344501SMatthew G. Knepley   case 1:
24165c344501SMatthew G. Knepley     numCells = 16;
24175c344501SMatthew G. Knepley     numVerts = numCells;
24185c344501SMatthew G. Knepley 
24195c344501SMatthew G. Knepley     // Build Topology
24205c344501SMatthew G. Knepley     PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts));
24215c344501SMatthew G. Knepley     for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim));
24225c344501SMatthew G. Knepley     PetscCall(DMSetUp(dm));
24235c344501SMatthew G. Knepley     for (PetscInt c = 0; c < numCells; ++c) {
24245c344501SMatthew G. Knepley       PetscInt cone[2];
24255c344501SMatthew G. Knepley 
24265c344501SMatthew G. Knepley       cone[0] = c + numCells;
24275c344501SMatthew G. Knepley       cone[1] = (c + 1) % numVerts + numCells;
24285c344501SMatthew G. Knepley       PetscCall(DMPlexSetCone(dm, c, cone));
24295c344501SMatthew G. Knepley     }
24305c344501SMatthew G. Knepley     PetscCall(DMPlexSymmetrize(dm));
24315c344501SMatthew G. Knepley     PetscCall(DMPlexStratify(dm));
24325c344501SMatthew G. Knepley     PetscCall(PetscMalloc1(numVerts * embedDim, &coordsIn));
24335c344501SMatthew G. Knepley     for (PetscInt v = 0; v < numVerts; ++v) {
24345c344501SMatthew G. Knepley       const PetscReal rad = 2. * PETSC_PI * v / numVerts;
24355c344501SMatthew G. Knepley 
24365c344501SMatthew G. Knepley       coordsIn[v * embedDim + 0] = PetscCosReal(rad);
24375c344501SMatthew G. Knepley       coordsIn[v * embedDim + 1] = PetscSinReal(rad);
24385c344501SMatthew G. Knepley     }
24395c344501SMatthew G. Knepley     break;
244065a81367SMatthew G. Knepley   case 2:
244165a81367SMatthew G. Knepley     if (simplex) {
244251a74b61SMatthew G. Knepley       const PetscReal radius    = PetscSqrtReal(1 + PETSC_PHI * PETSC_PHI) / (1.0 + PETSC_PHI);
244351a74b61SMatthew G. Knepley       const PetscReal edgeLen   = 2.0 / (1.0 + PETSC_PHI) * (R / radius);
244465a81367SMatthew G. Knepley       const PetscInt  degree    = 5;
244551a74b61SMatthew G. Knepley       PetscReal       vertex[3] = {0.0, 1.0 / (1.0 + PETSC_PHI), PETSC_PHI / (1.0 + PETSC_PHI)};
244665a81367SMatthew G. Knepley       PetscInt        s[3]      = {1, 1, 1};
244765a81367SMatthew G. Knepley       PetscInt        cone[3];
244807c565c5SJose E. Roman       PetscInt       *graph;
244965a81367SMatthew G. Knepley 
24509371c9d4SSatish Balay       vertex[0] *= R / radius;
24519371c9d4SSatish Balay       vertex[1] *= R / radius;
24529371c9d4SSatish Balay       vertex[2] *= R / radius;
2453dd400576SPatrick Sanan       numCells    = rank == 0 ? 20 : 0;
2454dd400576SPatrick Sanan       numVerts    = rank == 0 ? 12 : 0;
245565a81367SMatthew G. Knepley       firstVertex = numCells;
245651a74b61SMatthew G. Knepley       /* Use icosahedron, which for a R-sphere has coordinates which are all cyclic permutations of
245765a81367SMatthew G. Knepley 
245865a81367SMatthew G. Knepley            (0, \pm 1/\phi+1, \pm \phi/\phi+1)
245965a81367SMatthew G. Knepley 
246065a81367SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
246151a74b61SMatthew G. Knepley          length is then given by 2/(1+\phi) = 2 * 0.38197 = 0.76393.
246265a81367SMatthew G. Knepley       */
246365a81367SMatthew G. Knepley       /* Construct vertices */
24649566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
2465dd400576SPatrick Sanan       if (rank == 0) {
246607c565c5SJose E. Roman         for (PetscInt p = 0, i = 0; p < embedDim; ++p) {
246765a81367SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
246865a81367SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
246965a81367SMatthew G. Knepley               for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[(d + p) % embedDim] * vertex[(d + p) % embedDim];
247065a81367SMatthew G. Knepley               ++i;
247165a81367SMatthew G. Knepley             }
247265a81367SMatthew G. Knepley           }
247365a81367SMatthew G. Knepley         }
247445da822fSValeria Barra       }
247565a81367SMatthew G. Knepley       /* Construct graph */
24769566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * numVerts, &graph));
247707c565c5SJose E. Roman       for (PetscInt i = 0; i < numVerts; ++i) {
247807c565c5SJose E. Roman         PetscInt k = 0;
247907c565c5SJose E. Roman         for (PetscInt j = 0; j < numVerts; ++j) {
24809371c9d4SSatish Balay           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i * embedDim], &coordsIn[j * embedDim]) - edgeLen) < PETSC_SMALL) {
24819371c9d4SSatish Balay             graph[i * numVerts + j] = 1;
24829371c9d4SSatish Balay             ++k;
24839371c9d4SSatish Balay           }
248465a81367SMatthew G. Knepley         }
248563a3b9bcSJacob Faibussowitsch         PetscCheck(k == degree, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid icosahedron, vertex %" PetscInt_FMT " degree %" PetscInt_FMT " != %" PetscInt_FMT, i, k, degree);
248665a81367SMatthew G. Knepley       }
248765a81367SMatthew G. Knepley       /* Build Topology */
24889566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts));
248907c565c5SJose E. Roman       for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim));
24909566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
249165a81367SMatthew G. Knepley       /* Cells */
249207c565c5SJose E. Roman       for (PetscInt i = 0, c = 0; i < numVerts; ++i) {
249307c565c5SJose E. Roman         for (PetscInt j = 0; j < i; ++j) {
249407c565c5SJose E. Roman           for (PetscInt k = 0; k < j; ++k) {
249565a81367SMatthew G. Knepley             if (graph[i * numVerts + j] && graph[j * numVerts + k] && graph[k * numVerts + i]) {
24969371c9d4SSatish Balay               cone[0] = firstVertex + i;
24979371c9d4SSatish Balay               cone[1] = firstVertex + j;
24989371c9d4SSatish Balay               cone[2] = firstVertex + k;
249965a81367SMatthew G. Knepley               /* Check orientation */
250065a81367SMatthew G. Knepley               {
25019371c9d4SSatish Balay                 const PetscInt epsilon[3][3][3] = {
25029371c9d4SSatish Balay                   {{0, 0, 0},  {0, 0, 1},  {0, -1, 0}},
25039371c9d4SSatish Balay                   {{0, 0, -1}, {0, 0, 0},  {1, 0, 0} },
25049371c9d4SSatish Balay                   {{0, 1, 0},  {-1, 0, 0}, {0, 0, 0} }
25059371c9d4SSatish Balay                 };
250665a81367SMatthew G. Knepley                 PetscReal normal[3];
250765a81367SMatthew G. Knepley                 PetscInt  e, f;
250865a81367SMatthew G. Knepley 
250965a81367SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) {
251065a81367SMatthew G. Knepley                   normal[d] = 0.0;
251165a81367SMatthew G. Knepley                   for (e = 0; e < embedDim; ++e) {
2512ad540459SPierre 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]);
251365a81367SMatthew G. Knepley                   }
251465a81367SMatthew G. Knepley                 }
25159371c9d4SSatish Balay                 if (DotReal(embedDim, normal, &coordsIn[i * embedDim]) < 0) {
25169371c9d4SSatish Balay                   PetscInt tmp = cone[1];
25179371c9d4SSatish Balay                   cone[1]      = cone[2];
25189371c9d4SSatish Balay                   cone[2]      = tmp;
251965a81367SMatthew G. Knepley                 }
252065a81367SMatthew G. Knepley               }
25219566063dSJacob Faibussowitsch               PetscCall(DMPlexSetCone(dm, c++, cone));
252265a81367SMatthew G. Knepley             }
252365a81367SMatthew G. Knepley           }
252465a81367SMatthew G. Knepley         }
252565a81367SMatthew G. Knepley       }
25269566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
25279566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
25289566063dSJacob Faibussowitsch       PetscCall(PetscFree(graph));
252965a81367SMatthew G. Knepley     } else {
25302829fed8SMatthew G. Knepley       /*
25312829fed8SMatthew G. Knepley         12-21--13
25322829fed8SMatthew G. Knepley          |     |
25332829fed8SMatthew G. Knepley         25  4  24
25342829fed8SMatthew G. Knepley          |     |
25352829fed8SMatthew G. Knepley   12-25--9-16--8-24--13
25362829fed8SMatthew G. Knepley    |     |     |     |
25372829fed8SMatthew G. Knepley   23  5 17  0 15  3  22
25382829fed8SMatthew G. Knepley    |     |     |     |
25392829fed8SMatthew G. Knepley   10-20--6-14--7-19--11
25402829fed8SMatthew G. Knepley          |     |
25412829fed8SMatthew G. Knepley         20  1  19
25422829fed8SMatthew G. Knepley          |     |
25432829fed8SMatthew G. Knepley         10-18--11
25442829fed8SMatthew G. Knepley          |     |
25452829fed8SMatthew G. Knepley         23  2  22
25462829fed8SMatthew G. Knepley          |     |
25472829fed8SMatthew G. Knepley         12-21--13
25482829fed8SMatthew G. Knepley        */
25492829fed8SMatthew G. Knepley       PetscInt cone[4], ornt[4];
25502829fed8SMatthew G. Knepley 
2551dd400576SPatrick Sanan       numCells    = rank == 0 ? 6 : 0;
2552dd400576SPatrick Sanan       numEdges    = rank == 0 ? 12 : 0;
2553dd400576SPatrick Sanan       numVerts    = rank == 0 ? 8 : 0;
255465a81367SMatthew G. Knepley       firstVertex = numCells;
255565a81367SMatthew G. Knepley       firstEdge   = numCells + numVerts;
25562829fed8SMatthew G. Knepley       /* Build Topology */
25579566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells + numEdges + numVerts));
255807c565c5SJose E. Roman       for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 4));
255948a46eb9SPierre Jolivet       for (e = firstEdge; e < firstEdge + numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2));
25609566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
2561dd400576SPatrick Sanan       if (rank == 0) {
25622829fed8SMatthew G. Knepley         /* Cell 0 */
25639371c9d4SSatish Balay         cone[0] = 14;
25649371c9d4SSatish Balay         cone[1] = 15;
25659371c9d4SSatish Balay         cone[2] = 16;
25669371c9d4SSatish Balay         cone[3] = 17;
25679566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
25689371c9d4SSatish Balay         ornt[0] = 0;
25699371c9d4SSatish Balay         ornt[1] = 0;
25709371c9d4SSatish Balay         ornt[2] = 0;
25719371c9d4SSatish Balay         ornt[3] = 0;
25729566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 0, ornt));
25732829fed8SMatthew G. Knepley         /* Cell 1 */
25749371c9d4SSatish Balay         cone[0] = 18;
25759371c9d4SSatish Balay         cone[1] = 19;
25769371c9d4SSatish Balay         cone[2] = 14;
25779371c9d4SSatish Balay         cone[3] = 20;
25789566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
25799371c9d4SSatish Balay         ornt[0] = 0;
25809371c9d4SSatish Balay         ornt[1] = 0;
25819371c9d4SSatish Balay         ornt[2] = -1;
25829371c9d4SSatish Balay         ornt[3] = 0;
25839566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 1, ornt));
25842829fed8SMatthew G. Knepley         /* Cell 2 */
25859371c9d4SSatish Balay         cone[0] = 21;
25869371c9d4SSatish Balay         cone[1] = 22;
25879371c9d4SSatish Balay         cone[2] = 18;
25889371c9d4SSatish Balay         cone[3] = 23;
25899566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
25909371c9d4SSatish Balay         ornt[0] = 0;
25919371c9d4SSatish Balay         ornt[1] = 0;
25929371c9d4SSatish Balay         ornt[2] = -1;
25939371c9d4SSatish Balay         ornt[3] = 0;
25949566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 2, ornt));
25952829fed8SMatthew G. Knepley         /* Cell 3 */
25969371c9d4SSatish Balay         cone[0] = 19;
25979371c9d4SSatish Balay         cone[1] = 22;
25989371c9d4SSatish Balay         cone[2] = 24;
25999371c9d4SSatish Balay         cone[3] = 15;
26009566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
26019371c9d4SSatish Balay         ornt[0] = -1;
26029371c9d4SSatish Balay         ornt[1] = -1;
26039371c9d4SSatish Balay         ornt[2] = 0;
26049371c9d4SSatish Balay         ornt[3] = -1;
26059566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 3, ornt));
26062829fed8SMatthew G. Knepley         /* Cell 4 */
26079371c9d4SSatish Balay         cone[0] = 16;
26089371c9d4SSatish Balay         cone[1] = 24;
26099371c9d4SSatish Balay         cone[2] = 21;
26109371c9d4SSatish Balay         cone[3] = 25;
26119566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
26129371c9d4SSatish Balay         ornt[0] = -1;
26139371c9d4SSatish Balay         ornt[1] = -1;
26149371c9d4SSatish Balay         ornt[2] = -1;
26159371c9d4SSatish Balay         ornt[3] = 0;
26169566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 4, ornt));
26172829fed8SMatthew G. Knepley         /* Cell 5 */
26189371c9d4SSatish Balay         cone[0] = 20;
26199371c9d4SSatish Balay         cone[1] = 17;
26209371c9d4SSatish Balay         cone[2] = 25;
26219371c9d4SSatish Balay         cone[3] = 23;
26229566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 5, cone));
26239371c9d4SSatish Balay         ornt[0] = -1;
26249371c9d4SSatish Balay         ornt[1] = -1;
26259371c9d4SSatish Balay         ornt[2] = -1;
26269371c9d4SSatish Balay         ornt[3] = -1;
26279566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 5, ornt));
26282829fed8SMatthew G. Knepley         /* Edges */
26299371c9d4SSatish Balay         cone[0] = 6;
26309371c9d4SSatish Balay         cone[1] = 7;
26319566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 14, cone));
26329371c9d4SSatish Balay         cone[0] = 7;
26339371c9d4SSatish Balay         cone[1] = 8;
26349566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 15, cone));
26359371c9d4SSatish Balay         cone[0] = 8;
26369371c9d4SSatish Balay         cone[1] = 9;
26379566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 16, cone));
26389371c9d4SSatish Balay         cone[0] = 9;
26399371c9d4SSatish Balay         cone[1] = 6;
26409566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 17, cone));
26419371c9d4SSatish Balay         cone[0] = 10;
26429371c9d4SSatish Balay         cone[1] = 11;
26439566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 18, cone));
26449371c9d4SSatish Balay         cone[0] = 11;
26459371c9d4SSatish Balay         cone[1] = 7;
26469566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 19, cone));
26479371c9d4SSatish Balay         cone[0] = 6;
26489371c9d4SSatish Balay         cone[1] = 10;
26499566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 20, cone));
26509371c9d4SSatish Balay         cone[0] = 12;
26519371c9d4SSatish Balay         cone[1] = 13;
26529566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 21, cone));
26539371c9d4SSatish Balay         cone[0] = 13;
26549371c9d4SSatish Balay         cone[1] = 11;
26559566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 22, cone));
26569371c9d4SSatish Balay         cone[0] = 10;
26579371c9d4SSatish Balay         cone[1] = 12;
26589566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 23, cone));
26599371c9d4SSatish Balay         cone[0] = 13;
26609371c9d4SSatish Balay         cone[1] = 8;
26619566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 24, cone));
26629371c9d4SSatish Balay         cone[0] = 12;
26639371c9d4SSatish Balay         cone[1] = 9;
26649566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 25, cone));
266545da822fSValeria Barra       }
26669566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
26679566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
26682829fed8SMatthew G. Knepley       /* Build coordinates */
26699566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
2670dd400576SPatrick Sanan       if (rank == 0) {
26719371c9d4SSatish Balay         coordsIn[0 * embedDim + 0] = -R;
26729371c9d4SSatish Balay         coordsIn[0 * embedDim + 1] = R;
26739371c9d4SSatish Balay         coordsIn[0 * embedDim + 2] = -R;
26749371c9d4SSatish Balay         coordsIn[1 * embedDim + 0] = R;
26759371c9d4SSatish Balay         coordsIn[1 * embedDim + 1] = R;
26769371c9d4SSatish Balay         coordsIn[1 * embedDim + 2] = -R;
26779371c9d4SSatish Balay         coordsIn[2 * embedDim + 0] = R;
26789371c9d4SSatish Balay         coordsIn[2 * embedDim + 1] = -R;
26799371c9d4SSatish Balay         coordsIn[2 * embedDim + 2] = -R;
26809371c9d4SSatish Balay         coordsIn[3 * embedDim + 0] = -R;
26819371c9d4SSatish Balay         coordsIn[3 * embedDim + 1] = -R;
26829371c9d4SSatish Balay         coordsIn[3 * embedDim + 2] = -R;
26839371c9d4SSatish Balay         coordsIn[4 * embedDim + 0] = -R;
26849371c9d4SSatish Balay         coordsIn[4 * embedDim + 1] = R;
26859371c9d4SSatish Balay         coordsIn[4 * embedDim + 2] = R;
26869371c9d4SSatish Balay         coordsIn[5 * embedDim + 0] = R;
26879371c9d4SSatish Balay         coordsIn[5 * embedDim + 1] = R;
26889371c9d4SSatish Balay         coordsIn[5 * embedDim + 2] = R;
26899371c9d4SSatish Balay         coordsIn[6 * embedDim + 0] = -R;
26909371c9d4SSatish Balay         coordsIn[6 * embedDim + 1] = -R;
26919371c9d4SSatish Balay         coordsIn[6 * embedDim + 2] = R;
26929371c9d4SSatish Balay         coordsIn[7 * embedDim + 0] = R;
26939371c9d4SSatish Balay         coordsIn[7 * embedDim + 1] = -R;
26949371c9d4SSatish Balay         coordsIn[7 * embedDim + 2] = R;
269565a81367SMatthew G. Knepley       }
269645da822fSValeria Barra     }
269765a81367SMatthew G. Knepley     break;
269865a81367SMatthew G. Knepley   case 3:
2699116ded15SMatthew G. Knepley     if (simplex) {
2700116ded15SMatthew G. Knepley       const PetscReal edgeLen         = 1.0 / PETSC_PHI;
270151a74b61SMatthew G. Knepley       PetscReal       vertexA[4]      = {0.5, 0.5, 0.5, 0.5};
270251a74b61SMatthew G. Knepley       PetscReal       vertexB[4]      = {1.0, 0.0, 0.0, 0.0};
270351a74b61SMatthew G. Knepley       PetscReal       vertexC[4]      = {0.5, 0.5 * PETSC_PHI, 0.5 / PETSC_PHI, 0.0};
2704116ded15SMatthew G. Knepley       const PetscInt  degree          = 12;
2705116ded15SMatthew G. Knepley       PetscInt        s[4]            = {1, 1, 1};
27069371c9d4SSatish Balay       PetscInt        evenPerm[12][4] = {
27079371c9d4SSatish Balay         {0, 1, 2, 3},
27089371c9d4SSatish Balay         {0, 2, 3, 1},
27099371c9d4SSatish Balay         {0, 3, 1, 2},
27109371c9d4SSatish Balay         {1, 0, 3, 2},
27119371c9d4SSatish Balay         {1, 2, 0, 3},
27129371c9d4SSatish Balay         {1, 3, 2, 0},
27139371c9d4SSatish Balay         {2, 0, 1, 3},
27149371c9d4SSatish Balay         {2, 1, 3, 0},
27159371c9d4SSatish Balay         {2, 3, 0, 1},
27169371c9d4SSatish Balay         {3, 0, 2, 1},
27179371c9d4SSatish Balay         {3, 1, 0, 2},
27189371c9d4SSatish Balay         {3, 2, 1, 0}
27199371c9d4SSatish Balay       };
2720116ded15SMatthew G. Knepley       PetscInt  cone[4];
2721116ded15SMatthew G. Knepley       PetscInt *graph, p, i, j, k, l;
2722116ded15SMatthew G. Knepley 
27239371c9d4SSatish Balay       vertexA[0] *= R;
27249371c9d4SSatish Balay       vertexA[1] *= R;
27259371c9d4SSatish Balay       vertexA[2] *= R;
27269371c9d4SSatish Balay       vertexA[3] *= R;
27279371c9d4SSatish Balay       vertexB[0] *= R;
27289371c9d4SSatish Balay       vertexB[1] *= R;
27299371c9d4SSatish Balay       vertexB[2] *= R;
27309371c9d4SSatish Balay       vertexB[3] *= R;
27319371c9d4SSatish Balay       vertexC[0] *= R;
27329371c9d4SSatish Balay       vertexC[1] *= R;
27339371c9d4SSatish Balay       vertexC[2] *= R;
27349371c9d4SSatish Balay       vertexC[3] *= R;
2735dd400576SPatrick Sanan       numCells    = rank == 0 ? 600 : 0;
2736dd400576SPatrick Sanan       numVerts    = rank == 0 ? 120 : 0;
2737116ded15SMatthew G. Knepley       firstVertex = numCells;
2738116ded15SMatthew G. Knepley       /* Use the 600-cell, which for a unit sphere has coordinates which are
2739116ded15SMatthew G. Knepley 
2740116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm 1,    \pm 1, \pm 1)                          16
2741116ded15SMatthew G. Knepley                (\pm 1,    0,       0,      0)  all cyclic permutations   8
2742116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm phi, \pm 1/phi, 0)  all even permutations    96
2743116ded15SMatthew G. Knepley 
2744116ded15SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
27456333ae4fSvaleriabarra          length is then given by 1/\phi = 0.61803.
2746116ded15SMatthew G. Knepley 
2747116ded15SMatthew G. Knepley          http://buzzard.pugetsound.edu/sage-practice/ch03s03.html
2748116ded15SMatthew G. Knepley          http://mathworld.wolfram.com/600-Cell.html
2749116ded15SMatthew G. Knepley       */
2750116ded15SMatthew G. Knepley       /* Construct vertices */
27519566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
2752116ded15SMatthew G. Knepley       i = 0;
2753dd400576SPatrick Sanan       if (rank == 0) {
2754116ded15SMatthew G. Knepley         for (s[0] = -1; s[0] < 2; s[0] += 2) {
2755116ded15SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
2756116ded15SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
2757116ded15SMatthew G. Knepley               for (s[3] = -1; s[3] < 2; s[3] += 2) {
2758116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[d] * vertexA[d];
2759116ded15SMatthew G. Knepley                 ++i;
2760116ded15SMatthew G. Knepley               }
2761116ded15SMatthew G. Knepley             }
2762116ded15SMatthew G. Knepley           }
2763116ded15SMatthew G. Knepley         }
2764116ded15SMatthew G. Knepley         for (p = 0; p < embedDim; ++p) {
2765116ded15SMatthew G. Knepley           s[1] = s[2] = s[3] = 1;
2766116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
2767116ded15SMatthew G. Knepley             for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[(d + p) % embedDim] * vertexB[(d + p) % embedDim];
2768116ded15SMatthew G. Knepley             ++i;
2769116ded15SMatthew G. Knepley           }
2770116ded15SMatthew G. Knepley         }
2771116ded15SMatthew G. Knepley         for (p = 0; p < 12; ++p) {
2772116ded15SMatthew G. Knepley           s[3] = 1;
2773116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
2774116ded15SMatthew G. Knepley             for (s[1] = -1; s[1] < 2; s[1] += 2) {
2775116ded15SMatthew G. Knepley               for (s[2] = -1; s[2] < 2; s[2] += 2) {
2776116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[evenPerm[p][d]] * vertexC[evenPerm[p][d]];
2777116ded15SMatthew G. Knepley                 ++i;
2778116ded15SMatthew G. Knepley               }
2779116ded15SMatthew G. Knepley             }
2780116ded15SMatthew G. Knepley           }
2781116ded15SMatthew G. Knepley         }
278245da822fSValeria Barra       }
278363a3b9bcSJacob Faibussowitsch       PetscCheck(i == numVerts, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertices %" PetscInt_FMT " != %" PetscInt_FMT, i, numVerts);
2784116ded15SMatthew G. Knepley       /* Construct graph */
27859566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * numVerts, &graph));
2786116ded15SMatthew G. Knepley       for (i = 0; i < numVerts; ++i) {
2787116ded15SMatthew G. Knepley         for (j = 0, k = 0; j < numVerts; ++j) {
27889371c9d4SSatish Balay           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i * embedDim], &coordsIn[j * embedDim]) - edgeLen) < PETSC_SMALL) {
27899371c9d4SSatish Balay             graph[i * numVerts + j] = 1;
27909371c9d4SSatish Balay             ++k;
27919371c9d4SSatish Balay           }
2792116ded15SMatthew G. Knepley         }
279363a3b9bcSJacob Faibussowitsch         PetscCheck(k == degree, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertex %" PetscInt_FMT " degree %" PetscInt_FMT " != %" PetscInt_FMT, i, k, degree);
2794116ded15SMatthew G. Knepley       }
2795116ded15SMatthew G. Knepley       /* Build Topology */
27969566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts));
279707c565c5SJose E. Roman       for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim));
27989566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
2799116ded15SMatthew G. Knepley       /* Cells */
2800dd400576SPatrick Sanan       if (rank == 0) {
280107c565c5SJose E. Roman         for (PetscInt i = 0, c = 0; i < numVerts; ++i) {
2802116ded15SMatthew G. Knepley           for (j = 0; j < i; ++j) {
2803116ded15SMatthew G. Knepley             for (k = 0; k < j; ++k) {
2804116ded15SMatthew G. Knepley               for (l = 0; l < k; ++l) {
28059371c9d4SSatish 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]) {
28069371c9d4SSatish Balay                   cone[0] = firstVertex + i;
28079371c9d4SSatish Balay                   cone[1] = firstVertex + j;
28089371c9d4SSatish Balay                   cone[2] = firstVertex + k;
28099371c9d4SSatish Balay                   cone[3] = firstVertex + l;
2810116ded15SMatthew G. Knepley                   /* Check orientation: https://ef.gy/linear-algebra:normal-vectors-in-higher-dimensional-spaces */
2811116ded15SMatthew G. Knepley                   {
28129371c9d4SSatish Balay                     const PetscInt epsilon[4][4][4][4] = {
28139371c9d4SSatish 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}}},
2814116ded15SMatthew G. Knepley 
28159371c9d4SSatish 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}}},
2816116ded15SMatthew G. Knepley 
28179371c9d4SSatish 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}}},
2818116ded15SMatthew G. Knepley 
28199371c9d4SSatish 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}} }
28209371c9d4SSatish Balay                     };
2821116ded15SMatthew G. Knepley                     PetscReal normal[4];
2822116ded15SMatthew G. Knepley                     PetscInt  e, f, g;
2823116ded15SMatthew G. Knepley 
2824116ded15SMatthew G. Knepley                     for (d = 0; d < embedDim; ++d) {
2825116ded15SMatthew G. Knepley                       normal[d] = 0.0;
2826116ded15SMatthew G. Knepley                       for (e = 0; e < embedDim; ++e) {
2827116ded15SMatthew G. Knepley                         for (f = 0; f < embedDim; ++f) {
2828116ded15SMatthew G. Knepley                           for (g = 0; g < embedDim; ++g) {
2829116ded15SMatthew 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]);
2830116ded15SMatthew G. Knepley                           }
2831116ded15SMatthew G. Knepley                         }
2832116ded15SMatthew G. Knepley                       }
2833116ded15SMatthew G. Knepley                     }
28349371c9d4SSatish Balay                     if (DotReal(embedDim, normal, &coordsIn[i * embedDim]) < 0) {
28359371c9d4SSatish Balay                       PetscInt tmp = cone[1];
28369371c9d4SSatish Balay                       cone[1]      = cone[2];
28379371c9d4SSatish Balay                       cone[2]      = tmp;
28389371c9d4SSatish Balay                     }
2839116ded15SMatthew G. Knepley                   }
28409566063dSJacob Faibussowitsch                   PetscCall(DMPlexSetCone(dm, c++, cone));
2841116ded15SMatthew G. Knepley                 }
2842116ded15SMatthew G. Knepley               }
2843116ded15SMatthew G. Knepley             }
2844116ded15SMatthew G. Knepley           }
2845116ded15SMatthew G. Knepley         }
284645da822fSValeria Barra       }
28479566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
28489566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
28499566063dSJacob Faibussowitsch       PetscCall(PetscFree(graph));
2850116ded15SMatthew G. Knepley     }
2851f4d061e9SPierre Jolivet     break;
2852d71ae5a4SJacob Faibussowitsch   default:
2853d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension for sphere: %" PetscInt_FMT, dim);
285465a81367SMatthew G. Knepley   }
285565a81367SMatthew G. Knepley   /* Create coordinates */
28569566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
28579566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
28589566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, embedDim));
28599566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numVerts));
28602829fed8SMatthew G. Knepley   for (v = firstVertex; v < firstVertex + numVerts; ++v) {
28619566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, embedDim));
28629566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, embedDim));
28632829fed8SMatthew G. Knepley   }
28649566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
28659566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
28669566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
28679566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, embedDim));
28689566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
28699566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
28709566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
28719566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
28729371c9d4SSatish Balay   for (v = 0; v < numVerts; ++v)
2873ad540459SPierre Jolivet     for (d = 0; d < embedDim; ++d) coords[v * embedDim + d] = coordsIn[v * embedDim + d];
28749566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
28759566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
28769566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
28779566063dSJacob Faibussowitsch   PetscCall(PetscFree(coordsIn));
287851a74b61SMatthew G. Knepley   {
287951a74b61SMatthew G. Knepley     DM          cdm;
288051a74b61SMatthew G. Knepley     PetscDS     cds;
28819318fe57SMatthew G. Knepley     PetscScalar c = R;
288251a74b61SMatthew G. Knepley 
2883e44f6aebSMatthew G. Knepley     PetscCall(DMPlexCreateCoordinateSpace(dm, 1, PETSC_TRUE, snapToSphere));
28849566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
28859566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
28869566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, 1, &c));
288751a74b61SMatthew G. Knepley   }
288846139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
28899318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
28909566063dSJacob Faibussowitsch   if (simplex) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
28913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
28929318fe57SMatthew G. Knepley }
28939318fe57SMatthew G. Knepley 
2894b7f5c055SJed Brown typedef void (*TPSEvaluateFunc)(const PetscReal[], PetscReal *, PetscReal[], PetscReal (*)[3]);
2895b7f5c055SJed Brown 
2896b7f5c055SJed Brown /*
2897b7f5c055SJed Brown  The Schwarz P implicit surface is
2898b7f5c055SJed Brown 
2899b7f5c055SJed Brown      f(x) = cos(x0) + cos(x1) + cos(x2) = 0
2900b7f5c055SJed Brown */
2901d71ae5a4SJacob Faibussowitsch static void TPSEvaluate_SchwarzP(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3])
2902d71ae5a4SJacob Faibussowitsch {
2903b7f5c055SJed Brown   PetscReal c[3] = {PetscCosReal(y[0] * PETSC_PI), PetscCosReal(y[1] * PETSC_PI), PetscCosReal(y[2] * PETSC_PI)};
2904b7f5c055SJed Brown   PetscReal g[3] = {-PetscSinReal(y[0] * PETSC_PI), -PetscSinReal(y[1] * PETSC_PI), -PetscSinReal(y[2] * PETSC_PI)};
2905b7f5c055SJed Brown   f[0]           = c[0] + c[1] + c[2];
2906b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) {
2907b7f5c055SJed Brown     grad[i] = PETSC_PI * g[i];
2908ad540459SPierre Jolivet     for (PetscInt j = 0; j < 3; j++) hess[i][j] = (i == j) ? -PetscSqr(PETSC_PI) * c[i] : 0.;
2909b7f5c055SJed Brown   }
2910b7f5c055SJed Brown }
2911b7f5c055SJed Brown 
29124663dae6SJed Brown // u[] is a tentative normal on input. Replace with the implicit function gradient in the same direction
2913d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSExtrudeNormalFunc_SchwarzP(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt r, PetscScalar u[], void *ctx)
2914d71ae5a4SJacob Faibussowitsch {
2915ad540459SPierre Jolivet   for (PetscInt i = 0; i < 3; i++) u[i] = -PETSC_PI * PetscSinReal(x[i] * PETSC_PI);
29163ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
29174663dae6SJed Brown }
29184663dae6SJed Brown 
2919b7f5c055SJed Brown /*
2920b7f5c055SJed Brown  The Gyroid implicit surface is
2921b7f5c055SJed Brown 
2922b7f5c055SJed 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)
2923b7f5c055SJed Brown 
2924b7f5c055SJed Brown */
2925d71ae5a4SJacob Faibussowitsch static void TPSEvaluate_Gyroid(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3])
2926d71ae5a4SJacob Faibussowitsch {
2927b7f5c055SJed Brown   PetscReal s[3] = {PetscSinReal(PETSC_PI * y[0]), PetscSinReal(PETSC_PI * (y[1] + .5)), PetscSinReal(PETSC_PI * (y[2] + .25))};
2928b7f5c055SJed Brown   PetscReal c[3] = {PetscCosReal(PETSC_PI * y[0]), PetscCosReal(PETSC_PI * (y[1] + .5)), PetscCosReal(PETSC_PI * (y[2] + .25))};
2929b7f5c055SJed Brown   f[0]           = s[0] * c[1] + s[1] * c[2] + s[2] * c[0];
2930b7f5c055SJed Brown   grad[0]        = PETSC_PI * (c[0] * c[1] - s[2] * s[0]);
2931b7f5c055SJed Brown   grad[1]        = PETSC_PI * (c[1] * c[2] - s[0] * s[1]);
2932b7f5c055SJed Brown   grad[2]        = PETSC_PI * (c[2] * c[0] - s[1] * s[2]);
2933b7f5c055SJed Brown   hess[0][0]     = -PetscSqr(PETSC_PI) * (s[0] * c[1] + s[2] * c[0]);
2934b7f5c055SJed Brown   hess[0][1]     = -PetscSqr(PETSC_PI) * (c[0] * s[1]);
2935b7f5c055SJed Brown   hess[0][2]     = -PetscSqr(PETSC_PI) * (c[2] * s[0]);
2936b7f5c055SJed Brown   hess[1][0]     = -PetscSqr(PETSC_PI) * (s[1] * c[2] + s[0] * c[1]);
2937b7f5c055SJed Brown   hess[1][1]     = -PetscSqr(PETSC_PI) * (c[1] * s[2]);
2938b7f5c055SJed Brown   hess[2][2]     = -PetscSqr(PETSC_PI) * (c[0] * s[1]);
2939b7f5c055SJed Brown   hess[2][0]     = -PetscSqr(PETSC_PI) * (s[2] * c[0] + s[1] * c[2]);
2940b7f5c055SJed Brown   hess[2][1]     = -PetscSqr(PETSC_PI) * (c[2] * s[0]);
2941b7f5c055SJed Brown   hess[2][2]     = -PetscSqr(PETSC_PI) * (c[1] * s[2]);
2942b7f5c055SJed Brown }
2943b7f5c055SJed Brown 
29444663dae6SJed Brown // u[] is a tentative normal on input. Replace with the implicit function gradient in the same direction
2945d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSExtrudeNormalFunc_Gyroid(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt r, PetscScalar u[], void *ctx)
2946d71ae5a4SJacob Faibussowitsch {
29474663dae6SJed Brown   PetscReal s[3] = {PetscSinReal(PETSC_PI * x[0]), PetscSinReal(PETSC_PI * (x[1] + .5)), PetscSinReal(PETSC_PI * (x[2] + .25))};
29484663dae6SJed Brown   PetscReal c[3] = {PetscCosReal(PETSC_PI * x[0]), PetscCosReal(PETSC_PI * (x[1] + .5)), PetscCosReal(PETSC_PI * (x[2] + .25))};
29494663dae6SJed Brown   u[0]           = PETSC_PI * (c[0] * c[1] - s[2] * s[0]);
29504663dae6SJed Brown   u[1]           = PETSC_PI * (c[1] * c[2] - s[0] * s[1]);
29514663dae6SJed Brown   u[2]           = PETSC_PI * (c[2] * c[0] - s[1] * s[2]);
29523ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
29534663dae6SJed Brown }
29544663dae6SJed Brown 
2955b7f5c055SJed Brown /*
2956b7f5c055SJed Brown    We wish to solve
2957b7f5c055SJed Brown 
2958b7f5c055SJed Brown          min_y || y - x ||^2  subject to f(y) = 0
2959b7f5c055SJed Brown 
2960b7f5c055SJed Brown    Let g(y) = grad(f).  The minimization problem is equivalent to asking to satisfy
2961b7f5c055SJed 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
2962b7f5c055SJed Brown    tangent space and ask for both components in the tangent space to be zero.
2963b7f5c055SJed Brown 
2964b7f5c055SJed Brown    Take g to be a column vector and compute the "full QR" factorization Q R = g,
2965b7f5c055SJed Brown    where Q = I - 2 n n^T is a symmetric orthogonal matrix.
2966b7f5c055SJed Brown    The first column of Q is parallel to g so the remaining two columns span the null space.
2967b7f5c055SJed Brown    Let Qn = Q[:,1:] be those remaining columns.  Then Qn Qn^T is an orthogonal projector into the tangent space.
2968da81f932SPierre Jolivet    Since Q is symmetric, this is equivalent to multiplying by Q and taking the last two entries.
2969b7f5c055SJed Brown    In total, we have a system of 3 equations in 3 unknowns:
2970b7f5c055SJed Brown 
2971b7f5c055SJed Brown      f(y) = 0                       1 equation
2972b7f5c055SJed Brown      Qn^T (y - x) = 0               2 equations
2973b7f5c055SJed Brown 
2974b7f5c055SJed Brown    Here, we compute the residual and Jacobian of this system.
2975b7f5c055SJed Brown */
2976d71ae5a4SJacob Faibussowitsch static void TPSNearestPointResJac(TPSEvaluateFunc feval, const PetscScalar x[], const PetscScalar y[], PetscScalar res[], PetscScalar J[])
2977d71ae5a4SJacob Faibussowitsch {
2978b7f5c055SJed Brown   PetscReal yreal[3] = {PetscRealPart(y[0]), PetscRealPart(y[1]), PetscRealPart(y[2])};
2979b7f5c055SJed Brown   PetscReal d[3]     = {PetscRealPart(y[0] - x[0]), PetscRealPart(y[1] - x[1]), PetscRealPart(y[2] - x[2])};
29802f0490c0SSatish Balay   PetscReal f, grad[3], n[3], norm, norm_y[3], nd, nd_y[3], sign;
29819371c9d4SSatish Balay   PetscReal n_y[3][3] = {
29829371c9d4SSatish Balay     {0, 0, 0},
29839371c9d4SSatish Balay     {0, 0, 0},
29849371c9d4SSatish Balay     {0, 0, 0}
29859371c9d4SSatish Balay   };
2986b7f5c055SJed Brown 
2987b7f5c055SJed Brown   feval(yreal, &f, grad, n_y);
2988b7f5c055SJed Brown 
2989b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) n[i] = grad[i];
2990b7f5c055SJed Brown   norm = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2]));
2991ad540459SPierre Jolivet   for (PetscInt i = 0; i < 3; i++) norm_y[i] = 1. / norm * n[i] * n_y[i][i];
2992b7f5c055SJed Brown 
2993b7f5c055SJed Brown   // Define the Householder reflector
2994b7f5c055SJed Brown   sign = n[0] >= 0 ? 1. : -1.;
2995b7f5c055SJed Brown   n[0] += norm * sign;
2996b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) n_y[0][i] += norm_y[i] * sign;
2997b7f5c055SJed Brown 
2998b7f5c055SJed Brown   norm      = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2]));
2999b7f5c055SJed Brown   norm_y[0] = 1. / norm * (n[0] * n_y[0][0]);
3000b7f5c055SJed Brown   norm_y[1] = 1. / norm * (n[0] * n_y[0][1] + n[1] * n_y[1][1]);
3001b7f5c055SJed Brown   norm_y[2] = 1. / norm * (n[0] * n_y[0][2] + n[2] * n_y[2][2]);
3002b7f5c055SJed Brown 
3003b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) {
3004b7f5c055SJed Brown     n[i] /= norm;
3005b7f5c055SJed Brown     for (PetscInt j = 0; j < 3; j++) {
3006b7f5c055SJed Brown       // note that n[i] is n_old[i]/norm when executing the code below
3007b7f5c055SJed Brown       n_y[i][j] = n_y[i][j] / norm - n[i] / norm * norm_y[j];
3008b7f5c055SJed Brown     }
3009b7f5c055SJed Brown   }
3010b7f5c055SJed Brown 
3011b7f5c055SJed Brown   nd = n[0] * d[0] + n[1] * d[1] + n[2] * d[2];
3012b7f5c055SJed 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];
3013b7f5c055SJed Brown 
3014b7f5c055SJed Brown   res[0] = f;
3015b7f5c055SJed Brown   res[1] = d[1] - 2 * n[1] * nd;
3016b7f5c055SJed Brown   res[2] = d[2] - 2 * n[2] * nd;
3017b7f5c055SJed Brown   // J[j][i] is J_{ij} (column major)
3018b7f5c055SJed Brown   for (PetscInt j = 0; j < 3; j++) {
3019b7f5c055SJed Brown     J[0 + j * 3] = grad[j];
3020b7f5c055SJed Brown     J[1 + j * 3] = (j == 1) * 1. - 2 * (n_y[1][j] * nd + n[1] * nd_y[j]);
3021b7f5c055SJed Brown     J[2 + j * 3] = (j == 2) * 1. - 2 * (n_y[2][j] * nd + n[2] * nd_y[j]);
3022b7f5c055SJed Brown   }
3023b7f5c055SJed Brown }
3024b7f5c055SJed Brown 
3025b7f5c055SJed Brown /*
3026b7f5c055SJed Brown    Project x to the nearest point on the implicit surface using Newton's method.
3027b7f5c055SJed Brown */
3028d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSNearestPoint(TPSEvaluateFunc feval, PetscScalar x[])
3029d71ae5a4SJacob Faibussowitsch {
3030b7f5c055SJed Brown   PetscScalar y[3] = {x[0], x[1], x[2]}; // Initial guess
3031b7f5c055SJed Brown 
3032b7f5c055SJed Brown   PetscFunctionBegin;
3033b7f5c055SJed Brown   for (PetscInt iter = 0; iter < 10; iter++) {
3034b7f5c055SJed Brown     PetscScalar res[3], J[9];
3035b7f5c055SJed Brown     PetscReal   resnorm;
3036b7f5c055SJed Brown     TPSNearestPointResJac(feval, x, y, res, J);
3037b7f5c055SJed Brown     resnorm = PetscSqrtReal(PetscSqr(PetscRealPart(res[0])) + PetscSqr(PetscRealPart(res[1])) + PetscSqr(PetscRealPart(res[2])));
3038b7f5c055SJed Brown     if (0) { // Turn on this monitor if you need to confirm quadratic convergence
303963a3b9bcSJacob 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])));
3040b7f5c055SJed Brown     }
3041b7f5c055SJed Brown     if (resnorm < PETSC_SMALL) break;
3042b7f5c055SJed Brown 
3043b7f5c055SJed Brown     // Take the Newton step
30449566063dSJacob Faibussowitsch     PetscCall(PetscKernel_A_gets_inverse_A_3(J, 0., PETSC_FALSE, NULL));
3045b7f5c055SJed Brown     PetscKernel_v_gets_v_minus_A_times_w_3(y, J, res);
3046b7f5c055SJed Brown   }
3047b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) x[i] = y[i];
30483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3049b7f5c055SJed Brown }
3050b7f5c055SJed Brown 
3051b7f5c055SJed Brown const char *const DMPlexTPSTypes[] = {"SCHWARZ_P", "GYROID", "DMPlexTPSType", "DMPLEX_TPS_", NULL};
3052b7f5c055SJed Brown 
3053d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateTPSMesh_Internal(DM dm, DMPlexTPSType tpstype, const PetscInt extent[], const DMBoundaryType periodic[], PetscBool tps_distribute, PetscInt refinements, PetscInt layers, PetscReal thickness)
3054d71ae5a4SJacob Faibussowitsch {
3055b7f5c055SJed Brown   PetscMPIInt rank;
3056b7f5c055SJed Brown   PetscInt    topoDim = 2, spaceDim = 3, numFaces = 0, numVertices = 0, numEdges = 0;
3057b7f5c055SJed Brown   PetscInt(*edges)[2] = NULL, *edgeSets = NULL;
3058b7f5c055SJed Brown   PetscInt           *cells_flat = NULL;
3059b7f5c055SJed Brown   PetscReal          *vtxCoords  = NULL;
3060b7f5c055SJed Brown   TPSEvaluateFunc     evalFunc   = NULL;
30618434afd1SBarry Smith   PetscSimplePointFn *normalFunc = NULL;
3062b7f5c055SJed Brown   DMLabel             label;
3063b7f5c055SJed Brown 
3064b7f5c055SJed Brown   PetscFunctionBegin;
306546139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
30669566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
306763a3b9bcSJacob 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);
3068b7f5c055SJed Brown   switch (tpstype) {
3069b7f5c055SJed Brown   case DMPLEX_TPS_SCHWARZ_P:
3070b7f5c055SJed 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");
3071c5853193SPierre Jolivet     if (rank == 0) {
3072b7f5c055SJed Brown       PetscInt(*cells)[6][4][4] = NULL; // [junction, junction-face, cell, conn]
3073b7f5c055SJed Brown       PetscInt  Njunctions = 0, Ncuts = 0, Npipes[3], vcount;
3074b7f5c055SJed Brown       PetscReal L = 1;
3075b7f5c055SJed Brown 
3076b7f5c055SJed Brown       Npipes[0]   = (extent[0] + 1) * extent[1] * extent[2];
3077b7f5c055SJed Brown       Npipes[1]   = extent[0] * (extent[1] + 1) * extent[2];
3078b7f5c055SJed Brown       Npipes[2]   = extent[0] * extent[1] * (extent[2] + 1);
3079b7f5c055SJed Brown       Njunctions  = extent[0] * extent[1] * extent[2];
3080b7f5c055SJed Brown       Ncuts       = 2 * (extent[0] * extent[1] + extent[1] * extent[2] + extent[2] * extent[0]);
3081b7f5c055SJed Brown       numVertices = 4 * (Npipes[0] + Npipes[1] + Npipes[2]) + 8 * Njunctions;
30829566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(3 * numVertices, &vtxCoords));
30839566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Njunctions, &cells));
30849566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Ncuts * 4, &edges));
30859566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Ncuts * 4, &edgeSets));
3086b7f5c055SJed Brown       // x-normal pipes
3087b7f5c055SJed Brown       vcount = 0;
3088b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0] + 1; i++) {
3089b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3090b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2]; k++) {
3091b7f5c055SJed Brown             for (PetscInt l = 0; l < 4; l++) {
3092b7f5c055SJed Brown               vtxCoords[vcount++] = (2 * i - 1) * L;
3093b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * j * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3094b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * k * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3095b7f5c055SJed Brown             }
3096b7f5c055SJed Brown           }
3097b7f5c055SJed Brown         }
3098b7f5c055SJed Brown       }
3099b7f5c055SJed Brown       // y-normal pipes
3100b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0]; i++) {
3101b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1] + 1; j++) {
3102b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2]; k++) {
3103b7f5c055SJed Brown             for (PetscInt l = 0; l < 4; l++) {
3104b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * i * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3105b7f5c055SJed Brown               vtxCoords[vcount++] = (2 * j - 1) * L;
3106b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * k * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3107b7f5c055SJed Brown             }
3108b7f5c055SJed Brown           }
3109b7f5c055SJed Brown         }
3110b7f5c055SJed Brown       }
3111b7f5c055SJed Brown       // z-normal pipes
3112b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0]; i++) {
3113b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3114b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2] + 1; k++) {
3115b7f5c055SJed Brown             for (PetscInt l = 0; l < 4; l++) {
3116b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * i * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3117b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * j * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3118b7f5c055SJed Brown               vtxCoords[vcount++] = (2 * k - 1) * L;
3119b7f5c055SJed Brown             }
3120b7f5c055SJed Brown           }
3121b7f5c055SJed Brown         }
3122b7f5c055SJed Brown       }
3123b7f5c055SJed Brown       // junctions
3124b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0]; i++) {
3125b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3126b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2]; k++) {
3127b7f5c055SJed Brown             const PetscInt J = (i * extent[1] + j) * extent[2] + k, Jvoff = (Npipes[0] + Npipes[1] + Npipes[2]) * 4 + J * 8;
3128b7f5c055SJed Brown             PetscCheck(vcount / 3 == Jvoff, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unexpected vertex count");
3129b7f5c055SJed Brown             for (PetscInt ii = 0; ii < 2; ii++) {
3130b7f5c055SJed Brown               for (PetscInt jj = 0; jj < 2; jj++) {
3131b7f5c055SJed Brown                 for (PetscInt kk = 0; kk < 2; kk++) {
3132b7f5c055SJed Brown                   double Ls           = (1 - sqrt(2) / 4) * L;
3133b7f5c055SJed Brown                   vtxCoords[vcount++] = 2 * i * L + (2 * ii - 1) * Ls;
3134b7f5c055SJed Brown                   vtxCoords[vcount++] = 2 * j * L + (2 * jj - 1) * Ls;
3135b7f5c055SJed Brown                   vtxCoords[vcount++] = 2 * k * L + (2 * kk - 1) * Ls;
3136b7f5c055SJed Brown                 }
3137b7f5c055SJed Brown               }
3138b7f5c055SJed Brown             }
3139b7f5c055SJed Brown             const PetscInt jfaces[3][2][4] = {
3140b7f5c055SJed Brown               {{3, 1, 0, 2}, {7, 5, 4, 6}}, // x-aligned
3141b7f5c055SJed Brown               {{5, 4, 0, 1}, {7, 6, 2, 3}}, // y-aligned
3142b7f5c055SJed Brown               {{6, 2, 0, 4}, {7, 3, 1, 5}}  // z-aligned
3143b7f5c055SJed Brown             };
3144b7f5c055SJed Brown             const PetscInt pipe_lo[3] = {// vertex numbers of pipes
31459371c9d4SSatish 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};
3146b7f5c055SJed Brown             const PetscInt pipe_hi[3] = {// vertex numbers of pipes
31479371c9d4SSatish 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};
3148b7f5c055SJed Brown             for (PetscInt dir = 0; dir < 3; dir++) { // x,y,z
3149b7f5c055SJed Brown               const PetscInt ijk[3] = {i, j, k};
3150b7f5c055SJed Brown               for (PetscInt l = 0; l < 4; l++) { // rotations
3151b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][0] = pipe_lo[dir] + l;
3152b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][1] = Jvoff + jfaces[dir][0][l];
3153b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][2] = Jvoff + jfaces[dir][0][(l - 1 + 4) % 4];
3154b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][3] = pipe_lo[dir] + (l - 1 + 4) % 4;
3155b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][0] = Jvoff + jfaces[dir][1][l];
3156b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][1] = pipe_hi[dir] + l;
3157b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][2] = pipe_hi[dir] + (l - 1 + 4) % 4;
3158b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][3] = Jvoff + jfaces[dir][1][(l - 1 + 4) % 4];
3159b7f5c055SJed Brown                 if (ijk[dir] == 0) {
3160b7f5c055SJed Brown                   edges[numEdges][0] = pipe_lo[dir] + l;
3161b7f5c055SJed Brown                   edges[numEdges][1] = pipe_lo[dir] + (l + 1) % 4;
3162b7f5c055SJed Brown                   edgeSets[numEdges] = dir * 2 + 1;
3163b7f5c055SJed Brown                   numEdges++;
3164b7f5c055SJed Brown                 }
3165b7f5c055SJed Brown                 if (ijk[dir] + 1 == extent[dir]) {
3166b7f5c055SJed Brown                   edges[numEdges][0] = pipe_hi[dir] + l;
3167b7f5c055SJed Brown                   edges[numEdges][1] = pipe_hi[dir] + (l + 1) % 4;
3168b7f5c055SJed Brown                   edgeSets[numEdges] = dir * 2 + 2;
3169b7f5c055SJed Brown                   numEdges++;
3170b7f5c055SJed Brown                 }
3171b7f5c055SJed Brown               }
3172b7f5c055SJed Brown             }
3173b7f5c055SJed Brown           }
3174b7f5c055SJed Brown         }
3175b7f5c055SJed Brown       }
317663a3b9bcSJacob Faibussowitsch       PetscCheck(numEdges == Ncuts * 4, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Edge count %" PetscInt_FMT " incompatible with number of cuts %" PetscInt_FMT, numEdges, Ncuts);
3177b7f5c055SJed Brown       numFaces   = 24 * Njunctions;
3178b7f5c055SJed Brown       cells_flat = cells[0][0][0];
3179b7f5c055SJed Brown     }
3180b7f5c055SJed Brown     evalFunc   = TPSEvaluate_SchwarzP;
31814663dae6SJed Brown     normalFunc = TPSExtrudeNormalFunc_SchwarzP;
3182b7f5c055SJed Brown     break;
3183b7f5c055SJed Brown   case DMPLEX_TPS_GYROID:
3184c5853193SPierre Jolivet     if (rank == 0) {
3185b7f5c055SJed Brown       // This is a coarse mesh approximation of the gyroid shifted to being the zero of the level set
3186b7f5c055SJed Brown       //
3187b7f5c055SJed 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)
3188b7f5c055SJed Brown       //
3189b7f5c055SJed Brown       // on the cell [0,2]^3.
3190b7f5c055SJed Brown       //
3191b7f5c055SJed Brown       // Think about dividing that cell into four columns, and focus on the column [0,1]x[0,1]x[0,2].
3192b7f5c055SJed Brown       // If you looked at the gyroid in that column at different slices of z you would see that it kind of spins
3193b7f5c055SJed Brown       // like a boomerang:
3194b7f5c055SJed Brown       //
3195b7f5c055SJed Brown       //     z = 0          z = 1/4        z = 1/2        z = 3/4     //
3196b7f5c055SJed Brown       //     -----          -------        -------        -------     //
3197b7f5c055SJed Brown       //                                                              //
3198b7f5c055SJed Brown       //     +       +      +       +      +       +      +   \   +   //
3199b7f5c055SJed Brown       //      \                                   /            \      //
3200b7f5c055SJed Brown       //       \            `-_   _-'            /              }     //
3201b7f5c055SJed Brown       //        *-_            `-'            _-'              /      //
3202b7f5c055SJed Brown       //     +     `-+      +       +      +-'     +      +   /   +   //
3203b7f5c055SJed Brown       //                                                              //
3204b7f5c055SJed Brown       //                                                              //
3205b7f5c055SJed Brown       //     z = 1          z = 5/4        z = 3/2        z = 7/4     //
3206b7f5c055SJed Brown       //     -----          -------        -------        -------     //
3207b7f5c055SJed Brown       //                                                              //
3208b7f5c055SJed Brown       //     +-_     +      +       +      +     _-+      +   /   +   //
3209b7f5c055SJed Brown       //        `-_            _-_            _-`            /        //
3210b7f5c055SJed Brown       //           \        _-'   `-_        /              {         //
3211b7f5c055SJed Brown       //            \                       /                \        //
3212b7f5c055SJed Brown       //     +       +      +       +      +       +      +   \   +   //
3213b7f5c055SJed Brown       //
3214b7f5c055SJed Brown       //
3215b7f5c055SJed Brown       // This course mesh approximates each of these slices by two line segments,
3216b7f5c055SJed Brown       // and then connects the segments in consecutive layers with quadrilateral faces.
3217b7f5c055SJed Brown       // All of the end points of the segments are multiples of 1/4 except for the
3218b7f5c055SJed Brown       // point * in the picture for z = 0 above and the similar points in other layers.
3219b7f5c055SJed Brown       // That point is at (gamma, gamma, 0), where gamma is calculated below.
3220b7f5c055SJed Brown       //
3221b7f5c055SJed Brown       // The column  [1,2]x[1,2]x[0,2] looks the same as this column;
3222b7f5c055SJed Brown       // The columns [1,2]x[0,1]x[0,2] and [0,1]x[1,2]x[0,2] are mirror images.
3223b7f5c055SJed Brown       //
3224b7f5c055SJed Brown       // As for how this method turned into the names given to the vertices:
3225b7f5c055SJed Brown       // that was not systematic, it was just the way it worked out in my handwritten notes.
3226b7f5c055SJed Brown 
3227b7f5c055SJed Brown       PetscInt facesPerBlock = 64;
3228b7f5c055SJed Brown       PetscInt vertsPerBlock = 56;
3229b7f5c055SJed Brown       PetscInt extentPlus[3];
3230b7f5c055SJed Brown       PetscInt numBlocks, numBlocksPlus;
32319371c9d4SSatish 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;
32329371c9d4SSatish Balay       const PetscInt pattern[64][4] = {
32339371c9d4SSatish Balay         /* face to vertex within the coarse discretization of a single gyroid block */
3234b7f5c055SJed Brown         /* layer 0 */
32359371c9d4SSatish Balay         {A,           C,           K,           G          },
32369371c9d4SSatish Balay         {C,           B,           II,          K          },
32379371c9d4SSatish Balay         {D,           A,           H,           L          },
32389371c9d4SSatish Balay         {B + 56 * 1,  D,           L,           J          },
32399371c9d4SSatish Balay         {E,           B + 56 * 1,  J,           N          },
32409371c9d4SSatish Balay         {A + 56 * 2,  E,           N,           H + 56 * 2 },
32419371c9d4SSatish Balay         {F,           A + 56 * 2,  G + 56 * 2,  M          },
32429371c9d4SSatish Balay         {B,           F,           M,           II         },
3243b7f5c055SJed Brown         /* layer 1 */
32449371c9d4SSatish Balay         {G,           K,           Q,           O          },
32459371c9d4SSatish Balay         {K,           II,          P,           Q          },
32469371c9d4SSatish Balay         {L,           H,           O + 56 * 1,  R          },
32479371c9d4SSatish Balay         {J,           L,           R,           P          },
32489371c9d4SSatish Balay         {N,           J,           P,           S          },
32499371c9d4SSatish Balay         {H + 56 * 2,  N,           S,           O + 56 * 3 },
32509371c9d4SSatish Balay         {M,           G + 56 * 2,  O + 56 * 2,  T          },
32519371c9d4SSatish Balay         {II,          M,           T,           P          },
3252b7f5c055SJed Brown         /* layer 2 */
32539371c9d4SSatish Balay         {O,           Q,           Y,           U          },
32549371c9d4SSatish Balay         {Q,           P,           W,           Y          },
32559371c9d4SSatish Balay         {R,           O + 56 * 1,  U + 56 * 1,  Ap         },
32569371c9d4SSatish Balay         {P,           R,           Ap,          W          },
32579371c9d4SSatish Balay         {S,           P,           X,           Bp         },
32589371c9d4SSatish Balay         {O + 56 * 3,  S,           Bp,          V + 56 * 1 },
32599371c9d4SSatish Balay         {T,           O + 56 * 2,  V,           Z          },
32609371c9d4SSatish Balay         {P,           T,           Z,           X          },
3261b7f5c055SJed Brown         /* layer 3 */
32629371c9d4SSatish Balay         {U,           Y,           Ep,          Dp         },
32639371c9d4SSatish Balay         {Y,           W,           Cp,          Ep         },
32649371c9d4SSatish Balay         {Ap,          U + 56 * 1,  Dp + 56 * 1, Gp         },
32659371c9d4SSatish Balay         {W,           Ap,          Gp,          Cp         },
32669371c9d4SSatish Balay         {Bp,          X,           Cp + 56 * 2, Fp         },
32679371c9d4SSatish Balay         {V + 56 * 1,  Bp,          Fp,          Dp + 56 * 1},
32689371c9d4SSatish Balay         {Z,           V,           Dp,          Hp         },
32699371c9d4SSatish Balay         {X,           Z,           Hp,          Cp + 56 * 2},
3270b7f5c055SJed Brown         /* layer 4 */
32719371c9d4SSatish Balay         {Dp,          Ep,          Mp,          Kp         },
32729371c9d4SSatish Balay         {Ep,          Cp,          Ip,          Mp         },
32739371c9d4SSatish Balay         {Gp,          Dp + 56 * 1, Lp,          Np         },
32749371c9d4SSatish Balay         {Cp,          Gp,          Np,          Jp         },
32759371c9d4SSatish Balay         {Fp,          Cp + 56 * 2, Jp + 56 * 2, Pp         },
32769371c9d4SSatish Balay         {Dp + 56 * 1, Fp,          Pp,          Lp         },
32779371c9d4SSatish Balay         {Hp,          Dp,          Kp,          Op         },
32789371c9d4SSatish Balay         {Cp + 56 * 2, Hp,          Op,          Ip + 56 * 2},
3279b7f5c055SJed Brown         /* layer 5 */
32809371c9d4SSatish Balay         {Kp,          Mp,          Sp,          Rp         },
32819371c9d4SSatish Balay         {Mp,          Ip,          Qp,          Sp         },
32829371c9d4SSatish Balay         {Np,          Lp,          Rp,          Tp         },
32839371c9d4SSatish Balay         {Jp,          Np,          Tp,          Qp + 56 * 1},
32849371c9d4SSatish Balay         {Pp,          Jp + 56 * 2, Qp + 56 * 3, Up         },
32859371c9d4SSatish Balay         {Lp,          Pp,          Up,          Rp         },
32869371c9d4SSatish Balay         {Op,          Kp,          Rp,          Vp         },
32879371c9d4SSatish Balay         {Ip + 56 * 2, Op,          Vp,          Qp + 56 * 2},
3288b7f5c055SJed Brown         /* layer 6 */
32899371c9d4SSatish Balay         {Rp,          Sp,          Aq,          Yp         },
32909371c9d4SSatish Balay         {Sp,          Qp,          Wp,          Aq         },
32919371c9d4SSatish Balay         {Tp,          Rp,          Yp,          Cq         },
32929371c9d4SSatish Balay         {Qp + 56 * 1, Tp,          Cq,          Wp + 56 * 1},
32939371c9d4SSatish Balay         {Up,          Qp + 56 * 3, Xp + 56 * 1, Dq         },
32949371c9d4SSatish Balay         {Rp,          Up,          Dq,          Zp         },
32959371c9d4SSatish Balay         {Vp,          Rp,          Zp,          Bq         },
32969371c9d4SSatish Balay         {Qp + 56 * 2, Vp,          Bq,          Xp         },
3297b7f5c055SJed Brown         /* layer 7 (the top is the periodic image of the bottom of layer 0) */
32989371c9d4SSatish Balay         {Yp,          Aq,          C + 56 * 4,  A + 56 * 4 },
32999371c9d4SSatish Balay         {Aq,          Wp,          B + 56 * 4,  C + 56 * 4 },
33009371c9d4SSatish Balay         {Cq,          Yp,          A + 56 * 4,  D + 56 * 4 },
33019371c9d4SSatish Balay         {Wp + 56 * 1, Cq,          D + 56 * 4,  B + 56 * 5 },
33029371c9d4SSatish Balay         {Dq,          Xp + 56 * 1, B + 56 * 5,  E + 56 * 4 },
33039371c9d4SSatish Balay         {Zp,          Dq,          E + 56 * 4,  A + 56 * 6 },
33049371c9d4SSatish Balay         {Bq,          Zp,          A + 56 * 6,  F + 56 * 4 },
33059371c9d4SSatish Balay         {Xp,          Bq,          F + 56 * 4,  B + 56 * 4 }
3306b7f5c055SJed Brown       };
3307b7f5c055SJed Brown       const PetscReal gamma                = PetscAcosReal((PetscSqrtReal(3.) - 1.) / PetscSqrtReal(2.)) / PETSC_PI;
33089371c9d4SSatish Balay       const PetscReal patternCoords[56][3] = {
3309bee3fc89SBarry Smith         {1.,        0.,        0.  }, /* A  */
3310bee3fc89SBarry Smith         {0.,        1.,        0.  }, /* B  */
3311bee3fc89SBarry Smith         {gamma,     gamma,     0.  }, /* C  */
3312bee3fc89SBarry Smith         {1 + gamma, 1 - gamma, 0.  }, /* D  */
3313bee3fc89SBarry Smith         {2 - gamma, 2 - gamma, 0.  }, /* E  */
3314bee3fc89SBarry Smith         {1 - gamma, 1 + gamma, 0.  }, /* F  */
3315b7f5c055SJed Brown 
3316bee3fc89SBarry Smith         {.5,        0,         .25 }, /* G  */
3317bee3fc89SBarry Smith         {1.5,       0.,        .25 }, /* H  */
3318bee3fc89SBarry Smith         {.5,        1.,        .25 }, /* II */
3319bee3fc89SBarry Smith         {1.5,       1.,        .25 }, /* J  */
3320bee3fc89SBarry Smith         {.25,       .5,        .25 }, /* K  */
3321bee3fc89SBarry Smith         {1.25,      .5,        .25 }, /* L  */
3322bee3fc89SBarry Smith         {.75,       1.5,       .25 }, /* M  */
3323bee3fc89SBarry Smith         {1.75,      1.5,       .25 }, /* N  */
3324b7f5c055SJed Brown 
3325bee3fc89SBarry Smith         {0.,        0.,        .5  }, /* O  */
3326bee3fc89SBarry Smith         {1.,        1.,        .5  }, /* P  */
3327bee3fc89SBarry Smith         {gamma,     1 - gamma, .5  }, /* Q  */
3328bee3fc89SBarry Smith         {1 + gamma, gamma,     .5  }, /* R  */
3329bee3fc89SBarry Smith         {2 - gamma, 1 + gamma, .5  }, /* S  */
3330bee3fc89SBarry Smith         {1 - gamma, 2 - gamma, .5  }, /* T  */
3331b7f5c055SJed Brown 
3332bee3fc89SBarry Smith         {0.,        .5,        .75 }, /* U  */
3333bee3fc89SBarry Smith         {0.,        1.5,       .75 }, /* V  */
3334bee3fc89SBarry Smith         {1.,        .5,        .75 }, /* W  */
3335bee3fc89SBarry Smith         {1.,        1.5,       .75 }, /* X  */
3336bee3fc89SBarry Smith         {.5,        .75,       .75 }, /* Y  */
3337bee3fc89SBarry Smith         {.5,        1.75,      .75 }, /* Z  */
3338bee3fc89SBarry Smith         {1.5,       .25,       .75 }, /* Ap */
3339bee3fc89SBarry Smith         {1.5,       1.25,      .75 }, /* Bp */
3340b7f5c055SJed Brown 
3341bee3fc89SBarry Smith         {1.,        0.,        1.  }, /* Cp */
3342bee3fc89SBarry Smith         {0.,        1.,        1.  }, /* Dp */
3343bee3fc89SBarry Smith         {1 - gamma, 1 - gamma, 1.  }, /* Ep */
3344bee3fc89SBarry Smith         {1 + gamma, 1 + gamma, 1.  }, /* Fp */
3345bee3fc89SBarry Smith         {2 - gamma, gamma,     1.  }, /* Gp */
3346bee3fc89SBarry Smith         {gamma,     2 - gamma, 1.  }, /* Hp */
3347b7f5c055SJed Brown 
3348bee3fc89SBarry Smith         {.5,        0.,        1.25}, /* Ip */
3349bee3fc89SBarry Smith         {1.5,       0.,        1.25}, /* Jp */
3350bee3fc89SBarry Smith         {.5,        1.,        1.25}, /* Kp */
3351bee3fc89SBarry Smith         {1.5,       1.,        1.25}, /* Lp */
3352bee3fc89SBarry Smith         {.75,       .5,        1.25}, /* Mp */
3353bee3fc89SBarry Smith         {1.75,      .5,        1.25}, /* Np */
3354bee3fc89SBarry Smith         {.25,       1.5,       1.25}, /* Op */
3355bee3fc89SBarry Smith         {1.25,      1.5,       1.25}, /* Pp */
3356b7f5c055SJed Brown 
3357bee3fc89SBarry Smith         {0.,        0.,        1.5 }, /* Qp */
3358bee3fc89SBarry Smith         {1.,        1.,        1.5 }, /* Rp */
3359bee3fc89SBarry Smith         {1 - gamma, gamma,     1.5 }, /* Sp */
3360bee3fc89SBarry Smith         {2 - gamma, 1 - gamma, 1.5 }, /* Tp */
3361bee3fc89SBarry Smith         {1 + gamma, 2 - gamma, 1.5 }, /* Up */
3362bee3fc89SBarry Smith         {gamma,     1 + gamma, 1.5 }, /* Vp */
3363b7f5c055SJed Brown 
3364bee3fc89SBarry Smith         {0.,        .5,        1.75}, /* Wp */
3365bee3fc89SBarry Smith         {0.,        1.5,       1.75}, /* Xp */
3366bee3fc89SBarry Smith         {1.,        .5,        1.75}, /* Yp */
3367bee3fc89SBarry Smith         {1.,        1.5,       1.75}, /* Zp */
3368bee3fc89SBarry Smith         {.5,        .25,       1.75}, /* Aq */
3369bee3fc89SBarry Smith         {.5,        1.25,      1.75}, /* Bq */
3370bee3fc89SBarry Smith         {1.5,       .75,       1.75}, /* Cq */
3371bee3fc89SBarry Smith         {1.5,       1.75,      1.75}, /* Dq */
3372b7f5c055SJed Brown       };
3373b7f5c055SJed Brown       PetscInt(*cells)[64][4] = NULL;
3374b7f5c055SJed Brown       PetscBool *seen;
3375b7f5c055SJed Brown       PetscInt  *vertToTrueVert;
3376b7f5c055SJed Brown       PetscInt   count;
3377b7f5c055SJed Brown 
3378b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) extentPlus[i] = extent[i] + 1;
3379b7f5c055SJed Brown       numBlocks = 1;
3380b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) numBlocks *= extent[i];
3381b7f5c055SJed Brown       numBlocksPlus = 1;
3382b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) numBlocksPlus *= extentPlus[i];
3383b7f5c055SJed Brown       numFaces = numBlocks * facesPerBlock;
33849566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numBlocks, &cells));
33859566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numBlocksPlus * vertsPerBlock, &seen));
3386b7f5c055SJed Brown       for (PetscInt k = 0; k < extent[2]; k++) {
3387b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3388b7f5c055SJed Brown           for (PetscInt i = 0; i < extent[0]; i++) {
3389b7f5c055SJed Brown             for (PetscInt f = 0; f < facesPerBlock; f++) {
3390b7f5c055SJed Brown               for (PetscInt v = 0; v < 4; v++) {
3391b7f5c055SJed Brown                 PetscInt vertRaw     = pattern[f][v];
3392b7f5c055SJed Brown                 PetscInt blockidx    = vertRaw / 56;
3393b7f5c055SJed Brown                 PetscInt patternvert = vertRaw % 56;
3394b7f5c055SJed Brown                 PetscInt xplus       = (blockidx & 1);
3395b7f5c055SJed Brown                 PetscInt yplus       = (blockidx & 2) >> 1;
3396b7f5c055SJed Brown                 PetscInt zplus       = (blockidx & 4) >> 2;
3397b7f5c055SJed Brown                 PetscInt zcoord      = (periodic && periodic[2] == DM_BOUNDARY_PERIODIC) ? ((k + zplus) % extent[2]) : (k + zplus);
3398b7f5c055SJed Brown                 PetscInt ycoord      = (periodic && periodic[1] == DM_BOUNDARY_PERIODIC) ? ((j + yplus) % extent[1]) : (j + yplus);
3399b7f5c055SJed Brown                 PetscInt xcoord      = (periodic && periodic[0] == DM_BOUNDARY_PERIODIC) ? ((i + xplus) % extent[0]) : (i + xplus);
3400b7f5c055SJed Brown                 PetscInt vert        = ((zcoord * extentPlus[1] + ycoord) * extentPlus[0] + xcoord) * 56 + patternvert;
3401b7f5c055SJed Brown 
3402b7f5c055SJed Brown                 cells[(k * extent[1] + j) * extent[0] + i][f][v] = vert;
3403b7f5c055SJed Brown                 seen[vert]                                       = PETSC_TRUE;
3404b7f5c055SJed Brown               }
3405b7f5c055SJed Brown             }
3406b7f5c055SJed Brown           }
3407b7f5c055SJed Brown         }
3408b7f5c055SJed Brown       }
34099371c9d4SSatish Balay       for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++)
34109371c9d4SSatish Balay         if (seen[i]) numVertices++;
3411b7f5c055SJed Brown       count = 0;
34129566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numBlocksPlus * vertsPerBlock, &vertToTrueVert));
34139566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numVertices * 3, &vtxCoords));
3414b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++) vertToTrueVert[i] = -1;
3415b7f5c055SJed Brown       for (PetscInt k = 0; k < extentPlus[2]; k++) {
3416b7f5c055SJed Brown         for (PetscInt j = 0; j < extentPlus[1]; j++) {
3417b7f5c055SJed Brown           for (PetscInt i = 0; i < extentPlus[0]; i++) {
3418b7f5c055SJed Brown             for (PetscInt v = 0; v < vertsPerBlock; v++) {
3419b7f5c055SJed Brown               PetscInt vIdx = ((k * extentPlus[1] + j) * extentPlus[0] + i) * vertsPerBlock + v;
3420b7f5c055SJed Brown 
3421b7f5c055SJed Brown               if (seen[vIdx]) {
3422b7f5c055SJed Brown                 PetscInt thisVert;
3423b7f5c055SJed Brown 
3424b7f5c055SJed Brown                 vertToTrueVert[vIdx] = thisVert = count++;
3425b7f5c055SJed Brown 
3426b7f5c055SJed Brown                 for (PetscInt d = 0; d < 3; d++) vtxCoords[3 * thisVert + d] = patternCoords[v][d];
3427b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 0] += i * 2;
3428b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 1] += j * 2;
3429b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 2] += k * 2;
3430b7f5c055SJed Brown               }
3431b7f5c055SJed Brown             }
3432b7f5c055SJed Brown           }
3433b7f5c055SJed Brown         }
3434b7f5c055SJed Brown       }
3435b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocks; i++) {
3436b7f5c055SJed Brown         for (PetscInt f = 0; f < facesPerBlock; f++) {
3437ad540459SPierre Jolivet           for (PetscInt v = 0; v < 4; v++) cells[i][f][v] = vertToTrueVert[cells[i][f][v]];
3438b7f5c055SJed Brown         }
3439b7f5c055SJed Brown       }
34409566063dSJacob Faibussowitsch       PetscCall(PetscFree(vertToTrueVert));
34419566063dSJacob Faibussowitsch       PetscCall(PetscFree(seen));
3442b7f5c055SJed Brown       cells_flat = cells[0][0];
3443b7f5c055SJed Brown       numEdges   = 0;
3444b7f5c055SJed Brown       for (PetscInt i = 0; i < numFaces; i++) {
3445b7f5c055SJed Brown         for (PetscInt e = 0; e < 4; e++) {
3446b7f5c055SJed Brown           PetscInt         ev[]       = {cells_flat[i * 4 + e], cells_flat[i * 4 + ((e + 1) % 4)]};
3447b7f5c055SJed Brown           const PetscReal *evCoords[] = {&vtxCoords[3 * ev[0]], &vtxCoords[3 * ev[1]]};
3448b7f5c055SJed Brown 
3449b7f5c055SJed Brown           for (PetscInt d = 0; d < 3; d++) {
3450b7f5c055SJed Brown             if (!periodic || periodic[0] != DM_BOUNDARY_PERIODIC) {
3451b7f5c055SJed Brown               if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) numEdges++;
3452b7f5c055SJed Brown               if (evCoords[0][d] == 2. * extent[d] && evCoords[1][d] == 2. * extent[d]) numEdges++;
3453b7f5c055SJed Brown             }
3454b7f5c055SJed Brown           }
3455b7f5c055SJed Brown         }
3456b7f5c055SJed Brown       }
34579566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numEdges, &edges));
34589566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numEdges, &edgeSets));
3459b7f5c055SJed Brown       for (PetscInt edge = 0, i = 0; i < numFaces; i++) {
3460b7f5c055SJed Brown         for (PetscInt e = 0; e < 4; e++) {
3461b7f5c055SJed Brown           PetscInt         ev[]       = {cells_flat[i * 4 + e], cells_flat[i * 4 + ((e + 1) % 4)]};
3462b7f5c055SJed Brown           const PetscReal *evCoords[] = {&vtxCoords[3 * ev[0]], &vtxCoords[3 * ev[1]]};
3463b7f5c055SJed Brown 
3464b7f5c055SJed Brown           for (PetscInt d = 0; d < 3; d++) {
3465b7f5c055SJed Brown             if (!periodic || periodic[d] != DM_BOUNDARY_PERIODIC) {
3466b7f5c055SJed Brown               if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) {
3467b7f5c055SJed Brown                 edges[edge][0]   = ev[0];
3468b7f5c055SJed Brown                 edges[edge][1]   = ev[1];
3469b7f5c055SJed Brown                 edgeSets[edge++] = 2 * d;
3470b7f5c055SJed Brown               }
3471b7f5c055SJed Brown               if (evCoords[0][d] == 2. * extent[d] && evCoords[1][d] == 2. * extent[d]) {
3472b7f5c055SJed Brown                 edges[edge][0]   = ev[0];
3473b7f5c055SJed Brown                 edges[edge][1]   = ev[1];
3474b7f5c055SJed Brown                 edgeSets[edge++] = 2 * d + 1;
3475b7f5c055SJed Brown               }
3476b7f5c055SJed Brown             }
3477b7f5c055SJed Brown           }
3478b7f5c055SJed Brown         }
3479b7f5c055SJed Brown       }
3480b7f5c055SJed Brown     }
3481b7f5c055SJed Brown     evalFunc   = TPSEvaluate_Gyroid;
34824663dae6SJed Brown     normalFunc = TPSExtrudeNormalFunc_Gyroid;
3483b7f5c055SJed Brown     break;
3484b7f5c055SJed Brown   }
3485b7f5c055SJed Brown 
34869566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, topoDim));
3487c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildFromCellList(dm, numFaces, numVertices, 4, cells_flat));
34889566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildFromCellList(dm, 0, 0, 0, NULL));
34899566063dSJacob Faibussowitsch   PetscCall(PetscFree(cells_flat));
3490b7f5c055SJed Brown   {
3491b7f5c055SJed Brown     DM idm;
34929566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(dm, &idm));
349369d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &idm));
3494b7f5c055SJed Brown   }
3495c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildCoordinatesFromCellList(dm, spaceDim, vtxCoords));
34969566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildCoordinatesFromCellList(dm, spaceDim, NULL));
34979566063dSJacob Faibussowitsch   PetscCall(PetscFree(vtxCoords));
3498b7f5c055SJed Brown 
34999566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
35009566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "Face Sets", &label));
3501b7f5c055SJed Brown   for (PetscInt e = 0; e < numEdges; e++) {
3502b7f5c055SJed Brown     PetscInt        njoin;
3503b7f5c055SJed Brown     const PetscInt *join, verts[] = {numFaces + edges[e][0], numFaces + edges[e][1]};
35049566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, 2, verts, &njoin, &join));
350563a3b9bcSJacob 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]);
35069566063dSJacob Faibussowitsch     PetscCall(DMLabelSetValue(label, join[0], edgeSets[e]));
35079566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, 2, verts, &njoin, &join));
3508b7f5c055SJed Brown   }
35099566063dSJacob Faibussowitsch   PetscCall(PetscFree(edges));
35109566063dSJacob Faibussowitsch   PetscCall(PetscFree(edgeSets));
35111436d7faSJed Brown   if (tps_distribute) {
35121436d7faSJed Brown     DM               pdm = NULL;
35131436d7faSJed Brown     PetscPartitioner part;
35141436d7faSJed Brown 
35159566063dSJacob Faibussowitsch     PetscCall(DMPlexGetPartitioner(dm, &part));
35169566063dSJacob Faibussowitsch     PetscCall(PetscPartitionerSetFromOptions(part));
35179566063dSJacob Faibussowitsch     PetscCall(DMPlexDistribute(dm, 0, NULL, &pdm));
351848a46eb9SPierre Jolivet     if (pdm) PetscCall(DMPlexReplace_Internal(dm, &pdm));
35191436d7faSJed Brown     // Do not auto-distribute again
35209566063dSJacob Faibussowitsch     PetscCall(DMPlexDistributeSetDefault(dm, PETSC_FALSE));
35211436d7faSJed Brown   }
3522b7f5c055SJed Brown 
35239566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
3524b7f5c055SJed Brown   for (PetscInt refine = 0; refine < refinements; refine++) {
3525b7f5c055SJed Brown     PetscInt     m;
3526b7f5c055SJed Brown     DM           dmf;
3527b7f5c055SJed Brown     Vec          X;
3528b7f5c055SJed Brown     PetscScalar *x;
35299566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, MPI_COMM_NULL, &dmf));
353069d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &dmf));
3531b7f5c055SJed Brown 
35329566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &X));
35339566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(X, &m));
35349566063dSJacob Faibussowitsch     PetscCall(VecGetArray(X, &x));
353548a46eb9SPierre Jolivet     for (PetscInt i = 0; i < m; i += 3) PetscCall(TPSNearestPoint(evalFunc, &x[i]));
35369566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(X, &x));
3537b7f5c055SJed Brown   }
3538b7f5c055SJed Brown 
3539b7f5c055SJed Brown   // Face Sets has already been propagated to new vertices during refinement; this propagates to the initial vertices.
35409566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "Face Sets", &label));
35419566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete(dm, label));
3542b7f5c055SJed Brown 
354346139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
354446139095SJed Brown 
3545b7f5c055SJed Brown   if (thickness > 0) {
35464663dae6SJed Brown     DM              edm, cdm, ecdm;
35474663dae6SJed Brown     DMPlexTransform tr;
35484663dae6SJed Brown     const char     *prefix;
35494663dae6SJed Brown     PetscOptions    options;
35504663dae6SJed Brown     // Code from DMPlexExtrude
35514663dae6SJed Brown     PetscCall(DMPlexTransformCreate(PetscObjectComm((PetscObject)dm), &tr));
35524663dae6SJed Brown     PetscCall(DMPlexTransformSetDM(tr, dm));
35534663dae6SJed Brown     PetscCall(DMPlexTransformSetType(tr, DMPLEXEXTRUDE));
35544663dae6SJed Brown     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, &prefix));
35554663dae6SJed Brown     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)tr, prefix));
35564663dae6SJed Brown     PetscCall(PetscObjectGetOptions((PetscObject)dm, &options));
35574663dae6SJed Brown     PetscCall(PetscObjectSetOptions((PetscObject)tr, options));
35584663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetLayers(tr, layers));
35594663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetThickness(tr, thickness));
35604663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetTensor(tr, PETSC_FALSE));
35614663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetSymmetric(tr, PETSC_TRUE));
35624663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetNormalFunction(tr, normalFunc));
35634663dae6SJed Brown     PetscCall(DMPlexTransformSetFromOptions(tr));
35644663dae6SJed Brown     PetscCall(PetscObjectSetOptions((PetscObject)tr, NULL));
35654663dae6SJed Brown     PetscCall(DMPlexTransformSetUp(tr));
35664663dae6SJed Brown     PetscCall(PetscObjectViewFromOptions((PetscObject)tr, NULL, "-dm_plex_tps_transform_view"));
35674663dae6SJed Brown     PetscCall(DMPlexTransformApply(tr, dm, &edm));
35684663dae6SJed Brown     PetscCall(DMCopyDisc(dm, edm));
35694663dae6SJed Brown     PetscCall(DMGetCoordinateDM(dm, &cdm));
35704663dae6SJed Brown     PetscCall(DMGetCoordinateDM(edm, &ecdm));
35714663dae6SJed Brown     PetscCall(DMCopyDisc(cdm, ecdm));
35724663dae6SJed Brown     PetscCall(DMPlexTransformCreateDiscLabels(tr, edm));
35734663dae6SJed Brown     PetscCall(DMPlexTransformDestroy(&tr));
35744663dae6SJed Brown     if (edm) {
35754663dae6SJed Brown       ((DM_Plex *)edm->data)->printFEM    = ((DM_Plex *)dm->data)->printFEM;
35764663dae6SJed Brown       ((DM_Plex *)edm->data)->printL2     = ((DM_Plex *)dm->data)->printL2;
3577f5867de0SMatthew G. Knepley       ((DM_Plex *)edm->data)->printLocate = ((DM_Plex *)dm->data)->printLocate;
35784663dae6SJed Brown     }
357969d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &edm));
3580b7f5c055SJed Brown   }
35813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3582b7f5c055SJed Brown }
3583b7f5c055SJed Brown 
3584b7f5c055SJed Brown /*@
3585b7f5c055SJed Brown   DMPlexCreateTPSMesh - Create a distributed, interpolated mesh of a triply-periodic surface
3586b7f5c055SJed Brown 
3587b7f5c055SJed Brown   Collective
3588b7f5c055SJed Brown 
3589b7f5c055SJed Brown   Input Parameters:
3590a1cb98faSBarry Smith + comm           - The communicator for the `DM` object
3591b7f5c055SJed Brown . tpstype        - Type of triply-periodic surface
3592b7f5c055SJed Brown . extent         - Array of length 3 containing number of periods in each direction
359320f4b53cSBarry Smith . periodic       - array of length 3 with periodicity, or `NULL` for non-periodic
35941436d7faSJed Brown . tps_distribute - Distribute 2D manifold mesh prior to refinement and extrusion (more scalable)
3595817da375SSatish Balay . refinements    - Number of factor-of-2 refinements of 2D manifold mesh
35961436d7faSJed Brown . layers         - Number of cell layers extruded in normal direction
3597817da375SSatish Balay - thickness      - Thickness in normal direction
3598b7f5c055SJed Brown 
3599b7f5c055SJed Brown   Output Parameter:
3600a1cb98faSBarry Smith . dm - The `DM` object
3601a1cb98faSBarry Smith 
3602a1cb98faSBarry Smith   Level: beginner
3603b7f5c055SJed Brown 
3604b7f5c055SJed Brown   Notes:
360515229ffcSPierre Jolivet   This meshes the surface of the Schwarz P or Gyroid surfaces.  Schwarz P is the simplest member of the triply-periodic minimal surfaces.
36061d27aa22SBarry Smith   <https://en.wikipedia.org/wiki/Schwarz_minimal_surface#Schwarz_P_(%22Primitive%22)> and can be cut with "clean" boundaries.
36071d27aa22SBarry 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.
3608b7f5c055SJed Brown   Our implementation creates a very coarse mesh of the surface and refines (by 4-way splitting) as many times as requested.
3609b7f5c055SJed Brown   On each refinement, all vertices are projected to their nearest point on the surface.
3610b7f5c055SJed Brown   This projection could readily be extended to related surfaces.
3611b7f5c055SJed Brown 
36121d27aa22SBarry Smith   See {cite}`maskery2018insights`
36131d27aa22SBarry Smith 
36141d27aa22SBarry Smith   The face (edge) sets for the Schwarz P surface are numbered $1(-x), 2(+x), 3(-y), 4(+y), 5(-z), 6(+z)$.
36151d27aa22SBarry Smith   When the mesh is refined, "Face Sets" contain the new vertices (created during refinement).
36161d27aa22SBarry Smith   Use `DMPlexLabelComplete()` to propagate to coarse-level vertices.
3617b7f5c055SJed Brown 
361860225df5SJacob Faibussowitsch   Developer Notes:
3619b7f5c055SJed Brown   The Gyroid mesh does not currently mark boundary sets.
3620b7f5c055SJed Brown 
36211cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSphereMesh()`, `DMSetType()`, `DMCreate()`
3622b7f5c055SJed Brown @*/
3623d71ae5a4SJacob 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)
3624d71ae5a4SJacob Faibussowitsch {
3625b7f5c055SJed Brown   PetscFunctionBegin;
36269566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
36279566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
36289566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateTPSMesh_Internal(*dm, tpstype, extent, periodic, tps_distribute, refinements, layers, thickness));
36293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3630b7f5c055SJed Brown }
3631b7f5c055SJed Brown 
36329318fe57SMatthew G. Knepley /*@
36339318fe57SMatthew G. Knepley   DMPlexCreateSphereMesh - Creates a mesh on the d-dimensional sphere, S^d.
36349318fe57SMatthew G. Knepley 
36359318fe57SMatthew G. Knepley   Collective
36369318fe57SMatthew G. Knepley 
36379318fe57SMatthew G. Knepley   Input Parameters:
3638a1cb98faSBarry Smith + comm    - The communicator for the `DM` object
36399318fe57SMatthew G. Knepley . dim     - The dimension
36409318fe57SMatthew G. Knepley . simplex - Use simplices, or tensor product cells
36419318fe57SMatthew G. Knepley - R       - The radius
36429318fe57SMatthew G. Knepley 
36439318fe57SMatthew G. Knepley   Output Parameter:
3644a1cb98faSBarry Smith . dm - The `DM` object
36459318fe57SMatthew G. Knepley 
36469318fe57SMatthew G. Knepley   Level: beginner
36479318fe57SMatthew G. Knepley 
36481cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBallMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
36499318fe57SMatthew G. Knepley @*/
3650d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateSphereMesh(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscReal R, DM *dm)
3651d71ae5a4SJacob Faibussowitsch {
36529318fe57SMatthew G. Knepley   PetscFunctionBegin;
36534f572ea9SToby Isaac   PetscAssertPointer(dm, 5);
36549566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
36559566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
36569566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSphereMesh_Internal(*dm, dim, simplex, R));
36573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36589318fe57SMatthew G. Knepley }
36599318fe57SMatthew G. Knepley 
3660d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBallMesh_Internal(DM dm, PetscInt dim, PetscReal R)
3661d71ae5a4SJacob Faibussowitsch {
36629318fe57SMatthew G. Knepley   DM          sdm, vol;
36639318fe57SMatthew G. Knepley   DMLabel     bdlabel;
3664dd2b43ebSStefano Zampini   const char *prefix;
36659318fe57SMatthew G. Knepley 
36669318fe57SMatthew G. Knepley   PetscFunctionBegin;
36679566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &sdm));
36689566063dSJacob Faibussowitsch   PetscCall(DMSetType(sdm, DMPLEX));
3669dd2b43ebSStefano Zampini   PetscCall(DMGetOptionsPrefix(dm, &prefix));
3670dd2b43ebSStefano Zampini   PetscCall(DMSetOptionsPrefix(sdm, prefix));
3671dd2b43ebSStefano Zampini   PetscCall(DMAppendOptionsPrefix(sdm, "bd_"));
3672dd2b43ebSStefano Zampini   PetscCall(DMPlexDistributeSetDefault(sdm, PETSC_FALSE));
36739566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSphereMesh_Internal(sdm, dim - 1, PETSC_TRUE, R));
36749566063dSJacob Faibussowitsch   PetscCall(DMSetFromOptions(sdm));
36759566063dSJacob Faibussowitsch   PetscCall(DMViewFromOptions(sdm, NULL, "-dm_view"));
36769566063dSJacob Faibussowitsch   PetscCall(DMPlexGenerate(sdm, NULL, PETSC_TRUE, &vol));
36779566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&sdm));
367869d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &vol));
36799566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
36809566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "marker", &bdlabel));
36819566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkBoundaryFaces(dm, PETSC_DETERMINE, bdlabel));
36829566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete(dm, bdlabel));
36833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
368451a74b61SMatthew G. Knepley }
368551a74b61SMatthew G. Knepley 
368651a74b61SMatthew G. Knepley /*@
368751a74b61SMatthew G. Knepley   DMPlexCreateBallMesh - Creates a simplex mesh on the d-dimensional ball, B^d.
368851a74b61SMatthew G. Knepley 
368951a74b61SMatthew G. Knepley   Collective
369051a74b61SMatthew G. Knepley 
369151a74b61SMatthew G. Knepley   Input Parameters:
3692a1cb98faSBarry Smith + comm - The communicator for the `DM` object
369351a74b61SMatthew G. Knepley . dim  - The dimension
369451a74b61SMatthew G. Knepley - R    - The radius
369551a74b61SMatthew G. Knepley 
369651a74b61SMatthew G. Knepley   Output Parameter:
3697a1cb98faSBarry Smith . dm - The `DM` object
369851a74b61SMatthew G. Knepley 
3699a1cb98faSBarry Smith   Options Database Key:
370060225df5SJacob Faibussowitsch . bd_dm_refine - This will refine the surface mesh preserving the sphere geometry
370151a74b61SMatthew G. Knepley 
370251a74b61SMatthew G. Knepley   Level: beginner
370351a74b61SMatthew G. Knepley 
37041cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSphereMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
370551a74b61SMatthew G. Knepley @*/
3706d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateBallMesh(MPI_Comm comm, PetscInt dim, PetscReal R, DM *dm)
3707d71ae5a4SJacob Faibussowitsch {
370851a74b61SMatthew G. Knepley   PetscFunctionBegin;
37099566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
37109566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
37119566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBallMesh_Internal(*dm, dim, R));
37123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
37132829fed8SMatthew G. Knepley }
37142829fed8SMatthew G. Knepley 
3715d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateReferenceCell_Internal(DM rdm, DMPolytopeType ct)
3716d71ae5a4SJacob Faibussowitsch {
37170a6ba040SMatthew G. Knepley   PetscFunctionBegin;
37189318fe57SMatthew G. Knepley   switch (ct) {
37199371c9d4SSatish Balay   case DM_POLYTOPE_POINT: {
37209318fe57SMatthew G. Knepley     PetscInt    numPoints[1]        = {1};
37219318fe57SMatthew G. Knepley     PetscInt    coneSize[1]         = {0};
37229318fe57SMatthew G. Knepley     PetscInt    cones[1]            = {0};
37239318fe57SMatthew G. Knepley     PetscInt    coneOrientations[1] = {0};
37249318fe57SMatthew G. Knepley     PetscScalar vertexCoords[1]     = {0.0};
37259318fe57SMatthew G. Knepley 
37269566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 0));
37279566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 0, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37289371c9d4SSatish Balay   } break;
37299371c9d4SSatish Balay   case DM_POLYTOPE_SEGMENT: {
37309318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {2, 1};
37319318fe57SMatthew G. Knepley     PetscInt    coneSize[3]         = {2, 0, 0};
37329318fe57SMatthew G. Knepley     PetscInt    cones[2]            = {1, 2};
37339318fe57SMatthew G. Knepley     PetscInt    coneOrientations[2] = {0, 0};
37349318fe57SMatthew G. Knepley     PetscScalar vertexCoords[2]     = {-1.0, 1.0};
37359318fe57SMatthew G. Knepley 
37369566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 1));
37379566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37389371c9d4SSatish Balay   } break;
37399371c9d4SSatish Balay   case DM_POLYTOPE_POINT_PRISM_TENSOR: {
3740b5a892a1SMatthew G. Knepley     PetscInt    numPoints[2]        = {2, 1};
3741b5a892a1SMatthew G. Knepley     PetscInt    coneSize[3]         = {2, 0, 0};
3742b5a892a1SMatthew G. Knepley     PetscInt    cones[2]            = {1, 2};
3743b5a892a1SMatthew G. Knepley     PetscInt    coneOrientations[2] = {0, 0};
3744b5a892a1SMatthew G. Knepley     PetscScalar vertexCoords[2]     = {-1.0, 1.0};
3745b5a892a1SMatthew G. Knepley 
37469566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 1));
37479566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37489371c9d4SSatish Balay   } break;
37499371c9d4SSatish Balay   case DM_POLYTOPE_TRIANGLE: {
37509318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {3, 1};
37519318fe57SMatthew G. Knepley     PetscInt    coneSize[4]         = {3, 0, 0, 0};
37529318fe57SMatthew G. Knepley     PetscInt    cones[3]            = {1, 2, 3};
37539318fe57SMatthew G. Knepley     PetscInt    coneOrientations[3] = {0, 0, 0};
37549318fe57SMatthew G. Knepley     PetscScalar vertexCoords[6]     = {-1.0, -1.0, 1.0, -1.0, -1.0, 1.0};
37559318fe57SMatthew G. Knepley 
37569566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 2));
37579566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37589371c9d4SSatish Balay   } break;
37599371c9d4SSatish Balay   case DM_POLYTOPE_QUADRILATERAL: {
37609318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {4, 1};
37619318fe57SMatthew G. Knepley     PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
37629318fe57SMatthew G. Knepley     PetscInt    cones[4]            = {1, 2, 3, 4};
37639318fe57SMatthew G. Knepley     PetscInt    coneOrientations[4] = {0, 0, 0, 0};
37649318fe57SMatthew G. Knepley     PetscScalar vertexCoords[8]     = {-1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0};
37659318fe57SMatthew G. Knepley 
37669566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 2));
37679566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37689371c9d4SSatish Balay   } break;
37699371c9d4SSatish Balay   case DM_POLYTOPE_SEG_PRISM_TENSOR: {
37709318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {4, 1};
37719318fe57SMatthew G. Knepley     PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
37729318fe57SMatthew G. Knepley     PetscInt    cones[4]            = {1, 2, 3, 4};
37739318fe57SMatthew G. Knepley     PetscInt    coneOrientations[4] = {0, 0, 0, 0};
37749318fe57SMatthew G. Knepley     PetscScalar vertexCoords[8]     = {-1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0};
37759318fe57SMatthew G. Knepley 
37769566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 2));
37779566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37789371c9d4SSatish Balay   } break;
37799371c9d4SSatish Balay   case DM_POLYTOPE_TETRAHEDRON: {
37809318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {4, 1};
37819318fe57SMatthew G. Knepley     PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
3782f0edb160SMatthew G. Knepley     PetscInt    cones[4]            = {1, 2, 3, 4};
37839318fe57SMatthew G. Knepley     PetscInt    coneOrientations[4] = {0, 0, 0, 0};
3784f0edb160SMatthew 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};
37859318fe57SMatthew G. Knepley 
37869566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
37879566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37889371c9d4SSatish Balay   } break;
37899371c9d4SSatish Balay   case DM_POLYTOPE_HEXAHEDRON: {
37909318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {8, 1};
37919318fe57SMatthew G. Knepley     PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
3792f0edb160SMatthew G. Knepley     PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
37939318fe57SMatthew G. Knepley     PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
37949371c9d4SSatish 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};
37959318fe57SMatthew G. Knepley 
37969566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
37979566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37989371c9d4SSatish Balay   } break;
37999371c9d4SSatish Balay   case DM_POLYTOPE_TRI_PRISM: {
38009318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {6, 1};
38019318fe57SMatthew G. Knepley     PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
3802f0edb160SMatthew G. Knepley     PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
38039318fe57SMatthew G. Knepley     PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
38049371c9d4SSatish 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};
38059318fe57SMatthew G. Knepley 
38069566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
38079566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
38089371c9d4SSatish Balay   } break;
38099371c9d4SSatish Balay   case DM_POLYTOPE_TRI_PRISM_TENSOR: {
38109318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {6, 1};
38119318fe57SMatthew G. Knepley     PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
38129318fe57SMatthew G. Knepley     PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
38139318fe57SMatthew G. Knepley     PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
38149371c9d4SSatish 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};
38159318fe57SMatthew G. Knepley 
38169566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
38179566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
38189371c9d4SSatish Balay   } break;
38199371c9d4SSatish Balay   case DM_POLYTOPE_QUAD_PRISM_TENSOR: {
38209318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {8, 1};
38219318fe57SMatthew G. Knepley     PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
38229318fe57SMatthew G. Knepley     PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
38239318fe57SMatthew G. Knepley     PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
38249371c9d4SSatish 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};
38259318fe57SMatthew G. Knepley 
38269566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
38279566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
38289371c9d4SSatish Balay   } break;
38299371c9d4SSatish Balay   case DM_POLYTOPE_PYRAMID: {
38309318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {5, 1};
38319318fe57SMatthew G. Knepley     PetscInt    coneSize[6]         = {5, 0, 0, 0, 0, 0};
3832f0edb160SMatthew G. Knepley     PetscInt    cones[5]            = {1, 2, 3, 4, 5};
38339318fe57SMatthew G. Knepley     PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
38349371c9d4SSatish 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};
38359318fe57SMatthew G. Knepley 
38369566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
38379566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
38389371c9d4SSatish Balay   } break;
3839d71ae5a4SJacob Faibussowitsch   default:
3840d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)rdm), PETSC_ERR_ARG_WRONG, "Cannot create reference cell for cell type %s", DMPolytopeTypes[ct]);
38419318fe57SMatthew G. Knepley   }
38429318fe57SMatthew G. Knepley   {
38439318fe57SMatthew G. Knepley     PetscInt Nv, v;
38449318fe57SMatthew G. Knepley 
38459318fe57SMatthew G. Knepley     /* Must create the celltype label here so that we do not automatically try to compute the types */
38469566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(rdm, "celltype"));
38479566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCellType(rdm, 0, ct));
38489566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(rdm, NULL, &Nv));
38499566063dSJacob Faibussowitsch     for (v = 1; v < Nv; ++v) PetscCall(DMPlexSetCellType(rdm, v, DM_POLYTOPE_POINT));
38509318fe57SMatthew G. Knepley   }
38519566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolateInPlace_Internal(rdm));
38529566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)rdm, DMPolytopeTypes[ct]));
38533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
38540a6ba040SMatthew G. Knepley }
38550a6ba040SMatthew G. Knepley 
38569318fe57SMatthew G. Knepley /*@
3857a1cb98faSBarry Smith   DMPlexCreateReferenceCell - Create a `DMPLEX` with the appropriate FEM reference cell
38589318fe57SMatthew G. Knepley 
38599318fe57SMatthew G. Knepley   Collective
38609318fe57SMatthew G. Knepley 
38619318fe57SMatthew G. Knepley   Input Parameters:
38629318fe57SMatthew G. Knepley + comm - The communicator
38639318fe57SMatthew G. Knepley - ct   - The cell type of the reference cell
38649318fe57SMatthew G. Knepley 
38659318fe57SMatthew G. Knepley   Output Parameter:
38669318fe57SMatthew G. Knepley . refdm - The reference cell
38679318fe57SMatthew G. Knepley 
38689318fe57SMatthew G. Knepley   Level: intermediate
38699318fe57SMatthew G. Knepley 
387042747ad1SJacob Faibussowitsch .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBoxMesh()`
38719318fe57SMatthew G. Knepley @*/
3872d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateReferenceCell(MPI_Comm comm, DMPolytopeType ct, DM *refdm)
3873d71ae5a4SJacob Faibussowitsch {
38740a6ba040SMatthew G. Knepley   PetscFunctionBegin;
38759566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, refdm));
38769566063dSJacob Faibussowitsch   PetscCall(DMSetType(*refdm, DMPLEX));
38779566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateReferenceCell_Internal(*refdm, ct));
38783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
38799318fe57SMatthew G. Knepley }
388079a015ccSMatthew G. Knepley 
3881d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoundaryLabel_Private(DM dm, const char name[])
3882d71ae5a4SJacob Faibussowitsch {
38839318fe57SMatthew G. Knepley   DM        plex;
38849318fe57SMatthew G. Knepley   DMLabel   label;
38859318fe57SMatthew G. Knepley   PetscBool hasLabel;
38860a6ba040SMatthew G. Knepley 
3887c22d3578SMatthew G. Knepley   PetscFunctionBegin;
38889566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &hasLabel));
38893ba16761SJacob Faibussowitsch   if (hasLabel) PetscFunctionReturn(PETSC_SUCCESS);
38909566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, name));
38919566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
38929566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
38939566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkBoundaryFaces(plex, 1, label));
38941c8afea9SMatthew G. Knepley   PetscCall(DMPlexLabelComplete(plex, label));
38959566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plex));
38963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
38979318fe57SMatthew G. Knepley }
3898acdc6f61SToby Isaac 
3899669647acSMatthew G. Knepley /*
3900669647acSMatthew 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.
3901669647acSMatthew G. Knepley 
3902669647acSMatthew G. Knepley     (x, y) -> (r, theta) = (x[1], (x[0] - lower[0]) * 2\pi/(upper[0] - lower[0]))
3903669647acSMatthew G. Knepley */
3904d71ae5a4SJacob 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[])
3905d71ae5a4SJacob Faibussowitsch {
3906669647acSMatthew G. Knepley   const PetscReal low = PetscRealPart(constants[0]);
3907669647acSMatthew G. Knepley   const PetscReal upp = PetscRealPart(constants[1]);
3908669647acSMatthew G. Knepley   const PetscReal r   = PetscRealPart(u[1]);
3909669647acSMatthew G. Knepley   const PetscReal th  = 2. * PETSC_PI * (PetscRealPart(u[0]) - low) / (upp - low);
3910669647acSMatthew G. Knepley 
3911669647acSMatthew G. Knepley   f0[0] = r * PetscCosReal(th);
3912669647acSMatthew G. Knepley   f0[1] = r * PetscSinReal(th);
3913669647acSMatthew G. Knepley }
3914669647acSMatthew G. Knepley 
39154e22dd4cSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscOptionsFindPairPrefix_Private(PetscOptions, const char pre[], const char name[], const char *option[], const char *value[], PetscBool *flg);
39164e22dd4cSMatthew G. Knepley 
39175dca41c3SJed Brown const char *const DMPlexShapes[] = {"box", "box_surface", "ball", "sphere", "cylinder", "schwarz_p", "gyroid", "doublet", "annulus", "hypercubic", "zbox", "unknown", "DMPlexShape", "DM_SHAPE_", NULL};
39189318fe57SMatthew G. Knepley 
3919d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateFromOptions_Internal(PetscOptionItems *PetscOptionsObject, PetscBool *useCoordSpace, DM dm)
3920d71ae5a4SJacob Faibussowitsch {
39219318fe57SMatthew G. Knepley   DMPlexShape    shape   = DM_SHAPE_BOX;
39229318fe57SMatthew G. Knepley   DMPolytopeType cell    = DM_POLYTOPE_TRIANGLE;
39239318fe57SMatthew G. Knepley   PetscInt       dim     = 2;
39249318fe57SMatthew G. Knepley   PetscBool      simplex = PETSC_TRUE, interpolate = PETSC_TRUE, adjCone = PETSC_FALSE, adjClosure = PETSC_TRUE, refDomain = PETSC_FALSE;
3925d0812dedSMatthew G. Knepley   PetscBool      flg, flg2, fflg, strflg, bdfflg, nameflg;
39269318fe57SMatthew G. Knepley   MPI_Comm       comm;
3927ed5e4e85SVaclav Hapla   char           filename[PETSC_MAX_PATH_LEN]   = "<unspecified>";
3928ed5e4e85SVaclav Hapla   char           bdFilename[PETSC_MAX_PATH_LEN] = "<unspecified>";
3929ed5e4e85SVaclav Hapla   char           plexname[PETSC_MAX_PATH_LEN]   = "";
39304e22dd4cSMatthew G. Knepley   const char    *option;
39319318fe57SMatthew G. Knepley 
39329318fe57SMatthew G. Knepley   PetscFunctionBegin;
3933708be2fdSJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_CreateFromOptions, dm, 0, 0, 0));
39349566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
39359318fe57SMatthew G. Knepley   /* TODO Turn this into a registration interface */
39369566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_filename", "File containing a mesh", "DMPlexCreateFromFile", filename, filename, sizeof(filename), &fflg));
3937d0812dedSMatthew G. Knepley   PetscCall(PetscOptionsString("-dm_plex_file_contents", "Contents of a file format in a string", "DMPlexCreateFromFile", filename, filename, sizeof(filename), &strflg));
39389566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_boundary_filename", "File containing a mesh boundary", "DMPlexCreateFromFile", bdFilename, bdFilename, sizeof(bdFilename), &bdfflg));
39399566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_name", "Name of the mesh in the file", "DMPlexCreateFromFile", plexname, plexname, sizeof(plexname), &nameflg));
39409566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_plex_cell", "Cell shape", "", DMPolytopeTypes, (PetscEnum)cell, (PetscEnum *)&cell, NULL));
39419566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_reference_cell_domain", "Use a reference cell domain", "", refDomain, &refDomain, NULL));
39429566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_plex_shape", "Shape for built-in mesh", "", DMPlexShapes, (PetscEnum)shape, (PetscEnum *)&shape, &flg));
39439566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_dim", "Topological dimension of the mesh", "DMGetDimension", dim, &dim, &flg, 0));
39449566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_simplex", "Mesh cell shape", "", simplex, &simplex, &flg));
39459566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_interpolate", "Flag to create edges and faces automatically", "", interpolate, &interpolate, &flg));
39469566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_adj_cone", "Set adjacency direction", "DMSetBasicAdjacency", adjCone, &adjCone, &flg));
39479566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_adj_closure", "Set adjacency size", "DMSetBasicAdjacency", adjClosure, &adjClosure, &flg2));
39489566063dSJacob Faibussowitsch   if (flg || flg2) PetscCall(DMSetBasicAdjacency(dm, adjCone, adjClosure));
39499318fe57SMatthew G. Knepley 
395061a622f3SMatthew G. Knepley   switch (cell) {
395161a622f3SMatthew G. Knepley   case DM_POLYTOPE_POINT:
395261a622f3SMatthew G. Knepley   case DM_POLYTOPE_SEGMENT:
395361a622f3SMatthew G. Knepley   case DM_POLYTOPE_POINT_PRISM_TENSOR:
395461a622f3SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
395561a622f3SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
395661a622f3SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
3957d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_HEXAHEDRON:
3958d71ae5a4SJacob Faibussowitsch     *useCoordSpace = PETSC_TRUE;
3959d71ae5a4SJacob Faibussowitsch     break;
3960d71ae5a4SJacob Faibussowitsch   default:
3961d71ae5a4SJacob Faibussowitsch     *useCoordSpace = PETSC_FALSE;
3962d71ae5a4SJacob Faibussowitsch     break;
396361a622f3SMatthew G. Knepley   }
396461a622f3SMatthew G. Knepley 
39659318fe57SMatthew G. Knepley   if (fflg) {
39669318fe57SMatthew G. Knepley     DM          dmnew;
39671e4a82c4SMatthew G. Knepley     const char *name;
39689318fe57SMatthew G. Knepley 
39691e4a82c4SMatthew G. Knepley     PetscCall(PetscObjectGetName((PetscObject)dm, &name));
39701e4a82c4SMatthew G. Knepley     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), filename, nameflg ? plexname : name, interpolate, &dmnew));
39715de52c6dSVaclav Hapla     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
397269d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &dmnew));
39739318fe57SMatthew G. Knepley   } else if (refDomain) {
39749566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateReferenceCell_Internal(dm, cell));
39759318fe57SMatthew G. Knepley   } else if (bdfflg) {
39769318fe57SMatthew G. Knepley     DM          bdm, dmnew;
39771e4a82c4SMatthew G. Knepley     const char *name;
39789318fe57SMatthew G. Knepley 
39791e4a82c4SMatthew G. Knepley     PetscCall(PetscObjectGetName((PetscObject)dm, &name));
39801e4a82c4SMatthew G. Knepley     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), bdFilename, nameflg ? plexname : name, interpolate, &bdm));
39819566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)bdm, "bd_"));
39829566063dSJacob Faibussowitsch     PetscCall(DMSetFromOptions(bdm));
39839566063dSJacob Faibussowitsch     PetscCall(DMPlexGenerate(bdm, NULL, interpolate, &dmnew));
39849566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&bdm));
39855de52c6dSVaclav Hapla     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
398669d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &dmnew));
3987d0812dedSMatthew G. Knepley   } else if (strflg) {
3988d0812dedSMatthew G. Knepley     DM          dmnew;
3989d0812dedSMatthew G. Knepley     PetscViewer viewer;
3990d0812dedSMatthew G. Knepley     const char *contents;
3991d0812dedSMatthew G. Knepley     char       *strname;
3992d0812dedSMatthew G. Knepley     char        tmpdir[PETSC_MAX_PATH_LEN];
3993d0812dedSMatthew G. Knepley     char        tmpfilename[PETSC_MAX_PATH_LEN];
3994d0812dedSMatthew G. Knepley     char        name[PETSC_MAX_PATH_LEN];
3995d0812dedSMatthew G. Knepley     MPI_Comm    comm;
3996d0812dedSMatthew G. Knepley     PetscMPIInt rank;
3997d0812dedSMatthew G. Knepley 
3998d0812dedSMatthew G. Knepley     PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
3999d0812dedSMatthew G. Knepley     PetscCallMPI(MPI_Comm_rank(comm, &rank));
4000d0812dedSMatthew G. Knepley     PetscCall(PetscStrchr(filename, ':', &strname));
4001d0812dedSMatthew G. Knepley     PetscCheck(strname, comm, PETSC_ERR_ARG_WRONG, "File contents must have the form \"ext:string_name\", not %s", filename);
4002d0812dedSMatthew G. Knepley     strname[0] = '\0';
4003d0812dedSMatthew G. Knepley     ++strname;
4004d0812dedSMatthew G. Knepley     PetscCall(PetscDLSym(NULL, strname, (void **)&contents));
4005d0812dedSMatthew G. Knepley     PetscCheck(contents, comm, PETSC_ERR_ARG_WRONG, "Could not locate mesh string %s", strname);
4006d0812dedSMatthew G. Knepley     PetscCall(PetscGetTmp(comm, tmpdir, PETSC_MAX_PATH_LEN));
4007ed32af8cSMatthew G. Knepley     PetscCall(PetscStrlcat(tmpdir, "/meshXXXXXX", PETSC_MAX_PATH_LEN));
4008ed32af8cSMatthew G. Knepley     PetscCall(PetscMkdtemp(tmpdir));
4009ed32af8cSMatthew G. Knepley     PetscCall(PetscSNPrintf(tmpfilename, PETSC_MAX_PATH_LEN, "%s/mesh.%s", tmpdir, filename));
4010d0812dedSMatthew G. Knepley     PetscCall(PetscViewerASCIIOpen(comm, tmpfilename, &viewer));
4011d0812dedSMatthew G. Knepley     PetscCall(PetscViewerASCIIPrintf(viewer, "%s\n", contents));
4012d0812dedSMatthew G. Knepley     PetscCall(PetscViewerDestroy(&viewer));
4013d0812dedSMatthew G. Knepley     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), tmpfilename, plexname, interpolate, &dmnew));
4014ed32af8cSMatthew G. Knepley     PetscCall(PetscRMTree(tmpdir));
4015d0812dedSMatthew G. Knepley     PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN, "%s Mesh", strname));
4016d0812dedSMatthew G. Knepley     PetscCall(PetscObjectSetName((PetscObject)dm, name));
4017d0812dedSMatthew G. Knepley     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
4018d0812dedSMatthew G. Knepley     PetscCall(DMPlexReplace_Internal(dm, &dmnew));
40199318fe57SMatthew G. Knepley   } else {
40209566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)dm, DMPlexShapes[shape]));
40219318fe57SMatthew G. Knepley     switch (shape) {
4022669647acSMatthew G. Knepley     case DM_SHAPE_BOX:
40235dca41c3SJed Brown     case DM_SHAPE_ZBOX:
4024669647acSMatthew G. Knepley     case DM_SHAPE_ANNULUS: {
40259318fe57SMatthew G. Knepley       PetscInt       faces[3]  = {0, 0, 0};
40269318fe57SMatthew G. Knepley       PetscReal      lower[3]  = {0, 0, 0};
40279318fe57SMatthew G. Knepley       PetscReal      upper[3]  = {1, 1, 1};
40289318fe57SMatthew G. Knepley       DMBoundaryType bdt[3]    = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
4029669647acSMatthew G. Knepley       PetscBool      isAnnular = shape == DM_SHAPE_ANNULUS ? PETSC_TRUE : PETSC_FALSE;
40309318fe57SMatthew G. Knepley       PetscInt       i, n;
40319318fe57SMatthew G. Knepley 
40329318fe57SMatthew G. Knepley       n = dim;
40339318fe57SMatthew G. Knepley       for (i = 0; i < dim; ++i) faces[i] = (dim == 1 ? 1 : 4 - dim);
40349566063dSJacob Faibussowitsch       PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg));
40359318fe57SMatthew G. Knepley       n = 3;
40369566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
403763a3b9bcSJacob Faibussowitsch       PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Lower box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
40389318fe57SMatthew G. Knepley       n = 3;
40399566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
404063a3b9bcSJacob Faibussowitsch       PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Upper box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
40419318fe57SMatthew G. Knepley       n = 3;
40429566063dSJacob Faibussowitsch       PetscCall(PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *)bdt, &n, &flg));
404363a3b9bcSJacob Faibussowitsch       PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Box boundary types had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
4044669647acSMatthew G. Knepley 
4045669647acSMatthew G. Knepley       PetscCheck(!isAnnular || dim == 2, comm, PETSC_ERR_ARG_OUTOFRANGE, "Only two dimensional annuli have been implemented");
4046669647acSMatthew G. Knepley       if (isAnnular)
4047669647acSMatthew G. Knepley         for (i = 0; i < dim - 1; ++i) bdt[i] = DM_BOUNDARY_PERIODIC;
4048669647acSMatthew G. Knepley 
40499318fe57SMatthew G. Knepley       switch (cell) {
405061a622f3SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
40519566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateWedgeBoxMesh_Internal(dm, faces, lower, upper, bdt));
4052d410b0cfSMatthew G. Knepley         if (!interpolate) {
4053d410b0cfSMatthew G. Knepley           DM udm;
4054d410b0cfSMatthew G. Knepley 
40559566063dSJacob Faibussowitsch           PetscCall(DMPlexUninterpolate(dm, &udm));
405669d8a87bSksagiyam           PetscCall(DMPlexReplace_Internal(dm, &udm));
4057d410b0cfSMatthew G. Knepley         }
40589318fe57SMatthew G. Knepley         break;
4059d71ae5a4SJacob Faibussowitsch       default:
40605dca41c3SJed Brown         PetscCall(DMPlexCreateBoxMesh_Internal(dm, shape, dim, simplex, faces, lower, upper, bdt, interpolate));
4061d71ae5a4SJacob Faibussowitsch         break;
40629318fe57SMatthew G. Knepley       }
4063669647acSMatthew G. Knepley       if (isAnnular) {
4064669647acSMatthew G. Knepley         DM          cdm;
4065669647acSMatthew G. Knepley         PetscDS     cds;
4066669647acSMatthew G. Knepley         PetscScalar bounds[2] = {lower[0], upper[0]};
4067669647acSMatthew G. Knepley 
4068669647acSMatthew G. Knepley         // Fix coordinates for annular region
4069669647acSMatthew G. Knepley         PetscCall(DMSetPeriodicity(dm, NULL, NULL, NULL));
4070669647acSMatthew G. Knepley         PetscCall(DMSetCellCoordinatesLocal(dm, NULL));
4071669647acSMatthew G. Knepley         PetscCall(DMSetCellCoordinates(dm, NULL));
4072e44f6aebSMatthew G. Knepley         PetscCall(DMPlexCreateCoordinateSpace(dm, 1, PETSC_TRUE, NULL));
4073669647acSMatthew G. Knepley         PetscCall(DMGetCoordinateDM(dm, &cdm));
4074669647acSMatthew G. Knepley         PetscCall(DMGetDS(cdm, &cds));
4075669647acSMatthew G. Knepley         PetscCall(PetscDSSetConstants(cds, 2, bounds));
4076669647acSMatthew G. Knepley         PetscCall(DMPlexRemapGeometry(dm, 0.0, boxToAnnulus));
4077669647acSMatthew G. Knepley       }
40789371c9d4SSatish Balay     } break;
40799371c9d4SSatish Balay     case DM_SHAPE_BOX_SURFACE: {
40809318fe57SMatthew G. Knepley       PetscInt  faces[3] = {0, 0, 0};
40819318fe57SMatthew G. Knepley       PetscReal lower[3] = {0, 0, 0};
40829318fe57SMatthew G. Knepley       PetscReal upper[3] = {1, 1, 1};
40839318fe57SMatthew G. Knepley       PetscInt  i, n;
40849318fe57SMatthew G. Knepley 
40859318fe57SMatthew G. Knepley       n = dim + 1;
40869318fe57SMatthew G. Knepley       for (i = 0; i < dim + 1; ++i) faces[i] = (dim + 1 == 1 ? 1 : 4 - (dim + 1));
40879566063dSJacob Faibussowitsch       PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg));
40889318fe57SMatthew G. Knepley       n = 3;
40899566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
409063a3b9bcSJacob 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);
40919318fe57SMatthew G. Knepley       n = 3;
40929566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
409363a3b9bcSJacob 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);
40949566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(dm, dim + 1, faces, lower, upper, interpolate));
40959371c9d4SSatish Balay     } break;
40969371c9d4SSatish Balay     case DM_SHAPE_SPHERE: {
40979318fe57SMatthew G. Knepley       PetscReal R = 1.0;
40989318fe57SMatthew G. Knepley 
40999566063dSJacob Faibussowitsch       PetscCall(PetscOptionsReal("-dm_plex_sphere_radius", "Radius of the sphere", "", R, &R, &flg));
41009566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateSphereMesh_Internal(dm, dim, simplex, R));
41019371c9d4SSatish Balay     } break;
41029371c9d4SSatish Balay     case DM_SHAPE_BALL: {
41039318fe57SMatthew G. Knepley       PetscReal R = 1.0;
41049318fe57SMatthew G. Knepley 
41059566063dSJacob Faibussowitsch       PetscCall(PetscOptionsReal("-dm_plex_ball_radius", "Radius of the ball", "", R, &R, &flg));
41069566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateBallMesh_Internal(dm, dim, R));
41079371c9d4SSatish Balay     } break;
41089371c9d4SSatish Balay     case DM_SHAPE_CYLINDER: {
41099318fe57SMatthew G. Knepley       DMBoundaryType bdt = DM_BOUNDARY_NONE;
41109318fe57SMatthew G. Knepley       PetscInt       Nw  = 6;
4111*49704ca5SMatthew G. Knepley       PetscInt       Nr  = 0;
41129318fe57SMatthew G. Knepley 
41139566063dSJacob Faibussowitsch       PetscCall(PetscOptionsEnum("-dm_plex_cylinder_bd", "Boundary type in the z direction", "", DMBoundaryTypes, (PetscEnum)bdt, (PetscEnum *)&bdt, NULL));
41149566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-dm_plex_cylinder_num_wedges", "Number of wedges around the cylinder", "", Nw, &Nw, NULL));
4115*49704ca5SMatthew G. Knepley       PetscCall(PetscOptionsInt("-dm_plex_cylinder_num_refine", "Number of refinements before projection", "", Nr, &Nr, NULL));
41169318fe57SMatthew G. Knepley       switch (cell) {
4117d71ae5a4SJacob Faibussowitsch       case DM_POLYTOPE_TRI_PRISM_TENSOR:
4118d71ae5a4SJacob Faibussowitsch         PetscCall(DMPlexCreateWedgeCylinderMesh_Internal(dm, Nw, interpolate));
4119d71ae5a4SJacob Faibussowitsch         break;
4120d71ae5a4SJacob Faibussowitsch       default:
4121*49704ca5SMatthew G. Knepley         PetscCall(DMPlexCreateHexCylinderMesh_Internal(dm, bdt, Nr));
4122d71ae5a4SJacob Faibussowitsch         break;
41239318fe57SMatthew G. Knepley       }
41249371c9d4SSatish Balay     } break;
4125b7f5c055SJed Brown     case DM_SHAPE_SCHWARZ_P: // fallthrough
41269371c9d4SSatish Balay     case DM_SHAPE_GYROID: {
4127b7f5c055SJed Brown       PetscInt       extent[3] = {1, 1, 1}, refine = 0, layers = 0, three;
4128b7f5c055SJed Brown       PetscReal      thickness   = 0.;
4129b7f5c055SJed Brown       DMBoundaryType periodic[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
4130b7f5c055SJed Brown       DMPlexTPSType  tps_type    = shape == DM_SHAPE_SCHWARZ_P ? DMPLEX_TPS_SCHWARZ_P : DMPLEX_TPS_GYROID;
41311436d7faSJed Brown       PetscBool      tps_distribute;
41329566063dSJacob Faibussowitsch       PetscCall(PetscOptionsIntArray("-dm_plex_tps_extent", "Number of replicas for each of three dimensions", NULL, extent, (three = 3, &three), NULL));
41339566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-dm_plex_tps_refine", "Number of refinements", NULL, refine, &refine, NULL));
41349566063dSJacob Faibussowitsch       PetscCall(PetscOptionsEnumArray("-dm_plex_tps_periodic", "Periodicity in each of three dimensions", NULL, DMBoundaryTypes, (PetscEnum *)periodic, (three = 3, &three), NULL));
41359566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-dm_plex_tps_layers", "Number of layers in volumetric extrusion (or zero to not extrude)", NULL, layers, &layers, NULL));
41369566063dSJacob Faibussowitsch       PetscCall(PetscOptionsReal("-dm_plex_tps_thickness", "Thickness of volumetric extrusion", NULL, thickness, &thickness, NULL));
41379566063dSJacob Faibussowitsch       PetscCall(DMPlexDistributeGetDefault(dm, &tps_distribute));
41389566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_tps_distribute", "Distribute the 2D mesh prior to refinement and extrusion", NULL, tps_distribute, &tps_distribute, NULL));
41399566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateTPSMesh_Internal(dm, tps_type, extent, periodic, tps_distribute, refine, layers, thickness));
41409371c9d4SSatish Balay     } break;
41419371c9d4SSatish Balay     case DM_SHAPE_DOUBLET: {
414205bd46c0SStefano Zampini       DM        dmnew;
414305bd46c0SStefano Zampini       PetscReal rl = 0.0;
414405bd46c0SStefano Zampini 
414505bd46c0SStefano Zampini       PetscCall(PetscOptionsReal("-dm_plex_doublet_refinementlimit", "Refinement limit", NULL, rl, &rl, NULL));
414605bd46c0SStefano Zampini       PetscCall(DMPlexCreateDoublet(PetscObjectComm((PetscObject)dm), dim, simplex, interpolate, rl, &dmnew));
41475de52c6dSVaclav Hapla       PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
414869d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &dmnew));
41499371c9d4SSatish Balay     } break;
4150cfb853baSMatthew G. Knepley     case DM_SHAPE_HYPERCUBIC: {
4151cfb853baSMatthew G. Knepley       PetscInt       *edges;
4152cfb853baSMatthew G. Knepley       PetscReal      *lower, *upper;
4153cfb853baSMatthew G. Knepley       DMBoundaryType *bdt;
4154cfb853baSMatthew G. Knepley       PetscInt        n, d;
4155cfb853baSMatthew G. Knepley 
4156cfb853baSMatthew G. Knepley       *useCoordSpace = PETSC_FALSE;
4157cfb853baSMatthew G. Knepley       PetscCall(PetscMalloc4(dim, &edges, dim, &lower, dim, &upper, dim, &bdt));
4158cfb853baSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
4159cfb853baSMatthew G. Knepley         edges[d] = 1;
4160cfb853baSMatthew G. Knepley         lower[d] = 0.;
4161cfb853baSMatthew G. Knepley         upper[d] = 1.;
4162cfb853baSMatthew G. Knepley         bdt[d]   = DM_BOUNDARY_PERIODIC;
4163cfb853baSMatthew G. Knepley       }
4164cfb853baSMatthew G. Knepley       n = dim;
4165cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", edges, &n, &flg));
4166cfb853baSMatthew G. Knepley       n = dim;
4167cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
4168cfb853baSMatthew 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);
4169cfb853baSMatthew G. Knepley       n = dim;
4170cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
4171cfb853baSMatthew 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);
4172cfb853baSMatthew G. Knepley       n = dim;
4173cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *)bdt, &n, &flg));
4174cfb853baSMatthew 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);
4175cfb853baSMatthew G. Knepley       PetscCall(DMPlexCreateHypercubicMesh_Internal(dm, dim, lower, upper, edges, bdt));
4176cfb853baSMatthew G. Knepley       PetscCall(PetscFree4(edges, lower, upper, bdt));
4177cfb853baSMatthew G. Knepley     } break;
4178d71ae5a4SJacob Faibussowitsch     default:
4179d71ae5a4SJacob Faibussowitsch       SETERRQ(comm, PETSC_ERR_SUP, "Domain shape %s is unsupported", DMPlexShapes[shape]);
41809318fe57SMatthew G. Knepley     }
41819318fe57SMatthew G. Knepley   }
41829566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
418348a46eb9SPierre Jolivet   if (!((PetscObject)dm)->name && nameflg) PetscCall(PetscObjectSetName((PetscObject)dm, plexname));
41844e22dd4cSMatthew G. Knepley   // Allow label creation
41854e22dd4cSMatthew G. Knepley   PetscCall(PetscOptionsFindPairPrefix_Private(NULL, ((PetscObject)dm)->prefix, "-dm_plex_label_", &option, NULL, &flg));
41864e22dd4cSMatthew G. Knepley   if (flg) {
41874e22dd4cSMatthew G. Knepley     DMLabel     label;
41884e22dd4cSMatthew G. Knepley     PetscInt    points[1024], n = 1024;
41894e22dd4cSMatthew G. Knepley     char        fulloption[PETSC_MAX_PATH_LEN];
41904e22dd4cSMatthew G. Knepley     const char *name = &option[14];
41914e22dd4cSMatthew G. Knepley 
41924e22dd4cSMatthew G. Knepley     PetscCall(DMCreateLabel(dm, name));
41934e22dd4cSMatthew G. Knepley     PetscCall(DMGetLabel(dm, name, &label));
41944e22dd4cSMatthew G. Knepley     fulloption[0] = '-';
41954e22dd4cSMatthew G. Knepley     fulloption[1] = 0;
41964e22dd4cSMatthew G. Knepley     PetscCall(PetscStrlcat(fulloption, option, PETSC_MAX_PATH_LEN));
41974e22dd4cSMatthew G. Knepley     PetscCall(PetscOptionsGetIntArray(NULL, ((PetscObject)dm)->prefix, fulloption, points, &n, NULL));
41984e22dd4cSMatthew G. Knepley     for (PetscInt p = 0; p < n; ++p) PetscCall(DMLabelSetValue(label, points[p], 1));
41994e22dd4cSMatthew G. Knepley   }
4200dd0eeac9SMatthew G. Knepley   // Allow cohesive label creation
4201dd0eeac9SMatthew G. Knepley   //   Faces are input, completed, and all points are marked with their depth
4202dd0eeac9SMatthew G. Knepley   PetscCall(PetscOptionsFindPairPrefix_Private(NULL, ((PetscObject)dm)->prefix, "-dm_plex_cohesive_label_", &option, NULL, &flg));
4203dd0eeac9SMatthew G. Knepley   if (flg) {
4204dd0eeac9SMatthew G. Knepley     DMLabel  label;
4205dd0eeac9SMatthew G. Knepley     PetscInt points[1024], n, pStart, pEnd, Nl = 1;
4206dd0eeac9SMatthew G. Knepley     char     fulloption[PETSC_MAX_PATH_LEN];
4207dd0eeac9SMatthew G. Knepley     char     name[PETSC_MAX_PATH_LEN];
4208dd0eeac9SMatthew G. Knepley     size_t   len;
4209dd0eeac9SMatthew G. Knepley 
4210dd0eeac9SMatthew G. Knepley     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
4211dd0eeac9SMatthew G. Knepley     PetscCall(PetscStrncpy(name, &option[23], PETSC_MAX_PATH_LEN));
4212dd0eeac9SMatthew G. Knepley     PetscCall(PetscStrlen(name, &len));
4213dd0eeac9SMatthew G. Knepley     if (name[len - 1] == '0') Nl = 10;
4214dd0eeac9SMatthew G. Knepley     for (PetscInt l = 0; l < Nl; ++l) {
4215dd0eeac9SMatthew G. Knepley       if (l > 0) name[len - 1] = '0' + l;
4216dd0eeac9SMatthew G. Knepley       fulloption[0] = 0;
4217dd0eeac9SMatthew G. Knepley       PetscCall(PetscStrlcat(fulloption, "-dm_plex_cohesive_label_", 32));
4218dd0eeac9SMatthew G. Knepley       PetscCall(PetscStrlcat(fulloption, name, PETSC_MAX_PATH_LEN - 32));
4219dd0eeac9SMatthew G. Knepley       n = 1024;
4220dd0eeac9SMatthew G. Knepley       PetscCall(PetscOptionsGetIntArray(NULL, ((PetscObject)dm)->prefix, fulloption, points, &n, &flg));
4221dd0eeac9SMatthew G. Knepley       if (!flg) break;
4222dd0eeac9SMatthew G. Knepley       PetscCall(DMCreateLabel(dm, name));
4223dd0eeac9SMatthew G. Knepley       PetscCall(DMGetLabel(dm, name, &label));
4224dd0eeac9SMatthew G. Knepley       if (pStart >= pEnd) n = 0;
4225dd0eeac9SMatthew G. Knepley       for (PetscInt p = 0; p < n; ++p) {
4226dd0eeac9SMatthew G. Knepley         const PetscInt point   = points[p];
4227dd0eeac9SMatthew G. Knepley         PetscInt      *closure = NULL;
4228dd0eeac9SMatthew G. Knepley         PetscInt       clSize, pdepth;
4229dd0eeac9SMatthew G. Knepley 
4230dd0eeac9SMatthew G. Knepley         PetscCall(DMPlexGetPointDepth(dm, point, &pdepth));
4231dd0eeac9SMatthew G. Knepley         PetscCall(DMLabelSetValue(label, point, pdepth));
4232dd0eeac9SMatthew G. Knepley         PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &clSize, &closure));
4233dd0eeac9SMatthew G. Knepley         for (PetscInt cl = 0; cl < clSize * 2; cl += 2) {
4234dd0eeac9SMatthew G. Knepley           PetscCall(DMPlexGetPointDepth(dm, closure[cl], &pdepth));
4235dd0eeac9SMatthew G. Knepley           PetscCall(DMLabelSetValue(label, closure[cl], pdepth));
4236dd0eeac9SMatthew G. Knepley         }
4237dd0eeac9SMatthew G. Knepley         PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &clSize, &closure));
4238dd0eeac9SMatthew G. Knepley       }
4239dd0eeac9SMatthew G. Knepley       PetscCall(DMPlexOrientLabel(dm, label));
42400542aa8cSMatthew G. Knepley       PetscCall(DMPlexLabelCohesiveComplete(dm, label, NULL, 1, PETSC_FALSE, PETSC_FALSE, NULL));
4241dd0eeac9SMatthew G. Knepley     }
4242dd0eeac9SMatthew G. Knepley   }
4243708be2fdSJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_CreateFromOptions, dm, 0, 0, 0));
42443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
42450a6ba040SMatthew G. Knepley }
42460a6ba040SMatthew G. Knepley 
4247d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions_NonRefinement_Plex(DM dm, PetscOptionItems *PetscOptionsObject)
4248d71ae5a4SJacob Faibussowitsch {
42490a6ba040SMatthew G. Knepley   DM_Plex  *mesh = (DM_Plex *)dm->data;
42507f9d8d6cSVaclav Hapla   PetscBool flg, flg2;
42519318fe57SMatthew G. Knepley   char      bdLabel[PETSC_MAX_PATH_LEN];
4252adc21957SMatthew G. Knepley   char      method[PETSC_MAX_PATH_LEN];
42530a6ba040SMatthew G. Knepley 
42540a6ba040SMatthew G. Knepley   PetscFunctionBegin;
42550a6ba040SMatthew G. Knepley   /* Handle viewing */
42569566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_print_set_values", "Output all set values info", "DMPlexMatSetClosure", PETSC_FALSE, &mesh->printSetValues, NULL));
42575962854dSMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_fem", "Debug output level for all fem computations", "DMPlexSNESComputeResidualFEM", 0, &mesh->printFEM, NULL, 0));
42585962854dSMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_fvm", "Debug output level for all fvm computations", "DMPlexSNESComputeResidualFVM", 0, &mesh->printFVM, NULL, 0));
42599566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-dm_plex_print_tol", "Tolerance for FEM output", "DMPlexSNESComputeResidualFEM", mesh->printTol, &mesh->printTol, NULL));
42609566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_l2", "Debug output level all L2 diff computations", "DMComputeL2Diff", 0, &mesh->printL2, NULL, 0));
4261f5867de0SMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_locate", "Debug output level all point location computations", "DMLocatePoints", 0, &mesh->printLocate, NULL, 0));
42629566063dSJacob Faibussowitsch   PetscCall(DMMonitorSetFromOptions(dm, "-dm_plex_monitor_throughput", "Monitor the simulation throughput", "DMPlexMonitorThroughput", DMPlexMonitorThroughput, NULL, &flg));
42639566063dSJacob Faibussowitsch   if (flg) PetscCall(PetscLogDefaultBegin());
42649318fe57SMatthew G. Knepley   /* Labeling */
42659566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_boundary_label", "Label to mark the mesh boundary", "", bdLabel, bdLabel, sizeof(bdLabel), &flg));
42669566063dSJacob Faibussowitsch   if (flg) PetscCall(DMPlexCreateBoundaryLabel_Private(dm, bdLabel));
4267953fc75cSMatthew G. Knepley   /* Point Location */
42689566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_hash_location", "Use grid hashing for point location", "DMInterpolate", PETSC_FALSE, &mesh->useHashLocation, NULL));
42690848f4b5SMatthew G. Knepley   /* Partitioning and distribution */
42709566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_partition_balance", "Attempt to evenly divide points on partition boundary between processes", "DMPlexSetPartitionBalance", PETSC_FALSE, &mesh->partitionBalance, NULL));
4271d02c7345SMatthew G. Knepley   /* Reordering */
4272adc21957SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_reorder_section", "Compute point permutation for local section", "DMReorderSectionSetDefault", PETSC_FALSE, &flg2, &flg));
4273adc21957SMatthew G. Knepley   if (flg) PetscCall(DMReorderSectionSetDefault(dm, flg2 ? DM_REORDER_DEFAULT_TRUE : DM_REORDER_DEFAULT_FALSE));
4274adc21957SMatthew G. Knepley   PetscCall(PetscOptionsString("-dm_reorder_section_type", "Reordering method for local section", "DMReorderSectionSetType", method, method, PETSC_MAX_PATH_LEN, &flg));
4275adc21957SMatthew G. Knepley   if (flg) PetscCall(DMReorderSectionSetType(dm, method));
42762e62ab5aSMatthew G. Knepley   /* Generation and remeshing */
42779566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_remesh_bd", "Allow changes to the boundary on remeshing", "DMAdapt", PETSC_FALSE, &mesh->remeshBd, NULL));
4278b29cfa1cSToby Isaac   /* Projection behavior */
4279d5b43468SJose E. Roman   PetscCall(PetscOptionsBoundedInt("-dm_plex_max_projection_height", "Maximum mesh point height used to project locally", "DMPlexSetMaxProjectionHeight", 0, &mesh->maxProjectionHeight, NULL, 0));
42809566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_regular_refinement", "Use special nested projection algorithm for regular refinement", "DMPlexSetRegularRefinement", mesh->regularRefinement, &mesh->regularRefinement, NULL));
4281f12cf164SMatthew G. Knepley   /* Checking structure */
4282f12cf164SMatthew G. Knepley   {
42837f9d8d6cSVaclav Hapla     PetscBool all = PETSC_FALSE;
4284f12cf164SMatthew G. Knepley 
42857f9d8d6cSVaclav Hapla     PetscCall(PetscOptionsBool("-dm_plex_check_all", "Perform all basic checks", "DMPlexCheck", PETSC_FALSE, &all, NULL));
42867f9d8d6cSVaclav Hapla     if (all) {
42877f9d8d6cSVaclav Hapla       PetscCall(DMPlexCheck(dm));
42887f9d8d6cSVaclav Hapla     } else {
42899566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_symmetry", "Check that the adjacency information in the mesh is symmetric", "DMPlexCheckSymmetry", PETSC_FALSE, &flg, &flg2));
42907f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckSymmetry(dm));
42919566063dSJacob 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));
42927f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckSkeleton(dm, 0));
42939566063dSJacob 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));
42947f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckFaces(dm, 0));
42959566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_geometry", "Check that cells have positive volume", "DMPlexCheckGeometry", PETSC_FALSE, &flg, &flg2));
42967f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckGeometry(dm));
42979566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_pointsf", "Check some necessary conditions for PointSF", "DMPlexCheckPointSF", PETSC_FALSE, &flg, &flg2));
4298d7d32a9aSMatthew G. Knepley       if (flg && flg2) PetscCall(DMPlexCheckPointSF(dm, NULL, PETSC_FALSE));
42999566063dSJacob 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));
43007f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckInterfaceCones(dm));
43017f9d8d6cSVaclav Hapla     }
43029566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-dm_plex_check_cell_shape", "Check cell shape", "DMPlexCheckCellShape", PETSC_FALSE, &flg, &flg2));
43039566063dSJacob Faibussowitsch     if (flg && flg2) PetscCall(DMPlexCheckCellShape(dm, PETSC_TRUE, PETSC_DETERMINE));
4304f12cf164SMatthew G. Knepley   }
43059318fe57SMatthew G. Knepley   {
43069318fe57SMatthew G. Knepley     PetscReal scale = 1.0;
43074f3833eaSMatthew G. Knepley 
43089566063dSJacob Faibussowitsch     PetscCall(PetscOptionsReal("-dm_plex_scale", "Scale factor for mesh coordinates", "DMPlexScale", scale, &scale, &flg));
43099318fe57SMatthew G. Knepley     if (flg) {
43109318fe57SMatthew G. Knepley       Vec coordinates, coordinatesLocal;
43119318fe57SMatthew G. Knepley 
43129566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinates(dm, &coordinates));
43139566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinatesLocal(dm, &coordinatesLocal));
43149566063dSJacob Faibussowitsch       PetscCall(VecScale(coordinates, scale));
43159566063dSJacob Faibussowitsch       PetscCall(VecScale(coordinatesLocal, scale));
43169318fe57SMatthew G. Knepley     }
43179318fe57SMatthew G. Knepley   }
43189566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerSetFromOptions(mesh->partitioner));
43193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
432068d4fef7SMatthew G. Knepley }
432168d4fef7SMatthew G. Knepley 
4322d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions_Overlap_Plex(DM dm, PetscOptionItems *PetscOptionsObject, PetscInt *overlap)
4323d71ae5a4SJacob Faibussowitsch {
4324c506a872SMatthew G. Knepley   PetscInt  numOvLabels = 16, numOvExLabels = 16;
4325c506a872SMatthew G. Knepley   char     *ovLabelNames[16], *ovExLabelNames[16];
4326c506a872SMatthew G. Knepley   PetscInt  numOvValues = 16, numOvExValues = 16, l;
4327c506a872SMatthew G. Knepley   PetscBool flg;
4328c506a872SMatthew G. Knepley 
4329c506a872SMatthew G. Knepley   PetscFunctionBegin;
4330c506a872SMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_distribute_overlap", "The size of the overlap halo", "DMPlexDistribute", *overlap, overlap, NULL, 0));
4331c506a872SMatthew G. Knepley   PetscCall(PetscOptionsStringArray("-dm_distribute_overlap_labels", "List of overlap label names", "DMPlexDistribute", ovLabelNames, &numOvLabels, &flg));
4332c506a872SMatthew G. Knepley   if (!flg) numOvLabels = 0;
4333c506a872SMatthew G. Knepley   if (numOvLabels) {
4334c506a872SMatthew G. Knepley     ((DM_Plex *)dm->data)->numOvLabels = numOvLabels;
4335c506a872SMatthew G. Knepley     for (l = 0; l < numOvLabels; ++l) {
4336c506a872SMatthew G. Knepley       PetscCall(DMGetLabel(dm, ovLabelNames[l], &((DM_Plex *)dm->data)->ovLabels[l]));
4337c506a872SMatthew G. Knepley       PetscCheck(((DM_Plex *)dm->data)->ovLabels[l], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid label name %s", ovLabelNames[l]);
4338c506a872SMatthew G. Knepley       PetscCall(PetscFree(ovLabelNames[l]));
4339c506a872SMatthew G. Knepley     }
4340c506a872SMatthew G. Knepley     PetscCall(PetscOptionsIntArray("-dm_distribute_overlap_values", "List of overlap label values", "DMPlexDistribute", ((DM_Plex *)dm->data)->ovValues, &numOvValues, &flg));
4341c506a872SMatthew G. Knepley     if (!flg) numOvValues = 0;
4342c506a872SMatthew 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);
4343c506a872SMatthew G. Knepley 
4344c506a872SMatthew G. Knepley     PetscCall(PetscOptionsStringArray("-dm_distribute_overlap_exclude_labels", "List of overlap exclude label names", "DMPlexDistribute", ovExLabelNames, &numOvExLabels, &flg));
4345c506a872SMatthew G. Knepley     if (!flg) numOvExLabels = 0;
4346c506a872SMatthew G. Knepley     ((DM_Plex *)dm->data)->numOvExLabels = numOvExLabels;
4347c506a872SMatthew G. Knepley     for (l = 0; l < numOvExLabels; ++l) {
4348c506a872SMatthew G. Knepley       PetscCall(DMGetLabel(dm, ovExLabelNames[l], &((DM_Plex *)dm->data)->ovExLabels[l]));
4349c506a872SMatthew G. Knepley       PetscCheck(((DM_Plex *)dm->data)->ovExLabels[l], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid label name %s", ovExLabelNames[l]);
4350c506a872SMatthew G. Knepley       PetscCall(PetscFree(ovExLabelNames[l]));
4351c506a872SMatthew G. Knepley     }
4352c506a872SMatthew G. Knepley     PetscCall(PetscOptionsIntArray("-dm_distribute_overlap_exclude_values", "List of overlap exclude label values", "DMPlexDistribute", ((DM_Plex *)dm->data)->ovExValues, &numOvExValues, &flg));
4353c506a872SMatthew G. Knepley     if (!flg) numOvExValues = 0;
4354c506a872SMatthew 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);
4355c506a872SMatthew G. Knepley   }
43563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4357c506a872SMatthew G. Knepley }
4358c506a872SMatthew G. Knepley 
4359d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetFromOptions_Plex(DM dm, PetscOptionItems *PetscOptionsObject)
4360d71ae5a4SJacob Faibussowitsch {
4361bdf63967SMatthew G. Knepley   PetscFunctionList    ordlist;
4362bdf63967SMatthew G. Knepley   char                 oname[256];
43634e22dd4cSMatthew G. Knepley   char                 sublabelname[PETSC_MAX_PATH_LEN] = "";
4364adc21957SMatthew G. Knepley   DMReorderDefaultFlag reorder;
4365d410b0cfSMatthew G. Knepley   PetscReal            volume    = -1.0;
43669318fe57SMatthew G. Knepley   PetscInt             prerefine = 0, refine = 0, r, coarsen = 0, overlap = 0, extLayers = 0, dim;
43671b742f01SMatthew 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, flg;
436868d4fef7SMatthew G. Knepley 
436968d4fef7SMatthew G. Knepley   PetscFunctionBegin;
4370d0609cedSBarry Smith   PetscOptionsHeadBegin(PetscOptionsObject, "DMPlex Options");
4371dd4c3f67SMatthew G. Knepley   if (dm->cloneOpts) goto non_refine;
43729318fe57SMatthew G. Knepley   /* Handle automatic creation */
43739566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
43746bc1bd01Sksagiyam   if (dim < 0) {
43756bc1bd01Sksagiyam     PetscCall(DMPlexCreateFromOptions_Internal(PetscOptionsObject, &coordSpace, dm));
43766bc1bd01Sksagiyam     created = PETSC_TRUE;
43776bc1bd01Sksagiyam   }
43786bc1bd01Sksagiyam   PetscCall(DMGetDimension(dm, &dim));
4379d89e6e46SMatthew G. Knepley   /* Handle interpolation before distribution */
43809566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_interpolate_pre", "Flag to interpolate mesh before distribution", "", interpolate, &interpolate, &flg));
4381d89e6e46SMatthew G. Knepley   if (flg) {
4382d89e6e46SMatthew G. Knepley     DMPlexInterpolatedFlag interpolated;
4383d89e6e46SMatthew G. Knepley 
43849566063dSJacob Faibussowitsch     PetscCall(DMPlexIsInterpolated(dm, &interpolated));
4385d89e6e46SMatthew G. Knepley     if (interpolated == DMPLEX_INTERPOLATED_FULL && !interpolate) {
4386d89e6e46SMatthew G. Knepley       DM udm;
4387d89e6e46SMatthew G. Knepley 
43889566063dSJacob Faibussowitsch       PetscCall(DMPlexUninterpolate(dm, &udm));
438969d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &udm));
4390d89e6e46SMatthew G. Knepley     } else if (interpolated != DMPLEX_INTERPOLATED_FULL && interpolate) {
4391d89e6e46SMatthew G. Knepley       DM idm;
4392d89e6e46SMatthew G. Knepley 
43939566063dSJacob Faibussowitsch       PetscCall(DMPlexInterpolate(dm, &idm));
439469d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &idm));
4395d89e6e46SMatthew G. Knepley     }
4396d89e6e46SMatthew G. Knepley   }
43974e22dd4cSMatthew G. Knepley   // Handle submesh selection before distribution
43984e22dd4cSMatthew G. Knepley   PetscCall(PetscOptionsString("-dm_plex_submesh", "Label to use for submesh selection", "", sublabelname, sublabelname, PETSC_MAX_PATH_LEN, &flg));
43994e22dd4cSMatthew G. Knepley   if (flg) {
44004e22dd4cSMatthew G. Knepley     DM              subdm;
44014e22dd4cSMatthew G. Knepley     DMLabel         label;
44024e22dd4cSMatthew G. Knepley     IS              valueIS, pointIS;
44034e22dd4cSMatthew G. Knepley     const PetscInt *values, *points;
44044e22dd4cSMatthew G. Knepley     PetscBool       markedFaces = PETSC_FALSE;
44054e22dd4cSMatthew G. Knepley     PetscInt        Nv, value, Np;
44064e22dd4cSMatthew G. Knepley 
44074e22dd4cSMatthew G. Knepley     PetscCall(DMGetLabel(dm, sublabelname, &label));
44084e22dd4cSMatthew G. Knepley     PetscCall(DMLabelGetNumValues(label, &Nv));
44094e22dd4cSMatthew 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);
44104e22dd4cSMatthew G. Knepley     PetscCall(DMLabelGetValueIS(label, &valueIS));
44114e22dd4cSMatthew G. Knepley     PetscCall(ISGetIndices(valueIS, &values));
44124e22dd4cSMatthew G. Knepley     value = values[0];
44134e22dd4cSMatthew G. Knepley     PetscCall(ISRestoreIndices(valueIS, &values));
44144e22dd4cSMatthew G. Knepley     PetscCall(ISDestroy(&valueIS));
44154e22dd4cSMatthew G. Knepley     PetscCall(DMLabelGetStratumSize(label, value, &Np));
44164e22dd4cSMatthew G. Knepley     PetscCall(DMLabelGetStratumIS(label, value, &pointIS));
44174e22dd4cSMatthew G. Knepley     PetscCall(ISGetIndices(pointIS, &points));
44184e22dd4cSMatthew G. Knepley     for (PetscInt p = 0; p < Np; ++p) {
44194e22dd4cSMatthew G. Knepley       PetscInt pdepth;
44204e22dd4cSMatthew G. Knepley 
44214e22dd4cSMatthew G. Knepley       PetscCall(DMPlexGetPointDepth(dm, points[p], &pdepth));
44224e22dd4cSMatthew G. Knepley       if (pdepth) {
44234e22dd4cSMatthew G. Knepley         markedFaces = PETSC_TRUE;
44244e22dd4cSMatthew G. Knepley         break;
44254e22dd4cSMatthew G. Knepley       }
44264e22dd4cSMatthew G. Knepley     }
44274e22dd4cSMatthew G. Knepley     PetscCall(ISRestoreIndices(pointIS, &points));
44284e22dd4cSMatthew G. Knepley     PetscCall(ISDestroy(&pointIS));
44294e22dd4cSMatthew G. Knepley     PetscCall(DMPlexCreateSubmesh(dm, label, value, markedFaces, &subdm));
44304e22dd4cSMatthew G. Knepley     PetscCall(DMPlexReplace_Internal(dm, &subdm));
44314e22dd4cSMatthew G. Knepley     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
44324e22dd4cSMatthew G. Knepley   }
44339b44eab4SMatthew G. Knepley   /* Handle DMPlex refinement before distribution */
44349566063dSJacob Faibussowitsch   PetscCall(DMPlexGetRefinementUniform(dm, &uniformOrig));
44359566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine_pre", "The number of refinements before distribution", "DMCreate", prerefine, &prerefine, NULL, 0));
44369566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_remap_pre", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL));
44379566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_uniform_pre", "Flag for uniform refinement before distribution", "DMCreate", uniform, &uniform, &flg));
44389566063dSJacob Faibussowitsch   if (flg) PetscCall(DMPlexSetRefinementUniform(dm, uniform));
44399566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-dm_refine_volume_limit_pre", "The maximum cell volume after refinement before distribution", "DMCreate", volume, &volume, &flg));
44409318fe57SMatthew G. Knepley   if (flg) {
44419566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementUniform(dm, PETSC_FALSE));
44429566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementLimit(dm, volume));
44439318fe57SMatthew G. Knepley     prerefine = PetscMax(prerefine, 1);
44449318fe57SMatthew G. Knepley   }
4445b23db253SStefano Zampini   if (prerefine) PetscCall(DMLocalizeCoordinates(dm));
44469b44eab4SMatthew G. Knepley   for (r = 0; r < prerefine; ++r) {
44479b44eab4SMatthew G. Knepley     DM             rdm;
44489b44eab4SMatthew G. Knepley     PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc;
44499b44eab4SMatthew G. Knepley 
4450dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
44519566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &rdm));
445269d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &rdm));
4453dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
445461a622f3SMatthew G. Knepley     if (coordFunc && remap) {
44559566063dSJacob Faibussowitsch       PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
44569b44eab4SMatthew G. Knepley       ((DM_Plex *)dm->data)->coordFunc = coordFunc;
44579b44eab4SMatthew G. Knepley     }
44589b44eab4SMatthew G. Knepley   }
44599566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, uniformOrig));
44609318fe57SMatthew G. Knepley   /* Handle DMPlex extrusion before distribution */
44619566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_extrude", "The number of layers to extrude", "", extLayers, &extLayers, NULL, 0));
44629318fe57SMatthew G. Knepley   if (extLayers) {
44639318fe57SMatthew G. Knepley     DM edm;
44649318fe57SMatthew G. Knepley 
44659566063dSJacob Faibussowitsch     PetscCall(DMExtrude(dm, extLayers, &edm));
446669d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &edm));
446748d16a33SMatthew G. Knepley     ((DM_Plex *)dm->data)->coordFunc = NULL;
4468dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
4469d410b0cfSMatthew G. Knepley     extLayers = 0;
44705e17fc22SAidan Hamilton     PetscCall(DMGetDimension(dm, &dim));
44719318fe57SMatthew G. Knepley   }
4472bdf63967SMatthew G. Knepley   /* Handle DMPlex reordering before distribution */
44736bc1bd01Sksagiyam   PetscCall(DMPlexReorderGetDefault(dm, &reorder));
44749566063dSJacob Faibussowitsch   PetscCall(MatGetOrderingList(&ordlist));
44756bc1bd01Sksagiyam   PetscCall(PetscStrncpy(oname, MATORDERINGNATURAL, sizeof(oname)));
44769566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_plex_reorder", "Set mesh reordering type", "DMPlexGetOrdering", ordlist, MATORDERINGNATURAL, oname, sizeof(oname), &flg));
4477adc21957SMatthew G. Knepley   if (reorder == DM_REORDER_DEFAULT_TRUE || flg) {
4478bdf63967SMatthew G. Knepley     DM pdm;
4479bdf63967SMatthew G. Knepley     IS perm;
4480bdf63967SMatthew G. Knepley 
44819566063dSJacob Faibussowitsch     PetscCall(DMPlexGetOrdering(dm, oname, NULL, &perm));
44829566063dSJacob Faibussowitsch     PetscCall(DMPlexPermute(dm, perm, &pdm));
44839566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&perm));
448469d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &pdm));
4485dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
4486bdf63967SMatthew G. Knepley   }
44879b44eab4SMatthew G. Knepley   /* Handle DMPlex distribution */
44889566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeGetDefault(dm, &distribute));
4489c506a872SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_distribute", "Flag to redistribute a mesh among processes", "DMPlexDistribute", distribute, &distribute, NULL));
4490a286e215SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_distribute_save_sf", "Flag to save the migration SF", "DMPlexSetMigrationSF", saveSF, &saveSF, NULL));
4491dbbe0bcdSBarry Smith   PetscCall(DMSetFromOptions_Overlap_Plex(dm, PetscOptionsObject, &overlap));
44929b44eab4SMatthew G. Knepley   if (distribute) {
44939b44eab4SMatthew G. Knepley     DM               pdm = NULL;
44949b44eab4SMatthew G. Knepley     PetscPartitioner part;
4495a286e215SMatthew G. Knepley     PetscSF          sfMigration;
44969b44eab4SMatthew G. Knepley 
44979566063dSJacob Faibussowitsch     PetscCall(DMPlexGetPartitioner(dm, &part));
44989566063dSJacob Faibussowitsch     PetscCall(PetscPartitionerSetFromOptions(part));
4499a286e215SMatthew G. Knepley     PetscCall(DMPlexDistribute(dm, overlap, &sfMigration, &pdm));
450048a46eb9SPierre Jolivet     if (pdm) PetscCall(DMPlexReplace_Internal(dm, &pdm));
4501a286e215SMatthew G. Knepley     if (saveSF) PetscCall(DMPlexSetMigrationSF(dm, sfMigration));
4502a286e215SMatthew G. Knepley     PetscCall(PetscSFDestroy(&sfMigration));
45039b44eab4SMatthew G. Knepley   }
4504d2b2dc1eSMatthew G. Knepley   /* Must check CEED options before creating function space for coordinates */
4505d2b2dc1eSMatthew G. Knepley   {
4506d2b2dc1eSMatthew G. Knepley     PetscBool useCeed = PETSC_FALSE, flg;
4507d2b2dc1eSMatthew G. Knepley 
4508d2b2dc1eSMatthew G. Knepley     PetscCall(PetscOptionsBool("-dm_plex_use_ceed", "Use LibCEED as the FEM backend", "DMPlexSetUseCeed", useCeed, &useCeed, &flg));
4509d2b2dc1eSMatthew G. Knepley     if (flg) PetscCall(DMPlexSetUseCeed(dm, useCeed));
4510d2b2dc1eSMatthew G. Knepley   }
45119318fe57SMatthew G. Knepley   /* Create coordinate space */
45129318fe57SMatthew G. Knepley   if (created) {
451361a622f3SMatthew G. Knepley     DM_Plex  *mesh   = (DM_Plex *)dm->data;
4514e44f6aebSMatthew G. Knepley     PetscInt  degree = 1, deg;
45155515ebd3SMatthew G. Knepley     PetscInt  height = 0;
45165515ebd3SMatthew G. Knepley     DM        cdm;
4517c3db174cSMatthew G. Knepley     PetscBool flg, localize = PETSC_TRUE, sparseLocalize = PETSC_TRUE;
45189318fe57SMatthew G. Knepley 
45199566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-dm_coord_space", "Use an FEM space for coordinates", "", coordSpace, &coordSpace, &flg));
45209566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-dm_coord_petscspace_degree", "FEM degree for coordinate space", "", degree, &degree, NULL));
4521e44f6aebSMatthew G. Knepley     PetscCall(DMGetCoordinateDegree_Internal(dm, &deg));
4522e44f6aebSMatthew G. Knepley     if (coordSpace && deg <= 1) PetscCall(DMPlexCreateCoordinateSpace(dm, degree, PETSC_TRUE, mesh->coordFunc));
45235515ebd3SMatthew G. Knepley     PetscCall(DMGetCoordinateDM(dm, &cdm));
452461a622f3SMatthew G. Knepley     if (flg && !coordSpace) {
452561a622f3SMatthew G. Knepley       PetscDS      cds;
452661a622f3SMatthew G. Knepley       PetscObject  obj;
452761a622f3SMatthew G. Knepley       PetscClassId id;
452861a622f3SMatthew G. Knepley 
45299566063dSJacob Faibussowitsch       PetscCall(DMGetDS(cdm, &cds));
45309566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
45319566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
453261a622f3SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
453361a622f3SMatthew G. Knepley         PetscContainer dummy;
453461a622f3SMatthew G. Knepley 
45359566063dSJacob Faibussowitsch         PetscCall(PetscContainerCreate(PETSC_COMM_SELF, &dummy));
45369566063dSJacob Faibussowitsch         PetscCall(PetscObjectSetName((PetscObject)dummy, "coordinates"));
45379566063dSJacob Faibussowitsch         PetscCall(DMSetField(cdm, 0, NULL, (PetscObject)dummy));
45389566063dSJacob Faibussowitsch         PetscCall(PetscContainerDestroy(&dummy));
45399566063dSJacob Faibussowitsch         PetscCall(DMClearDS(cdm));
454061a622f3SMatthew G. Knepley       }
454161a622f3SMatthew G. Knepley       mesh->coordFunc = NULL;
454261a622f3SMatthew G. Knepley     }
4543c3db174cSMatthew G. Knepley     PetscCall(PetscOptionsBool("-dm_localize", "Localize mesh coordinates", "", localize, &localize, NULL));
4544c3db174cSMatthew G. Knepley     PetscCall(PetscOptionsBool("-dm_sparse_localize", "Localize only necessary cells", "DMSetSparseLocalize", sparseLocalize, &sparseLocalize, &flg));
4545c3db174cSMatthew G. Knepley     if (flg) PetscCall(DMSetSparseLocalize(dm, sparseLocalize));
45465515ebd3SMatthew G. Knepley     PetscCall(PetscOptionsInt("-dm_localize_height", "Localize edges and faces in addition to cells", "", height, &height, &flg));
45475515ebd3SMatthew G. Knepley     if (flg) PetscCall(DMPlexSetMaxProjectionHeight(cdm, height));
4548c3db174cSMatthew G. Knepley     if (localize) PetscCall(DMLocalizeCoordinates(dm));
45499318fe57SMatthew G. Knepley   }
455068d4fef7SMatthew G. Knepley   /* Handle DMPlex refinement */
455161a622f3SMatthew G. Knepley   remap = PETSC_TRUE;
45529566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine", "The number of uniform refinements", "DMCreate", refine, &refine, NULL, 0));
45539566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_remap", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL));
45549566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine_hierarchy", "The number of uniform refinements", "DMCreate", refine, &refine, &isHierarchy, 0));
45559566063dSJacob Faibussowitsch   if (refine) PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
455668d4fef7SMatthew G. Knepley   if (refine && isHierarchy) {
4557acdc6f61SToby Isaac     DM *dms, coarseDM;
455868d4fef7SMatthew G. Knepley 
45599566063dSJacob Faibussowitsch     PetscCall(DMGetCoarseDM(dm, &coarseDM));
45609566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)coarseDM));
45619566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(refine, &dms));
45629566063dSJacob Faibussowitsch     PetscCall(DMRefineHierarchy(dm, refine, dms));
456368d4fef7SMatthew G. Knepley     /* Total hack since we do not pass in a pointer */
45649566063dSJacob Faibussowitsch     PetscCall(DMPlexSwap_Static(dm, dms[refine - 1]));
456568d4fef7SMatthew G. Knepley     if (refine == 1) {
45669566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dm, dms[0]));
45679566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dm, PETSC_TRUE));
456868d4fef7SMatthew G. Knepley     } else {
45699566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dm, dms[refine - 2]));
45709566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dm, PETSC_TRUE));
45719566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dms[0], dms[refine - 1]));
45729566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dms[0], PETSC_TRUE));
457368d4fef7SMatthew G. Knepley     }
45749566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dms[refine - 1], coarseDM));
45759566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)coarseDM));
457668d4fef7SMatthew G. Knepley     /* Free DMs */
457768d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
4578dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dms[r], PetscOptionsObject));
45799566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dms[r]));
458068d4fef7SMatthew G. Knepley     }
45819566063dSJacob Faibussowitsch     PetscCall(PetscFree(dms));
458268d4fef7SMatthew G. Knepley   } else {
458368d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
45849318fe57SMatthew G. Knepley       DM             rdm;
458551a74b61SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc;
458668d4fef7SMatthew G. Knepley 
4587dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
45889566063dSJacob Faibussowitsch       PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &rdm));
458968d4fef7SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
459069d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &rdm));
4591dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
459261a622f3SMatthew G. Knepley       if (coordFunc && remap) {
45939566063dSJacob Faibussowitsch         PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
459451a74b61SMatthew G. Knepley         ((DM_Plex *)dm->data)->coordFunc = coordFunc;
459551a74b61SMatthew G. Knepley       }
459668d4fef7SMatthew G. Knepley     }
459768d4fef7SMatthew G. Knepley   }
45983cf6fe12SMatthew G. Knepley   /* Handle DMPlex coarsening */
45999566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_coarsen", "Coarsen the mesh", "DMCreate", coarsen, &coarsen, NULL, 0));
46009566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_coarsen_hierarchy", "The number of coarsenings", "DMCreate", coarsen, &coarsen, &isHierarchy, 0));
4601b653a561SMatthew G. Knepley   if (coarsen && isHierarchy) {
4602b653a561SMatthew G. Knepley     DM *dms;
4603b653a561SMatthew G. Knepley 
46049566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(coarsen, &dms));
46059566063dSJacob Faibussowitsch     PetscCall(DMCoarsenHierarchy(dm, coarsen, dms));
4606b653a561SMatthew G. Knepley     /* Free DMs */
4607b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
4608dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dms[r], PetscOptionsObject));
46099566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dms[r]));
4610b653a561SMatthew G. Knepley     }
46119566063dSJacob Faibussowitsch     PetscCall(PetscFree(dms));
4612b653a561SMatthew G. Knepley   } else {
4613b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
46149318fe57SMatthew G. Knepley       DM             cdm;
46159318fe57SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc;
46163cf6fe12SMatthew G. Knepley 
4617dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
46189566063dSJacob Faibussowitsch       PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &cdm));
46193cf6fe12SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
462069d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &cdm));
4621dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
46229318fe57SMatthew G. Knepley       if (coordFunc) {
46239566063dSJacob Faibussowitsch         PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
46249318fe57SMatthew G. Knepley         ((DM_Plex *)dm->data)->coordFunc = coordFunc;
46259318fe57SMatthew G. Knepley       }
46263cf6fe12SMatthew G. Knepley     }
4627b653a561SMatthew G. Knepley   }
4628be664eb1SMatthew G. Knepley   // Handle coordinate remapping
4629be664eb1SMatthew G. Knepley   remap = PETSC_FALSE;
4630be664eb1SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_coord_remap", "Flag to control coordinate remapping", "", remap, &remap, NULL));
4631be664eb1SMatthew G. Knepley   if (remap) {
4632be664eb1SMatthew G. Knepley     DMPlexCoordMap map     = DM_COORD_MAP_NONE;
4633be664eb1SMatthew G. Knepley     PetscPointFunc mapFunc = NULL;
4634be664eb1SMatthew G. Knepley     PetscScalar    params[16];
4635f45b553cSPierre Jolivet     PetscInt       Np = PETSC_STATIC_ARRAY_LENGTH(params), cdim;
4636be664eb1SMatthew G. Knepley     MPI_Comm       comm;
4637be664eb1SMatthew G. Knepley 
4638be664eb1SMatthew G. Knepley     PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4639be664eb1SMatthew G. Knepley     PetscCall(DMGetCoordinateDim(dm, &cdim));
4640be664eb1SMatthew G. Knepley     PetscCall(PetscOptionsScalarArray("-dm_coord_map_params", "Parameters for the coordinate remapping", "", params, &Np, &flg));
4641be664eb1SMatthew G. Knepley     if (!flg) Np = 0;
4642be664eb1SMatthew G. Knepley     // TODO Allow user to pass a map function by name
4643be664eb1SMatthew G. Knepley     PetscCall(PetscOptionsEnum("-dm_coord_map", "Coordinate mapping for built-in mesh", "", DMPlexCoordMaps, (PetscEnum)map, (PetscEnum *)&map, &flg));
4644be664eb1SMatthew G. Knepley     if (flg) {
4645be664eb1SMatthew G. Knepley       switch (map) {
4646be664eb1SMatthew G. Knepley       case DM_COORD_MAP_NONE:
4647be664eb1SMatthew G. Knepley         mapFunc = coordMap_identity;
4648be664eb1SMatthew G. Knepley         break;
4649be664eb1SMatthew G. Knepley       case DM_COORD_MAP_SHEAR:
4650be664eb1SMatthew G. Knepley         mapFunc = coordMap_shear;
4651be664eb1SMatthew G. Knepley         if (!Np) {
4652be664eb1SMatthew G. Knepley           Np        = cdim + 1;
4653be664eb1SMatthew G. Knepley           params[0] = 0;
4654be664eb1SMatthew G. Knepley           for (PetscInt d = 1; d <= cdim; ++d) params[d] = 1.0;
4655be664eb1SMatthew G. Knepley         }
4656be664eb1SMatthew 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);
4657be664eb1SMatthew G. Knepley         break;
4658be664eb1SMatthew G. Knepley       case DM_COORD_MAP_FLARE:
4659be664eb1SMatthew G. Knepley         mapFunc = coordMap_flare;
4660be664eb1SMatthew G. Knepley         if (!Np) {
4661be664eb1SMatthew G. Knepley           Np        = cdim + 1;
4662be664eb1SMatthew G. Knepley           params[0] = 0;
4663be664eb1SMatthew G. Knepley           for (PetscInt d = 1; d <= cdim; ++d) params[d] = 1.0;
4664be664eb1SMatthew G. Knepley         }
4665be664eb1SMatthew 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);
4666be664eb1SMatthew G. Knepley         break;
4667be664eb1SMatthew G. Knepley       case DM_COORD_MAP_ANNULUS:
4668be664eb1SMatthew G. Knepley         mapFunc = coordMap_annulus;
4669be664eb1SMatthew G. Knepley         if (!Np) {
4670be664eb1SMatthew G. Knepley           Np        = 2;
4671be664eb1SMatthew G. Knepley           params[0] = 1.;
4672be664eb1SMatthew G. Knepley           params[1] = 2.;
4673be664eb1SMatthew G. Knepley         }
4674be664eb1SMatthew G. Knepley         PetscCheck(Np == 2, comm, PETSC_ERR_ARG_WRONG, "The annulus coordinate map must have 2 parameters, not %" PetscInt_FMT, Np);
4675be664eb1SMatthew G. Knepley         break;
4676be664eb1SMatthew G. Knepley       case DM_COORD_MAP_SHELL:
4677be664eb1SMatthew G. Knepley         mapFunc = coordMap_shell;
4678be664eb1SMatthew G. Knepley         if (!Np) {
4679be664eb1SMatthew G. Knepley           Np        = 2;
4680be664eb1SMatthew G. Knepley           params[0] = 1.;
4681be664eb1SMatthew G. Knepley           params[1] = 2.;
4682be664eb1SMatthew G. Knepley         }
4683be664eb1SMatthew G. Knepley         PetscCheck(Np == 2, comm, PETSC_ERR_ARG_WRONG, "The spherical shell coordinate map must have 2 parameters, not %" PetscInt_FMT, Np);
4684be664eb1SMatthew G. Knepley         break;
4685be664eb1SMatthew G. Knepley       default:
4686be664eb1SMatthew G. Knepley         mapFunc = coordMap_identity;
4687be664eb1SMatthew G. Knepley       }
4688be664eb1SMatthew G. Knepley     }
4689be664eb1SMatthew G. Knepley     if (Np) {
4690be664eb1SMatthew G. Knepley       DM      cdm;
4691be664eb1SMatthew G. Knepley       PetscDS cds;
4692be664eb1SMatthew G. Knepley 
4693be664eb1SMatthew G. Knepley       PetscCall(DMGetCoordinateDM(dm, &cdm));
4694be664eb1SMatthew G. Knepley       PetscCall(DMGetDS(cdm, &cds));
4695be664eb1SMatthew G. Knepley       PetscCall(PetscDSSetConstants(cds, Np, params));
4696be664eb1SMatthew G. Knepley     }
4697be664eb1SMatthew G. Knepley     PetscCall(DMPlexRemapGeometry(dm, 0.0, mapFunc));
4698be664eb1SMatthew G. Knepley   }
4699909dfd52SMatthew G. Knepley   /* Handle ghost cells */
47009566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_create_fv_ghost_cells", "Flag to create finite volume ghost cells on the boundary", "DMCreate", ghostCells, &ghostCells, NULL));
4701909dfd52SMatthew G. Knepley   if (ghostCells) {
4702909dfd52SMatthew G. Knepley     DM   gdm;
4703909dfd52SMatthew G. Knepley     char lname[PETSC_MAX_PATH_LEN];
4704909dfd52SMatthew G. Knepley 
4705909dfd52SMatthew G. Knepley     lname[0] = '\0';
47069566063dSJacob Faibussowitsch     PetscCall(PetscOptionsString("-dm_plex_fv_ghost_cells_label", "Label name for ghost cells boundary", "DMCreate", lname, lname, sizeof(lname), &flg));
47079566063dSJacob Faibussowitsch     PetscCall(DMPlexConstructGhostCells(dm, flg ? lname : NULL, NULL, &gdm));
470869d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &gdm));
4709909dfd52SMatthew G. Knepley   }
47106913077dSMatthew G. Knepley   /* Handle 1D order */
4711adc21957SMatthew G. Knepley   if (reorder != DM_REORDER_DEFAULT_FALSE && dim == 1) {
47126913077dSMatthew G. Knepley     DM           cdm, rdm;
47136913077dSMatthew G. Knepley     PetscDS      cds;
47146913077dSMatthew G. Knepley     PetscObject  obj;
47156913077dSMatthew G. Knepley     PetscClassId id = PETSC_OBJECT_CLASSID;
47166913077dSMatthew G. Knepley     IS           perm;
47176bc1bd01Sksagiyam     PetscInt     Nf;
47186913077dSMatthew G. Knepley     PetscBool    distributed;
47196913077dSMatthew G. Knepley 
47209566063dSJacob Faibussowitsch     PetscCall(DMPlexIsDistributed(dm, &distributed));
47219566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
47229566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
47239566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(cds, &Nf));
47246913077dSMatthew G. Knepley     if (Nf) {
47259566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
47269566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
47276913077dSMatthew G. Knepley     }
47286bc1bd01Sksagiyam     if (!distributed && id != PETSCFE_CLASSID) {
47299566063dSJacob Faibussowitsch       PetscCall(DMPlexGetOrdering1D(dm, &perm));
47309566063dSJacob Faibussowitsch       PetscCall(DMPlexPermute(dm, perm, &rdm));
473169d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &rdm));
47329566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&perm));
47336913077dSMatthew G. Knepley     }
47346913077dSMatthew G. Knepley   }
47353cf6fe12SMatthew G. Knepley /* Handle */
4736dd4c3f67SMatthew G. Knepley non_refine:
4737dbbe0bcdSBarry Smith   PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
4738d0609cedSBarry Smith   PetscOptionsHeadEnd();
47393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
47400a6ba040SMatthew G. Knepley }
47410a6ba040SMatthew G. Knepley 
4742d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMCreateGlobalVector_Plex(DM dm, Vec *vec)
4743d71ae5a4SJacob Faibussowitsch {
4744552f7358SJed Brown   PetscFunctionBegin;
47459566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector_Section_Private(dm, vec));
47469566063dSJacob Faibussowitsch   /* PetscCall(VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM)); */
47479566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEW, (void (*)(void))VecView_Plex));
47489566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEWNATIVE, (void (*)(void))VecView_Plex_Native));
47499566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOAD, (void (*)(void))VecLoad_Plex));
47509566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOADNATIVE, (void (*)(void))VecLoad_Plex_Native));
47513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4752552f7358SJed Brown }
4753552f7358SJed Brown 
4754d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMCreateLocalVector_Plex(DM dm, Vec *vec)
4755d71ae5a4SJacob Faibussowitsch {
4756552f7358SJed Brown   PetscFunctionBegin;
47579566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector_Section_Private(dm, vec));
47589566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEW, (void (*)(void))VecView_Plex_Local));
47599566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOAD, (void (*)(void))VecLoad_Plex_Local));
47603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4761552f7358SJed Brown }
4762552f7358SJed Brown 
4763d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGetDimPoints_Plex(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
4764d71ae5a4SJacob Faibussowitsch {
4765793f3fe5SMatthew G. Knepley   PetscInt depth, d;
4766793f3fe5SMatthew G. Knepley 
4767793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
47689566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
4769793f3fe5SMatthew G. Knepley   if (depth == 1) {
47709566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(dm, &d));
47719566063dSJacob Faibussowitsch     if (dim == 0) PetscCall(DMPlexGetDepthStratum(dm, dim, pStart, pEnd));
47729566063dSJacob Faibussowitsch     else if (dim == d) PetscCall(DMPlexGetDepthStratum(dm, 1, pStart, pEnd));
47739371c9d4SSatish Balay     else {
47749371c9d4SSatish Balay       *pStart = 0;
47759371c9d4SSatish Balay       *pEnd   = 0;
47769371c9d4SSatish Balay     }
4777793f3fe5SMatthew G. Knepley   } else {
47789566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, dim, pStart, pEnd));
4779793f3fe5SMatthew G. Knepley   }
47803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4781793f3fe5SMatthew G. Knepley }
4782793f3fe5SMatthew G. Knepley 
4783d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGetNeighbors_Plex(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
4784d71ae5a4SJacob Faibussowitsch {
4785502a2867SDave May   PetscSF            sf;
47860a19bb7dSprj-   PetscInt           niranks, njranks, n;
47870a19bb7dSprj-   const PetscMPIInt *iranks, *jranks;
47880a19bb7dSprj-   DM_Plex           *data = (DM_Plex *)dm->data;
4789502a2867SDave May 
47902f356facSMatthew G. Knepley   PetscFunctionBegin;
47919566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
47920a19bb7dSprj-   if (!data->neighbors) {
47939566063dSJacob Faibussowitsch     PetscCall(PetscSFSetUp(sf));
47949566063dSJacob Faibussowitsch     PetscCall(PetscSFGetRootRanks(sf, &njranks, &jranks, NULL, NULL, NULL));
47959566063dSJacob Faibussowitsch     PetscCall(PetscSFGetLeafRanks(sf, &niranks, &iranks, NULL, NULL));
47969566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(njranks + niranks + 1, &data->neighbors));
47979566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(data->neighbors + 1, jranks, njranks));
47989566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(data->neighbors + njranks + 1, iranks, niranks));
47990a19bb7dSprj-     n = njranks + niranks;
48009566063dSJacob Faibussowitsch     PetscCall(PetscSortRemoveDupsMPIInt(&n, data->neighbors + 1));
48010a19bb7dSprj-     /* The following cast should never fail: can't have more neighbors than PETSC_MPI_INT_MAX */
48029566063dSJacob Faibussowitsch     PetscCall(PetscMPIIntCast(n, data->neighbors));
48030a19bb7dSprj-   }
48040a19bb7dSprj-   if (nranks) *nranks = data->neighbors[0];
48050a19bb7dSprj-   if (ranks) {
48060a19bb7dSprj-     if (data->neighbors[0]) *ranks = data->neighbors + 1;
48070a19bb7dSprj-     else *ranks = NULL;
48080a19bb7dSprj-   }
48093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4810502a2867SDave May }
4811502a2867SDave May 
48121eb70e55SToby Isaac PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM, DM, Mat, Vec, Vec);
48131eb70e55SToby Isaac 
4814d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMInitialize_Plex(DM dm)
4815d71ae5a4SJacob Faibussowitsch {
4816552f7358SJed Brown   PetscFunctionBegin;
4817552f7358SJed Brown   dm->ops->view                      = DMView_Plex;
48182c40f234SMatthew G. Knepley   dm->ops->load                      = DMLoad_Plex;
4819552f7358SJed Brown   dm->ops->setfromoptions            = DMSetFromOptions_Plex;
482038221697SMatthew G. Knepley   dm->ops->clone                     = DMClone_Plex;
4821552f7358SJed Brown   dm->ops->setup                     = DMSetUp_Plex;
48221bb6d2a8SBarry Smith   dm->ops->createlocalsection        = DMCreateLocalSection_Plex;
4823adc21957SMatthew G. Knepley   dm->ops->createsectionpermutation  = DMCreateSectionPermutation_Plex;
482466ad2231SToby Isaac   dm->ops->createdefaultconstraints  = DMCreateDefaultConstraints_Plex;
4825552f7358SJed Brown   dm->ops->createglobalvector        = DMCreateGlobalVector_Plex;
4826552f7358SJed Brown   dm->ops->createlocalvector         = DMCreateLocalVector_Plex;
4827184d77edSJed Brown   dm->ops->getlocaltoglobalmapping   = NULL;
48280298fd71SBarry Smith   dm->ops->createfieldis             = NULL;
4829552f7358SJed Brown   dm->ops->createcoordinatedm        = DMCreateCoordinateDM_Plex;
4830f19dbd58SToby Isaac   dm->ops->createcoordinatefield     = DMCreateCoordinateField_Plex;
48310a6ba040SMatthew G. Knepley   dm->ops->getcoloring               = NULL;
4832552f7358SJed Brown   dm->ops->creatematrix              = DMCreateMatrix_Plex;
4833bceba477SMatthew G. Knepley   dm->ops->createinterpolation       = DMCreateInterpolation_Plex;
4834bd041c0cSMatthew G. Knepley   dm->ops->createmassmatrix          = DMCreateMassMatrix_Plex;
4835b4937a87SMatthew G. Knepley   dm->ops->createmassmatrixlumped    = DMCreateMassMatrixLumped_Plex;
48365a84ad33SLisandro Dalcin   dm->ops->createinjection           = DMCreateInjection_Plex;
4837552f7358SJed Brown   dm->ops->refine                    = DMRefine_Plex;
48380a6ba040SMatthew G. Knepley   dm->ops->coarsen                   = DMCoarsen_Plex;
48390a6ba040SMatthew G. Knepley   dm->ops->refinehierarchy           = DMRefineHierarchy_Plex;
4840b653a561SMatthew G. Knepley   dm->ops->coarsenhierarchy          = DMCoarsenHierarchy_Plex;
4841d410b0cfSMatthew G. Knepley   dm->ops->extrude                   = DMExtrude_Plex;
48420298fd71SBarry Smith   dm->ops->globaltolocalbegin        = NULL;
48430298fd71SBarry Smith   dm->ops->globaltolocalend          = NULL;
48440298fd71SBarry Smith   dm->ops->localtoglobalbegin        = NULL;
48450298fd71SBarry Smith   dm->ops->localtoglobalend          = NULL;
4846552f7358SJed Brown   dm->ops->destroy                   = DMDestroy_Plex;
4847552f7358SJed Brown   dm->ops->createsubdm               = DMCreateSubDM_Plex;
48482adcc780SMatthew G. Knepley   dm->ops->createsuperdm             = DMCreateSuperDM_Plex;
4849793f3fe5SMatthew G. Knepley   dm->ops->getdimpoints              = DMGetDimPoints_Plex;
4850552f7358SJed Brown   dm->ops->locatepoints              = DMLocatePoints_Plex;
48510709b2feSToby Isaac   dm->ops->projectfunctionlocal      = DMProjectFunctionLocal_Plex;
48520709b2feSToby Isaac   dm->ops->projectfunctionlabellocal = DMProjectFunctionLabelLocal_Plex;
4853bfc4295aSToby Isaac   dm->ops->projectfieldlocal         = DMProjectFieldLocal_Plex;
48548c6c5593SMatthew G. Knepley   dm->ops->projectfieldlabellocal    = DMProjectFieldLabelLocal_Plex;
4855ece3a9fcSMatthew G. Knepley   dm->ops->projectbdfieldlabellocal  = DMProjectBdFieldLabelLocal_Plex;
48560709b2feSToby Isaac   dm->ops->computel2diff             = DMComputeL2Diff_Plex;
4857b698f381SToby Isaac   dm->ops->computel2gradientdiff     = DMComputeL2GradientDiff_Plex;
48582a16baeaSToby Isaac   dm->ops->computel2fielddiff        = DMComputeL2FieldDiff_Plex;
485928d58a37SPierre Jolivet   dm->ops->getneighbors              = DMGetNeighbors_Plex;
48606c6a6b79SMatthew G. Knepley   dm->ops->getlocalboundingbox       = DMGetLocalBoundingBox_Coordinates;
4861907a3e9cSStefano Zampini   dm->ops->createdomaindecomposition = DMCreateDomainDecomposition_Plex;
4862907a3e9cSStefano Zampini   dm->ops->createddscatters          = DMCreateDomainDecompositionScatters_Plex;
48639566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertBoundaryValues_C", DMPlexInsertBoundaryValues_Plex));
48646c51210dSStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertTimeDerivativeBoundaryValues_C", DMPlexInsertTimeDerivativeBoundaryValues_Plex));
48659566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMSetUpGLVisViewer_C", DMSetUpGLVisViewer_Plex));
48669566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMCreateNeumannOverlap_C", DMCreateNeumannOverlap_Plex));
48679566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeGetDefault_C", DMPlexDistributeGetDefault_Plex));
48689566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeSetDefault_C", DMPlexDistributeSetDefault_Plex));
48696bc1bd01Sksagiyam   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderGetDefault_C", DMPlexReorderGetDefault_Plex));
48706bc1bd01Sksagiyam   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderSetDefault_C", DMPlexReorderSetDefault_Plex));
4871adc21957SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionGetDefault_C", DMReorderSectionGetDefault_Plex));
4872adc21957SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionSetDefault_C", DMReorderSectionSetDefault_Plex));
4873adc21957SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionGetType_C", DMReorderSectionGetType_Plex));
4874adc21957SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionSetType_C", DMReorderSectionSetType_Plex));
48759566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMInterpolateSolution_C", DMInterpolateSolution_Plex));
4876c506a872SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetOverlap_C", DMPlexGetOverlap_Plex));
4877c506a872SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexSetOverlap_C", DMPlexSetOverlap_Plex));
4878d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetUseCeed_C", DMPlexGetUseCeed_Plex));
4879d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexSetUseCeed_C", DMPlexSetUseCeed_Plex));
48803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4881552f7358SJed Brown }
4882552f7358SJed Brown 
4883d71ae5a4SJacob Faibussowitsch PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm)
4884d71ae5a4SJacob Faibussowitsch {
488563a16f15SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *)dm->data;
48861fca310dSJames Wright   const PetscSF *face_sfs;
48871fca310dSJames Wright   PetscInt       num_face_sfs;
488863a16f15SMatthew G. Knepley 
488963a16f15SMatthew G. Knepley   PetscFunctionBegin;
489063a16f15SMatthew G. Knepley   mesh->refct++;
489163a16f15SMatthew G. Knepley   (*newdm)->data = mesh;
48921fca310dSJames Wright   PetscCall(DMPlexGetIsoperiodicFaceSF(dm, &num_face_sfs, &face_sfs));
48931fca310dSJames Wright   PetscCall(DMPlexSetIsoperiodicFaceSF(*newdm, num_face_sfs, (PetscSF *)face_sfs));
48949566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)*newdm, DMPLEX));
48959566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(*newdm));
48963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
489763a16f15SMatthew G. Knepley }
489863a16f15SMatthew G. Knepley 
48998818961aSMatthew G Knepley /*MC
4900a1cb98faSBarry Smith   DMPLEX = "plex" - A `DM` object that encapsulates an unstructured mesh, or CW Complex, which can be expressed using a Hasse Diagram.
490120f4b53cSBarry Smith                     In the local representation, `Vec`s contain all unknowns in the interior and shared boundary. This is
49028818961aSMatthew G Knepley                     specified by a PetscSection object. Ownership in the global representation is determined by
4903a1cb98faSBarry Smith                     ownership of the underlying `DMPLEX` points. This is specified by another `PetscSection` object.
49048818961aSMatthew G Knepley 
4905e5893cccSMatthew G. Knepley   Options Database Keys:
4906250712c9SMatthew G. Knepley + -dm_refine_pre                     - Refine mesh before distribution
4907250712c9SMatthew G. Knepley + -dm_refine_uniform_pre             - Choose uniform or generator-based refinement
4908250712c9SMatthew G. Knepley + -dm_refine_volume_limit_pre        - Cell volume limit after pre-refinement using generator
4909250712c9SMatthew G. Knepley . -dm_distribute                     - Distribute mesh across processes
4910250712c9SMatthew G. Knepley . -dm_distribute_overlap             - Number of cells to overlap for distribution
4911250712c9SMatthew G. Knepley . -dm_refine                         - Refine mesh after distribution
4912c3db174cSMatthew G. Knepley . -dm_localize <bool>                - Whether to localize coordinates for periodic meshes
4913c3db174cSMatthew G. Knepley . -dm_sparse_localize <bool>         - Whether to only localize cells on the periodic boundary
4914250712c9SMatthew G. Knepley . -dm_plex_hash_location             - Use grid hashing for point location
4915ddce0771SMatthew G. Knepley . -dm_plex_hash_box_faces <n,m,p>    - The number of divisions in each direction of the grid hash
4916f12cf164SMatthew G. Knepley . -dm_plex_partition_balance         - Attempt to evenly divide points on partition boundary between processes
4917f12cf164SMatthew G. Knepley . -dm_plex_remesh_bd                 - Allow changes to the boundary on remeshing
4918d5b43468SJose E. Roman . -dm_plex_max_projection_height     - Maximum mesh point height used to project locally
4919f12cf164SMatthew G. Knepley . -dm_plex_regular_refinement        - Use special nested projection algorithm for regular refinement
4920d02c7345SMatthew G. Knepley . -dm_plex_reorder_section           - Use specialized blocking if available
4921aaa8cc7dSPierre Jolivet . -dm_plex_check_all                 - Perform all checks below
4922f12cf164SMatthew G. Knepley . -dm_plex_check_symmetry            - Check that the adjacency information in the mesh is symmetric
4923f12cf164SMatthew G. Knepley . -dm_plex_check_skeleton <celltype> - Check that each cell has the correct number of vertices
4924f12cf164SMatthew 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
4925f12cf164SMatthew G. Knepley . -dm_plex_check_geometry            - Check that cells have positive volume
4926f12cf164SMatthew G. Knepley . -dm_view :mesh.tex:ascii_latex     - View the mesh in LaTeX/TikZ
4927e5893cccSMatthew G. Knepley . -dm_plex_view_scale <num>          - Scale the TikZ
49285962854dSMatthew G. Knepley . -dm_plex_print_fem <num>           - View FEM assembly information, such as element vectors and matrices
49295962854dSMatthew G. Knepley - -dm_plex_print_fvm <num>           - View FVM assembly information, such as flux updates
4930e5893cccSMatthew G. Knepley 
49318818961aSMatthew G Knepley   Level: intermediate
49328818961aSMatthew G Knepley 
49331cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMType`, `DMPlexCreate()`, `DMCreate()`, `DMSetType()`, `PetscSection`
49348818961aSMatthew G Knepley M*/
49358818961aSMatthew G Knepley 
4936d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode DMCreate_Plex(DM dm)
4937d71ae5a4SJacob Faibussowitsch {
4938552f7358SJed Brown   DM_Plex *mesh;
4939412e9a14SMatthew G. Knepley   PetscInt unit;
4940552f7358SJed Brown 
4941552f7358SJed Brown   PetscFunctionBegin;
4942f39ec787SMatthew G. Knepley   PetscCall(PetscCitationsRegister(PlexCitation, &Plexcite));
4943552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
49444dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&mesh));
4945adc21957SMatthew G. Knepley   dm->reorderSection = DM_REORDER_DEFAULT_NOTSET;
4946552f7358SJed Brown   dm->data           = mesh;
4947552f7358SJed Brown 
4948552f7358SJed Brown   mesh->refct = 1;
49499566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->coneSection));
49509566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->supportSection));
4951552f7358SJed Brown   mesh->refinementUniform      = PETSC_TRUE;
4952552f7358SJed Brown   mesh->refinementLimit        = -1.0;
4953e600fa54SMatthew G. Knepley   mesh->distDefault            = PETSC_TRUE;
4954adc21957SMatthew G. Knepley   mesh->reorderDefault         = DM_REORDER_DEFAULT_NOTSET;
49551d1f2f2aSksagiyam   mesh->distributionName       = NULL;
49567d0f5628SVaclav Hapla   mesh->interpolated           = DMPLEX_INTERPOLATED_INVALID;
49577d0f5628SVaclav Hapla   mesh->interpolatedCollective = DMPLEX_INTERPOLATED_INVALID;
4958552f7358SJed Brown 
49599566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerCreate(PetscObjectComm((PetscObject)dm), &mesh->partitioner));
49602e62ab5aSMatthew G. Knepley   mesh->remeshBd = PETSC_FALSE;
4961d9deefdfSMatthew G. Knepley 
49628865f1eaSKarl Rupp   for (unit = 0; unit < NUM_PETSC_UNITS; ++unit) mesh->scale[unit] = 1.0;
4963552f7358SJed Brown 
4964df0420ecSMatthew G. Knepley   mesh->depthState    = -1;
4965ba2698f1SMatthew G. Knepley   mesh->celltypeState = -1;
49666113b454SMatthew G. Knepley   mesh->printTol      = 1.0e-10;
4967552f7358SJed Brown 
49689566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(dm));
49693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4970552f7358SJed Brown }
4971552f7358SJed Brown 
4972552f7358SJed Brown /*@
4973a1cb98faSBarry Smith   DMPlexCreate - Creates a `DMPLEX` object, which encapsulates an unstructured mesh, or CW complex, which can be expressed using a Hasse Diagram.
4974552f7358SJed Brown 
4975d083f849SBarry Smith   Collective
4976552f7358SJed Brown 
4977552f7358SJed Brown   Input Parameter:
4978a1cb98faSBarry Smith . comm - The communicator for the `DMPLEX` object
4979552f7358SJed Brown 
4980552f7358SJed Brown   Output Parameter:
4981a1cb98faSBarry Smith . mesh - The `DMPLEX` object
4982552f7358SJed Brown 
4983552f7358SJed Brown   Level: beginner
4984552f7358SJed Brown 
498542747ad1SJacob Faibussowitsch .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMType`, `DMCreate()`, `DMSetType()`
4986552f7358SJed Brown @*/
4987d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreate(MPI_Comm comm, DM *mesh)
4988d71ae5a4SJacob Faibussowitsch {
4989552f7358SJed Brown   PetscFunctionBegin;
49904f572ea9SToby Isaac   PetscAssertPointer(mesh, 2);
49919566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, mesh));
49929566063dSJacob Faibussowitsch   PetscCall(DMSetType(*mesh, DMPLEX));
49933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4994552f7358SJed Brown }
4995552f7358SJed Brown 
4996b09969d6SVaclav Hapla /*@C
4997a1cb98faSBarry Smith   DMPlexBuildFromCellListParallel - Build distributed `DMPLEX` topology from a list of vertices for each cell (common mesh generator output)
4998a1cb98faSBarry Smith 
499920f4b53cSBarry Smith   Collective; No Fortran Support
5000b09969d6SVaclav Hapla 
5001b09969d6SVaclav Hapla   Input Parameters:
5002a1cb98faSBarry Smith + dm          - The `DM`
5003b09969d6SVaclav Hapla . numCells    - The number of cells owned by this process
5004a1cb98faSBarry Smith . numVertices - The number of vertices to be owned by this process, or `PETSC_DECIDE`
5005a1cb98faSBarry Smith . NVertices   - The global number of vertices, or `PETSC_DETERMINE`
5006b09969d6SVaclav Hapla . numCorners  - The number of vertices for each cell
50075e488331SVaclav Hapla - cells       - An array of numCells*numCorners numbers, the global vertex numbers for each cell
5008b09969d6SVaclav Hapla 
5009be8c289dSNicolas Barral   Output Parameters:
5010a1cb98faSBarry Smith + vertexSF         - (Optional) `PetscSF` describing complete vertex ownership
5011be8c289dSNicolas Barral - verticesAdjSaved - (Optional) vertex adjacency array
5012b09969d6SVaclav Hapla 
5013b09969d6SVaclav Hapla   Level: advanced
5014b09969d6SVaclav Hapla 
5015a1cb98faSBarry Smith   Notes:
5016a1cb98faSBarry Smith   Two triangles sharing a face
5017a1cb98faSBarry Smith .vb
5018a1cb98faSBarry Smith 
5019a1cb98faSBarry Smith         2
5020a1cb98faSBarry Smith       / | \
5021a1cb98faSBarry Smith      /  |  \
5022a1cb98faSBarry Smith     /   |   \
5023a1cb98faSBarry Smith    0  0 | 1  3
5024a1cb98faSBarry Smith     \   |   /
5025a1cb98faSBarry Smith      \  |  /
5026a1cb98faSBarry Smith       \ | /
5027a1cb98faSBarry Smith         1
5028a1cb98faSBarry Smith .ve
5029a1cb98faSBarry Smith   would have input
5030a1cb98faSBarry Smith .vb
5031a1cb98faSBarry Smith   numCells = 2, numVertices = 4
5032a1cb98faSBarry Smith   cells = [0 1 2  1 3 2]
5033a1cb98faSBarry Smith .ve
5034a1cb98faSBarry Smith   which would result in the `DMPLEX`
5035a1cb98faSBarry Smith .vb
5036a1cb98faSBarry Smith 
5037a1cb98faSBarry Smith         4
5038a1cb98faSBarry Smith       / | \
5039a1cb98faSBarry Smith      /  |  \
5040a1cb98faSBarry Smith     /   |   \
5041a1cb98faSBarry Smith    2  0 | 1  5
5042a1cb98faSBarry Smith     \   |   /
5043a1cb98faSBarry Smith      \  |  /
5044a1cb98faSBarry Smith       \ | /
5045a1cb98faSBarry Smith         3
5046a1cb98faSBarry Smith .ve
5047a1cb98faSBarry Smith 
5048a1cb98faSBarry Smith   Vertices are implicitly numbered consecutively 0,...,NVertices.
5049a1cb98faSBarry Smith   Each rank owns a chunk of numVertices consecutive vertices.
5050a1cb98faSBarry Smith   If numVertices is `PETSC_DECIDE`, PETSc will distribute them as evenly as possible using PetscLayout.
5051a1cb98faSBarry Smith   If NVertices is `PETSC_DETERMINE` and numVertices is PETSC_DECIDE, NVertices is computed by PETSc as the maximum vertex index in cells + 1.
5052a1cb98faSBarry Smith   If only NVertices is `PETSC_DETERMINE`, it is computed as the sum of numVertices over all ranks.
5053a1cb98faSBarry Smith 
5054a1cb98faSBarry Smith   The cell distribution is arbitrary non-overlapping, independent of the vertex distribution.
5055a1cb98faSBarry Smith 
50561cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildFromCellList()`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildCoordinatesFromCellListParallel()`,
5057a1cb98faSBarry Smith           `PetscSF`
5058b09969d6SVaclav Hapla @*/
5059d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildFromCellListParallel(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt NVertices, PetscInt numCorners, const PetscInt cells[], PetscSF *vertexSF, PetscInt **verticesAdjSaved)
5060d71ae5a4SJacob Faibussowitsch {
50612464107aSksagiyam   PetscSF     sfPoint;
50622464107aSksagiyam   PetscLayout layout;
506382fb893eSVaclav Hapla   PetscInt    numVerticesAdj, *verticesAdj, *cones, c, p;
5064a47d0d45SMatthew G. Knepley 
5065a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
506625b6865aSVaclav Hapla   PetscValidLogicalCollectiveInt(dm, NVertices, 4);
50679566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
506825b6865aSVaclav Hapla   /* Get/check global number of vertices */
506925b6865aSVaclav Hapla   {
507025b6865aSVaclav Hapla     PetscInt       NVerticesInCells, i;
507125b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
507225b6865aSVaclav Hapla 
507325b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
507425b6865aSVaclav Hapla     NVerticesInCells = PETSC_MIN_INT;
50759371c9d4SSatish Balay     for (i = 0; i < len; i++)
50769371c9d4SSatish Balay       if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
507725b6865aSVaclav Hapla     ++NVerticesInCells;
5078712fec58SPierre Jolivet     PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &NVerticesInCells, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
507925b6865aSVaclav Hapla 
508025b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE && NVertices == PETSC_DECIDE) NVertices = NVerticesInCells;
50819371c9d4SSatish Balay     else
50829371c9d4SSatish 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);
508325b6865aSVaclav Hapla   }
50849079aca8SVaclav Hapla   /* Count locally unique vertices */
50859079aca8SVaclav Hapla   {
50869079aca8SVaclav Hapla     PetscHSetI vhash;
50879079aca8SVaclav Hapla     PetscInt   off = 0;
50889079aca8SVaclav Hapla 
50899566063dSJacob Faibussowitsch     PetscCall(PetscHSetICreate(&vhash));
5090a47d0d45SMatthew G. Knepley     for (c = 0; c < numCells; ++c) {
509148a46eb9SPierre Jolivet       for (p = 0; p < numCorners; ++p) PetscCall(PetscHSetIAdd(vhash, cells[c * numCorners + p]));
5092a47d0d45SMatthew G. Knepley     }
50939566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetSize(vhash, &numVerticesAdj));
50949566063dSJacob Faibussowitsch     if (!verticesAdjSaved) PetscCall(PetscMalloc1(numVerticesAdj, &verticesAdj));
5095ad540459SPierre Jolivet     else verticesAdj = *verticesAdjSaved;
50969566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetElems(vhash, &off, verticesAdj));
50979566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&vhash));
509863a3b9bcSJacob Faibussowitsch     PetscCheck(off == numVerticesAdj, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid number of local vertices %" PetscInt_FMT " should be %" PetscInt_FMT, off, numVerticesAdj);
5099a47d0d45SMatthew G. Knepley   }
51009566063dSJacob Faibussowitsch   PetscCall(PetscSortInt(numVerticesAdj, verticesAdj));
5101a47d0d45SMatthew G. Knepley   /* Create cones */
51029566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, 0, numCells + numVerticesAdj));
51039566063dSJacob Faibussowitsch   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, numCorners));
51049566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm));
51059566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCones(dm, &cones));
5106a47d0d45SMatthew G. Knepley   for (c = 0; c < numCells; ++c) {
5107a47d0d45SMatthew G. Knepley     for (p = 0; p < numCorners; ++p) {
5108a47d0d45SMatthew G. Knepley       const PetscInt gv = cells[c * numCorners + p];
5109a47d0d45SMatthew G. Knepley       PetscInt       lv;
5110a47d0d45SMatthew G. Knepley 
51119079aca8SVaclav Hapla       /* Positions within verticesAdj form 0-based local vertex numbering;
51129079aca8SVaclav Hapla          we need to shift it by numCells to get correct DAG points (cells go first) */
51139566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv));
511463a3b9bcSJacob Faibussowitsch       PetscCheck(lv >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not find global vertex %" PetscInt_FMT " in local connectivity", gv);
5115961cfab0SVaclav Hapla       cones[c * numCorners + p] = lv + numCells;
5116a47d0d45SMatthew G. Knepley     }
5117a47d0d45SMatthew G. Knepley   }
51182464107aSksagiyam   /* Build point sf */
51199566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(PetscObjectComm((PetscObject)dm), &layout));
51209566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetSize(layout, NVertices));
51219566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, numVertices));
51229566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
51239566063dSJacob Faibussowitsch   PetscCall(PetscSFCreateByMatchingIndices(layout, numVerticesAdj, verticesAdj, NULL, numCells, numVerticesAdj, verticesAdj, NULL, numCells, vertexSF, &sfPoint));
51249566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
51259566063dSJacob Faibussowitsch   if (!verticesAdjSaved) PetscCall(PetscFree(verticesAdj));
51269566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)sfPoint, "point SF"));
51272464107aSksagiyam   if (dm->sf) {
51282464107aSksagiyam     const char *prefix;
51292464107aSksagiyam 
51309566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm->sf, &prefix));
51319566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)sfPoint, prefix));
51322464107aSksagiyam   }
51339566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dm, sfPoint));
51349566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&sfPoint));
5135f4f49eeaSPierre Jolivet   if (vertexSF) PetscCall(PetscObjectSetName((PetscObject)*vertexSF, "Vertex Ownership SF"));
5136a47d0d45SMatthew G. Knepley   /* Fill in the rest of the topology structure */
51379566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
51389566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
51399566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
51403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5141a47d0d45SMatthew G. Knepley }
5142a47d0d45SMatthew G. Knepley 
5143cc4c1da9SBarry Smith /*@
5144a1cb98faSBarry Smith   DMPlexBuildCoordinatesFromCellListParallel - Build `DM` coordinates from a list of coordinates for each owned vertex (common mesh generator output)
5145a1cb98faSBarry Smith 
514620f4b53cSBarry Smith   Collective; No Fortran Support
5147b09969d6SVaclav Hapla 
5148b09969d6SVaclav Hapla   Input Parameters:
5149a1cb98faSBarry Smith + dm           - The `DM`
5150b09969d6SVaclav Hapla . spaceDim     - The spatial dimension used for coordinates
5151a1cb98faSBarry Smith . sfVert       - `PetscSF` describing complete vertex ownership
5152b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
5153b09969d6SVaclav Hapla 
5154b09969d6SVaclav Hapla   Level: advanced
5155b09969d6SVaclav Hapla 
51561cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildFromCellListParallel()`
5157b09969d6SVaclav Hapla @*/
5158d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildCoordinatesFromCellListParallel(DM dm, PetscInt spaceDim, PetscSF sfVert, const PetscReal vertexCoords[])
5159d71ae5a4SJacob Faibussowitsch {
5160a47d0d45SMatthew G. Knepley   PetscSection coordSection;
5161a47d0d45SMatthew G. Knepley   Vec          coordinates;
5162a47d0d45SMatthew G. Knepley   PetscScalar *coords;
51631edcf0b2SVaclav Hapla   PetscInt     numVertices, numVerticesAdj, coordSize, v, vStart, vEnd;
5164a47d0d45SMatthew G. Knepley 
5165a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
51669566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
51679566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
51681dca8a05SBarry Smith   PetscCheck(vStart >= 0 && vEnd >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
51699566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, spaceDim));
51709566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfVert, &numVertices, &numVerticesAdj, NULL, NULL));
51711dca8a05SBarry 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);
51729566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
51739566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
51749566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, spaceDim));
51759566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, vStart, vEnd));
51761edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
51779566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, spaceDim));
51789566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, spaceDim));
5179a47d0d45SMatthew G. Knepley   }
51809566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
51819566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
51829566063dSJacob Faibussowitsch   PetscCall(VecCreate(PetscObjectComm((PetscObject)dm), &coordinates));
51839566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, spaceDim));
51849566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
51859566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
51869566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
51879566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
5188a47d0d45SMatthew G. Knepley   {
5189a47d0d45SMatthew G. Knepley     MPI_Datatype coordtype;
5190a47d0d45SMatthew G. Knepley 
5191a47d0d45SMatthew G. Knepley     /* Need a temp buffer for coords if we have complex/single */
51929566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_contiguous(spaceDim, MPIU_SCALAR, &coordtype));
51939566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_commit(&coordtype));
519421016a8bSBarry Smith #if defined(PETSC_USE_COMPLEX)
519521016a8bSBarry Smith     {
519621016a8bSBarry Smith       PetscScalar *svertexCoords;
519721016a8bSBarry Smith       PetscInt     i;
51989566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numVertices * spaceDim, &svertexCoords));
51993612f820SVaclav Hapla       for (i = 0; i < numVertices * spaceDim; i++) svertexCoords[i] = vertexCoords[i];
52009566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(sfVert, coordtype, svertexCoords, coords, MPI_REPLACE));
52019566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(sfVert, coordtype, svertexCoords, coords, MPI_REPLACE));
52029566063dSJacob Faibussowitsch       PetscCall(PetscFree(svertexCoords));
520321016a8bSBarry Smith     }
520421016a8bSBarry Smith #else
52059566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sfVert, coordtype, vertexCoords, coords, MPI_REPLACE));
52069566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sfVert, coordtype, vertexCoords, coords, MPI_REPLACE));
520721016a8bSBarry Smith #endif
52089566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_free(&coordtype));
5209a47d0d45SMatthew G. Knepley   }
52109566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
52119566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
52129566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
52139566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
52143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5215a47d0d45SMatthew G. Knepley }
5216a47d0d45SMatthew G. Knepley 
5217c3edce3dSSatish Balay /*@
5218a1cb98faSBarry Smith   DMPlexCreateFromCellListParallelPetsc - Create distributed `DMPLEX` from a list of vertices for each cell (common mesh generator output)
5219a1cb98faSBarry Smith 
5220a1cb98faSBarry Smith   Collective
5221a47d0d45SMatthew G. Knepley 
5222a47d0d45SMatthew G. Knepley   Input Parameters:
5223a47d0d45SMatthew G. Knepley + comm         - The communicator
5224a47d0d45SMatthew G. Knepley . dim          - The topological dimension of the mesh
5225a47d0d45SMatthew G. Knepley . numCells     - The number of cells owned by this process
5226a1cb98faSBarry Smith . numVertices  - The number of vertices owned by this process, or `PETSC_DECIDE`
5227a1cb98faSBarry Smith . NVertices    - The global number of vertices, or `PETSC_DECIDE`
5228a47d0d45SMatthew G. Knepley . numCorners   - The number of vertices for each cell
5229a47d0d45SMatthew G. Knepley . interpolate  - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
5230a47d0d45SMatthew G. Knepley . cells        - An array of numCells*numCorners numbers, the global vertex numbers for each cell
5231a47d0d45SMatthew G. Knepley . spaceDim     - The spatial dimension used for coordinates
5232a47d0d45SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
5233a47d0d45SMatthew G. Knepley 
5234d8d19677SJose E. Roman   Output Parameters:
5235a1cb98faSBarry Smith + dm          - The `DM`
5236a1cb98faSBarry Smith . vertexSF    - (Optional) `PetscSF` describing complete vertex ownership
523760225df5SJacob Faibussowitsch - verticesAdj - (Optional) vertex adjacency array
5238a47d0d45SMatthew G. Knepley 
5239b09969d6SVaclav Hapla   Level: intermediate
5240a47d0d45SMatthew G. Knepley 
5241a1cb98faSBarry Smith   Notes:
5242a1cb98faSBarry Smith   This function is just a convenient sequence of `DMCreate()`, `DMSetType()`, `DMSetDimension()`,
5243a1cb98faSBarry Smith   `DMPlexBuildFromCellListParallel()`, `DMPlexInterpolate()`, `DMPlexBuildCoordinatesFromCellListParallel()`
5244a1cb98faSBarry Smith 
5245a1cb98faSBarry Smith   See `DMPlexBuildFromCellListParallel()` for an example and details about the topology-related parameters.
5246a1cb98faSBarry Smith 
5247a1cb98faSBarry Smith   See `DMPlexBuildCoordinatesFromCellListParallel()` for details about the geometry-related parameters.
5248a1cb98faSBarry Smith 
52491cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()`
5250a47d0d45SMatthew G. Knepley @*/
5251d71ae5a4SJacob 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)
5252d71ae5a4SJacob Faibussowitsch {
5253a47d0d45SMatthew G. Knepley   PetscSF sfVert;
5254a47d0d45SMatthew G. Knepley 
5255a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
52569566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
52579566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
5258a47d0d45SMatthew G. Knepley   PetscValidLogicalCollectiveInt(*dm, dim, 2);
5259064a246eSJacob Faibussowitsch   PetscValidLogicalCollectiveInt(*dm, spaceDim, 9);
52609566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
52619566063dSJacob Faibussowitsch   PetscCall(DMPlexBuildFromCellListParallel(*dm, numCells, numVertices, NVertices, numCorners, cells, &sfVert, verticesAdj));
5262a47d0d45SMatthew G. Knepley   if (interpolate) {
52635fd9971aSMatthew G. Knepley     DM idm;
5264a47d0d45SMatthew G. Knepley 
52659566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
52669566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
5267a47d0d45SMatthew G. Knepley     *dm = idm;
5268a47d0d45SMatthew G. Knepley   }
52699566063dSJacob Faibussowitsch   PetscCall(DMPlexBuildCoordinatesFromCellListParallel(*dm, spaceDim, sfVert, vertexCoords));
527018d54ad4SMichael Lange   if (vertexSF) *vertexSF = sfVert;
52719566063dSJacob Faibussowitsch   else PetscCall(PetscSFDestroy(&sfVert));
52723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5273a47d0d45SMatthew G. Knepley }
5274a47d0d45SMatthew G. Knepley 
5275cc4c1da9SBarry Smith /*@
5276a1cb98faSBarry Smith   DMPlexBuildFromCellList - Build `DMPLEX` topology from a list of vertices for each cell (common mesh generator output)
5277a1cb98faSBarry Smith 
527820f4b53cSBarry Smith   Collective; No Fortran Support
52799298eaa6SMatthew G Knepley 
52809298eaa6SMatthew G Knepley   Input Parameters:
5281a1cb98faSBarry Smith + dm          - The `DM`
5282b09969d6SVaclav Hapla . numCells    - The number of cells owned by this process
5283a1cb98faSBarry Smith . numVertices - The number of vertices owned by this process, or `PETSC_DETERMINE`
52849298eaa6SMatthew G Knepley . numCorners  - The number of vertices for each cell
5285a3b724e8SBarry Smith - cells       - An array of `numCells` x `numCorners` numbers, the global vertex numbers for each cell
52869298eaa6SMatthew G Knepley 
5287b09969d6SVaclav Hapla   Level: advanced
52889298eaa6SMatthew G Knepley 
5289b09969d6SVaclav Hapla   Notes:
5290b09969d6SVaclav Hapla   Two triangles sharing a face
5291a1cb98faSBarry Smith .vb
52929298eaa6SMatthew G Knepley 
5293a1cb98faSBarry Smith         2
5294a1cb98faSBarry Smith       / | \
5295a1cb98faSBarry Smith      /  |  \
5296a1cb98faSBarry Smith     /   |   \
5297a1cb98faSBarry Smith    0  0 | 1  3
5298a1cb98faSBarry Smith     \   |   /
5299a1cb98faSBarry Smith      \  |  /
5300a1cb98faSBarry Smith       \ | /
5301a1cb98faSBarry Smith         1
5302a1cb98faSBarry Smith .ve
5303a1cb98faSBarry Smith   would have input
5304a1cb98faSBarry Smith .vb
5305a1cb98faSBarry Smith   numCells = 2, numVertices = 4
5306a1cb98faSBarry Smith   cells = [0 1 2  1 3 2]
5307a1cb98faSBarry Smith .ve
5308a1cb98faSBarry Smith   which would result in the `DMPLEX`
5309a1cb98faSBarry Smith .vb
5310a1cb98faSBarry Smith 
5311a1cb98faSBarry Smith         4
5312a1cb98faSBarry Smith       / | \
5313a1cb98faSBarry Smith      /  |  \
5314a1cb98faSBarry Smith     /   |   \
5315a1cb98faSBarry Smith    2  0 | 1  5
5316a1cb98faSBarry Smith     \   |   /
5317a1cb98faSBarry Smith      \  |  /
5318a1cb98faSBarry Smith       \ | /
5319a1cb98faSBarry Smith         3
5320a1cb98faSBarry Smith .ve
5321a1cb98faSBarry Smith 
5322a1cb98faSBarry Smith   If numVertices is `PETSC_DETERMINE`, it is computed by PETSc as the maximum vertex index in cells + 1.
532325b6865aSVaclav Hapla 
53241cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromCellListPetsc()`
5325b09969d6SVaclav Hapla @*/
5326d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildFromCellList(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const PetscInt cells[])
5327d71ae5a4SJacob Faibussowitsch {
5328961cfab0SVaclav Hapla   PetscInt *cones, c, p, dim;
5329b09969d6SVaclav Hapla 
5330b09969d6SVaclav Hapla   PetscFunctionBegin;
53319566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
53329566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
533325b6865aSVaclav Hapla   /* Get/check global number of vertices */
533425b6865aSVaclav Hapla   {
533525b6865aSVaclav Hapla     PetscInt       NVerticesInCells, i;
533625b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
533725b6865aSVaclav Hapla 
533825b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
533925b6865aSVaclav Hapla     NVerticesInCells = PETSC_MIN_INT;
53409371c9d4SSatish Balay     for (i = 0; i < len; i++)
53419371c9d4SSatish Balay       if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
534225b6865aSVaclav Hapla     ++NVerticesInCells;
534325b6865aSVaclav Hapla 
534425b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE) numVertices = NVerticesInCells;
53459371c9d4SSatish Balay     else
53469371c9d4SSatish 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);
534725b6865aSVaclav Hapla   }
53489566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
534948a46eb9SPierre Jolivet   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, numCorners));
53509566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm));
53519566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCones(dm, &cones));
5352b09969d6SVaclav Hapla   for (c = 0; c < numCells; ++c) {
5353ad540459SPierre Jolivet     for (p = 0; p < numCorners; ++p) cones[c * numCorners + p] = cells[c * numCorners + p] + numCells;
5354b09969d6SVaclav Hapla   }
53559566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
53569566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
53579566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
53583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5359b09969d6SVaclav Hapla }
5360b09969d6SVaclav Hapla 
5361cc4c1da9SBarry Smith /*@
5362a1cb98faSBarry Smith   DMPlexBuildCoordinatesFromCellList - Build `DM` coordinates from a list of coordinates for each owned vertex (common mesh generator output)
5363a1cb98faSBarry Smith 
5364cc4c1da9SBarry Smith   Collective
5365b09969d6SVaclav Hapla 
5366b09969d6SVaclav Hapla   Input Parameters:
5367a1cb98faSBarry Smith + dm           - The `DM`
5368b09969d6SVaclav Hapla . spaceDim     - The spatial dimension used for coordinates
5369b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
5370b09969d6SVaclav Hapla 
5371b09969d6SVaclav Hapla   Level: advanced
5372b09969d6SVaclav Hapla 
53731cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellList()`
5374b09969d6SVaclav Hapla @*/
5375d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildCoordinatesFromCellList(DM dm, PetscInt spaceDim, const PetscReal vertexCoords[])
5376d71ae5a4SJacob Faibussowitsch {
5377b09969d6SVaclav Hapla   PetscSection coordSection;
5378b09969d6SVaclav Hapla   Vec          coordinates;
5379b09969d6SVaclav Hapla   DM           cdm;
5380b09969d6SVaclav Hapla   PetscScalar *coords;
53811edcf0b2SVaclav Hapla   PetscInt     v, vStart, vEnd, d;
5382b09969d6SVaclav Hapla 
5383b09969d6SVaclav Hapla   PetscFunctionBegin;
53849566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
53859566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
53861dca8a05SBarry Smith   PetscCheck(vStart >= 0 && vEnd >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
53879566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, spaceDim));
53889566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
53899566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
53909566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, spaceDim));
53919566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, vStart, vEnd));
53921edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
53939566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, spaceDim));
53949566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, spaceDim));
5395b09969d6SVaclav Hapla   }
53969566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
5397b09969d6SVaclav Hapla 
53989566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
53999566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(cdm, &coordinates));
54009566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, spaceDim));
54019566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
54029566063dSJacob Faibussowitsch   PetscCall(VecGetArrayWrite(coordinates, &coords));
54031edcf0b2SVaclav Hapla   for (v = 0; v < vEnd - vStart; ++v) {
5404ad540459SPierre Jolivet     for (d = 0; d < spaceDim; ++d) coords[v * spaceDim + d] = vertexCoords[v * spaceDim + d];
5405b09969d6SVaclav Hapla   }
54069566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayWrite(coordinates, &coords));
54079566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
54089566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
54099566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
54103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5411b09969d6SVaclav Hapla }
5412b09969d6SVaclav Hapla 
5413b09969d6SVaclav Hapla /*@
5414a1cb98faSBarry Smith   DMPlexCreateFromCellListPetsc - Create `DMPLEX` from a list of vertices for each cell (common mesh generator output), but only process 0 takes in the input
54153df08285SMatthew G. Knepley 
5416a1cb98faSBarry Smith   Collective
5417b09969d6SVaclav Hapla 
5418b09969d6SVaclav Hapla   Input Parameters:
5419b09969d6SVaclav Hapla + comm         - The communicator
5420b09969d6SVaclav Hapla . dim          - The topological dimension of the mesh
54213df08285SMatthew G. Knepley . numCells     - The number of cells, only on process 0
5422a1cb98faSBarry Smith . numVertices  - The number of vertices owned by this process, or `PETSC_DECIDE`, only on process 0
54233df08285SMatthew G. Knepley . numCorners   - The number of vertices for each cell, only on process 0
5424b09969d6SVaclav Hapla . interpolate  - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
54253df08285SMatthew G. Knepley . cells        - An array of numCells*numCorners numbers, the vertices for each cell, only on process 0
5426b09969d6SVaclav Hapla . spaceDim     - The spatial dimension used for coordinates
54273df08285SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex, only on process 0
5428b09969d6SVaclav Hapla 
5429b09969d6SVaclav Hapla   Output Parameter:
5430a1cb98faSBarry Smith . dm - The `DM`, which only has points on process 0
543125b6865aSVaclav Hapla 
5432b09969d6SVaclav Hapla   Level: intermediate
5433b09969d6SVaclav Hapla 
5434a1cb98faSBarry Smith   Notes:
5435a1cb98faSBarry Smith   This function is just a convenient sequence of `DMCreate()`, `DMSetType()`, `DMSetDimension()`, `DMPlexBuildFromCellList()`,
5436a1cb98faSBarry Smith   `DMPlexInterpolate()`, `DMPlexBuildCoordinatesFromCellList()`
5437a1cb98faSBarry Smith 
5438a1cb98faSBarry Smith   See `DMPlexBuildFromCellList()` for an example and details about the topology-related parameters.
5439a1cb98faSBarry Smith   See `DMPlexBuildCoordinatesFromCellList()` for details about the geometry-related parameters.
5440a1cb98faSBarry Smith   See `DMPlexCreateFromCellListParallelPetsc()` for parallel input
5441a1cb98faSBarry Smith 
54421cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildFromCellList()`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()`
54439298eaa6SMatthew G Knepley @*/
5444d71ae5a4SJacob 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)
5445d71ae5a4SJacob Faibussowitsch {
54463df08285SMatthew G. Knepley   PetscMPIInt rank;
54479298eaa6SMatthew G Knepley 
54489298eaa6SMatthew G Knepley   PetscFunctionBegin;
544928b400f6SJacob 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.");
54509566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
54519566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
54529566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
54539566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
5454c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildFromCellList(*dm, numCells, numVertices, numCorners, cells));
54559566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildFromCellList(*dm, 0, 0, 0, NULL));
54569298eaa6SMatthew G Knepley   if (interpolate) {
54575fd9971aSMatthew G. Knepley     DM idm;
54589298eaa6SMatthew G Knepley 
54599566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
54609566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
54619298eaa6SMatthew G Knepley     *dm = idm;
54629298eaa6SMatthew G Knepley   }
5463c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, vertexCoords));
54649566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, NULL));
54653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
54669298eaa6SMatthew G Knepley }
54679298eaa6SMatthew G Knepley 
5468939f6067SMatthew G. Knepley /*@
546920f4b53cSBarry Smith   DMPlexCreateFromDAG - This takes as input the adjacency-list representation of the Directed Acyclic Graph (Hasse Diagram) encoding a mesh, and produces a `DM`
5470939f6067SMatthew G. Knepley 
5471939f6067SMatthew G. Knepley   Input Parameters:
547220f4b53cSBarry Smith + dm               - The empty `DM` object, usually from `DMCreate()` and `DMSetDimension()`
5473939f6067SMatthew G. Knepley . depth            - The depth of the DAG
547420f4b53cSBarry Smith . numPoints        - Array of size depth + 1 containing the number of points at each `depth`
5475939f6067SMatthew G. Knepley . coneSize         - The cone size of each point
5476939f6067SMatthew G. Knepley . cones            - The concatenation of the cone points for each point, the cone list must be oriented correctly for each point
5477939f6067SMatthew G. Knepley . coneOrientations - The orientation of each cone point
547820f4b53cSBarry Smith - vertexCoords     - An array of `numPoints`[0]*spacedim numbers representing the coordinates of each vertex, with spacedim the value set via `DMSetCoordinateDim()`
5479939f6067SMatthew G. Knepley 
5480939f6067SMatthew G. Knepley   Output Parameter:
548120f4b53cSBarry Smith . dm - The `DM`
548220f4b53cSBarry Smith 
548320f4b53cSBarry Smith   Level: advanced
5484939f6067SMatthew G. Knepley 
5485a1cb98faSBarry Smith   Note:
5486a1cb98faSBarry Smith   Two triangles sharing a face would have input
5487a1cb98faSBarry Smith .vb
5488a1cb98faSBarry Smith   depth = 1, numPoints = [4 2], coneSize = [3 3 0 0 0 0]
5489a1cb98faSBarry Smith   cones = [2 3 4  3 5 4], coneOrientations = [0 0 0  0 0 0]
5490a1cb98faSBarry Smith  vertexCoords = [-1.0 0.0  0.0 -1.0  0.0 1.0  1.0 0.0]
5491a1cb98faSBarry Smith .ve
5492939f6067SMatthew G. Knepley   which would result in the DMPlex
5493a1cb98faSBarry Smith .vb
5494a1cb98faSBarry Smith         4
5495a1cb98faSBarry Smith       / | \
5496a1cb98faSBarry Smith      /  |  \
5497a1cb98faSBarry Smith     /   |   \
5498a1cb98faSBarry Smith    2  0 | 1  5
5499a1cb98faSBarry Smith     \   |   /
5500a1cb98faSBarry Smith      \  |  /
5501a1cb98faSBarry Smith       \ | /
5502a1cb98faSBarry Smith         3
5503a1cb98faSBarry Smith .ve
5504a1cb98faSBarry Smith   Notice that all points are numbered consecutively, unlike `DMPlexCreateFromCellListPetsc()`
5505939f6067SMatthew G. Knepley 
55061cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`
5507939f6067SMatthew G. Knepley @*/
5508d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateFromDAG(DM dm, PetscInt depth, const PetscInt numPoints[], const PetscInt coneSize[], const PetscInt cones[], const PetscInt coneOrientations[], const PetscScalar vertexCoords[])
5509d71ae5a4SJacob Faibussowitsch {
55109298eaa6SMatthew G Knepley   Vec          coordinates;
55119298eaa6SMatthew G Knepley   PetscSection coordSection;
55129298eaa6SMatthew G Knepley   PetscScalar *coords;
5513811e8653SToby Isaac   PetscInt     coordSize, firstVertex = -1, pStart = 0, pEnd = 0, p, v, dim, dimEmbed, d, off;
55149298eaa6SMatthew G Knepley 
55159298eaa6SMatthew G Knepley   PetscFunctionBegin;
55169566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
55179566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dimEmbed));
551863a3b9bcSJacob Faibussowitsch   PetscCheck(dimEmbed >= dim, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Embedding dimension %" PetscInt_FMT " cannot be less than intrinsic dimension %" PetscInt_FMT, dimEmbed, dim);
55199298eaa6SMatthew G Knepley   for (d = 0; d <= depth; ++d) pEnd += numPoints[d];
55209566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, pStart, pEnd));
55219298eaa6SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
55229566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeSize(dm, p, coneSize[p - pStart]));
5523ad540459SPierre Jolivet     if (firstVertex < 0 && !coneSize[p - pStart]) firstVertex = p - pStart;
552497e052ccSToby Isaac   }
55251dca8a05SBarry Smith   PetscCheck(firstVertex >= 0 || !numPoints[0], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Expected %" PetscInt_FMT " vertices but could not find any", numPoints[0]);
55269566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm)); /* Allocate space for cones */
55279298eaa6SMatthew G Knepley   for (p = pStart, off = 0; p < pEnd; off += coneSize[p - pStart], ++p) {
55289566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(dm, p, &cones[off]));
55299566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeOrientation(dm, p, &coneOrientations[off]));
55309298eaa6SMatthew G Knepley   }
55319566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
55329566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
55339298eaa6SMatthew G Knepley   /* Build coordinates */
55349566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
55359566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
55369566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dimEmbed));
55379566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numPoints[0]));
55389298eaa6SMatthew G Knepley   for (v = firstVertex; v < firstVertex + numPoints[0]; ++v) {
55399566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, dimEmbed));
55409566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dimEmbed));
55419298eaa6SMatthew G Knepley   }
55429566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
55439566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
55449566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
55459566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
55469566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
55479566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, dimEmbed));
55489566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
55499318fe57SMatthew G. Knepley   if (vertexCoords) {
55509566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
55519298eaa6SMatthew G Knepley     for (v = 0; v < numPoints[0]; ++v) {
55529298eaa6SMatthew G Knepley       PetscInt off;
55539298eaa6SMatthew G Knepley 
55549566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, v + firstVertex, &off));
5555ad540459SPierre Jolivet       for (d = 0; d < dimEmbed; ++d) coords[off + d] = vertexCoords[v * dimEmbed + d];
55569298eaa6SMatthew G Knepley     }
55579318fe57SMatthew G. Knepley   }
55589566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
55599566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
55609566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
55613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
55629298eaa6SMatthew G Knepley }
55638415267dSToby Isaac 
5564a4e35b19SJacob Faibussowitsch /*
5565a1cb98faSBarry Smith   DMPlexCreateCellVertexFromFile - Create a `DMPLEX` mesh from a simple cell-vertex file.
5566a1cb98faSBarry Smith 
5567a1cb98faSBarry Smith   Collective
55688ca92349SMatthew G. Knepley 
55698ca92349SMatthew G. Knepley + comm        - The MPI communicator
55708ca92349SMatthew G. Knepley . filename    - Name of the .dat file
55718ca92349SMatthew G. Knepley - interpolate - Create faces and edges in the mesh
55728ca92349SMatthew G. Knepley 
55738ca92349SMatthew G. Knepley   Output Parameter:
5574a1cb98faSBarry Smith . dm  - The `DM` object representing the mesh
55758ca92349SMatthew G. Knepley 
55768ca92349SMatthew G. Knepley   Level: beginner
55778ca92349SMatthew G. Knepley 
5578a1cb98faSBarry Smith   Note:
5579a1cb98faSBarry Smith   The format is the simplest possible:
5580a1cb98faSBarry Smith .vb
5581d0812dedSMatthew G. Knepley   dim Ne Nv Nc Nl
5582d0812dedSMatthew G. Knepley   v_1 v_2 ... v_Nc
5583d0812dedSMatthew G. Knepley   ...
5584d0812dedSMatthew G. Knepley   x y z marker_1 ... marker_Nl
5585a1cb98faSBarry Smith .ve
5586a1cb98faSBarry Smith 
5587a1cb98faSBarry Smith   Developer Note:
5588a1cb98faSBarry Smith   Should use a `PetscViewer` not a filename
5589a1cb98faSBarry Smith 
55906afe31f6SMartin Diehl .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromFile()`, `DMPlexCreateGmsh()`, `DMPlexCreate()`
5591a4e35b19SJacob Faibussowitsch */
5592ff6a9541SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCellVertexFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
5593d71ae5a4SJacob Faibussowitsch {
55948ca92349SMatthew G. Knepley   DMLabel      marker;
55958ca92349SMatthew G. Knepley   PetscViewer  viewer;
55968ca92349SMatthew G. Knepley   Vec          coordinates;
55978ca92349SMatthew G. Knepley   PetscSection coordSection;
55988ca92349SMatthew G. Knepley   PetscScalar *coords;
55998ca92349SMatthew G. Knepley   char         line[PETSC_MAX_PATH_LEN];
5600d0812dedSMatthew G. Knepley   PetscInt     cdim, coordSize, v, c, d;
56018ca92349SMatthew G. Knepley   PetscMPIInt  rank;
5602d0812dedSMatthew G. Knepley   int          snum, dim, Nv, Nc, Ncn, Nl;
56038ca92349SMatthew G. Knepley 
56048ca92349SMatthew G. Knepley   PetscFunctionBegin;
56059566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
56069566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, &viewer));
56079566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(viewer, PETSCVIEWERASCII));
56089566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetMode(viewer, FILE_MODE_READ));
56099566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetName(viewer, filename));
5610dd400576SPatrick Sanan   if (rank == 0) {
5611d0812dedSMatthew G. Knepley     PetscCall(PetscViewerRead(viewer, line, 5, NULL, PETSC_STRING));
5612d0812dedSMatthew G. Knepley     snum = sscanf(line, "%d %d %d %d %d", &dim, &Nc, &Nv, &Ncn, &Nl);
5613d0812dedSMatthew G. Knepley     PetscCheck(snum == 5, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
561425ce1634SJed Brown   } else {
5615f8d5e320SMatthew G. Knepley     Nc = Nv = Ncn = Nl = 0;
56168ca92349SMatthew G. Knepley   }
5617d0812dedSMatthew G. Knepley   PetscCallMPI(MPI_Bcast(&dim, 1, MPI_INT, 0, comm));
5618d0812dedSMatthew G. Knepley   cdim = (PetscInt)dim;
56199566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
56209566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
56219566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(*dm, 0, Nc + Nv));
5622d0812dedSMatthew G. Knepley   PetscCall(DMSetDimension(*dm, (PetscInt)dim));
56239566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*dm, cdim));
56248ca92349SMatthew G. Knepley   /* Read topology */
5625dd400576SPatrick Sanan   if (rank == 0) {
5626f8d5e320SMatthew G. Knepley     char     format[PETSC_MAX_PATH_LEN];
5627f8d5e320SMatthew G. Knepley     PetscInt cone[8];
56288ca92349SMatthew G. Knepley     int      vbuf[8], v;
56298ca92349SMatthew G. Knepley 
56309371c9d4SSatish Balay     for (c = 0; c < Ncn; ++c) {
56319371c9d4SSatish Balay       format[c * 3 + 0] = '%';
56329371c9d4SSatish Balay       format[c * 3 + 1] = 'd';
56339371c9d4SSatish Balay       format[c * 3 + 2] = ' ';
56349371c9d4SSatish Balay     }
5635f8d5e320SMatthew G. Knepley     format[Ncn * 3 - 1] = '\0';
56369566063dSJacob Faibussowitsch     for (c = 0; c < Nc; ++c) PetscCall(DMPlexSetConeSize(*dm, c, Ncn));
56379566063dSJacob Faibussowitsch     PetscCall(DMSetUp(*dm));
56388ca92349SMatthew G. Knepley     for (c = 0; c < Nc; ++c) {
56399566063dSJacob Faibussowitsch       PetscCall(PetscViewerRead(viewer, line, Ncn, NULL, PETSC_STRING));
5640f8d5e320SMatthew G. Knepley       switch (Ncn) {
5641d71ae5a4SJacob Faibussowitsch       case 2:
5642d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1]);
5643d71ae5a4SJacob Faibussowitsch         break;
5644d71ae5a4SJacob Faibussowitsch       case 3:
5645d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2]);
5646d71ae5a4SJacob Faibussowitsch         break;
5647d71ae5a4SJacob Faibussowitsch       case 4:
5648d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3]);
5649d71ae5a4SJacob Faibussowitsch         break;
5650d71ae5a4SJacob Faibussowitsch       case 6:
5651d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5]);
5652d71ae5a4SJacob Faibussowitsch         break;
5653d71ae5a4SJacob Faibussowitsch       case 8:
5654d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5], &vbuf[6], &vbuf[7]);
5655d71ae5a4SJacob Faibussowitsch         break;
5656d71ae5a4SJacob Faibussowitsch       default:
5657d71ae5a4SJacob Faibussowitsch         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No cell shape with %d vertices", Ncn);
5658f8d5e320SMatthew G. Knepley       }
565908401ef6SPierre Jolivet       PetscCheck(snum == Ncn, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
5660f8d5e320SMatthew G. Knepley       for (v = 0; v < Ncn; ++v) cone[v] = vbuf[v] + Nc;
56618ca92349SMatthew G. Knepley       /* Hexahedra are inverted */
5662f8d5e320SMatthew G. Knepley       if (Ncn == 8) {
56638ca92349SMatthew G. Knepley         PetscInt tmp = cone[1];
56648ca92349SMatthew G. Knepley         cone[1]      = cone[3];
56658ca92349SMatthew G. Knepley         cone[3]      = tmp;
56668ca92349SMatthew G. Knepley       }
56679566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(*dm, c, cone));
56688ca92349SMatthew G. Knepley     }
56698ca92349SMatthew G. Knepley   }
56709566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(*dm));
56719566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(*dm));
56728ca92349SMatthew G. Knepley   /* Read coordinates */
56739566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(*dm, &coordSection));
56749566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
56759566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, cdim));
56769566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, Nc, Nc + Nv));
56778ca92349SMatthew G. Knepley   for (v = Nc; v < Nc + Nv; ++v) {
56789566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, cdim));
56799566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, cdim));
56808ca92349SMatthew G. Knepley   }
56819566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
56829566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
56839566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
56849566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
56859566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
56869566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, cdim));
56879566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
56889566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
5689dd400576SPatrick Sanan   if (rank == 0) {
5690f8d5e320SMatthew G. Knepley     char   format[PETSC_MAX_PATH_LEN];
56918ca92349SMatthew G. Knepley     double x[3];
5692f8d5e320SMatthew G. Knepley     int    l, val[3];
56938ca92349SMatthew G. Knepley 
5694f8d5e320SMatthew G. Knepley     if (Nl) {
56959371c9d4SSatish Balay       for (l = 0; l < Nl; ++l) {
56969371c9d4SSatish Balay         format[l * 3 + 0] = '%';
56979371c9d4SSatish Balay         format[l * 3 + 1] = 'd';
56989371c9d4SSatish Balay         format[l * 3 + 2] = ' ';
56999371c9d4SSatish Balay       }
5700f8d5e320SMatthew G. Knepley       format[Nl * 3 - 1] = '\0';
57019566063dSJacob Faibussowitsch       PetscCall(DMCreateLabel(*dm, "marker"));
57029566063dSJacob Faibussowitsch       PetscCall(DMGetLabel(*dm, "marker", &marker));
5703f8d5e320SMatthew G. Knepley     }
57048ca92349SMatthew G. Knepley     for (v = 0; v < Nv; ++v) {
57059566063dSJacob Faibussowitsch       PetscCall(PetscViewerRead(viewer, line, 3 + Nl, NULL, PETSC_STRING));
5706f8d5e320SMatthew G. Knepley       snum = sscanf(line, "%lg %lg %lg", &x[0], &x[1], &x[2]);
570708401ef6SPierre Jolivet       PetscCheck(snum == 3, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
5708f8d5e320SMatthew G. Knepley       switch (Nl) {
5709d71ae5a4SJacob Faibussowitsch       case 0:
5710d71ae5a4SJacob Faibussowitsch         snum = 0;
5711d71ae5a4SJacob Faibussowitsch         break;
5712d71ae5a4SJacob Faibussowitsch       case 1:
5713d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &val[0]);
5714d71ae5a4SJacob Faibussowitsch         break;
5715d71ae5a4SJacob Faibussowitsch       case 2:
5716d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &val[0], &val[1]);
5717d71ae5a4SJacob Faibussowitsch         break;
5718d71ae5a4SJacob Faibussowitsch       case 3:
5719d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &val[0], &val[1], &val[2]);
5720d71ae5a4SJacob Faibussowitsch         break;
5721d71ae5a4SJacob Faibussowitsch       default:
5722d71ae5a4SJacob Faibussowitsch         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Request support for %d labels", Nl);
5723f8d5e320SMatthew G. Knepley       }
572408401ef6SPierre Jolivet       PetscCheck(snum == Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
57258ca92349SMatthew G. Knepley       for (d = 0; d < cdim; ++d) coords[v * cdim + d] = x[d];
57269566063dSJacob Faibussowitsch       for (l = 0; l < Nl; ++l) PetscCall(DMLabelSetValue(marker, v + Nc, val[l]));
57278ca92349SMatthew G. Knepley     }
57288ca92349SMatthew G. Knepley   }
57299566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
57309566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(*dm, coordinates));
57319566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
57329566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(&viewer));
57338ca92349SMatthew G. Knepley   if (interpolate) {
57348ca92349SMatthew G. Knepley     DM      idm;
57358ca92349SMatthew G. Knepley     DMLabel bdlabel;
57368ca92349SMatthew G. Knepley 
57379566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
57389566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
57398ca92349SMatthew G. Knepley     *dm = idm;
57408ca92349SMatthew G. Knepley 
5741f8d5e320SMatthew G. Knepley     if (!Nl) {
57429566063dSJacob Faibussowitsch       PetscCall(DMCreateLabel(*dm, "marker"));
57439566063dSJacob Faibussowitsch       PetscCall(DMGetLabel(*dm, "marker", &bdlabel));
57449566063dSJacob Faibussowitsch       PetscCall(DMPlexMarkBoundaryFaces(*dm, PETSC_DETERMINE, bdlabel));
57459566063dSJacob Faibussowitsch       PetscCall(DMPlexLabelComplete(*dm, bdlabel));
57468ca92349SMatthew G. Knepley     }
5747f8d5e320SMatthew G. Knepley   }
57483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
57498ca92349SMatthew G. Knepley }
57508ca92349SMatthew G. Knepley 
5751cc4c1da9SBarry Smith /*@
5752a1cb98faSBarry Smith   DMPlexCreateFromFile - This takes a filename and produces a `DM`
5753a1cb98faSBarry Smith 
5754a1cb98faSBarry Smith   Collective
5755ca522641SMatthew G. Knepley 
5756ca522641SMatthew G. Knepley   Input Parameters:
5757ca522641SMatthew G. Knepley + comm        - The communicator
5758ca522641SMatthew G. Knepley . filename    - A file name
5759a1cb98faSBarry Smith . plexname    - The object name of the resulting `DM`, also used for intra-datafile lookup by some formats
5760ca522641SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
5761ca522641SMatthew G. Knepley 
5762ca522641SMatthew G. Knepley   Output Parameter:
5763a1cb98faSBarry Smith . dm - The `DM`
5764ca522641SMatthew G. Knepley 
5765a1cb98faSBarry Smith   Options Database Key:
5766a1cb98faSBarry Smith . -dm_plex_create_from_hdf5_xdmf - use the `PETSC_VIEWER_HDF5_XDMF` format for reading HDF5
576702ef0d99SVaclav Hapla 
576837fdd005SBarry Smith   Use `-dm_plex_create_ prefix` to pass options to the internal `PetscViewer`, e.g.
5769bca97951SVaclav Hapla $ -dm_plex_create_viewer_hdf5_collective
5770bca97951SVaclav Hapla 
5771ca522641SMatthew G. Knepley   Level: beginner
5772ca522641SMatthew G. Knepley 
5773a1cb98faSBarry Smith   Notes:
5774a1cb98faSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
5775a1cb98faSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
5776a1cb98faSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
5777a1cb98faSBarry Smith   The input parameter name is thus used to name the `DMPLEX` object when `DMPlexCreateFromFile()` internally
5778a1cb98faSBarry Smith   calls `DMLoad()`. Currently, name is ignored for other viewer types and/or formats.
5779a1cb98faSBarry Smith 
57801cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromDAG()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`, `PetscObjectSetName()`, `DMView()`, `DMLoad()`
5781ca522641SMatthew G. Knepley @*/
5782d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateFromFile(MPI_Comm comm, const char filename[], const char plexname[], PetscBool interpolate, DM *dm)
5783d71ae5a4SJacob Faibussowitsch {
5784ef3a5affSJacob Faibussowitsch   const char  extGmsh[]      = ".msh";
5785ef3a5affSJacob Faibussowitsch   const char  extGmsh2[]     = ".msh2";
5786ef3a5affSJacob Faibussowitsch   const char  extGmsh4[]     = ".msh4";
5787ef3a5affSJacob Faibussowitsch   const char  extCGNS[]      = ".cgns";
5788ef3a5affSJacob Faibussowitsch   const char  extExodus[]    = ".exo";
5789ef3a5affSJacob Faibussowitsch   const char  extExodus_e[]  = ".e";
5790ef3a5affSJacob Faibussowitsch   const char  extGenesis[]   = ".gen";
5791ef3a5affSJacob Faibussowitsch   const char  extFluent[]    = ".cas";
5792ef3a5affSJacob Faibussowitsch   const char  extHDF5[]      = ".h5";
57936f2c871aSStefano Zampini   const char  extXDMFHDF5[]  = ".xdmf.h5";
5794ef3a5affSJacob Faibussowitsch   const char  extPLY[]       = ".ply";
5795ef3a5affSJacob Faibussowitsch   const char  extEGADSLite[] = ".egadslite";
5796ef3a5affSJacob Faibussowitsch   const char  extEGADS[]     = ".egads";
5797ef3a5affSJacob Faibussowitsch   const char  extIGES[]      = ".igs";
5798ef3a5affSJacob Faibussowitsch   const char  extSTEP[]      = ".stp";
5799ef3a5affSJacob Faibussowitsch   const char  extCV[]        = ".dat";
5800ca522641SMatthew G. Knepley   size_t      len;
58016afe31f6SMartin Diehl   PetscBool   isGmsh, isGmsh2, isGmsh4, isCGNS, isExodus, isGenesis, isFluent, isHDF5, isPLY, isEGADSLite, isEGADS, isIGES, isSTEP, isCV, isXDMFHDF5;
5802ca522641SMatthew G. Knepley   PetscMPIInt rank;
5803ca522641SMatthew G. Knepley 
5804ca522641SMatthew G. Knepley   PetscFunctionBegin;
58054f572ea9SToby Isaac   PetscAssertPointer(filename, 2);
58064f572ea9SToby Isaac   if (plexname) PetscAssertPointer(plexname, 3);
58074f572ea9SToby Isaac   PetscAssertPointer(dm, 5);
58089566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
58099566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_CreateFromFile, 0, 0, 0, 0));
58109566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
58119566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(filename, &len));
581228b400f6SJacob Faibussowitsch   PetscCheck(len, comm, PETSC_ERR_ARG_WRONG, "Filename must be a valid path");
5813ef3a5affSJacob Faibussowitsch 
58149371c9d4SSatish Balay #define CheckExtension(extension__, is_extension__) \
58159371c9d4SSatish Balay   do { \
5816274aaeaaSJacob Faibussowitsch     PetscAssert(sizeof(extension__), comm, PETSC_ERR_PLIB, "Zero-size extension: %s", extension__); \
5817274aaeaaSJacob Faibussowitsch     /* don't count the null-terminator at the end */ \
5818274aaeaaSJacob Faibussowitsch     const size_t ext_len = sizeof(extension__) - 1; \
5819274aaeaaSJacob Faibussowitsch     if (len < ext_len) { \
5820ef3a5affSJacob Faibussowitsch       is_extension__ = PETSC_FALSE; \
5821ef3a5affSJacob Faibussowitsch     } else { \
5822274aaeaaSJacob Faibussowitsch       PetscCall(PetscStrncmp(filename + len - ext_len, extension__, ext_len, &is_extension__)); \
5823ef3a5affSJacob Faibussowitsch     } \
5824ef3a5affSJacob Faibussowitsch   } while (0)
5825ef3a5affSJacob Faibussowitsch 
5826ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh, isGmsh);
5827ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh2, isGmsh2);
5828ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh4, isGmsh4);
5829ef3a5affSJacob Faibussowitsch   CheckExtension(extCGNS, isCGNS);
5830ef3a5affSJacob Faibussowitsch   CheckExtension(extExodus, isExodus);
5831ef3a5affSJacob Faibussowitsch   if (!isExodus) CheckExtension(extExodus_e, isExodus);
5832ef3a5affSJacob Faibussowitsch   CheckExtension(extGenesis, isGenesis);
5833ef3a5affSJacob Faibussowitsch   CheckExtension(extFluent, isFluent);
5834ef3a5affSJacob Faibussowitsch   CheckExtension(extHDF5, isHDF5);
5835ef3a5affSJacob Faibussowitsch   CheckExtension(extPLY, isPLY);
5836ef3a5affSJacob Faibussowitsch   CheckExtension(extEGADSLite, isEGADSLite);
5837ef3a5affSJacob Faibussowitsch   CheckExtension(extEGADS, isEGADS);
5838ef3a5affSJacob Faibussowitsch   CheckExtension(extIGES, isIGES);
5839ef3a5affSJacob Faibussowitsch   CheckExtension(extSTEP, isSTEP);
5840ef3a5affSJacob Faibussowitsch   CheckExtension(extCV, isCV);
58416f2c871aSStefano Zampini   CheckExtension(extXDMFHDF5, isXDMFHDF5);
5842ef3a5affSJacob Faibussowitsch 
5843ef3a5affSJacob Faibussowitsch #undef CheckExtension
5844ef3a5affSJacob Faibussowitsch 
5845de78e4feSLisandro Dalcin   if (isGmsh || isGmsh2 || isGmsh4) {
58469566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateGmshFromFile(comm, filename, interpolate, dm));
5847ca522641SMatthew G. Knepley   } else if (isCGNS) {
58489566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCGNSFromFile(comm, filename, interpolate, dm));
584990c68965SMatthew G. Knepley   } else if (isExodus || isGenesis) {
58509566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateExodusFromFile(comm, filename, interpolate, dm));
58512f0bd6dcSMichael Lange   } else if (isFluent) {
58529566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFluentFromFile(comm, filename, interpolate, dm));
5853cc2f8f65SMatthew G. Knepley   } else if (isHDF5) {
5854cc2f8f65SMatthew G. Knepley     PetscViewer viewer;
5855cc2f8f65SMatthew G. Knepley 
585643b242b4SVaclav 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 */
58576f2c871aSStefano Zampini     PetscCall(PetscOptionsGetBool(NULL, NULL, "-dm_plex_create_from_hdf5_xdmf", &isXDMFHDF5, NULL));
58589566063dSJacob Faibussowitsch     PetscCall(PetscViewerCreate(comm, &viewer));
58599566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetType(viewer, PETSCVIEWERHDF5));
58609566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetOptionsPrefix(viewer, "dm_plex_create_"));
58619566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetFromOptions(viewer));
58629566063dSJacob Faibussowitsch     PetscCall(PetscViewerFileSetMode(viewer, FILE_MODE_READ));
58639566063dSJacob Faibussowitsch     PetscCall(PetscViewerFileSetName(viewer, filename));
5864cd7e8a5eSksagiyam 
58659566063dSJacob Faibussowitsch     PetscCall(DMCreate(comm, dm));
5866f4f49eeaSPierre Jolivet     PetscCall(PetscObjectSetName((PetscObject)*dm, plexname));
58679566063dSJacob Faibussowitsch     PetscCall(DMSetType(*dm, DMPLEX));
58686f2c871aSStefano Zampini     if (isXDMFHDF5) PetscCall(PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_XDMF));
58699566063dSJacob Faibussowitsch     PetscCall(DMLoad(*dm, viewer));
58706f2c871aSStefano Zampini     if (isXDMFHDF5) PetscCall(PetscViewerPopFormat(viewer));
58719566063dSJacob Faibussowitsch     PetscCall(PetscViewerDestroy(&viewer));
58725fd9971aSMatthew G. Knepley 
58735fd9971aSMatthew G. Knepley     if (interpolate) {
58745fd9971aSMatthew G. Knepley       DM idm;
58755fd9971aSMatthew G. Knepley 
58769566063dSJacob Faibussowitsch       PetscCall(DMPlexInterpolate(*dm, &idm));
58779566063dSJacob Faibussowitsch       PetscCall(DMDestroy(dm));
58785fd9971aSMatthew G. Knepley       *dm = idm;
58795fd9971aSMatthew G. Knepley     }
5880f2801cd6SMatthew G. Knepley   } else if (isPLY) {
58819566063dSJacob Faibussowitsch     PetscCall(DMPlexCreatePLYFromFile(comm, filename, interpolate, dm));
5882c1cad2e7SMatthew G. Knepley   } else if (isEGADSLite || isEGADS || isIGES || isSTEP) {
58839566063dSJacob Faibussowitsch     if (isEGADSLite) PetscCall(DMPlexCreateEGADSLiteFromFile(comm, filename, dm));
58849566063dSJacob Faibussowitsch     else PetscCall(DMPlexCreateEGADSFromFile(comm, filename, dm));
58857bee2925SMatthew Knepley     if (!interpolate) {
58867bee2925SMatthew Knepley       DM udm;
58877bee2925SMatthew Knepley 
58889566063dSJacob Faibussowitsch       PetscCall(DMPlexUninterpolate(*dm, &udm));
58899566063dSJacob Faibussowitsch       PetscCall(DMDestroy(dm));
58907bee2925SMatthew Knepley       *dm = udm;
58917bee2925SMatthew Knepley     }
58928ca92349SMatthew G. Knepley   } else if (isCV) {
58939566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCellVertexFromFile(comm, filename, interpolate, dm));
589498921bdaSJacob Faibussowitsch   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot load file %s: unrecognized extension", filename);
58959566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(plexname, &len));
5896f4f49eeaSPierre Jolivet   if (len) PetscCall(PetscObjectSetName((PetscObject)*dm, plexname));
58979566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_CreateFromFile, 0, 0, 0, 0));
58983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5899ca522641SMatthew G. Knepley }
590012b8a6daSStefano Zampini 
5901cc4c1da9SBarry Smith /*@
59029f6c5813SMatthew 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.
59039f6c5813SMatthew G. Knepley 
59040528010dSStefano Zampini   Input Parameters:
59050528010dSStefano Zampini + tr     - The `DMPlexTransform`
59060528010dSStefano Zampini - prefix - An options prefix, or NULL
59079f6c5813SMatthew G. Knepley 
59089f6c5813SMatthew G. Knepley   Output Parameter:
59099f6c5813SMatthew G. Knepley . dm - The `DM`
59109f6c5813SMatthew G. Knepley 
59119f6c5813SMatthew G. Knepley   Level: beginner
59129f6c5813SMatthew G. Knepley 
591320f4b53cSBarry Smith   Notes:
591420f4b53cSBarry 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.
591520f4b53cSBarry Smith 
59169f6c5813SMatthew G. Knepley .seealso: `DMPlexCreateFromFile`, `DMPlexCreateFromDAG()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`
59179f6c5813SMatthew G. Knepley @*/
59180528010dSStefano Zampini PetscErrorCode DMPlexCreateEphemeral(DMPlexTransform tr, const char prefix[], DM *dm)
59199f6c5813SMatthew G. Knepley {
59200528010dSStefano Zampini   DM           bdm, bcdm, cdm;
59210528010dSStefano Zampini   Vec          coordinates, coordinatesNew;
59220528010dSStefano Zampini   PetscSection cs;
59230528010dSStefano Zampini   PetscInt     dim, cdim, Nl;
59249f6c5813SMatthew G. Knepley 
59259f6c5813SMatthew G. Knepley   PetscFunctionBegin;
59269f6c5813SMatthew G. Knepley   PetscCall(DMCreate(PetscObjectComm((PetscObject)tr), dm));
59279f6c5813SMatthew G. Knepley   PetscCall(DMSetType(*dm, DMPLEX));
59280528010dSStefano Zampini   ((DM_Plex *)(*dm)->data)->interpolated = DMPLEX_INTERPOLATED_FULL;
59290528010dSStefano Zampini   // Handle coordinates
59300528010dSStefano Zampini   PetscCall(DMPlexTransformGetDM(tr, &bdm));
59310528010dSStefano Zampini   PetscCall(DMGetCoordinateDim(bdm, &cdim));
59320528010dSStefano Zampini   PetscCall(DMSetCoordinateDim(*dm, cdim));
59330528010dSStefano Zampini   PetscCall(DMGetDimension(bdm, &dim));
59340528010dSStefano Zampini   PetscCall(DMSetDimension(*dm, dim));
59350528010dSStefano Zampini   PetscCall(DMGetCoordinateDM(bdm, &bcdm));
59360528010dSStefano Zampini   PetscCall(DMGetCoordinateDM(*dm, &cdm));
59370528010dSStefano Zampini   PetscCall(DMCopyDisc(bcdm, cdm));
59380528010dSStefano Zampini   PetscCall(DMGetLocalSection(cdm, &cs));
59390528010dSStefano Zampini   PetscCall(PetscSectionSetNumFields(cs, 1));
59400528010dSStefano Zampini   PetscCall(PetscSectionSetFieldComponents(cs, 0, cdim));
59410528010dSStefano Zampini   PetscCall(DMGetCoordinatesLocal(bdm, &coordinates));
59420528010dSStefano Zampini   PetscCall(VecDuplicate(coordinates, &coordinatesNew));
59430528010dSStefano Zampini   PetscCall(VecCopy(coordinates, coordinatesNew));
59440528010dSStefano Zampini   PetscCall(DMSetCoordinatesLocal(*dm, coordinatesNew));
59450528010dSStefano Zampini   PetscCall(VecDestroy(&coordinatesNew));
59469f6c5813SMatthew G. Knepley 
59479f6c5813SMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)tr));
59489f6c5813SMatthew G. Knepley   PetscCall(DMPlexTransformDestroy(&((DM_Plex *)(*dm)->data)->tr));
59499f6c5813SMatthew G. Knepley   ((DM_Plex *)(*dm)->data)->tr = tr;
59500528010dSStefano Zampini   PetscCall(DMPlexDistributeSetDefault(*dm, PETSC_FALSE));
59510528010dSStefano Zampini   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)*dm, prefix));
59520528010dSStefano Zampini   PetscCall(DMSetFromOptions(*dm));
59539f6c5813SMatthew G. Knepley 
59549f6c5813SMatthew G. Knepley   PetscCall(DMGetNumLabels(bdm, &Nl));
59559f6c5813SMatthew G. Knepley   for (PetscInt l = 0; l < Nl; ++l) {
59569f6c5813SMatthew G. Knepley     DMLabel     label, labelNew;
59579f6c5813SMatthew G. Knepley     const char *lname;
59589f6c5813SMatthew G. Knepley     PetscBool   isDepth, isCellType;
59599f6c5813SMatthew G. Knepley 
59609f6c5813SMatthew G. Knepley     PetscCall(DMGetLabelName(bdm, l, &lname));
59619f6c5813SMatthew G. Knepley     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
59629f6c5813SMatthew G. Knepley     if (isDepth) continue;
59639f6c5813SMatthew G. Knepley     PetscCall(PetscStrcmp(lname, "celltype", &isCellType));
59649f6c5813SMatthew G. Knepley     if (isCellType) continue;
59659f6c5813SMatthew G. Knepley     PetscCall(DMCreateLabel(*dm, lname));
59669f6c5813SMatthew G. Knepley     PetscCall(DMGetLabel(bdm, lname, &label));
59679f6c5813SMatthew G. Knepley     PetscCall(DMGetLabel(*dm, lname, &labelNew));
59689f6c5813SMatthew G. Knepley     PetscCall(DMLabelSetType(labelNew, DMLABELEPHEMERAL));
59699f6c5813SMatthew G. Knepley     PetscCall(DMLabelEphemeralSetLabel(labelNew, label));
59709f6c5813SMatthew G. Knepley     PetscCall(DMLabelEphemeralSetTransform(labelNew, tr));
59719f6c5813SMatthew G. Knepley     PetscCall(DMLabelSetUp(labelNew));
59729f6c5813SMatthew G. Knepley   }
59733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
59749f6c5813SMatthew G. Knepley }
5975