xref: /petsc/src/dm/impls/plex/plexcreate.c (revision 4054ae399d1c74e8e8baf8c6ed3987b643c521f2)
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 
843*4054ae39SJames Wright // Creates "Face Sets" label based on the standard box labeling conventions
844*4054ae39SJames Wright static PetscErrorCode DMPlexSetBoxLabel_Internal(DM dm)
845*4054ae39SJames Wright {
846*4054ae39SJames Wright   DMLabel         label;
847*4054ae39SJames Wright   IS              is;
848*4054ae39SJames Wright   PetscInt        dim, num_face;
849*4054ae39SJames Wright   const PetscInt *faces;
850*4054ae39SJames Wright   PetscInt        faceMarkerBottom, faceMarkerTop, faceMarkerFront, faceMarkerBack, faceMarkerRight, faceMarkerLeft;
851*4054ae39SJames Wright 
852*4054ae39SJames Wright   PetscFunctionBeginUser;
853*4054ae39SJames Wright   PetscCall(DMGetDimension(dm, &dim));
854*4054ae39SJames Wright   // Get Face Sets label
855*4054ae39SJames Wright   PetscCall(DMGetLabel(dm, "Face Sets", &label));
856*4054ae39SJames Wright   if (label) {
857*4054ae39SJames Wright     PetscCall(DMLabelReset(label));
858*4054ae39SJames Wright   } else {
859*4054ae39SJames Wright     PetscCall(DMCreateLabel(dm, "Face Sets"));
860*4054ae39SJames Wright     PetscCall(DMGetLabel(dm, "Face Sets", &label));
861*4054ae39SJames Wright   }
862*4054ae39SJames Wright   PetscCall(DMPlexMarkBoundaryFaces(dm, 1, label));
863*4054ae39SJames Wright   PetscCall(DMGetStratumIS(dm, "Face Sets", 1, &is));
864*4054ae39SJames Wright   if (!is) PetscFunctionReturn(PETSC_SUCCESS); // No faces on rank
865*4054ae39SJames Wright 
866*4054ae39SJames Wright   switch (dim) {
867*4054ae39SJames Wright   case 2:
868*4054ae39SJames Wright     faceMarkerTop    = 3;
869*4054ae39SJames Wright     faceMarkerBottom = 1;
870*4054ae39SJames Wright     faceMarkerRight  = 2;
871*4054ae39SJames Wright     faceMarkerLeft   = 4;
872*4054ae39SJames Wright     break;
873*4054ae39SJames Wright   case 3:
874*4054ae39SJames Wright     faceMarkerBottom = 1;
875*4054ae39SJames Wright     faceMarkerTop    = 2;
876*4054ae39SJames Wright     faceMarkerFront  = 3;
877*4054ae39SJames Wright     faceMarkerBack   = 4;
878*4054ae39SJames Wright     faceMarkerRight  = 5;
879*4054ae39SJames Wright     faceMarkerLeft   = 6;
880*4054ae39SJames Wright     break;
881*4054ae39SJames Wright   default:
882*4054ae39SJames Wright     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Dimension %" PetscInt_FMT " not supported", dim);
883*4054ae39SJames Wright   }
884*4054ae39SJames Wright 
885*4054ae39SJames Wright   PetscCall(ISGetLocalSize(is, &num_face));
886*4054ae39SJames Wright   PetscCall(ISGetIndices(is, &faces));
887*4054ae39SJames Wright   for (PetscInt f = 0; f < num_face; ++f) {
888*4054ae39SJames Wright     PetscInt  flip = 1, label_value = -1, face = faces[f];
889*4054ae39SJames Wright     PetscReal normal[3];
890*4054ae39SJames Wright 
891*4054ae39SJames Wright     { // Determine if orientation of face is flipped
892*4054ae39SJames Wright       PetscInt        num_cells_support, num_faces, start = -1;
893*4054ae39SJames Wright       const PetscInt *orients, *cell_faces, *cells;
894*4054ae39SJames Wright 
895*4054ae39SJames Wright       PetscCall(DMPlexGetSupport(dm, face, &cells));
896*4054ae39SJames Wright       PetscCall(DMPlexGetSupportSize(dm, face, &num_cells_support));
897*4054ae39SJames Wright       PetscCheck(num_cells_support == 1, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Expected one cell in support of exterior face, but got %" PetscInt_FMT " cells", num_cells_support);
898*4054ae39SJames Wright       PetscCall(DMPlexGetCone(dm, cells[0], &cell_faces));
899*4054ae39SJames Wright       PetscCall(DMPlexGetConeSize(dm, cells[0], &num_faces));
900*4054ae39SJames Wright       for (PetscInt i = 0; i < num_faces; i++) {
901*4054ae39SJames Wright         if (cell_faces[i] == face) start = i;
902*4054ae39SJames Wright       }
903*4054ae39SJames Wright       PetscCheck(start >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "Could not find face %" PetscInt_FMT " in cone of its support", face);
904*4054ae39SJames Wright       PetscCall(DMPlexGetConeOrientation(dm, cells[0], &orients));
905*4054ae39SJames Wright       if (orients[start] < 0) flip = -1;
906*4054ae39SJames Wright     }
907*4054ae39SJames Wright 
908*4054ae39SJames Wright     PetscCall(DMPlexComputeCellGeometryFVM(dm, face, NULL, NULL, normal));
909*4054ae39SJames Wright     for (PetscInt i = 0; i < dim; i++) normal[i] *= flip;
910*4054ae39SJames Wright     switch (dim) {
911*4054ae39SJames Wright     case 2: {
912*4054ae39SJames Wright       if (PetscAbsReal(normal[0]) > PetscAbsReal(normal[1])) {
913*4054ae39SJames Wright         label_value = normal[0] > 0 ? faceMarkerRight : faceMarkerLeft;
914*4054ae39SJames Wright       } else {
915*4054ae39SJames Wright         label_value = normal[1] > 0 ? faceMarkerTop : faceMarkerBottom;
916*4054ae39SJames Wright       }
917*4054ae39SJames Wright     } break;
918*4054ae39SJames Wright     case 3: {
919*4054ae39SJames Wright       if (PetscAbsReal(normal[0]) > PetscAbsReal(normal[1])) {
920*4054ae39SJames Wright         if (PetscAbsReal(normal[0]) > PetscAbsReal(normal[2])) {
921*4054ae39SJames Wright           label_value = normal[0] > 0 ? faceMarkerRight : faceMarkerLeft;
922*4054ae39SJames Wright         } else {
923*4054ae39SJames Wright           label_value = normal[2] > 0 ? faceMarkerTop : faceMarkerBottom;
924*4054ae39SJames Wright         }
925*4054ae39SJames Wright       } else {
926*4054ae39SJames Wright         if (PetscAbsReal(normal[1]) > PetscAbsReal(normal[2])) {
927*4054ae39SJames Wright           label_value = normal[1] > 0 ? faceMarkerBack : faceMarkerFront;
928*4054ae39SJames Wright         } else {
929*4054ae39SJames Wright           label_value = normal[2] > 0 ? faceMarkerTop : faceMarkerBottom;
930*4054ae39SJames Wright         }
931*4054ae39SJames Wright       }
932*4054ae39SJames Wright     } break;
933*4054ae39SJames Wright     }
934*4054ae39SJames Wright 
935*4054ae39SJames Wright     PetscInt previous_label_value; // always 1 due to DMPlexMarkBoundaryFaces call above
936*4054ae39SJames Wright     PetscCall(DMGetLabelValue(dm, "Face Sets", face, &previous_label_value));
937*4054ae39SJames Wright     PetscCall(DMClearLabelValue(dm, "Face Sets", face, previous_label_value));
938*4054ae39SJames Wright     PetscCall(DMSetLabelValue(dm, "Face Sets", face, label_value));
939*4054ae39SJames Wright   }
940*4054ae39SJames Wright   PetscCall(ISRestoreIndices(is, &faces));
941*4054ae39SJames Wright   PetscCall(ISDestroy(&is));
942*4054ae39SJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
943*4054ae39SJames Wright }
944*4054ae39SJames Wright 
945d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxMesh_Simplex_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate)
946d71ae5a4SJacob Faibussowitsch {
9479318fe57SMatthew G. Knepley   DM      boundary, vol;
948c22d3578SMatthew G. Knepley   DMLabel bdlabel;
949d6218766SMatthew G. Knepley 
950d6218766SMatthew G. Knepley   PetscFunctionBegin;
9514f572ea9SToby Isaac   PetscAssertPointer(dm, 1);
952c22d3578SMatthew 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");
9539566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &boundary));
9549566063dSJacob Faibussowitsch   PetscCall(DMSetType(boundary, DMPLEX));
9559566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(boundary, dim, faces, lower, upper, PETSC_FALSE));
9569566063dSJacob Faibussowitsch   PetscCall(DMPlexGenerate(boundary, NULL, interpolate, &vol));
957c22d3578SMatthew G. Knepley   PetscCall(DMGetLabel(vol, "marker", &bdlabel));
958c22d3578SMatthew G. Knepley   if (bdlabel) PetscCall(DMPlexLabelComplete(vol, bdlabel));
9595de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_FALSE, vol));
96069d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &vol));
961*4054ae39SJames Wright   if (interpolate) {
962*4054ae39SJames Wright     PetscCall(DMPlexInterpolateInPlace_Internal(dm));
963*4054ae39SJames Wright     PetscCall(DMPlexSetBoxLabel_Internal(dm));
964*4054ae39SJames Wright   }
9659566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&boundary));
9663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
967d6218766SMatthew G. Knepley }
968d6218766SMatthew G. Knepley 
969d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCubeMesh_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], DMBoundaryType bdX, DMBoundaryType bdY, DMBoundaryType bdZ)
970d71ae5a4SJacob Faibussowitsch {
971ed0e4b50SMatthew G. Knepley   DMLabel     cutLabel  = NULL;
972f4eb4c5dSMatthew G. Knepley   PetscInt    markerTop = 1, faceMarkerTop = 1;
973f4eb4c5dSMatthew G. Knepley   PetscInt    markerBottom = 1, faceMarkerBottom = 1;
974f4eb4c5dSMatthew G. Knepley   PetscInt    markerFront = 1, faceMarkerFront = 1;
975f4eb4c5dSMatthew G. Knepley   PetscInt    markerBack = 1, faceMarkerBack = 1;
976f4eb4c5dSMatthew G. Knepley   PetscInt    markerRight = 1, faceMarkerRight = 1;
977f4eb4c5dSMatthew G. Knepley   PetscInt    markerLeft = 1, faceMarkerLeft = 1;
9783dfda0b1SToby Isaac   PetscInt    dim;
979d8211ee3SMatthew G. Knepley   PetscBool   markerSeparate = PETSC_FALSE, cutMarker = PETSC_FALSE;
9803dfda0b1SToby Isaac   PetscMPIInt rank;
9813dfda0b1SToby Isaac 
9823dfda0b1SToby Isaac   PetscFunctionBegin;
9839566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
9849566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
9859566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
9869566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
9879566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL));
9889371c9d4SSatish 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) {
9899371c9d4SSatish Balay     if (cutMarker) {
9909371c9d4SSatish Balay       PetscCall(DMCreateLabel(dm, "periodic_cut"));
9919371c9d4SSatish Balay       PetscCall(DMGetLabel(dm, "periodic_cut", &cutLabel));
9929371c9d4SSatish Balay     }
993d8211ee3SMatthew G. Knepley   }
9943dfda0b1SToby Isaac   switch (dim) {
9953dfda0b1SToby Isaac   case 2:
996f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 3;
997f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
998f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 2;
999f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 4;
10003dfda0b1SToby Isaac     break;
10013dfda0b1SToby Isaac   case 3:
1002f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
1003f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 2;
1004f4eb4c5dSMatthew G. Knepley     faceMarkerFront  = 3;
1005f4eb4c5dSMatthew G. Knepley     faceMarkerBack   = 4;
1006f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 5;
1007f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 6;
10083dfda0b1SToby Isaac     break;
1009d71ae5a4SJacob Faibussowitsch   default:
1010d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Dimension %" PetscInt_FMT " not supported", dim);
10113dfda0b1SToby Isaac   }
10129566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
1013f4eb4c5dSMatthew G. Knepley   if (markerSeparate) {
1014f4eb4c5dSMatthew G. Knepley     markerBottom = faceMarkerBottom;
1015f4eb4c5dSMatthew G. Knepley     markerTop    = faceMarkerTop;
1016f4eb4c5dSMatthew G. Knepley     markerFront  = faceMarkerFront;
1017f4eb4c5dSMatthew G. Knepley     markerBack   = faceMarkerBack;
1018f4eb4c5dSMatthew G. Knepley     markerRight  = faceMarkerRight;
1019f4eb4c5dSMatthew G. Knepley     markerLeft   = faceMarkerLeft;
10203dfda0b1SToby Isaac   }
10213dfda0b1SToby Isaac   {
1022dd400576SPatrick Sanan     const PetscInt numXEdges    = rank == 0 ? edges[0] : 0;
1023dd400576SPatrick Sanan     const PetscInt numYEdges    = rank == 0 ? edges[1] : 0;
1024dd400576SPatrick Sanan     const PetscInt numZEdges    = rank == 0 ? edges[2] : 0;
1025dd400576SPatrick Sanan     const PetscInt numXVertices = rank == 0 ? (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ? edges[0] : edges[0] + 1) : 0;
1026dd400576SPatrick Sanan     const PetscInt numYVertices = rank == 0 ? (bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ? edges[1] : edges[1] + 1) : 0;
1027dd400576SPatrick Sanan     const PetscInt numZVertices = rank == 0 ? (bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST ? edges[2] : edges[2] + 1) : 0;
10283dfda0b1SToby Isaac     const PetscInt numCells     = numXEdges * numYEdges * numZEdges;
10293dfda0b1SToby Isaac     const PetscInt numXFaces    = numYEdges * numZEdges;
10303dfda0b1SToby Isaac     const PetscInt numYFaces    = numXEdges * numZEdges;
10313dfda0b1SToby Isaac     const PetscInt numZFaces    = numXEdges * numYEdges;
10323dfda0b1SToby Isaac     const PetscInt numTotXFaces = numXVertices * numXFaces;
10333dfda0b1SToby Isaac     const PetscInt numTotYFaces = numYVertices * numYFaces;
10343dfda0b1SToby Isaac     const PetscInt numTotZFaces = numZVertices * numZFaces;
10353dfda0b1SToby Isaac     const PetscInt numFaces     = numTotXFaces + numTotYFaces + numTotZFaces;
10363dfda0b1SToby Isaac     const PetscInt numTotXEdges = numXEdges * numYVertices * numZVertices;
10373dfda0b1SToby Isaac     const PetscInt numTotYEdges = numYEdges * numXVertices * numZVertices;
10383dfda0b1SToby Isaac     const PetscInt numTotZEdges = numZEdges * numXVertices * numYVertices;
10393dfda0b1SToby Isaac     const PetscInt numVertices  = numXVertices * numYVertices * numZVertices;
10403dfda0b1SToby Isaac     const PetscInt numEdges     = numTotXEdges + numTotYEdges + numTotZEdges;
10413dfda0b1SToby Isaac     const PetscInt firstVertex  = (dim == 2) ? numFaces : numCells;
10423dfda0b1SToby Isaac     const PetscInt firstXFace   = (dim == 2) ? 0 : numCells + numVertices;
10433dfda0b1SToby Isaac     const PetscInt firstYFace   = firstXFace + numTotXFaces;
10443dfda0b1SToby Isaac     const PetscInt firstZFace   = firstYFace + numTotYFaces;
10453dfda0b1SToby Isaac     const PetscInt firstXEdge   = numCells + numFaces + numVertices;
10463dfda0b1SToby Isaac     const PetscInt firstYEdge   = firstXEdge + numTotXEdges;
10473dfda0b1SToby Isaac     const PetscInt firstZEdge   = firstYEdge + numTotYEdges;
10483dfda0b1SToby Isaac     Vec            coordinates;
10493dfda0b1SToby Isaac     PetscSection   coordSection;
10503dfda0b1SToby Isaac     PetscScalar   *coords;
10513dfda0b1SToby Isaac     PetscInt       coordSize;
10523dfda0b1SToby Isaac     PetscInt       v, vx, vy, vz;
10533dfda0b1SToby Isaac     PetscInt       c, f, fx, fy, fz, e, ex, ey, ez;
10543dfda0b1SToby Isaac 
10559566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells + numFaces + numEdges + numVertices));
105648a46eb9SPierre Jolivet     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 6));
105748a46eb9SPierre Jolivet     for (f = firstXFace; f < firstXFace + numFaces; ++f) PetscCall(DMPlexSetConeSize(dm, f, 4));
105848a46eb9SPierre Jolivet     for (e = firstXEdge; e < firstXEdge + numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2));
10599566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
10603dfda0b1SToby Isaac     /* Build cells */
10613dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
10623dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
10633dfda0b1SToby Isaac         for (fx = 0; fx < numXEdges; ++fx) {
10643dfda0b1SToby Isaac           PetscInt cell  = (fz * numYEdges + fy) * numXEdges + fx;
10653dfda0b1SToby Isaac           PetscInt faceB = firstZFace + (fy * numXEdges + fx) * numZVertices + fz;
10663dfda0b1SToby Isaac           PetscInt faceT = firstZFace + (fy * numXEdges + fx) * numZVertices + ((fz + 1) % numZVertices);
10673dfda0b1SToby Isaac           PetscInt faceF = firstYFace + (fz * numXEdges + fx) * numYVertices + fy;
10683dfda0b1SToby Isaac           PetscInt faceK = firstYFace + (fz * numXEdges + fx) * numYVertices + ((fy + 1) % numYVertices);
10693dfda0b1SToby Isaac           PetscInt faceL = firstXFace + (fz * numYEdges + fy) * numXVertices + fx;
10703dfda0b1SToby Isaac           PetscInt faceR = firstXFace + (fz * numYEdges + fy) * numXVertices + ((fx + 1) % numXVertices);
10713dfda0b1SToby Isaac           /* B,  T,  F,  K,  R,  L */
1072b5a892a1SMatthew G. Knepley           PetscInt ornt[6] = {-2, 0, 0, -3, 0, -2}; /* ??? */
107342206facSLisandro Dalcin           PetscInt cone[6];
10743dfda0b1SToby Isaac 
10753dfda0b1SToby Isaac           /* no boundary twisting in 3D */
10769371c9d4SSatish Balay           cone[0] = faceB;
10779371c9d4SSatish Balay           cone[1] = faceT;
10789371c9d4SSatish Balay           cone[2] = faceF;
10799371c9d4SSatish Balay           cone[3] = faceK;
10809371c9d4SSatish Balay           cone[4] = faceR;
10819371c9d4SSatish Balay           cone[5] = faceL;
10829566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, cell, cone));
10839566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, cell, ornt));
10849566063dSJacob Faibussowitsch           if (bdX != DM_BOUNDARY_NONE && fx == numXEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
10859566063dSJacob Faibussowitsch           if (bdY != DM_BOUNDARY_NONE && fy == numYEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
10869566063dSJacob Faibussowitsch           if (bdZ != DM_BOUNDARY_NONE && fz == numZEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
10873dfda0b1SToby Isaac         }
10883dfda0b1SToby Isaac       }
10893dfda0b1SToby Isaac     }
10903dfda0b1SToby Isaac     /* Build x faces */
10913dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
10923dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
10933dfda0b1SToby Isaac         for (fx = 0; fx < numXVertices; ++fx) {
10943dfda0b1SToby Isaac           PetscInt face    = firstXFace + (fz * numYEdges + fy) * numXVertices + fx;
10953dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy * numXVertices + fx) * numZEdges + fz;
10963dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (((fy + 1) % numYVertices) * numXVertices + fx) * numZEdges + fz;
10973dfda0b1SToby Isaac           PetscInt edgeB   = firstYEdge + (fz * numXVertices + fx) * numYEdges + fy;
10983dfda0b1SToby Isaac           PetscInt edgeT   = firstYEdge + (((fz + 1) % numZVertices) * numXVertices + fx) * numYEdges + fy;
1099b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
11003dfda0b1SToby Isaac           PetscInt cone[4];
11013dfda0b1SToby Isaac 
11023dfda0b1SToby Isaac           if (dim == 3) {
11033dfda0b1SToby Isaac             /* markers */
11043dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
11053dfda0b1SToby Isaac               if (fx == numXVertices - 1) {
11069566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerRight));
11079566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerRight));
11089371c9d4SSatish Balay               } else if (fx == 0) {
11099566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerLeft));
11109566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerLeft));
11113dfda0b1SToby Isaac               }
11123dfda0b1SToby Isaac             }
11133dfda0b1SToby Isaac           }
11149371c9d4SSatish Balay           cone[0] = edgeB;
11159371c9d4SSatish Balay           cone[1] = edgeR;
11169371c9d4SSatish Balay           cone[2] = edgeT;
11179371c9d4SSatish Balay           cone[3] = edgeL;
11189566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
11199566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
11203dfda0b1SToby Isaac         }
11213dfda0b1SToby Isaac       }
11223dfda0b1SToby Isaac     }
11233dfda0b1SToby Isaac     /* Build y faces */
11243dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
112542206facSLisandro Dalcin       for (fx = 0; fx < numXEdges; ++fx) {
11263dfda0b1SToby Isaac         for (fy = 0; fy < numYVertices; ++fy) {
11273dfda0b1SToby Isaac           PetscInt face    = firstYFace + (fz * numXEdges + fx) * numYVertices + fy;
11283dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy * numXVertices + fx) * numZEdges + fz;
11293dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (fy * numXVertices + ((fx + 1) % numXVertices)) * numZEdges + fz;
11303dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz * numYVertices + fy) * numXEdges + fx;
11313dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (((fz + 1) % numZVertices) * numYVertices + fy) * numXEdges + fx;
1132b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
11333dfda0b1SToby Isaac           PetscInt cone[4];
11343dfda0b1SToby Isaac 
11353dfda0b1SToby Isaac           if (dim == 3) {
11363dfda0b1SToby Isaac             /* markers */
11373dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
11383dfda0b1SToby Isaac               if (fy == numYVertices - 1) {
11399566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerBack));
11409566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerBack));
11419371c9d4SSatish Balay               } else if (fy == 0) {
11429566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerFront));
11439566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerFront));
11443dfda0b1SToby Isaac               }
11453dfda0b1SToby Isaac             }
11463dfda0b1SToby Isaac           }
11479371c9d4SSatish Balay           cone[0] = edgeB;
11489371c9d4SSatish Balay           cone[1] = edgeR;
11499371c9d4SSatish Balay           cone[2] = edgeT;
11509371c9d4SSatish Balay           cone[3] = edgeL;
11519566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
11529566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
11533dfda0b1SToby Isaac         }
11543dfda0b1SToby Isaac       }
11553dfda0b1SToby Isaac     }
11563dfda0b1SToby Isaac     /* Build z faces */
11573dfda0b1SToby Isaac     for (fy = 0; fy < numYEdges; ++fy) {
11583dfda0b1SToby Isaac       for (fx = 0; fx < numXEdges; ++fx) {
11593dfda0b1SToby Isaac         for (fz = 0; fz < numZVertices; fz++) {
11603dfda0b1SToby Isaac           PetscInt face    = firstZFace + (fy * numXEdges + fx) * numZVertices + fz;
11613dfda0b1SToby Isaac           PetscInt edgeL   = firstYEdge + (fz * numXVertices + fx) * numYEdges + fy;
11623dfda0b1SToby Isaac           PetscInt edgeR   = firstYEdge + (fz * numXVertices + ((fx + 1) % numXVertices)) * numYEdges + fy;
11633dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz * numYVertices + fy) * numXEdges + fx;
11643dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (fz * numYVertices + ((fy + 1) % numYVertices)) * numXEdges + fx;
1165b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
11663dfda0b1SToby Isaac           PetscInt cone[4];
11673dfda0b1SToby Isaac 
11683dfda0b1SToby Isaac           if (dim == 2) {
11699371c9d4SSatish Balay             if (bdX == DM_BOUNDARY_TWIST && fx == numXEdges - 1) {
11709371c9d4SSatish Balay               edgeR += numYEdges - 1 - 2 * fy;
11719371c9d4SSatish Balay               ornt[1] = -1;
11729371c9d4SSatish Balay             }
11739371c9d4SSatish Balay             if (bdY == DM_BOUNDARY_TWIST && fy == numYEdges - 1) {
11749371c9d4SSatish Balay               edgeT += numXEdges - 1 - 2 * fx;
11759371c9d4SSatish Balay               ornt[2] = 0;
11769371c9d4SSatish Balay             }
11779566063dSJacob Faibussowitsch             if (bdX != DM_BOUNDARY_NONE && fx == numXEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, face, 2));
11789566063dSJacob Faibussowitsch             if (bdY != DM_BOUNDARY_NONE && fy == numYEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, face, 2));
1179d1c88043SMatthew G. Knepley           } else {
11803dfda0b1SToby Isaac             /* markers */
11813dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
11823dfda0b1SToby Isaac               if (fz == numZVertices - 1) {
11839566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerTop));
11849566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerTop));
11859371c9d4SSatish Balay               } else if (fz == 0) {
11869566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerBottom));
11879566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerBottom));
11883dfda0b1SToby Isaac               }
11893dfda0b1SToby Isaac             }
11903dfda0b1SToby Isaac           }
11919371c9d4SSatish Balay           cone[0] = edgeB;
11929371c9d4SSatish Balay           cone[1] = edgeR;
11939371c9d4SSatish Balay           cone[2] = edgeT;
11949371c9d4SSatish Balay           cone[3] = edgeL;
11959566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
11969566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
11973dfda0b1SToby Isaac         }
11983dfda0b1SToby Isaac       }
11993dfda0b1SToby Isaac     }
12003dfda0b1SToby Isaac     /* Build Z edges*/
12013dfda0b1SToby Isaac     for (vy = 0; vy < numYVertices; vy++) {
12023dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
12033dfda0b1SToby Isaac         for (ez = 0; ez < numZEdges; ez++) {
12043dfda0b1SToby Isaac           const PetscInt edge    = firstZEdge + (vy * numXVertices + vx) * numZEdges + ez;
12053dfda0b1SToby Isaac           const PetscInt vertexB = firstVertex + (ez * numYVertices + vy) * numXVertices + vx;
12063dfda0b1SToby Isaac           const PetscInt vertexT = firstVertex + (((ez + 1) % numZVertices) * numYVertices + vy) * numXVertices + vx;
12073dfda0b1SToby Isaac           PetscInt       cone[2];
12083dfda0b1SToby Isaac 
12099371c9d4SSatish Balay           cone[0] = vertexB;
12109371c9d4SSatish Balay           cone[1] = vertexT;
1211c2df9bbfSMatthew G. Knepley           PetscCall(DMPlexSetCone(dm, edge, cone));
12123dfda0b1SToby Isaac           if (dim == 3) {
12133dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
12143dfda0b1SToby Isaac               if (vx == numXVertices - 1) {
12159566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
1216c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1217c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1218c2df9bbfSMatthew G. Knepley               } else if (vx == 0) {
12199566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
1220c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1221c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
12223dfda0b1SToby Isaac               }
12233dfda0b1SToby Isaac             }
12243dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
12253dfda0b1SToby Isaac               if (vy == numYVertices - 1) {
12269566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBack));
1227c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBack));
1228c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBack));
1229c2df9bbfSMatthew G. Knepley               } else if (vy == 0) {
12309566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerFront));
1231c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerFront));
1232c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerFront));
12333dfda0b1SToby Isaac               }
12343dfda0b1SToby Isaac             }
12353dfda0b1SToby Isaac           }
12363dfda0b1SToby Isaac         }
12373dfda0b1SToby Isaac       }
12383dfda0b1SToby Isaac     }
12393dfda0b1SToby Isaac     /* Build Y edges*/
12403dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
12413dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
12423dfda0b1SToby Isaac         for (ey = 0; ey < numYEdges; ey++) {
12433dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdY == DM_BOUNDARY_TWIST && ey == numYEdges - 1) ? (numXVertices - vx - 1) : (vz * numYVertices + ((ey + 1) % numYVertices)) * numXVertices + vx;
12443dfda0b1SToby Isaac           const PetscInt edge    = firstYEdge + (vz * numXVertices + vx) * numYEdges + ey;
12453dfda0b1SToby Isaac           const PetscInt vertexF = firstVertex + (vz * numYVertices + ey) * numXVertices + vx;
12463dfda0b1SToby Isaac           const PetscInt vertexK = firstVertex + nextv;
12473dfda0b1SToby Isaac           PetscInt       cone[2];
12483dfda0b1SToby Isaac 
12499371c9d4SSatish Balay           cone[0] = vertexF;
12509371c9d4SSatish Balay           cone[1] = vertexK;
12519566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, edge, cone));
12523dfda0b1SToby Isaac           if (dim == 2) {
12533dfda0b1SToby Isaac             if ((bdX != DM_BOUNDARY_PERIODIC) && (bdX != DM_BOUNDARY_TWIST)) {
12543dfda0b1SToby Isaac               if (vx == numXVertices - 1) {
12559566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerRight));
12569566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
12579566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1258c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1259d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
12609566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerLeft));
12619566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
12629566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1263c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
12643dfda0b1SToby Isaac               }
1265d8211ee3SMatthew G. Knepley             } else {
12664c67ea77SStefano Zampini               if (vx == 0 && cutLabel) {
12679566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, edge, 1));
12689566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, cone[0], 1));
1269c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMLabelSetValue(cutLabel, cone[1], 1));
12703dfda0b1SToby Isaac               }
1271d8211ee3SMatthew G. Knepley             }
1272d8211ee3SMatthew G. Knepley           } else {
12733dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
12743dfda0b1SToby Isaac               if (vx == numXVertices - 1) {
12759566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
1276c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1277c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1278d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
12799566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
1280c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1281c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
12823dfda0b1SToby Isaac               }
12833dfda0b1SToby Isaac             }
12843dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
12853dfda0b1SToby Isaac               if (vz == numZVertices - 1) {
12869566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
1287c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1288c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1289d8211ee3SMatthew G. Knepley               } else if (vz == 0) {
12909566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
1291c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1292c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
12933dfda0b1SToby Isaac               }
12943dfda0b1SToby Isaac             }
12953dfda0b1SToby Isaac           }
12963dfda0b1SToby Isaac         }
12973dfda0b1SToby Isaac       }
12983dfda0b1SToby Isaac     }
12993dfda0b1SToby Isaac     /* Build X edges*/
13003dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
13013dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; vy++) {
13023dfda0b1SToby Isaac         for (ex = 0; ex < numXEdges; ex++) {
13033dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdX == DM_BOUNDARY_TWIST && ex == numXEdges - 1) ? (numYVertices - vy - 1) * numXVertices : (vz * numYVertices + vy) * numXVertices + (ex + 1) % numXVertices;
13043dfda0b1SToby Isaac           const PetscInt edge    = firstXEdge + (vz * numYVertices + vy) * numXEdges + ex;
13053dfda0b1SToby Isaac           const PetscInt vertexL = firstVertex + (vz * numYVertices + vy) * numXVertices + ex;
13063dfda0b1SToby Isaac           const PetscInt vertexR = firstVertex + nextv;
13073dfda0b1SToby Isaac           PetscInt       cone[2];
13083dfda0b1SToby Isaac 
13099371c9d4SSatish Balay           cone[0] = vertexL;
13109371c9d4SSatish Balay           cone[1] = vertexR;
13119566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, edge, cone));
13123dfda0b1SToby Isaac           if (dim == 2) {
13133dfda0b1SToby Isaac             if ((bdY != DM_BOUNDARY_PERIODIC) && (bdY != DM_BOUNDARY_TWIST)) {
13143dfda0b1SToby Isaac               if (vy == numYVertices - 1) {
13159566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerTop));
13169566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
13179566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1318c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1319d8211ee3SMatthew G. Knepley               } else if (vy == 0) {
13209566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerBottom));
13219566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
13229566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1323c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
13243dfda0b1SToby Isaac               }
1325d8211ee3SMatthew G. Knepley             } else {
13264c67ea77SStefano Zampini               if (vy == 0 && cutLabel) {
13279566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, edge, 1));
13289566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, cone[0], 1));
1329c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMLabelSetValue(cutLabel, cone[1], 1));
13303dfda0b1SToby Isaac               }
1331d8211ee3SMatthew G. Knepley             }
1332d8211ee3SMatthew G. Knepley           } else {
13333dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
13343dfda0b1SToby Isaac               if (vy == numYVertices - 1) {
13359566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBack));
1336c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBack));
1337c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBack));
1338c2df9bbfSMatthew G. Knepley               } else if (vy == 0) {
13399566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerFront));
1340c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerFront));
1341c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerFront));
13423dfda0b1SToby Isaac               }
13433dfda0b1SToby Isaac             }
13443dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
13453dfda0b1SToby Isaac               if (vz == numZVertices - 1) {
13469566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
1347c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1348c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1349c2df9bbfSMatthew G. Knepley               } else if (vz == 0) {
13509566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
1351c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1352c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
13533dfda0b1SToby Isaac               }
13543dfda0b1SToby Isaac             }
13553dfda0b1SToby Isaac           }
13563dfda0b1SToby Isaac         }
13573dfda0b1SToby Isaac       }
13583dfda0b1SToby Isaac     }
13599566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
13609566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
13613dfda0b1SToby Isaac     /* Build coordinates */
13629566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
13639566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
13649566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
13659566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numVertices));
13663dfda0b1SToby Isaac     for (v = firstVertex; v < firstVertex + numVertices; ++v) {
13679566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
13689566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
13693dfda0b1SToby Isaac     }
13709566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
13719566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
13729566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
13739566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
13749566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
13759566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
13769566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates, VECSTANDARD));
13779566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
13783dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; ++vz) {
13793dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; ++vy) {
13803dfda0b1SToby Isaac         for (vx = 0; vx < numXVertices; ++vx) {
13813dfda0b1SToby Isaac           coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 0] = lower[0] + ((upper[0] - lower[0]) / numXEdges) * vx;
13823dfda0b1SToby Isaac           coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 1] = lower[1] + ((upper[1] - lower[1]) / numYEdges) * vy;
1383ad540459SPierre Jolivet           if (dim == 3) coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 2] = lower[2] + ((upper[2] - lower[2]) / numZEdges) * vz;
13843dfda0b1SToby Isaac         }
13853dfda0b1SToby Isaac       }
13863dfda0b1SToby Isaac     }
13879566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
13889566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
13899566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
13903dfda0b1SToby Isaac   }
13913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
13923dfda0b1SToby Isaac }
13933dfda0b1SToby Isaac 
1394d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxMesh_Tensor_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
1395d71ae5a4SJacob Faibussowitsch {
13969318fe57SMatthew G. Knepley   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
13979318fe57SMatthew G. Knepley   PetscInt       fac[3] = {0, 0, 0}, d;
1398552f7358SJed Brown 
1399552f7358SJed Brown   PetscFunctionBegin;
14004f572ea9SToby Isaac   PetscAssertPointer(dm, 1);
14019318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
14029566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
14039371c9d4SSatish Balay   for (d = 0; d < dim; ++d) {
14049371c9d4SSatish Balay     fac[d] = faces[d];
14059371c9d4SSatish Balay     bdt[d] = periodicity[d];
14069371c9d4SSatish Balay   }
14079566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateCubeMesh_Internal(dm, lower, upper, fac, bdt[0], bdt[1], bdt[2]));
14089371c9d4SSatish 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))) {
14096858538eSMatthew G. Knepley     PetscReal L[3]       = {-1., -1., 0.};
14106858538eSMatthew G. Knepley     PetscReal maxCell[3] = {-1., -1., 0.};
1411552f7358SJed Brown 
14129318fe57SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
14136858538eSMatthew G. Knepley       if (periodicity[d] != DM_BOUNDARY_NONE) {
14149318fe57SMatthew G. Knepley         L[d]       = upper[d] - lower[d];
14159318fe57SMatthew G. Knepley         maxCell[d] = 1.1 * (L[d] / PetscMax(1, faces[d]));
1416768d5fceSMatthew G. Knepley       }
14176858538eSMatthew G. Knepley     }
14184fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, maxCell, lower, L));
1419768d5fceSMatthew G. Knepley   }
14209566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
14213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
14229318fe57SMatthew G. Knepley }
14239318fe57SMatthew G. Knepley 
14245dca41c3SJed 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)
1425d71ae5a4SJacob Faibussowitsch {
14269318fe57SMatthew G. Knepley   PetscFunctionBegin;
142746139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
14285dca41c3SJed Brown   if (shape == DM_SHAPE_ZBOX) PetscCall(DMPlexCreateBoxMesh_Tensor_SFC_Internal(dm, dim, faces, lower, upper, periodicity, interpolate));
14296725e60dSJed Brown   else if (dim == 1) PetscCall(DMPlexCreateLineMesh_Internal(dm, faces[0], lower[0], upper[0], periodicity[0]));
14309566063dSJacob Faibussowitsch   else if (simplex) PetscCall(DMPlexCreateBoxMesh_Simplex_Internal(dm, dim, faces, lower, upper, periodicity, interpolate));
14319566063dSJacob Faibussowitsch   else PetscCall(DMPlexCreateBoxMesh_Tensor_Internal(dm, dim, faces, lower, upper, periodicity));
14329318fe57SMatthew G. Knepley   if (!interpolate && dim > 1 && !simplex) {
1433768d5fceSMatthew G. Knepley     DM udm;
1434768d5fceSMatthew G. Knepley 
14359566063dSJacob Faibussowitsch     PetscCall(DMPlexUninterpolate(dm, &udm));
14369566063dSJacob Faibussowitsch     PetscCall(DMPlexCopyCoordinates(dm, udm));
143769d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &udm));
1438768d5fceSMatthew G. Knepley   }
143946139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
14403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1441c8c68bd8SToby Isaac }
1442c8c68bd8SToby Isaac 
14435d83a8b1SBarry Smith /*@
1444768d5fceSMatthew G. Knepley   DMPlexCreateBoxMesh - Creates a mesh on the tensor product of unit intervals (box) using simplices or tensor cells (hexahedra).
1445768d5fceSMatthew G. Knepley 
1446d083f849SBarry Smith   Collective
1447768d5fceSMatthew G. Knepley 
1448768d5fceSMatthew G. Knepley   Input Parameters:
1449a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
1450768d5fceSMatthew G. Knepley . dim         - The spatial dimension
1451a1cb98faSBarry Smith . simplex     - `PETSC_TRUE` for simplices, `PETSC_FALSE` for tensor cells
145220f4b53cSBarry Smith . faces       - Number of faces per dimension, or `NULL` for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
145320f4b53cSBarry Smith . lower       - The lower left corner, or `NULL` for (0, 0, 0)
145420f4b53cSBarry Smith . upper       - The upper right corner, or `NULL` for (1, 1, 1)
145520f4b53cSBarry Smith . periodicity - The boundary type for the X,Y,Z direction, or `NULL` for `DM_BOUNDARY_NONE`
1456768d5fceSMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
1457768d5fceSMatthew G. Knepley 
1458768d5fceSMatthew G. Knepley   Output Parameter:
1459a1cb98faSBarry Smith . dm - The `DM` object
1460768d5fceSMatthew G. Knepley 
1461768d5fceSMatthew G. Knepley   Level: beginner
1462768d5fceSMatthew G. Knepley 
1463a1cb98faSBarry Smith   Note:
1464a1cb98faSBarry Smith   To customize this mesh using options, use
1465a1cb98faSBarry Smith .vb
1466a1cb98faSBarry Smith   DMCreate(comm, &dm);
1467a1cb98faSBarry Smith   DMSetType(dm, DMPLEX);
1468a1cb98faSBarry Smith   DMSetFromOptions(dm);
1469a1cb98faSBarry Smith .ve
1470a1cb98faSBarry Smith   and use the options in `DMSetFromOptions()`.
1471a1cb98faSBarry Smith 
1472a4e35b19SJacob Faibussowitsch   Here is the numbering returned for 2 faces in each direction for tensor cells\:
1473a1cb98faSBarry Smith .vb
1474a1cb98faSBarry Smith  10---17---11---18----12
1475a1cb98faSBarry Smith   |         |         |
1476a1cb98faSBarry Smith   |         |         |
1477a1cb98faSBarry Smith  20    2   22    3    24
1478a1cb98faSBarry Smith   |         |         |
1479a1cb98faSBarry Smith   |         |         |
1480a1cb98faSBarry Smith   7---15----8---16----9
1481a1cb98faSBarry Smith   |         |         |
1482a1cb98faSBarry Smith   |         |         |
1483a1cb98faSBarry Smith  19    0   21    1   23
1484a1cb98faSBarry Smith   |         |         |
1485a1cb98faSBarry Smith   |         |         |
1486a1cb98faSBarry Smith   4---13----5---14----6
1487a1cb98faSBarry Smith .ve
1488a1cb98faSBarry Smith   and for simplicial cells
1489a1cb98faSBarry Smith .vb
1490a1cb98faSBarry Smith  14----8---15----9----16
1491a1cb98faSBarry Smith   |\     5  |\      7 |
1492a1cb98faSBarry Smith   | \       | \       |
1493a1cb98faSBarry Smith  13   2    14    3    15
1494a1cb98faSBarry Smith   | 4   \   | 6   \   |
1495a1cb98faSBarry Smith   |       \ |       \ |
1496a1cb98faSBarry Smith  11----6---12----7----13
1497a1cb98faSBarry Smith   |\        |\        |
1498a1cb98faSBarry Smith   | \    1  | \     3 |
1499a1cb98faSBarry Smith  10   0    11    1    12
1500a1cb98faSBarry Smith   | 0   \   | 2   \   |
1501a1cb98faSBarry Smith   |       \ |       \ |
1502a1cb98faSBarry Smith   8----4----9----5----10
1503a1cb98faSBarry Smith .ve
1504a1cb98faSBarry Smith 
15051cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMSetFromOptions()`, `DMPlexCreateFromFile()`, `DMPlexCreateHexCylinderMesh()`, `DMSetType()`, `DMCreate()`
1506768d5fceSMatthew G. Knepley @*/
1507d71ae5a4SJacob 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)
1508d71ae5a4SJacob Faibussowitsch {
15099318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
1510fdbf62faSLisandro Dalcin   PetscReal      low[3] = {0, 0, 0};
1511fdbf62faSLisandro Dalcin   PetscReal      upp[3] = {1, 1, 1};
1512fdbf62faSLisandro Dalcin   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
1513552f7358SJed Brown 
1514768d5fceSMatthew G. Knepley   PetscFunctionBegin;
15159566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
15169566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
15175dca41c3SJed Brown   PetscCall(DMPlexCreateBoxMesh_Internal(*dm, DM_SHAPE_BOX, dim, simplex, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt, interpolate));
15187ff04441SMatthew G. Knepley   if (periodicity) PetscCall(DMLocalizeCoordinates(*dm));
15193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
15209318fe57SMatthew G. Knepley }
1521fdbf62faSLisandro Dalcin 
1522d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateWedgeBoxMesh_Internal(DM dm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
1523d71ae5a4SJacob Faibussowitsch {
15249318fe57SMatthew G. Knepley   DM       bdm, vol;
15259318fe57SMatthew G. Knepley   PetscInt i;
15269318fe57SMatthew G. Knepley 
15279318fe57SMatthew G. Knepley   PetscFunctionBegin;
15281fcf445aSMatthew G. Knepley   // TODO Now we can support periodicity
152908401ef6SPierre Jolivet   for (i = 0; i < 3; ++i) PetscCheck(periodicity[i] == DM_BOUNDARY_NONE, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Periodicity not yet supported");
15309566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &bdm));
15319566063dSJacob Faibussowitsch   PetscCall(DMSetType(bdm, DMPLEX));
15329566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(bdm, 2));
153346139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, bdm, 0, 0, 0));
15349566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxMesh_Simplex_Internal(bdm, 2, faces, lower, upper, periodicity, PETSC_TRUE));
15351fcf445aSMatthew G. Knepley   PetscCall(DMPlexExtrude(bdm, faces[2], upper[2] - lower[2], PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, NULL, NULL, &vol));
153646139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, bdm, 0, 0, 0));
15379566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&bdm));
153869d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &vol));
15399318fe57SMatthew G. Knepley   if (lower[2] != 0.0) {
15409318fe57SMatthew G. Knepley     Vec          v;
15419318fe57SMatthew G. Knepley     PetscScalar *x;
15429318fe57SMatthew G. Knepley     PetscInt     cDim, n;
15439318fe57SMatthew G. Knepley 
15449566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &v));
15459566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(v, &cDim));
15469566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(v, &n));
15479566063dSJacob Faibussowitsch     PetscCall(VecGetArray(v, &x));
15489318fe57SMatthew G. Knepley     x += cDim;
15499318fe57SMatthew G. Knepley     for (i = 0; i < n; i += cDim) x[i] += lower[2];
15509566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(v, &x));
15519566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, v));
15529318fe57SMatthew G. Knepley   }
15533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1554552f7358SJed Brown }
1555552f7358SJed Brown 
155600dabe28SStefano Zampini /*@
155739f4f5dbSPierre Jolivet   DMPlexCreateWedgeBoxMesh - Creates a 3-D mesh tessellating the (x,y) plane and extruding in the third direction using wedge cells.
155800dabe28SStefano Zampini 
1559d083f849SBarry Smith   Collective
156000dabe28SStefano Zampini 
156100dabe28SStefano Zampini   Input Parameters:
1562a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
156320f4b53cSBarry Smith . faces       - Number of faces per dimension, or `NULL` for (1, 1, 1)
156420f4b53cSBarry Smith . lower       - The lower left corner, or `NULL` for (0, 0, 0)
156520f4b53cSBarry Smith . upper       - The upper right corner, or `NULL` for (1, 1, 1)
156620f4b53cSBarry Smith . periodicity - The boundary type for the X,Y,Z direction, or `NULL` for `DM_BOUNDARY_NONE`
1567a1cb98faSBarry Smith . orderHeight - If `PETSC_TRUE`, orders the extruded cells in the height first. Otherwise, orders the cell on the layers first
156800dabe28SStefano Zampini - interpolate - Flag to create intermediate mesh pieces (edges, faces)
156900dabe28SStefano Zampini 
157000dabe28SStefano Zampini   Output Parameter:
1571a1cb98faSBarry Smith . dm - The `DM` object
157200dabe28SStefano Zampini 
157300dabe28SStefano Zampini   Level: beginner
157400dabe28SStefano Zampini 
15751cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateHexCylinderMesh()`, `DMPlexCreateWedgeCylinderMesh()`, `DMExtrude()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
157600dabe28SStefano Zampini @*/
1577d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateWedgeBoxMesh(MPI_Comm comm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool orderHeight, PetscBool interpolate, DM *dm)
1578d71ae5a4SJacob Faibussowitsch {
15799318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
158000dabe28SStefano Zampini   PetscReal      low[3] = {0, 0, 0};
158100dabe28SStefano Zampini   PetscReal      upp[3] = {1, 1, 1};
158200dabe28SStefano Zampini   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
158300dabe28SStefano Zampini 
158400dabe28SStefano Zampini   PetscFunctionBegin;
15859566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
15869566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
15879566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateWedgeBoxMesh_Internal(*dm, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt));
1588d410b0cfSMatthew G. Knepley   if (!interpolate) {
1589d410b0cfSMatthew G. Knepley     DM udm;
159000dabe28SStefano Zampini 
15919566063dSJacob Faibussowitsch     PetscCall(DMPlexUninterpolate(*dm, &udm));
159269d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(*dm, &udm));
159300dabe28SStefano Zampini   }
15947ff04441SMatthew G. Knepley   if (periodicity) PetscCall(DMLocalizeCoordinates(*dm));
15953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
159600dabe28SStefano Zampini }
159700dabe28SStefano Zampini 
1598cfb853baSMatthew G. Knepley /*
1599cfb853baSMatthew 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.
1600cfb853baSMatthew G. Knepley 
1601cfb853baSMatthew G. Knepley   Input Parameters:
1602cfb853baSMatthew G. Knepley + len - The length of the tuple
1603cfb853baSMatthew G. Knepley . max - The maximum for each dimension, so values are in [0, max)
1604cfb853baSMatthew G. Knepley - tup - A tuple of length len+1: tup[len] > 0 indicates a stopping condition
1605cfb853baSMatthew G. Knepley 
1606cfb853baSMatthew G. Knepley   Output Parameter:
160720f4b53cSBarry Smith . tup - A tuple of `len` integers whose entries are at most `max`
1608cfb853baSMatthew G. Knepley 
1609cfb853baSMatthew G. Knepley   Level: developer
1610cfb853baSMatthew G. Knepley 
161120f4b53cSBarry Smith   Note:
161220f4b53cSBarry Smith   Ordering is lexicographic with lowest index as least significant in ordering.
161320f4b53cSBarry 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}.
161420f4b53cSBarry Smith 
1615cfb853baSMatthew G. Knepley .seealso: PetscDualSpaceTensorPointLexicographic_Internal(), PetscDualSpaceLatticePointLexicographic_Internal()
1616cfb853baSMatthew G. Knepley */
1617cfb853baSMatthew G. Knepley static PetscErrorCode DMPlexTensorPointLexicographic_Private(PetscInt len, const PetscInt max[], PetscInt tup[])
1618cfb853baSMatthew G. Knepley {
1619cfb853baSMatthew G. Knepley   PetscInt i;
1620cfb853baSMatthew G. Knepley 
1621cfb853baSMatthew G. Knepley   PetscFunctionBegin;
1622cfb853baSMatthew G. Knepley   for (i = 0; i < len; ++i) {
1623cfb853baSMatthew G. Knepley     if (tup[i] < max[i] - 1) {
1624cfb853baSMatthew G. Knepley       break;
1625cfb853baSMatthew G. Knepley     } else {
1626cfb853baSMatthew G. Knepley       tup[i] = 0;
1627cfb853baSMatthew G. Knepley     }
1628cfb853baSMatthew G. Knepley   }
1629cfb853baSMatthew G. Knepley   if (i == len) tup[i - 1] = max[i - 1];
1630cfb853baSMatthew G. Knepley   else ++tup[i];
16313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1632cfb853baSMatthew G. Knepley }
1633cfb853baSMatthew G. Knepley 
1634cfb853baSMatthew G. Knepley static PetscInt TupleToIndex_Private(PetscInt len, const PetscInt max[], const PetscInt tup[])
1635cfb853baSMatthew G. Knepley {
1636cfb853baSMatthew G. Knepley   PetscInt i, idx = tup[len - 1];
1637cfb853baSMatthew G. Knepley 
1638cfb853baSMatthew G. Knepley   for (i = len - 2; i >= 0; --i) {
1639cfb853baSMatthew G. Knepley     idx *= max[i];
1640cfb853baSMatthew G. Knepley     idx += tup[i];
1641cfb853baSMatthew G. Knepley   }
1642cfb853baSMatthew G. Knepley   return idx;
1643cfb853baSMatthew G. Knepley }
1644cfb853baSMatthew G. Knepley 
1645cfb853baSMatthew G. Knepley static PetscErrorCode DestroyExtent_Private(void *extent)
1646cfb853baSMatthew G. Knepley {
1647cfb853baSMatthew G. Knepley   return PetscFree(extent);
1648cfb853baSMatthew G. Knepley }
1649cfb853baSMatthew G. Knepley 
1650cfb853baSMatthew G. Knepley static PetscErrorCode DMPlexCreateHypercubicMesh_Internal(DM dm, PetscInt dim, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], const DMBoundaryType bd[])
1651cfb853baSMatthew G. Knepley {
1652cfb853baSMatthew G. Knepley   Vec          coordinates;
1653cfb853baSMatthew G. Knepley   PetscSection coordSection;
1654cfb853baSMatthew G. Knepley   DMLabel      cutLabel    = NULL;
1655cfb853baSMatthew G. Knepley   PetscBool    cutMarker   = PETSC_FALSE;
1656cfb853baSMatthew G. Knepley   PetscBool    periodic    = PETSC_FALSE;
1657cfb853baSMatthew G. Knepley   PetscInt     numCells    = 1, c;
1658cfb853baSMatthew G. Knepley   PetscInt     numVertices = 1, v;
1659cfb853baSMatthew G. Knepley   PetscScalar *coords;
1660cfb853baSMatthew G. Knepley   PetscInt    *vertices, *vert, *vtmp, *supp, cone[2];
1661cfb853baSMatthew G. Knepley   PetscInt     d, e, cell = 0, coordSize;
1662cfb853baSMatthew G. Knepley   PetscMPIInt  rank;
1663cfb853baSMatthew G. Knepley 
1664cfb853baSMatthew G. Knepley   PetscFunctionBegin;
1665cfb853baSMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
1666cfb853baSMatthew G. Knepley   PetscCall(DMSetDimension(dm, dim));
1667cfb853baSMatthew G. Knepley   PetscCall(PetscCalloc4(dim, &vertices, dim, &vert, dim, &vtmp, 2 * dim, &supp));
1668cfb853baSMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "marker"));
1669cfb853baSMatthew G. Knepley   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL));
1670cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) periodic = (periodic || bd[d] == DM_BOUNDARY_PERIODIC) ? PETSC_TRUE : PETSC_FALSE;
1671cfb853baSMatthew G. Knepley   if (periodic && cutMarker) {
1672cfb853baSMatthew G. Knepley     PetscCall(DMCreateLabel(dm, "periodic_cut"));
1673cfb853baSMatthew G. Knepley     PetscCall(DMGetLabel(dm, "periodic_cut", &cutLabel));
1674cfb853baSMatthew G. Knepley   }
1675cfb853baSMatthew 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");
1676cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) {
1677cfb853baSMatthew G. Knepley     vertices[d] = edges[d];
1678cfb853baSMatthew G. Knepley     numVertices *= vertices[d];
1679cfb853baSMatthew G. Knepley   }
1680cfb853baSMatthew G. Knepley   numCells = numVertices * dim;
1681cfb853baSMatthew G. Knepley   PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
1682cfb853baSMatthew G. Knepley   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, 2));
1683cfb853baSMatthew G. Knepley   for (v = numCells; v < numCells + numVertices; ++v) PetscCall(DMPlexSetSupportSize(dm, v, 2 * dim));
1684cfb853baSMatthew G. Knepley   /* TODO Loop over boundary and reset support sizes */
1685cfb853baSMatthew G. Knepley   PetscCall(DMSetUp(dm)); /* Allocate space for cones and supports */
1686cfb853baSMatthew G. Knepley   /* Build cell cones and vertex supports */
1687cfb853baSMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "celltype"));
1688cfb853baSMatthew G. Knepley   while (vert[dim - 1] < vertices[dim - 1]) {
1689cfb853baSMatthew G. Knepley     const PetscInt vertex = TupleToIndex_Private(dim, vertices, vert) + numCells;
1690cfb853baSMatthew G. Knepley     PetscInt       s      = 0;
1691cfb853baSMatthew G. Knepley 
16923ba16761SJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "Vertex %" PetscInt_FMT ":", vertex));
16933ba16761SJacob Faibussowitsch     for (d = 0; d < dim; ++d) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, vert[d]));
16943ba16761SJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
1695cfb853baSMatthew G. Knepley     PetscCall(DMPlexSetCellType(dm, vertex, DM_POLYTOPE_POINT));
1696cfb853baSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
1697cfb853baSMatthew G. Knepley       for (e = 0; e < dim; ++e) vtmp[e] = vert[e];
1698cfb853baSMatthew G. Knepley       vtmp[d] = (vert[d] + 1) % vertices[d];
1699cfb853baSMatthew G. Knepley       cone[0] = vertex;
1700cfb853baSMatthew G. Knepley       cone[1] = TupleToIndex_Private(dim, vertices, vtmp) + numCells;
17013ba16761SJacob Faibussowitsch       PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Vertex %" PetscInt_FMT ":", cone[1]));
17023ba16761SJacob Faibussowitsch       for (e = 0; e < dim; ++e) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, vtmp[e]));
17033ba16761SJacob Faibussowitsch       PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
1704cfb853baSMatthew G. Knepley       PetscCall(DMPlexSetCone(dm, cell, cone));
1705cfb853baSMatthew G. Knepley       PetscCall(DMPlexSetCellType(dm, cell, DM_POLYTOPE_SEGMENT));
17063ba16761SJacob Faibussowitsch       PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Edge %" PetscInt_FMT " (%" PetscInt_FMT " %" PetscInt_FMT ")\n", cell, cone[0], cone[1]));
1707cfb853baSMatthew G. Knepley       ++cell;
1708cfb853baSMatthew G. Knepley     }
1709cfb853baSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
1710cfb853baSMatthew G. Knepley       for (e = 0; e < dim; ++e) vtmp[e] = vert[e];
1711cfb853baSMatthew G. Knepley       vtmp[d]   = (vert[d] + vertices[d] - 1) % vertices[d];
1712cfb853baSMatthew G. Knepley       supp[s++] = TupleToIndex_Private(dim, vertices, vtmp) * dim + d;
1713cfb853baSMatthew G. Knepley       supp[s++] = (vertex - numCells) * dim + d;
1714cfb853baSMatthew G. Knepley       PetscCall(DMPlexSetSupport(dm, vertex, supp));
1715cfb853baSMatthew G. Knepley     }
1716cfb853baSMatthew G. Knepley     PetscCall(DMPlexTensorPointLexicographic_Private(dim, vertices, vert));
1717cfb853baSMatthew G. Knepley   }
1718cfb853baSMatthew G. Knepley   PetscCall(DMPlexStratify(dm));
1719cfb853baSMatthew G. Knepley   /* Build coordinates */
1720cfb853baSMatthew G. Knepley   PetscCall(DMGetCoordinateSection(dm, &coordSection));
1721cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetNumFields(coordSection, 1));
1722cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
1723cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices));
1724cfb853baSMatthew G. Knepley   for (v = numCells; v < numCells + numVertices; ++v) {
1725cfb853baSMatthew G. Knepley     PetscCall(PetscSectionSetDof(coordSection, v, dim));
1726cfb853baSMatthew G. Knepley     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
1727cfb853baSMatthew G. Knepley   }
1728cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetUp(coordSection));
1729cfb853baSMatthew G. Knepley   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
1730cfb853baSMatthew G. Knepley   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
1731cfb853baSMatthew G. Knepley   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
1732cfb853baSMatthew G. Knepley   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
1733cfb853baSMatthew G. Knepley   PetscCall(VecSetBlockSize(coordinates, dim));
1734cfb853baSMatthew G. Knepley   PetscCall(VecSetType(coordinates, VECSTANDARD));
1735cfb853baSMatthew G. Knepley   PetscCall(VecGetArray(coordinates, &coords));
1736cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) vert[d] = 0;
1737cfb853baSMatthew G. Knepley   while (vert[dim - 1] < vertices[dim - 1]) {
1738cfb853baSMatthew G. Knepley     const PetscInt vertex = TupleToIndex_Private(dim, vertices, vert);
1739cfb853baSMatthew G. Knepley 
1740cfb853baSMatthew G. Knepley     for (d = 0; d < dim; ++d) coords[vertex * dim + d] = lower[d] + ((upper[d] - lower[d]) / vertices[d]) * vert[d];
17413ba16761SJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "Vertex %" PetscInt_FMT ":", vertex));
17423ba16761SJacob Faibussowitsch     for (d = 0; d < dim; ++d) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, vert[d]));
17433ba16761SJacob Faibussowitsch     for (d = 0; d < dim; ++d) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %g", (double)PetscRealPart(coords[vertex * dim + d])));
17443ba16761SJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
1745cfb853baSMatthew G. Knepley     PetscCall(DMPlexTensorPointLexicographic_Private(dim, vertices, vert));
1746cfb853baSMatthew G. Knepley   }
1747cfb853baSMatthew G. Knepley   PetscCall(VecRestoreArray(coordinates, &coords));
1748cfb853baSMatthew G. Knepley   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
1749cfb853baSMatthew G. Knepley   PetscCall(VecDestroy(&coordinates));
1750cfb853baSMatthew G. Knepley   PetscCall(PetscFree4(vertices, vert, vtmp, supp));
1751cfb853baSMatthew G. Knepley   //PetscCall(DMSetPeriodicity(dm, NULL, lower, upper));
1752cfb853baSMatthew G. Knepley   // Attach the extent
1753cfb853baSMatthew G. Knepley   {
1754cfb853baSMatthew G. Knepley     PetscContainer c;
1755cfb853baSMatthew G. Knepley     PetscInt      *extent;
1756cfb853baSMatthew G. Knepley 
1757cfb853baSMatthew G. Knepley     PetscCall(PetscMalloc1(dim, &extent));
1758cfb853baSMatthew G. Knepley     for (PetscInt d = 0; d < dim; ++d) extent[d] = edges[d];
1759cfb853baSMatthew G. Knepley     PetscCall(PetscContainerCreate(PETSC_COMM_SELF, &c));
1760cfb853baSMatthew G. Knepley     PetscCall(PetscContainerSetUserDestroy(c, DestroyExtent_Private));
1761cfb853baSMatthew G. Knepley     PetscCall(PetscContainerSetPointer(c, extent));
1762cfb853baSMatthew G. Knepley     PetscCall(PetscObjectCompose((PetscObject)dm, "_extent", (PetscObject)c));
1763cfb853baSMatthew G. Knepley     PetscCall(PetscContainerDestroy(&c));
1764cfb853baSMatthew G. Knepley   }
17653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1766cfb853baSMatthew G. Knepley }
1767cfb853baSMatthew G. Knepley 
1768cfb853baSMatthew G. Knepley /*@C
1769aaa8cc7dSPierre Jolivet   DMPlexCreateHypercubicMesh - Creates a periodic mesh on the tensor product of unit intervals using only vertices and edges.
1770cfb853baSMatthew G. Knepley 
1771cfb853baSMatthew G. Knepley   Collective
1772cfb853baSMatthew G. Knepley 
1773cfb853baSMatthew G. Knepley   Input Parameters:
1774cfb853baSMatthew G. Knepley + comm  - The communicator for the DM object
1775cfb853baSMatthew G. Knepley . dim   - The spatial dimension
177620f4b53cSBarry Smith . edges - Number of edges per dimension, or `NULL` for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
177720f4b53cSBarry Smith . lower - The lower left corner, or `NULL` for (0, 0, 0)
177820f4b53cSBarry Smith - upper - The upper right corner, or `NULL` for (1, 1, 1)
1779cfb853baSMatthew G. Knepley 
1780cfb853baSMatthew G. Knepley   Output Parameter:
1781cfb853baSMatthew G. Knepley . dm - The DM object
1782cfb853baSMatthew G. Knepley 
178320f4b53cSBarry Smith   Level: beginner
178420f4b53cSBarry Smith 
178520f4b53cSBarry Smith   Note:
178620f4b53cSBarry Smith   If you want to customize this mesh using options, you just need to
178720f4b53cSBarry Smith .vb
178820f4b53cSBarry Smith   DMCreate(comm, &dm);
178920f4b53cSBarry Smith   DMSetType(dm, DMPLEX);
179020f4b53cSBarry Smith   DMSetFromOptions(dm);
179120f4b53cSBarry Smith .ve
179220f4b53cSBarry Smith   and use the options on the `DMSetFromOptions()` page.
1793cfb853baSMatthew G. Knepley 
1794cfb853baSMatthew G. Knepley   The vertices are numbered is lexicographic order, and the dim edges exiting a vertex in the positive orthant are number consecutively,
179520f4b53cSBarry Smith .vb
179620f4b53cSBarry Smith  18--0-19--2-20--4-18
179720f4b53cSBarry Smith   |     |     |     |
179820f4b53cSBarry Smith  13    15    17    13
179920f4b53cSBarry Smith   |     |     |     |
180020f4b53cSBarry Smith  24-12-25-14-26-16-24
180120f4b53cSBarry Smith   |     |     |     |
180220f4b53cSBarry Smith   7     9    11     7
180320f4b53cSBarry Smith   |     |     |     |
180420f4b53cSBarry Smith  21--6-22--8-23-10-21
180520f4b53cSBarry Smith   |     |     |     |
180620f4b53cSBarry Smith   1     3     5     1
180720f4b53cSBarry Smith   |     |     |     |
180820f4b53cSBarry Smith  18--0-19--2-20--4-18
180920f4b53cSBarry Smith .ve
1810cfb853baSMatthew G. Knepley 
181176fbde31SPierre Jolivet .seealso: `DMSetFromOptions()`, `DMPlexCreateFromFile()`, `DMPlexCreateHexCylinderMesh()`, `DMSetType()`, `DMCreate()`
1812cfb853baSMatthew G. Knepley @*/
1813cfb853baSMatthew G. Knepley PetscErrorCode DMPlexCreateHypercubicMesh(MPI_Comm comm, PetscInt dim, const PetscInt edges[], const PetscReal lower[], const PetscReal upper[], DM *dm)
1814cfb853baSMatthew G. Knepley {
1815cfb853baSMatthew G. Knepley   PetscInt       *edg;
1816cfb853baSMatthew G. Knepley   PetscReal      *low, *upp;
1817cfb853baSMatthew G. Knepley   DMBoundaryType *bdt;
1818cfb853baSMatthew G. Knepley   PetscInt        d;
1819cfb853baSMatthew G. Knepley 
1820cfb853baSMatthew G. Knepley   PetscFunctionBegin;
1821cfb853baSMatthew G. Knepley   PetscCall(DMCreate(comm, dm));
1822cfb853baSMatthew G. Knepley   PetscCall(DMSetType(*dm, DMPLEX));
1823cfb853baSMatthew G. Knepley   PetscCall(PetscMalloc4(dim, &edg, dim, &low, dim, &upp, dim, &bdt));
1824cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) {
1825cfb853baSMatthew G. Knepley     edg[d] = edges ? edges[d] : 1;
1826cfb853baSMatthew G. Knepley     low[d] = lower ? lower[d] : 0.;
1827cfb853baSMatthew G. Knepley     upp[d] = upper ? upper[d] : 1.;
1828cfb853baSMatthew G. Knepley     bdt[d] = DM_BOUNDARY_PERIODIC;
1829cfb853baSMatthew G. Knepley   }
1830cfb853baSMatthew G. Knepley   PetscCall(DMPlexCreateHypercubicMesh_Internal(*dm, dim, low, upp, edg, bdt));
1831cfb853baSMatthew G. Knepley   PetscCall(PetscFree4(edg, low, upp, bdt));
18323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1833cfb853baSMatthew G. Knepley }
1834cfb853baSMatthew G. Knepley 
1835cc4c1da9SBarry Smith /*@
1836a1cb98faSBarry Smith   DMPlexSetOptionsPrefix - Sets the prefix used for searching for all `DM` options in the database.
1837a9074c1eSMatthew G. Knepley 
183820f4b53cSBarry Smith   Logically Collective
1839a9074c1eSMatthew G. Knepley 
1840a9074c1eSMatthew G. Knepley   Input Parameters:
184120f4b53cSBarry Smith + dm     - the `DM` context
1842a9074c1eSMatthew G. Knepley - prefix - the prefix to prepend to all option names
1843a9074c1eSMatthew G. Knepley 
1844a1cb98faSBarry Smith   Level: advanced
1845a1cb98faSBarry Smith 
1846a1cb98faSBarry Smith   Note:
1847a9074c1eSMatthew G. Knepley   A hyphen (-) must NOT be given at the beginning of the prefix name.
1848a9074c1eSMatthew G. Knepley   The first character of all runtime options is AUTOMATICALLY the hyphen.
1849a9074c1eSMatthew G. Knepley 
18501cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `SNESSetFromOptions()`
1851a9074c1eSMatthew G. Knepley @*/
1852d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetOptionsPrefix(DM dm, const char prefix[])
1853d71ae5a4SJacob Faibussowitsch {
1854a9074c1eSMatthew G. Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
1855a9074c1eSMatthew G. Knepley 
1856a9074c1eSMatthew G. Knepley   PetscFunctionBegin;
1857a9074c1eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18589566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, prefix));
18599566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)mesh->partitioner, prefix));
18603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1861a9074c1eSMatthew G. Knepley }
1862a9074c1eSMatthew G. Knepley 
18639318fe57SMatthew G. Knepley /* Remap geometry to cylinder
186461a622f3SMatthew G. Knepley    TODO: This only works for a single refinement, then it is broken
186561a622f3SMatthew G. Knepley 
18669318fe57SMatthew G. Knepley      Interior square: Linear interpolation is correct
18679318fe57SMatthew G. Knepley      The other cells all have vertices on rays from the origin. We want to uniformly expand the spacing
18689318fe57SMatthew G. Knepley      such that the last vertex is on the unit circle. So the closest and farthest vertices are at distance
18690510c589SMatthew G. Knepley 
18709318fe57SMatthew G. Knepley        phi     = arctan(y/x)
18719318fe57SMatthew G. Knepley        d_close = sqrt(1/8 + 1/4 sin^2(phi))
18729318fe57SMatthew G. Knepley        d_far   = sqrt(1/2 + sin^2(phi))
18730510c589SMatthew G. Knepley 
18749318fe57SMatthew G. Knepley      so we remap them using
18750510c589SMatthew G. Knepley 
18769318fe57SMatthew G. Knepley        x_new = x_close + (x - x_close) (1 - d_close) / (d_far - d_close)
18779318fe57SMatthew G. Knepley        y_new = y_close + (y - y_close) (1 - d_close) / (d_far - d_close)
18780510c589SMatthew G. Knepley 
18799318fe57SMatthew G. Knepley      If pi/4 < phi < 3pi/4 or -3pi/4 < phi < -pi/4, then we switch x and y.
18809318fe57SMatthew G. Knepley */
1881d71ae5a4SJacob 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[])
1882d71ae5a4SJacob Faibussowitsch {
18839318fe57SMatthew G. Knepley   const PetscReal dis = 1.0 / PetscSqrtReal(2.0);
18849318fe57SMatthew G. Knepley   const PetscReal ds2 = 0.5 * dis;
188522cc497dSMatthew G. Knepley 
18869318fe57SMatthew G. Knepley   if ((PetscAbsScalar(u[0]) <= ds2) && (PetscAbsScalar(u[1]) <= ds2)) {
18879318fe57SMatthew G. Knepley     f0[0] = u[0];
18889318fe57SMatthew G. Knepley     f0[1] = u[1];
18899318fe57SMatthew G. Knepley   } else {
18909318fe57SMatthew G. Knepley     PetscReal phi, sinp, cosp, dc, df, x, y, xc, yc;
18910510c589SMatthew G. Knepley 
18929318fe57SMatthew G. Knepley     x    = PetscRealPart(u[0]);
18939318fe57SMatthew G. Knepley     y    = PetscRealPart(u[1]);
18949318fe57SMatthew G. Knepley     phi  = PetscAtan2Real(y, x);
18959318fe57SMatthew G. Knepley     sinp = PetscSinReal(phi);
18969318fe57SMatthew G. Knepley     cosp = PetscCosReal(phi);
18979318fe57SMatthew G. Knepley     if ((PetscAbsReal(phi) > PETSC_PI / 4.0) && (PetscAbsReal(phi) < 3.0 * PETSC_PI / 4.0)) {
18989318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2 / sinp);
18999318fe57SMatthew G. Knepley       df = PetscAbsReal(dis / sinp);
19009318fe57SMatthew G. Knepley       xc = ds2 * x / PetscAbsReal(y);
19019318fe57SMatthew G. Knepley       yc = ds2 * PetscSignReal(y);
19029318fe57SMatthew G. Knepley     } else {
19039318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2 / cosp);
19049318fe57SMatthew G. Knepley       df = PetscAbsReal(dis / cosp);
19059318fe57SMatthew G. Knepley       xc = ds2 * PetscSignReal(x);
19069318fe57SMatthew G. Knepley       yc = ds2 * y / PetscAbsReal(x);
19079318fe57SMatthew G. Knepley     }
19089318fe57SMatthew G. Knepley     f0[0] = xc + (u[0] - xc) * (1.0 - dc) / (df - dc);
19099318fe57SMatthew G. Knepley     f0[1] = yc + (u[1] - yc) * (1.0 - dc) / (df - dc);
19109318fe57SMatthew G. Knepley   }
19119318fe57SMatthew G. Knepley   f0[2] = u[2];
19129318fe57SMatthew G. Knepley }
19130510c589SMatthew G. Knepley 
191449704ca5SMatthew G. Knepley static PetscErrorCode DMPlexCreateHexCylinderMesh_Internal(DM dm, DMBoundaryType periodicZ, PetscInt Nr)
1915d71ae5a4SJacob Faibussowitsch {
19160510c589SMatthew G. Knepley   const PetscInt dim = 3;
19179318fe57SMatthew G. Knepley   PetscInt       numCells, numVertices;
1918d8c47e87SMatthew G. Knepley   PetscMPIInt    rank;
19190510c589SMatthew G. Knepley 
19200510c589SMatthew G. Knepley   PetscFunctionBegin;
192146139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
19229566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
19239566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
19240510c589SMatthew G. Knepley   /* Create topology */
19250510c589SMatthew G. Knepley   {
19260510c589SMatthew G. Knepley     PetscInt cone[8], c;
19270510c589SMatthew G. Knepley 
1928dd400576SPatrick Sanan     numCells    = rank == 0 ? 5 : 0;
1929dd400576SPatrick Sanan     numVertices = rank == 0 ? 16 : 0;
1930006a8963SMatthew G. Knepley     if (periodicZ == DM_BOUNDARY_PERIODIC) {
1931ae8bcbbbSMatthew G. Knepley       numCells *= 3;
1932dd400576SPatrick Sanan       numVertices = rank == 0 ? 24 : 0;
1933006a8963SMatthew G. Knepley     }
19349566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
19359566063dSJacob Faibussowitsch     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 8));
19369566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm));
1937dd400576SPatrick Sanan     if (rank == 0) {
1938006a8963SMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
19399371c9d4SSatish Balay         cone[0] = 15;
19409371c9d4SSatish Balay         cone[1] = 18;
19419371c9d4SSatish Balay         cone[2] = 17;
19429371c9d4SSatish Balay         cone[3] = 16;
19439371c9d4SSatish Balay         cone[4] = 31;
19449371c9d4SSatish Balay         cone[5] = 32;
19459371c9d4SSatish Balay         cone[6] = 33;
19469371c9d4SSatish Balay         cone[7] = 34;
19479566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
19489371c9d4SSatish Balay         cone[0] = 16;
19499371c9d4SSatish Balay         cone[1] = 17;
19509371c9d4SSatish Balay         cone[2] = 24;
19519371c9d4SSatish Balay         cone[3] = 23;
19529371c9d4SSatish Balay         cone[4] = 32;
19539371c9d4SSatish Balay         cone[5] = 36;
19549371c9d4SSatish Balay         cone[6] = 37;
19559371c9d4SSatish Balay         cone[7] = 33; /* 22 25 26 21 */
19569566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
19579371c9d4SSatish Balay         cone[0] = 18;
19589371c9d4SSatish Balay         cone[1] = 27;
19599371c9d4SSatish Balay         cone[2] = 24;
19609371c9d4SSatish Balay         cone[3] = 17;
19619371c9d4SSatish Balay         cone[4] = 34;
19629371c9d4SSatish Balay         cone[5] = 33;
19639371c9d4SSatish Balay         cone[6] = 37;
19649371c9d4SSatish Balay         cone[7] = 38;
19659566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
19669371c9d4SSatish Balay         cone[0] = 29;
19679371c9d4SSatish Balay         cone[1] = 27;
19689371c9d4SSatish Balay         cone[2] = 18;
19699371c9d4SSatish Balay         cone[3] = 15;
19709371c9d4SSatish Balay         cone[4] = 35;
19719371c9d4SSatish Balay         cone[5] = 31;
19729371c9d4SSatish Balay         cone[6] = 34;
19739371c9d4SSatish Balay         cone[7] = 38;
19749566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
19759371c9d4SSatish Balay         cone[0] = 29;
19769371c9d4SSatish Balay         cone[1] = 15;
19779371c9d4SSatish Balay         cone[2] = 16;
19789371c9d4SSatish Balay         cone[3] = 23;
19799371c9d4SSatish Balay         cone[4] = 35;
19809371c9d4SSatish Balay         cone[5] = 36;
19819371c9d4SSatish Balay         cone[6] = 32;
19829371c9d4SSatish Balay         cone[7] = 31;
19839566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
1984006a8963SMatthew G. Knepley 
19859371c9d4SSatish Balay         cone[0] = 31;
19869371c9d4SSatish Balay         cone[1] = 34;
19879371c9d4SSatish Balay         cone[2] = 33;
19889371c9d4SSatish Balay         cone[3] = 32;
19899371c9d4SSatish Balay         cone[4] = 19;
19909371c9d4SSatish Balay         cone[5] = 22;
19919371c9d4SSatish Balay         cone[6] = 21;
19929371c9d4SSatish Balay         cone[7] = 20;
19939566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 5, cone));
19949371c9d4SSatish Balay         cone[0] = 32;
19959371c9d4SSatish Balay         cone[1] = 33;
19969371c9d4SSatish Balay         cone[2] = 37;
19979371c9d4SSatish Balay         cone[3] = 36;
19989371c9d4SSatish Balay         cone[4] = 22;
19999371c9d4SSatish Balay         cone[5] = 25;
20009371c9d4SSatish Balay         cone[6] = 26;
20019371c9d4SSatish Balay         cone[7] = 21;
20029566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 6, cone));
20039371c9d4SSatish Balay         cone[0] = 34;
20049371c9d4SSatish Balay         cone[1] = 38;
20059371c9d4SSatish Balay         cone[2] = 37;
20069371c9d4SSatish Balay         cone[3] = 33;
20079371c9d4SSatish Balay         cone[4] = 20;
20089371c9d4SSatish Balay         cone[5] = 21;
20099371c9d4SSatish Balay         cone[6] = 26;
20109371c9d4SSatish Balay         cone[7] = 28;
20119566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 7, cone));
20129371c9d4SSatish Balay         cone[0] = 35;
20139371c9d4SSatish Balay         cone[1] = 38;
20149371c9d4SSatish Balay         cone[2] = 34;
20159371c9d4SSatish Balay         cone[3] = 31;
20169371c9d4SSatish Balay         cone[4] = 30;
20179371c9d4SSatish Balay         cone[5] = 19;
20189371c9d4SSatish Balay         cone[6] = 20;
20199371c9d4SSatish Balay         cone[7] = 28;
20209566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 8, cone));
20219371c9d4SSatish Balay         cone[0] = 35;
20229371c9d4SSatish Balay         cone[1] = 31;
20239371c9d4SSatish Balay         cone[2] = 32;
20249371c9d4SSatish Balay         cone[3] = 36;
20259371c9d4SSatish Balay         cone[4] = 30;
20269371c9d4SSatish Balay         cone[5] = 25;
20279371c9d4SSatish Balay         cone[6] = 22;
20289371c9d4SSatish Balay         cone[7] = 19;
20299566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 9, cone));
2030ae8bcbbbSMatthew G. Knepley 
20319371c9d4SSatish Balay         cone[0] = 19;
20329371c9d4SSatish Balay         cone[1] = 20;
20339371c9d4SSatish Balay         cone[2] = 21;
20349371c9d4SSatish Balay         cone[3] = 22;
20359371c9d4SSatish Balay         cone[4] = 15;
20369371c9d4SSatish Balay         cone[5] = 16;
20379371c9d4SSatish Balay         cone[6] = 17;
20389371c9d4SSatish Balay         cone[7] = 18;
20399566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 10, cone));
20409371c9d4SSatish Balay         cone[0] = 22;
20419371c9d4SSatish Balay         cone[1] = 21;
20429371c9d4SSatish Balay         cone[2] = 26;
20439371c9d4SSatish Balay         cone[3] = 25;
20449371c9d4SSatish Balay         cone[4] = 16;
20459371c9d4SSatish Balay         cone[5] = 23;
20469371c9d4SSatish Balay         cone[6] = 24;
20479371c9d4SSatish Balay         cone[7] = 17;
20489566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 11, cone));
20499371c9d4SSatish Balay         cone[0] = 20;
20509371c9d4SSatish Balay         cone[1] = 28;
20519371c9d4SSatish Balay         cone[2] = 26;
20529371c9d4SSatish Balay         cone[3] = 21;
20539371c9d4SSatish Balay         cone[4] = 18;
20549371c9d4SSatish Balay         cone[5] = 17;
20559371c9d4SSatish Balay         cone[6] = 24;
20569371c9d4SSatish Balay         cone[7] = 27;
20579566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 12, cone));
20589371c9d4SSatish Balay         cone[0] = 30;
20599371c9d4SSatish Balay         cone[1] = 28;
20609371c9d4SSatish Balay         cone[2] = 20;
20619371c9d4SSatish Balay         cone[3] = 19;
20629371c9d4SSatish Balay         cone[4] = 29;
20639371c9d4SSatish Balay         cone[5] = 15;
20649371c9d4SSatish Balay         cone[6] = 18;
20659371c9d4SSatish Balay         cone[7] = 27;
20669566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 13, cone));
20679371c9d4SSatish Balay         cone[0] = 30;
20689371c9d4SSatish Balay         cone[1] = 19;
20699371c9d4SSatish Balay         cone[2] = 22;
20709371c9d4SSatish Balay         cone[3] = 25;
20719371c9d4SSatish Balay         cone[4] = 29;
20729371c9d4SSatish Balay         cone[5] = 23;
20739371c9d4SSatish Balay         cone[6] = 16;
20749371c9d4SSatish Balay         cone[7] = 15;
20759566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 14, cone));
2076006a8963SMatthew G. Knepley       } else {
20779371c9d4SSatish Balay         cone[0] = 5;
20789371c9d4SSatish Balay         cone[1] = 8;
20799371c9d4SSatish Balay         cone[2] = 7;
20809371c9d4SSatish Balay         cone[3] = 6;
20819371c9d4SSatish Balay         cone[4] = 9;
20829371c9d4SSatish Balay         cone[5] = 12;
20839371c9d4SSatish Balay         cone[6] = 11;
20849371c9d4SSatish Balay         cone[7] = 10;
20859566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
20869371c9d4SSatish Balay         cone[0] = 6;
20879371c9d4SSatish Balay         cone[1] = 7;
20889371c9d4SSatish Balay         cone[2] = 14;
20899371c9d4SSatish Balay         cone[3] = 13;
20909371c9d4SSatish Balay         cone[4] = 12;
20919371c9d4SSatish Balay         cone[5] = 15;
20929371c9d4SSatish Balay         cone[6] = 16;
20939371c9d4SSatish Balay         cone[7] = 11;
20949566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
20959371c9d4SSatish Balay         cone[0] = 8;
20969371c9d4SSatish Balay         cone[1] = 17;
20979371c9d4SSatish Balay         cone[2] = 14;
20989371c9d4SSatish Balay         cone[3] = 7;
20999371c9d4SSatish Balay         cone[4] = 10;
21009371c9d4SSatish Balay         cone[5] = 11;
21019371c9d4SSatish Balay         cone[6] = 16;
21029371c9d4SSatish Balay         cone[7] = 18;
21039566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
21049371c9d4SSatish Balay         cone[0] = 19;
21059371c9d4SSatish Balay         cone[1] = 17;
21069371c9d4SSatish Balay         cone[2] = 8;
21079371c9d4SSatish Balay         cone[3] = 5;
21089371c9d4SSatish Balay         cone[4] = 20;
21099371c9d4SSatish Balay         cone[5] = 9;
21109371c9d4SSatish Balay         cone[6] = 10;
21119371c9d4SSatish Balay         cone[7] = 18;
21129566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
21139371c9d4SSatish Balay         cone[0] = 19;
21149371c9d4SSatish Balay         cone[1] = 5;
21159371c9d4SSatish Balay         cone[2] = 6;
21169371c9d4SSatish Balay         cone[3] = 13;
21179371c9d4SSatish Balay         cone[4] = 20;
21189371c9d4SSatish Balay         cone[5] = 15;
21199371c9d4SSatish Balay         cone[6] = 12;
21209371c9d4SSatish Balay         cone[7] = 9;
21219566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
2122006a8963SMatthew G. Knepley       }
2123d8c47e87SMatthew G. Knepley     }
21249566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
21259566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
21260510c589SMatthew G. Knepley   }
2127dbc1dc17SMatthew G. Knepley   /* Create cube geometry */
21280510c589SMatthew G. Knepley   {
21290510c589SMatthew G. Knepley     Vec             coordinates;
21300510c589SMatthew G. Knepley     PetscSection    coordSection;
21310510c589SMatthew G. Knepley     PetscScalar    *coords;
21320510c589SMatthew G. Knepley     PetscInt        coordSize, v;
21330510c589SMatthew G. Knepley     const PetscReal dis = 1.0 / PetscSqrtReal(2.0);
21340510c589SMatthew G. Knepley     const PetscReal ds2 = dis / 2.0;
21350510c589SMatthew G. Knepley 
21360510c589SMatthew G. Knepley     /* Build coordinates */
21379566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
21389566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
21399566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
21409566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices));
21410510c589SMatthew G. Knepley     for (v = numCells; v < numCells + numVertices; ++v) {
21429566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
21439566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
21440510c589SMatthew G. Knepley     }
21459566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
21469566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
21479566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
21489566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
21499566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
21509566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
21519566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates, VECSTANDARD));
21529566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
2153dd400576SPatrick Sanan     if (rank == 0) {
21549371c9d4SSatish Balay       coords[0 * dim + 0]  = -ds2;
21559371c9d4SSatish Balay       coords[0 * dim + 1]  = -ds2;
21569371c9d4SSatish Balay       coords[0 * dim + 2]  = 0.0;
21579371c9d4SSatish Balay       coords[1 * dim + 0]  = ds2;
21589371c9d4SSatish Balay       coords[1 * dim + 1]  = -ds2;
21599371c9d4SSatish Balay       coords[1 * dim + 2]  = 0.0;
21609371c9d4SSatish Balay       coords[2 * dim + 0]  = ds2;
21619371c9d4SSatish Balay       coords[2 * dim + 1]  = ds2;
21629371c9d4SSatish Balay       coords[2 * dim + 2]  = 0.0;
21639371c9d4SSatish Balay       coords[3 * dim + 0]  = -ds2;
21649371c9d4SSatish Balay       coords[3 * dim + 1]  = ds2;
21659371c9d4SSatish Balay       coords[3 * dim + 2]  = 0.0;
21669371c9d4SSatish Balay       coords[4 * dim + 0]  = -ds2;
21679371c9d4SSatish Balay       coords[4 * dim + 1]  = -ds2;
21689371c9d4SSatish Balay       coords[4 * dim + 2]  = 1.0;
21699371c9d4SSatish Balay       coords[5 * dim + 0]  = -ds2;
21709371c9d4SSatish Balay       coords[5 * dim + 1]  = ds2;
21719371c9d4SSatish Balay       coords[5 * dim + 2]  = 1.0;
21729371c9d4SSatish Balay       coords[6 * dim + 0]  = ds2;
21739371c9d4SSatish Balay       coords[6 * dim + 1]  = ds2;
21749371c9d4SSatish Balay       coords[6 * dim + 2]  = 1.0;
21759371c9d4SSatish Balay       coords[7 * dim + 0]  = ds2;
21769371c9d4SSatish Balay       coords[7 * dim + 1]  = -ds2;
21779371c9d4SSatish Balay       coords[7 * dim + 2]  = 1.0;
21789371c9d4SSatish Balay       coords[8 * dim + 0]  = dis;
21799371c9d4SSatish Balay       coords[8 * dim + 1]  = -dis;
21809371c9d4SSatish Balay       coords[8 * dim + 2]  = 0.0;
21819371c9d4SSatish Balay       coords[9 * dim + 0]  = dis;
21829371c9d4SSatish Balay       coords[9 * dim + 1]  = dis;
21839371c9d4SSatish Balay       coords[9 * dim + 2]  = 0.0;
21849371c9d4SSatish Balay       coords[10 * dim + 0] = dis;
21859371c9d4SSatish Balay       coords[10 * dim + 1] = -dis;
21869371c9d4SSatish Balay       coords[10 * dim + 2] = 1.0;
21879371c9d4SSatish Balay       coords[11 * dim + 0] = dis;
21889371c9d4SSatish Balay       coords[11 * dim + 1] = dis;
21899371c9d4SSatish Balay       coords[11 * dim + 2] = 1.0;
21909371c9d4SSatish Balay       coords[12 * dim + 0] = -dis;
21919371c9d4SSatish Balay       coords[12 * dim + 1] = dis;
21929371c9d4SSatish Balay       coords[12 * dim + 2] = 0.0;
21939371c9d4SSatish Balay       coords[13 * dim + 0] = -dis;
21949371c9d4SSatish Balay       coords[13 * dim + 1] = dis;
21959371c9d4SSatish Balay       coords[13 * dim + 2] = 1.0;
21969371c9d4SSatish Balay       coords[14 * dim + 0] = -dis;
21979371c9d4SSatish Balay       coords[14 * dim + 1] = -dis;
21989371c9d4SSatish Balay       coords[14 * dim + 2] = 0.0;
21999371c9d4SSatish Balay       coords[15 * dim + 0] = -dis;
22009371c9d4SSatish Balay       coords[15 * dim + 1] = -dis;
22019371c9d4SSatish Balay       coords[15 * dim + 2] = 1.0;
2202ae8bcbbbSMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
22039371c9d4SSatish Balay         /* 15 31 19 */ coords[16 * dim + 0] = -ds2;
22049371c9d4SSatish Balay         coords[16 * dim + 1]                = -ds2;
22059371c9d4SSatish Balay         coords[16 * dim + 2]                = 0.5;
22069371c9d4SSatish Balay         /* 16 32 22 */ coords[17 * dim + 0] = ds2;
22079371c9d4SSatish Balay         coords[17 * dim + 1]                = -ds2;
22089371c9d4SSatish Balay         coords[17 * dim + 2]                = 0.5;
22099371c9d4SSatish Balay         /* 17 33 21 */ coords[18 * dim + 0] = ds2;
22109371c9d4SSatish Balay         coords[18 * dim + 1]                = ds2;
22119371c9d4SSatish Balay         coords[18 * dim + 2]                = 0.5;
22129371c9d4SSatish Balay         /* 18 34 20 */ coords[19 * dim + 0] = -ds2;
22139371c9d4SSatish Balay         coords[19 * dim + 1]                = ds2;
22149371c9d4SSatish Balay         coords[19 * dim + 2]                = 0.5;
22159371c9d4SSatish Balay         /* 29 35 30 */ coords[20 * dim + 0] = -dis;
22169371c9d4SSatish Balay         coords[20 * dim + 1]                = -dis;
22179371c9d4SSatish Balay         coords[20 * dim + 2]                = 0.5;
22189371c9d4SSatish Balay         /* 23 36 25 */ coords[21 * dim + 0] = dis;
22199371c9d4SSatish Balay         coords[21 * dim + 1]                = -dis;
22209371c9d4SSatish Balay         coords[21 * dim + 2]                = 0.5;
22219371c9d4SSatish Balay         /* 24 37 26 */ coords[22 * dim + 0] = dis;
22229371c9d4SSatish Balay         coords[22 * dim + 1]                = dis;
22239371c9d4SSatish Balay         coords[22 * dim + 2]                = 0.5;
22249371c9d4SSatish Balay         /* 27 38 28 */ coords[23 * dim + 0] = -dis;
22259371c9d4SSatish Balay         coords[23 * dim + 1]                = dis;
22269371c9d4SSatish Balay         coords[23 * dim + 2]                = 0.5;
2227ae8bcbbbSMatthew G. Knepley       }
2228d8c47e87SMatthew G. Knepley     }
22299566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
22309566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
22319566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
22320510c589SMatthew G. Knepley   }
2233006a8963SMatthew G. Knepley   /* Create periodicity */
2234006a8963SMatthew G. Knepley   if (periodicZ == DM_BOUNDARY_PERIODIC || periodicZ == DM_BOUNDARY_TWIST) {
22356858538eSMatthew G. Knepley     PetscReal L[3]       = {-1., -1., 0.};
22366858538eSMatthew G. Knepley     PetscReal maxCell[3] = {-1., -1., 0.};
2237006a8963SMatthew G. Knepley     PetscReal lower[3]   = {0.0, 0.0, 0.0};
2238ae8bcbbbSMatthew G. Knepley     PetscReal upper[3]   = {1.0, 1.0, 1.5};
22396858538eSMatthew G. Knepley     PetscInt  numZCells  = 3;
2240006a8963SMatthew G. Knepley 
22416858538eSMatthew G. Knepley     L[2]       = upper[2] - lower[2];
22426858538eSMatthew G. Knepley     maxCell[2] = 1.1 * (L[2] / numZCells);
22434fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, maxCell, lower, L));
2244006a8963SMatthew G. Knepley   }
2245dbc1dc17SMatthew G. Knepley   {
22469318fe57SMatthew G. Knepley     DM          cdm;
22479318fe57SMatthew G. Knepley     PetscDS     cds;
22489318fe57SMatthew G. Knepley     PetscScalar c[2] = {1.0, 1.0};
2249dbc1dc17SMatthew G. Knepley 
225049704ca5SMatthew G. Knepley     PetscCall(DMPlexCreateCoordinateSpace(dm, 1, PETSC_TRUE, NULL));
22519566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
22529566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
22539566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, 2, c));
2254dbc1dc17SMatthew G. Knepley   }
225546139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
225646139095SJed Brown 
22579318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
22589566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolateInPlace_Internal(dm));
225949704ca5SMatthew G. Knepley 
226049704ca5SMatthew G. Knepley   char        oldprefix[PETSC_MAX_PATH_LEN];
226149704ca5SMatthew G. Knepley   const char *prefix;
226249704ca5SMatthew G. Knepley 
226349704ca5SMatthew G. Knepley   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, &prefix));
226449704ca5SMatthew G. Knepley   PetscCall(PetscStrncpy(oldprefix, prefix, PETSC_MAX_PATH_LEN));
226549704ca5SMatthew G. Knepley   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, "petsc_cyl_ref_"));
226649704ca5SMatthew G. Knepley   for (PetscInt r = 0; r < PetscMax(0, Nr); ++r) {
226749704ca5SMatthew G. Knepley     DM rdm;
226849704ca5SMatthew G. Knepley 
226949704ca5SMatthew G. Knepley     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &rdm));
227049704ca5SMatthew G. Knepley     PetscCall(DMPlexReplace_Internal(dm, &rdm));
227149704ca5SMatthew G. Knepley   }
227249704ca5SMatthew G. Knepley   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, oldprefix));
227349704ca5SMatthew G. Knepley   PetscCall(DMPlexRemapGeometry(dm, 0.0, snapToCylinder));
227449704ca5SMatthew G. Knepley 
227549704ca5SMatthew G. Knepley   DMLabel         bdlabel, edgelabel;
227649704ca5SMatthew G. Knepley   IS              faceIS;
227749704ca5SMatthew G. Knepley   const PetscInt *faces;
227849704ca5SMatthew G. Knepley   PetscInt        Nf;
227949704ca5SMatthew G. Knepley 
228049704ca5SMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "marker"));
228149704ca5SMatthew G. Knepley   PetscCall(DMGetLabel(dm, "marker", &bdlabel));
228249704ca5SMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "generatrix"));
228349704ca5SMatthew G. Knepley   PetscCall(DMGetLabel(dm, "generatrix", &edgelabel));
228449704ca5SMatthew G. Knepley   PetscCall(DMPlexMarkBoundaryFaces(dm, PETSC_DETERMINE, bdlabel));
228549704ca5SMatthew G. Knepley   // Remove faces on top and bottom
228649704ca5SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(bdlabel, 1, &faceIS));
2287ba1b3593SJeremy L Thompson   if (faceIS) {
228849704ca5SMatthew G. Knepley     PetscCall(ISGetLocalSize(faceIS, &Nf));
228949704ca5SMatthew G. Knepley     PetscCall(ISGetIndices(faceIS, &faces));
229049704ca5SMatthew G. Knepley     for (PetscInt f = 0; f < Nf; ++f) {
229149704ca5SMatthew G. Knepley       PetscReal vol, normal[3];
229249704ca5SMatthew G. Knepley 
229349704ca5SMatthew G. Knepley       PetscCall(DMPlexComputeCellGeometryFVM(dm, faces[f], &vol, NULL, normal));
229449704ca5SMatthew G. Knepley       if (PetscAbsReal(normal[2]) < PETSC_SMALL) PetscCall(DMLabelSetValue(edgelabel, faces[f], 1));
229549704ca5SMatthew G. Knepley     }
229649704ca5SMatthew G. Knepley     PetscCall(ISRestoreIndices(faceIS, &faces));
229749704ca5SMatthew G. Knepley     PetscCall(ISDestroy(&faceIS));
2298ba1b3593SJeremy L Thompson   }
229949704ca5SMatthew G. Knepley   PetscCall(DMPlexLabelComplete(dm, bdlabel));
230049704ca5SMatthew G. Knepley   PetscCall(DMPlexLabelComplete(dm, edgelabel));
23013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
23020510c589SMatthew G. Knepley }
23030510c589SMatthew G. Knepley 
230424119c2aSMatthew G. Knepley /*@
23059318fe57SMatthew G. Knepley   DMPlexCreateHexCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using hexahedra.
230624119c2aSMatthew G. Knepley 
2307d083f849SBarry Smith   Collective
230824119c2aSMatthew G. Knepley 
230924119c2aSMatthew G. Knepley   Input Parameters:
2310a1cb98faSBarry Smith + comm      - The communicator for the `DM` object
231149704ca5SMatthew G. Knepley . periodicZ - The boundary type for the Z direction
231249704ca5SMatthew G. Knepley - Nr        - The number of refinements to carry out
231324119c2aSMatthew G. Knepley 
231424119c2aSMatthew G. Knepley   Output Parameter:
231520f4b53cSBarry Smith . dm - The `DM` object
231624119c2aSMatthew G. Knepley 
231724119c2aSMatthew G. Knepley   Level: beginner
231824119c2aSMatthew G. Knepley 
2319a1cb98faSBarry Smith   Note:
2320a4e35b19SJacob Faibussowitsch   Here is the output numbering looking from the bottom of the cylinder\:
2321a1cb98faSBarry Smith .vb
2322a1cb98faSBarry Smith        17-----14
2323a1cb98faSBarry Smith         |     |
2324a1cb98faSBarry Smith         |  2  |
2325a1cb98faSBarry Smith         |     |
2326a1cb98faSBarry Smith  17-----8-----7-----14
2327a1cb98faSBarry Smith   |     |     |     |
2328a1cb98faSBarry Smith   |  3  |  0  |  1  |
2329a1cb98faSBarry Smith   |     |     |     |
2330a1cb98faSBarry Smith  19-----5-----6-----13
2331a1cb98faSBarry Smith         |     |
2332a1cb98faSBarry Smith         |  4  |
2333a1cb98faSBarry Smith         |     |
2334a1cb98faSBarry Smith        19-----13
2335a1cb98faSBarry Smith 
2336a1cb98faSBarry Smith  and up through the top
2337a1cb98faSBarry Smith 
2338a1cb98faSBarry Smith        18-----16
2339a1cb98faSBarry Smith         |     |
2340a1cb98faSBarry Smith         |  2  |
2341a1cb98faSBarry Smith         |     |
2342a1cb98faSBarry Smith  18----10----11-----16
2343a1cb98faSBarry Smith   |     |     |     |
2344a1cb98faSBarry Smith   |  3  |  0  |  1  |
2345a1cb98faSBarry Smith   |     |     |     |
2346a1cb98faSBarry Smith  20-----9----12-----15
2347a1cb98faSBarry Smith         |     |
2348a1cb98faSBarry Smith         |  4  |
2349a1cb98faSBarry Smith         |     |
2350a1cb98faSBarry Smith        20-----15
2351a1cb98faSBarry Smith .ve
2352a1cb98faSBarry Smith 
23531cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
235424119c2aSMatthew G. Knepley @*/
235549704ca5SMatthew G. Knepley PetscErrorCode DMPlexCreateHexCylinderMesh(MPI_Comm comm, DMBoundaryType periodicZ, PetscInt Nr, DM *dm)
2356d71ae5a4SJacob Faibussowitsch {
23579318fe57SMatthew G. Knepley   PetscFunctionBegin;
235849704ca5SMatthew G. Knepley   PetscAssertPointer(dm, 4);
23599566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
23609566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
236149704ca5SMatthew G. Knepley   PetscCall(DMPlexCreateHexCylinderMesh_Internal(*dm, periodicZ, Nr));
23623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
23639318fe57SMatthew G. Knepley }
23649318fe57SMatthew G. Knepley 
2365d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateWedgeCylinderMesh_Internal(DM dm, PetscInt n, PetscBool interpolate)
2366d71ae5a4SJacob Faibussowitsch {
236724119c2aSMatthew G. Knepley   const PetscInt dim = 3;
2368412e9a14SMatthew G. Knepley   PetscInt       numCells, numVertices, v;
23699fe9f049SMatthew G. Knepley   PetscMPIInt    rank;
237024119c2aSMatthew G. Knepley 
237124119c2aSMatthew G. Knepley   PetscFunctionBegin;
237263a3b9bcSJacob Faibussowitsch   PetscCheck(n >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of wedges %" PetscInt_FMT " cannot be negative", n);
237346139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
23749566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
23759566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
2376412e9a14SMatthew G. Knepley   /* Must create the celltype label here so that we do not automatically try to compute the types */
23779566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "celltype"));
237824119c2aSMatthew G. Knepley   /* Create topology */
237924119c2aSMatthew G. Knepley   {
238024119c2aSMatthew G. Knepley     PetscInt cone[6], c;
238124119c2aSMatthew G. Knepley 
2382dd400576SPatrick Sanan     numCells    = rank == 0 ? n : 0;
2383dd400576SPatrick Sanan     numVertices = rank == 0 ? 2 * (n + 1) : 0;
23849566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
23859566063dSJacob Faibussowitsch     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 6));
23869566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm));
238724119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
23889371c9d4SSatish Balay       cone[0] = c + n * 1;
23899371c9d4SSatish Balay       cone[1] = (c + 1) % n + n * 1;
23909371c9d4SSatish Balay       cone[2] = 0 + 3 * n;
23919371c9d4SSatish Balay       cone[3] = c + n * 2;
23929371c9d4SSatish Balay       cone[4] = (c + 1) % n + n * 2;
23939371c9d4SSatish Balay       cone[5] = 1 + 3 * n;
23949566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(dm, c, cone));
23959566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(dm, c, DM_POLYTOPE_TRI_PRISM_TENSOR));
239624119c2aSMatthew G. Knepley     }
23979566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
23989566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
239924119c2aSMatthew G. Knepley   }
240048a46eb9SPierre Jolivet   for (v = numCells; v < numCells + numVertices; ++v) PetscCall(DMPlexSetCellType(dm, v, DM_POLYTOPE_POINT));
240124119c2aSMatthew G. Knepley   /* Create cylinder geometry */
240224119c2aSMatthew G. Knepley   {
240324119c2aSMatthew G. Knepley     Vec          coordinates;
240424119c2aSMatthew G. Knepley     PetscSection coordSection;
240524119c2aSMatthew G. Knepley     PetscScalar *coords;
2406412e9a14SMatthew G. Knepley     PetscInt     coordSize, c;
240724119c2aSMatthew G. Knepley 
240824119c2aSMatthew G. Knepley     /* Build coordinates */
24099566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
24109566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
24119566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
24129566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices));
241324119c2aSMatthew G. Knepley     for (v = numCells; v < numCells + numVertices; ++v) {
24149566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
24159566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
241624119c2aSMatthew G. Knepley     }
24179566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
24189566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
24199566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
24209566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
24219566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
24229566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
24239566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates, VECSTANDARD));
24249566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
242524119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
24269371c9d4SSatish Balay       coords[(c + 0 * n) * dim + 0] = PetscCosReal(2.0 * c * PETSC_PI / n);
24279371c9d4SSatish Balay       coords[(c + 0 * n) * dim + 1] = PetscSinReal(2.0 * c * PETSC_PI / n);
24289371c9d4SSatish Balay       coords[(c + 0 * n) * dim + 2] = 1.0;
24299371c9d4SSatish Balay       coords[(c + 1 * n) * dim + 0] = PetscCosReal(2.0 * c * PETSC_PI / n);
24309371c9d4SSatish Balay       coords[(c + 1 * n) * dim + 1] = PetscSinReal(2.0 * c * PETSC_PI / n);
24319371c9d4SSatish Balay       coords[(c + 1 * n) * dim + 2] = 0.0;
243224119c2aSMatthew G. Knepley     }
2433dd400576SPatrick Sanan     if (rank == 0) {
24349371c9d4SSatish Balay       coords[(2 * n + 0) * dim + 0] = 0.0;
24359371c9d4SSatish Balay       coords[(2 * n + 0) * dim + 1] = 0.0;
24369371c9d4SSatish Balay       coords[(2 * n + 0) * dim + 2] = 1.0;
24379371c9d4SSatish Balay       coords[(2 * n + 1) * dim + 0] = 0.0;
24389371c9d4SSatish Balay       coords[(2 * n + 1) * dim + 1] = 0.0;
24399371c9d4SSatish Balay       coords[(2 * n + 1) * dim + 2] = 0.0;
24409fe9f049SMatthew G. Knepley     }
24419566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
24429566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
24439566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
244424119c2aSMatthew G. Knepley   }
244546139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
24469318fe57SMatthew G. Knepley   /* Interpolate */
24479566063dSJacob Faibussowitsch   if (interpolate) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
24483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24499318fe57SMatthew G. Knepley }
24509318fe57SMatthew G. Knepley 
24519318fe57SMatthew G. Knepley /*@
24529318fe57SMatthew G. Knepley   DMPlexCreateWedgeCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using wedges.
24539318fe57SMatthew G. Knepley 
24549318fe57SMatthew G. Knepley   Collective
24559318fe57SMatthew G. Knepley 
24569318fe57SMatthew G. Knepley   Input Parameters:
2457a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
24589318fe57SMatthew G. Knepley . n           - The number of wedges around the origin
24599318fe57SMatthew G. Knepley - interpolate - Create edges and faces
24609318fe57SMatthew G. Knepley 
24619318fe57SMatthew G. Knepley   Output Parameter:
2462a1cb98faSBarry Smith . dm - The `DM` object
24639318fe57SMatthew G. Knepley 
24649318fe57SMatthew G. Knepley   Level: beginner
24659318fe57SMatthew G. Knepley 
24661cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateHexCylinderMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
24679318fe57SMatthew G. Knepley @*/
2468d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateWedgeCylinderMesh(MPI_Comm comm, PetscInt n, PetscBool interpolate, DM *dm)
2469d71ae5a4SJacob Faibussowitsch {
24709318fe57SMatthew G. Knepley   PetscFunctionBegin;
24714f572ea9SToby Isaac   PetscAssertPointer(dm, 4);
24729566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
24739566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
24749566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateWedgeCylinderMesh_Internal(*dm, n, interpolate));
24753ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
247624119c2aSMatthew G. Knepley }
247724119c2aSMatthew G. Knepley 
2478d71ae5a4SJacob Faibussowitsch static inline PetscReal DiffNormReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
2479d71ae5a4SJacob Faibussowitsch {
248065a81367SMatthew G. Knepley   PetscReal prod = 0.0;
248165a81367SMatthew G. Knepley   PetscInt  i;
248265a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += PetscSqr(x[i] - y[i]);
248365a81367SMatthew G. Knepley   return PetscSqrtReal(prod);
248465a81367SMatthew G. Knepley }
2485dd2b43ebSStefano Zampini 
2486d71ae5a4SJacob Faibussowitsch static inline PetscReal DotReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
2487d71ae5a4SJacob Faibussowitsch {
248865a81367SMatthew G. Knepley   PetscReal prod = 0.0;
248965a81367SMatthew G. Knepley   PetscInt  i;
249065a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += x[i] * y[i];
249165a81367SMatthew G. Knepley   return prod;
249265a81367SMatthew G. Knepley }
249365a81367SMatthew G. Knepley 
249451a74b61SMatthew G. Knepley /* The first constant is the sphere radius */
2495d71ae5a4SJacob 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[])
2496d71ae5a4SJacob Faibussowitsch {
249751a74b61SMatthew G. Knepley   PetscReal r     = PetscRealPart(constants[0]);
249851a74b61SMatthew G. Knepley   PetscReal norm2 = 0.0, fac;
249951a74b61SMatthew G. Knepley   PetscInt  n     = uOff[1] - uOff[0], d;
250051a74b61SMatthew G. Knepley 
250151a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) norm2 += PetscSqr(PetscRealPart(u[d]));
250251a74b61SMatthew G. Knepley   fac = r / PetscSqrtReal(norm2);
250351a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) f0[d] = u[d] * fac;
250451a74b61SMatthew G. Knepley }
250551a74b61SMatthew G. Knepley 
2506d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSphereMesh_Internal(DM dm, PetscInt dim, PetscBool simplex, PetscReal R)
2507d71ae5a4SJacob Faibussowitsch {
250865a81367SMatthew G. Knepley   const PetscInt embedDim = dim + 1;
250965a81367SMatthew G. Knepley   PetscSection   coordSection;
251065a81367SMatthew G. Knepley   Vec            coordinates;
251165a81367SMatthew G. Knepley   PetscScalar   *coords;
251265a81367SMatthew G. Knepley   PetscReal     *coordsIn;
251307c565c5SJose E. Roman   PetscInt       numCells, numEdges, numVerts = 0, firstVertex = 0, v, firstEdge, coordSize, d, e;
251465a81367SMatthew G. Knepley   PetscMPIInt    rank;
251565a81367SMatthew G. Knepley 
251665a81367SMatthew G. Knepley   PetscFunctionBegin;
25179318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveBool(dm, simplex, 3);
251846139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
25199566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
25209566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, dim + 1));
25219566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
252265a81367SMatthew G. Knepley   switch (dim) {
25235c344501SMatthew G. Knepley   case 1:
25245c344501SMatthew G. Knepley     numCells = 16;
25255c344501SMatthew G. Knepley     numVerts = numCells;
25265c344501SMatthew G. Knepley 
25275c344501SMatthew G. Knepley     // Build Topology
25285c344501SMatthew G. Knepley     PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts));
25295c344501SMatthew G. Knepley     for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim));
25305c344501SMatthew G. Knepley     PetscCall(DMSetUp(dm));
25315c344501SMatthew G. Knepley     for (PetscInt c = 0; c < numCells; ++c) {
25325c344501SMatthew G. Knepley       PetscInt cone[2];
25335c344501SMatthew G. Knepley 
25345c344501SMatthew G. Knepley       cone[0] = c + numCells;
25355c344501SMatthew G. Knepley       cone[1] = (c + 1) % numVerts + numCells;
25365c344501SMatthew G. Knepley       PetscCall(DMPlexSetCone(dm, c, cone));
25375c344501SMatthew G. Knepley     }
25385c344501SMatthew G. Knepley     PetscCall(DMPlexSymmetrize(dm));
25395c344501SMatthew G. Knepley     PetscCall(DMPlexStratify(dm));
25405c344501SMatthew G. Knepley     PetscCall(PetscMalloc1(numVerts * embedDim, &coordsIn));
25415c344501SMatthew G. Knepley     for (PetscInt v = 0; v < numVerts; ++v) {
25425c344501SMatthew G. Knepley       const PetscReal rad = 2. * PETSC_PI * v / numVerts;
25435c344501SMatthew G. Knepley 
25445c344501SMatthew G. Knepley       coordsIn[v * embedDim + 0] = PetscCosReal(rad);
25455c344501SMatthew G. Knepley       coordsIn[v * embedDim + 1] = PetscSinReal(rad);
25465c344501SMatthew G. Knepley     }
25475c344501SMatthew G. Knepley     break;
254865a81367SMatthew G. Knepley   case 2:
254965a81367SMatthew G. Knepley     if (simplex) {
255051a74b61SMatthew G. Knepley       const PetscReal radius    = PetscSqrtReal(1 + PETSC_PHI * PETSC_PHI) / (1.0 + PETSC_PHI);
255151a74b61SMatthew G. Knepley       const PetscReal edgeLen   = 2.0 / (1.0 + PETSC_PHI) * (R / radius);
255265a81367SMatthew G. Knepley       const PetscInt  degree    = 5;
255351a74b61SMatthew G. Knepley       PetscReal       vertex[3] = {0.0, 1.0 / (1.0 + PETSC_PHI), PETSC_PHI / (1.0 + PETSC_PHI)};
255465a81367SMatthew G. Knepley       PetscInt        s[3]      = {1, 1, 1};
255565a81367SMatthew G. Knepley       PetscInt        cone[3];
255607c565c5SJose E. Roman       PetscInt       *graph;
255765a81367SMatthew G. Knepley 
25589371c9d4SSatish Balay       vertex[0] *= R / radius;
25599371c9d4SSatish Balay       vertex[1] *= R / radius;
25609371c9d4SSatish Balay       vertex[2] *= R / radius;
2561dd400576SPatrick Sanan       numCells    = rank == 0 ? 20 : 0;
2562dd400576SPatrick Sanan       numVerts    = rank == 0 ? 12 : 0;
256365a81367SMatthew G. Knepley       firstVertex = numCells;
256451a74b61SMatthew G. Knepley       /* Use icosahedron, which for a R-sphere has coordinates which are all cyclic permutations of
256565a81367SMatthew G. Knepley 
256665a81367SMatthew G. Knepley            (0, \pm 1/\phi+1, \pm \phi/\phi+1)
256765a81367SMatthew G. Knepley 
256865a81367SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
256951a74b61SMatthew G. Knepley          length is then given by 2/(1+\phi) = 2 * 0.38197 = 0.76393.
257065a81367SMatthew G. Knepley       */
257165a81367SMatthew G. Knepley       /* Construct vertices */
25729566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
2573dd400576SPatrick Sanan       if (rank == 0) {
257407c565c5SJose E. Roman         for (PetscInt p = 0, i = 0; p < embedDim; ++p) {
257565a81367SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
257665a81367SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
257765a81367SMatthew G. Knepley               for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[(d + p) % embedDim] * vertex[(d + p) % embedDim];
257865a81367SMatthew G. Knepley               ++i;
257965a81367SMatthew G. Knepley             }
258065a81367SMatthew G. Knepley           }
258165a81367SMatthew G. Knepley         }
258245da822fSValeria Barra       }
258365a81367SMatthew G. Knepley       /* Construct graph */
25849566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * numVerts, &graph));
258507c565c5SJose E. Roman       for (PetscInt i = 0; i < numVerts; ++i) {
258607c565c5SJose E. Roman         PetscInt k = 0;
258707c565c5SJose E. Roman         for (PetscInt j = 0; j < numVerts; ++j) {
25889371c9d4SSatish Balay           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i * embedDim], &coordsIn[j * embedDim]) - edgeLen) < PETSC_SMALL) {
25899371c9d4SSatish Balay             graph[i * numVerts + j] = 1;
25909371c9d4SSatish Balay             ++k;
25919371c9d4SSatish Balay           }
259265a81367SMatthew G. Knepley         }
259363a3b9bcSJacob Faibussowitsch         PetscCheck(k == degree, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid icosahedron, vertex %" PetscInt_FMT " degree %" PetscInt_FMT " != %" PetscInt_FMT, i, k, degree);
259465a81367SMatthew G. Knepley       }
259565a81367SMatthew G. Knepley       /* Build Topology */
25969566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts));
259707c565c5SJose E. Roman       for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim));
25989566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
259965a81367SMatthew G. Knepley       /* Cells */
260007c565c5SJose E. Roman       for (PetscInt i = 0, c = 0; i < numVerts; ++i) {
260107c565c5SJose E. Roman         for (PetscInt j = 0; j < i; ++j) {
260207c565c5SJose E. Roman           for (PetscInt k = 0; k < j; ++k) {
260365a81367SMatthew G. Knepley             if (graph[i * numVerts + j] && graph[j * numVerts + k] && graph[k * numVerts + i]) {
26049371c9d4SSatish Balay               cone[0] = firstVertex + i;
26059371c9d4SSatish Balay               cone[1] = firstVertex + j;
26069371c9d4SSatish Balay               cone[2] = firstVertex + k;
260765a81367SMatthew G. Knepley               /* Check orientation */
260865a81367SMatthew G. Knepley               {
26099371c9d4SSatish Balay                 const PetscInt epsilon[3][3][3] = {
26109371c9d4SSatish Balay                   {{0, 0, 0},  {0, 0, 1},  {0, -1, 0}},
26119371c9d4SSatish Balay                   {{0, 0, -1}, {0, 0, 0},  {1, 0, 0} },
26129371c9d4SSatish Balay                   {{0, 1, 0},  {-1, 0, 0}, {0, 0, 0} }
26139371c9d4SSatish Balay                 };
261465a81367SMatthew G. Knepley                 PetscReal normal[3];
261565a81367SMatthew G. Knepley                 PetscInt  e, f;
261665a81367SMatthew G. Knepley 
261765a81367SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) {
261865a81367SMatthew G. Knepley                   normal[d] = 0.0;
261965a81367SMatthew G. Knepley                   for (e = 0; e < embedDim; ++e) {
2620ad540459SPierre 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]);
262165a81367SMatthew G. Knepley                   }
262265a81367SMatthew G. Knepley                 }
26239371c9d4SSatish Balay                 if (DotReal(embedDim, normal, &coordsIn[i * embedDim]) < 0) {
26249371c9d4SSatish Balay                   PetscInt tmp = cone[1];
26259371c9d4SSatish Balay                   cone[1]      = cone[2];
26269371c9d4SSatish Balay                   cone[2]      = tmp;
262765a81367SMatthew G. Knepley                 }
262865a81367SMatthew G. Knepley               }
26299566063dSJacob Faibussowitsch               PetscCall(DMPlexSetCone(dm, c++, cone));
263065a81367SMatthew G. Knepley             }
263165a81367SMatthew G. Knepley           }
263265a81367SMatthew G. Knepley         }
263365a81367SMatthew G. Knepley       }
26349566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
26359566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
26369566063dSJacob Faibussowitsch       PetscCall(PetscFree(graph));
263765a81367SMatthew G. Knepley     } else {
26382829fed8SMatthew G. Knepley       /*
26392829fed8SMatthew G. Knepley         12-21--13
26402829fed8SMatthew G. Knepley          |     |
26412829fed8SMatthew G. Knepley         25  4  24
26422829fed8SMatthew G. Knepley          |     |
26432829fed8SMatthew G. Knepley   12-25--9-16--8-24--13
26442829fed8SMatthew G. Knepley    |     |     |     |
26452829fed8SMatthew G. Knepley   23  5 17  0 15  3  22
26462829fed8SMatthew G. Knepley    |     |     |     |
26472829fed8SMatthew G. Knepley   10-20--6-14--7-19--11
26482829fed8SMatthew G. Knepley          |     |
26492829fed8SMatthew G. Knepley         20  1  19
26502829fed8SMatthew G. Knepley          |     |
26512829fed8SMatthew G. Knepley         10-18--11
26522829fed8SMatthew G. Knepley          |     |
26532829fed8SMatthew G. Knepley         23  2  22
26542829fed8SMatthew G. Knepley          |     |
26552829fed8SMatthew G. Knepley         12-21--13
26562829fed8SMatthew G. Knepley        */
26572829fed8SMatthew G. Knepley       PetscInt cone[4], ornt[4];
26582829fed8SMatthew G. Knepley 
2659dd400576SPatrick Sanan       numCells    = rank == 0 ? 6 : 0;
2660dd400576SPatrick Sanan       numEdges    = rank == 0 ? 12 : 0;
2661dd400576SPatrick Sanan       numVerts    = rank == 0 ? 8 : 0;
266265a81367SMatthew G. Knepley       firstVertex = numCells;
266365a81367SMatthew G. Knepley       firstEdge   = numCells + numVerts;
26642829fed8SMatthew G. Knepley       /* Build Topology */
26659566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells + numEdges + numVerts));
266607c565c5SJose E. Roman       for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 4));
266748a46eb9SPierre Jolivet       for (e = firstEdge; e < firstEdge + numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2));
26689566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
2669dd400576SPatrick Sanan       if (rank == 0) {
26702829fed8SMatthew G. Knepley         /* Cell 0 */
26719371c9d4SSatish Balay         cone[0] = 14;
26729371c9d4SSatish Balay         cone[1] = 15;
26739371c9d4SSatish Balay         cone[2] = 16;
26749371c9d4SSatish Balay         cone[3] = 17;
26759566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
26769371c9d4SSatish Balay         ornt[0] = 0;
26779371c9d4SSatish Balay         ornt[1] = 0;
26789371c9d4SSatish Balay         ornt[2] = 0;
26799371c9d4SSatish Balay         ornt[3] = 0;
26809566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 0, ornt));
26812829fed8SMatthew G. Knepley         /* Cell 1 */
26829371c9d4SSatish Balay         cone[0] = 18;
26839371c9d4SSatish Balay         cone[1] = 19;
26849371c9d4SSatish Balay         cone[2] = 14;
26859371c9d4SSatish Balay         cone[3] = 20;
26869566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
26879371c9d4SSatish Balay         ornt[0] = 0;
26889371c9d4SSatish Balay         ornt[1] = 0;
26899371c9d4SSatish Balay         ornt[2] = -1;
26909371c9d4SSatish Balay         ornt[3] = 0;
26919566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 1, ornt));
26922829fed8SMatthew G. Knepley         /* Cell 2 */
26939371c9d4SSatish Balay         cone[0] = 21;
26949371c9d4SSatish Balay         cone[1] = 22;
26959371c9d4SSatish Balay         cone[2] = 18;
26969371c9d4SSatish Balay         cone[3] = 23;
26979566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
26989371c9d4SSatish Balay         ornt[0] = 0;
26999371c9d4SSatish Balay         ornt[1] = 0;
27009371c9d4SSatish Balay         ornt[2] = -1;
27019371c9d4SSatish Balay         ornt[3] = 0;
27029566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 2, ornt));
27032829fed8SMatthew G. Knepley         /* Cell 3 */
27049371c9d4SSatish Balay         cone[0] = 19;
27059371c9d4SSatish Balay         cone[1] = 22;
27069371c9d4SSatish Balay         cone[2] = 24;
27079371c9d4SSatish Balay         cone[3] = 15;
27089566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
27099371c9d4SSatish Balay         ornt[0] = -1;
27109371c9d4SSatish Balay         ornt[1] = -1;
27119371c9d4SSatish Balay         ornt[2] = 0;
27129371c9d4SSatish Balay         ornt[3] = -1;
27139566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 3, ornt));
27142829fed8SMatthew G. Knepley         /* Cell 4 */
27159371c9d4SSatish Balay         cone[0] = 16;
27169371c9d4SSatish Balay         cone[1] = 24;
27179371c9d4SSatish Balay         cone[2] = 21;
27189371c9d4SSatish Balay         cone[3] = 25;
27199566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
27209371c9d4SSatish Balay         ornt[0] = -1;
27219371c9d4SSatish Balay         ornt[1] = -1;
27229371c9d4SSatish Balay         ornt[2] = -1;
27239371c9d4SSatish Balay         ornt[3] = 0;
27249566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 4, ornt));
27252829fed8SMatthew G. Knepley         /* Cell 5 */
27269371c9d4SSatish Balay         cone[0] = 20;
27279371c9d4SSatish Balay         cone[1] = 17;
27289371c9d4SSatish Balay         cone[2] = 25;
27299371c9d4SSatish Balay         cone[3] = 23;
27309566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 5, cone));
27319371c9d4SSatish Balay         ornt[0] = -1;
27329371c9d4SSatish Balay         ornt[1] = -1;
27339371c9d4SSatish Balay         ornt[2] = -1;
27349371c9d4SSatish Balay         ornt[3] = -1;
27359566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 5, ornt));
27362829fed8SMatthew G. Knepley         /* Edges */
27379371c9d4SSatish Balay         cone[0] = 6;
27389371c9d4SSatish Balay         cone[1] = 7;
27399566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 14, cone));
27409371c9d4SSatish Balay         cone[0] = 7;
27419371c9d4SSatish Balay         cone[1] = 8;
27429566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 15, cone));
27439371c9d4SSatish Balay         cone[0] = 8;
27449371c9d4SSatish Balay         cone[1] = 9;
27459566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 16, cone));
27469371c9d4SSatish Balay         cone[0] = 9;
27479371c9d4SSatish Balay         cone[1] = 6;
27489566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 17, cone));
27499371c9d4SSatish Balay         cone[0] = 10;
27509371c9d4SSatish Balay         cone[1] = 11;
27519566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 18, cone));
27529371c9d4SSatish Balay         cone[0] = 11;
27539371c9d4SSatish Balay         cone[1] = 7;
27549566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 19, cone));
27559371c9d4SSatish Balay         cone[0] = 6;
27569371c9d4SSatish Balay         cone[1] = 10;
27579566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 20, cone));
27589371c9d4SSatish Balay         cone[0] = 12;
27599371c9d4SSatish Balay         cone[1] = 13;
27609566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 21, cone));
27619371c9d4SSatish Balay         cone[0] = 13;
27629371c9d4SSatish Balay         cone[1] = 11;
27639566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 22, cone));
27649371c9d4SSatish Balay         cone[0] = 10;
27659371c9d4SSatish Balay         cone[1] = 12;
27669566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 23, cone));
27679371c9d4SSatish Balay         cone[0] = 13;
27689371c9d4SSatish Balay         cone[1] = 8;
27699566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 24, cone));
27709371c9d4SSatish Balay         cone[0] = 12;
27719371c9d4SSatish Balay         cone[1] = 9;
27729566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 25, cone));
277345da822fSValeria Barra       }
27749566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
27759566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
27762829fed8SMatthew G. Knepley       /* Build coordinates */
27779566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
2778dd400576SPatrick Sanan       if (rank == 0) {
27799371c9d4SSatish Balay         coordsIn[0 * embedDim + 0] = -R;
27809371c9d4SSatish Balay         coordsIn[0 * embedDim + 1] = R;
27819371c9d4SSatish Balay         coordsIn[0 * embedDim + 2] = -R;
27829371c9d4SSatish Balay         coordsIn[1 * embedDim + 0] = R;
27839371c9d4SSatish Balay         coordsIn[1 * embedDim + 1] = R;
27849371c9d4SSatish Balay         coordsIn[1 * embedDim + 2] = -R;
27859371c9d4SSatish Balay         coordsIn[2 * embedDim + 0] = R;
27869371c9d4SSatish Balay         coordsIn[2 * embedDim + 1] = -R;
27879371c9d4SSatish Balay         coordsIn[2 * embedDim + 2] = -R;
27889371c9d4SSatish Balay         coordsIn[3 * embedDim + 0] = -R;
27899371c9d4SSatish Balay         coordsIn[3 * embedDim + 1] = -R;
27909371c9d4SSatish Balay         coordsIn[3 * embedDim + 2] = -R;
27919371c9d4SSatish Balay         coordsIn[4 * embedDim + 0] = -R;
27929371c9d4SSatish Balay         coordsIn[4 * embedDim + 1] = R;
27939371c9d4SSatish Balay         coordsIn[4 * embedDim + 2] = R;
27949371c9d4SSatish Balay         coordsIn[5 * embedDim + 0] = R;
27959371c9d4SSatish Balay         coordsIn[5 * embedDim + 1] = R;
27969371c9d4SSatish Balay         coordsIn[5 * embedDim + 2] = R;
27979371c9d4SSatish Balay         coordsIn[6 * embedDim + 0] = -R;
27989371c9d4SSatish Balay         coordsIn[6 * embedDim + 1] = -R;
27999371c9d4SSatish Balay         coordsIn[6 * embedDim + 2] = R;
28009371c9d4SSatish Balay         coordsIn[7 * embedDim + 0] = R;
28019371c9d4SSatish Balay         coordsIn[7 * embedDim + 1] = -R;
28029371c9d4SSatish Balay         coordsIn[7 * embedDim + 2] = R;
280365a81367SMatthew G. Knepley       }
280445da822fSValeria Barra     }
280565a81367SMatthew G. Knepley     break;
280665a81367SMatthew G. Knepley   case 3:
2807116ded15SMatthew G. Knepley     if (simplex) {
2808116ded15SMatthew G. Knepley       const PetscReal edgeLen         = 1.0 / PETSC_PHI;
280951a74b61SMatthew G. Knepley       PetscReal       vertexA[4]      = {0.5, 0.5, 0.5, 0.5};
281051a74b61SMatthew G. Knepley       PetscReal       vertexB[4]      = {1.0, 0.0, 0.0, 0.0};
281151a74b61SMatthew G. Knepley       PetscReal       vertexC[4]      = {0.5, 0.5 * PETSC_PHI, 0.5 / PETSC_PHI, 0.0};
2812116ded15SMatthew G. Knepley       const PetscInt  degree          = 12;
2813116ded15SMatthew G. Knepley       PetscInt        s[4]            = {1, 1, 1};
28149371c9d4SSatish Balay       PetscInt        evenPerm[12][4] = {
28159371c9d4SSatish Balay         {0, 1, 2, 3},
28169371c9d4SSatish Balay         {0, 2, 3, 1},
28179371c9d4SSatish Balay         {0, 3, 1, 2},
28189371c9d4SSatish Balay         {1, 0, 3, 2},
28199371c9d4SSatish Balay         {1, 2, 0, 3},
28209371c9d4SSatish Balay         {1, 3, 2, 0},
28219371c9d4SSatish Balay         {2, 0, 1, 3},
28229371c9d4SSatish Balay         {2, 1, 3, 0},
28239371c9d4SSatish Balay         {2, 3, 0, 1},
28249371c9d4SSatish Balay         {3, 0, 2, 1},
28259371c9d4SSatish Balay         {3, 1, 0, 2},
28269371c9d4SSatish Balay         {3, 2, 1, 0}
28279371c9d4SSatish Balay       };
2828116ded15SMatthew G. Knepley       PetscInt  cone[4];
2829116ded15SMatthew G. Knepley       PetscInt *graph, p, i, j, k, l;
2830116ded15SMatthew G. Knepley 
28319371c9d4SSatish Balay       vertexA[0] *= R;
28329371c9d4SSatish Balay       vertexA[1] *= R;
28339371c9d4SSatish Balay       vertexA[2] *= R;
28349371c9d4SSatish Balay       vertexA[3] *= R;
28359371c9d4SSatish Balay       vertexB[0] *= R;
28369371c9d4SSatish Balay       vertexB[1] *= R;
28379371c9d4SSatish Balay       vertexB[2] *= R;
28389371c9d4SSatish Balay       vertexB[3] *= R;
28399371c9d4SSatish Balay       vertexC[0] *= R;
28409371c9d4SSatish Balay       vertexC[1] *= R;
28419371c9d4SSatish Balay       vertexC[2] *= R;
28429371c9d4SSatish Balay       vertexC[3] *= R;
2843dd400576SPatrick Sanan       numCells    = rank == 0 ? 600 : 0;
2844dd400576SPatrick Sanan       numVerts    = rank == 0 ? 120 : 0;
2845116ded15SMatthew G. Knepley       firstVertex = numCells;
2846116ded15SMatthew G. Knepley       /* Use the 600-cell, which for a unit sphere has coordinates which are
2847116ded15SMatthew G. Knepley 
2848116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm 1,    \pm 1, \pm 1)                          16
2849116ded15SMatthew G. Knepley                (\pm 1,    0,       0,      0)  all cyclic permutations   8
2850116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm phi, \pm 1/phi, 0)  all even permutations    96
2851116ded15SMatthew G. Knepley 
2852116ded15SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
28536333ae4fSvaleriabarra          length is then given by 1/\phi = 0.61803.
2854116ded15SMatthew G. Knepley 
2855116ded15SMatthew G. Knepley          http://buzzard.pugetsound.edu/sage-practice/ch03s03.html
2856116ded15SMatthew G. Knepley          http://mathworld.wolfram.com/600-Cell.html
2857116ded15SMatthew G. Knepley       */
2858116ded15SMatthew G. Knepley       /* Construct vertices */
28599566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
2860116ded15SMatthew G. Knepley       i = 0;
2861dd400576SPatrick Sanan       if (rank == 0) {
2862116ded15SMatthew G. Knepley         for (s[0] = -1; s[0] < 2; s[0] += 2) {
2863116ded15SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
2864116ded15SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
2865116ded15SMatthew G. Knepley               for (s[3] = -1; s[3] < 2; s[3] += 2) {
2866116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[d] * vertexA[d];
2867116ded15SMatthew G. Knepley                 ++i;
2868116ded15SMatthew G. Knepley               }
2869116ded15SMatthew G. Knepley             }
2870116ded15SMatthew G. Knepley           }
2871116ded15SMatthew G. Knepley         }
2872116ded15SMatthew G. Knepley         for (p = 0; p < embedDim; ++p) {
2873116ded15SMatthew G. Knepley           s[1] = s[2] = s[3] = 1;
2874116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
2875116ded15SMatthew G. Knepley             for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[(d + p) % embedDim] * vertexB[(d + p) % embedDim];
2876116ded15SMatthew G. Knepley             ++i;
2877116ded15SMatthew G. Knepley           }
2878116ded15SMatthew G. Knepley         }
2879116ded15SMatthew G. Knepley         for (p = 0; p < 12; ++p) {
2880116ded15SMatthew G. Knepley           s[3] = 1;
2881116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
2882116ded15SMatthew G. Knepley             for (s[1] = -1; s[1] < 2; s[1] += 2) {
2883116ded15SMatthew G. Knepley               for (s[2] = -1; s[2] < 2; s[2] += 2) {
2884116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[evenPerm[p][d]] * vertexC[evenPerm[p][d]];
2885116ded15SMatthew G. Knepley                 ++i;
2886116ded15SMatthew G. Knepley               }
2887116ded15SMatthew G. Knepley             }
2888116ded15SMatthew G. Knepley           }
2889116ded15SMatthew G. Knepley         }
289045da822fSValeria Barra       }
289163a3b9bcSJacob Faibussowitsch       PetscCheck(i == numVerts, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertices %" PetscInt_FMT " != %" PetscInt_FMT, i, numVerts);
2892116ded15SMatthew G. Knepley       /* Construct graph */
28939566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * numVerts, &graph));
2894116ded15SMatthew G. Knepley       for (i = 0; i < numVerts; ++i) {
2895116ded15SMatthew G. Knepley         for (j = 0, k = 0; j < numVerts; ++j) {
28969371c9d4SSatish Balay           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i * embedDim], &coordsIn[j * embedDim]) - edgeLen) < PETSC_SMALL) {
28979371c9d4SSatish Balay             graph[i * numVerts + j] = 1;
28989371c9d4SSatish Balay             ++k;
28999371c9d4SSatish Balay           }
2900116ded15SMatthew G. Knepley         }
290163a3b9bcSJacob Faibussowitsch         PetscCheck(k == degree, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertex %" PetscInt_FMT " degree %" PetscInt_FMT " != %" PetscInt_FMT, i, k, degree);
2902116ded15SMatthew G. Knepley       }
2903116ded15SMatthew G. Knepley       /* Build Topology */
29049566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts));
290507c565c5SJose E. Roman       for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim));
29069566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
2907116ded15SMatthew G. Knepley       /* Cells */
2908dd400576SPatrick Sanan       if (rank == 0) {
290907c565c5SJose E. Roman         for (PetscInt i = 0, c = 0; i < numVerts; ++i) {
2910116ded15SMatthew G. Knepley           for (j = 0; j < i; ++j) {
2911116ded15SMatthew G. Knepley             for (k = 0; k < j; ++k) {
2912116ded15SMatthew G. Knepley               for (l = 0; l < k; ++l) {
29139371c9d4SSatish 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]) {
29149371c9d4SSatish Balay                   cone[0] = firstVertex + i;
29159371c9d4SSatish Balay                   cone[1] = firstVertex + j;
29169371c9d4SSatish Balay                   cone[2] = firstVertex + k;
29179371c9d4SSatish Balay                   cone[3] = firstVertex + l;
2918116ded15SMatthew G. Knepley                   /* Check orientation: https://ef.gy/linear-algebra:normal-vectors-in-higher-dimensional-spaces */
2919116ded15SMatthew G. Knepley                   {
29209371c9d4SSatish Balay                     const PetscInt epsilon[4][4][4][4] = {
29219371c9d4SSatish 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}}},
2922116ded15SMatthew G. Knepley 
29239371c9d4SSatish 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}}},
2924116ded15SMatthew G. Knepley 
29259371c9d4SSatish 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}}},
2926116ded15SMatthew G. Knepley 
29279371c9d4SSatish 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}} }
29289371c9d4SSatish Balay                     };
2929116ded15SMatthew G. Knepley                     PetscReal normal[4];
2930116ded15SMatthew G. Knepley                     PetscInt  e, f, g;
2931116ded15SMatthew G. Knepley 
2932116ded15SMatthew G. Knepley                     for (d = 0; d < embedDim; ++d) {
2933116ded15SMatthew G. Knepley                       normal[d] = 0.0;
2934116ded15SMatthew G. Knepley                       for (e = 0; e < embedDim; ++e) {
2935116ded15SMatthew G. Knepley                         for (f = 0; f < embedDim; ++f) {
2936116ded15SMatthew G. Knepley                           for (g = 0; g < embedDim; ++g) {
2937116ded15SMatthew 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]);
2938116ded15SMatthew G. Knepley                           }
2939116ded15SMatthew G. Knepley                         }
2940116ded15SMatthew G. Knepley                       }
2941116ded15SMatthew G. Knepley                     }
29429371c9d4SSatish Balay                     if (DotReal(embedDim, normal, &coordsIn[i * embedDim]) < 0) {
29439371c9d4SSatish Balay                       PetscInt tmp = cone[1];
29449371c9d4SSatish Balay                       cone[1]      = cone[2];
29459371c9d4SSatish Balay                       cone[2]      = tmp;
29469371c9d4SSatish Balay                     }
2947116ded15SMatthew G. Knepley                   }
29489566063dSJacob Faibussowitsch                   PetscCall(DMPlexSetCone(dm, c++, cone));
2949116ded15SMatthew G. Knepley                 }
2950116ded15SMatthew G. Knepley               }
2951116ded15SMatthew G. Knepley             }
2952116ded15SMatthew G. Knepley           }
2953116ded15SMatthew G. Knepley         }
295445da822fSValeria Barra       }
29559566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
29569566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
29579566063dSJacob Faibussowitsch       PetscCall(PetscFree(graph));
2958116ded15SMatthew G. Knepley     }
2959f4d061e9SPierre Jolivet     break;
2960d71ae5a4SJacob Faibussowitsch   default:
2961d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension for sphere: %" PetscInt_FMT, dim);
296265a81367SMatthew G. Knepley   }
296365a81367SMatthew G. Knepley   /* Create coordinates */
29649566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
29659566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
29669566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, embedDim));
29679566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numVerts));
29682829fed8SMatthew G. Knepley   for (v = firstVertex; v < firstVertex + numVerts; ++v) {
29699566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, embedDim));
29709566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, embedDim));
29712829fed8SMatthew G. Knepley   }
29729566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
29739566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
29749566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
29759566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, embedDim));
29769566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
29779566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
29789566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
29799566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
29809371c9d4SSatish Balay   for (v = 0; v < numVerts; ++v)
2981ad540459SPierre Jolivet     for (d = 0; d < embedDim; ++d) coords[v * embedDim + d] = coordsIn[v * embedDim + d];
29829566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
29839566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
29849566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
29859566063dSJacob Faibussowitsch   PetscCall(PetscFree(coordsIn));
298651a74b61SMatthew G. Knepley   {
298751a74b61SMatthew G. Knepley     DM          cdm;
298851a74b61SMatthew G. Knepley     PetscDS     cds;
29899318fe57SMatthew G. Knepley     PetscScalar c = R;
299051a74b61SMatthew G. Knepley 
2991e44f6aebSMatthew G. Knepley     PetscCall(DMPlexCreateCoordinateSpace(dm, 1, PETSC_TRUE, snapToSphere));
29929566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
29939566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
29949566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, 1, &c));
299551a74b61SMatthew G. Knepley   }
299646139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
29979318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
29989566063dSJacob Faibussowitsch   if (simplex) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
29993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
30009318fe57SMatthew G. Knepley }
30019318fe57SMatthew G. Knepley 
3002b7f5c055SJed Brown typedef void (*TPSEvaluateFunc)(const PetscReal[], PetscReal *, PetscReal[], PetscReal (*)[3]);
3003b7f5c055SJed Brown 
3004b7f5c055SJed Brown /*
3005b7f5c055SJed Brown  The Schwarz P implicit surface is
3006b7f5c055SJed Brown 
3007b7f5c055SJed Brown      f(x) = cos(x0) + cos(x1) + cos(x2) = 0
3008b7f5c055SJed Brown */
3009d71ae5a4SJacob Faibussowitsch static void TPSEvaluate_SchwarzP(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3])
3010d71ae5a4SJacob Faibussowitsch {
3011b7f5c055SJed Brown   PetscReal c[3] = {PetscCosReal(y[0] * PETSC_PI), PetscCosReal(y[1] * PETSC_PI), PetscCosReal(y[2] * PETSC_PI)};
3012b7f5c055SJed Brown   PetscReal g[3] = {-PetscSinReal(y[0] * PETSC_PI), -PetscSinReal(y[1] * PETSC_PI), -PetscSinReal(y[2] * PETSC_PI)};
3013b7f5c055SJed Brown   f[0]           = c[0] + c[1] + c[2];
3014b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) {
3015b7f5c055SJed Brown     grad[i] = PETSC_PI * g[i];
3016ad540459SPierre Jolivet     for (PetscInt j = 0; j < 3; j++) hess[i][j] = (i == j) ? -PetscSqr(PETSC_PI) * c[i] : 0.;
3017b7f5c055SJed Brown   }
3018b7f5c055SJed Brown }
3019b7f5c055SJed Brown 
30204663dae6SJed Brown // u[] is a tentative normal on input. Replace with the implicit function gradient in the same direction
3021d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSExtrudeNormalFunc_SchwarzP(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt r, PetscScalar u[], void *ctx)
3022d71ae5a4SJacob Faibussowitsch {
3023ad540459SPierre Jolivet   for (PetscInt i = 0; i < 3; i++) u[i] = -PETSC_PI * PetscSinReal(x[i] * PETSC_PI);
30243ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
30254663dae6SJed Brown }
30264663dae6SJed Brown 
3027b7f5c055SJed Brown /*
3028b7f5c055SJed Brown  The Gyroid implicit surface is
3029b7f5c055SJed Brown 
3030b7f5c055SJed 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)
3031b7f5c055SJed Brown 
3032b7f5c055SJed Brown */
3033d71ae5a4SJacob Faibussowitsch static void TPSEvaluate_Gyroid(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3])
3034d71ae5a4SJacob Faibussowitsch {
3035b7f5c055SJed Brown   PetscReal s[3] = {PetscSinReal(PETSC_PI * y[0]), PetscSinReal(PETSC_PI * (y[1] + .5)), PetscSinReal(PETSC_PI * (y[2] + .25))};
3036b7f5c055SJed Brown   PetscReal c[3] = {PetscCosReal(PETSC_PI * y[0]), PetscCosReal(PETSC_PI * (y[1] + .5)), PetscCosReal(PETSC_PI * (y[2] + .25))};
3037b7f5c055SJed Brown   f[0]           = s[0] * c[1] + s[1] * c[2] + s[2] * c[0];
3038b7f5c055SJed Brown   grad[0]        = PETSC_PI * (c[0] * c[1] - s[2] * s[0]);
3039b7f5c055SJed Brown   grad[1]        = PETSC_PI * (c[1] * c[2] - s[0] * s[1]);
3040b7f5c055SJed Brown   grad[2]        = PETSC_PI * (c[2] * c[0] - s[1] * s[2]);
3041b7f5c055SJed Brown   hess[0][0]     = -PetscSqr(PETSC_PI) * (s[0] * c[1] + s[2] * c[0]);
3042b7f5c055SJed Brown   hess[0][1]     = -PetscSqr(PETSC_PI) * (c[0] * s[1]);
3043b7f5c055SJed Brown   hess[0][2]     = -PetscSqr(PETSC_PI) * (c[2] * s[0]);
3044b7f5c055SJed Brown   hess[1][0]     = -PetscSqr(PETSC_PI) * (s[1] * c[2] + s[0] * c[1]);
3045b7f5c055SJed Brown   hess[1][1]     = -PetscSqr(PETSC_PI) * (c[1] * s[2]);
3046b7f5c055SJed Brown   hess[2][2]     = -PetscSqr(PETSC_PI) * (c[0] * s[1]);
3047b7f5c055SJed Brown   hess[2][0]     = -PetscSqr(PETSC_PI) * (s[2] * c[0] + s[1] * c[2]);
3048b7f5c055SJed Brown   hess[2][1]     = -PetscSqr(PETSC_PI) * (c[2] * s[0]);
3049b7f5c055SJed Brown   hess[2][2]     = -PetscSqr(PETSC_PI) * (c[1] * s[2]);
3050b7f5c055SJed Brown }
3051b7f5c055SJed Brown 
30524663dae6SJed Brown // u[] is a tentative normal on input. Replace with the implicit function gradient in the same direction
3053d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSExtrudeNormalFunc_Gyroid(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt r, PetscScalar u[], void *ctx)
3054d71ae5a4SJacob Faibussowitsch {
30554663dae6SJed Brown   PetscReal s[3] = {PetscSinReal(PETSC_PI * x[0]), PetscSinReal(PETSC_PI * (x[1] + .5)), PetscSinReal(PETSC_PI * (x[2] + .25))};
30564663dae6SJed Brown   PetscReal c[3] = {PetscCosReal(PETSC_PI * x[0]), PetscCosReal(PETSC_PI * (x[1] + .5)), PetscCosReal(PETSC_PI * (x[2] + .25))};
30574663dae6SJed Brown   u[0]           = PETSC_PI * (c[0] * c[1] - s[2] * s[0]);
30584663dae6SJed Brown   u[1]           = PETSC_PI * (c[1] * c[2] - s[0] * s[1]);
30594663dae6SJed Brown   u[2]           = PETSC_PI * (c[2] * c[0] - s[1] * s[2]);
30603ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
30614663dae6SJed Brown }
30624663dae6SJed Brown 
3063b7f5c055SJed Brown /*
3064b7f5c055SJed Brown    We wish to solve
3065b7f5c055SJed Brown 
3066b7f5c055SJed Brown          min_y || y - x ||^2  subject to f(y) = 0
3067b7f5c055SJed Brown 
3068b7f5c055SJed Brown    Let g(y) = grad(f).  The minimization problem is equivalent to asking to satisfy
3069b7f5c055SJed 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
3070b7f5c055SJed Brown    tangent space and ask for both components in the tangent space to be zero.
3071b7f5c055SJed Brown 
3072b7f5c055SJed Brown    Take g to be a column vector and compute the "full QR" factorization Q R = g,
3073b7f5c055SJed Brown    where Q = I - 2 n n^T is a symmetric orthogonal matrix.
3074b7f5c055SJed Brown    The first column of Q is parallel to g so the remaining two columns span the null space.
3075b7f5c055SJed Brown    Let Qn = Q[:,1:] be those remaining columns.  Then Qn Qn^T is an orthogonal projector into the tangent space.
3076da81f932SPierre Jolivet    Since Q is symmetric, this is equivalent to multiplying by Q and taking the last two entries.
3077b7f5c055SJed Brown    In total, we have a system of 3 equations in 3 unknowns:
3078b7f5c055SJed Brown 
3079b7f5c055SJed Brown      f(y) = 0                       1 equation
3080b7f5c055SJed Brown      Qn^T (y - x) = 0               2 equations
3081b7f5c055SJed Brown 
3082b7f5c055SJed Brown    Here, we compute the residual and Jacobian of this system.
3083b7f5c055SJed Brown */
3084d71ae5a4SJacob Faibussowitsch static void TPSNearestPointResJac(TPSEvaluateFunc feval, const PetscScalar x[], const PetscScalar y[], PetscScalar res[], PetscScalar J[])
3085d71ae5a4SJacob Faibussowitsch {
3086b7f5c055SJed Brown   PetscReal yreal[3] = {PetscRealPart(y[0]), PetscRealPart(y[1]), PetscRealPart(y[2])};
3087b7f5c055SJed Brown   PetscReal d[3]     = {PetscRealPart(y[0] - x[0]), PetscRealPart(y[1] - x[1]), PetscRealPart(y[2] - x[2])};
30882f0490c0SSatish Balay   PetscReal f, grad[3], n[3], norm, norm_y[3], nd, nd_y[3], sign;
30899371c9d4SSatish Balay   PetscReal n_y[3][3] = {
30909371c9d4SSatish Balay     {0, 0, 0},
30919371c9d4SSatish Balay     {0, 0, 0},
30929371c9d4SSatish Balay     {0, 0, 0}
30939371c9d4SSatish Balay   };
3094b7f5c055SJed Brown 
3095b7f5c055SJed Brown   feval(yreal, &f, grad, n_y);
3096b7f5c055SJed Brown 
3097b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) n[i] = grad[i];
3098b7f5c055SJed Brown   norm = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2]));
3099ad540459SPierre Jolivet   for (PetscInt i = 0; i < 3; i++) norm_y[i] = 1. / norm * n[i] * n_y[i][i];
3100b7f5c055SJed Brown 
3101b7f5c055SJed Brown   // Define the Householder reflector
3102b7f5c055SJed Brown   sign = n[0] >= 0 ? 1. : -1.;
3103b7f5c055SJed Brown   n[0] += norm * sign;
3104b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) n_y[0][i] += norm_y[i] * sign;
3105b7f5c055SJed Brown 
3106b7f5c055SJed Brown   norm      = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2]));
3107b7f5c055SJed Brown   norm_y[0] = 1. / norm * (n[0] * n_y[0][0]);
3108b7f5c055SJed Brown   norm_y[1] = 1. / norm * (n[0] * n_y[0][1] + n[1] * n_y[1][1]);
3109b7f5c055SJed Brown   norm_y[2] = 1. / norm * (n[0] * n_y[0][2] + n[2] * n_y[2][2]);
3110b7f5c055SJed Brown 
3111b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) {
3112b7f5c055SJed Brown     n[i] /= norm;
3113b7f5c055SJed Brown     for (PetscInt j = 0; j < 3; j++) {
3114b7f5c055SJed Brown       // note that n[i] is n_old[i]/norm when executing the code below
3115b7f5c055SJed Brown       n_y[i][j] = n_y[i][j] / norm - n[i] / norm * norm_y[j];
3116b7f5c055SJed Brown     }
3117b7f5c055SJed Brown   }
3118b7f5c055SJed Brown 
3119b7f5c055SJed Brown   nd = n[0] * d[0] + n[1] * d[1] + n[2] * d[2];
3120b7f5c055SJed 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];
3121b7f5c055SJed Brown 
3122b7f5c055SJed Brown   res[0] = f;
3123b7f5c055SJed Brown   res[1] = d[1] - 2 * n[1] * nd;
3124b7f5c055SJed Brown   res[2] = d[2] - 2 * n[2] * nd;
3125b7f5c055SJed Brown   // J[j][i] is J_{ij} (column major)
3126b7f5c055SJed Brown   for (PetscInt j = 0; j < 3; j++) {
3127b7f5c055SJed Brown     J[0 + j * 3] = grad[j];
3128b7f5c055SJed Brown     J[1 + j * 3] = (j == 1) * 1. - 2 * (n_y[1][j] * nd + n[1] * nd_y[j]);
3129b7f5c055SJed Brown     J[2 + j * 3] = (j == 2) * 1. - 2 * (n_y[2][j] * nd + n[2] * nd_y[j]);
3130b7f5c055SJed Brown   }
3131b7f5c055SJed Brown }
3132b7f5c055SJed Brown 
3133b7f5c055SJed Brown /*
3134b7f5c055SJed Brown    Project x to the nearest point on the implicit surface using Newton's method.
3135b7f5c055SJed Brown */
3136d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSNearestPoint(TPSEvaluateFunc feval, PetscScalar x[])
3137d71ae5a4SJacob Faibussowitsch {
3138b7f5c055SJed Brown   PetscScalar y[3] = {x[0], x[1], x[2]}; // Initial guess
3139b7f5c055SJed Brown 
3140b7f5c055SJed Brown   PetscFunctionBegin;
3141b7f5c055SJed Brown   for (PetscInt iter = 0; iter < 10; iter++) {
3142b7f5c055SJed Brown     PetscScalar res[3], J[9];
3143b7f5c055SJed Brown     PetscReal   resnorm;
3144b7f5c055SJed Brown     TPSNearestPointResJac(feval, x, y, res, J);
3145b7f5c055SJed Brown     resnorm = PetscSqrtReal(PetscSqr(PetscRealPart(res[0])) + PetscSqr(PetscRealPart(res[1])) + PetscSqr(PetscRealPart(res[2])));
3146b7f5c055SJed Brown     if (0) { // Turn on this monitor if you need to confirm quadratic convergence
314763a3b9bcSJacob 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])));
3148b7f5c055SJed Brown     }
3149b7f5c055SJed Brown     if (resnorm < PETSC_SMALL) break;
3150b7f5c055SJed Brown 
3151b7f5c055SJed Brown     // Take the Newton step
31529566063dSJacob Faibussowitsch     PetscCall(PetscKernel_A_gets_inverse_A_3(J, 0., PETSC_FALSE, NULL));
3153b7f5c055SJed Brown     PetscKernel_v_gets_v_minus_A_times_w_3(y, J, res);
3154b7f5c055SJed Brown   }
3155b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) x[i] = y[i];
31563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3157b7f5c055SJed Brown }
3158b7f5c055SJed Brown 
3159b7f5c055SJed Brown const char *const DMPlexTPSTypes[] = {"SCHWARZ_P", "GYROID", "DMPlexTPSType", "DMPLEX_TPS_", NULL};
3160b7f5c055SJed Brown 
3161d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateTPSMesh_Internal(DM dm, DMPlexTPSType tpstype, const PetscInt extent[], const DMBoundaryType periodic[], PetscBool tps_distribute, PetscInt refinements, PetscInt layers, PetscReal thickness)
3162d71ae5a4SJacob Faibussowitsch {
3163b7f5c055SJed Brown   PetscMPIInt rank;
3164b7f5c055SJed Brown   PetscInt    topoDim = 2, spaceDim = 3, numFaces = 0, numVertices = 0, numEdges = 0;
3165b7f5c055SJed Brown   PetscInt(*edges)[2] = NULL, *edgeSets = NULL;
3166b7f5c055SJed Brown   PetscInt           *cells_flat = NULL;
3167b7f5c055SJed Brown   PetscReal          *vtxCoords  = NULL;
3168b7f5c055SJed Brown   TPSEvaluateFunc     evalFunc   = NULL;
31698434afd1SBarry Smith   PetscSimplePointFn *normalFunc = NULL;
3170b7f5c055SJed Brown   DMLabel             label;
3171b7f5c055SJed Brown 
3172b7f5c055SJed Brown   PetscFunctionBegin;
317346139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
31749566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
317563a3b9bcSJacob 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);
3176b7f5c055SJed Brown   switch (tpstype) {
3177b7f5c055SJed Brown   case DMPLEX_TPS_SCHWARZ_P:
3178b7f5c055SJed 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");
3179c5853193SPierre Jolivet     if (rank == 0) {
3180b7f5c055SJed Brown       PetscInt(*cells)[6][4][4] = NULL; // [junction, junction-face, cell, conn]
3181b7f5c055SJed Brown       PetscInt  Njunctions = 0, Ncuts = 0, Npipes[3], vcount;
3182b7f5c055SJed Brown       PetscReal L = 1;
3183b7f5c055SJed Brown 
3184b7f5c055SJed Brown       Npipes[0]   = (extent[0] + 1) * extent[1] * extent[2];
3185b7f5c055SJed Brown       Npipes[1]   = extent[0] * (extent[1] + 1) * extent[2];
3186b7f5c055SJed Brown       Npipes[2]   = extent[0] * extent[1] * (extent[2] + 1);
3187b7f5c055SJed Brown       Njunctions  = extent[0] * extent[1] * extent[2];
3188b7f5c055SJed Brown       Ncuts       = 2 * (extent[0] * extent[1] + extent[1] * extent[2] + extent[2] * extent[0]);
3189b7f5c055SJed Brown       numVertices = 4 * (Npipes[0] + Npipes[1] + Npipes[2]) + 8 * Njunctions;
31909566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(3 * numVertices, &vtxCoords));
31919566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Njunctions, &cells));
31929566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Ncuts * 4, &edges));
31939566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Ncuts * 4, &edgeSets));
3194b7f5c055SJed Brown       // x-normal pipes
3195b7f5c055SJed Brown       vcount = 0;
3196b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0] + 1; i++) {
3197b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3198b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2]; k++) {
3199b7f5c055SJed Brown             for (PetscInt l = 0; l < 4; l++) {
3200b7f5c055SJed Brown               vtxCoords[vcount++] = (2 * i - 1) * L;
3201b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * j * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3202b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * k * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3203b7f5c055SJed Brown             }
3204b7f5c055SJed Brown           }
3205b7f5c055SJed Brown         }
3206b7f5c055SJed Brown       }
3207b7f5c055SJed Brown       // y-normal pipes
3208b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0]; i++) {
3209b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1] + 1; j++) {
3210b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2]; k++) {
3211b7f5c055SJed Brown             for (PetscInt l = 0; l < 4; l++) {
3212b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * i * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3213b7f5c055SJed Brown               vtxCoords[vcount++] = (2 * j - 1) * L;
3214b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * k * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3215b7f5c055SJed Brown             }
3216b7f5c055SJed Brown           }
3217b7f5c055SJed Brown         }
3218b7f5c055SJed Brown       }
3219b7f5c055SJed Brown       // z-normal pipes
3220b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0]; i++) {
3221b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3222b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2] + 1; k++) {
3223b7f5c055SJed Brown             for (PetscInt l = 0; l < 4; l++) {
3224b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * i * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3225b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * j * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3226b7f5c055SJed Brown               vtxCoords[vcount++] = (2 * k - 1) * L;
3227b7f5c055SJed Brown             }
3228b7f5c055SJed Brown           }
3229b7f5c055SJed Brown         }
3230b7f5c055SJed Brown       }
3231b7f5c055SJed Brown       // junctions
3232b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0]; i++) {
3233b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3234b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2]; k++) {
3235b7f5c055SJed Brown             const PetscInt J = (i * extent[1] + j) * extent[2] + k, Jvoff = (Npipes[0] + Npipes[1] + Npipes[2]) * 4 + J * 8;
3236b7f5c055SJed Brown             PetscCheck(vcount / 3 == Jvoff, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unexpected vertex count");
3237b7f5c055SJed Brown             for (PetscInt ii = 0; ii < 2; ii++) {
3238b7f5c055SJed Brown               for (PetscInt jj = 0; jj < 2; jj++) {
3239b7f5c055SJed Brown                 for (PetscInt kk = 0; kk < 2; kk++) {
3240b7f5c055SJed Brown                   double Ls           = (1 - sqrt(2) / 4) * L;
3241b7f5c055SJed Brown                   vtxCoords[vcount++] = 2 * i * L + (2 * ii - 1) * Ls;
3242b7f5c055SJed Brown                   vtxCoords[vcount++] = 2 * j * L + (2 * jj - 1) * Ls;
3243b7f5c055SJed Brown                   vtxCoords[vcount++] = 2 * k * L + (2 * kk - 1) * Ls;
3244b7f5c055SJed Brown                 }
3245b7f5c055SJed Brown               }
3246b7f5c055SJed Brown             }
3247b7f5c055SJed Brown             const PetscInt jfaces[3][2][4] = {
3248b7f5c055SJed Brown               {{3, 1, 0, 2}, {7, 5, 4, 6}}, // x-aligned
3249b7f5c055SJed Brown               {{5, 4, 0, 1}, {7, 6, 2, 3}}, // y-aligned
3250b7f5c055SJed Brown               {{6, 2, 0, 4}, {7, 3, 1, 5}}  // z-aligned
3251b7f5c055SJed Brown             };
3252b7f5c055SJed Brown             const PetscInt pipe_lo[3] = {// vertex numbers of pipes
32539371c9d4SSatish 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};
3254b7f5c055SJed Brown             const PetscInt pipe_hi[3] = {// vertex numbers of pipes
32559371c9d4SSatish 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};
3256b7f5c055SJed Brown             for (PetscInt dir = 0; dir < 3; dir++) { // x,y,z
3257b7f5c055SJed Brown               const PetscInt ijk[3] = {i, j, k};
3258b7f5c055SJed Brown               for (PetscInt l = 0; l < 4; l++) { // rotations
3259b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][0] = pipe_lo[dir] + l;
3260b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][1] = Jvoff + jfaces[dir][0][l];
3261b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][2] = Jvoff + jfaces[dir][0][(l - 1 + 4) % 4];
3262b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][3] = pipe_lo[dir] + (l - 1 + 4) % 4;
3263b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][0] = Jvoff + jfaces[dir][1][l];
3264b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][1] = pipe_hi[dir] + l;
3265b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][2] = pipe_hi[dir] + (l - 1 + 4) % 4;
3266b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][3] = Jvoff + jfaces[dir][1][(l - 1 + 4) % 4];
3267b7f5c055SJed Brown                 if (ijk[dir] == 0) {
3268b7f5c055SJed Brown                   edges[numEdges][0] = pipe_lo[dir] + l;
3269b7f5c055SJed Brown                   edges[numEdges][1] = pipe_lo[dir] + (l + 1) % 4;
3270b7f5c055SJed Brown                   edgeSets[numEdges] = dir * 2 + 1;
3271b7f5c055SJed Brown                   numEdges++;
3272b7f5c055SJed Brown                 }
3273b7f5c055SJed Brown                 if (ijk[dir] + 1 == extent[dir]) {
3274b7f5c055SJed Brown                   edges[numEdges][0] = pipe_hi[dir] + l;
3275b7f5c055SJed Brown                   edges[numEdges][1] = pipe_hi[dir] + (l + 1) % 4;
3276b7f5c055SJed Brown                   edgeSets[numEdges] = dir * 2 + 2;
3277b7f5c055SJed Brown                   numEdges++;
3278b7f5c055SJed Brown                 }
3279b7f5c055SJed Brown               }
3280b7f5c055SJed Brown             }
3281b7f5c055SJed Brown           }
3282b7f5c055SJed Brown         }
3283b7f5c055SJed Brown       }
328463a3b9bcSJacob Faibussowitsch       PetscCheck(numEdges == Ncuts * 4, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Edge count %" PetscInt_FMT " incompatible with number of cuts %" PetscInt_FMT, numEdges, Ncuts);
3285b7f5c055SJed Brown       numFaces   = 24 * Njunctions;
3286b7f5c055SJed Brown       cells_flat = cells[0][0][0];
3287b7f5c055SJed Brown     }
3288b7f5c055SJed Brown     evalFunc   = TPSEvaluate_SchwarzP;
32894663dae6SJed Brown     normalFunc = TPSExtrudeNormalFunc_SchwarzP;
3290b7f5c055SJed Brown     break;
3291b7f5c055SJed Brown   case DMPLEX_TPS_GYROID:
3292c5853193SPierre Jolivet     if (rank == 0) {
3293b7f5c055SJed Brown       // This is a coarse mesh approximation of the gyroid shifted to being the zero of the level set
3294b7f5c055SJed Brown       //
3295b7f5c055SJed 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)
3296b7f5c055SJed Brown       //
3297b7f5c055SJed Brown       // on the cell [0,2]^3.
3298b7f5c055SJed Brown       //
3299b7f5c055SJed Brown       // Think about dividing that cell into four columns, and focus on the column [0,1]x[0,1]x[0,2].
3300b7f5c055SJed Brown       // If you looked at the gyroid in that column at different slices of z you would see that it kind of spins
3301b7f5c055SJed Brown       // like a boomerang:
3302b7f5c055SJed Brown       //
3303b7f5c055SJed Brown       //     z = 0          z = 1/4        z = 1/2        z = 3/4     //
3304b7f5c055SJed Brown       //     -----          -------        -------        -------     //
3305b7f5c055SJed Brown       //                                                              //
3306b7f5c055SJed Brown       //     +       +      +       +      +       +      +   \   +   //
3307b7f5c055SJed Brown       //      \                                   /            \      //
3308b7f5c055SJed Brown       //       \            `-_   _-'            /              }     //
3309b7f5c055SJed Brown       //        *-_            `-'            _-'              /      //
3310b7f5c055SJed Brown       //     +     `-+      +       +      +-'     +      +   /   +   //
3311b7f5c055SJed Brown       //                                                              //
3312b7f5c055SJed Brown       //                                                              //
3313b7f5c055SJed Brown       //     z = 1          z = 5/4        z = 3/2        z = 7/4     //
3314b7f5c055SJed Brown       //     -----          -------        -------        -------     //
3315b7f5c055SJed Brown       //                                                              //
3316b7f5c055SJed Brown       //     +-_     +      +       +      +     _-+      +   /   +   //
3317b7f5c055SJed Brown       //        `-_            _-_            _-`            /        //
3318b7f5c055SJed Brown       //           \        _-'   `-_        /              {         //
3319b7f5c055SJed Brown       //            \                       /                \        //
3320b7f5c055SJed Brown       //     +       +      +       +      +       +      +   \   +   //
3321b7f5c055SJed Brown       //
3322b7f5c055SJed Brown       //
3323b7f5c055SJed Brown       // This course mesh approximates each of these slices by two line segments,
3324b7f5c055SJed Brown       // and then connects the segments in consecutive layers with quadrilateral faces.
3325b7f5c055SJed Brown       // All of the end points of the segments are multiples of 1/4 except for the
3326b7f5c055SJed Brown       // point * in the picture for z = 0 above and the similar points in other layers.
3327b7f5c055SJed Brown       // That point is at (gamma, gamma, 0), where gamma is calculated below.
3328b7f5c055SJed Brown       //
3329b7f5c055SJed Brown       // The column  [1,2]x[1,2]x[0,2] looks the same as this column;
3330b7f5c055SJed Brown       // The columns [1,2]x[0,1]x[0,2] and [0,1]x[1,2]x[0,2] are mirror images.
3331b7f5c055SJed Brown       //
3332b7f5c055SJed Brown       // As for how this method turned into the names given to the vertices:
3333b7f5c055SJed Brown       // that was not systematic, it was just the way it worked out in my handwritten notes.
3334b7f5c055SJed Brown 
3335b7f5c055SJed Brown       PetscInt facesPerBlock = 64;
3336b7f5c055SJed Brown       PetscInt vertsPerBlock = 56;
3337b7f5c055SJed Brown       PetscInt extentPlus[3];
3338b7f5c055SJed Brown       PetscInt numBlocks, numBlocksPlus;
33399371c9d4SSatish 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;
33409371c9d4SSatish Balay       const PetscInt pattern[64][4] = {
33419371c9d4SSatish Balay         /* face to vertex within the coarse discretization of a single gyroid block */
3342b7f5c055SJed Brown         /* layer 0 */
33439371c9d4SSatish Balay         {A,           C,           K,           G          },
33449371c9d4SSatish Balay         {C,           B,           II,          K          },
33459371c9d4SSatish Balay         {D,           A,           H,           L          },
33469371c9d4SSatish Balay         {B + 56 * 1,  D,           L,           J          },
33479371c9d4SSatish Balay         {E,           B + 56 * 1,  J,           N          },
33489371c9d4SSatish Balay         {A + 56 * 2,  E,           N,           H + 56 * 2 },
33499371c9d4SSatish Balay         {F,           A + 56 * 2,  G + 56 * 2,  M          },
33509371c9d4SSatish Balay         {B,           F,           M,           II         },
3351b7f5c055SJed Brown         /* layer 1 */
33529371c9d4SSatish Balay         {G,           K,           Q,           O          },
33539371c9d4SSatish Balay         {K,           II,          P,           Q          },
33549371c9d4SSatish Balay         {L,           H,           O + 56 * 1,  R          },
33559371c9d4SSatish Balay         {J,           L,           R,           P          },
33569371c9d4SSatish Balay         {N,           J,           P,           S          },
33579371c9d4SSatish Balay         {H + 56 * 2,  N,           S,           O + 56 * 3 },
33589371c9d4SSatish Balay         {M,           G + 56 * 2,  O + 56 * 2,  T          },
33599371c9d4SSatish Balay         {II,          M,           T,           P          },
3360b7f5c055SJed Brown         /* layer 2 */
33619371c9d4SSatish Balay         {O,           Q,           Y,           U          },
33629371c9d4SSatish Balay         {Q,           P,           W,           Y          },
33639371c9d4SSatish Balay         {R,           O + 56 * 1,  U + 56 * 1,  Ap         },
33649371c9d4SSatish Balay         {P,           R,           Ap,          W          },
33659371c9d4SSatish Balay         {S,           P,           X,           Bp         },
33669371c9d4SSatish Balay         {O + 56 * 3,  S,           Bp,          V + 56 * 1 },
33679371c9d4SSatish Balay         {T,           O + 56 * 2,  V,           Z          },
33689371c9d4SSatish Balay         {P,           T,           Z,           X          },
3369b7f5c055SJed Brown         /* layer 3 */
33709371c9d4SSatish Balay         {U,           Y,           Ep,          Dp         },
33719371c9d4SSatish Balay         {Y,           W,           Cp,          Ep         },
33729371c9d4SSatish Balay         {Ap,          U + 56 * 1,  Dp + 56 * 1, Gp         },
33739371c9d4SSatish Balay         {W,           Ap,          Gp,          Cp         },
33749371c9d4SSatish Balay         {Bp,          X,           Cp + 56 * 2, Fp         },
33759371c9d4SSatish Balay         {V + 56 * 1,  Bp,          Fp,          Dp + 56 * 1},
33769371c9d4SSatish Balay         {Z,           V,           Dp,          Hp         },
33779371c9d4SSatish Balay         {X,           Z,           Hp,          Cp + 56 * 2},
3378b7f5c055SJed Brown         /* layer 4 */
33799371c9d4SSatish Balay         {Dp,          Ep,          Mp,          Kp         },
33809371c9d4SSatish Balay         {Ep,          Cp,          Ip,          Mp         },
33819371c9d4SSatish Balay         {Gp,          Dp + 56 * 1, Lp,          Np         },
33829371c9d4SSatish Balay         {Cp,          Gp,          Np,          Jp         },
33839371c9d4SSatish Balay         {Fp,          Cp + 56 * 2, Jp + 56 * 2, Pp         },
33849371c9d4SSatish Balay         {Dp + 56 * 1, Fp,          Pp,          Lp         },
33859371c9d4SSatish Balay         {Hp,          Dp,          Kp,          Op         },
33869371c9d4SSatish Balay         {Cp + 56 * 2, Hp,          Op,          Ip + 56 * 2},
3387b7f5c055SJed Brown         /* layer 5 */
33889371c9d4SSatish Balay         {Kp,          Mp,          Sp,          Rp         },
33899371c9d4SSatish Balay         {Mp,          Ip,          Qp,          Sp         },
33909371c9d4SSatish Balay         {Np,          Lp,          Rp,          Tp         },
33919371c9d4SSatish Balay         {Jp,          Np,          Tp,          Qp + 56 * 1},
33929371c9d4SSatish Balay         {Pp,          Jp + 56 * 2, Qp + 56 * 3, Up         },
33939371c9d4SSatish Balay         {Lp,          Pp,          Up,          Rp         },
33949371c9d4SSatish Balay         {Op,          Kp,          Rp,          Vp         },
33959371c9d4SSatish Balay         {Ip + 56 * 2, Op,          Vp,          Qp + 56 * 2},
3396b7f5c055SJed Brown         /* layer 6 */
33979371c9d4SSatish Balay         {Rp,          Sp,          Aq,          Yp         },
33989371c9d4SSatish Balay         {Sp,          Qp,          Wp,          Aq         },
33999371c9d4SSatish Balay         {Tp,          Rp,          Yp,          Cq         },
34009371c9d4SSatish Balay         {Qp + 56 * 1, Tp,          Cq,          Wp + 56 * 1},
34019371c9d4SSatish Balay         {Up,          Qp + 56 * 3, Xp + 56 * 1, Dq         },
34029371c9d4SSatish Balay         {Rp,          Up,          Dq,          Zp         },
34039371c9d4SSatish Balay         {Vp,          Rp,          Zp,          Bq         },
34049371c9d4SSatish Balay         {Qp + 56 * 2, Vp,          Bq,          Xp         },
3405b7f5c055SJed Brown         /* layer 7 (the top is the periodic image of the bottom of layer 0) */
34069371c9d4SSatish Balay         {Yp,          Aq,          C + 56 * 4,  A + 56 * 4 },
34079371c9d4SSatish Balay         {Aq,          Wp,          B + 56 * 4,  C + 56 * 4 },
34089371c9d4SSatish Balay         {Cq,          Yp,          A + 56 * 4,  D + 56 * 4 },
34099371c9d4SSatish Balay         {Wp + 56 * 1, Cq,          D + 56 * 4,  B + 56 * 5 },
34109371c9d4SSatish Balay         {Dq,          Xp + 56 * 1, B + 56 * 5,  E + 56 * 4 },
34119371c9d4SSatish Balay         {Zp,          Dq,          E + 56 * 4,  A + 56 * 6 },
34129371c9d4SSatish Balay         {Bq,          Zp,          A + 56 * 6,  F + 56 * 4 },
34139371c9d4SSatish Balay         {Xp,          Bq,          F + 56 * 4,  B + 56 * 4 }
3414b7f5c055SJed Brown       };
3415b7f5c055SJed Brown       const PetscReal gamma                = PetscAcosReal((PetscSqrtReal(3.) - 1.) / PetscSqrtReal(2.)) / PETSC_PI;
34169371c9d4SSatish Balay       const PetscReal patternCoords[56][3] = {
3417bee3fc89SBarry Smith         {1.,        0.,        0.  }, /* A  */
3418bee3fc89SBarry Smith         {0.,        1.,        0.  }, /* B  */
3419bee3fc89SBarry Smith         {gamma,     gamma,     0.  }, /* C  */
3420bee3fc89SBarry Smith         {1 + gamma, 1 - gamma, 0.  }, /* D  */
3421bee3fc89SBarry Smith         {2 - gamma, 2 - gamma, 0.  }, /* E  */
3422bee3fc89SBarry Smith         {1 - gamma, 1 + gamma, 0.  }, /* F  */
3423b7f5c055SJed Brown 
3424bee3fc89SBarry Smith         {.5,        0,         .25 }, /* G  */
3425bee3fc89SBarry Smith         {1.5,       0.,        .25 }, /* H  */
3426bee3fc89SBarry Smith         {.5,        1.,        .25 }, /* II */
3427bee3fc89SBarry Smith         {1.5,       1.,        .25 }, /* J  */
3428bee3fc89SBarry Smith         {.25,       .5,        .25 }, /* K  */
3429bee3fc89SBarry Smith         {1.25,      .5,        .25 }, /* L  */
3430bee3fc89SBarry Smith         {.75,       1.5,       .25 }, /* M  */
3431bee3fc89SBarry Smith         {1.75,      1.5,       .25 }, /* N  */
3432b7f5c055SJed Brown 
3433bee3fc89SBarry Smith         {0.,        0.,        .5  }, /* O  */
3434bee3fc89SBarry Smith         {1.,        1.,        .5  }, /* P  */
3435bee3fc89SBarry Smith         {gamma,     1 - gamma, .5  }, /* Q  */
3436bee3fc89SBarry Smith         {1 + gamma, gamma,     .5  }, /* R  */
3437bee3fc89SBarry Smith         {2 - gamma, 1 + gamma, .5  }, /* S  */
3438bee3fc89SBarry Smith         {1 - gamma, 2 - gamma, .5  }, /* T  */
3439b7f5c055SJed Brown 
3440bee3fc89SBarry Smith         {0.,        .5,        .75 }, /* U  */
3441bee3fc89SBarry Smith         {0.,        1.5,       .75 }, /* V  */
3442bee3fc89SBarry Smith         {1.,        .5,        .75 }, /* W  */
3443bee3fc89SBarry Smith         {1.,        1.5,       .75 }, /* X  */
3444bee3fc89SBarry Smith         {.5,        .75,       .75 }, /* Y  */
3445bee3fc89SBarry Smith         {.5,        1.75,      .75 }, /* Z  */
3446bee3fc89SBarry Smith         {1.5,       .25,       .75 }, /* Ap */
3447bee3fc89SBarry Smith         {1.5,       1.25,      .75 }, /* Bp */
3448b7f5c055SJed Brown 
3449bee3fc89SBarry Smith         {1.,        0.,        1.  }, /* Cp */
3450bee3fc89SBarry Smith         {0.,        1.,        1.  }, /* Dp */
3451bee3fc89SBarry Smith         {1 - gamma, 1 - gamma, 1.  }, /* Ep */
3452bee3fc89SBarry Smith         {1 + gamma, 1 + gamma, 1.  }, /* Fp */
3453bee3fc89SBarry Smith         {2 - gamma, gamma,     1.  }, /* Gp */
3454bee3fc89SBarry Smith         {gamma,     2 - gamma, 1.  }, /* Hp */
3455b7f5c055SJed Brown 
3456bee3fc89SBarry Smith         {.5,        0.,        1.25}, /* Ip */
3457bee3fc89SBarry Smith         {1.5,       0.,        1.25}, /* Jp */
3458bee3fc89SBarry Smith         {.5,        1.,        1.25}, /* Kp */
3459bee3fc89SBarry Smith         {1.5,       1.,        1.25}, /* Lp */
3460bee3fc89SBarry Smith         {.75,       .5,        1.25}, /* Mp */
3461bee3fc89SBarry Smith         {1.75,      .5,        1.25}, /* Np */
3462bee3fc89SBarry Smith         {.25,       1.5,       1.25}, /* Op */
3463bee3fc89SBarry Smith         {1.25,      1.5,       1.25}, /* Pp */
3464b7f5c055SJed Brown 
3465bee3fc89SBarry Smith         {0.,        0.,        1.5 }, /* Qp */
3466bee3fc89SBarry Smith         {1.,        1.,        1.5 }, /* Rp */
3467bee3fc89SBarry Smith         {1 - gamma, gamma,     1.5 }, /* Sp */
3468bee3fc89SBarry Smith         {2 - gamma, 1 - gamma, 1.5 }, /* Tp */
3469bee3fc89SBarry Smith         {1 + gamma, 2 - gamma, 1.5 }, /* Up */
3470bee3fc89SBarry Smith         {gamma,     1 + gamma, 1.5 }, /* Vp */
3471b7f5c055SJed Brown 
3472bee3fc89SBarry Smith         {0.,        .5,        1.75}, /* Wp */
3473bee3fc89SBarry Smith         {0.,        1.5,       1.75}, /* Xp */
3474bee3fc89SBarry Smith         {1.,        .5,        1.75}, /* Yp */
3475bee3fc89SBarry Smith         {1.,        1.5,       1.75}, /* Zp */
3476bee3fc89SBarry Smith         {.5,        .25,       1.75}, /* Aq */
3477bee3fc89SBarry Smith         {.5,        1.25,      1.75}, /* Bq */
3478bee3fc89SBarry Smith         {1.5,       .75,       1.75}, /* Cq */
3479bee3fc89SBarry Smith         {1.5,       1.75,      1.75}, /* Dq */
3480b7f5c055SJed Brown       };
3481b7f5c055SJed Brown       PetscInt(*cells)[64][4] = NULL;
3482b7f5c055SJed Brown       PetscBool *seen;
3483b7f5c055SJed Brown       PetscInt  *vertToTrueVert;
3484b7f5c055SJed Brown       PetscInt   count;
3485b7f5c055SJed Brown 
3486b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) extentPlus[i] = extent[i] + 1;
3487b7f5c055SJed Brown       numBlocks = 1;
3488b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) numBlocks *= extent[i];
3489b7f5c055SJed Brown       numBlocksPlus = 1;
3490b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) numBlocksPlus *= extentPlus[i];
3491b7f5c055SJed Brown       numFaces = numBlocks * facesPerBlock;
34929566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numBlocks, &cells));
34939566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numBlocksPlus * vertsPerBlock, &seen));
3494b7f5c055SJed Brown       for (PetscInt k = 0; k < extent[2]; k++) {
3495b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3496b7f5c055SJed Brown           for (PetscInt i = 0; i < extent[0]; i++) {
3497b7f5c055SJed Brown             for (PetscInt f = 0; f < facesPerBlock; f++) {
3498b7f5c055SJed Brown               for (PetscInt v = 0; v < 4; v++) {
3499b7f5c055SJed Brown                 PetscInt vertRaw     = pattern[f][v];
3500b7f5c055SJed Brown                 PetscInt blockidx    = vertRaw / 56;
3501b7f5c055SJed Brown                 PetscInt patternvert = vertRaw % 56;
3502b7f5c055SJed Brown                 PetscInt xplus       = (blockidx & 1);
3503b7f5c055SJed Brown                 PetscInt yplus       = (blockidx & 2) >> 1;
3504b7f5c055SJed Brown                 PetscInt zplus       = (blockidx & 4) >> 2;
3505b7f5c055SJed Brown                 PetscInt zcoord      = (periodic && periodic[2] == DM_BOUNDARY_PERIODIC) ? ((k + zplus) % extent[2]) : (k + zplus);
3506b7f5c055SJed Brown                 PetscInt ycoord      = (periodic && periodic[1] == DM_BOUNDARY_PERIODIC) ? ((j + yplus) % extent[1]) : (j + yplus);
3507b7f5c055SJed Brown                 PetscInt xcoord      = (periodic && periodic[0] == DM_BOUNDARY_PERIODIC) ? ((i + xplus) % extent[0]) : (i + xplus);
3508b7f5c055SJed Brown                 PetscInt vert        = ((zcoord * extentPlus[1] + ycoord) * extentPlus[0] + xcoord) * 56 + patternvert;
3509b7f5c055SJed Brown 
3510b7f5c055SJed Brown                 cells[(k * extent[1] + j) * extent[0] + i][f][v] = vert;
3511b7f5c055SJed Brown                 seen[vert]                                       = PETSC_TRUE;
3512b7f5c055SJed Brown               }
3513b7f5c055SJed Brown             }
3514b7f5c055SJed Brown           }
3515b7f5c055SJed Brown         }
3516b7f5c055SJed Brown       }
35179371c9d4SSatish Balay       for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++)
35189371c9d4SSatish Balay         if (seen[i]) numVertices++;
3519b7f5c055SJed Brown       count = 0;
35209566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numBlocksPlus * vertsPerBlock, &vertToTrueVert));
35219566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numVertices * 3, &vtxCoords));
3522b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++) vertToTrueVert[i] = -1;
3523b7f5c055SJed Brown       for (PetscInt k = 0; k < extentPlus[2]; k++) {
3524b7f5c055SJed Brown         for (PetscInt j = 0; j < extentPlus[1]; j++) {
3525b7f5c055SJed Brown           for (PetscInt i = 0; i < extentPlus[0]; i++) {
3526b7f5c055SJed Brown             for (PetscInt v = 0; v < vertsPerBlock; v++) {
3527b7f5c055SJed Brown               PetscInt vIdx = ((k * extentPlus[1] + j) * extentPlus[0] + i) * vertsPerBlock + v;
3528b7f5c055SJed Brown 
3529b7f5c055SJed Brown               if (seen[vIdx]) {
3530b7f5c055SJed Brown                 PetscInt thisVert;
3531b7f5c055SJed Brown 
3532b7f5c055SJed Brown                 vertToTrueVert[vIdx] = thisVert = count++;
3533b7f5c055SJed Brown 
3534b7f5c055SJed Brown                 for (PetscInt d = 0; d < 3; d++) vtxCoords[3 * thisVert + d] = patternCoords[v][d];
3535b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 0] += i * 2;
3536b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 1] += j * 2;
3537b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 2] += k * 2;
3538b7f5c055SJed Brown               }
3539b7f5c055SJed Brown             }
3540b7f5c055SJed Brown           }
3541b7f5c055SJed Brown         }
3542b7f5c055SJed Brown       }
3543b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocks; i++) {
3544b7f5c055SJed Brown         for (PetscInt f = 0; f < facesPerBlock; f++) {
3545ad540459SPierre Jolivet           for (PetscInt v = 0; v < 4; v++) cells[i][f][v] = vertToTrueVert[cells[i][f][v]];
3546b7f5c055SJed Brown         }
3547b7f5c055SJed Brown       }
35489566063dSJacob Faibussowitsch       PetscCall(PetscFree(vertToTrueVert));
35499566063dSJacob Faibussowitsch       PetscCall(PetscFree(seen));
3550b7f5c055SJed Brown       cells_flat = cells[0][0];
3551b7f5c055SJed Brown       numEdges   = 0;
3552b7f5c055SJed Brown       for (PetscInt i = 0; i < numFaces; i++) {
3553b7f5c055SJed Brown         for (PetscInt e = 0; e < 4; e++) {
3554b7f5c055SJed Brown           PetscInt         ev[]       = {cells_flat[i * 4 + e], cells_flat[i * 4 + ((e + 1) % 4)]};
3555b7f5c055SJed Brown           const PetscReal *evCoords[] = {&vtxCoords[3 * ev[0]], &vtxCoords[3 * ev[1]]};
3556b7f5c055SJed Brown 
3557b7f5c055SJed Brown           for (PetscInt d = 0; d < 3; d++) {
3558b7f5c055SJed Brown             if (!periodic || periodic[0] != DM_BOUNDARY_PERIODIC) {
3559b7f5c055SJed Brown               if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) numEdges++;
3560b7f5c055SJed Brown               if (evCoords[0][d] == 2. * extent[d] && evCoords[1][d] == 2. * extent[d]) numEdges++;
3561b7f5c055SJed Brown             }
3562b7f5c055SJed Brown           }
3563b7f5c055SJed Brown         }
3564b7f5c055SJed Brown       }
35659566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numEdges, &edges));
35669566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numEdges, &edgeSets));
3567b7f5c055SJed Brown       for (PetscInt edge = 0, i = 0; i < numFaces; i++) {
3568b7f5c055SJed Brown         for (PetscInt e = 0; e < 4; e++) {
3569b7f5c055SJed Brown           PetscInt         ev[]       = {cells_flat[i * 4 + e], cells_flat[i * 4 + ((e + 1) % 4)]};
3570b7f5c055SJed Brown           const PetscReal *evCoords[] = {&vtxCoords[3 * ev[0]], &vtxCoords[3 * ev[1]]};
3571b7f5c055SJed Brown 
3572b7f5c055SJed Brown           for (PetscInt d = 0; d < 3; d++) {
3573b7f5c055SJed Brown             if (!periodic || periodic[d] != DM_BOUNDARY_PERIODIC) {
3574b7f5c055SJed Brown               if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) {
3575b7f5c055SJed Brown                 edges[edge][0]   = ev[0];
3576b7f5c055SJed Brown                 edges[edge][1]   = ev[1];
3577b7f5c055SJed Brown                 edgeSets[edge++] = 2 * d;
3578b7f5c055SJed Brown               }
3579b7f5c055SJed Brown               if (evCoords[0][d] == 2. * extent[d] && evCoords[1][d] == 2. * extent[d]) {
3580b7f5c055SJed Brown                 edges[edge][0]   = ev[0];
3581b7f5c055SJed Brown                 edges[edge][1]   = ev[1];
3582b7f5c055SJed Brown                 edgeSets[edge++] = 2 * d + 1;
3583b7f5c055SJed Brown               }
3584b7f5c055SJed Brown             }
3585b7f5c055SJed Brown           }
3586b7f5c055SJed Brown         }
3587b7f5c055SJed Brown       }
3588b7f5c055SJed Brown     }
3589b7f5c055SJed Brown     evalFunc   = TPSEvaluate_Gyroid;
35904663dae6SJed Brown     normalFunc = TPSExtrudeNormalFunc_Gyroid;
3591b7f5c055SJed Brown     break;
3592b7f5c055SJed Brown   }
3593b7f5c055SJed Brown 
35949566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, topoDim));
3595c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildFromCellList(dm, numFaces, numVertices, 4, cells_flat));
35969566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildFromCellList(dm, 0, 0, 0, NULL));
35979566063dSJacob Faibussowitsch   PetscCall(PetscFree(cells_flat));
3598b7f5c055SJed Brown   {
3599b7f5c055SJed Brown     DM idm;
36009566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(dm, &idm));
360169d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &idm));
3602b7f5c055SJed Brown   }
3603c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildCoordinatesFromCellList(dm, spaceDim, vtxCoords));
36049566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildCoordinatesFromCellList(dm, spaceDim, NULL));
36059566063dSJacob Faibussowitsch   PetscCall(PetscFree(vtxCoords));
3606b7f5c055SJed Brown 
36079566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
36089566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "Face Sets", &label));
3609b7f5c055SJed Brown   for (PetscInt e = 0; e < numEdges; e++) {
3610b7f5c055SJed Brown     PetscInt        njoin;
3611b7f5c055SJed Brown     const PetscInt *join, verts[] = {numFaces + edges[e][0], numFaces + edges[e][1]};
36129566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, 2, verts, &njoin, &join));
361363a3b9bcSJacob 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]);
36149566063dSJacob Faibussowitsch     PetscCall(DMLabelSetValue(label, join[0], edgeSets[e]));
36159566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, 2, verts, &njoin, &join));
3616b7f5c055SJed Brown   }
36179566063dSJacob Faibussowitsch   PetscCall(PetscFree(edges));
36189566063dSJacob Faibussowitsch   PetscCall(PetscFree(edgeSets));
36191436d7faSJed Brown   if (tps_distribute) {
36201436d7faSJed Brown     DM               pdm = NULL;
36211436d7faSJed Brown     PetscPartitioner part;
36221436d7faSJed Brown 
36239566063dSJacob Faibussowitsch     PetscCall(DMPlexGetPartitioner(dm, &part));
36249566063dSJacob Faibussowitsch     PetscCall(PetscPartitionerSetFromOptions(part));
36259566063dSJacob Faibussowitsch     PetscCall(DMPlexDistribute(dm, 0, NULL, &pdm));
362648a46eb9SPierre Jolivet     if (pdm) PetscCall(DMPlexReplace_Internal(dm, &pdm));
36271436d7faSJed Brown     // Do not auto-distribute again
36289566063dSJacob Faibussowitsch     PetscCall(DMPlexDistributeSetDefault(dm, PETSC_FALSE));
36291436d7faSJed Brown   }
3630b7f5c055SJed Brown 
36319566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
3632b7f5c055SJed Brown   for (PetscInt refine = 0; refine < refinements; refine++) {
3633b7f5c055SJed Brown     PetscInt     m;
3634b7f5c055SJed Brown     DM           dmf;
3635b7f5c055SJed Brown     Vec          X;
3636b7f5c055SJed Brown     PetscScalar *x;
36379566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, MPI_COMM_NULL, &dmf));
363869d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &dmf));
3639b7f5c055SJed Brown 
36409566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &X));
36419566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(X, &m));
36429566063dSJacob Faibussowitsch     PetscCall(VecGetArray(X, &x));
364348a46eb9SPierre Jolivet     for (PetscInt i = 0; i < m; i += 3) PetscCall(TPSNearestPoint(evalFunc, &x[i]));
36449566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(X, &x));
3645b7f5c055SJed Brown   }
3646b7f5c055SJed Brown 
3647b7f5c055SJed Brown   // Face Sets has already been propagated to new vertices during refinement; this propagates to the initial vertices.
36489566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "Face Sets", &label));
36499566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete(dm, label));
3650b7f5c055SJed Brown 
365146139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
365246139095SJed Brown 
3653b7f5c055SJed Brown   if (thickness > 0) {
36544663dae6SJed Brown     DM              edm, cdm, ecdm;
36554663dae6SJed Brown     DMPlexTransform tr;
36564663dae6SJed Brown     const char     *prefix;
36574663dae6SJed Brown     PetscOptions    options;
36584663dae6SJed Brown     // Code from DMPlexExtrude
36594663dae6SJed Brown     PetscCall(DMPlexTransformCreate(PetscObjectComm((PetscObject)dm), &tr));
36604663dae6SJed Brown     PetscCall(DMPlexTransformSetDM(tr, dm));
36614663dae6SJed Brown     PetscCall(DMPlexTransformSetType(tr, DMPLEXEXTRUDE));
36624663dae6SJed Brown     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, &prefix));
36634663dae6SJed Brown     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)tr, prefix));
36644663dae6SJed Brown     PetscCall(PetscObjectGetOptions((PetscObject)dm, &options));
36654663dae6SJed Brown     PetscCall(PetscObjectSetOptions((PetscObject)tr, options));
36664663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetLayers(tr, layers));
36674663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetThickness(tr, thickness));
36684663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetTensor(tr, PETSC_FALSE));
36694663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetSymmetric(tr, PETSC_TRUE));
36704663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetNormalFunction(tr, normalFunc));
36714663dae6SJed Brown     PetscCall(DMPlexTransformSetFromOptions(tr));
36724663dae6SJed Brown     PetscCall(PetscObjectSetOptions((PetscObject)tr, NULL));
36734663dae6SJed Brown     PetscCall(DMPlexTransformSetUp(tr));
36744663dae6SJed Brown     PetscCall(PetscObjectViewFromOptions((PetscObject)tr, NULL, "-dm_plex_tps_transform_view"));
36754663dae6SJed Brown     PetscCall(DMPlexTransformApply(tr, dm, &edm));
36764663dae6SJed Brown     PetscCall(DMCopyDisc(dm, edm));
36774663dae6SJed Brown     PetscCall(DMGetCoordinateDM(dm, &cdm));
36784663dae6SJed Brown     PetscCall(DMGetCoordinateDM(edm, &ecdm));
36794663dae6SJed Brown     PetscCall(DMCopyDisc(cdm, ecdm));
36804663dae6SJed Brown     PetscCall(DMPlexTransformCreateDiscLabels(tr, edm));
36814663dae6SJed Brown     PetscCall(DMPlexTransformDestroy(&tr));
36824663dae6SJed Brown     if (edm) {
36834663dae6SJed Brown       ((DM_Plex *)edm->data)->printFEM    = ((DM_Plex *)dm->data)->printFEM;
36844663dae6SJed Brown       ((DM_Plex *)edm->data)->printL2     = ((DM_Plex *)dm->data)->printL2;
3685f5867de0SMatthew G. Knepley       ((DM_Plex *)edm->data)->printLocate = ((DM_Plex *)dm->data)->printLocate;
36864663dae6SJed Brown     }
368769d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &edm));
3688b7f5c055SJed Brown   }
36893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3690b7f5c055SJed Brown }
3691b7f5c055SJed Brown 
3692b7f5c055SJed Brown /*@
3693b7f5c055SJed Brown   DMPlexCreateTPSMesh - Create a distributed, interpolated mesh of a triply-periodic surface
3694b7f5c055SJed Brown 
3695b7f5c055SJed Brown   Collective
3696b7f5c055SJed Brown 
3697b7f5c055SJed Brown   Input Parameters:
3698a1cb98faSBarry Smith + comm           - The communicator for the `DM` object
3699b7f5c055SJed Brown . tpstype        - Type of triply-periodic surface
3700b7f5c055SJed Brown . extent         - Array of length 3 containing number of periods in each direction
370120f4b53cSBarry Smith . periodic       - array of length 3 with periodicity, or `NULL` for non-periodic
37021436d7faSJed Brown . tps_distribute - Distribute 2D manifold mesh prior to refinement and extrusion (more scalable)
3703817da375SSatish Balay . refinements    - Number of factor-of-2 refinements of 2D manifold mesh
37041436d7faSJed Brown . layers         - Number of cell layers extruded in normal direction
3705817da375SSatish Balay - thickness      - Thickness in normal direction
3706b7f5c055SJed Brown 
3707b7f5c055SJed Brown   Output Parameter:
3708a1cb98faSBarry Smith . dm - The `DM` object
3709a1cb98faSBarry Smith 
3710a1cb98faSBarry Smith   Level: beginner
3711b7f5c055SJed Brown 
3712b7f5c055SJed Brown   Notes:
371315229ffcSPierre Jolivet   This meshes the surface of the Schwarz P or Gyroid surfaces.  Schwarz P is the simplest member of the triply-periodic minimal surfaces.
37141d27aa22SBarry Smith   <https://en.wikipedia.org/wiki/Schwarz_minimal_surface#Schwarz_P_(%22Primitive%22)> and can be cut with "clean" boundaries.
37151d27aa22SBarry 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.
3716b7f5c055SJed Brown   Our implementation creates a very coarse mesh of the surface and refines (by 4-way splitting) as many times as requested.
3717b7f5c055SJed Brown   On each refinement, all vertices are projected to their nearest point on the surface.
3718b7f5c055SJed Brown   This projection could readily be extended to related surfaces.
3719b7f5c055SJed Brown 
37201d27aa22SBarry Smith   See {cite}`maskery2018insights`
37211d27aa22SBarry Smith 
37221d27aa22SBarry Smith   The face (edge) sets for the Schwarz P surface are numbered $1(-x), 2(+x), 3(-y), 4(+y), 5(-z), 6(+z)$.
37231d27aa22SBarry Smith   When the mesh is refined, "Face Sets" contain the new vertices (created during refinement).
37241d27aa22SBarry Smith   Use `DMPlexLabelComplete()` to propagate to coarse-level vertices.
3725b7f5c055SJed Brown 
372660225df5SJacob Faibussowitsch   Developer Notes:
3727b7f5c055SJed Brown   The Gyroid mesh does not currently mark boundary sets.
3728b7f5c055SJed Brown 
37291cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSphereMesh()`, `DMSetType()`, `DMCreate()`
3730b7f5c055SJed Brown @*/
3731d71ae5a4SJacob 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)
3732d71ae5a4SJacob Faibussowitsch {
3733b7f5c055SJed Brown   PetscFunctionBegin;
37349566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
37359566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
37369566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateTPSMesh_Internal(*dm, tpstype, extent, periodic, tps_distribute, refinements, layers, thickness));
37373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3738b7f5c055SJed Brown }
3739b7f5c055SJed Brown 
37409318fe57SMatthew G. Knepley /*@
37419318fe57SMatthew G. Knepley   DMPlexCreateSphereMesh - Creates a mesh on the d-dimensional sphere, S^d.
37429318fe57SMatthew G. Knepley 
37439318fe57SMatthew G. Knepley   Collective
37449318fe57SMatthew G. Knepley 
37459318fe57SMatthew G. Knepley   Input Parameters:
3746a1cb98faSBarry Smith + comm    - The communicator for the `DM` object
37479318fe57SMatthew G. Knepley . dim     - The dimension
37489318fe57SMatthew G. Knepley . simplex - Use simplices, or tensor product cells
37499318fe57SMatthew G. Knepley - R       - The radius
37509318fe57SMatthew G. Knepley 
37519318fe57SMatthew G. Knepley   Output Parameter:
3752a1cb98faSBarry Smith . dm - The `DM` object
37539318fe57SMatthew G. Knepley 
37549318fe57SMatthew G. Knepley   Level: beginner
37559318fe57SMatthew G. Knepley 
37561cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBallMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
37579318fe57SMatthew G. Knepley @*/
3758d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateSphereMesh(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscReal R, DM *dm)
3759d71ae5a4SJacob Faibussowitsch {
37609318fe57SMatthew G. Knepley   PetscFunctionBegin;
37614f572ea9SToby Isaac   PetscAssertPointer(dm, 5);
37629566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
37639566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
37649566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSphereMesh_Internal(*dm, dim, simplex, R));
37653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
37669318fe57SMatthew G. Knepley }
37679318fe57SMatthew G. Knepley 
3768d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBallMesh_Internal(DM dm, PetscInt dim, PetscReal R)
3769d71ae5a4SJacob Faibussowitsch {
37709318fe57SMatthew G. Knepley   DM          sdm, vol;
37719318fe57SMatthew G. Knepley   DMLabel     bdlabel;
3772dd2b43ebSStefano Zampini   const char *prefix;
37739318fe57SMatthew G. Knepley 
37749318fe57SMatthew G. Knepley   PetscFunctionBegin;
37759566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &sdm));
37769566063dSJacob Faibussowitsch   PetscCall(DMSetType(sdm, DMPLEX));
3777dd2b43ebSStefano Zampini   PetscCall(DMGetOptionsPrefix(dm, &prefix));
3778dd2b43ebSStefano Zampini   PetscCall(DMSetOptionsPrefix(sdm, prefix));
3779dd2b43ebSStefano Zampini   PetscCall(DMAppendOptionsPrefix(sdm, "bd_"));
3780dd2b43ebSStefano Zampini   PetscCall(DMPlexDistributeSetDefault(sdm, PETSC_FALSE));
37819566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSphereMesh_Internal(sdm, dim - 1, PETSC_TRUE, R));
37829566063dSJacob Faibussowitsch   PetscCall(DMSetFromOptions(sdm));
37839566063dSJacob Faibussowitsch   PetscCall(DMViewFromOptions(sdm, NULL, "-dm_view"));
37849566063dSJacob Faibussowitsch   PetscCall(DMPlexGenerate(sdm, NULL, PETSC_TRUE, &vol));
37859566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&sdm));
378669d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &vol));
37879566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
37889566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "marker", &bdlabel));
37899566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkBoundaryFaces(dm, PETSC_DETERMINE, bdlabel));
37909566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete(dm, bdlabel));
37913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
379251a74b61SMatthew G. Knepley }
379351a74b61SMatthew G. Knepley 
379451a74b61SMatthew G. Knepley /*@
379551a74b61SMatthew G. Knepley   DMPlexCreateBallMesh - Creates a simplex mesh on the d-dimensional ball, B^d.
379651a74b61SMatthew G. Knepley 
379751a74b61SMatthew G. Knepley   Collective
379851a74b61SMatthew G. Knepley 
379951a74b61SMatthew G. Knepley   Input Parameters:
3800a1cb98faSBarry Smith + comm - The communicator for the `DM` object
380151a74b61SMatthew G. Knepley . dim  - The dimension
380251a74b61SMatthew G. Knepley - R    - The radius
380351a74b61SMatthew G. Knepley 
380451a74b61SMatthew G. Knepley   Output Parameter:
3805a1cb98faSBarry Smith . dm - The `DM` object
380651a74b61SMatthew G. Knepley 
3807a1cb98faSBarry Smith   Options Database Key:
380860225df5SJacob Faibussowitsch . bd_dm_refine - This will refine the surface mesh preserving the sphere geometry
380951a74b61SMatthew G. Knepley 
381051a74b61SMatthew G. Knepley   Level: beginner
381151a74b61SMatthew G. Knepley 
38121cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSphereMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
381351a74b61SMatthew G. Knepley @*/
3814d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateBallMesh(MPI_Comm comm, PetscInt dim, PetscReal R, DM *dm)
3815d71ae5a4SJacob Faibussowitsch {
381651a74b61SMatthew G. Knepley   PetscFunctionBegin;
38179566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
38189566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
38199566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBallMesh_Internal(*dm, dim, R));
38203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
38212829fed8SMatthew G. Knepley }
38222829fed8SMatthew G. Knepley 
3823d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateReferenceCell_Internal(DM rdm, DMPolytopeType ct)
3824d71ae5a4SJacob Faibussowitsch {
38250a6ba040SMatthew G. Knepley   PetscFunctionBegin;
38269318fe57SMatthew G. Knepley   switch (ct) {
38279371c9d4SSatish Balay   case DM_POLYTOPE_POINT: {
38289318fe57SMatthew G. Knepley     PetscInt    numPoints[1]        = {1};
38299318fe57SMatthew G. Knepley     PetscInt    coneSize[1]         = {0};
38309318fe57SMatthew G. Knepley     PetscInt    cones[1]            = {0};
38319318fe57SMatthew G. Knepley     PetscInt    coneOrientations[1] = {0};
38329318fe57SMatthew G. Knepley     PetscScalar vertexCoords[1]     = {0.0};
38339318fe57SMatthew G. Knepley 
38349566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 0));
38359566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 0, numPoints, coneSize, cones, coneOrientations, vertexCoords));
38369371c9d4SSatish Balay   } break;
38379371c9d4SSatish Balay   case DM_POLYTOPE_SEGMENT: {
38389318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {2, 1};
38399318fe57SMatthew G. Knepley     PetscInt    coneSize[3]         = {2, 0, 0};
38409318fe57SMatthew G. Knepley     PetscInt    cones[2]            = {1, 2};
38419318fe57SMatthew G. Knepley     PetscInt    coneOrientations[2] = {0, 0};
38429318fe57SMatthew G. Knepley     PetscScalar vertexCoords[2]     = {-1.0, 1.0};
38439318fe57SMatthew G. Knepley 
38449566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 1));
38459566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
38469371c9d4SSatish Balay   } break;
38479371c9d4SSatish Balay   case DM_POLYTOPE_POINT_PRISM_TENSOR: {
3848b5a892a1SMatthew G. Knepley     PetscInt    numPoints[2]        = {2, 1};
3849b5a892a1SMatthew G. Knepley     PetscInt    coneSize[3]         = {2, 0, 0};
3850b5a892a1SMatthew G. Knepley     PetscInt    cones[2]            = {1, 2};
3851b5a892a1SMatthew G. Knepley     PetscInt    coneOrientations[2] = {0, 0};
3852b5a892a1SMatthew G. Knepley     PetscScalar vertexCoords[2]     = {-1.0, 1.0};
3853b5a892a1SMatthew G. Knepley 
38549566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 1));
38559566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
38569371c9d4SSatish Balay   } break;
38579371c9d4SSatish Balay   case DM_POLYTOPE_TRIANGLE: {
38589318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {3, 1};
38599318fe57SMatthew G. Knepley     PetscInt    coneSize[4]         = {3, 0, 0, 0};
38609318fe57SMatthew G. Knepley     PetscInt    cones[3]            = {1, 2, 3};
38619318fe57SMatthew G. Knepley     PetscInt    coneOrientations[3] = {0, 0, 0};
38629318fe57SMatthew G. Knepley     PetscScalar vertexCoords[6]     = {-1.0, -1.0, 1.0, -1.0, -1.0, 1.0};
38639318fe57SMatthew G. Knepley 
38649566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 2));
38659566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
38669371c9d4SSatish Balay   } break;
38679371c9d4SSatish Balay   case DM_POLYTOPE_QUADRILATERAL: {
38689318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {4, 1};
38699318fe57SMatthew G. Knepley     PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
38709318fe57SMatthew G. Knepley     PetscInt    cones[4]            = {1, 2, 3, 4};
38719318fe57SMatthew G. Knepley     PetscInt    coneOrientations[4] = {0, 0, 0, 0};
38729318fe57SMatthew G. Knepley     PetscScalar vertexCoords[8]     = {-1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0};
38739318fe57SMatthew G. Knepley 
38749566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 2));
38759566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
38769371c9d4SSatish Balay   } break;
38779371c9d4SSatish Balay   case DM_POLYTOPE_SEG_PRISM_TENSOR: {
38789318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {4, 1};
38799318fe57SMatthew G. Knepley     PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
38809318fe57SMatthew G. Knepley     PetscInt    cones[4]            = {1, 2, 3, 4};
38819318fe57SMatthew G. Knepley     PetscInt    coneOrientations[4] = {0, 0, 0, 0};
38829318fe57SMatthew G. Knepley     PetscScalar vertexCoords[8]     = {-1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0};
38839318fe57SMatthew G. Knepley 
38849566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 2));
38859566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
38869371c9d4SSatish Balay   } break;
38879371c9d4SSatish Balay   case DM_POLYTOPE_TETRAHEDRON: {
38889318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {4, 1};
38899318fe57SMatthew G. Knepley     PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
3890f0edb160SMatthew G. Knepley     PetscInt    cones[4]            = {1, 2, 3, 4};
38919318fe57SMatthew G. Knepley     PetscInt    coneOrientations[4] = {0, 0, 0, 0};
3892f0edb160SMatthew 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};
38939318fe57SMatthew G. Knepley 
38949566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
38959566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
38969371c9d4SSatish Balay   } break;
38979371c9d4SSatish Balay   case DM_POLYTOPE_HEXAHEDRON: {
38989318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {8, 1};
38999318fe57SMatthew G. Knepley     PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
3900f0edb160SMatthew G. Knepley     PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
39019318fe57SMatthew G. Knepley     PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
39029371c9d4SSatish 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};
39039318fe57SMatthew G. Knepley 
39049566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
39059566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
39069371c9d4SSatish Balay   } break;
39079371c9d4SSatish Balay   case DM_POLYTOPE_TRI_PRISM: {
39089318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {6, 1};
39099318fe57SMatthew G. Knepley     PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
3910f0edb160SMatthew G. Knepley     PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
39119318fe57SMatthew G. Knepley     PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
39129371c9d4SSatish 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};
39139318fe57SMatthew G. Knepley 
39149566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
39159566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
39169371c9d4SSatish Balay   } break;
39179371c9d4SSatish Balay   case DM_POLYTOPE_TRI_PRISM_TENSOR: {
39189318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {6, 1};
39199318fe57SMatthew G. Knepley     PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
39209318fe57SMatthew G. Knepley     PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
39219318fe57SMatthew G. Knepley     PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
39229371c9d4SSatish 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};
39239318fe57SMatthew G. Knepley 
39249566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
39259566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
39269371c9d4SSatish Balay   } break;
39279371c9d4SSatish Balay   case DM_POLYTOPE_QUAD_PRISM_TENSOR: {
39289318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {8, 1};
39299318fe57SMatthew G. Knepley     PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
39309318fe57SMatthew G. Knepley     PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
39319318fe57SMatthew G. Knepley     PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
39329371c9d4SSatish 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};
39339318fe57SMatthew G. Knepley 
39349566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
39359566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
39369371c9d4SSatish Balay   } break;
39379371c9d4SSatish Balay   case DM_POLYTOPE_PYRAMID: {
39389318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {5, 1};
39399318fe57SMatthew G. Knepley     PetscInt    coneSize[6]         = {5, 0, 0, 0, 0, 0};
3940f0edb160SMatthew G. Knepley     PetscInt    cones[5]            = {1, 2, 3, 4, 5};
39419318fe57SMatthew G. Knepley     PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
39429371c9d4SSatish 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};
39439318fe57SMatthew G. Knepley 
39449566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
39459566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
39469371c9d4SSatish Balay   } break;
3947d71ae5a4SJacob Faibussowitsch   default:
3948d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)rdm), PETSC_ERR_ARG_WRONG, "Cannot create reference cell for cell type %s", DMPolytopeTypes[ct]);
39499318fe57SMatthew G. Knepley   }
39509318fe57SMatthew G. Knepley   {
39519318fe57SMatthew G. Knepley     PetscInt Nv, v;
39529318fe57SMatthew G. Knepley 
39539318fe57SMatthew G. Knepley     /* Must create the celltype label here so that we do not automatically try to compute the types */
39549566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(rdm, "celltype"));
39559566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCellType(rdm, 0, ct));
39569566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(rdm, NULL, &Nv));
39579566063dSJacob Faibussowitsch     for (v = 1; v < Nv; ++v) PetscCall(DMPlexSetCellType(rdm, v, DM_POLYTOPE_POINT));
39589318fe57SMatthew G. Knepley   }
39599566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolateInPlace_Internal(rdm));
39609566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)rdm, DMPolytopeTypes[ct]));
39613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
39620a6ba040SMatthew G. Knepley }
39630a6ba040SMatthew G. Knepley 
39649318fe57SMatthew G. Knepley /*@
3965a1cb98faSBarry Smith   DMPlexCreateReferenceCell - Create a `DMPLEX` with the appropriate FEM reference cell
39669318fe57SMatthew G. Knepley 
39679318fe57SMatthew G. Knepley   Collective
39689318fe57SMatthew G. Knepley 
39699318fe57SMatthew G. Knepley   Input Parameters:
39709318fe57SMatthew G. Knepley + comm - The communicator
39719318fe57SMatthew G. Knepley - ct   - The cell type of the reference cell
39729318fe57SMatthew G. Knepley 
39739318fe57SMatthew G. Knepley   Output Parameter:
39749318fe57SMatthew G. Knepley . refdm - The reference cell
39759318fe57SMatthew G. Knepley 
39769318fe57SMatthew G. Knepley   Level: intermediate
39779318fe57SMatthew G. Knepley 
397842747ad1SJacob Faibussowitsch .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBoxMesh()`
39799318fe57SMatthew G. Knepley @*/
3980d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateReferenceCell(MPI_Comm comm, DMPolytopeType ct, DM *refdm)
3981d71ae5a4SJacob Faibussowitsch {
39820a6ba040SMatthew G. Knepley   PetscFunctionBegin;
39839566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, refdm));
39849566063dSJacob Faibussowitsch   PetscCall(DMSetType(*refdm, DMPLEX));
39859566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateReferenceCell_Internal(*refdm, ct));
39863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
39879318fe57SMatthew G. Knepley }
398879a015ccSMatthew G. Knepley 
3989d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoundaryLabel_Private(DM dm, const char name[])
3990d71ae5a4SJacob Faibussowitsch {
39919318fe57SMatthew G. Knepley   DM        plex;
39929318fe57SMatthew G. Knepley   DMLabel   label;
39939318fe57SMatthew G. Knepley   PetscBool hasLabel;
39940a6ba040SMatthew G. Knepley 
3995c22d3578SMatthew G. Knepley   PetscFunctionBegin;
39969566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &hasLabel));
39973ba16761SJacob Faibussowitsch   if (hasLabel) PetscFunctionReturn(PETSC_SUCCESS);
39989566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, name));
39999566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
40009566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
40019566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkBoundaryFaces(plex, 1, label));
40021c8afea9SMatthew G. Knepley   PetscCall(DMPlexLabelComplete(plex, label));
40039566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plex));
40043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
40059318fe57SMatthew G. Knepley }
4006acdc6f61SToby Isaac 
4007669647acSMatthew G. Knepley /*
4008669647acSMatthew 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.
4009669647acSMatthew G. Knepley 
4010669647acSMatthew G. Knepley     (x, y) -> (r, theta) = (x[1], (x[0] - lower[0]) * 2\pi/(upper[0] - lower[0]))
4011669647acSMatthew G. Knepley */
4012d71ae5a4SJacob 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[])
4013d71ae5a4SJacob Faibussowitsch {
4014669647acSMatthew G. Knepley   const PetscReal low = PetscRealPart(constants[0]);
4015669647acSMatthew G. Knepley   const PetscReal upp = PetscRealPart(constants[1]);
4016669647acSMatthew G. Knepley   const PetscReal r   = PetscRealPart(u[1]);
4017669647acSMatthew G. Knepley   const PetscReal th  = 2. * PETSC_PI * (PetscRealPart(u[0]) - low) / (upp - low);
4018669647acSMatthew G. Knepley 
4019669647acSMatthew G. Knepley   f0[0] = r * PetscCosReal(th);
4020669647acSMatthew G. Knepley   f0[1] = r * PetscSinReal(th);
4021669647acSMatthew G. Knepley }
4022669647acSMatthew G. Knepley 
40235390be7dSMatthew G. Knepley // Insert vertices and their joins, marked by depth
40245390be7dSMatthew G. Knepley static PetscErrorCode ProcessCohesiveLabel_Vertices(DM dm, DMLabel label, DMLabel vlabel, PetscInt val, PetscInt n, const PetscInt vertices[])
40255390be7dSMatthew G. Knepley {
40265390be7dSMatthew G. Knepley   PetscFunctionBegin;
40275390be7dSMatthew G. Knepley   PetscCall(DMPlexMarkSubmesh_Interpolated(dm, vlabel, val, PETSC_FALSE, PETSC_FALSE, label, NULL));
40285390be7dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
40295390be7dSMatthew G. Knepley }
40305390be7dSMatthew G. Knepley 
40315390be7dSMatthew G. Knepley // Insert faces and their closures, marked by depth
40325390be7dSMatthew G. Knepley static PetscErrorCode ProcessCohesiveLabel_Faces(DM dm, DMLabel label, PetscInt n, const PetscInt faces[])
40335390be7dSMatthew G. Knepley {
40345390be7dSMatthew G. Knepley   PetscFunctionBegin;
40355390be7dSMatthew G. Knepley   for (PetscInt p = 0; p < n; ++p) {
40365390be7dSMatthew G. Knepley     const PetscInt point   = faces[p];
40375390be7dSMatthew G. Knepley     PetscInt      *closure = NULL;
40385390be7dSMatthew G. Knepley     PetscInt       clSize, pdepth;
40395390be7dSMatthew G. Knepley 
40405390be7dSMatthew G. Knepley     PetscCall(DMPlexGetPointDepth(dm, point, &pdepth));
40415390be7dSMatthew G. Knepley     PetscCall(DMLabelSetValue(label, point, pdepth));
40425390be7dSMatthew G. Knepley     PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &clSize, &closure));
40435390be7dSMatthew G. Knepley     for (PetscInt cl = 0; cl < clSize * 2; cl += 2) {
40445390be7dSMatthew G. Knepley       PetscCall(DMPlexGetPointDepth(dm, closure[cl], &pdepth));
40455390be7dSMatthew G. Knepley       PetscCall(DMLabelSetValue(label, closure[cl], pdepth));
40465390be7dSMatthew G. Knepley     }
40475390be7dSMatthew G. Knepley     PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &clSize, &closure));
40485390be7dSMatthew G. Knepley   }
40495390be7dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
40505390be7dSMatthew G. Knepley }
40515390be7dSMatthew G. Knepley 
40524e22dd4cSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscOptionsFindPairPrefix_Private(PetscOptions, const char pre[], const char name[], const char *option[], const char *value[], PetscBool *flg);
40534e22dd4cSMatthew G. Knepley 
40545dca41c3SJed Brown const char *const DMPlexShapes[] = {"box", "box_surface", "ball", "sphere", "cylinder", "schwarz_p", "gyroid", "doublet", "annulus", "hypercubic", "zbox", "unknown", "DMPlexShape", "DM_SHAPE_", NULL};
40559318fe57SMatthew G. Knepley 
4056d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateFromOptions_Internal(PetscOptionItems *PetscOptionsObject, PetscBool *useCoordSpace, DM dm)
4057d71ae5a4SJacob Faibussowitsch {
40589318fe57SMatthew G. Knepley   DMPlexShape    shape   = DM_SHAPE_BOX;
40599318fe57SMatthew G. Knepley   DMPolytopeType cell    = DM_POLYTOPE_TRIANGLE;
40609318fe57SMatthew G. Knepley   PetscInt       dim     = 2;
40619318fe57SMatthew G. Knepley   PetscBool      simplex = PETSC_TRUE, interpolate = PETSC_TRUE, adjCone = PETSC_FALSE, adjClosure = PETSC_TRUE, refDomain = PETSC_FALSE;
4062d0812dedSMatthew G. Knepley   PetscBool      flg, flg2, fflg, strflg, bdfflg, nameflg;
40639318fe57SMatthew G. Knepley   MPI_Comm       comm;
4064ed5e4e85SVaclav Hapla   char           filename[PETSC_MAX_PATH_LEN]   = "<unspecified>";
4065ed5e4e85SVaclav Hapla   char           bdFilename[PETSC_MAX_PATH_LEN] = "<unspecified>";
4066ed5e4e85SVaclav Hapla   char           plexname[PETSC_MAX_PATH_LEN]   = "";
40674e22dd4cSMatthew G. Knepley   const char    *option;
40689318fe57SMatthew G. Knepley 
40699318fe57SMatthew G. Knepley   PetscFunctionBegin;
4070708be2fdSJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_CreateFromOptions, dm, 0, 0, 0));
40719566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
40729318fe57SMatthew G. Knepley   /* TODO Turn this into a registration interface */
40739566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_filename", "File containing a mesh", "DMPlexCreateFromFile", filename, filename, sizeof(filename), &fflg));
4074d0812dedSMatthew G. Knepley   PetscCall(PetscOptionsString("-dm_plex_file_contents", "Contents of a file format in a string", "DMPlexCreateFromFile", filename, filename, sizeof(filename), &strflg));
40759566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_boundary_filename", "File containing a mesh boundary", "DMPlexCreateFromFile", bdFilename, bdFilename, sizeof(bdFilename), &bdfflg));
40769566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_name", "Name of the mesh in the file", "DMPlexCreateFromFile", plexname, plexname, sizeof(plexname), &nameflg));
40779566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_plex_cell", "Cell shape", "", DMPolytopeTypes, (PetscEnum)cell, (PetscEnum *)&cell, NULL));
40789566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_reference_cell_domain", "Use a reference cell domain", "", refDomain, &refDomain, NULL));
40799566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_plex_shape", "Shape for built-in mesh", "", DMPlexShapes, (PetscEnum)shape, (PetscEnum *)&shape, &flg));
40809566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_dim", "Topological dimension of the mesh", "DMGetDimension", dim, &dim, &flg, 0));
40819566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_simplex", "Mesh cell shape", "", simplex, &simplex, &flg));
40829566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_interpolate", "Flag to create edges and faces automatically", "", interpolate, &interpolate, &flg));
40839566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_adj_cone", "Set adjacency direction", "DMSetBasicAdjacency", adjCone, &adjCone, &flg));
40849566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_adj_closure", "Set adjacency size", "DMSetBasicAdjacency", adjClosure, &adjClosure, &flg2));
40859566063dSJacob Faibussowitsch   if (flg || flg2) PetscCall(DMSetBasicAdjacency(dm, adjCone, adjClosure));
40869318fe57SMatthew G. Knepley 
408761a622f3SMatthew G. Knepley   switch (cell) {
408861a622f3SMatthew G. Knepley   case DM_POLYTOPE_POINT:
408961a622f3SMatthew G. Knepley   case DM_POLYTOPE_SEGMENT:
409061a622f3SMatthew G. Knepley   case DM_POLYTOPE_POINT_PRISM_TENSOR:
409161a622f3SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
409261a622f3SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
409361a622f3SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
4094d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_HEXAHEDRON:
4095d71ae5a4SJacob Faibussowitsch     *useCoordSpace = PETSC_TRUE;
4096d71ae5a4SJacob Faibussowitsch     break;
4097d71ae5a4SJacob Faibussowitsch   default:
4098d71ae5a4SJacob Faibussowitsch     *useCoordSpace = PETSC_FALSE;
4099d71ae5a4SJacob Faibussowitsch     break;
410061a622f3SMatthew G. Knepley   }
410161a622f3SMatthew G. Knepley 
41029318fe57SMatthew G. Knepley   if (fflg) {
41039318fe57SMatthew G. Knepley     DM          dmnew;
41041e4a82c4SMatthew G. Knepley     const char *name;
41059318fe57SMatthew G. Knepley 
41061e4a82c4SMatthew G. Knepley     PetscCall(PetscObjectGetName((PetscObject)dm, &name));
41071e4a82c4SMatthew G. Knepley     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), filename, nameflg ? plexname : name, interpolate, &dmnew));
41085de52c6dSVaclav Hapla     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
410969d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &dmnew));
41109318fe57SMatthew G. Knepley   } else if (refDomain) {
41119566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateReferenceCell_Internal(dm, cell));
41129318fe57SMatthew G. Knepley   } else if (bdfflg) {
41139318fe57SMatthew G. Knepley     DM          bdm, dmnew;
41141e4a82c4SMatthew G. Knepley     const char *name;
41159318fe57SMatthew G. Knepley 
41161e4a82c4SMatthew G. Knepley     PetscCall(PetscObjectGetName((PetscObject)dm, &name));
41171e4a82c4SMatthew G. Knepley     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), bdFilename, nameflg ? plexname : name, interpolate, &bdm));
41189566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)bdm, "bd_"));
41199566063dSJacob Faibussowitsch     PetscCall(DMSetFromOptions(bdm));
41209566063dSJacob Faibussowitsch     PetscCall(DMPlexGenerate(bdm, NULL, interpolate, &dmnew));
41219566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&bdm));
41225de52c6dSVaclav Hapla     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
412369d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &dmnew));
4124d0812dedSMatthew G. Knepley   } else if (strflg) {
4125d0812dedSMatthew G. Knepley     DM          dmnew;
4126d0812dedSMatthew G. Knepley     PetscViewer viewer;
4127d0812dedSMatthew G. Knepley     const char *contents;
4128d0812dedSMatthew G. Knepley     char       *strname;
4129d0812dedSMatthew G. Knepley     char        tmpdir[PETSC_MAX_PATH_LEN];
4130d0812dedSMatthew G. Knepley     char        tmpfilename[PETSC_MAX_PATH_LEN];
4131d0812dedSMatthew G. Knepley     char        name[PETSC_MAX_PATH_LEN];
4132d0812dedSMatthew G. Knepley     MPI_Comm    comm;
4133d0812dedSMatthew G. Knepley     PetscMPIInt rank;
4134d0812dedSMatthew G. Knepley 
4135d0812dedSMatthew G. Knepley     PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4136d0812dedSMatthew G. Knepley     PetscCallMPI(MPI_Comm_rank(comm, &rank));
4137d0812dedSMatthew G. Knepley     PetscCall(PetscStrchr(filename, ':', &strname));
4138d0812dedSMatthew G. Knepley     PetscCheck(strname, comm, PETSC_ERR_ARG_WRONG, "File contents must have the form \"ext:string_name\", not %s", filename);
4139d0812dedSMatthew G. Knepley     strname[0] = '\0';
4140d0812dedSMatthew G. Knepley     ++strname;
4141d0812dedSMatthew G. Knepley     PetscCall(PetscDLSym(NULL, strname, (void **)&contents));
4142d0812dedSMatthew G. Knepley     PetscCheck(contents, comm, PETSC_ERR_ARG_WRONG, "Could not locate mesh string %s", strname);
4143d0812dedSMatthew G. Knepley     PetscCall(PetscGetTmp(comm, tmpdir, PETSC_MAX_PATH_LEN));
4144ed32af8cSMatthew G. Knepley     PetscCall(PetscStrlcat(tmpdir, "/meshXXXXXX", PETSC_MAX_PATH_LEN));
4145ed32af8cSMatthew G. Knepley     PetscCall(PetscMkdtemp(tmpdir));
4146ed32af8cSMatthew G. Knepley     PetscCall(PetscSNPrintf(tmpfilename, PETSC_MAX_PATH_LEN, "%s/mesh.%s", tmpdir, filename));
4147d0812dedSMatthew G. Knepley     PetscCall(PetscViewerASCIIOpen(comm, tmpfilename, &viewer));
4148d0812dedSMatthew G. Knepley     PetscCall(PetscViewerASCIIPrintf(viewer, "%s\n", contents));
4149d0812dedSMatthew G. Knepley     PetscCall(PetscViewerDestroy(&viewer));
4150d0812dedSMatthew G. Knepley     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), tmpfilename, plexname, interpolate, &dmnew));
4151ed32af8cSMatthew G. Knepley     PetscCall(PetscRMTree(tmpdir));
4152d0812dedSMatthew G. Knepley     PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN, "%s Mesh", strname));
4153d0812dedSMatthew G. Knepley     PetscCall(PetscObjectSetName((PetscObject)dm, name));
4154d0812dedSMatthew G. Knepley     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
4155d0812dedSMatthew G. Knepley     PetscCall(DMPlexReplace_Internal(dm, &dmnew));
41569318fe57SMatthew G. Knepley   } else {
41579566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)dm, DMPlexShapes[shape]));
41589318fe57SMatthew G. Knepley     switch (shape) {
4159669647acSMatthew G. Knepley     case DM_SHAPE_BOX:
41605dca41c3SJed Brown     case DM_SHAPE_ZBOX:
4161669647acSMatthew G. Knepley     case DM_SHAPE_ANNULUS: {
41629318fe57SMatthew G. Knepley       PetscInt       faces[3]  = {0, 0, 0};
41639318fe57SMatthew G. Knepley       PetscReal      lower[3]  = {0, 0, 0};
41649318fe57SMatthew G. Knepley       PetscReal      upper[3]  = {1, 1, 1};
41659318fe57SMatthew G. Knepley       DMBoundaryType bdt[3]    = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
4166669647acSMatthew G. Knepley       PetscBool      isAnnular = shape == DM_SHAPE_ANNULUS ? PETSC_TRUE : PETSC_FALSE;
41679318fe57SMatthew G. Knepley       PetscInt       i, n;
41689318fe57SMatthew G. Knepley 
41699318fe57SMatthew G. Knepley       n = dim;
41709318fe57SMatthew G. Knepley       for (i = 0; i < dim; ++i) faces[i] = (dim == 1 ? 1 : 4 - dim);
41719566063dSJacob Faibussowitsch       PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg));
41729318fe57SMatthew G. Knepley       n = 3;
41739566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
417463a3b9bcSJacob Faibussowitsch       PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Lower box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
41759318fe57SMatthew G. Knepley       n = 3;
41769566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
417763a3b9bcSJacob Faibussowitsch       PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Upper box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
41789318fe57SMatthew G. Knepley       n = 3;
41799566063dSJacob Faibussowitsch       PetscCall(PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *)bdt, &n, &flg));
418063a3b9bcSJacob Faibussowitsch       PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Box boundary types had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
4181669647acSMatthew G. Knepley 
4182669647acSMatthew G. Knepley       PetscCheck(!isAnnular || dim == 2, comm, PETSC_ERR_ARG_OUTOFRANGE, "Only two dimensional annuli have been implemented");
4183669647acSMatthew G. Knepley       if (isAnnular)
4184669647acSMatthew G. Knepley         for (i = 0; i < dim - 1; ++i) bdt[i] = DM_BOUNDARY_PERIODIC;
4185669647acSMatthew G. Knepley 
41869318fe57SMatthew G. Knepley       switch (cell) {
418761a622f3SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
41889566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateWedgeBoxMesh_Internal(dm, faces, lower, upper, bdt));
4189d410b0cfSMatthew G. Knepley         if (!interpolate) {
4190d410b0cfSMatthew G. Knepley           DM udm;
4191d410b0cfSMatthew G. Knepley 
41929566063dSJacob Faibussowitsch           PetscCall(DMPlexUninterpolate(dm, &udm));
419369d8a87bSksagiyam           PetscCall(DMPlexReplace_Internal(dm, &udm));
4194d410b0cfSMatthew G. Knepley         }
41959318fe57SMatthew G. Knepley         break;
4196d71ae5a4SJacob Faibussowitsch       default:
41975dca41c3SJed Brown         PetscCall(DMPlexCreateBoxMesh_Internal(dm, shape, dim, simplex, faces, lower, upper, bdt, interpolate));
4198d71ae5a4SJacob Faibussowitsch         break;
41999318fe57SMatthew G. Knepley       }
4200669647acSMatthew G. Knepley       if (isAnnular) {
4201669647acSMatthew G. Knepley         DM          cdm;
4202669647acSMatthew G. Knepley         PetscDS     cds;
4203669647acSMatthew G. Knepley         PetscScalar bounds[2] = {lower[0], upper[0]};
4204669647acSMatthew G. Knepley 
4205669647acSMatthew G. Knepley         // Fix coordinates for annular region
4206669647acSMatthew G. Knepley         PetscCall(DMSetPeriodicity(dm, NULL, NULL, NULL));
4207669647acSMatthew G. Knepley         PetscCall(DMSetCellCoordinatesLocal(dm, NULL));
4208669647acSMatthew G. Knepley         PetscCall(DMSetCellCoordinates(dm, NULL));
4209e44f6aebSMatthew G. Knepley         PetscCall(DMPlexCreateCoordinateSpace(dm, 1, PETSC_TRUE, NULL));
4210669647acSMatthew G. Knepley         PetscCall(DMGetCoordinateDM(dm, &cdm));
4211669647acSMatthew G. Knepley         PetscCall(DMGetDS(cdm, &cds));
4212669647acSMatthew G. Knepley         PetscCall(PetscDSSetConstants(cds, 2, bounds));
4213669647acSMatthew G. Knepley         PetscCall(DMPlexRemapGeometry(dm, 0.0, boxToAnnulus));
4214669647acSMatthew G. Knepley       }
42159371c9d4SSatish Balay     } break;
42169371c9d4SSatish Balay     case DM_SHAPE_BOX_SURFACE: {
42179318fe57SMatthew G. Knepley       PetscInt  faces[3] = {0, 0, 0};
42189318fe57SMatthew G. Knepley       PetscReal lower[3] = {0, 0, 0};
42199318fe57SMatthew G. Knepley       PetscReal upper[3] = {1, 1, 1};
42209318fe57SMatthew G. Knepley       PetscInt  i, n;
42219318fe57SMatthew G. Knepley 
42229318fe57SMatthew G. Knepley       n = dim + 1;
42239318fe57SMatthew G. Knepley       for (i = 0; i < dim + 1; ++i) faces[i] = (dim + 1 == 1 ? 1 : 4 - (dim + 1));
42249566063dSJacob Faibussowitsch       PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg));
42259318fe57SMatthew G. Knepley       n = 3;
42269566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
422763a3b9bcSJacob 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);
42289318fe57SMatthew G. Knepley       n = 3;
42299566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
423063a3b9bcSJacob 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);
42319566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(dm, dim + 1, faces, lower, upper, interpolate));
42329371c9d4SSatish Balay     } break;
42339371c9d4SSatish Balay     case DM_SHAPE_SPHERE: {
42349318fe57SMatthew G. Knepley       PetscReal R = 1.0;
42359318fe57SMatthew G. Knepley 
42369566063dSJacob Faibussowitsch       PetscCall(PetscOptionsReal("-dm_plex_sphere_radius", "Radius of the sphere", "", R, &R, &flg));
42379566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateSphereMesh_Internal(dm, dim, simplex, R));
42389371c9d4SSatish Balay     } break;
42399371c9d4SSatish Balay     case DM_SHAPE_BALL: {
42409318fe57SMatthew G. Knepley       PetscReal R = 1.0;
42419318fe57SMatthew G. Knepley 
42429566063dSJacob Faibussowitsch       PetscCall(PetscOptionsReal("-dm_plex_ball_radius", "Radius of the ball", "", R, &R, &flg));
42439566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateBallMesh_Internal(dm, dim, R));
42449371c9d4SSatish Balay     } break;
42459371c9d4SSatish Balay     case DM_SHAPE_CYLINDER: {
42469318fe57SMatthew G. Knepley       DMBoundaryType bdt = DM_BOUNDARY_NONE;
42479318fe57SMatthew G. Knepley       PetscInt       Nw  = 6;
424849704ca5SMatthew G. Knepley       PetscInt       Nr  = 0;
42499318fe57SMatthew G. Knepley 
42509566063dSJacob Faibussowitsch       PetscCall(PetscOptionsEnum("-dm_plex_cylinder_bd", "Boundary type in the z direction", "", DMBoundaryTypes, (PetscEnum)bdt, (PetscEnum *)&bdt, NULL));
42519566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-dm_plex_cylinder_num_wedges", "Number of wedges around the cylinder", "", Nw, &Nw, NULL));
425249704ca5SMatthew G. Knepley       PetscCall(PetscOptionsInt("-dm_plex_cylinder_num_refine", "Number of refinements before projection", "", Nr, &Nr, NULL));
42539318fe57SMatthew G. Knepley       switch (cell) {
4254d71ae5a4SJacob Faibussowitsch       case DM_POLYTOPE_TRI_PRISM_TENSOR:
4255d71ae5a4SJacob Faibussowitsch         PetscCall(DMPlexCreateWedgeCylinderMesh_Internal(dm, Nw, interpolate));
4256d71ae5a4SJacob Faibussowitsch         break;
4257d71ae5a4SJacob Faibussowitsch       default:
425849704ca5SMatthew G. Knepley         PetscCall(DMPlexCreateHexCylinderMesh_Internal(dm, bdt, Nr));
4259d71ae5a4SJacob Faibussowitsch         break;
42609318fe57SMatthew G. Knepley       }
42619371c9d4SSatish Balay     } break;
4262b7f5c055SJed Brown     case DM_SHAPE_SCHWARZ_P: // fallthrough
42639371c9d4SSatish Balay     case DM_SHAPE_GYROID: {
4264b7f5c055SJed Brown       PetscInt       extent[3] = {1, 1, 1}, refine = 0, layers = 0, three;
4265b7f5c055SJed Brown       PetscReal      thickness   = 0.;
4266b7f5c055SJed Brown       DMBoundaryType periodic[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
4267b7f5c055SJed Brown       DMPlexTPSType  tps_type    = shape == DM_SHAPE_SCHWARZ_P ? DMPLEX_TPS_SCHWARZ_P : DMPLEX_TPS_GYROID;
42681436d7faSJed Brown       PetscBool      tps_distribute;
42699566063dSJacob Faibussowitsch       PetscCall(PetscOptionsIntArray("-dm_plex_tps_extent", "Number of replicas for each of three dimensions", NULL, extent, (three = 3, &three), NULL));
42709566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-dm_plex_tps_refine", "Number of refinements", NULL, refine, &refine, NULL));
42719566063dSJacob Faibussowitsch       PetscCall(PetscOptionsEnumArray("-dm_plex_tps_periodic", "Periodicity in each of three dimensions", NULL, DMBoundaryTypes, (PetscEnum *)periodic, (three = 3, &three), NULL));
42729566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-dm_plex_tps_layers", "Number of layers in volumetric extrusion (or zero to not extrude)", NULL, layers, &layers, NULL));
42739566063dSJacob Faibussowitsch       PetscCall(PetscOptionsReal("-dm_plex_tps_thickness", "Thickness of volumetric extrusion", NULL, thickness, &thickness, NULL));
42749566063dSJacob Faibussowitsch       PetscCall(DMPlexDistributeGetDefault(dm, &tps_distribute));
42759566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_tps_distribute", "Distribute the 2D mesh prior to refinement and extrusion", NULL, tps_distribute, &tps_distribute, NULL));
42769566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateTPSMesh_Internal(dm, tps_type, extent, periodic, tps_distribute, refine, layers, thickness));
42779371c9d4SSatish Balay     } break;
42789371c9d4SSatish Balay     case DM_SHAPE_DOUBLET: {
427905bd46c0SStefano Zampini       DM        dmnew;
428005bd46c0SStefano Zampini       PetscReal rl = 0.0;
428105bd46c0SStefano Zampini 
428205bd46c0SStefano Zampini       PetscCall(PetscOptionsReal("-dm_plex_doublet_refinementlimit", "Refinement limit", NULL, rl, &rl, NULL));
428305bd46c0SStefano Zampini       PetscCall(DMPlexCreateDoublet(PetscObjectComm((PetscObject)dm), dim, simplex, interpolate, rl, &dmnew));
42845de52c6dSVaclav Hapla       PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
428569d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &dmnew));
42869371c9d4SSatish Balay     } break;
4287cfb853baSMatthew G. Knepley     case DM_SHAPE_HYPERCUBIC: {
4288cfb853baSMatthew G. Knepley       PetscInt       *edges;
4289cfb853baSMatthew G. Knepley       PetscReal      *lower, *upper;
4290cfb853baSMatthew G. Knepley       DMBoundaryType *bdt;
4291cfb853baSMatthew G. Knepley       PetscInt        n, d;
4292cfb853baSMatthew G. Knepley 
4293cfb853baSMatthew G. Knepley       *useCoordSpace = PETSC_FALSE;
4294cfb853baSMatthew G. Knepley       PetscCall(PetscMalloc4(dim, &edges, dim, &lower, dim, &upper, dim, &bdt));
4295cfb853baSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
4296cfb853baSMatthew G. Knepley         edges[d] = 1;
4297cfb853baSMatthew G. Knepley         lower[d] = 0.;
4298cfb853baSMatthew G. Knepley         upper[d] = 1.;
4299cfb853baSMatthew G. Knepley         bdt[d]   = DM_BOUNDARY_PERIODIC;
4300cfb853baSMatthew G. Knepley       }
4301cfb853baSMatthew G. Knepley       n = dim;
4302cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", edges, &n, &flg));
4303cfb853baSMatthew G. Knepley       n = dim;
4304cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
4305cfb853baSMatthew 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);
4306cfb853baSMatthew G. Knepley       n = dim;
4307cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
4308cfb853baSMatthew 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);
4309cfb853baSMatthew G. Knepley       n = dim;
4310cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *)bdt, &n, &flg));
4311cfb853baSMatthew 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);
4312cfb853baSMatthew G. Knepley       PetscCall(DMPlexCreateHypercubicMesh_Internal(dm, dim, lower, upper, edges, bdt));
4313cfb853baSMatthew G. Knepley       PetscCall(PetscFree4(edges, lower, upper, bdt));
4314cfb853baSMatthew G. Knepley     } break;
4315d71ae5a4SJacob Faibussowitsch     default:
4316d71ae5a4SJacob Faibussowitsch       SETERRQ(comm, PETSC_ERR_SUP, "Domain shape %s is unsupported", DMPlexShapes[shape]);
43179318fe57SMatthew G. Knepley     }
43189318fe57SMatthew G. Knepley   }
43199566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
432048a46eb9SPierre Jolivet   if (!((PetscObject)dm)->name && nameflg) PetscCall(PetscObjectSetName((PetscObject)dm, plexname));
43214e22dd4cSMatthew G. Knepley   // Allow label creation
43224e22dd4cSMatthew G. Knepley   PetscCall(PetscOptionsFindPairPrefix_Private(NULL, ((PetscObject)dm)->prefix, "-dm_plex_label_", &option, NULL, &flg));
43234e22dd4cSMatthew G. Knepley   if (flg) {
43244e22dd4cSMatthew G. Knepley     DMLabel     label;
43254e22dd4cSMatthew G. Knepley     PetscInt    points[1024], n = 1024;
43264e22dd4cSMatthew G. Knepley     char        fulloption[PETSC_MAX_PATH_LEN];
43274e22dd4cSMatthew G. Knepley     const char *name = &option[14];
43284e22dd4cSMatthew G. Knepley 
43294e22dd4cSMatthew G. Knepley     PetscCall(DMCreateLabel(dm, name));
43304e22dd4cSMatthew G. Knepley     PetscCall(DMGetLabel(dm, name, &label));
43314e22dd4cSMatthew G. Knepley     fulloption[0] = '-';
43324e22dd4cSMatthew G. Knepley     fulloption[1] = 0;
43334e22dd4cSMatthew G. Knepley     PetscCall(PetscStrlcat(fulloption, option, PETSC_MAX_PATH_LEN));
43344e22dd4cSMatthew G. Knepley     PetscCall(PetscOptionsGetIntArray(NULL, ((PetscObject)dm)->prefix, fulloption, points, &n, NULL));
43354e22dd4cSMatthew G. Knepley     for (PetscInt p = 0; p < n; ++p) PetscCall(DMLabelSetValue(label, points[p], 1));
43364e22dd4cSMatthew G. Knepley   }
4337dd0eeac9SMatthew G. Knepley   // Allow cohesive label creation
4338dd0eeac9SMatthew G. Knepley   //   Faces are input, completed, and all points are marked with their depth
4339dd0eeac9SMatthew G. Knepley   PetscCall(PetscOptionsFindPairPrefix_Private(NULL, ((PetscObject)dm)->prefix, "-dm_plex_cohesive_label_", &option, NULL, &flg));
4340dd0eeac9SMatthew G. Knepley   if (flg) {
4341dd0eeac9SMatthew G. Knepley     DMLabel   label;
4342dd0eeac9SMatthew G. Knepley     PetscInt  points[1024], n, pStart, pEnd, Nl = 1;
43435390be7dSMatthew G. Knepley     PetscBool noCreate = PETSC_FALSE;
4344dd0eeac9SMatthew G. Knepley     char      fulloption[PETSC_MAX_PATH_LEN];
4345dd0eeac9SMatthew G. Knepley     char      name[PETSC_MAX_PATH_LEN];
4346dd0eeac9SMatthew G. Knepley     size_t    len;
4347dd0eeac9SMatthew G. Knepley 
4348dd0eeac9SMatthew G. Knepley     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
4349dd0eeac9SMatthew G. Knepley     PetscCall(PetscStrncpy(name, &option[23], PETSC_MAX_PATH_LEN));
4350dd0eeac9SMatthew G. Knepley     PetscCall(PetscStrlen(name, &len));
4351dd0eeac9SMatthew G. Knepley     if (name[len - 1] == '0') Nl = 10;
4352dd0eeac9SMatthew G. Knepley     for (PetscInt l = 0; l < Nl; ++l) {
4353dd0eeac9SMatthew G. Knepley       if (l > 0) name[len - 1] = '0' + l;
4354dd0eeac9SMatthew G. Knepley       fulloption[0] = 0;
4355dd0eeac9SMatthew G. Knepley       PetscCall(PetscStrlcat(fulloption, "-dm_plex_cohesive_label_", 32));
4356dd0eeac9SMatthew G. Knepley       PetscCall(PetscStrlcat(fulloption, name, PETSC_MAX_PATH_LEN - 32));
4357dd0eeac9SMatthew G. Knepley       n = 1024;
4358dd0eeac9SMatthew G. Knepley       PetscCall(PetscOptionsGetIntArray(NULL, ((PetscObject)dm)->prefix, fulloption, points, &n, &flg));
4359dd0eeac9SMatthew G. Knepley       if (!flg) break;
43605390be7dSMatthew G. Knepley       PetscCall(DMHasLabel(dm, name, &noCreate));
43615390be7dSMatthew G. Knepley       if (noCreate) {
43625390be7dSMatthew G. Knepley         DMLabel         inlabel;
43635390be7dSMatthew G. Knepley         IS              pointIS;
43645390be7dSMatthew G. Knepley         const PetscInt *lpoints;
43655390be7dSMatthew G. Knepley         PetscInt        pdep, ln, inval = points[0];
43665390be7dSMatthew G. Knepley         char            newname[PETSC_MAX_PATH_LEN];
43675390be7dSMatthew G. Knepley 
43685390be7dSMatthew G. Knepley         PetscCheck(n == 1, comm, PETSC_ERR_ARG_WRONG, "Must specify a label value with this option");
43695390be7dSMatthew G. Knepley         PetscCall(DMGetLabel(dm, name, &inlabel));
43705390be7dSMatthew G. Knepley         PetscCall(DMLabelGetStratumIS(inlabel, inval, &pointIS));
43715390be7dSMatthew G. Knepley         PetscCall(ISGetLocalSize(pointIS, &ln));
43725390be7dSMatthew G. Knepley         PetscCall(ISGetIndices(pointIS, &lpoints));
43735390be7dSMatthew G. Knepley         PetscCall(DMPlexGetPointDepth(dm, lpoints[0], &pdep));
43745390be7dSMatthew G. Knepley         PetscCall(PetscSNPrintf(newname, PETSC_MAX_PATH_LEN, "%s%" PetscInt_FMT, name, points[0]));
43755390be7dSMatthew G. Knepley         PetscCall(DMCreateLabel(dm, newname));
43765390be7dSMatthew G. Knepley         PetscCall(DMGetLabel(dm, newname, &label));
43775390be7dSMatthew G. Knepley         if (!pdep) PetscCall(ProcessCohesiveLabel_Vertices(dm, label, inlabel, inval, ln, lpoints));
43785390be7dSMatthew G. Knepley         else PetscCall(ProcessCohesiveLabel_Faces(dm, label, ln, lpoints));
43795390be7dSMatthew G. Knepley         PetscCall(ISRestoreIndices(pointIS, &lpoints));
43805390be7dSMatthew G. Knepley         PetscCall(ISDestroy(&pointIS));
43815390be7dSMatthew G. Knepley       } else {
4382dd0eeac9SMatthew G. Knepley         PetscCall(DMCreateLabel(dm, name));
4383dd0eeac9SMatthew G. Knepley         PetscCall(DMGetLabel(dm, name, &label));
4384dd0eeac9SMatthew G. Knepley         if (pStart >= pEnd) n = 0;
43855390be7dSMatthew G. Knepley         PetscCall(ProcessCohesiveLabel_Faces(dm, label, n, points));
4386dd0eeac9SMatthew G. Knepley       }
4387dd0eeac9SMatthew G. Knepley       PetscCall(DMPlexOrientLabel(dm, label));
43880542aa8cSMatthew G. Knepley       PetscCall(DMPlexLabelCohesiveComplete(dm, label, NULL, 1, PETSC_FALSE, PETSC_FALSE, NULL));
4389dd0eeac9SMatthew G. Knepley     }
4390dd0eeac9SMatthew G. Knepley   }
43915390be7dSMatthew G. Knepley   PetscCall(DMViewFromOptions(dm, NULL, "-created_dm_view"));
4392708be2fdSJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_CreateFromOptions, dm, 0, 0, 0));
43933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
43940a6ba040SMatthew G. Knepley }
43950a6ba040SMatthew G. Knepley 
4396d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions_NonRefinement_Plex(DM dm, PetscOptionItems *PetscOptionsObject)
4397d71ae5a4SJacob Faibussowitsch {
43980a6ba040SMatthew G. Knepley   DM_Plex  *mesh = (DM_Plex *)dm->data;
43997f9d8d6cSVaclav Hapla   PetscBool flg, flg2;
44009318fe57SMatthew G. Knepley   char      bdLabel[PETSC_MAX_PATH_LEN];
4401adc21957SMatthew G. Knepley   char      method[PETSC_MAX_PATH_LEN];
44020a6ba040SMatthew G. Knepley 
44030a6ba040SMatthew G. Knepley   PetscFunctionBegin;
44040a6ba040SMatthew G. Knepley   /* Handle viewing */
44059566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_print_set_values", "Output all set values info", "DMPlexMatSetClosure", PETSC_FALSE, &mesh->printSetValues, NULL));
44065962854dSMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_fem", "Debug output level for all fem computations", "DMPlexSNESComputeResidualFEM", 0, &mesh->printFEM, NULL, 0));
44075962854dSMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_fvm", "Debug output level for all fvm computations", "DMPlexSNESComputeResidualFVM", 0, &mesh->printFVM, NULL, 0));
44089566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-dm_plex_print_tol", "Tolerance for FEM output", "DMPlexSNESComputeResidualFEM", mesh->printTol, &mesh->printTol, NULL));
44099566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_l2", "Debug output level all L2 diff computations", "DMComputeL2Diff", 0, &mesh->printL2, NULL, 0));
4410f5867de0SMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_locate", "Debug output level all point location computations", "DMLocatePoints", 0, &mesh->printLocate, NULL, 0));
44119566063dSJacob Faibussowitsch   PetscCall(DMMonitorSetFromOptions(dm, "-dm_plex_monitor_throughput", "Monitor the simulation throughput", "DMPlexMonitorThroughput", DMPlexMonitorThroughput, NULL, &flg));
44129566063dSJacob Faibussowitsch   if (flg) PetscCall(PetscLogDefaultBegin());
44139318fe57SMatthew G. Knepley   /* Labeling */
44149566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_boundary_label", "Label to mark the mesh boundary", "", bdLabel, bdLabel, sizeof(bdLabel), &flg));
44159566063dSJacob Faibussowitsch   if (flg) PetscCall(DMPlexCreateBoundaryLabel_Private(dm, bdLabel));
4416953fc75cSMatthew G. Knepley   /* Point Location */
44179566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_hash_location", "Use grid hashing for point location", "DMInterpolate", PETSC_FALSE, &mesh->useHashLocation, NULL));
44180848f4b5SMatthew G. Knepley   /* Partitioning and distribution */
44199566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_partition_balance", "Attempt to evenly divide points on partition boundary between processes", "DMPlexSetPartitionBalance", PETSC_FALSE, &mesh->partitionBalance, NULL));
4420d02c7345SMatthew G. Knepley   /* Reordering */
4421adc21957SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_reorder_section", "Compute point permutation for local section", "DMReorderSectionSetDefault", PETSC_FALSE, &flg2, &flg));
4422adc21957SMatthew G. Knepley   if (flg) PetscCall(DMReorderSectionSetDefault(dm, flg2 ? DM_REORDER_DEFAULT_TRUE : DM_REORDER_DEFAULT_FALSE));
4423adc21957SMatthew G. Knepley   PetscCall(PetscOptionsString("-dm_reorder_section_type", "Reordering method for local section", "DMReorderSectionSetType", method, method, PETSC_MAX_PATH_LEN, &flg));
4424adc21957SMatthew G. Knepley   if (flg) PetscCall(DMReorderSectionSetType(dm, method));
44252e62ab5aSMatthew G. Knepley   /* Generation and remeshing */
44269566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_remesh_bd", "Allow changes to the boundary on remeshing", "DMAdapt", PETSC_FALSE, &mesh->remeshBd, NULL));
4427b29cfa1cSToby Isaac   /* Projection behavior */
4428d5b43468SJose E. Roman   PetscCall(PetscOptionsBoundedInt("-dm_plex_max_projection_height", "Maximum mesh point height used to project locally", "DMPlexSetMaxProjectionHeight", 0, &mesh->maxProjectionHeight, NULL, 0));
44299566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_regular_refinement", "Use special nested projection algorithm for regular refinement", "DMPlexSetRegularRefinement", mesh->regularRefinement, &mesh->regularRefinement, NULL));
4430f12cf164SMatthew G. Knepley   /* Checking structure */
4431f12cf164SMatthew G. Knepley   {
44327f9d8d6cSVaclav Hapla     PetscBool all = PETSC_FALSE;
4433f12cf164SMatthew G. Knepley 
44347f9d8d6cSVaclav Hapla     PetscCall(PetscOptionsBool("-dm_plex_check_all", "Perform all basic checks", "DMPlexCheck", PETSC_FALSE, &all, NULL));
44357f9d8d6cSVaclav Hapla     if (all) {
44367f9d8d6cSVaclav Hapla       PetscCall(DMPlexCheck(dm));
44377f9d8d6cSVaclav Hapla     } else {
44389566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_symmetry", "Check that the adjacency information in the mesh is symmetric", "DMPlexCheckSymmetry", PETSC_FALSE, &flg, &flg2));
44397f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckSymmetry(dm));
44409566063dSJacob 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));
44417f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckSkeleton(dm, 0));
44429566063dSJacob 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));
44437f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckFaces(dm, 0));
44449566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_geometry", "Check that cells have positive volume", "DMPlexCheckGeometry", PETSC_FALSE, &flg, &flg2));
44457f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckGeometry(dm));
44469566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_pointsf", "Check some necessary conditions for PointSF", "DMPlexCheckPointSF", PETSC_FALSE, &flg, &flg2));
4447d7d32a9aSMatthew G. Knepley       if (flg && flg2) PetscCall(DMPlexCheckPointSF(dm, NULL, PETSC_FALSE));
44489566063dSJacob 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));
44497f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckInterfaceCones(dm));
44507f9d8d6cSVaclav Hapla     }
44519566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-dm_plex_check_cell_shape", "Check cell shape", "DMPlexCheckCellShape", PETSC_FALSE, &flg, &flg2));
44529566063dSJacob Faibussowitsch     if (flg && flg2) PetscCall(DMPlexCheckCellShape(dm, PETSC_TRUE, PETSC_DETERMINE));
4453f12cf164SMatthew G. Knepley   }
44549318fe57SMatthew G. Knepley   {
44559318fe57SMatthew G. Knepley     PetscReal scale = 1.0;
44564f3833eaSMatthew G. Knepley 
44579566063dSJacob Faibussowitsch     PetscCall(PetscOptionsReal("-dm_plex_scale", "Scale factor for mesh coordinates", "DMPlexScale", scale, &scale, &flg));
44589318fe57SMatthew G. Knepley     if (flg) {
44599318fe57SMatthew G. Knepley       Vec coordinates, coordinatesLocal;
44609318fe57SMatthew G. Knepley 
44619566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinates(dm, &coordinates));
44629566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinatesLocal(dm, &coordinatesLocal));
44639566063dSJacob Faibussowitsch       PetscCall(VecScale(coordinates, scale));
44649566063dSJacob Faibussowitsch       PetscCall(VecScale(coordinatesLocal, scale));
44659318fe57SMatthew G. Knepley     }
44669318fe57SMatthew G. Knepley   }
44679566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerSetFromOptions(mesh->partitioner));
44683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
446968d4fef7SMatthew G. Knepley }
447068d4fef7SMatthew G. Knepley 
4471d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions_Overlap_Plex(DM dm, PetscOptionItems *PetscOptionsObject, PetscInt *overlap)
4472d71ae5a4SJacob Faibussowitsch {
4473c506a872SMatthew G. Knepley   PetscInt  numOvLabels = 16, numOvExLabels = 16;
4474c506a872SMatthew G. Knepley   char     *ovLabelNames[16], *ovExLabelNames[16];
4475c506a872SMatthew G. Knepley   PetscInt  numOvValues = 16, numOvExValues = 16, l;
4476c506a872SMatthew G. Knepley   PetscBool flg;
4477c506a872SMatthew G. Knepley 
4478c506a872SMatthew G. Knepley   PetscFunctionBegin;
4479c506a872SMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_distribute_overlap", "The size of the overlap halo", "DMPlexDistribute", *overlap, overlap, NULL, 0));
4480c506a872SMatthew G. Knepley   PetscCall(PetscOptionsStringArray("-dm_distribute_overlap_labels", "List of overlap label names", "DMPlexDistribute", ovLabelNames, &numOvLabels, &flg));
4481c506a872SMatthew G. Knepley   if (!flg) numOvLabels = 0;
4482c506a872SMatthew G. Knepley   if (numOvLabels) {
4483c506a872SMatthew G. Knepley     ((DM_Plex *)dm->data)->numOvLabels = numOvLabels;
4484c506a872SMatthew G. Knepley     for (l = 0; l < numOvLabels; ++l) {
4485c506a872SMatthew G. Knepley       PetscCall(DMGetLabel(dm, ovLabelNames[l], &((DM_Plex *)dm->data)->ovLabels[l]));
4486c506a872SMatthew G. Knepley       PetscCheck(((DM_Plex *)dm->data)->ovLabels[l], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid label name %s", ovLabelNames[l]);
4487c506a872SMatthew G. Knepley       PetscCall(PetscFree(ovLabelNames[l]));
4488c506a872SMatthew G. Knepley     }
4489c506a872SMatthew G. Knepley     PetscCall(PetscOptionsIntArray("-dm_distribute_overlap_values", "List of overlap label values", "DMPlexDistribute", ((DM_Plex *)dm->data)->ovValues, &numOvValues, &flg));
4490c506a872SMatthew G. Knepley     if (!flg) numOvValues = 0;
4491c506a872SMatthew 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);
4492c506a872SMatthew G. Knepley 
4493c506a872SMatthew G. Knepley     PetscCall(PetscOptionsStringArray("-dm_distribute_overlap_exclude_labels", "List of overlap exclude label names", "DMPlexDistribute", ovExLabelNames, &numOvExLabels, &flg));
4494c506a872SMatthew G. Knepley     if (!flg) numOvExLabels = 0;
4495c506a872SMatthew G. Knepley     ((DM_Plex *)dm->data)->numOvExLabels = numOvExLabels;
4496c506a872SMatthew G. Knepley     for (l = 0; l < numOvExLabels; ++l) {
4497c506a872SMatthew G. Knepley       PetscCall(DMGetLabel(dm, ovExLabelNames[l], &((DM_Plex *)dm->data)->ovExLabels[l]));
4498c506a872SMatthew G. Knepley       PetscCheck(((DM_Plex *)dm->data)->ovExLabels[l], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid label name %s", ovExLabelNames[l]);
4499c506a872SMatthew G. Knepley       PetscCall(PetscFree(ovExLabelNames[l]));
4500c506a872SMatthew G. Knepley     }
4501c506a872SMatthew G. Knepley     PetscCall(PetscOptionsIntArray("-dm_distribute_overlap_exclude_values", "List of overlap exclude label values", "DMPlexDistribute", ((DM_Plex *)dm->data)->ovExValues, &numOvExValues, &flg));
4502c506a872SMatthew G. Knepley     if (!flg) numOvExValues = 0;
4503c506a872SMatthew 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);
4504c506a872SMatthew G. Knepley   }
45053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4506c506a872SMatthew G. Knepley }
4507c506a872SMatthew G. Knepley 
4508d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetFromOptions_Plex(DM dm, PetscOptionItems *PetscOptionsObject)
4509d71ae5a4SJacob Faibussowitsch {
4510bdf63967SMatthew G. Knepley   PetscFunctionList    ordlist;
4511bdf63967SMatthew G. Knepley   char                 oname[256];
45124e22dd4cSMatthew G. Knepley   char                 sublabelname[PETSC_MAX_PATH_LEN] = "";
4513adc21957SMatthew G. Knepley   DMReorderDefaultFlag reorder;
4514d410b0cfSMatthew G. Knepley   PetscReal            volume    = -1.0;
45159318fe57SMatthew G. Knepley   PetscInt             prerefine = 0, refine = 0, r, coarsen = 0, overlap = 0, extLayers = 0, dim;
45161b742f01SMatthew 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;
451768d4fef7SMatthew G. Knepley 
451868d4fef7SMatthew G. Knepley   PetscFunctionBegin;
4519d0609cedSBarry Smith   PetscOptionsHeadBegin(PetscOptionsObject, "DMPlex Options");
4520dd4c3f67SMatthew G. Knepley   if (dm->cloneOpts) goto non_refine;
45219318fe57SMatthew G. Knepley   /* Handle automatic creation */
45229566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
45236bc1bd01Sksagiyam   if (dim < 0) {
45246bc1bd01Sksagiyam     PetscCall(DMPlexCreateFromOptions_Internal(PetscOptionsObject, &coordSpace, dm));
45256bc1bd01Sksagiyam     created = PETSC_TRUE;
45266bc1bd01Sksagiyam   }
45276bc1bd01Sksagiyam   PetscCall(DMGetDimension(dm, &dim));
4528d89e6e46SMatthew G. Knepley   /* Handle interpolation before distribution */
45299566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_interpolate_pre", "Flag to interpolate mesh before distribution", "", interpolate, &interpolate, &flg));
4530d89e6e46SMatthew G. Knepley   if (flg) {
4531d89e6e46SMatthew G. Knepley     DMPlexInterpolatedFlag interpolated;
4532d89e6e46SMatthew G. Knepley 
45339566063dSJacob Faibussowitsch     PetscCall(DMPlexIsInterpolated(dm, &interpolated));
4534d89e6e46SMatthew G. Knepley     if (interpolated == DMPLEX_INTERPOLATED_FULL && !interpolate) {
4535d89e6e46SMatthew G. Knepley       DM udm;
4536d89e6e46SMatthew G. Knepley 
45379566063dSJacob Faibussowitsch       PetscCall(DMPlexUninterpolate(dm, &udm));
453869d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &udm));
4539d89e6e46SMatthew G. Knepley     } else if (interpolated != DMPLEX_INTERPOLATED_FULL && interpolate) {
4540d89e6e46SMatthew G. Knepley       DM idm;
4541d89e6e46SMatthew G. Knepley 
45429566063dSJacob Faibussowitsch       PetscCall(DMPlexInterpolate(dm, &idm));
454369d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &idm));
4544d89e6e46SMatthew G. Knepley     }
4545d89e6e46SMatthew G. Knepley   }
45464e22dd4cSMatthew G. Knepley   // Handle submesh selection before distribution
45474e22dd4cSMatthew G. Knepley   PetscCall(PetscOptionsString("-dm_plex_submesh", "Label to use for submesh selection", "", sublabelname, sublabelname, PETSC_MAX_PATH_LEN, &flg));
45484e22dd4cSMatthew G. Knepley   if (flg) {
45494e22dd4cSMatthew G. Knepley     DM              subdm;
45504e22dd4cSMatthew G. Knepley     DMLabel         label;
45514e22dd4cSMatthew G. Knepley     IS              valueIS, pointIS;
45524e22dd4cSMatthew G. Knepley     const PetscInt *values, *points;
45534e22dd4cSMatthew G. Knepley     PetscBool       markedFaces = PETSC_FALSE;
45544e22dd4cSMatthew G. Knepley     PetscInt        Nv, value, Np;
45554e22dd4cSMatthew G. Knepley 
45564e22dd4cSMatthew G. Knepley     PetscCall(DMGetLabel(dm, sublabelname, &label));
45574e22dd4cSMatthew G. Knepley     PetscCall(DMLabelGetNumValues(label, &Nv));
45584e22dd4cSMatthew 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);
45594e22dd4cSMatthew G. Knepley     PetscCall(DMLabelGetValueIS(label, &valueIS));
45604e22dd4cSMatthew G. Knepley     PetscCall(ISGetIndices(valueIS, &values));
45614e22dd4cSMatthew G. Knepley     value = values[0];
45624e22dd4cSMatthew G. Knepley     PetscCall(ISRestoreIndices(valueIS, &values));
45634e22dd4cSMatthew G. Knepley     PetscCall(ISDestroy(&valueIS));
45644e22dd4cSMatthew G. Knepley     PetscCall(DMLabelGetStratumSize(label, value, &Np));
45654e22dd4cSMatthew G. Knepley     PetscCall(DMLabelGetStratumIS(label, value, &pointIS));
45664e22dd4cSMatthew G. Knepley     PetscCall(ISGetIndices(pointIS, &points));
45674e22dd4cSMatthew G. Knepley     for (PetscInt p = 0; p < Np; ++p) {
45684e22dd4cSMatthew G. Knepley       PetscInt pdepth;
45694e22dd4cSMatthew G. Knepley 
45704e22dd4cSMatthew G. Knepley       PetscCall(DMPlexGetPointDepth(dm, points[p], &pdepth));
45714e22dd4cSMatthew G. Knepley       if (pdepth) {
45724e22dd4cSMatthew G. Knepley         markedFaces = PETSC_TRUE;
45734e22dd4cSMatthew G. Knepley         break;
45744e22dd4cSMatthew G. Knepley       }
45754e22dd4cSMatthew G. Knepley     }
45764e22dd4cSMatthew G. Knepley     PetscCall(ISRestoreIndices(pointIS, &points));
45774e22dd4cSMatthew G. Knepley     PetscCall(ISDestroy(&pointIS));
45784e22dd4cSMatthew G. Knepley     PetscCall(DMPlexCreateSubmesh(dm, label, value, markedFaces, &subdm));
45794e22dd4cSMatthew G. Knepley     PetscCall(DMPlexReplace_Internal(dm, &subdm));
45804e22dd4cSMatthew G. Knepley     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
45814e22dd4cSMatthew G. Knepley   }
45829b44eab4SMatthew G. Knepley   /* Handle DMPlex refinement before distribution */
45839566063dSJacob Faibussowitsch   PetscCall(DMPlexGetRefinementUniform(dm, &uniformOrig));
45849566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine_pre", "The number of refinements before distribution", "DMCreate", prerefine, &prerefine, NULL, 0));
45859566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_remap_pre", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL));
45869566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_uniform_pre", "Flag for uniform refinement before distribution", "DMCreate", uniform, &uniform, &flg));
45879566063dSJacob Faibussowitsch   if (flg) PetscCall(DMPlexSetRefinementUniform(dm, uniform));
45889566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-dm_refine_volume_limit_pre", "The maximum cell volume after refinement before distribution", "DMCreate", volume, &volume, &flg));
45899318fe57SMatthew G. Knepley   if (flg) {
45909566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementUniform(dm, PETSC_FALSE));
45919566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementLimit(dm, volume));
45929318fe57SMatthew G. Knepley     prerefine = PetscMax(prerefine, 1);
45939318fe57SMatthew G. Knepley   }
4594b23db253SStefano Zampini   if (prerefine) PetscCall(DMLocalizeCoordinates(dm));
45959b44eab4SMatthew G. Knepley   for (r = 0; r < prerefine; ++r) {
45969b44eab4SMatthew G. Knepley     DM             rdm;
45979b44eab4SMatthew G. Knepley     PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc;
45989b44eab4SMatthew G. Knepley 
4599dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
46009566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &rdm));
460169d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &rdm));
4602dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
460361a622f3SMatthew G. Knepley     if (coordFunc && remap) {
46049566063dSJacob Faibussowitsch       PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
46059b44eab4SMatthew G. Knepley       ((DM_Plex *)dm->data)->coordFunc = coordFunc;
46069b44eab4SMatthew G. Knepley     }
46079b44eab4SMatthew G. Knepley   }
46089566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, uniformOrig));
46099318fe57SMatthew G. Knepley   /* Handle DMPlex extrusion before distribution */
46109566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_extrude", "The number of layers to extrude", "", extLayers, &extLayers, NULL, 0));
46119318fe57SMatthew G. Knepley   if (extLayers) {
46129318fe57SMatthew G. Knepley     DM edm;
46139318fe57SMatthew G. Knepley 
46149566063dSJacob Faibussowitsch     PetscCall(DMExtrude(dm, extLayers, &edm));
461569d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &edm));
461648d16a33SMatthew G. Knepley     ((DM_Plex *)dm->data)->coordFunc = NULL;
4617dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
4618d410b0cfSMatthew G. Knepley     extLayers = 0;
46195e17fc22SAidan Hamilton     PetscCall(DMGetDimension(dm, &dim));
46209318fe57SMatthew G. Knepley   }
4621bdf63967SMatthew G. Knepley   /* Handle DMPlex reordering before distribution */
46226bc1bd01Sksagiyam   PetscCall(DMPlexReorderGetDefault(dm, &reorder));
46239566063dSJacob Faibussowitsch   PetscCall(MatGetOrderingList(&ordlist));
46246bc1bd01Sksagiyam   PetscCall(PetscStrncpy(oname, MATORDERINGNATURAL, sizeof(oname)));
46259566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_plex_reorder", "Set mesh reordering type", "DMPlexGetOrdering", ordlist, MATORDERINGNATURAL, oname, sizeof(oname), &flg));
4626adc21957SMatthew G. Knepley   if (reorder == DM_REORDER_DEFAULT_TRUE || flg) {
4627bdf63967SMatthew G. Knepley     DM pdm;
4628bdf63967SMatthew G. Knepley     IS perm;
4629bdf63967SMatthew G. Knepley 
46309566063dSJacob Faibussowitsch     PetscCall(DMPlexGetOrdering(dm, oname, NULL, &perm));
46319566063dSJacob Faibussowitsch     PetscCall(DMPlexPermute(dm, perm, &pdm));
46329566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&perm));
463369d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &pdm));
4634dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
4635bdf63967SMatthew G. Knepley   }
46369b44eab4SMatthew G. Knepley   /* Handle DMPlex distribution */
46379566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeGetDefault(dm, &distribute));
4638c506a872SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_distribute", "Flag to redistribute a mesh among processes", "DMPlexDistribute", distribute, &distribute, NULL));
4639a286e215SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_distribute_save_sf", "Flag to save the migration SF", "DMPlexSetMigrationSF", saveSF, &saveSF, NULL));
4640dbbe0bcdSBarry Smith   PetscCall(DMSetFromOptions_Overlap_Plex(dm, PetscOptionsObject, &overlap));
46419b44eab4SMatthew G. Knepley   if (distribute) {
46429b44eab4SMatthew G. Knepley     DM               pdm = NULL;
46439b44eab4SMatthew G. Knepley     PetscPartitioner part;
4644a286e215SMatthew G. Knepley     PetscSF          sfMigration;
46459b44eab4SMatthew G. Knepley 
46469566063dSJacob Faibussowitsch     PetscCall(DMPlexGetPartitioner(dm, &part));
46479566063dSJacob Faibussowitsch     PetscCall(PetscPartitionerSetFromOptions(part));
4648a286e215SMatthew G. Knepley     PetscCall(DMPlexDistribute(dm, overlap, &sfMigration, &pdm));
464948a46eb9SPierre Jolivet     if (pdm) PetscCall(DMPlexReplace_Internal(dm, &pdm));
4650a286e215SMatthew G. Knepley     if (saveSF) PetscCall(DMPlexSetMigrationSF(dm, sfMigration));
4651a286e215SMatthew G. Knepley     PetscCall(PetscSFDestroy(&sfMigration));
46529b44eab4SMatthew G. Knepley   }
4653*4054ae39SJames Wright 
4654*4054ae39SJames Wright   {
4655*4054ae39SJames Wright     PetscBool useBoxLabel = PETSC_FALSE;
4656*4054ae39SJames Wright     PetscCall(PetscOptionsBool("-dm_plex_box_label", "Create 'Face Sets' assuming boundary faces align with cartesian directions", "DMCreate", useBoxLabel, &useBoxLabel, NULL));
4657*4054ae39SJames Wright     if (useBoxLabel) PetscCall(DMPlexSetBoxLabel_Internal(dm));
4658*4054ae39SJames Wright   }
4659d2b2dc1eSMatthew G. Knepley   /* Must check CEED options before creating function space for coordinates */
4660d2b2dc1eSMatthew G. Knepley   {
4661d2b2dc1eSMatthew G. Knepley     PetscBool useCeed = PETSC_FALSE, flg;
4662d2b2dc1eSMatthew G. Knepley 
4663d2b2dc1eSMatthew G. Knepley     PetscCall(PetscOptionsBool("-dm_plex_use_ceed", "Use LibCEED as the FEM backend", "DMPlexSetUseCeed", useCeed, &useCeed, &flg));
4664d2b2dc1eSMatthew G. Knepley     if (flg) PetscCall(DMPlexSetUseCeed(dm, useCeed));
4665d2b2dc1eSMatthew G. Knepley   }
46669318fe57SMatthew G. Knepley   /* Create coordinate space */
46679318fe57SMatthew G. Knepley   if (created) {
466861a622f3SMatthew G. Knepley     DM_Plex  *mesh   = (DM_Plex *)dm->data;
4669e44f6aebSMatthew G. Knepley     PetscInt  degree = 1, deg;
46705515ebd3SMatthew G. Knepley     PetscInt  height = 0;
46715515ebd3SMatthew G. Knepley     DM        cdm;
4672c3db174cSMatthew G. Knepley     PetscBool flg, localize = PETSC_TRUE, sparseLocalize = PETSC_TRUE;
46739318fe57SMatthew G. Knepley 
46749566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-dm_coord_space", "Use an FEM space for coordinates", "", coordSpace, &coordSpace, &flg));
46759566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-dm_coord_petscspace_degree", "FEM degree for coordinate space", "", degree, &degree, NULL));
4676e44f6aebSMatthew G. Knepley     PetscCall(DMGetCoordinateDegree_Internal(dm, &deg));
4677e44f6aebSMatthew G. Knepley     if (coordSpace && deg <= 1) PetscCall(DMPlexCreateCoordinateSpace(dm, degree, PETSC_TRUE, mesh->coordFunc));
46785515ebd3SMatthew G. Knepley     PetscCall(DMGetCoordinateDM(dm, &cdm));
467961a622f3SMatthew G. Knepley     if (flg && !coordSpace) {
468061a622f3SMatthew G. Knepley       PetscDS      cds;
468161a622f3SMatthew G. Knepley       PetscObject  obj;
468261a622f3SMatthew G. Knepley       PetscClassId id;
468361a622f3SMatthew G. Knepley 
46849566063dSJacob Faibussowitsch       PetscCall(DMGetDS(cdm, &cds));
46859566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
46869566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
468761a622f3SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
468861a622f3SMatthew G. Knepley         PetscContainer dummy;
468961a622f3SMatthew G. Knepley 
46909566063dSJacob Faibussowitsch         PetscCall(PetscContainerCreate(PETSC_COMM_SELF, &dummy));
46919566063dSJacob Faibussowitsch         PetscCall(PetscObjectSetName((PetscObject)dummy, "coordinates"));
46929566063dSJacob Faibussowitsch         PetscCall(DMSetField(cdm, 0, NULL, (PetscObject)dummy));
46939566063dSJacob Faibussowitsch         PetscCall(PetscContainerDestroy(&dummy));
46949566063dSJacob Faibussowitsch         PetscCall(DMClearDS(cdm));
469561a622f3SMatthew G. Knepley       }
469661a622f3SMatthew G. Knepley       mesh->coordFunc = NULL;
469761a622f3SMatthew G. Knepley     }
4698c3db174cSMatthew G. Knepley     PetscCall(PetscOptionsBool("-dm_localize", "Localize mesh coordinates", "", localize, &localize, NULL));
4699c3db174cSMatthew G. Knepley     PetscCall(PetscOptionsBool("-dm_sparse_localize", "Localize only necessary cells", "DMSetSparseLocalize", sparseLocalize, &sparseLocalize, &flg));
4700c3db174cSMatthew G. Knepley     if (flg) PetscCall(DMSetSparseLocalize(dm, sparseLocalize));
47015515ebd3SMatthew G. Knepley     PetscCall(PetscOptionsInt("-dm_localize_height", "Localize edges and faces in addition to cells", "", height, &height, &flg));
47025515ebd3SMatthew G. Knepley     if (flg) PetscCall(DMPlexSetMaxProjectionHeight(cdm, height));
4703c3db174cSMatthew G. Knepley     if (localize) PetscCall(DMLocalizeCoordinates(dm));
47049318fe57SMatthew G. Knepley   }
470568d4fef7SMatthew G. Knepley   /* Handle DMPlex refinement */
470661a622f3SMatthew G. Knepley   remap = PETSC_TRUE;
47079566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine", "The number of uniform refinements", "DMCreate", refine, &refine, NULL, 0));
47089566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_remap", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL));
47099566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine_hierarchy", "The number of uniform refinements", "DMCreate", refine, &refine, &isHierarchy, 0));
47109566063dSJacob Faibussowitsch   if (refine) PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
471168d4fef7SMatthew G. Knepley   if (refine && isHierarchy) {
4712acdc6f61SToby Isaac     DM *dms, coarseDM;
471368d4fef7SMatthew G. Knepley 
47149566063dSJacob Faibussowitsch     PetscCall(DMGetCoarseDM(dm, &coarseDM));
47159566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)coarseDM));
47169566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(refine, &dms));
47179566063dSJacob Faibussowitsch     PetscCall(DMRefineHierarchy(dm, refine, dms));
471868d4fef7SMatthew G. Knepley     /* Total hack since we do not pass in a pointer */
47199566063dSJacob Faibussowitsch     PetscCall(DMPlexSwap_Static(dm, dms[refine - 1]));
472068d4fef7SMatthew G. Knepley     if (refine == 1) {
47219566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dm, dms[0]));
47229566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dm, PETSC_TRUE));
472368d4fef7SMatthew G. Knepley     } else {
47249566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dm, dms[refine - 2]));
47259566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dm, PETSC_TRUE));
47269566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dms[0], dms[refine - 1]));
47279566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dms[0], PETSC_TRUE));
472868d4fef7SMatthew G. Knepley     }
47299566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dms[refine - 1], coarseDM));
47309566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)coarseDM));
473168d4fef7SMatthew G. Knepley     /* Free DMs */
473268d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
4733dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dms[r], PetscOptionsObject));
47349566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dms[r]));
473568d4fef7SMatthew G. Knepley     }
47369566063dSJacob Faibussowitsch     PetscCall(PetscFree(dms));
473768d4fef7SMatthew G. Knepley   } else {
473868d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
47399318fe57SMatthew G. Knepley       DM             rdm;
474051a74b61SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc;
474168d4fef7SMatthew G. Knepley 
4742dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
47439566063dSJacob Faibussowitsch       PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &rdm));
474468d4fef7SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
474569d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &rdm));
4746dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
474761a622f3SMatthew G. Knepley       if (coordFunc && remap) {
47489566063dSJacob Faibussowitsch         PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
474951a74b61SMatthew G. Knepley         ((DM_Plex *)dm->data)->coordFunc = coordFunc;
475051a74b61SMatthew G. Knepley       }
475168d4fef7SMatthew G. Knepley     }
475268d4fef7SMatthew G. Knepley   }
47533cf6fe12SMatthew G. Knepley   /* Handle DMPlex coarsening */
47549566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_coarsen", "Coarsen the mesh", "DMCreate", coarsen, &coarsen, NULL, 0));
47559566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_coarsen_hierarchy", "The number of coarsenings", "DMCreate", coarsen, &coarsen, &isHierarchy, 0));
4756b653a561SMatthew G. Knepley   if (coarsen && isHierarchy) {
4757b653a561SMatthew G. Knepley     DM *dms;
4758b653a561SMatthew G. Knepley 
47599566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(coarsen, &dms));
47609566063dSJacob Faibussowitsch     PetscCall(DMCoarsenHierarchy(dm, coarsen, dms));
4761b653a561SMatthew G. Knepley     /* Free DMs */
4762b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
4763dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dms[r], PetscOptionsObject));
47649566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dms[r]));
4765b653a561SMatthew G. Knepley     }
47669566063dSJacob Faibussowitsch     PetscCall(PetscFree(dms));
4767b653a561SMatthew G. Knepley   } else {
4768b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
47699318fe57SMatthew G. Knepley       DM             cdm;
47709318fe57SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc;
47713cf6fe12SMatthew G. Knepley 
4772dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
47739566063dSJacob Faibussowitsch       PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &cdm));
47743cf6fe12SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
477569d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &cdm));
4776dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
47779318fe57SMatthew G. Knepley       if (coordFunc) {
47789566063dSJacob Faibussowitsch         PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
47799318fe57SMatthew G. Knepley         ((DM_Plex *)dm->data)->coordFunc = coordFunc;
47809318fe57SMatthew G. Knepley       }
47813cf6fe12SMatthew G. Knepley     }
4782b653a561SMatthew G. Knepley   }
4783be664eb1SMatthew G. Knepley   // Handle coordinate remapping
4784be664eb1SMatthew G. Knepley   remap = PETSC_FALSE;
4785be664eb1SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_coord_remap", "Flag to control coordinate remapping", "", remap, &remap, NULL));
4786be664eb1SMatthew G. Knepley   if (remap) {
4787be664eb1SMatthew G. Knepley     DMPlexCoordMap map     = DM_COORD_MAP_NONE;
4788be664eb1SMatthew G. Knepley     PetscPointFunc mapFunc = NULL;
4789be664eb1SMatthew G. Knepley     PetscScalar    params[16];
4790f45b553cSPierre Jolivet     PetscInt       Np = PETSC_STATIC_ARRAY_LENGTH(params), cdim;
4791be664eb1SMatthew G. Knepley     MPI_Comm       comm;
4792be664eb1SMatthew G. Knepley 
4793be664eb1SMatthew G. Knepley     PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4794be664eb1SMatthew G. Knepley     PetscCall(DMGetCoordinateDim(dm, &cdim));
4795be664eb1SMatthew G. Knepley     PetscCall(PetscOptionsScalarArray("-dm_coord_map_params", "Parameters for the coordinate remapping", "", params, &Np, &flg));
4796be664eb1SMatthew G. Knepley     if (!flg) Np = 0;
4797be664eb1SMatthew G. Knepley     // TODO Allow user to pass a map function by name
4798be664eb1SMatthew G. Knepley     PetscCall(PetscOptionsEnum("-dm_coord_map", "Coordinate mapping for built-in mesh", "", DMPlexCoordMaps, (PetscEnum)map, (PetscEnum *)&map, &flg));
4799be664eb1SMatthew G. Knepley     if (flg) {
4800be664eb1SMatthew G. Knepley       switch (map) {
4801be664eb1SMatthew G. Knepley       case DM_COORD_MAP_NONE:
4802be664eb1SMatthew G. Knepley         mapFunc = coordMap_identity;
4803be664eb1SMatthew G. Knepley         break;
4804be664eb1SMatthew G. Knepley       case DM_COORD_MAP_SHEAR:
4805be664eb1SMatthew G. Knepley         mapFunc = coordMap_shear;
4806be664eb1SMatthew G. Knepley         if (!Np) {
4807be664eb1SMatthew G. Knepley           Np        = cdim + 1;
4808be664eb1SMatthew G. Knepley           params[0] = 0;
4809be664eb1SMatthew G. Knepley           for (PetscInt d = 1; d <= cdim; ++d) params[d] = 1.0;
4810be664eb1SMatthew G. Knepley         }
4811be664eb1SMatthew 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);
4812be664eb1SMatthew G. Knepley         break;
4813be664eb1SMatthew G. Knepley       case DM_COORD_MAP_FLARE:
4814be664eb1SMatthew G. Knepley         mapFunc = coordMap_flare;
4815be664eb1SMatthew G. Knepley         if (!Np) {
4816be664eb1SMatthew G. Knepley           Np        = cdim + 1;
4817be664eb1SMatthew G. Knepley           params[0] = 0;
4818be664eb1SMatthew G. Knepley           for (PetscInt d = 1; d <= cdim; ++d) params[d] = 1.0;
4819be664eb1SMatthew G. Knepley         }
4820be664eb1SMatthew 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);
4821be664eb1SMatthew G. Knepley         break;
4822be664eb1SMatthew G. Knepley       case DM_COORD_MAP_ANNULUS:
4823be664eb1SMatthew G. Knepley         mapFunc = coordMap_annulus;
4824be664eb1SMatthew G. Knepley         if (!Np) {
4825be664eb1SMatthew G. Knepley           Np        = 2;
4826be664eb1SMatthew G. Knepley           params[0] = 1.;
4827be664eb1SMatthew G. Knepley           params[1] = 2.;
4828be664eb1SMatthew G. Knepley         }
4829be664eb1SMatthew G. Knepley         PetscCheck(Np == 2, comm, PETSC_ERR_ARG_WRONG, "The annulus coordinate map must have 2 parameters, not %" PetscInt_FMT, Np);
4830be664eb1SMatthew G. Knepley         break;
4831be664eb1SMatthew G. Knepley       case DM_COORD_MAP_SHELL:
4832be664eb1SMatthew G. Knepley         mapFunc = coordMap_shell;
4833be664eb1SMatthew G. Knepley         if (!Np) {
4834be664eb1SMatthew G. Knepley           Np        = 2;
4835be664eb1SMatthew G. Knepley           params[0] = 1.;
4836be664eb1SMatthew G. Knepley           params[1] = 2.;
4837be664eb1SMatthew G. Knepley         }
4838be664eb1SMatthew G. Knepley         PetscCheck(Np == 2, comm, PETSC_ERR_ARG_WRONG, "The spherical shell coordinate map must have 2 parameters, not %" PetscInt_FMT, Np);
4839be664eb1SMatthew G. Knepley         break;
4840be664eb1SMatthew G. Knepley       default:
4841be664eb1SMatthew G. Knepley         mapFunc = coordMap_identity;
4842be664eb1SMatthew G. Knepley       }
4843be664eb1SMatthew G. Knepley     }
4844be664eb1SMatthew G. Knepley     if (Np) {
4845be664eb1SMatthew G. Knepley       DM      cdm;
4846be664eb1SMatthew G. Knepley       PetscDS cds;
4847be664eb1SMatthew G. Knepley 
4848be664eb1SMatthew G. Knepley       PetscCall(DMGetCoordinateDM(dm, &cdm));
4849be664eb1SMatthew G. Knepley       PetscCall(DMGetDS(cdm, &cds));
4850be664eb1SMatthew G. Knepley       PetscCall(PetscDSSetConstants(cds, Np, params));
4851be664eb1SMatthew G. Knepley     }
4852be664eb1SMatthew G. Knepley     PetscCall(DMPlexRemapGeometry(dm, 0.0, mapFunc));
4853be664eb1SMatthew G. Knepley   }
4854909dfd52SMatthew G. Knepley   /* Handle ghost cells */
48559566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_create_fv_ghost_cells", "Flag to create finite volume ghost cells on the boundary", "DMCreate", ghostCells, &ghostCells, NULL));
4856909dfd52SMatthew G. Knepley   if (ghostCells) {
4857909dfd52SMatthew G. Knepley     DM   gdm;
4858909dfd52SMatthew G. Knepley     char lname[PETSC_MAX_PATH_LEN];
4859909dfd52SMatthew G. Knepley 
4860909dfd52SMatthew G. Knepley     lname[0] = '\0';
48619566063dSJacob Faibussowitsch     PetscCall(PetscOptionsString("-dm_plex_fv_ghost_cells_label", "Label name for ghost cells boundary", "DMCreate", lname, lname, sizeof(lname), &flg));
48629566063dSJacob Faibussowitsch     PetscCall(DMPlexConstructGhostCells(dm, flg ? lname : NULL, NULL, &gdm));
486369d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &gdm));
4864909dfd52SMatthew G. Knepley   }
48656913077dSMatthew G. Knepley   /* Handle 1D order */
4866adc21957SMatthew G. Knepley   if (reorder != DM_REORDER_DEFAULT_FALSE && dim == 1) {
48676913077dSMatthew G. Knepley     DM           cdm, rdm;
48686913077dSMatthew G. Knepley     PetscDS      cds;
48696913077dSMatthew G. Knepley     PetscObject  obj;
48706913077dSMatthew G. Knepley     PetscClassId id = PETSC_OBJECT_CLASSID;
48716913077dSMatthew G. Knepley     IS           perm;
48726bc1bd01Sksagiyam     PetscInt     Nf;
48736913077dSMatthew G. Knepley     PetscBool    distributed;
48746913077dSMatthew G. Knepley 
48759566063dSJacob Faibussowitsch     PetscCall(DMPlexIsDistributed(dm, &distributed));
48769566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
48779566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
48789566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(cds, &Nf));
48796913077dSMatthew G. Knepley     if (Nf) {
48809566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
48819566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
48826913077dSMatthew G. Knepley     }
48836bc1bd01Sksagiyam     if (!distributed && id != PETSCFE_CLASSID) {
48849566063dSJacob Faibussowitsch       PetscCall(DMPlexGetOrdering1D(dm, &perm));
48859566063dSJacob Faibussowitsch       PetscCall(DMPlexPermute(dm, perm, &rdm));
488669d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &rdm));
48879566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&perm));
48886913077dSMatthew G. Knepley     }
48896913077dSMatthew G. Knepley   }
48903cf6fe12SMatthew G. Knepley /* Handle */
4891dd4c3f67SMatthew G. Knepley non_refine:
4892dbbe0bcdSBarry Smith   PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
4893d0609cedSBarry Smith   PetscOptionsHeadEnd();
48943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
48950a6ba040SMatthew G. Knepley }
48960a6ba040SMatthew G. Knepley 
4897d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMCreateGlobalVector_Plex(DM dm, Vec *vec)
4898d71ae5a4SJacob Faibussowitsch {
4899552f7358SJed Brown   PetscFunctionBegin;
49009566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector_Section_Private(dm, vec));
49019566063dSJacob Faibussowitsch   /* PetscCall(VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM)); */
49029566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEW, (void (*)(void))VecView_Plex));
49039566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEWNATIVE, (void (*)(void))VecView_Plex_Native));
49049566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOAD, (void (*)(void))VecLoad_Plex));
49059566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOADNATIVE, (void (*)(void))VecLoad_Plex_Native));
49063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4907552f7358SJed Brown }
4908552f7358SJed Brown 
4909d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMCreateLocalVector_Plex(DM dm, Vec *vec)
4910d71ae5a4SJacob Faibussowitsch {
4911552f7358SJed Brown   PetscFunctionBegin;
49129566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector_Section_Private(dm, vec));
49139566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEW, (void (*)(void))VecView_Plex_Local));
49149566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOAD, (void (*)(void))VecLoad_Plex_Local));
49153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4916552f7358SJed Brown }
4917552f7358SJed Brown 
4918d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGetDimPoints_Plex(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
4919d71ae5a4SJacob Faibussowitsch {
4920793f3fe5SMatthew G. Knepley   PetscInt depth, d;
4921793f3fe5SMatthew G. Knepley 
4922793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
49239566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
4924793f3fe5SMatthew G. Knepley   if (depth == 1) {
49259566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(dm, &d));
49269566063dSJacob Faibussowitsch     if (dim == 0) PetscCall(DMPlexGetDepthStratum(dm, dim, pStart, pEnd));
49279566063dSJacob Faibussowitsch     else if (dim == d) PetscCall(DMPlexGetDepthStratum(dm, 1, pStart, pEnd));
49289371c9d4SSatish Balay     else {
49299371c9d4SSatish Balay       *pStart = 0;
49309371c9d4SSatish Balay       *pEnd   = 0;
49319371c9d4SSatish Balay     }
4932793f3fe5SMatthew G. Knepley   } else {
49339566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, dim, pStart, pEnd));
4934793f3fe5SMatthew G. Knepley   }
49353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4936793f3fe5SMatthew G. Knepley }
4937793f3fe5SMatthew G. Knepley 
4938d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGetNeighbors_Plex(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
4939d71ae5a4SJacob Faibussowitsch {
4940502a2867SDave May   PetscSF            sf;
49410a19bb7dSprj-   PetscInt           niranks, njranks, n;
49420a19bb7dSprj-   const PetscMPIInt *iranks, *jranks;
49430a19bb7dSprj-   DM_Plex           *data = (DM_Plex *)dm->data;
4944502a2867SDave May 
49452f356facSMatthew G. Knepley   PetscFunctionBegin;
49469566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
49470a19bb7dSprj-   if (!data->neighbors) {
49489566063dSJacob Faibussowitsch     PetscCall(PetscSFSetUp(sf));
49499566063dSJacob Faibussowitsch     PetscCall(PetscSFGetRootRanks(sf, &njranks, &jranks, NULL, NULL, NULL));
49509566063dSJacob Faibussowitsch     PetscCall(PetscSFGetLeafRanks(sf, &niranks, &iranks, NULL, NULL));
49519566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(njranks + niranks + 1, &data->neighbors));
49529566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(data->neighbors + 1, jranks, njranks));
49539566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(data->neighbors + njranks + 1, iranks, niranks));
49540a19bb7dSprj-     n = njranks + niranks;
49559566063dSJacob Faibussowitsch     PetscCall(PetscSortRemoveDupsMPIInt(&n, data->neighbors + 1));
49560a19bb7dSprj-     /* The following cast should never fail: can't have more neighbors than PETSC_MPI_INT_MAX */
49579566063dSJacob Faibussowitsch     PetscCall(PetscMPIIntCast(n, data->neighbors));
49580a19bb7dSprj-   }
49590a19bb7dSprj-   if (nranks) *nranks = data->neighbors[0];
49600a19bb7dSprj-   if (ranks) {
49610a19bb7dSprj-     if (data->neighbors[0]) *ranks = data->neighbors + 1;
49620a19bb7dSprj-     else *ranks = NULL;
49630a19bb7dSprj-   }
49643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4965502a2867SDave May }
4966502a2867SDave May 
49671eb70e55SToby Isaac PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM, DM, Mat, Vec, Vec);
49681eb70e55SToby Isaac 
4969d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMInitialize_Plex(DM dm)
4970d71ae5a4SJacob Faibussowitsch {
4971552f7358SJed Brown   PetscFunctionBegin;
4972552f7358SJed Brown   dm->ops->view                      = DMView_Plex;
49732c40f234SMatthew G. Knepley   dm->ops->load                      = DMLoad_Plex;
4974552f7358SJed Brown   dm->ops->setfromoptions            = DMSetFromOptions_Plex;
497538221697SMatthew G. Knepley   dm->ops->clone                     = DMClone_Plex;
4976552f7358SJed Brown   dm->ops->setup                     = DMSetUp_Plex;
49771bb6d2a8SBarry Smith   dm->ops->createlocalsection        = DMCreateLocalSection_Plex;
4978adc21957SMatthew G. Knepley   dm->ops->createsectionpermutation  = DMCreateSectionPermutation_Plex;
497966ad2231SToby Isaac   dm->ops->createdefaultconstraints  = DMCreateDefaultConstraints_Plex;
4980552f7358SJed Brown   dm->ops->createglobalvector        = DMCreateGlobalVector_Plex;
4981552f7358SJed Brown   dm->ops->createlocalvector         = DMCreateLocalVector_Plex;
4982184d77edSJed Brown   dm->ops->getlocaltoglobalmapping   = NULL;
49830298fd71SBarry Smith   dm->ops->createfieldis             = NULL;
4984552f7358SJed Brown   dm->ops->createcoordinatedm        = DMCreateCoordinateDM_Plex;
4985f19dbd58SToby Isaac   dm->ops->createcoordinatefield     = DMCreateCoordinateField_Plex;
49860a6ba040SMatthew G. Knepley   dm->ops->getcoloring               = NULL;
4987552f7358SJed Brown   dm->ops->creatematrix              = DMCreateMatrix_Plex;
4988bceba477SMatthew G. Knepley   dm->ops->createinterpolation       = DMCreateInterpolation_Plex;
4989bd041c0cSMatthew G. Knepley   dm->ops->createmassmatrix          = DMCreateMassMatrix_Plex;
4990b4937a87SMatthew G. Knepley   dm->ops->createmassmatrixlumped    = DMCreateMassMatrixLumped_Plex;
49915a84ad33SLisandro Dalcin   dm->ops->createinjection           = DMCreateInjection_Plex;
4992552f7358SJed Brown   dm->ops->refine                    = DMRefine_Plex;
49930a6ba040SMatthew G. Knepley   dm->ops->coarsen                   = DMCoarsen_Plex;
49940a6ba040SMatthew G. Knepley   dm->ops->refinehierarchy           = DMRefineHierarchy_Plex;
4995b653a561SMatthew G. Knepley   dm->ops->coarsenhierarchy          = DMCoarsenHierarchy_Plex;
4996d410b0cfSMatthew G. Knepley   dm->ops->extrude                   = DMExtrude_Plex;
49970298fd71SBarry Smith   dm->ops->globaltolocalbegin        = NULL;
49980298fd71SBarry Smith   dm->ops->globaltolocalend          = NULL;
49990298fd71SBarry Smith   dm->ops->localtoglobalbegin        = NULL;
50000298fd71SBarry Smith   dm->ops->localtoglobalend          = NULL;
5001552f7358SJed Brown   dm->ops->destroy                   = DMDestroy_Plex;
5002552f7358SJed Brown   dm->ops->createsubdm               = DMCreateSubDM_Plex;
50032adcc780SMatthew G. Knepley   dm->ops->createsuperdm             = DMCreateSuperDM_Plex;
5004793f3fe5SMatthew G. Knepley   dm->ops->getdimpoints              = DMGetDimPoints_Plex;
5005552f7358SJed Brown   dm->ops->locatepoints              = DMLocatePoints_Plex;
50060709b2feSToby Isaac   dm->ops->projectfunctionlocal      = DMProjectFunctionLocal_Plex;
50070709b2feSToby Isaac   dm->ops->projectfunctionlabellocal = DMProjectFunctionLabelLocal_Plex;
5008bfc4295aSToby Isaac   dm->ops->projectfieldlocal         = DMProjectFieldLocal_Plex;
50098c6c5593SMatthew G. Knepley   dm->ops->projectfieldlabellocal    = DMProjectFieldLabelLocal_Plex;
5010ece3a9fcSMatthew G. Knepley   dm->ops->projectbdfieldlabellocal  = DMProjectBdFieldLabelLocal_Plex;
50110709b2feSToby Isaac   dm->ops->computel2diff             = DMComputeL2Diff_Plex;
5012b698f381SToby Isaac   dm->ops->computel2gradientdiff     = DMComputeL2GradientDiff_Plex;
50132a16baeaSToby Isaac   dm->ops->computel2fielddiff        = DMComputeL2FieldDiff_Plex;
501428d58a37SPierre Jolivet   dm->ops->getneighbors              = DMGetNeighbors_Plex;
50156c6a6b79SMatthew G. Knepley   dm->ops->getlocalboundingbox       = DMGetLocalBoundingBox_Coordinates;
5016907a3e9cSStefano Zampini   dm->ops->createdomaindecomposition = DMCreateDomainDecomposition_Plex;
5017907a3e9cSStefano Zampini   dm->ops->createddscatters          = DMCreateDomainDecompositionScatters_Plex;
50189566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertBoundaryValues_C", DMPlexInsertBoundaryValues_Plex));
50196c51210dSStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertTimeDerivativeBoundaryValues_C", DMPlexInsertTimeDerivativeBoundaryValues_Plex));
50209566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMSetUpGLVisViewer_C", DMSetUpGLVisViewer_Plex));
50219566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMCreateNeumannOverlap_C", DMCreateNeumannOverlap_Plex));
50229566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeGetDefault_C", DMPlexDistributeGetDefault_Plex));
50239566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeSetDefault_C", DMPlexDistributeSetDefault_Plex));
50246bc1bd01Sksagiyam   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderGetDefault_C", DMPlexReorderGetDefault_Plex));
50256bc1bd01Sksagiyam   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderSetDefault_C", DMPlexReorderSetDefault_Plex));
5026adc21957SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionGetDefault_C", DMReorderSectionGetDefault_Plex));
5027adc21957SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionSetDefault_C", DMReorderSectionSetDefault_Plex));
5028adc21957SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionGetType_C", DMReorderSectionGetType_Plex));
5029adc21957SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionSetType_C", DMReorderSectionSetType_Plex));
50309566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMInterpolateSolution_C", DMInterpolateSolution_Plex));
5031c506a872SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetOverlap_C", DMPlexGetOverlap_Plex));
5032c506a872SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexSetOverlap_C", DMPlexSetOverlap_Plex));
5033d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetUseCeed_C", DMPlexGetUseCeed_Plex));
5034d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexSetUseCeed_C", DMPlexSetUseCeed_Plex));
50353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5036552f7358SJed Brown }
5037552f7358SJed Brown 
5038d71ae5a4SJacob Faibussowitsch PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm)
5039d71ae5a4SJacob Faibussowitsch {
504063a16f15SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *)dm->data;
50411fca310dSJames Wright   const PetscSF *face_sfs;
50421fca310dSJames Wright   PetscInt       num_face_sfs;
504363a16f15SMatthew G. Knepley 
504463a16f15SMatthew G. Knepley   PetscFunctionBegin;
504563a16f15SMatthew G. Knepley   mesh->refct++;
504663a16f15SMatthew G. Knepley   (*newdm)->data = mesh;
50471fca310dSJames Wright   PetscCall(DMPlexGetIsoperiodicFaceSF(dm, &num_face_sfs, &face_sfs));
50481fca310dSJames Wright   PetscCall(DMPlexSetIsoperiodicFaceSF(*newdm, num_face_sfs, (PetscSF *)face_sfs));
50499566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)*newdm, DMPLEX));
50509566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(*newdm));
50513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
505263a16f15SMatthew G. Knepley }
505363a16f15SMatthew G. Knepley 
50548818961aSMatthew G Knepley /*MC
5055a1cb98faSBarry Smith   DMPLEX = "plex" - A `DM` object that encapsulates an unstructured mesh, or CW Complex, which can be expressed using a Hasse Diagram.
505620f4b53cSBarry Smith                     In the local representation, `Vec`s contain all unknowns in the interior and shared boundary. This is
50578818961aSMatthew G Knepley                     specified by a PetscSection object. Ownership in the global representation is determined by
5058a1cb98faSBarry Smith                     ownership of the underlying `DMPLEX` points. This is specified by another `PetscSection` object.
50598818961aSMatthew G Knepley 
5060e5893cccSMatthew G. Knepley   Options Database Keys:
5061250712c9SMatthew G. Knepley + -dm_refine_pre                     - Refine mesh before distribution
5062250712c9SMatthew G. Knepley + -dm_refine_uniform_pre             - Choose uniform or generator-based refinement
5063250712c9SMatthew G. Knepley + -dm_refine_volume_limit_pre        - Cell volume limit after pre-refinement using generator
5064250712c9SMatthew G. Knepley . -dm_distribute                     - Distribute mesh across processes
5065250712c9SMatthew G. Knepley . -dm_distribute_overlap             - Number of cells to overlap for distribution
5066250712c9SMatthew G. Knepley . -dm_refine                         - Refine mesh after distribution
5067c3db174cSMatthew G. Knepley . -dm_localize <bool>                - Whether to localize coordinates for periodic meshes
5068c3db174cSMatthew G. Knepley . -dm_sparse_localize <bool>         - Whether to only localize cells on the periodic boundary
5069250712c9SMatthew G. Knepley . -dm_plex_hash_location             - Use grid hashing for point location
5070ddce0771SMatthew G. Knepley . -dm_plex_hash_box_faces <n,m,p>    - The number of divisions in each direction of the grid hash
5071f12cf164SMatthew G. Knepley . -dm_plex_partition_balance         - Attempt to evenly divide points on partition boundary between processes
5072f12cf164SMatthew G. Knepley . -dm_plex_remesh_bd                 - Allow changes to the boundary on remeshing
5073d5b43468SJose E. Roman . -dm_plex_max_projection_height     - Maximum mesh point height used to project locally
5074f12cf164SMatthew G. Knepley . -dm_plex_regular_refinement        - Use special nested projection algorithm for regular refinement
5075d02c7345SMatthew G. Knepley . -dm_plex_reorder_section           - Use specialized blocking if available
5076aaa8cc7dSPierre Jolivet . -dm_plex_check_all                 - Perform all checks below
5077f12cf164SMatthew G. Knepley . -dm_plex_check_symmetry            - Check that the adjacency information in the mesh is symmetric
5078f12cf164SMatthew G. Knepley . -dm_plex_check_skeleton <celltype> - Check that each cell has the correct number of vertices
5079f12cf164SMatthew 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
5080f12cf164SMatthew G. Knepley . -dm_plex_check_geometry            - Check that cells have positive volume
5081f12cf164SMatthew G. Knepley . -dm_view :mesh.tex:ascii_latex     - View the mesh in LaTeX/TikZ
5082e5893cccSMatthew G. Knepley . -dm_plex_view_scale <num>          - Scale the TikZ
50835962854dSMatthew G. Knepley . -dm_plex_print_fem <num>           - View FEM assembly information, such as element vectors and matrices
50845962854dSMatthew G. Knepley - -dm_plex_print_fvm <num>           - View FVM assembly information, such as flux updates
5085e5893cccSMatthew G. Knepley 
50868818961aSMatthew G Knepley   Level: intermediate
50878818961aSMatthew G Knepley 
50881cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMType`, `DMPlexCreate()`, `DMCreate()`, `DMSetType()`, `PetscSection`
50898818961aSMatthew G Knepley M*/
50908818961aSMatthew G Knepley 
5091d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode DMCreate_Plex(DM dm)
5092d71ae5a4SJacob Faibussowitsch {
5093552f7358SJed Brown   DM_Plex *mesh;
5094412e9a14SMatthew G. Knepley   PetscInt unit;
5095552f7358SJed Brown 
5096552f7358SJed Brown   PetscFunctionBegin;
5097f39ec787SMatthew G. Knepley   PetscCall(PetscCitationsRegister(PlexCitation, &Plexcite));
5098552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
50994dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&mesh));
5100adc21957SMatthew G. Knepley   dm->reorderSection = DM_REORDER_DEFAULT_NOTSET;
5101552f7358SJed Brown   dm->data           = mesh;
5102552f7358SJed Brown 
5103552f7358SJed Brown   mesh->refct = 1;
51049566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->coneSection));
51059566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->supportSection));
5106552f7358SJed Brown   mesh->refinementUniform      = PETSC_TRUE;
5107552f7358SJed Brown   mesh->refinementLimit        = -1.0;
5108e600fa54SMatthew G. Knepley   mesh->distDefault            = PETSC_TRUE;
5109adc21957SMatthew G. Knepley   mesh->reorderDefault         = DM_REORDER_DEFAULT_NOTSET;
51101d1f2f2aSksagiyam   mesh->distributionName       = NULL;
51117d0f5628SVaclav Hapla   mesh->interpolated           = DMPLEX_INTERPOLATED_INVALID;
51127d0f5628SVaclav Hapla   mesh->interpolatedCollective = DMPLEX_INTERPOLATED_INVALID;
5113552f7358SJed Brown 
51149566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerCreate(PetscObjectComm((PetscObject)dm), &mesh->partitioner));
51152e62ab5aSMatthew G. Knepley   mesh->remeshBd = PETSC_FALSE;
5116d9deefdfSMatthew G. Knepley 
51178865f1eaSKarl Rupp   for (unit = 0; unit < NUM_PETSC_UNITS; ++unit) mesh->scale[unit] = 1.0;
5118552f7358SJed Brown 
5119df0420ecSMatthew G. Knepley   mesh->depthState    = -1;
5120ba2698f1SMatthew G. Knepley   mesh->celltypeState = -1;
51216113b454SMatthew G. Knepley   mesh->printTol      = 1.0e-10;
5122552f7358SJed Brown 
51239566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(dm));
51243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5125552f7358SJed Brown }
5126552f7358SJed Brown 
5127552f7358SJed Brown /*@
5128a1cb98faSBarry Smith   DMPlexCreate - Creates a `DMPLEX` object, which encapsulates an unstructured mesh, or CW complex, which can be expressed using a Hasse Diagram.
5129552f7358SJed Brown 
5130d083f849SBarry Smith   Collective
5131552f7358SJed Brown 
5132552f7358SJed Brown   Input Parameter:
5133a1cb98faSBarry Smith . comm - The communicator for the `DMPLEX` object
5134552f7358SJed Brown 
5135552f7358SJed Brown   Output Parameter:
5136a1cb98faSBarry Smith . mesh - The `DMPLEX` object
5137552f7358SJed Brown 
5138552f7358SJed Brown   Level: beginner
5139552f7358SJed Brown 
514042747ad1SJacob Faibussowitsch .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMType`, `DMCreate()`, `DMSetType()`
5141552f7358SJed Brown @*/
5142d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreate(MPI_Comm comm, DM *mesh)
5143d71ae5a4SJacob Faibussowitsch {
5144552f7358SJed Brown   PetscFunctionBegin;
51454f572ea9SToby Isaac   PetscAssertPointer(mesh, 2);
51469566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, mesh));
51479566063dSJacob Faibussowitsch   PetscCall(DMSetType(*mesh, DMPLEX));
51483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5149552f7358SJed Brown }
5150552f7358SJed Brown 
5151b09969d6SVaclav Hapla /*@C
5152a1cb98faSBarry Smith   DMPlexBuildFromCellListParallel - Build distributed `DMPLEX` topology from a list of vertices for each cell (common mesh generator output)
5153a1cb98faSBarry Smith 
515420f4b53cSBarry Smith   Collective; No Fortran Support
5155b09969d6SVaclav Hapla 
5156b09969d6SVaclav Hapla   Input Parameters:
5157a1cb98faSBarry Smith + dm          - The `DM`
5158b09969d6SVaclav Hapla . numCells    - The number of cells owned by this process
5159a1cb98faSBarry Smith . numVertices - The number of vertices to be owned by this process, or `PETSC_DECIDE`
5160a1cb98faSBarry Smith . NVertices   - The global number of vertices, or `PETSC_DETERMINE`
5161b09969d6SVaclav Hapla . numCorners  - The number of vertices for each cell
51625e488331SVaclav Hapla - cells       - An array of numCells*numCorners numbers, the global vertex numbers for each cell
5163b09969d6SVaclav Hapla 
5164be8c289dSNicolas Barral   Output Parameters:
5165a1cb98faSBarry Smith + vertexSF         - (Optional) `PetscSF` describing complete vertex ownership
5166be8c289dSNicolas Barral - verticesAdjSaved - (Optional) vertex adjacency array
5167b09969d6SVaclav Hapla 
5168b09969d6SVaclav Hapla   Level: advanced
5169b09969d6SVaclav Hapla 
5170a1cb98faSBarry Smith   Notes:
5171a1cb98faSBarry Smith   Two triangles sharing a face
5172a1cb98faSBarry Smith .vb
5173a1cb98faSBarry Smith 
5174a1cb98faSBarry Smith         2
5175a1cb98faSBarry Smith       / | \
5176a1cb98faSBarry Smith      /  |  \
5177a1cb98faSBarry Smith     /   |   \
5178a1cb98faSBarry Smith    0  0 | 1  3
5179a1cb98faSBarry Smith     \   |   /
5180a1cb98faSBarry Smith      \  |  /
5181a1cb98faSBarry Smith       \ | /
5182a1cb98faSBarry Smith         1
5183a1cb98faSBarry Smith .ve
5184a1cb98faSBarry Smith   would have input
5185a1cb98faSBarry Smith .vb
5186a1cb98faSBarry Smith   numCells = 2, numVertices = 4
5187a1cb98faSBarry Smith   cells = [0 1 2  1 3 2]
5188a1cb98faSBarry Smith .ve
5189a1cb98faSBarry Smith   which would result in the `DMPLEX`
5190a1cb98faSBarry Smith .vb
5191a1cb98faSBarry Smith 
5192a1cb98faSBarry Smith         4
5193a1cb98faSBarry Smith       / | \
5194a1cb98faSBarry Smith      /  |  \
5195a1cb98faSBarry Smith     /   |   \
5196a1cb98faSBarry Smith    2  0 | 1  5
5197a1cb98faSBarry Smith     \   |   /
5198a1cb98faSBarry Smith      \  |  /
5199a1cb98faSBarry Smith       \ | /
5200a1cb98faSBarry Smith         3
5201a1cb98faSBarry Smith .ve
5202a1cb98faSBarry Smith 
5203a1cb98faSBarry Smith   Vertices are implicitly numbered consecutively 0,...,NVertices.
5204a1cb98faSBarry Smith   Each rank owns a chunk of numVertices consecutive vertices.
5205a1cb98faSBarry Smith   If numVertices is `PETSC_DECIDE`, PETSc will distribute them as evenly as possible using PetscLayout.
5206a1cb98faSBarry Smith   If NVertices is `PETSC_DETERMINE` and numVertices is PETSC_DECIDE, NVertices is computed by PETSc as the maximum vertex index in cells + 1.
5207a1cb98faSBarry Smith   If only NVertices is `PETSC_DETERMINE`, it is computed as the sum of numVertices over all ranks.
5208a1cb98faSBarry Smith 
5209a1cb98faSBarry Smith   The cell distribution is arbitrary non-overlapping, independent of the vertex distribution.
5210a1cb98faSBarry Smith 
52111cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildFromCellList()`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildCoordinatesFromCellListParallel()`,
5212a1cb98faSBarry Smith           `PetscSF`
5213b09969d6SVaclav Hapla @*/
5214d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildFromCellListParallel(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt NVertices, PetscInt numCorners, const PetscInt cells[], PetscSF *vertexSF, PetscInt **verticesAdjSaved)
5215d71ae5a4SJacob Faibussowitsch {
52162464107aSksagiyam   PetscSF     sfPoint;
52172464107aSksagiyam   PetscLayout layout;
521882fb893eSVaclav Hapla   PetscInt    numVerticesAdj, *verticesAdj, *cones, c, p;
5219a47d0d45SMatthew G. Knepley 
5220a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
522125b6865aSVaclav Hapla   PetscValidLogicalCollectiveInt(dm, NVertices, 4);
52229566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
522325b6865aSVaclav Hapla   /* Get/check global number of vertices */
522425b6865aSVaclav Hapla   {
522525b6865aSVaclav Hapla     PetscInt       NVerticesInCells, i;
522625b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
522725b6865aSVaclav Hapla 
522825b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
522925b6865aSVaclav Hapla     NVerticesInCells = PETSC_MIN_INT;
52309371c9d4SSatish Balay     for (i = 0; i < len; i++)
52319371c9d4SSatish Balay       if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
523225b6865aSVaclav Hapla     ++NVerticesInCells;
5233712fec58SPierre Jolivet     PetscCall(MPIU_Allreduce(MPI_IN_PLACE, &NVerticesInCells, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
523425b6865aSVaclav Hapla 
523525b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE && NVertices == PETSC_DECIDE) NVertices = NVerticesInCells;
52369371c9d4SSatish Balay     else
52379371c9d4SSatish 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);
523825b6865aSVaclav Hapla   }
52399079aca8SVaclav Hapla   /* Count locally unique vertices */
52409079aca8SVaclav Hapla   {
52419079aca8SVaclav Hapla     PetscHSetI vhash;
52429079aca8SVaclav Hapla     PetscInt   off = 0;
52439079aca8SVaclav Hapla 
52449566063dSJacob Faibussowitsch     PetscCall(PetscHSetICreate(&vhash));
5245a47d0d45SMatthew G. Knepley     for (c = 0; c < numCells; ++c) {
524648a46eb9SPierre Jolivet       for (p = 0; p < numCorners; ++p) PetscCall(PetscHSetIAdd(vhash, cells[c * numCorners + p]));
5247a47d0d45SMatthew G. Knepley     }
52489566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetSize(vhash, &numVerticesAdj));
52499566063dSJacob Faibussowitsch     if (!verticesAdjSaved) PetscCall(PetscMalloc1(numVerticesAdj, &verticesAdj));
5250ad540459SPierre Jolivet     else verticesAdj = *verticesAdjSaved;
52519566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetElems(vhash, &off, verticesAdj));
52529566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&vhash));
525363a3b9bcSJacob Faibussowitsch     PetscCheck(off == numVerticesAdj, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid number of local vertices %" PetscInt_FMT " should be %" PetscInt_FMT, off, numVerticesAdj);
5254a47d0d45SMatthew G. Knepley   }
52559566063dSJacob Faibussowitsch   PetscCall(PetscSortInt(numVerticesAdj, verticesAdj));
5256a47d0d45SMatthew G. Knepley   /* Create cones */
52579566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, 0, numCells + numVerticesAdj));
52589566063dSJacob Faibussowitsch   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, numCorners));
52599566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm));
52609566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCones(dm, &cones));
5261a47d0d45SMatthew G. Knepley   for (c = 0; c < numCells; ++c) {
5262a47d0d45SMatthew G. Knepley     for (p = 0; p < numCorners; ++p) {
5263a47d0d45SMatthew G. Knepley       const PetscInt gv = cells[c * numCorners + p];
5264a47d0d45SMatthew G. Knepley       PetscInt       lv;
5265a47d0d45SMatthew G. Knepley 
52669079aca8SVaclav Hapla       /* Positions within verticesAdj form 0-based local vertex numbering;
52679079aca8SVaclav Hapla          we need to shift it by numCells to get correct DAG points (cells go first) */
52689566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv));
526963a3b9bcSJacob Faibussowitsch       PetscCheck(lv >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not find global vertex %" PetscInt_FMT " in local connectivity", gv);
5270961cfab0SVaclav Hapla       cones[c * numCorners + p] = lv + numCells;
5271a47d0d45SMatthew G. Knepley     }
5272a47d0d45SMatthew G. Knepley   }
52732464107aSksagiyam   /* Build point sf */
52749566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(PetscObjectComm((PetscObject)dm), &layout));
52759566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetSize(layout, NVertices));
52769566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, numVertices));
52779566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
52789566063dSJacob Faibussowitsch   PetscCall(PetscSFCreateByMatchingIndices(layout, numVerticesAdj, verticesAdj, NULL, numCells, numVerticesAdj, verticesAdj, NULL, numCells, vertexSF, &sfPoint));
52799566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
52809566063dSJacob Faibussowitsch   if (!verticesAdjSaved) PetscCall(PetscFree(verticesAdj));
52819566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)sfPoint, "point SF"));
52822464107aSksagiyam   if (dm->sf) {
52832464107aSksagiyam     const char *prefix;
52842464107aSksagiyam 
52859566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm->sf, &prefix));
52869566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)sfPoint, prefix));
52872464107aSksagiyam   }
52889566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dm, sfPoint));
52899566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&sfPoint));
5290f4f49eeaSPierre Jolivet   if (vertexSF) PetscCall(PetscObjectSetName((PetscObject)*vertexSF, "Vertex Ownership SF"));
5291a47d0d45SMatthew G. Knepley   /* Fill in the rest of the topology structure */
52929566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
52939566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
52949566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
52953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5296a47d0d45SMatthew G. Knepley }
5297a47d0d45SMatthew G. Knepley 
5298cc4c1da9SBarry Smith /*@
5299a1cb98faSBarry Smith   DMPlexBuildCoordinatesFromCellListParallel - Build `DM` coordinates from a list of coordinates for each owned vertex (common mesh generator output)
5300a1cb98faSBarry Smith 
530120f4b53cSBarry Smith   Collective; No Fortran Support
5302b09969d6SVaclav Hapla 
5303b09969d6SVaclav Hapla   Input Parameters:
5304a1cb98faSBarry Smith + dm           - The `DM`
5305b09969d6SVaclav Hapla . spaceDim     - The spatial dimension used for coordinates
5306a1cb98faSBarry Smith . sfVert       - `PetscSF` describing complete vertex ownership
5307b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
5308b09969d6SVaclav Hapla 
5309b09969d6SVaclav Hapla   Level: advanced
5310b09969d6SVaclav Hapla 
53111cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildFromCellListParallel()`
5312b09969d6SVaclav Hapla @*/
5313d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildCoordinatesFromCellListParallel(DM dm, PetscInt spaceDim, PetscSF sfVert, const PetscReal vertexCoords[])
5314d71ae5a4SJacob Faibussowitsch {
5315a47d0d45SMatthew G. Knepley   PetscSection coordSection;
5316a47d0d45SMatthew G. Knepley   Vec          coordinates;
5317a47d0d45SMatthew G. Knepley   PetscScalar *coords;
53181edcf0b2SVaclav Hapla   PetscInt     numVertices, numVerticesAdj, coordSize, v, vStart, vEnd;
5319a47d0d45SMatthew G. Knepley 
5320a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
53219566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
53229566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
53231dca8a05SBarry Smith   PetscCheck(vStart >= 0 && vEnd >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
53249566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, spaceDim));
53259566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfVert, &numVertices, &numVerticesAdj, NULL, NULL));
53261dca8a05SBarry 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);
53279566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
53289566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
53299566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, spaceDim));
53309566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, vStart, vEnd));
53311edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
53329566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, spaceDim));
53339566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, spaceDim));
5334a47d0d45SMatthew G. Knepley   }
53359566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
53369566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
53379566063dSJacob Faibussowitsch   PetscCall(VecCreate(PetscObjectComm((PetscObject)dm), &coordinates));
53389566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, spaceDim));
53399566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
53409566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
53419566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
53429566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
5343a47d0d45SMatthew G. Knepley   {
5344a47d0d45SMatthew G. Knepley     MPI_Datatype coordtype;
5345a47d0d45SMatthew G. Knepley 
5346a47d0d45SMatthew G. Knepley     /* Need a temp buffer for coords if we have complex/single */
53479566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_contiguous(spaceDim, MPIU_SCALAR, &coordtype));
53489566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_commit(&coordtype));
534921016a8bSBarry Smith #if defined(PETSC_USE_COMPLEX)
535021016a8bSBarry Smith     {
535121016a8bSBarry Smith       PetscScalar *svertexCoords;
535221016a8bSBarry Smith       PetscInt     i;
53539566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numVertices * spaceDim, &svertexCoords));
53543612f820SVaclav Hapla       for (i = 0; i < numVertices * spaceDim; i++) svertexCoords[i] = vertexCoords[i];
53559566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(sfVert, coordtype, svertexCoords, coords, MPI_REPLACE));
53569566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(sfVert, coordtype, svertexCoords, coords, MPI_REPLACE));
53579566063dSJacob Faibussowitsch       PetscCall(PetscFree(svertexCoords));
535821016a8bSBarry Smith     }
535921016a8bSBarry Smith #else
53609566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sfVert, coordtype, vertexCoords, coords, MPI_REPLACE));
53619566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sfVert, coordtype, vertexCoords, coords, MPI_REPLACE));
536221016a8bSBarry Smith #endif
53639566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_free(&coordtype));
5364a47d0d45SMatthew G. Knepley   }
53659566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
53669566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
53679566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
53689566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
53693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5370a47d0d45SMatthew G. Knepley }
5371a47d0d45SMatthew G. Knepley 
5372c3edce3dSSatish Balay /*@
5373a1cb98faSBarry Smith   DMPlexCreateFromCellListParallelPetsc - Create distributed `DMPLEX` from a list of vertices for each cell (common mesh generator output)
5374a1cb98faSBarry Smith 
5375a1cb98faSBarry Smith   Collective
5376a47d0d45SMatthew G. Knepley 
5377a47d0d45SMatthew G. Knepley   Input Parameters:
5378a47d0d45SMatthew G. Knepley + comm         - The communicator
5379a47d0d45SMatthew G. Knepley . dim          - The topological dimension of the mesh
5380a47d0d45SMatthew G. Knepley . numCells     - The number of cells owned by this process
5381a1cb98faSBarry Smith . numVertices  - The number of vertices owned by this process, or `PETSC_DECIDE`
5382a1cb98faSBarry Smith . NVertices    - The global number of vertices, or `PETSC_DECIDE`
5383a47d0d45SMatthew G. Knepley . numCorners   - The number of vertices for each cell
5384a47d0d45SMatthew G. Knepley . interpolate  - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
5385a47d0d45SMatthew G. Knepley . cells        - An array of numCells*numCorners numbers, the global vertex numbers for each cell
5386a47d0d45SMatthew G. Knepley . spaceDim     - The spatial dimension used for coordinates
5387a47d0d45SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
5388a47d0d45SMatthew G. Knepley 
5389d8d19677SJose E. Roman   Output Parameters:
5390a1cb98faSBarry Smith + dm          - The `DM`
5391a1cb98faSBarry Smith . vertexSF    - (Optional) `PetscSF` describing complete vertex ownership
539260225df5SJacob Faibussowitsch - verticesAdj - (Optional) vertex adjacency array
5393a47d0d45SMatthew G. Knepley 
5394b09969d6SVaclav Hapla   Level: intermediate
5395a47d0d45SMatthew G. Knepley 
5396a1cb98faSBarry Smith   Notes:
5397a1cb98faSBarry Smith   This function is just a convenient sequence of `DMCreate()`, `DMSetType()`, `DMSetDimension()`,
5398a1cb98faSBarry Smith   `DMPlexBuildFromCellListParallel()`, `DMPlexInterpolate()`, `DMPlexBuildCoordinatesFromCellListParallel()`
5399a1cb98faSBarry Smith 
5400a1cb98faSBarry Smith   See `DMPlexBuildFromCellListParallel()` for an example and details about the topology-related parameters.
5401a1cb98faSBarry Smith 
5402a1cb98faSBarry Smith   See `DMPlexBuildCoordinatesFromCellListParallel()` for details about the geometry-related parameters.
5403a1cb98faSBarry Smith 
54041cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()`
5405a47d0d45SMatthew G. Knepley @*/
5406d71ae5a4SJacob 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)
5407d71ae5a4SJacob Faibussowitsch {
5408a47d0d45SMatthew G. Knepley   PetscSF sfVert;
5409a47d0d45SMatthew G. Knepley 
5410a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
54119566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
54129566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
5413a47d0d45SMatthew G. Knepley   PetscValidLogicalCollectiveInt(*dm, dim, 2);
5414064a246eSJacob Faibussowitsch   PetscValidLogicalCollectiveInt(*dm, spaceDim, 9);
54159566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
54169566063dSJacob Faibussowitsch   PetscCall(DMPlexBuildFromCellListParallel(*dm, numCells, numVertices, NVertices, numCorners, cells, &sfVert, verticesAdj));
5417a47d0d45SMatthew G. Knepley   if (interpolate) {
54185fd9971aSMatthew G. Knepley     DM idm;
5419a47d0d45SMatthew G. Knepley 
54209566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
54219566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
5422a47d0d45SMatthew G. Knepley     *dm = idm;
5423a47d0d45SMatthew G. Knepley   }
54249566063dSJacob Faibussowitsch   PetscCall(DMPlexBuildCoordinatesFromCellListParallel(*dm, spaceDim, sfVert, vertexCoords));
542518d54ad4SMichael Lange   if (vertexSF) *vertexSF = sfVert;
54269566063dSJacob Faibussowitsch   else PetscCall(PetscSFDestroy(&sfVert));
54273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5428a47d0d45SMatthew G. Knepley }
5429a47d0d45SMatthew G. Knepley 
5430cc4c1da9SBarry Smith /*@
5431a1cb98faSBarry Smith   DMPlexBuildFromCellList - Build `DMPLEX` topology from a list of vertices for each cell (common mesh generator output)
5432a1cb98faSBarry Smith 
543320f4b53cSBarry Smith   Collective; No Fortran Support
54349298eaa6SMatthew G Knepley 
54359298eaa6SMatthew G Knepley   Input Parameters:
5436a1cb98faSBarry Smith + dm          - The `DM`
5437b09969d6SVaclav Hapla . numCells    - The number of cells owned by this process
5438a1cb98faSBarry Smith . numVertices - The number of vertices owned by this process, or `PETSC_DETERMINE`
54399298eaa6SMatthew G Knepley . numCorners  - The number of vertices for each cell
5440a3b724e8SBarry Smith - cells       - An array of `numCells` x `numCorners` numbers, the global vertex numbers for each cell
54419298eaa6SMatthew G Knepley 
5442b09969d6SVaclav Hapla   Level: advanced
54439298eaa6SMatthew G Knepley 
5444b09969d6SVaclav Hapla   Notes:
5445b09969d6SVaclav Hapla   Two triangles sharing a face
5446a1cb98faSBarry Smith .vb
54479298eaa6SMatthew G Knepley 
5448a1cb98faSBarry Smith         2
5449a1cb98faSBarry Smith       / | \
5450a1cb98faSBarry Smith      /  |  \
5451a1cb98faSBarry Smith     /   |   \
5452a1cb98faSBarry Smith    0  0 | 1  3
5453a1cb98faSBarry Smith     \   |   /
5454a1cb98faSBarry Smith      \  |  /
5455a1cb98faSBarry Smith       \ | /
5456a1cb98faSBarry Smith         1
5457a1cb98faSBarry Smith .ve
5458a1cb98faSBarry Smith   would have input
5459a1cb98faSBarry Smith .vb
5460a1cb98faSBarry Smith   numCells = 2, numVertices = 4
5461a1cb98faSBarry Smith   cells = [0 1 2  1 3 2]
5462a1cb98faSBarry Smith .ve
5463a1cb98faSBarry Smith   which would result in the `DMPLEX`
5464a1cb98faSBarry Smith .vb
5465a1cb98faSBarry Smith 
5466a1cb98faSBarry Smith         4
5467a1cb98faSBarry Smith       / | \
5468a1cb98faSBarry Smith      /  |  \
5469a1cb98faSBarry Smith     /   |   \
5470a1cb98faSBarry Smith    2  0 | 1  5
5471a1cb98faSBarry Smith     \   |   /
5472a1cb98faSBarry Smith      \  |  /
5473a1cb98faSBarry Smith       \ | /
5474a1cb98faSBarry Smith         3
5475a1cb98faSBarry Smith .ve
5476a1cb98faSBarry Smith 
5477a1cb98faSBarry Smith   If numVertices is `PETSC_DETERMINE`, it is computed by PETSc as the maximum vertex index in cells + 1.
547825b6865aSVaclav Hapla 
54791cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromCellListPetsc()`
5480b09969d6SVaclav Hapla @*/
5481d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildFromCellList(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const PetscInt cells[])
5482d71ae5a4SJacob Faibussowitsch {
5483961cfab0SVaclav Hapla   PetscInt *cones, c, p, dim;
5484b09969d6SVaclav Hapla 
5485b09969d6SVaclav Hapla   PetscFunctionBegin;
54869566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
54879566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
548825b6865aSVaclav Hapla   /* Get/check global number of vertices */
548925b6865aSVaclav Hapla   {
549025b6865aSVaclav Hapla     PetscInt       NVerticesInCells, i;
549125b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
549225b6865aSVaclav Hapla 
549325b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
549425b6865aSVaclav Hapla     NVerticesInCells = PETSC_MIN_INT;
54959371c9d4SSatish Balay     for (i = 0; i < len; i++)
54969371c9d4SSatish Balay       if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
549725b6865aSVaclav Hapla     ++NVerticesInCells;
549825b6865aSVaclav Hapla 
549925b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE) numVertices = NVerticesInCells;
55009371c9d4SSatish Balay     else
55019371c9d4SSatish 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);
550225b6865aSVaclav Hapla   }
55039566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
550448a46eb9SPierre Jolivet   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, numCorners));
55059566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm));
55069566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCones(dm, &cones));
5507b09969d6SVaclav Hapla   for (c = 0; c < numCells; ++c) {
5508ad540459SPierre Jolivet     for (p = 0; p < numCorners; ++p) cones[c * numCorners + p] = cells[c * numCorners + p] + numCells;
5509b09969d6SVaclav Hapla   }
55109566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
55119566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
55129566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
55133ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5514b09969d6SVaclav Hapla }
5515b09969d6SVaclav Hapla 
5516cc4c1da9SBarry Smith /*@
5517a1cb98faSBarry Smith   DMPlexBuildCoordinatesFromCellList - Build `DM` coordinates from a list of coordinates for each owned vertex (common mesh generator output)
5518a1cb98faSBarry Smith 
5519cc4c1da9SBarry Smith   Collective
5520b09969d6SVaclav Hapla 
5521b09969d6SVaclav Hapla   Input Parameters:
5522a1cb98faSBarry Smith + dm           - The `DM`
5523b09969d6SVaclav Hapla . spaceDim     - The spatial dimension used for coordinates
5524b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
5525b09969d6SVaclav Hapla 
5526b09969d6SVaclav Hapla   Level: advanced
5527b09969d6SVaclav Hapla 
55281cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellList()`
5529b09969d6SVaclav Hapla @*/
5530d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildCoordinatesFromCellList(DM dm, PetscInt spaceDim, const PetscReal vertexCoords[])
5531d71ae5a4SJacob Faibussowitsch {
5532b09969d6SVaclav Hapla   PetscSection coordSection;
5533b09969d6SVaclav Hapla   Vec          coordinates;
5534b09969d6SVaclav Hapla   DM           cdm;
5535b09969d6SVaclav Hapla   PetscScalar *coords;
55361edcf0b2SVaclav Hapla   PetscInt     v, vStart, vEnd, d;
5537b09969d6SVaclav Hapla 
5538b09969d6SVaclav Hapla   PetscFunctionBegin;
55399566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
55409566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
55411dca8a05SBarry Smith   PetscCheck(vStart >= 0 && vEnd >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
55429566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, spaceDim));
55439566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
55449566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
55459566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, spaceDim));
55469566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, vStart, vEnd));
55471edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
55489566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, spaceDim));
55499566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, spaceDim));
5550b09969d6SVaclav Hapla   }
55519566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
5552b09969d6SVaclav Hapla 
55539566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
55549566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(cdm, &coordinates));
55559566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, spaceDim));
55569566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
55579566063dSJacob Faibussowitsch   PetscCall(VecGetArrayWrite(coordinates, &coords));
55581edcf0b2SVaclav Hapla   for (v = 0; v < vEnd - vStart; ++v) {
5559ad540459SPierre Jolivet     for (d = 0; d < spaceDim; ++d) coords[v * spaceDim + d] = vertexCoords[v * spaceDim + d];
5560b09969d6SVaclav Hapla   }
55619566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayWrite(coordinates, &coords));
55629566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
55639566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
55649566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
55653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5566b09969d6SVaclav Hapla }
5567b09969d6SVaclav Hapla 
5568b09969d6SVaclav Hapla /*@
5569a1cb98faSBarry Smith   DMPlexCreateFromCellListPetsc - Create `DMPLEX` from a list of vertices for each cell (common mesh generator output), but only process 0 takes in the input
55703df08285SMatthew G. Knepley 
5571a1cb98faSBarry Smith   Collective
5572b09969d6SVaclav Hapla 
5573b09969d6SVaclav Hapla   Input Parameters:
5574b09969d6SVaclav Hapla + comm         - The communicator
5575b09969d6SVaclav Hapla . dim          - The topological dimension of the mesh
55763df08285SMatthew G. Knepley . numCells     - The number of cells, only on process 0
5577a1cb98faSBarry Smith . numVertices  - The number of vertices owned by this process, or `PETSC_DECIDE`, only on process 0
55783df08285SMatthew G. Knepley . numCorners   - The number of vertices for each cell, only on process 0
5579b09969d6SVaclav Hapla . interpolate  - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
55803df08285SMatthew G. Knepley . cells        - An array of numCells*numCorners numbers, the vertices for each cell, only on process 0
5581b09969d6SVaclav Hapla . spaceDim     - The spatial dimension used for coordinates
55823df08285SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex, only on process 0
5583b09969d6SVaclav Hapla 
5584b09969d6SVaclav Hapla   Output Parameter:
5585a1cb98faSBarry Smith . dm - The `DM`, which only has points on process 0
558625b6865aSVaclav Hapla 
5587b09969d6SVaclav Hapla   Level: intermediate
5588b09969d6SVaclav Hapla 
5589a1cb98faSBarry Smith   Notes:
5590a1cb98faSBarry Smith   This function is just a convenient sequence of `DMCreate()`, `DMSetType()`, `DMSetDimension()`, `DMPlexBuildFromCellList()`,
5591a1cb98faSBarry Smith   `DMPlexInterpolate()`, `DMPlexBuildCoordinatesFromCellList()`
5592a1cb98faSBarry Smith 
5593a1cb98faSBarry Smith   See `DMPlexBuildFromCellList()` for an example and details about the topology-related parameters.
5594a1cb98faSBarry Smith   See `DMPlexBuildCoordinatesFromCellList()` for details about the geometry-related parameters.
5595a1cb98faSBarry Smith   See `DMPlexCreateFromCellListParallelPetsc()` for parallel input
5596a1cb98faSBarry Smith 
55971cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildFromCellList()`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()`
55989298eaa6SMatthew G Knepley @*/
5599d71ae5a4SJacob 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)
5600d71ae5a4SJacob Faibussowitsch {
56013df08285SMatthew G. Knepley   PetscMPIInt rank;
56029298eaa6SMatthew G Knepley 
56039298eaa6SMatthew G Knepley   PetscFunctionBegin;
560428b400f6SJacob 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.");
56059566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
56069566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
56079566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
56089566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
5609c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildFromCellList(*dm, numCells, numVertices, numCorners, cells));
56109566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildFromCellList(*dm, 0, 0, 0, NULL));
56119298eaa6SMatthew G Knepley   if (interpolate) {
56125fd9971aSMatthew G. Knepley     DM idm;
56139298eaa6SMatthew G Knepley 
56149566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
56159566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
56169298eaa6SMatthew G Knepley     *dm = idm;
56179298eaa6SMatthew G Knepley   }
5618c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, vertexCoords));
56199566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, NULL));
56203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
56219298eaa6SMatthew G Knepley }
56229298eaa6SMatthew G Knepley 
5623939f6067SMatthew G. Knepley /*@
562420f4b53cSBarry Smith   DMPlexCreateFromDAG - This takes as input the adjacency-list representation of the Directed Acyclic Graph (Hasse Diagram) encoding a mesh, and produces a `DM`
5625939f6067SMatthew G. Knepley 
5626939f6067SMatthew G. Knepley   Input Parameters:
562720f4b53cSBarry Smith + dm               - The empty `DM` object, usually from `DMCreate()` and `DMSetDimension()`
5628939f6067SMatthew G. Knepley . depth            - The depth of the DAG
562920f4b53cSBarry Smith . numPoints        - Array of size depth + 1 containing the number of points at each `depth`
5630939f6067SMatthew G. Knepley . coneSize         - The cone size of each point
5631939f6067SMatthew G. Knepley . cones            - The concatenation of the cone points for each point, the cone list must be oriented correctly for each point
5632939f6067SMatthew G. Knepley . coneOrientations - The orientation of each cone point
563320f4b53cSBarry Smith - vertexCoords     - An array of `numPoints`[0]*spacedim numbers representing the coordinates of each vertex, with spacedim the value set via `DMSetCoordinateDim()`
5634939f6067SMatthew G. Knepley 
5635939f6067SMatthew G. Knepley   Output Parameter:
563620f4b53cSBarry Smith . dm - The `DM`
563720f4b53cSBarry Smith 
563820f4b53cSBarry Smith   Level: advanced
5639939f6067SMatthew G. Knepley 
5640a1cb98faSBarry Smith   Note:
5641a1cb98faSBarry Smith   Two triangles sharing a face would have input
5642a1cb98faSBarry Smith .vb
5643a1cb98faSBarry Smith   depth = 1, numPoints = [4 2], coneSize = [3 3 0 0 0 0]
5644a1cb98faSBarry Smith   cones = [2 3 4  3 5 4], coneOrientations = [0 0 0  0 0 0]
5645a1cb98faSBarry Smith  vertexCoords = [-1.0 0.0  0.0 -1.0  0.0 1.0  1.0 0.0]
5646a1cb98faSBarry Smith .ve
5647939f6067SMatthew G. Knepley   which would result in the DMPlex
5648a1cb98faSBarry Smith .vb
5649a1cb98faSBarry Smith         4
5650a1cb98faSBarry Smith       / | \
5651a1cb98faSBarry Smith      /  |  \
5652a1cb98faSBarry Smith     /   |   \
5653a1cb98faSBarry Smith    2  0 | 1  5
5654a1cb98faSBarry Smith     \   |   /
5655a1cb98faSBarry Smith      \  |  /
5656a1cb98faSBarry Smith       \ | /
5657a1cb98faSBarry Smith         3
5658a1cb98faSBarry Smith .ve
5659a1cb98faSBarry Smith   Notice that all points are numbered consecutively, unlike `DMPlexCreateFromCellListPetsc()`
5660939f6067SMatthew G. Knepley 
56611cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`
5662939f6067SMatthew G. Knepley @*/
5663d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateFromDAG(DM dm, PetscInt depth, const PetscInt numPoints[], const PetscInt coneSize[], const PetscInt cones[], const PetscInt coneOrientations[], const PetscScalar vertexCoords[])
5664d71ae5a4SJacob Faibussowitsch {
56659298eaa6SMatthew G Knepley   Vec          coordinates;
56669298eaa6SMatthew G Knepley   PetscSection coordSection;
56679298eaa6SMatthew G Knepley   PetscScalar *coords;
5668811e8653SToby Isaac   PetscInt     coordSize, firstVertex = -1, pStart = 0, pEnd = 0, p, v, dim, dimEmbed, d, off;
56699298eaa6SMatthew G Knepley 
56709298eaa6SMatthew G Knepley   PetscFunctionBegin;
56719566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
56729566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dimEmbed));
567363a3b9bcSJacob Faibussowitsch   PetscCheck(dimEmbed >= dim, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Embedding dimension %" PetscInt_FMT " cannot be less than intrinsic dimension %" PetscInt_FMT, dimEmbed, dim);
56749298eaa6SMatthew G Knepley   for (d = 0; d <= depth; ++d) pEnd += numPoints[d];
56759566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, pStart, pEnd));
56769298eaa6SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
56779566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeSize(dm, p, coneSize[p - pStart]));
5678ad540459SPierre Jolivet     if (firstVertex < 0 && !coneSize[p - pStart]) firstVertex = p - pStart;
567997e052ccSToby Isaac   }
56801dca8a05SBarry Smith   PetscCheck(firstVertex >= 0 || !numPoints[0], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Expected %" PetscInt_FMT " vertices but could not find any", numPoints[0]);
56819566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm)); /* Allocate space for cones */
56829298eaa6SMatthew G Knepley   for (p = pStart, off = 0; p < pEnd; off += coneSize[p - pStart], ++p) {
56839566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(dm, p, &cones[off]));
56849566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeOrientation(dm, p, &coneOrientations[off]));
56859298eaa6SMatthew G Knepley   }
56869566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
56879566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
56889298eaa6SMatthew G Knepley   /* Build coordinates */
56899566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
56909566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
56919566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dimEmbed));
56929566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numPoints[0]));
56939298eaa6SMatthew G Knepley   for (v = firstVertex; v < firstVertex + numPoints[0]; ++v) {
56949566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, dimEmbed));
56959566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dimEmbed));
56969298eaa6SMatthew G Knepley   }
56979566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
56989566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
56999566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
57009566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
57019566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
57029566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, dimEmbed));
57039566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
57049318fe57SMatthew G. Knepley   if (vertexCoords) {
57059566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
57069298eaa6SMatthew G Knepley     for (v = 0; v < numPoints[0]; ++v) {
57079298eaa6SMatthew G Knepley       PetscInt off;
57089298eaa6SMatthew G Knepley 
57099566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, v + firstVertex, &off));
5710ad540459SPierre Jolivet       for (d = 0; d < dimEmbed; ++d) coords[off + d] = vertexCoords[v * dimEmbed + d];
57119298eaa6SMatthew G Knepley     }
57129318fe57SMatthew G. Knepley   }
57139566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
57149566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
57159566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
57163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
57179298eaa6SMatthew G Knepley }
57188415267dSToby Isaac 
5719a4e35b19SJacob Faibussowitsch /*
5720a1cb98faSBarry Smith   DMPlexCreateCellVertexFromFile - Create a `DMPLEX` mesh from a simple cell-vertex file.
5721a1cb98faSBarry Smith 
5722a1cb98faSBarry Smith   Collective
57238ca92349SMatthew G. Knepley 
57248ca92349SMatthew G. Knepley + comm        - The MPI communicator
57258ca92349SMatthew G. Knepley . filename    - Name of the .dat file
57268ca92349SMatthew G. Knepley - interpolate - Create faces and edges in the mesh
57278ca92349SMatthew G. Knepley 
57288ca92349SMatthew G. Knepley   Output Parameter:
5729a1cb98faSBarry Smith . dm  - The `DM` object representing the mesh
57308ca92349SMatthew G. Knepley 
57318ca92349SMatthew G. Knepley   Level: beginner
57328ca92349SMatthew G. Knepley 
5733a1cb98faSBarry Smith   Note:
5734a1cb98faSBarry Smith   The format is the simplest possible:
5735a1cb98faSBarry Smith .vb
5736d0812dedSMatthew G. Knepley   dim Ne Nv Nc Nl
5737d0812dedSMatthew G. Knepley   v_1 v_2 ... v_Nc
5738d0812dedSMatthew G. Knepley   ...
5739d0812dedSMatthew G. Knepley   x y z marker_1 ... marker_Nl
5740a1cb98faSBarry Smith .ve
5741a1cb98faSBarry Smith 
5742a1cb98faSBarry Smith   Developer Note:
5743a1cb98faSBarry Smith   Should use a `PetscViewer` not a filename
5744a1cb98faSBarry Smith 
57456afe31f6SMartin Diehl .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromFile()`, `DMPlexCreateGmsh()`, `DMPlexCreate()`
5746a4e35b19SJacob Faibussowitsch */
5747ff6a9541SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCellVertexFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
5748d71ae5a4SJacob Faibussowitsch {
57498ca92349SMatthew G. Knepley   DMLabel      marker;
57508ca92349SMatthew G. Knepley   PetscViewer  viewer;
57518ca92349SMatthew G. Knepley   Vec          coordinates;
57528ca92349SMatthew G. Knepley   PetscSection coordSection;
57538ca92349SMatthew G. Knepley   PetscScalar *coords;
57548ca92349SMatthew G. Knepley   char         line[PETSC_MAX_PATH_LEN];
5755d0812dedSMatthew G. Knepley   PetscInt     cdim, coordSize, v, c, d;
57568ca92349SMatthew G. Knepley   PetscMPIInt  rank;
5757d0812dedSMatthew G. Knepley   int          snum, dim, Nv, Nc, Ncn, Nl;
57588ca92349SMatthew G. Knepley 
57598ca92349SMatthew G. Knepley   PetscFunctionBegin;
57609566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
57619566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, &viewer));
57629566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(viewer, PETSCVIEWERASCII));
57639566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetMode(viewer, FILE_MODE_READ));
57649566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetName(viewer, filename));
5765dd400576SPatrick Sanan   if (rank == 0) {
5766d0812dedSMatthew G. Knepley     PetscCall(PetscViewerRead(viewer, line, 5, NULL, PETSC_STRING));
5767d0812dedSMatthew G. Knepley     snum = sscanf(line, "%d %d %d %d %d", &dim, &Nc, &Nv, &Ncn, &Nl);
5768d0812dedSMatthew G. Knepley     PetscCheck(snum == 5, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
576925ce1634SJed Brown   } else {
5770f8d5e320SMatthew G. Knepley     Nc = Nv = Ncn = Nl = 0;
57718ca92349SMatthew G. Knepley   }
5772d0812dedSMatthew G. Knepley   PetscCallMPI(MPI_Bcast(&dim, 1, MPI_INT, 0, comm));
5773d0812dedSMatthew G. Knepley   cdim = (PetscInt)dim;
57749566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
57759566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
57769566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(*dm, 0, Nc + Nv));
5777d0812dedSMatthew G. Knepley   PetscCall(DMSetDimension(*dm, (PetscInt)dim));
57789566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*dm, cdim));
57798ca92349SMatthew G. Knepley   /* Read topology */
5780dd400576SPatrick Sanan   if (rank == 0) {
5781f8d5e320SMatthew G. Knepley     char     format[PETSC_MAX_PATH_LEN];
5782f8d5e320SMatthew G. Knepley     PetscInt cone[8];
57838ca92349SMatthew G. Knepley     int      vbuf[8], v;
57848ca92349SMatthew G. Knepley 
57859371c9d4SSatish Balay     for (c = 0; c < Ncn; ++c) {
57869371c9d4SSatish Balay       format[c * 3 + 0] = '%';
57879371c9d4SSatish Balay       format[c * 3 + 1] = 'd';
57889371c9d4SSatish Balay       format[c * 3 + 2] = ' ';
57899371c9d4SSatish Balay     }
5790f8d5e320SMatthew G. Knepley     format[Ncn * 3 - 1] = '\0';
57919566063dSJacob Faibussowitsch     for (c = 0; c < Nc; ++c) PetscCall(DMPlexSetConeSize(*dm, c, Ncn));
57929566063dSJacob Faibussowitsch     PetscCall(DMSetUp(*dm));
57938ca92349SMatthew G. Knepley     for (c = 0; c < Nc; ++c) {
57949566063dSJacob Faibussowitsch       PetscCall(PetscViewerRead(viewer, line, Ncn, NULL, PETSC_STRING));
5795f8d5e320SMatthew G. Knepley       switch (Ncn) {
5796d71ae5a4SJacob Faibussowitsch       case 2:
5797d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1]);
5798d71ae5a4SJacob Faibussowitsch         break;
5799d71ae5a4SJacob Faibussowitsch       case 3:
5800d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2]);
5801d71ae5a4SJacob Faibussowitsch         break;
5802d71ae5a4SJacob Faibussowitsch       case 4:
5803d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3]);
5804d71ae5a4SJacob Faibussowitsch         break;
5805d71ae5a4SJacob Faibussowitsch       case 6:
5806d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5]);
5807d71ae5a4SJacob Faibussowitsch         break;
5808d71ae5a4SJacob Faibussowitsch       case 8:
5809d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5], &vbuf[6], &vbuf[7]);
5810d71ae5a4SJacob Faibussowitsch         break;
5811d71ae5a4SJacob Faibussowitsch       default:
5812d71ae5a4SJacob Faibussowitsch         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No cell shape with %d vertices", Ncn);
5813f8d5e320SMatthew G. Knepley       }
581408401ef6SPierre Jolivet       PetscCheck(snum == Ncn, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
5815f8d5e320SMatthew G. Knepley       for (v = 0; v < Ncn; ++v) cone[v] = vbuf[v] + Nc;
58168ca92349SMatthew G. Knepley       /* Hexahedra are inverted */
5817f8d5e320SMatthew G. Knepley       if (Ncn == 8) {
58188ca92349SMatthew G. Knepley         PetscInt tmp = cone[1];
58198ca92349SMatthew G. Knepley         cone[1]      = cone[3];
58208ca92349SMatthew G. Knepley         cone[3]      = tmp;
58218ca92349SMatthew G. Knepley       }
58229566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(*dm, c, cone));
58238ca92349SMatthew G. Knepley     }
58248ca92349SMatthew G. Knepley   }
58259566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(*dm));
58269566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(*dm));
58278ca92349SMatthew G. Knepley   /* Read coordinates */
58289566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(*dm, &coordSection));
58299566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
58309566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, cdim));
58319566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, Nc, Nc + Nv));
58328ca92349SMatthew G. Knepley   for (v = Nc; v < Nc + Nv; ++v) {
58339566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, cdim));
58349566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, cdim));
58358ca92349SMatthew G. Knepley   }
58369566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
58379566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
58389566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
58399566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
58409566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
58419566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, cdim));
58429566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
58439566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
5844dd400576SPatrick Sanan   if (rank == 0) {
5845f8d5e320SMatthew G. Knepley     char   format[PETSC_MAX_PATH_LEN];
58468ca92349SMatthew G. Knepley     double x[3];
5847f8d5e320SMatthew G. Knepley     int    l, val[3];
58488ca92349SMatthew G. Knepley 
5849f8d5e320SMatthew G. Knepley     if (Nl) {
58509371c9d4SSatish Balay       for (l = 0; l < Nl; ++l) {
58519371c9d4SSatish Balay         format[l * 3 + 0] = '%';
58529371c9d4SSatish Balay         format[l * 3 + 1] = 'd';
58539371c9d4SSatish Balay         format[l * 3 + 2] = ' ';
58549371c9d4SSatish Balay       }
5855f8d5e320SMatthew G. Knepley       format[Nl * 3 - 1] = '\0';
58569566063dSJacob Faibussowitsch       PetscCall(DMCreateLabel(*dm, "marker"));
58579566063dSJacob Faibussowitsch       PetscCall(DMGetLabel(*dm, "marker", &marker));
5858f8d5e320SMatthew G. Knepley     }
58598ca92349SMatthew G. Knepley     for (v = 0; v < Nv; ++v) {
58609566063dSJacob Faibussowitsch       PetscCall(PetscViewerRead(viewer, line, 3 + Nl, NULL, PETSC_STRING));
5861f8d5e320SMatthew G. Knepley       snum = sscanf(line, "%lg %lg %lg", &x[0], &x[1], &x[2]);
586208401ef6SPierre Jolivet       PetscCheck(snum == 3, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
5863f8d5e320SMatthew G. Knepley       switch (Nl) {
5864d71ae5a4SJacob Faibussowitsch       case 0:
5865d71ae5a4SJacob Faibussowitsch         snum = 0;
5866d71ae5a4SJacob Faibussowitsch         break;
5867d71ae5a4SJacob Faibussowitsch       case 1:
5868d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &val[0]);
5869d71ae5a4SJacob Faibussowitsch         break;
5870d71ae5a4SJacob Faibussowitsch       case 2:
5871d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &val[0], &val[1]);
5872d71ae5a4SJacob Faibussowitsch         break;
5873d71ae5a4SJacob Faibussowitsch       case 3:
5874d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &val[0], &val[1], &val[2]);
5875d71ae5a4SJacob Faibussowitsch         break;
5876d71ae5a4SJacob Faibussowitsch       default:
5877d71ae5a4SJacob Faibussowitsch         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Request support for %d labels", Nl);
5878f8d5e320SMatthew G. Knepley       }
587908401ef6SPierre Jolivet       PetscCheck(snum == Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
58808ca92349SMatthew G. Knepley       for (d = 0; d < cdim; ++d) coords[v * cdim + d] = x[d];
58819566063dSJacob Faibussowitsch       for (l = 0; l < Nl; ++l) PetscCall(DMLabelSetValue(marker, v + Nc, val[l]));
58828ca92349SMatthew G. Knepley     }
58838ca92349SMatthew G. Knepley   }
58849566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
58859566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(*dm, coordinates));
58869566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
58879566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(&viewer));
58888ca92349SMatthew G. Knepley   if (interpolate) {
58898ca92349SMatthew G. Knepley     DM      idm;
58908ca92349SMatthew G. Knepley     DMLabel bdlabel;
58918ca92349SMatthew G. Knepley 
58929566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
58939566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
58948ca92349SMatthew G. Knepley     *dm = idm;
58958ca92349SMatthew G. Knepley 
5896f8d5e320SMatthew G. Knepley     if (!Nl) {
58979566063dSJacob Faibussowitsch       PetscCall(DMCreateLabel(*dm, "marker"));
58989566063dSJacob Faibussowitsch       PetscCall(DMGetLabel(*dm, "marker", &bdlabel));
58999566063dSJacob Faibussowitsch       PetscCall(DMPlexMarkBoundaryFaces(*dm, PETSC_DETERMINE, bdlabel));
59009566063dSJacob Faibussowitsch       PetscCall(DMPlexLabelComplete(*dm, bdlabel));
59018ca92349SMatthew G. Knepley     }
5902f8d5e320SMatthew G. Knepley   }
59033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
59048ca92349SMatthew G. Knepley }
59058ca92349SMatthew G. Knepley 
5906cc4c1da9SBarry Smith /*@
5907a1cb98faSBarry Smith   DMPlexCreateFromFile - This takes a filename and produces a `DM`
5908a1cb98faSBarry Smith 
5909a1cb98faSBarry Smith   Collective
5910ca522641SMatthew G. Knepley 
5911ca522641SMatthew G. Knepley   Input Parameters:
5912ca522641SMatthew G. Knepley + comm        - The communicator
5913ca522641SMatthew G. Knepley . filename    - A file name
5914a1cb98faSBarry Smith . plexname    - The object name of the resulting `DM`, also used for intra-datafile lookup by some formats
5915ca522641SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
5916ca522641SMatthew G. Knepley 
5917ca522641SMatthew G. Knepley   Output Parameter:
5918a1cb98faSBarry Smith . dm - The `DM`
5919ca522641SMatthew G. Knepley 
5920a1cb98faSBarry Smith   Options Database Key:
5921a1cb98faSBarry Smith . -dm_plex_create_from_hdf5_xdmf - use the `PETSC_VIEWER_HDF5_XDMF` format for reading HDF5
592202ef0d99SVaclav Hapla 
592337fdd005SBarry Smith   Use `-dm_plex_create_ prefix` to pass options to the internal `PetscViewer`, e.g.
5924bca97951SVaclav Hapla $ -dm_plex_create_viewer_hdf5_collective
5925bca97951SVaclav Hapla 
5926ca522641SMatthew G. Knepley   Level: beginner
5927ca522641SMatthew G. Knepley 
5928a1cb98faSBarry Smith   Notes:
5929a1cb98faSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
5930a1cb98faSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
5931a1cb98faSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
5932a1cb98faSBarry Smith   The input parameter name is thus used to name the `DMPLEX` object when `DMPlexCreateFromFile()` internally
5933a1cb98faSBarry Smith   calls `DMLoad()`. Currently, name is ignored for other viewer types and/or formats.
5934a1cb98faSBarry Smith 
59351cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromDAG()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`, `PetscObjectSetName()`, `DMView()`, `DMLoad()`
5936ca522641SMatthew G. Knepley @*/
5937d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateFromFile(MPI_Comm comm, const char filename[], const char plexname[], PetscBool interpolate, DM *dm)
5938d71ae5a4SJacob Faibussowitsch {
5939ef3a5affSJacob Faibussowitsch   const char  extGmsh[]      = ".msh";
5940ef3a5affSJacob Faibussowitsch   const char  extGmsh2[]     = ".msh2";
5941ef3a5affSJacob Faibussowitsch   const char  extGmsh4[]     = ".msh4";
5942ef3a5affSJacob Faibussowitsch   const char  extCGNS[]      = ".cgns";
5943ef3a5affSJacob Faibussowitsch   const char  extExodus[]    = ".exo";
5944ef3a5affSJacob Faibussowitsch   const char  extExodus_e[]  = ".e";
5945ef3a5affSJacob Faibussowitsch   const char  extGenesis[]   = ".gen";
5946ef3a5affSJacob Faibussowitsch   const char  extFluent[]    = ".cas";
5947ef3a5affSJacob Faibussowitsch   const char  extHDF5[]      = ".h5";
59486f2c871aSStefano Zampini   const char  extXDMFHDF5[]  = ".xdmf.h5";
5949ef3a5affSJacob Faibussowitsch   const char  extPLY[]       = ".ply";
5950ef3a5affSJacob Faibussowitsch   const char  extEGADSLite[] = ".egadslite";
5951ef3a5affSJacob Faibussowitsch   const char  extEGADS[]     = ".egads";
5952ef3a5affSJacob Faibussowitsch   const char  extIGES[]      = ".igs";
5953ef3a5affSJacob Faibussowitsch   const char  extSTEP[]      = ".stp";
5954ef3a5affSJacob Faibussowitsch   const char  extCV[]        = ".dat";
5955ca522641SMatthew G. Knepley   size_t      len;
59566afe31f6SMartin Diehl   PetscBool   isGmsh, isGmsh2, isGmsh4, isCGNS, isExodus, isGenesis, isFluent, isHDF5, isPLY, isEGADSLite, isEGADS, isIGES, isSTEP, isCV, isXDMFHDF5;
5957ca522641SMatthew G. Knepley   PetscMPIInt rank;
5958ca522641SMatthew G. Knepley 
5959ca522641SMatthew G. Knepley   PetscFunctionBegin;
59604f572ea9SToby Isaac   PetscAssertPointer(filename, 2);
59614f572ea9SToby Isaac   if (plexname) PetscAssertPointer(plexname, 3);
59624f572ea9SToby Isaac   PetscAssertPointer(dm, 5);
59639566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
59649566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_CreateFromFile, 0, 0, 0, 0));
59659566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
59669566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(filename, &len));
596728b400f6SJacob Faibussowitsch   PetscCheck(len, comm, PETSC_ERR_ARG_WRONG, "Filename must be a valid path");
5968ef3a5affSJacob Faibussowitsch 
59699371c9d4SSatish Balay #define CheckExtension(extension__, is_extension__) \
59709371c9d4SSatish Balay   do { \
5971274aaeaaSJacob Faibussowitsch     PetscAssert(sizeof(extension__), comm, PETSC_ERR_PLIB, "Zero-size extension: %s", extension__); \
5972274aaeaaSJacob Faibussowitsch     /* don't count the null-terminator at the end */ \
5973274aaeaaSJacob Faibussowitsch     const size_t ext_len = sizeof(extension__) - 1; \
5974274aaeaaSJacob Faibussowitsch     if (len < ext_len) { \
5975ef3a5affSJacob Faibussowitsch       is_extension__ = PETSC_FALSE; \
5976ef3a5affSJacob Faibussowitsch     } else { \
5977274aaeaaSJacob Faibussowitsch       PetscCall(PetscStrncmp(filename + len - ext_len, extension__, ext_len, &is_extension__)); \
5978ef3a5affSJacob Faibussowitsch     } \
5979ef3a5affSJacob Faibussowitsch   } while (0)
5980ef3a5affSJacob Faibussowitsch 
5981ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh, isGmsh);
5982ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh2, isGmsh2);
5983ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh4, isGmsh4);
5984ef3a5affSJacob Faibussowitsch   CheckExtension(extCGNS, isCGNS);
5985ef3a5affSJacob Faibussowitsch   CheckExtension(extExodus, isExodus);
5986ef3a5affSJacob Faibussowitsch   if (!isExodus) CheckExtension(extExodus_e, isExodus);
5987ef3a5affSJacob Faibussowitsch   CheckExtension(extGenesis, isGenesis);
5988ef3a5affSJacob Faibussowitsch   CheckExtension(extFluent, isFluent);
5989ef3a5affSJacob Faibussowitsch   CheckExtension(extHDF5, isHDF5);
5990ef3a5affSJacob Faibussowitsch   CheckExtension(extPLY, isPLY);
5991ef3a5affSJacob Faibussowitsch   CheckExtension(extEGADSLite, isEGADSLite);
5992ef3a5affSJacob Faibussowitsch   CheckExtension(extEGADS, isEGADS);
5993ef3a5affSJacob Faibussowitsch   CheckExtension(extIGES, isIGES);
5994ef3a5affSJacob Faibussowitsch   CheckExtension(extSTEP, isSTEP);
5995ef3a5affSJacob Faibussowitsch   CheckExtension(extCV, isCV);
59966f2c871aSStefano Zampini   CheckExtension(extXDMFHDF5, isXDMFHDF5);
5997ef3a5affSJacob Faibussowitsch 
5998ef3a5affSJacob Faibussowitsch #undef CheckExtension
5999ef3a5affSJacob Faibussowitsch 
6000de78e4feSLisandro Dalcin   if (isGmsh || isGmsh2 || isGmsh4) {
60019566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateGmshFromFile(comm, filename, interpolate, dm));
6002ca522641SMatthew G. Knepley   } else if (isCGNS) {
60039566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCGNSFromFile(comm, filename, interpolate, dm));
600490c68965SMatthew G. Knepley   } else if (isExodus || isGenesis) {
60059566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateExodusFromFile(comm, filename, interpolate, dm));
60062f0bd6dcSMichael Lange   } else if (isFluent) {
60079566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFluentFromFile(comm, filename, interpolate, dm));
6008cc2f8f65SMatthew G. Knepley   } else if (isHDF5) {
6009cc2f8f65SMatthew G. Knepley     PetscViewer viewer;
6010cc2f8f65SMatthew G. Knepley 
601143b242b4SVaclav 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 */
60126f2c871aSStefano Zampini     PetscCall(PetscOptionsGetBool(NULL, NULL, "-dm_plex_create_from_hdf5_xdmf", &isXDMFHDF5, NULL));
60139566063dSJacob Faibussowitsch     PetscCall(PetscViewerCreate(comm, &viewer));
60149566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetType(viewer, PETSCVIEWERHDF5));
60159566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetOptionsPrefix(viewer, "dm_plex_create_"));
60169566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetFromOptions(viewer));
60179566063dSJacob Faibussowitsch     PetscCall(PetscViewerFileSetMode(viewer, FILE_MODE_READ));
60189566063dSJacob Faibussowitsch     PetscCall(PetscViewerFileSetName(viewer, filename));
6019cd7e8a5eSksagiyam 
60209566063dSJacob Faibussowitsch     PetscCall(DMCreate(comm, dm));
6021f4f49eeaSPierre Jolivet     PetscCall(PetscObjectSetName((PetscObject)*dm, plexname));
60229566063dSJacob Faibussowitsch     PetscCall(DMSetType(*dm, DMPLEX));
60236f2c871aSStefano Zampini     if (isXDMFHDF5) PetscCall(PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_XDMF));
60249566063dSJacob Faibussowitsch     PetscCall(DMLoad(*dm, viewer));
60256f2c871aSStefano Zampini     if (isXDMFHDF5) PetscCall(PetscViewerPopFormat(viewer));
60269566063dSJacob Faibussowitsch     PetscCall(PetscViewerDestroy(&viewer));
60275fd9971aSMatthew G. Knepley 
60285fd9971aSMatthew G. Knepley     if (interpolate) {
60295fd9971aSMatthew G. Knepley       DM idm;
60305fd9971aSMatthew G. Knepley 
60319566063dSJacob Faibussowitsch       PetscCall(DMPlexInterpolate(*dm, &idm));
60329566063dSJacob Faibussowitsch       PetscCall(DMDestroy(dm));
60335fd9971aSMatthew G. Knepley       *dm = idm;
60345fd9971aSMatthew G. Knepley     }
6035f2801cd6SMatthew G. Knepley   } else if (isPLY) {
60369566063dSJacob Faibussowitsch     PetscCall(DMPlexCreatePLYFromFile(comm, filename, interpolate, dm));
6037c1cad2e7SMatthew G. Knepley   } else if (isEGADSLite || isEGADS || isIGES || isSTEP) {
60389566063dSJacob Faibussowitsch     if (isEGADSLite) PetscCall(DMPlexCreateEGADSLiteFromFile(comm, filename, dm));
60399566063dSJacob Faibussowitsch     else PetscCall(DMPlexCreateEGADSFromFile(comm, filename, dm));
60407bee2925SMatthew Knepley     if (!interpolate) {
60417bee2925SMatthew Knepley       DM udm;
60427bee2925SMatthew Knepley 
60439566063dSJacob Faibussowitsch       PetscCall(DMPlexUninterpolate(*dm, &udm));
60449566063dSJacob Faibussowitsch       PetscCall(DMDestroy(dm));
60457bee2925SMatthew Knepley       *dm = udm;
60467bee2925SMatthew Knepley     }
60478ca92349SMatthew G. Knepley   } else if (isCV) {
60489566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCellVertexFromFile(comm, filename, interpolate, dm));
604998921bdaSJacob Faibussowitsch   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot load file %s: unrecognized extension", filename);
60509566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(plexname, &len));
6051f4f49eeaSPierre Jolivet   if (len) PetscCall(PetscObjectSetName((PetscObject)*dm, plexname));
60529566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_CreateFromFile, 0, 0, 0, 0));
60533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6054ca522641SMatthew G. Knepley }
605512b8a6daSStefano Zampini 
6056cc4c1da9SBarry Smith /*@
60579f6c5813SMatthew 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.
60589f6c5813SMatthew G. Knepley 
60590528010dSStefano Zampini   Input Parameters:
60600528010dSStefano Zampini + tr     - The `DMPlexTransform`
60610528010dSStefano Zampini - prefix - An options prefix, or NULL
60629f6c5813SMatthew G. Knepley 
60639f6c5813SMatthew G. Knepley   Output Parameter:
60649f6c5813SMatthew G. Knepley . dm - The `DM`
60659f6c5813SMatthew G. Knepley 
60669f6c5813SMatthew G. Knepley   Level: beginner
60679f6c5813SMatthew G. Knepley 
606820f4b53cSBarry Smith   Notes:
606920f4b53cSBarry 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.
607020f4b53cSBarry Smith 
60719f6c5813SMatthew G. Knepley .seealso: `DMPlexCreateFromFile`, `DMPlexCreateFromDAG()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`
60729f6c5813SMatthew G. Knepley @*/
60730528010dSStefano Zampini PetscErrorCode DMPlexCreateEphemeral(DMPlexTransform tr, const char prefix[], DM *dm)
60749f6c5813SMatthew G. Knepley {
60750528010dSStefano Zampini   DM           bdm, bcdm, cdm;
60760528010dSStefano Zampini   Vec          coordinates, coordinatesNew;
60770528010dSStefano Zampini   PetscSection cs;
6078817b2c36SMatthew G. Knepley   PetscInt     cdim, Nl;
60799f6c5813SMatthew G. Knepley 
60809f6c5813SMatthew G. Knepley   PetscFunctionBegin;
60819f6c5813SMatthew G. Knepley   PetscCall(DMCreate(PetscObjectComm((PetscObject)tr), dm));
60829f6c5813SMatthew G. Knepley   PetscCall(DMSetType(*dm, DMPLEX));
60830528010dSStefano Zampini   ((DM_Plex *)(*dm)->data)->interpolated = DMPLEX_INTERPOLATED_FULL;
60840528010dSStefano Zampini   // Handle coordinates
60850528010dSStefano Zampini   PetscCall(DMPlexTransformGetDM(tr, &bdm));
6086817b2c36SMatthew G. Knepley   PetscCall(DMPlexTransformSetDimensions(tr, bdm, *dm));
6087817b2c36SMatthew G. Knepley   PetscCall(DMGetCoordinateDim(*dm, &cdim));
60880528010dSStefano Zampini   PetscCall(DMGetCoordinateDM(bdm, &bcdm));
60890528010dSStefano Zampini   PetscCall(DMGetCoordinateDM(*dm, &cdm));
60900528010dSStefano Zampini   PetscCall(DMCopyDisc(bcdm, cdm));
60910528010dSStefano Zampini   PetscCall(DMGetLocalSection(cdm, &cs));
60920528010dSStefano Zampini   PetscCall(PetscSectionSetNumFields(cs, 1));
60930528010dSStefano Zampini   PetscCall(PetscSectionSetFieldComponents(cs, 0, cdim));
60940528010dSStefano Zampini   PetscCall(DMGetCoordinatesLocal(bdm, &coordinates));
60950528010dSStefano Zampini   PetscCall(VecDuplicate(coordinates, &coordinatesNew));
60960528010dSStefano Zampini   PetscCall(VecCopy(coordinates, coordinatesNew));
60970528010dSStefano Zampini   PetscCall(DMSetCoordinatesLocal(*dm, coordinatesNew));
60980528010dSStefano Zampini   PetscCall(VecDestroy(&coordinatesNew));
60999f6c5813SMatthew G. Knepley 
61009f6c5813SMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)tr));
61019f6c5813SMatthew G. Knepley   PetscCall(DMPlexTransformDestroy(&((DM_Plex *)(*dm)->data)->tr));
61029f6c5813SMatthew G. Knepley   ((DM_Plex *)(*dm)->data)->tr = tr;
61030528010dSStefano Zampini   PetscCall(DMPlexDistributeSetDefault(*dm, PETSC_FALSE));
61040528010dSStefano Zampini   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)*dm, prefix));
61050528010dSStefano Zampini   PetscCall(DMSetFromOptions(*dm));
61069f6c5813SMatthew G. Knepley 
61079f6c5813SMatthew G. Knepley   PetscCall(DMGetNumLabels(bdm, &Nl));
61089f6c5813SMatthew G. Knepley   for (PetscInt l = 0; l < Nl; ++l) {
61099f6c5813SMatthew G. Knepley     DMLabel     label, labelNew;
61109f6c5813SMatthew G. Knepley     const char *lname;
61119f6c5813SMatthew G. Knepley     PetscBool   isDepth, isCellType;
61129f6c5813SMatthew G. Knepley 
61139f6c5813SMatthew G. Knepley     PetscCall(DMGetLabelName(bdm, l, &lname));
61149f6c5813SMatthew G. Knepley     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
61159f6c5813SMatthew G. Knepley     if (isDepth) continue;
61169f6c5813SMatthew G. Knepley     PetscCall(PetscStrcmp(lname, "celltype", &isCellType));
61179f6c5813SMatthew G. Knepley     if (isCellType) continue;
61189f6c5813SMatthew G. Knepley     PetscCall(DMCreateLabel(*dm, lname));
61199f6c5813SMatthew G. Knepley     PetscCall(DMGetLabel(bdm, lname, &label));
61209f6c5813SMatthew G. Knepley     PetscCall(DMGetLabel(*dm, lname, &labelNew));
61219f6c5813SMatthew G. Knepley     PetscCall(DMLabelSetType(labelNew, DMLABELEPHEMERAL));
61229f6c5813SMatthew G. Knepley     PetscCall(DMLabelEphemeralSetLabel(labelNew, label));
61239f6c5813SMatthew G. Knepley     PetscCall(DMLabelEphemeralSetTransform(labelNew, tr));
61249f6c5813SMatthew G. Knepley     PetscCall(DMLabelSetUp(labelNew));
61259f6c5813SMatthew G. Knepley   }
61263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
61279f6c5813SMatthew G. Knepley }
6128