xref: /petsc/src/dm/impls/plex/plexcreate.c (revision b9da1bb3453dee27f36fc5f67fa6245b4c0fc22b)
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;
30835f2295SStefano Zampini   if (dmin == dmout) PetscFunctionReturn(PETSC_SUCCESS);
3112a88998SMatthew G. Knepley   PetscCall(DMGetVecType(dmin, &vecType));
3212a88998SMatthew G. Knepley   PetscCall(DMSetVecType(dmout, vecType));
3312a88998SMatthew G. Knepley   PetscCall(DMGetMatType(dmin, &matType));
3412a88998SMatthew G. Knepley   PetscCall(DMSetMatType(dmout, matType));
35e600fa54SMatthew G. Knepley   if (copyPeriodicity) {
364fb89dddSMatthew G. Knepley     PetscCall(DMGetPeriodicity(dmin, &maxCell, &Lstart, &L));
374fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dmout, maxCell, Lstart, L));
383d0e8ed9SDavid Salac     PetscCall(DMLocalizeCoordinates(dmout));
39e600fa54SMatthew G. Knepley   }
409566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeGetDefault(dmin, &dist));
419566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeSetDefault(dmout, dist));
426bc1bd01Sksagiyam   PetscCall(DMPlexReorderGetDefault(dmin, &reorder));
436bc1bd01Sksagiyam   PetscCall(DMPlexReorderSetDefault(dmout, reorder));
445962854dSMatthew G. Knepley   PetscCall(DMPlexGetUseCeed(dmin, &useCeed));
455962854dSMatthew G. Knepley   PetscCall(DMPlexSetUseCeed(dmout, useCeed));
46e600fa54SMatthew G. Knepley   ((DM_Plex *)dmout->data)->useHashLocation = ((DM_Plex *)dmin->data)->useHashLocation;
475962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printSetValues  = ((DM_Plex *)dmin->data)->printSetValues;
485962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printFEM        = ((DM_Plex *)dmin->data)->printFEM;
495962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printFVM        = ((DM_Plex *)dmin->data)->printFVM;
505962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printL2         = ((DM_Plex *)dmin->data)->printL2;
515962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printLocate     = ((DM_Plex *)dmin->data)->printLocate;
52a77a5016SMatthew G. Knepley   ((DM_Plex *)dmout->data)->printProject    = ((DM_Plex *)dmin->data)->printProject;
535962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printTol        = ((DM_Plex *)dmin->data)->printTol;
541baa6e33SBarry Smith   if (copyOverlap) PetscCall(DMPlexSetOverlap_Plex(dmout, dmin, 0));
553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
56e600fa54SMatthew G. Knepley }
57e600fa54SMatthew G. Knepley 
589318fe57SMatthew G. Knepley /* Replace dm with the contents of ndm, and then destroy ndm
599318fe57SMatthew G. Knepley    - Share the DM_Plex structure
609318fe57SMatthew G. Knepley    - Share the coordinates
619318fe57SMatthew G. Knepley    - Share the SF
629318fe57SMatthew G. Knepley */
63d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexReplace_Internal(DM dm, DM *ndm)
64d71ae5a4SJacob Faibussowitsch {
659318fe57SMatthew G. Knepley   PetscSF          sf;
669318fe57SMatthew G. Knepley   DM               dmNew = *ndm, coordDM, coarseDM;
679318fe57SMatthew G. Knepley   Vec              coords;
684fb89dddSMatthew G. Knepley   const PetscReal *maxCell, *Lstart, *L;
699318fe57SMatthew G. Knepley   PetscInt         dim, cdim;
70e535cce4SJames Wright   PetscBool        use_natural;
719318fe57SMatthew G. Knepley 
729318fe57SMatthew G. Knepley   PetscFunctionBegin;
739318fe57SMatthew G. Knepley   if (dm == dmNew) {
749566063dSJacob Faibussowitsch     PetscCall(DMDestroy(ndm));
753ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
769318fe57SMatthew G. Knepley   }
779318fe57SMatthew G. Knepley   dm->setupcalled = dmNew->setupcalled;
78d0812dedSMatthew G. Knepley   if (!dm->hdr.name) {
79d0812dedSMatthew G. Knepley     const char *name;
80d0812dedSMatthew G. Knepley 
81d0812dedSMatthew G. Knepley     PetscCall(PetscObjectGetName((PetscObject)*ndm, &name));
82d0812dedSMatthew G. Knepley     PetscCall(PetscObjectSetName((PetscObject)dm, name));
83d0812dedSMatthew G. Knepley   }
849566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dmNew, &dim));
859566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
869566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dmNew, &cdim));
879566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, cdim));
889566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmNew, &sf));
899566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dm, sf));
909566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmNew, &coordDM));
919566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmNew, &coords));
929566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dm, coordDM));
939566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coords));
946858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dmNew, &coordDM));
956858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dmNew, &coords));
966858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinateDM(dm, coordDM));
976858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinatesLocal(dm, coords));
989318fe57SMatthew G. Knepley   /* Do not want to create the coordinate field if it does not already exist, so do not call DMGetCoordinateField() */
996858538eSMatthew G. Knepley   PetscCall(DMFieldDestroy(&dm->coordinates[0].field));
1006858538eSMatthew G. Knepley   dm->coordinates[0].field            = dmNew->coordinates[0].field;
10161a622f3SMatthew G. Knepley   ((DM_Plex *)dmNew->data)->coordFunc = ((DM_Plex *)dm->data)->coordFunc;
1024fb89dddSMatthew G. Knepley   PetscCall(DMGetPeriodicity(dmNew, &maxCell, &Lstart, &L));
1034fb89dddSMatthew G. Knepley   PetscCall(DMSetPeriodicity(dm, maxCell, Lstart, L));
104e535cce4SJames Wright   PetscCall(DMGetNaturalSF(dmNew, &sf));
105e535cce4SJames Wright   PetscCall(DMSetNaturalSF(dm, sf));
106e535cce4SJames Wright   PetscCall(DMGetUseNatural(dmNew, &use_natural));
107e535cce4SJames Wright   PetscCall(DMSetUseNatural(dm, use_natural));
1089566063dSJacob Faibussowitsch   PetscCall(DMDestroy_Plex(dm));
1099566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(dm));
1109318fe57SMatthew G. Knepley   dm->data = dmNew->data;
1119318fe57SMatthew G. Knepley   ((DM_Plex *)dmNew->data)->refct++;
1121fca310dSJames Wright   {
1131fca310dSJames Wright     PetscInt       num_face_sfs;
1141fca310dSJames Wright     const PetscSF *sfs;
1151fca310dSJames Wright     PetscCall(DMPlexGetIsoperiodicFaceSF(dm, &num_face_sfs, &sfs));
1161fca310dSJames Wright     PetscCall(DMPlexSetIsoperiodicFaceSF(dm, num_face_sfs, (PetscSF *)sfs)); // for the compose function effect on dm
1171fca310dSJames Wright   }
1189566063dSJacob Faibussowitsch   PetscCall(DMDestroyLabelLinkList_Internal(dm));
1199566063dSJacob Faibussowitsch   PetscCall(DMCopyLabels(dmNew, dm, PETSC_OWN_POINTER, PETSC_TRUE, DM_COPY_LABELS_FAIL));
1209566063dSJacob Faibussowitsch   PetscCall(DMGetCoarseDM(dmNew, &coarseDM));
1219566063dSJacob Faibussowitsch   PetscCall(DMSetCoarseDM(dm, coarseDM));
1229566063dSJacob Faibussowitsch   PetscCall(DMDestroy(ndm));
1233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1249318fe57SMatthew G. Knepley }
1259318fe57SMatthew G. Knepley 
1269318fe57SMatthew G. Knepley /* Swap dm with the contents of dmNew
1279318fe57SMatthew G. Knepley    - Swap the DM_Plex structure
1289318fe57SMatthew G. Knepley    - Swap the coordinates
1299318fe57SMatthew G. Knepley    - Swap the point PetscSF
1309318fe57SMatthew G. Knepley */
131d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexSwap_Static(DM dmA, DM dmB)
132d71ae5a4SJacob Faibussowitsch {
1339318fe57SMatthew G. Knepley   DM          coordDMA, coordDMB;
1349318fe57SMatthew G. Knepley   Vec         coordsA, coordsB;
1359318fe57SMatthew G. Knepley   PetscSF     sfA, sfB;
1369318fe57SMatthew G. Knepley   DMField     fieldTmp;
1379318fe57SMatthew G. Knepley   void       *tmp;
1389318fe57SMatthew G. Knepley   DMLabelLink listTmp;
1399318fe57SMatthew G. Knepley   DMLabel     depthTmp;
1409318fe57SMatthew G. Knepley   PetscInt    tmpI;
1419318fe57SMatthew G. Knepley 
1429318fe57SMatthew G. Knepley   PetscFunctionBegin;
1433ba16761SJacob Faibussowitsch   if (dmA == dmB) PetscFunctionReturn(PETSC_SUCCESS);
1449566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmA, &sfA));
1459566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmB, &sfB));
1469566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sfA));
1479566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dmA, sfB));
1489566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dmB, sfA));
1499566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)sfA));
1509318fe57SMatthew G. Knepley 
1519566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmA, &coordDMA));
1529566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmB, &coordDMB));
1539566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)coordDMA));
1549566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dmA, coordDMB));
1559566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dmB, coordDMA));
1569566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)coordDMA));
1579318fe57SMatthew G. Knepley 
1589566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmA, &coordsA));
1599566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmB, &coordsB));
1609566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)coordsA));
1619566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmA, coordsB));
1629566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmB, coordsA));
1639566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)coordsA));
1649318fe57SMatthew G. Knepley 
1656858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dmA, &coordDMA));
1666858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dmB, &coordDMB));
1676858538eSMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)coordDMA));
1686858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinateDM(dmA, coordDMB));
1696858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinateDM(dmB, coordDMA));
1706858538eSMatthew G. Knepley   PetscCall(PetscObjectDereference((PetscObject)coordDMA));
1716858538eSMatthew G. Knepley 
1726858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dmA, &coordsA));
1736858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dmB, &coordsB));
1746858538eSMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)coordsA));
1756858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinatesLocal(dmA, coordsB));
1766858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinatesLocal(dmB, coordsA));
1776858538eSMatthew G. Knepley   PetscCall(PetscObjectDereference((PetscObject)coordsA));
1786858538eSMatthew G. Knepley 
1796858538eSMatthew G. Knepley   fieldTmp                  = dmA->coordinates[0].field;
1806858538eSMatthew G. Knepley   dmA->coordinates[0].field = dmB->coordinates[0].field;
1816858538eSMatthew G. Knepley   dmB->coordinates[0].field = fieldTmp;
1826858538eSMatthew G. Knepley   fieldTmp                  = dmA->coordinates[1].field;
1836858538eSMatthew G. Knepley   dmA->coordinates[1].field = dmB->coordinates[1].field;
1846858538eSMatthew G. Knepley   dmB->coordinates[1].field = fieldTmp;
1859318fe57SMatthew G. Knepley   tmp                       = dmA->data;
1869318fe57SMatthew G. Knepley   dmA->data                 = dmB->data;
1879318fe57SMatthew G. Knepley   dmB->data                 = tmp;
1889318fe57SMatthew G. Knepley   listTmp                   = dmA->labels;
1899318fe57SMatthew G. Knepley   dmA->labels               = dmB->labels;
1909318fe57SMatthew G. Knepley   dmB->labels               = listTmp;
1919318fe57SMatthew G. Knepley   depthTmp                  = dmA->depthLabel;
1929318fe57SMatthew G. Knepley   dmA->depthLabel           = dmB->depthLabel;
1939318fe57SMatthew G. Knepley   dmB->depthLabel           = depthTmp;
1949318fe57SMatthew G. Knepley   depthTmp                  = dmA->celltypeLabel;
1959318fe57SMatthew G. Knepley   dmA->celltypeLabel        = dmB->celltypeLabel;
1969318fe57SMatthew G. Knepley   dmB->celltypeLabel        = depthTmp;
1979318fe57SMatthew G. Knepley   tmpI                      = dmA->levelup;
1989318fe57SMatthew G. Knepley   dmA->levelup              = dmB->levelup;
1999318fe57SMatthew G. Knepley   dmB->levelup              = tmpI;
2003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2019318fe57SMatthew G. Knepley }
2029318fe57SMatthew G. Knepley 
2033431e603SJed Brown PetscErrorCode DMPlexInterpolateInPlace_Internal(DM dm)
204d71ae5a4SJacob Faibussowitsch {
2059318fe57SMatthew G. Knepley   DM idm;
2069318fe57SMatthew G. Knepley 
2079318fe57SMatthew G. Knepley   PetscFunctionBegin;
2089566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolate(dm, &idm));
2099566063dSJacob Faibussowitsch   PetscCall(DMPlexCopyCoordinates(dm, idm));
21069d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &idm));
2113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2129318fe57SMatthew G. Knepley }
2139318fe57SMatthew G. Knepley 
2149318fe57SMatthew G. Knepley /*@C
2159318fe57SMatthew G. Knepley   DMPlexCreateCoordinateSpace - Creates a finite element space for the coordinates
2169318fe57SMatthew G. Knepley 
21720f4b53cSBarry Smith   Collective
2189318fe57SMatthew G. Knepley 
2199318fe57SMatthew G. Knepley   Input Parameters:
22060225df5SJacob Faibussowitsch + dm        - The `DMPLEX`
22120f4b53cSBarry Smith . degree    - The degree of the finite element or `PETSC_DECIDE`
222e44f6aebSMatthew G. Knepley . project   - Flag to project current coordinates into the space
2239318fe57SMatthew G. Knepley - coordFunc - An optional function to map new points from refinement to the surface
2249318fe57SMatthew G. Knepley 
2259318fe57SMatthew G. Knepley   Level: advanced
2269318fe57SMatthew G. Knepley 
2271cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `PetscPointFunc`, `PetscFECreateLagrange()`, `DMGetCoordinateDM()`
2289318fe57SMatthew G. Knepley @*/
229e44f6aebSMatthew G. Knepley PetscErrorCode DMPlexCreateCoordinateSpace(DM dm, PetscInt degree, PetscBool project, PetscPointFunc coordFunc)
230d71ae5a4SJacob Faibussowitsch {
2319318fe57SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
232e44f6aebSMatthew G. Knepley   PetscFE  fe   = NULL;
2339318fe57SMatthew G. Knepley   DM       cdm;
2341df12153SMatthew G. Knepley   PetscInt dim, dE, qorder, height;
2359318fe57SMatthew G. Knepley 
236e44f6aebSMatthew G. Knepley   PetscFunctionBegin;
2379566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
2389566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
2399318fe57SMatthew G. Knepley   qorder = degree;
240e44f6aebSMatthew G. Knepley   PetscCall(DMGetCoordinateDM(dm, &cdm));
241d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)cdm);
242dc431b0cSMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-default_quadrature_order", "Quadrature order is one less than quadrature points per edge", "DMPlexCreateCoordinateSpace", qorder, &qorder, NULL, 0));
243d0609cedSBarry Smith   PetscOptionsEnd();
2441df12153SMatthew G. Knepley   PetscCall(DMPlexGetVTKCellHeight(dm, &height));
245e44f6aebSMatthew G. Knepley   if (degree >= 0) {
246e44f6aebSMatthew G. Knepley     DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
247e44f6aebSMatthew G. Knepley     PetscInt       cStart, cEnd, gct;
248dc431b0cSMatthew G. Knepley 
2491df12153SMatthew G. Knepley     PetscCall(DMPlexGetHeightStratum(dm, height, &cStart, &cEnd));
250dc431b0cSMatthew G. Knepley     if (cEnd > cStart) PetscCall(DMPlexGetCellType(dm, cStart, &ct));
251e44f6aebSMatthew G. Knepley     gct = (PetscInt)ct;
252462c564dSBarry Smith     PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &gct, 1, MPIU_INT, MPI_MIN, PetscObjectComm((PetscObject)dm)));
253e44f6aebSMatthew G. Knepley     ct = (DMPolytopeType)gct;
254e44f6aebSMatthew G. Knepley     // Work around current bug in PetscDualSpaceSetUp_Lagrange()
255e44f6aebSMatthew G. Knepley     //   Can be seen in plex_tutorials-ex10_1
256e44f6aebSMatthew 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));
2574f9ab2b4SJed Brown   }
258e44f6aebSMatthew G. Knepley   PetscCall(DMSetCoordinateDisc(dm, fe, project));
2599566063dSJacob Faibussowitsch   PetscCall(PetscFEDestroy(&fe));
2609318fe57SMatthew G. Knepley   mesh->coordFunc = coordFunc;
2613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2629318fe57SMatthew G. Knepley }
2639318fe57SMatthew G. Knepley 
2641df5d5c5SMatthew G. Knepley /*@
2651df5d5c5SMatthew G. Knepley   DMPlexCreateDoublet - Creates a mesh of two cells of the specified type, optionally with later refinement.
2661df5d5c5SMatthew G. Knepley 
267d083f849SBarry Smith   Collective
2681df5d5c5SMatthew G. Knepley 
2691df5d5c5SMatthew G. Knepley   Input Parameters:
270a1cb98faSBarry Smith + comm            - The communicator for the `DM` object
2711df5d5c5SMatthew G. Knepley . dim             - The spatial dimension
2721df5d5c5SMatthew G. Knepley . simplex         - Flag for simplicial cells, otherwise they are tensor product cells
2731df5d5c5SMatthew G. Knepley . interpolate     - Flag to create intermediate mesh pieces (edges, faces)
2741df5d5c5SMatthew G. Knepley - refinementLimit - A nonzero number indicates the largest admissible volume for a refined cell
2751df5d5c5SMatthew G. Knepley 
2761df5d5c5SMatthew G. Knepley   Output Parameter:
27760225df5SJacob Faibussowitsch . newdm - The `DM` object
2781df5d5c5SMatthew G. Knepley 
2791df5d5c5SMatthew G. Knepley   Level: beginner
2801df5d5c5SMatthew G. Knepley 
2811cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMSetType()`, `DMCreate()`
2821df5d5c5SMatthew G. Knepley @*/
283d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateDoublet(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscBool interpolate, PetscReal refinementLimit, DM *newdm)
284d71ae5a4SJacob Faibussowitsch {
2851df5d5c5SMatthew G. Knepley   DM          dm;
2861df5d5c5SMatthew G. Knepley   PetscMPIInt rank;
2871df5d5c5SMatthew G. Knepley 
2881df5d5c5SMatthew G. Knepley   PetscFunctionBegin;
2899566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, &dm));
2909566063dSJacob Faibussowitsch   PetscCall(DMSetType(dm, DMPLEX));
2919566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
29246139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
2939566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
294ce78fa2fSMatthew G. Knepley   switch (dim) {
295ce78fa2fSMatthew G. Knepley   case 2:
2969566063dSJacob Faibussowitsch     if (simplex) PetscCall(PetscObjectSetName((PetscObject)dm, "triangular"));
2979566063dSJacob Faibussowitsch     else PetscCall(PetscObjectSetName((PetscObject)dm, "quadrilateral"));
298ce78fa2fSMatthew G. Knepley     break;
299ce78fa2fSMatthew G. Knepley   case 3:
3009566063dSJacob Faibussowitsch     if (simplex) PetscCall(PetscObjectSetName((PetscObject)dm, "tetrahedral"));
3019566063dSJacob Faibussowitsch     else PetscCall(PetscObjectSetName((PetscObject)dm, "hexahedral"));
302ce78fa2fSMatthew G. Knepley     break;
303d71ae5a4SJacob Faibussowitsch   default:
304d71ae5a4SJacob Faibussowitsch     SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %" PetscInt_FMT, dim);
305ce78fa2fSMatthew G. Knepley   }
3061df5d5c5SMatthew G. Knepley   if (rank) {
3071df5d5c5SMatthew G. Knepley     PetscInt numPoints[2] = {0, 0};
3089566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, NULL, NULL, NULL, NULL));
3091df5d5c5SMatthew G. Knepley   } else {
3101df5d5c5SMatthew G. Knepley     switch (dim) {
3111df5d5c5SMatthew G. Knepley     case 2:
3121df5d5c5SMatthew G. Knepley       if (simplex) {
3131df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {4, 2};
3141df5d5c5SMatthew G. Knepley         PetscInt    coneSize[6]         = {3, 3, 0, 0, 0, 0};
3151df5d5c5SMatthew G. Knepley         PetscInt    cones[6]            = {2, 3, 4, 5, 4, 3};
3161df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
3171df5d5c5SMatthew G. Knepley         PetscScalar vertexCoords[8]     = {-0.5, 0.5, 0.0, 0.0, 0.0, 1.0, 0.5, 0.5};
3181df5d5c5SMatthew G. Knepley 
3199566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
3201df5d5c5SMatthew G. Knepley       } else {
3211df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {6, 2};
3221df5d5c5SMatthew G. Knepley         PetscInt    coneSize[8]         = {4, 4, 0, 0, 0, 0, 0, 0};
3231df5d5c5SMatthew G. Knepley         PetscInt    cones[8]            = {2, 3, 4, 5, 3, 6, 7, 4};
3241df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
3251df5d5c5SMatthew 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};
3261df5d5c5SMatthew G. Knepley 
3279566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
3281df5d5c5SMatthew G. Knepley       }
3291df5d5c5SMatthew G. Knepley       break;
3301df5d5c5SMatthew G. Knepley     case 3:
3311df5d5c5SMatthew G. Knepley       if (simplex) {
3321df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {5, 2};
3331df5d5c5SMatthew G. Knepley         PetscInt    coneSize[7]         = {4, 4, 0, 0, 0, 0, 0};
3341df5d5c5SMatthew G. Knepley         PetscInt    cones[8]            = {4, 3, 5, 2, 5, 3, 4, 6};
3351df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
3361df5d5c5SMatthew 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};
3371df5d5c5SMatthew G. Knepley 
3389566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
3391df5d5c5SMatthew G. Knepley       } else {
3401df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]         = {12, 2};
3411df5d5c5SMatthew G. Knepley         PetscInt    coneSize[14]         = {8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
3421df5d5c5SMatthew G. Knepley         PetscInt    cones[16]            = {2, 3, 4, 5, 6, 7, 8, 9, 5, 4, 10, 11, 7, 12, 13, 8};
3431df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
3449371c9d4SSatish 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};
3451df5d5c5SMatthew G. Knepley 
3469566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
3471df5d5c5SMatthew G. Knepley       }
3481df5d5c5SMatthew G. Knepley       break;
349d71ae5a4SJacob Faibussowitsch     default:
350d71ae5a4SJacob Faibussowitsch       SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %" PetscInt_FMT, dim);
3511df5d5c5SMatthew G. Knepley     }
3521df5d5c5SMatthew G. Knepley   }
35346139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
3541df5d5c5SMatthew G. Knepley   *newdm = dm;
3551df5d5c5SMatthew G. Knepley   if (refinementLimit > 0.0) {
3561df5d5c5SMatthew G. Knepley     DM          rdm;
3571df5d5c5SMatthew G. Knepley     const char *name;
3581df5d5c5SMatthew G. Knepley 
3599566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementUniform(*newdm, PETSC_FALSE));
3609566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementLimit(*newdm, refinementLimit));
3619566063dSJacob Faibussowitsch     PetscCall(DMRefine(*newdm, comm, &rdm));
3629566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)*newdm, &name));
3639566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)rdm, name));
3649566063dSJacob Faibussowitsch     PetscCall(DMDestroy(newdm));
3651df5d5c5SMatthew G. Knepley     *newdm = rdm;
3661df5d5c5SMatthew G. Knepley   }
3671df5d5c5SMatthew G. Knepley   if (interpolate) {
3685fd9971aSMatthew G. Knepley     DM idm;
3691df5d5c5SMatthew G. Knepley 
3709566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*newdm, &idm));
3719566063dSJacob Faibussowitsch     PetscCall(DMDestroy(newdm));
3721df5d5c5SMatthew G. Knepley     *newdm = idm;
3731df5d5c5SMatthew G. Knepley   }
3743ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3751df5d5c5SMatthew G. Knepley }
3761df5d5c5SMatthew G. Knepley 
377d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
378d71ae5a4SJacob Faibussowitsch {
3799318fe57SMatthew G. Knepley   const PetscInt numVertices    = 2;
3809318fe57SMatthew G. Knepley   PetscInt       markerRight    = 1;
3819318fe57SMatthew G. Knepley   PetscInt       markerLeft     = 1;
3829318fe57SMatthew G. Knepley   PetscBool      markerSeparate = PETSC_FALSE;
3839318fe57SMatthew G. Knepley   Vec            coordinates;
3849318fe57SMatthew G. Knepley   PetscSection   coordSection;
3859318fe57SMatthew G. Knepley   PetscScalar   *coords;
3869318fe57SMatthew G. Knepley   PetscInt       coordSize;
3879318fe57SMatthew G. Knepley   PetscMPIInt    rank;
3889318fe57SMatthew G. Knepley   PetscInt       cdim = 1, v;
389552f7358SJed Brown 
3909318fe57SMatthew G. Knepley   PetscFunctionBegin;
3919566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
3929318fe57SMatthew G. Knepley   if (markerSeparate) {
3939318fe57SMatthew G. Knepley     markerRight = 2;
3949318fe57SMatthew G. Knepley     markerLeft  = 1;
3959318fe57SMatthew G. Knepley   }
3969566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
397c5853193SPierre Jolivet   if (rank == 0) {
3989566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numVertices));
3999566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
4009566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", 0, markerLeft));
4019566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", 1, markerRight));
4029318fe57SMatthew G. Knepley   }
4039566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
4049566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
4059318fe57SMatthew G. Knepley   /* Build coordinates */
4069566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, cdim));
4079566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
4089566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
4099566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, 0, numVertices));
4109566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, cdim));
4119318fe57SMatthew G. Knepley   for (v = 0; v < numVertices; ++v) {
4129566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, cdim));
4139566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, cdim));
4149318fe57SMatthew G. Knepley   }
4159566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
4169566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
4179566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
4189566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
4199566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
4209566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, cdim));
4219566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
4229566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
4239318fe57SMatthew G. Knepley   coords[0] = lower[0];
4249318fe57SMatthew G. Knepley   coords[1] = upper[0];
4259566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
4269566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
4279566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
4283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4299318fe57SMatthew G. Knepley }
43026492d91SMatthew G. Knepley 
431d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
432d71ae5a4SJacob Faibussowitsch {
4331df21d24SMatthew G. Knepley   const PetscInt numVertices    = (edges[0] + 1) * (edges[1] + 1);
4341df21d24SMatthew G. Knepley   const PetscInt numEdges       = edges[0] * (edges[1] + 1) + (edges[0] + 1) * edges[1];
435552f7358SJed Brown   PetscInt       markerTop      = 1;
436552f7358SJed Brown   PetscInt       markerBottom   = 1;
437552f7358SJed Brown   PetscInt       markerRight    = 1;
438552f7358SJed Brown   PetscInt       markerLeft     = 1;
439552f7358SJed Brown   PetscBool      markerSeparate = PETSC_FALSE;
440552f7358SJed Brown   Vec            coordinates;
441552f7358SJed Brown   PetscSection   coordSection;
442552f7358SJed Brown   PetscScalar   *coords;
443552f7358SJed Brown   PetscInt       coordSize;
444552f7358SJed Brown   PetscMPIInt    rank;
445552f7358SJed Brown   PetscInt       v, vx, vy;
446552f7358SJed Brown 
447552f7358SJed Brown   PetscFunctionBegin;
4489566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
449552f7358SJed Brown   if (markerSeparate) {
4501df21d24SMatthew G. Knepley     markerTop    = 3;
4511df21d24SMatthew G. Knepley     markerBottom = 1;
4521df21d24SMatthew G. Knepley     markerRight  = 2;
4531df21d24SMatthew G. Knepley     markerLeft   = 4;
454552f7358SJed Brown   }
4559566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
456dd400576SPatrick Sanan   if (rank == 0) {
457552f7358SJed Brown     PetscInt e, ex, ey;
458552f7358SJed Brown 
4599566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numEdges + numVertices));
46048a46eb9SPierre Jolivet     for (e = 0; e < numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2));
4619566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
462552f7358SJed Brown     for (vx = 0; vx <= edges[0]; vx++) {
463552f7358SJed Brown       for (ey = 0; ey < edges[1]; ey++) {
464552f7358SJed Brown         PetscInt edge   = vx * edges[1] + ey + edges[0] * (edges[1] + 1);
465552f7358SJed Brown         PetscInt vertex = ey * (edges[0] + 1) + vx + numEdges;
466da80777bSKarl Rupp         PetscInt cone[2];
467552f7358SJed Brown 
4689371c9d4SSatish Balay         cone[0] = vertex;
4699371c9d4SSatish Balay         cone[1] = vertex + edges[0] + 1;
4709566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, edge, cone));
471552f7358SJed Brown         if (vx == edges[0]) {
4729566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
4739566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
474552f7358SJed Brown           if (ey == edges[1] - 1) {
4759566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
4769566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerRight));
477552f7358SJed Brown           }
478552f7358SJed Brown         } else if (vx == 0) {
4799566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
4809566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
481552f7358SJed Brown           if (ey == edges[1] - 1) {
4829566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
4839566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerLeft));
484552f7358SJed Brown           }
485552f7358SJed Brown         }
486552f7358SJed Brown       }
487552f7358SJed Brown     }
488552f7358SJed Brown     for (vy = 0; vy <= edges[1]; vy++) {
489552f7358SJed Brown       for (ex = 0; ex < edges[0]; ex++) {
490552f7358SJed Brown         PetscInt edge   = vy * edges[0] + ex;
491552f7358SJed Brown         PetscInt vertex = vy * (edges[0] + 1) + ex + numEdges;
492da80777bSKarl Rupp         PetscInt cone[2];
493552f7358SJed Brown 
4949371c9d4SSatish Balay         cone[0] = vertex;
4959371c9d4SSatish Balay         cone[1] = vertex + 1;
4969566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, edge, cone));
497552f7358SJed Brown         if (vy == edges[1]) {
4989566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
4999566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
500552f7358SJed Brown           if (ex == edges[0] - 1) {
5019566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
5029566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerTop));
503552f7358SJed Brown           }
504552f7358SJed Brown         } else if (vy == 0) {
5059566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
5069566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
507552f7358SJed Brown           if (ex == edges[0] - 1) {
5089566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
5099566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerBottom));
510552f7358SJed Brown           }
511552f7358SJed Brown         }
512552f7358SJed Brown       }
513552f7358SJed Brown     }
514552f7358SJed Brown   }
5159566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
5169566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
517552f7358SJed Brown   /* Build coordinates */
5189566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, 2));
5199566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
5209566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
5219566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, numEdges, numEdges + numVertices));
5229566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, 2));
523552f7358SJed Brown   for (v = numEdges; v < numEdges + numVertices; ++v) {
5249566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, 2));
5259566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, 2));
526552f7358SJed Brown   }
5279566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
5289566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
5299566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
5309566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
5319566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
5329566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, 2));
5339566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
5349566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
535552f7358SJed Brown   for (vy = 0; vy <= edges[1]; ++vy) {
536552f7358SJed Brown     for (vx = 0; vx <= edges[0]; ++vx) {
537552f7358SJed Brown       coords[(vy * (edges[0] + 1) + vx) * 2 + 0] = lower[0] + ((upper[0] - lower[0]) / edges[0]) * vx;
538552f7358SJed Brown       coords[(vy * (edges[0] + 1) + vx) * 2 + 1] = lower[1] + ((upper[1] - lower[1]) / edges[1]) * vy;
539552f7358SJed Brown     }
540552f7358SJed Brown   }
5419566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
5429566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
5439566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
5443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
545552f7358SJed Brown }
546552f7358SJed Brown 
547d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt faces[])
548d71ae5a4SJacob Faibussowitsch {
5499e8abbc3SMichael Lange   PetscInt     vertices[3], numVertices;
5507b59f5a9SMichael Lange   PetscInt     numFaces       = 2 * faces[0] * faces[1] + 2 * faces[1] * faces[2] + 2 * faces[0] * faces[2];
551c2df9bbfSMatthew G. Knepley   PetscInt     markerTop      = 1;
552c2df9bbfSMatthew G. Knepley   PetscInt     markerBottom   = 1;
553c2df9bbfSMatthew G. Knepley   PetscInt     markerFront    = 1;
554c2df9bbfSMatthew G. Knepley   PetscInt     markerBack     = 1;
555c2df9bbfSMatthew G. Knepley   PetscInt     markerRight    = 1;
556c2df9bbfSMatthew G. Knepley   PetscInt     markerLeft     = 1;
557c2df9bbfSMatthew G. Knepley   PetscBool    markerSeparate = PETSC_FALSE;
558552f7358SJed Brown   Vec          coordinates;
559552f7358SJed Brown   PetscSection coordSection;
560552f7358SJed Brown   PetscScalar *coords;
561552f7358SJed Brown   PetscInt     coordSize;
562552f7358SJed Brown   PetscMPIInt  rank;
563552f7358SJed Brown   PetscInt     v, vx, vy, vz;
5647b59f5a9SMichael Lange   PetscInt     voffset, iface = 0, cone[4];
565552f7358SJed Brown 
566552f7358SJed Brown   PetscFunctionBegin;
5671dca8a05SBarry 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");
5689566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
569c2df9bbfSMatthew G. Knepley   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
570c2df9bbfSMatthew G. Knepley   if (markerSeparate) {
571c2df9bbfSMatthew G. Knepley     markerBottom = 1;
572c2df9bbfSMatthew G. Knepley     markerTop    = 2;
573c2df9bbfSMatthew G. Knepley     markerFront  = 3;
574c2df9bbfSMatthew G. Knepley     markerBack   = 4;
575c2df9bbfSMatthew G. Knepley     markerRight  = 5;
576c2df9bbfSMatthew G. Knepley     markerLeft   = 6;
577c2df9bbfSMatthew G. Knepley   }
5789371c9d4SSatish Balay   vertices[0] = faces[0] + 1;
5799371c9d4SSatish Balay   vertices[1] = faces[1] + 1;
5809371c9d4SSatish Balay   vertices[2] = faces[2] + 1;
5819e8abbc3SMichael Lange   numVertices = vertices[0] * vertices[1] * vertices[2];
582dd400576SPatrick Sanan   if (rank == 0) {
583552f7358SJed Brown     PetscInt f;
584552f7358SJed Brown 
5859566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numFaces + numVertices));
58648a46eb9SPierre Jolivet     for (f = 0; f < numFaces; ++f) PetscCall(DMPlexSetConeSize(dm, f, 4));
5879566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
5887b59f5a9SMichael Lange 
5897b59f5a9SMichael Lange     /* Side 0 (Top) */
5907b59f5a9SMichael Lange     for (vy = 0; vy < faces[1]; vy++) {
5917b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5927b59f5a9SMichael Lange         voffset = numFaces + vertices[0] * vertices[1] * (vertices[2] - 1) + vy * vertices[0] + vx;
5939371c9d4SSatish Balay         cone[0] = voffset;
5949371c9d4SSatish Balay         cone[1] = voffset + 1;
5959371c9d4SSatish Balay         cone[2] = voffset + vertices[0] + 1;
5969371c9d4SSatish Balay         cone[3] = voffset + vertices[0];
5979566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
598c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerTop));
599c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerTop));
600c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerTop));
601c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerTop));
602c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 1, markerTop));
6037b59f5a9SMichael Lange         iface++;
604552f7358SJed Brown       }
605552f7358SJed Brown     }
6067b59f5a9SMichael Lange 
6077b59f5a9SMichael Lange     /* Side 1 (Bottom) */
6087b59f5a9SMichael Lange     for (vy = 0; vy < faces[1]; vy++) {
6097b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
6107b59f5a9SMichael Lange         voffset = numFaces + vy * (faces[0] + 1) + vx;
6119371c9d4SSatish Balay         cone[0] = voffset + 1;
6129371c9d4SSatish Balay         cone[1] = voffset;
6139371c9d4SSatish Balay         cone[2] = voffset + vertices[0];
6149371c9d4SSatish Balay         cone[3] = voffset + vertices[0] + 1;
6159566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
616c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerBottom));
617c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerBottom));
618c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerBottom));
619c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerBottom));
620c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 1, markerBottom));
6217b59f5a9SMichael Lange         iface++;
622552f7358SJed Brown       }
623552f7358SJed Brown     }
6247b59f5a9SMichael Lange 
6257b59f5a9SMichael Lange     /* Side 2 (Front) */
6267b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
6277b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
6287b59f5a9SMichael Lange         voffset = numFaces + vz * vertices[0] * vertices[1] + vx;
6299371c9d4SSatish Balay         cone[0] = voffset;
6309371c9d4SSatish Balay         cone[1] = voffset + 1;
6319371c9d4SSatish Balay         cone[2] = voffset + vertices[0] * vertices[1] + 1;
6329371c9d4SSatish Balay         cone[3] = voffset + vertices[0] * vertices[1];
6339566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
634c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerFront));
635c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerFront));
636c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerFront));
637c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 0, markerFront));
638c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 1, markerFront));
6397b59f5a9SMichael Lange         iface++;
640552f7358SJed Brown       }
6417b59f5a9SMichael Lange     }
6427b59f5a9SMichael Lange 
6437b59f5a9SMichael Lange     /* Side 3 (Back) */
6447b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
6457b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
6467b59f5a9SMichael Lange         voffset = numFaces + vz * vertices[0] * vertices[1] + vertices[0] * (vertices[1] - 1) + vx;
6479371c9d4SSatish Balay         cone[0] = voffset + vertices[0] * vertices[1];
6489371c9d4SSatish Balay         cone[1] = voffset + vertices[0] * vertices[1] + 1;
6499371c9d4SSatish Balay         cone[2] = voffset + 1;
6509371c9d4SSatish Balay         cone[3] = voffset;
6519566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
652c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerBack));
653c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerBack));
654c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerBack));
655c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 0, markerBack));
656c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 1, markerBack));
6577b59f5a9SMichael Lange         iface++;
6587b59f5a9SMichael Lange       }
6597b59f5a9SMichael Lange     }
6607b59f5a9SMichael Lange 
6617b59f5a9SMichael Lange     /* Side 4 (Left) */
6627b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
6637b59f5a9SMichael Lange       for (vy = 0; vy < faces[1]; vy++) {
6647b59f5a9SMichael Lange         voffset = numFaces + vz * vertices[0] * vertices[1] + vy * vertices[0];
6659371c9d4SSatish Balay         cone[0] = voffset;
6669371c9d4SSatish Balay         cone[1] = voffset + vertices[0] * vertices[1];
6679371c9d4SSatish Balay         cone[2] = voffset + vertices[0] * vertices[1] + vertices[0];
6689371c9d4SSatish Balay         cone[3] = voffset + vertices[0];
6699566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
670c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerLeft));
671c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerLeft));
672c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerLeft));
673c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[1] + 0, markerLeft));
674c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + vertices[0], markerLeft));
6757b59f5a9SMichael Lange         iface++;
6767b59f5a9SMichael Lange       }
6777b59f5a9SMichael Lange     }
6787b59f5a9SMichael Lange 
6797b59f5a9SMichael Lange     /* Side 5 (Right) */
6807b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
6817b59f5a9SMichael Lange       for (vy = 0; vy < faces[1]; vy++) {
682aab5bcd8SJed Brown         voffset = numFaces + vz * vertices[0] * vertices[1] + vy * vertices[0] + faces[0];
6839371c9d4SSatish Balay         cone[0] = voffset + vertices[0] * vertices[1];
6849371c9d4SSatish Balay         cone[1] = voffset;
6859371c9d4SSatish Balay         cone[2] = voffset + vertices[0];
6869371c9d4SSatish Balay         cone[3] = voffset + vertices[0] * vertices[1] + vertices[0];
6879566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
688c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerRight));
689c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerRight));
690c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerRight));
691c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 0, markerRight));
692c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + vertices[0], markerRight));
6937b59f5a9SMichael Lange         iface++;
6947b59f5a9SMichael Lange       }
695552f7358SJed Brown     }
696552f7358SJed Brown   }
6979566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
6989566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
699552f7358SJed Brown   /* Build coordinates */
7009566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, 3));
7019566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
7029566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
7039566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, numFaces, numFaces + numVertices));
7049566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, 3));
705552f7358SJed Brown   for (v = numFaces; v < numFaces + numVertices; ++v) {
7069566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, 3));
7079566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, 3));
708552f7358SJed Brown   }
7099566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
7109566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
7119566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
7129566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
7139566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
7149566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, 3));
7159566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
7169566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
717552f7358SJed Brown   for (vz = 0; vz <= faces[2]; ++vz) {
718552f7358SJed Brown     for (vy = 0; vy <= faces[1]; ++vy) {
719552f7358SJed Brown       for (vx = 0; vx <= faces[0]; ++vx) {
720552f7358SJed Brown         coords[((vz * (faces[1] + 1) + vy) * (faces[0] + 1) + vx) * 3 + 0] = lower[0] + ((upper[0] - lower[0]) / faces[0]) * vx;
721552f7358SJed Brown         coords[((vz * (faces[1] + 1) + vy) * (faces[0] + 1) + vx) * 3 + 1] = lower[1] + ((upper[1] - lower[1]) / faces[1]) * vy;
722552f7358SJed Brown         coords[((vz * (faces[1] + 1) + vy) * (faces[0] + 1) + vx) * 3 + 2] = lower[2] + ((upper[2] - lower[2]) / faces[2]) * vz;
723552f7358SJed Brown       }
724552f7358SJed Brown     }
725552f7358SJed Brown   }
7269566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
7279566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
7289566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
7293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
730552f7358SJed Brown }
731552f7358SJed Brown 
732d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate)
733d71ae5a4SJacob Faibussowitsch {
7349318fe57SMatthew G. Knepley   PetscFunctionBegin;
7359318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
73646139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
7379566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim - 1));
7389566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, dim));
7399318fe57SMatthew G. Knepley   switch (dim) {
740d71ae5a4SJacob Faibussowitsch   case 1:
741d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(dm, lower, upper, faces));
742d71ae5a4SJacob Faibussowitsch     break;
743d71ae5a4SJacob Faibussowitsch   case 2:
744d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(dm, lower, upper, faces));
745d71ae5a4SJacob Faibussowitsch     break;
746d71ae5a4SJacob Faibussowitsch   case 3:
747d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(dm, lower, upper, faces));
748d71ae5a4SJacob Faibussowitsch     break;
749d71ae5a4SJacob Faibussowitsch   default:
750d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Dimension not supported: %" PetscInt_FMT, dim);
7519318fe57SMatthew G. Knepley   }
75246139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
7539566063dSJacob Faibussowitsch   if (interpolate) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
7543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7559318fe57SMatthew G. Knepley }
7569318fe57SMatthew G. Knepley 
7579318fe57SMatthew G. Knepley /*@C
7589318fe57SMatthew G. Knepley   DMPlexCreateBoxSurfaceMesh - Creates a mesh on the surface of the tensor product of unit intervals (box) using tensor cells (hexahedra).
7599318fe57SMatthew G. Knepley 
7609318fe57SMatthew G. Knepley   Collective
7619318fe57SMatthew G. Knepley 
7629318fe57SMatthew G. Knepley   Input Parameters:
763a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
76420f4b53cSBarry Smith . dim         - The spatial dimension of the box, so the resulting mesh is has dimension `dim`-1
76520f4b53cSBarry Smith . faces       - Number of faces per dimension, or `NULL` for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
76620f4b53cSBarry Smith . lower       - The lower left corner, or `NULL` for (0, 0, 0)
76720f4b53cSBarry Smith . upper       - The upper right corner, or `NULL` for (1, 1, 1)
7689318fe57SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
7699318fe57SMatthew G. Knepley 
7709318fe57SMatthew G. Knepley   Output Parameter:
771a1cb98faSBarry Smith . dm - The `DM` object
7729318fe57SMatthew G. Knepley 
7739318fe57SMatthew G. Knepley   Level: beginner
7749318fe57SMatthew G. Knepley 
7751cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMSetFromOptions()`, `DMPlexCreateBoxMesh()`, `DMPlexCreateFromFile()`, `DMSetType()`, `DMCreate()`
7769318fe57SMatthew G. Knepley @*/
777d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateBoxSurfaceMesh(MPI_Comm comm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate, DM *dm)
778d71ae5a4SJacob Faibussowitsch {
7799318fe57SMatthew G. Knepley   PetscInt  fac[3] = {1, 1, 1};
7809318fe57SMatthew G. Knepley   PetscReal low[3] = {0, 0, 0};
7819318fe57SMatthew G. Knepley   PetscReal upp[3] = {1, 1, 1};
7829318fe57SMatthew G. Knepley 
7839318fe57SMatthew G. Knepley   PetscFunctionBegin;
7849566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
7859566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
7869566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(*dm, dim, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, interpolate));
7873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7889318fe57SMatthew G. Knepley }
7899318fe57SMatthew G. Knepley 
790d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateLineMesh_Internal(DM dm, PetscInt segments, PetscReal lower, PetscReal upper, DMBoundaryType bd)
791d71ae5a4SJacob Faibussowitsch {
792fdbf62faSLisandro Dalcin   PetscInt     i, fStart, fEnd, numCells = 0, numVerts = 0;
793fdbf62faSLisandro Dalcin   PetscInt     numPoints[2], *coneSize, *cones, *coneOrientations;
794fdbf62faSLisandro Dalcin   PetscScalar *vertexCoords;
795fdbf62faSLisandro Dalcin   PetscReal    L, maxCell;
796fdbf62faSLisandro Dalcin   PetscBool    markerSeparate = PETSC_FALSE;
797fdbf62faSLisandro Dalcin   PetscInt     markerLeft = 1, faceMarkerLeft = 1;
798fdbf62faSLisandro Dalcin   PetscInt     markerRight = 1, faceMarkerRight = 2;
799fdbf62faSLisandro Dalcin   PetscBool    wrap = (bd == DM_BOUNDARY_PERIODIC || bd == DM_BOUNDARY_TWIST) ? PETSC_TRUE : PETSC_FALSE;
800fdbf62faSLisandro Dalcin   PetscMPIInt  rank;
801fdbf62faSLisandro Dalcin 
802fdbf62faSLisandro Dalcin   PetscFunctionBegin;
8034f572ea9SToby Isaac   PetscAssertPointer(dm, 1);
804fdbf62faSLisandro Dalcin 
8059566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, 1));
8069566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
8079566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
808fdbf62faSLisandro Dalcin 
8099566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
810dd400576SPatrick Sanan   if (rank == 0) numCells = segments;
811dd400576SPatrick Sanan   if (rank == 0) numVerts = segments + (wrap ? 0 : 1);
812fdbf62faSLisandro Dalcin 
8139371c9d4SSatish Balay   numPoints[0] = numVerts;
8149371c9d4SSatish Balay   numPoints[1] = numCells;
8159566063dSJacob Faibussowitsch   PetscCall(PetscMalloc4(numCells + numVerts, &coneSize, numCells * 2, &cones, numCells + numVerts, &coneOrientations, numVerts, &vertexCoords));
8169566063dSJacob Faibussowitsch   PetscCall(PetscArrayzero(coneOrientations, numCells + numVerts));
817ad540459SPierre Jolivet   for (i = 0; i < numCells; ++i) coneSize[i] = 2;
818ad540459SPierre Jolivet   for (i = 0; i < numVerts; ++i) coneSize[numCells + i] = 0;
8199371c9d4SSatish Balay   for (i = 0; i < numCells; ++i) {
8209371c9d4SSatish Balay     cones[2 * i]     = numCells + i % numVerts;
8219371c9d4SSatish Balay     cones[2 * i + 1] = numCells + (i + 1) % numVerts;
8229371c9d4SSatish Balay   }
823ad540459SPierre Jolivet   for (i = 0; i < numVerts; ++i) vertexCoords[i] = lower + (upper - lower) * ((PetscReal)i / (PetscReal)numCells);
8249566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
8259566063dSJacob Faibussowitsch   PetscCall(PetscFree4(coneSize, cones, coneOrientations, vertexCoords));
826fdbf62faSLisandro Dalcin 
8279566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
8289371c9d4SSatish Balay   if (markerSeparate) {
8299371c9d4SSatish Balay     markerLeft  = faceMarkerLeft;
8309371c9d4SSatish Balay     markerRight = faceMarkerRight;
8319371c9d4SSatish Balay   }
832dd400576SPatrick Sanan   if (!wrap && rank == 0) {
8339566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
8349566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", fStart, markerLeft));
8359566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", fEnd - 1, markerRight));
8369566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "Face Sets", fStart, faceMarkerLeft));
8379566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "Face Sets", fEnd - 1, faceMarkerRight));
838fdbf62faSLisandro Dalcin   }
839fdbf62faSLisandro Dalcin   if (wrap) {
840fdbf62faSLisandro Dalcin     L       = upper - lower;
841fdbf62faSLisandro Dalcin     maxCell = (PetscReal)1.1 * (L / (PetscReal)PetscMax(1, segments));
8424fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, &maxCell, &lower, &L));
843fdbf62faSLisandro Dalcin   }
8449566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
8453ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
846fdbf62faSLisandro Dalcin }
847fdbf62faSLisandro Dalcin 
8484054ae39SJames Wright // Creates "Face Sets" label based on the standard box labeling conventions
8494054ae39SJames Wright static PetscErrorCode DMPlexSetBoxLabel_Internal(DM dm)
8504054ae39SJames Wright {
8516ff49feeSJames Wright   DM              cdm;
8526ff49feeSJames Wright   PetscSection    csection;
8536ff49feeSJames Wright   Vec             coordinates;
8544054ae39SJames Wright   DMLabel         label;
8556ff49feeSJames Wright   IS              faces_is;
8564054ae39SJames Wright   PetscInt        dim, num_face;
8574054ae39SJames Wright   const PetscInt *faces;
8584054ae39SJames Wright   PetscInt        faceMarkerBottom, faceMarkerTop, faceMarkerFront, faceMarkerBack, faceMarkerRight, faceMarkerLeft;
8594054ae39SJames Wright 
8604054ae39SJames Wright   PetscFunctionBeginUser;
8614054ae39SJames Wright   PetscCall(DMGetDimension(dm, &dim));
862d7c1f440SPierre Jolivet   PetscCheck((dim == 2) || (dim == 3), PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DMPlex box labeling only supports 2D and 3D meshes, received DM of dimension %" PetscInt_FMT, dim);
8634054ae39SJames Wright   // Get Face Sets label
8644054ae39SJames Wright   PetscCall(DMGetLabel(dm, "Face Sets", &label));
8654054ae39SJames Wright   if (label) {
8664054ae39SJames Wright     PetscCall(DMLabelReset(label));
8674054ae39SJames Wright   } else {
8684054ae39SJames Wright     PetscCall(DMCreateLabel(dm, "Face Sets"));
8694054ae39SJames Wright     PetscCall(DMGetLabel(dm, "Face Sets", &label));
8704054ae39SJames Wright   }
8714054ae39SJames Wright   PetscCall(DMPlexMarkBoundaryFaces(dm, 1, label));
8726ff49feeSJames Wright   PetscCall(DMGetStratumIS(dm, "Face Sets", 1, &faces_is));
8736ff49feeSJames Wright   if (!faces_is) PetscFunctionReturn(PETSC_SUCCESS); // No faces on rank
8744054ae39SJames Wright 
8754054ae39SJames Wright   switch (dim) {
8764054ae39SJames Wright   case 2:
8774054ae39SJames Wright     faceMarkerTop    = 3;
8784054ae39SJames Wright     faceMarkerBottom = 1;
8794054ae39SJames Wright     faceMarkerRight  = 2;
8804054ae39SJames Wright     faceMarkerLeft   = 4;
8814054ae39SJames Wright     break;
8824054ae39SJames Wright   case 3:
8834054ae39SJames Wright     faceMarkerBottom = 1;
8844054ae39SJames Wright     faceMarkerTop    = 2;
8854054ae39SJames Wright     faceMarkerFront  = 3;
8864054ae39SJames Wright     faceMarkerBack   = 4;
8874054ae39SJames Wright     faceMarkerRight  = 5;
8884054ae39SJames Wright     faceMarkerLeft   = 6;
8894054ae39SJames Wright     break;
8904054ae39SJames Wright   default:
8914054ae39SJames Wright     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Dimension %" PetscInt_FMT " not supported", dim);
8924054ae39SJames Wright   }
8934054ae39SJames Wright 
8946ff49feeSJames Wright   PetscCall(ISGetLocalSize(faces_is, &num_face));
8956ff49feeSJames Wright   PetscCall(ISGetIndices(faces_is, &faces));
8966ff49feeSJames Wright   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
8976ff49feeSJames Wright   PetscCall(DMGetCoordinateDM(dm, &cdm));
8986ff49feeSJames Wright   PetscCall(DMGetLocalSection(cdm, &csection));
8994054ae39SJames Wright   for (PetscInt f = 0; f < num_face; ++f) {
9006ff49feeSJames Wright     PetscScalar *coords = NULL;
9016ff49feeSJames Wright     PetscInt     face = faces[f], flip = 1, label_value = -1, coords_size;
9024054ae39SJames Wright 
9034054ae39SJames Wright     { // Determine if orientation of face is flipped
9044054ae39SJames Wright       PetscInt        num_cells_support, num_faces, start = -1;
9054054ae39SJames Wright       const PetscInt *orients, *cell_faces, *cells;
9064054ae39SJames Wright 
9074054ae39SJames Wright       PetscCall(DMPlexGetSupport(dm, face, &cells));
9084054ae39SJames Wright       PetscCall(DMPlexGetSupportSize(dm, face, &num_cells_support));
9094054ae39SJames 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);
9104054ae39SJames Wright       PetscCall(DMPlexGetCone(dm, cells[0], &cell_faces));
9114054ae39SJames Wright       PetscCall(DMPlexGetConeSize(dm, cells[0], &num_faces));
9124054ae39SJames Wright       for (PetscInt i = 0; i < num_faces; i++) {
9134054ae39SJames Wright         if (cell_faces[i] == face) start = i;
9144054ae39SJames Wright       }
9154054ae39SJames Wright       PetscCheck(start >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "Could not find face %" PetscInt_FMT " in cone of its support", face);
9164054ae39SJames Wright       PetscCall(DMPlexGetConeOrientation(dm, cells[0], &orients));
9174054ae39SJames Wright       if (orients[start] < 0) flip = -1;
9184054ae39SJames Wright     }
9194054ae39SJames Wright 
9206ff49feeSJames Wright     // Cannot use DMPlexComputeCellGeometryFVM() for high-order geometry, so must calculate normal vectors manually
9216ff49feeSJames Wright     // Use the vertices (depth 0) of coordinate DM to calculate normal vector
9226ff49feeSJames Wright     PetscCall(DMPlexVecGetClosureAtDepth_Internal(cdm, csection, coordinates, face, 0, &coords_size, &coords));
9234054ae39SJames Wright     switch (dim) {
9244054ae39SJames Wright     case 2: {
9256ff49feeSJames Wright       PetscScalar vec[2];
9266ff49feeSJames Wright 
9276ff49feeSJames Wright       for (PetscInt d = 0; d < dim; ++d) vec[d] = flip * (PetscRealPart(coords[1 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
9286ff49feeSJames Wright       PetscScalar normal[] = {vec[1], -vec[0]};
9296ff49feeSJames Wright       if (PetscAbsScalar(normal[0]) > PetscAbsScalar(normal[1])) {
9306ff49feeSJames Wright         label_value = PetscRealPart(normal[0]) > 0 ? faceMarkerRight : faceMarkerLeft;
9314054ae39SJames Wright       } else {
9326ff49feeSJames Wright         label_value = PetscRealPart(normal[1]) > 0 ? faceMarkerTop : faceMarkerBottom;
9334054ae39SJames Wright       }
9344054ae39SJames Wright     } break;
9354054ae39SJames Wright     case 3: {
9366ff49feeSJames Wright       PetscScalar vec1[3], vec2[3], normal[3];
9376ff49feeSJames Wright 
9386ff49feeSJames Wright       for (PetscInt d = 0; d < dim; ++d) {
9396ff49feeSJames Wright         vec1[d] = PetscRealPart(coords[1 * dim + d]) - PetscRealPart(coords[0 * dim + d]);
9406ff49feeSJames Wright         vec2[d] = PetscRealPart(coords[2 * dim + d]) - PetscRealPart(coords[1 * dim + d]);
9416ff49feeSJames Wright       }
9426ff49feeSJames Wright 
9436ff49feeSJames Wright       // Calculate normal vector via cross-product
9446ff49feeSJames Wright       normal[0] = flip * ((vec1[1] * vec2[2]) - (vec1[2] * vec2[1]));
9456ff49feeSJames Wright       normal[1] = flip * ((vec1[2] * vec2[0]) - (vec1[0] * vec2[2]));
9466ff49feeSJames Wright       normal[2] = flip * ((vec1[0] * vec2[1]) - (vec1[1] * vec2[0]));
9476ff49feeSJames Wright 
9486ff49feeSJames Wright       if (PetscAbsScalar(normal[0]) > PetscAbsScalar(normal[1])) {
9496ff49feeSJames Wright         if (PetscAbsScalar(normal[0]) > PetscAbsScalar(normal[2])) {
9506ff49feeSJames Wright           label_value = PetscRealPart(normal[0]) > 0 ? faceMarkerRight : faceMarkerLeft;
9514054ae39SJames Wright         } else {
9526ff49feeSJames Wright           label_value = PetscRealPart(normal[2]) > 0 ? faceMarkerTop : faceMarkerBottom;
9534054ae39SJames Wright         }
9544054ae39SJames Wright       } else {
9556ff49feeSJames Wright         if (PetscAbsScalar(normal[1]) > PetscAbsScalar(normal[2])) {
9566ff49feeSJames Wright           label_value = PetscRealPart(normal[1]) > 0 ? faceMarkerBack : faceMarkerFront;
9574054ae39SJames Wright         } else {
9586ff49feeSJames Wright           label_value = PetscRealPart(normal[2]) > 0 ? faceMarkerTop : faceMarkerBottom;
9594054ae39SJames Wright         }
9604054ae39SJames Wright       }
9614054ae39SJames Wright     } break;
9624054ae39SJames Wright     }
9634054ae39SJames Wright 
9644054ae39SJames Wright     PetscInt previous_label_value; // always 1 due to DMPlexMarkBoundaryFaces call above
9654054ae39SJames Wright     PetscCall(DMGetLabelValue(dm, "Face Sets", face, &previous_label_value));
9664054ae39SJames Wright     PetscCall(DMClearLabelValue(dm, "Face Sets", face, previous_label_value));
9674054ae39SJames Wright     PetscCall(DMSetLabelValue(dm, "Face Sets", face, label_value));
9686ff49feeSJames Wright     PetscCall(DMPlexVecRestoreClosure(cdm, csection, coordinates, face, &coords_size, &coords));
9694054ae39SJames Wright   }
9706ff49feeSJames Wright   PetscCall(ISRestoreIndices(faces_is, &faces));
9716ff49feeSJames Wright   PetscCall(ISDestroy(&faces_is));
9724054ae39SJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
9734054ae39SJames Wright }
9744054ae39SJames Wright 
975d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxMesh_Simplex_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate)
976d71ae5a4SJacob Faibussowitsch {
9779318fe57SMatthew G. Knepley   DM      boundary, vol;
978c22d3578SMatthew G. Knepley   DMLabel bdlabel;
979d6218766SMatthew G. Knepley 
980d6218766SMatthew G. Knepley   PetscFunctionBegin;
9814f572ea9SToby Isaac   PetscAssertPointer(dm, 1);
982c22d3578SMatthew 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");
9839566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &boundary));
9849566063dSJacob Faibussowitsch   PetscCall(DMSetType(boundary, DMPLEX));
9859566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(boundary, dim, faces, lower, upper, PETSC_FALSE));
9869566063dSJacob Faibussowitsch   PetscCall(DMPlexGenerate(boundary, NULL, interpolate, &vol));
987c22d3578SMatthew G. Knepley   PetscCall(DMGetLabel(vol, "marker", &bdlabel));
988c22d3578SMatthew G. Knepley   if (bdlabel) PetscCall(DMPlexLabelComplete(vol, bdlabel));
9895de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_FALSE, vol));
99069d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &vol));
9914054ae39SJames Wright   if (interpolate) {
9924054ae39SJames Wright     PetscCall(DMPlexInterpolateInPlace_Internal(dm));
9934054ae39SJames Wright     PetscCall(DMPlexSetBoxLabel_Internal(dm));
9944054ae39SJames Wright   }
9959566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&boundary));
9963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
997d6218766SMatthew G. Knepley }
998d6218766SMatthew G. Knepley 
999d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCubeMesh_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], DMBoundaryType bdX, DMBoundaryType bdY, DMBoundaryType bdZ)
1000d71ae5a4SJacob Faibussowitsch {
1001ed0e4b50SMatthew G. Knepley   DMLabel     cutLabel  = NULL;
1002f4eb4c5dSMatthew G. Knepley   PetscInt    markerTop = 1, faceMarkerTop = 1;
1003f4eb4c5dSMatthew G. Knepley   PetscInt    markerBottom = 1, faceMarkerBottom = 1;
1004f4eb4c5dSMatthew G. Knepley   PetscInt    markerFront = 1, faceMarkerFront = 1;
1005f4eb4c5dSMatthew G. Knepley   PetscInt    markerBack = 1, faceMarkerBack = 1;
1006f4eb4c5dSMatthew G. Knepley   PetscInt    markerRight = 1, faceMarkerRight = 1;
1007f4eb4c5dSMatthew G. Knepley   PetscInt    markerLeft = 1, faceMarkerLeft = 1;
10083dfda0b1SToby Isaac   PetscInt    dim;
1009d8211ee3SMatthew G. Knepley   PetscBool   markerSeparate = PETSC_FALSE, cutMarker = PETSC_FALSE;
10103dfda0b1SToby Isaac   PetscMPIInt rank;
10113dfda0b1SToby Isaac 
10123dfda0b1SToby Isaac   PetscFunctionBegin;
10139566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
10149566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
10159566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
10169566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
10179566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL));
10189371c9d4SSatish 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) {
10199371c9d4SSatish Balay     if (cutMarker) {
10209371c9d4SSatish Balay       PetscCall(DMCreateLabel(dm, "periodic_cut"));
10219371c9d4SSatish Balay       PetscCall(DMGetLabel(dm, "periodic_cut", &cutLabel));
10229371c9d4SSatish Balay     }
1023d8211ee3SMatthew G. Knepley   }
10243dfda0b1SToby Isaac   switch (dim) {
10253dfda0b1SToby Isaac   case 2:
1026f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 3;
1027f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
1028f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 2;
1029f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 4;
10303dfda0b1SToby Isaac     break;
10313dfda0b1SToby Isaac   case 3:
1032f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
1033f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 2;
1034f4eb4c5dSMatthew G. Knepley     faceMarkerFront  = 3;
1035f4eb4c5dSMatthew G. Knepley     faceMarkerBack   = 4;
1036f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 5;
1037f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 6;
10383dfda0b1SToby Isaac     break;
1039d71ae5a4SJacob Faibussowitsch   default:
1040d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Dimension %" PetscInt_FMT " not supported", dim);
10413dfda0b1SToby Isaac   }
10429566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
1043f4eb4c5dSMatthew G. Knepley   if (markerSeparate) {
1044f4eb4c5dSMatthew G. Knepley     markerBottom = faceMarkerBottom;
1045f4eb4c5dSMatthew G. Knepley     markerTop    = faceMarkerTop;
1046f4eb4c5dSMatthew G. Knepley     markerFront  = faceMarkerFront;
1047f4eb4c5dSMatthew G. Knepley     markerBack   = faceMarkerBack;
1048f4eb4c5dSMatthew G. Knepley     markerRight  = faceMarkerRight;
1049f4eb4c5dSMatthew G. Knepley     markerLeft   = faceMarkerLeft;
10503dfda0b1SToby Isaac   }
10513dfda0b1SToby Isaac   {
1052dd400576SPatrick Sanan     const PetscInt numXEdges    = rank == 0 ? edges[0] : 0;
1053dd400576SPatrick Sanan     const PetscInt numYEdges    = rank == 0 ? edges[1] : 0;
1054dd400576SPatrick Sanan     const PetscInt numZEdges    = rank == 0 ? edges[2] : 0;
1055dd400576SPatrick Sanan     const PetscInt numXVertices = rank == 0 ? (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ? edges[0] : edges[0] + 1) : 0;
1056dd400576SPatrick Sanan     const PetscInt numYVertices = rank == 0 ? (bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ? edges[1] : edges[1] + 1) : 0;
1057dd400576SPatrick Sanan     const PetscInt numZVertices = rank == 0 ? (bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST ? edges[2] : edges[2] + 1) : 0;
10583dfda0b1SToby Isaac     const PetscInt numCells     = numXEdges * numYEdges * numZEdges;
10593dfda0b1SToby Isaac     const PetscInt numXFaces    = numYEdges * numZEdges;
10603dfda0b1SToby Isaac     const PetscInt numYFaces    = numXEdges * numZEdges;
10613dfda0b1SToby Isaac     const PetscInt numZFaces    = numXEdges * numYEdges;
10623dfda0b1SToby Isaac     const PetscInt numTotXFaces = numXVertices * numXFaces;
10633dfda0b1SToby Isaac     const PetscInt numTotYFaces = numYVertices * numYFaces;
10643dfda0b1SToby Isaac     const PetscInt numTotZFaces = numZVertices * numZFaces;
10653dfda0b1SToby Isaac     const PetscInt numFaces     = numTotXFaces + numTotYFaces + numTotZFaces;
10663dfda0b1SToby Isaac     const PetscInt numTotXEdges = numXEdges * numYVertices * numZVertices;
10673dfda0b1SToby Isaac     const PetscInt numTotYEdges = numYEdges * numXVertices * numZVertices;
10683dfda0b1SToby Isaac     const PetscInt numTotZEdges = numZEdges * numXVertices * numYVertices;
10693dfda0b1SToby Isaac     const PetscInt numVertices  = numXVertices * numYVertices * numZVertices;
10703dfda0b1SToby Isaac     const PetscInt numEdges     = numTotXEdges + numTotYEdges + numTotZEdges;
10713dfda0b1SToby Isaac     const PetscInt firstVertex  = (dim == 2) ? numFaces : numCells;
10723dfda0b1SToby Isaac     const PetscInt firstXFace   = (dim == 2) ? 0 : numCells + numVertices;
10733dfda0b1SToby Isaac     const PetscInt firstYFace   = firstXFace + numTotXFaces;
10743dfda0b1SToby Isaac     const PetscInt firstZFace   = firstYFace + numTotYFaces;
10753dfda0b1SToby Isaac     const PetscInt firstXEdge   = numCells + numFaces + numVertices;
10763dfda0b1SToby Isaac     const PetscInt firstYEdge   = firstXEdge + numTotXEdges;
10773dfda0b1SToby Isaac     const PetscInt firstZEdge   = firstYEdge + numTotYEdges;
10783dfda0b1SToby Isaac     Vec            coordinates;
10793dfda0b1SToby Isaac     PetscSection   coordSection;
10803dfda0b1SToby Isaac     PetscScalar   *coords;
10813dfda0b1SToby Isaac     PetscInt       coordSize;
10823dfda0b1SToby Isaac     PetscInt       v, vx, vy, vz;
10833dfda0b1SToby Isaac     PetscInt       c, f, fx, fy, fz, e, ex, ey, ez;
10843dfda0b1SToby Isaac 
10859566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells + numFaces + numEdges + numVertices));
108648a46eb9SPierre Jolivet     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 6));
108748a46eb9SPierre Jolivet     for (f = firstXFace; f < firstXFace + numFaces; ++f) PetscCall(DMPlexSetConeSize(dm, f, 4));
108848a46eb9SPierre Jolivet     for (e = firstXEdge; e < firstXEdge + numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2));
10899566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
10903dfda0b1SToby Isaac     /* Build cells */
10913dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
10923dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
10933dfda0b1SToby Isaac         for (fx = 0; fx < numXEdges; ++fx) {
10943dfda0b1SToby Isaac           PetscInt cell  = (fz * numYEdges + fy) * numXEdges + fx;
10953dfda0b1SToby Isaac           PetscInt faceB = firstZFace + (fy * numXEdges + fx) * numZVertices + fz;
10963dfda0b1SToby Isaac           PetscInt faceT = firstZFace + (fy * numXEdges + fx) * numZVertices + ((fz + 1) % numZVertices);
10973dfda0b1SToby Isaac           PetscInt faceF = firstYFace + (fz * numXEdges + fx) * numYVertices + fy;
10983dfda0b1SToby Isaac           PetscInt faceK = firstYFace + (fz * numXEdges + fx) * numYVertices + ((fy + 1) % numYVertices);
10993dfda0b1SToby Isaac           PetscInt faceL = firstXFace + (fz * numYEdges + fy) * numXVertices + fx;
11003dfda0b1SToby Isaac           PetscInt faceR = firstXFace + (fz * numYEdges + fy) * numXVertices + ((fx + 1) % numXVertices);
11013dfda0b1SToby Isaac           /* B,  T,  F,  K,  R,  L */
1102b5a892a1SMatthew G. Knepley           PetscInt ornt[6] = {-2, 0, 0, -3, 0, -2}; /* ??? */
110342206facSLisandro Dalcin           PetscInt cone[6];
11043dfda0b1SToby Isaac 
11053dfda0b1SToby Isaac           /* no boundary twisting in 3D */
11069371c9d4SSatish Balay           cone[0] = faceB;
11079371c9d4SSatish Balay           cone[1] = faceT;
11089371c9d4SSatish Balay           cone[2] = faceF;
11099371c9d4SSatish Balay           cone[3] = faceK;
11109371c9d4SSatish Balay           cone[4] = faceR;
11119371c9d4SSatish Balay           cone[5] = faceL;
11129566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, cell, cone));
11139566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, cell, ornt));
11149566063dSJacob Faibussowitsch           if (bdX != DM_BOUNDARY_NONE && fx == numXEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
11159566063dSJacob Faibussowitsch           if (bdY != DM_BOUNDARY_NONE && fy == numYEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
11169566063dSJacob Faibussowitsch           if (bdZ != DM_BOUNDARY_NONE && fz == numZEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
11173dfda0b1SToby Isaac         }
11183dfda0b1SToby Isaac       }
11193dfda0b1SToby Isaac     }
11203dfda0b1SToby Isaac     /* Build x faces */
11213dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
11223dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
11233dfda0b1SToby Isaac         for (fx = 0; fx < numXVertices; ++fx) {
11243dfda0b1SToby Isaac           PetscInt face    = firstXFace + (fz * numYEdges + fy) * numXVertices + fx;
11253dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy * numXVertices + fx) * numZEdges + fz;
11263dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (((fy + 1) % numYVertices) * numXVertices + fx) * numZEdges + fz;
11273dfda0b1SToby Isaac           PetscInt edgeB   = firstYEdge + (fz * numXVertices + fx) * numYEdges + fy;
11283dfda0b1SToby Isaac           PetscInt edgeT   = firstYEdge + (((fz + 1) % numZVertices) * numXVertices + fx) * numYEdges + fy;
1129b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
11303dfda0b1SToby Isaac           PetscInt cone[4];
11313dfda0b1SToby Isaac 
11323dfda0b1SToby Isaac           if (dim == 3) {
11333dfda0b1SToby Isaac             /* markers */
11343dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
11353dfda0b1SToby Isaac               if (fx == numXVertices - 1) {
11369566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerRight));
11379566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerRight));
11389371c9d4SSatish Balay               } else if (fx == 0) {
11399566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerLeft));
11409566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerLeft));
11413dfda0b1SToby Isaac               }
11423dfda0b1SToby Isaac             }
11433dfda0b1SToby Isaac           }
11449371c9d4SSatish Balay           cone[0] = edgeB;
11459371c9d4SSatish Balay           cone[1] = edgeR;
11469371c9d4SSatish Balay           cone[2] = edgeT;
11479371c9d4SSatish Balay           cone[3] = edgeL;
11489566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
11499566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
11503dfda0b1SToby Isaac         }
11513dfda0b1SToby Isaac       }
11523dfda0b1SToby Isaac     }
11533dfda0b1SToby Isaac     /* Build y faces */
11543dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
115542206facSLisandro Dalcin       for (fx = 0; fx < numXEdges; ++fx) {
11563dfda0b1SToby Isaac         for (fy = 0; fy < numYVertices; ++fy) {
11573dfda0b1SToby Isaac           PetscInt face    = firstYFace + (fz * numXEdges + fx) * numYVertices + fy;
11583dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy * numXVertices + fx) * numZEdges + fz;
11593dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (fy * numXVertices + ((fx + 1) % numXVertices)) * numZEdges + fz;
11603dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz * numYVertices + fy) * numXEdges + fx;
11613dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (((fz + 1) % numZVertices) * numYVertices + fy) * numXEdges + fx;
1162b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
11633dfda0b1SToby Isaac           PetscInt cone[4];
11643dfda0b1SToby Isaac 
11653dfda0b1SToby Isaac           if (dim == 3) {
11663dfda0b1SToby Isaac             /* markers */
11673dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
11683dfda0b1SToby Isaac               if (fy == numYVertices - 1) {
11699566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerBack));
11709566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerBack));
11719371c9d4SSatish Balay               } else if (fy == 0) {
11729566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerFront));
11739566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerFront));
11743dfda0b1SToby Isaac               }
11753dfda0b1SToby Isaac             }
11763dfda0b1SToby Isaac           }
11779371c9d4SSatish Balay           cone[0] = edgeB;
11789371c9d4SSatish Balay           cone[1] = edgeR;
11799371c9d4SSatish Balay           cone[2] = edgeT;
11809371c9d4SSatish Balay           cone[3] = edgeL;
11819566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
11829566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
11833dfda0b1SToby Isaac         }
11843dfda0b1SToby Isaac       }
11853dfda0b1SToby Isaac     }
11863dfda0b1SToby Isaac     /* Build z faces */
11873dfda0b1SToby Isaac     for (fy = 0; fy < numYEdges; ++fy) {
11883dfda0b1SToby Isaac       for (fx = 0; fx < numXEdges; ++fx) {
11893dfda0b1SToby Isaac         for (fz = 0; fz < numZVertices; fz++) {
11903dfda0b1SToby Isaac           PetscInt face    = firstZFace + (fy * numXEdges + fx) * numZVertices + fz;
11913dfda0b1SToby Isaac           PetscInt edgeL   = firstYEdge + (fz * numXVertices + fx) * numYEdges + fy;
11923dfda0b1SToby Isaac           PetscInt edgeR   = firstYEdge + (fz * numXVertices + ((fx + 1) % numXVertices)) * numYEdges + fy;
11933dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz * numYVertices + fy) * numXEdges + fx;
11943dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (fz * numYVertices + ((fy + 1) % numYVertices)) * numXEdges + fx;
1195b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
11963dfda0b1SToby Isaac           PetscInt cone[4];
11973dfda0b1SToby Isaac 
11983dfda0b1SToby Isaac           if (dim == 2) {
11999371c9d4SSatish Balay             if (bdX == DM_BOUNDARY_TWIST && fx == numXEdges - 1) {
12009371c9d4SSatish Balay               edgeR += numYEdges - 1 - 2 * fy;
12019371c9d4SSatish Balay               ornt[1] = -1;
12029371c9d4SSatish Balay             }
12039371c9d4SSatish Balay             if (bdY == DM_BOUNDARY_TWIST && fy == numYEdges - 1) {
12049371c9d4SSatish Balay               edgeT += numXEdges - 1 - 2 * fx;
12059371c9d4SSatish Balay               ornt[2] = 0;
12069371c9d4SSatish Balay             }
12079566063dSJacob Faibussowitsch             if (bdX != DM_BOUNDARY_NONE && fx == numXEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, face, 2));
12089566063dSJacob Faibussowitsch             if (bdY != DM_BOUNDARY_NONE && fy == numYEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, face, 2));
1209d1c88043SMatthew G. Knepley           } else {
12103dfda0b1SToby Isaac             /* markers */
12113dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
12123dfda0b1SToby Isaac               if (fz == numZVertices - 1) {
12139566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerTop));
12149566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerTop));
12159371c9d4SSatish Balay               } else if (fz == 0) {
12169566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerBottom));
12179566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerBottom));
12183dfda0b1SToby Isaac               }
12193dfda0b1SToby Isaac             }
12203dfda0b1SToby Isaac           }
12219371c9d4SSatish Balay           cone[0] = edgeB;
12229371c9d4SSatish Balay           cone[1] = edgeR;
12239371c9d4SSatish Balay           cone[2] = edgeT;
12249371c9d4SSatish Balay           cone[3] = edgeL;
12259566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
12269566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
12273dfda0b1SToby Isaac         }
12283dfda0b1SToby Isaac       }
12293dfda0b1SToby Isaac     }
12303dfda0b1SToby Isaac     /* Build Z edges*/
12313dfda0b1SToby Isaac     for (vy = 0; vy < numYVertices; vy++) {
12323dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
12333dfda0b1SToby Isaac         for (ez = 0; ez < numZEdges; ez++) {
12343dfda0b1SToby Isaac           const PetscInt edge    = firstZEdge + (vy * numXVertices + vx) * numZEdges + ez;
12353dfda0b1SToby Isaac           const PetscInt vertexB = firstVertex + (ez * numYVertices + vy) * numXVertices + vx;
12363dfda0b1SToby Isaac           const PetscInt vertexT = firstVertex + (((ez + 1) % numZVertices) * numYVertices + vy) * numXVertices + vx;
12373dfda0b1SToby Isaac           PetscInt       cone[2];
12383dfda0b1SToby Isaac 
12399371c9d4SSatish Balay           cone[0] = vertexB;
12409371c9d4SSatish Balay           cone[1] = vertexT;
1241c2df9bbfSMatthew G. Knepley           PetscCall(DMPlexSetCone(dm, edge, cone));
12423dfda0b1SToby Isaac           if (dim == 3) {
12433dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
12443dfda0b1SToby Isaac               if (vx == numXVertices - 1) {
12459566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
1246c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1247c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1248c2df9bbfSMatthew G. Knepley               } else if (vx == 0) {
12499566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
1250c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1251c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
12523dfda0b1SToby Isaac               }
12533dfda0b1SToby Isaac             }
12543dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
12553dfda0b1SToby Isaac               if (vy == numYVertices - 1) {
12569566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBack));
1257c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBack));
1258c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBack));
1259c2df9bbfSMatthew G. Knepley               } else if (vy == 0) {
12609566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerFront));
1261c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerFront));
1262c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerFront));
12633dfda0b1SToby Isaac               }
12643dfda0b1SToby Isaac             }
12653dfda0b1SToby Isaac           }
12663dfda0b1SToby Isaac         }
12673dfda0b1SToby Isaac       }
12683dfda0b1SToby Isaac     }
12693dfda0b1SToby Isaac     /* Build Y edges*/
12703dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
12713dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
12723dfda0b1SToby Isaac         for (ey = 0; ey < numYEdges; ey++) {
12733dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdY == DM_BOUNDARY_TWIST && ey == numYEdges - 1) ? (numXVertices - vx - 1) : (vz * numYVertices + ((ey + 1) % numYVertices)) * numXVertices + vx;
12743dfda0b1SToby Isaac           const PetscInt edge    = firstYEdge + (vz * numXVertices + vx) * numYEdges + ey;
12753dfda0b1SToby Isaac           const PetscInt vertexF = firstVertex + (vz * numYVertices + ey) * numXVertices + vx;
12763dfda0b1SToby Isaac           const PetscInt vertexK = firstVertex + nextv;
12773dfda0b1SToby Isaac           PetscInt       cone[2];
12783dfda0b1SToby Isaac 
12799371c9d4SSatish Balay           cone[0] = vertexF;
12809371c9d4SSatish Balay           cone[1] = vertexK;
12819566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, edge, cone));
12823dfda0b1SToby Isaac           if (dim == 2) {
12833dfda0b1SToby Isaac             if ((bdX != DM_BOUNDARY_PERIODIC) && (bdX != DM_BOUNDARY_TWIST)) {
12843dfda0b1SToby Isaac               if (vx == numXVertices - 1) {
12859566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerRight));
12869566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
12879566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1288c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1289d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
12909566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerLeft));
12919566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
12929566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1293c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
12943dfda0b1SToby Isaac               }
1295d8211ee3SMatthew G. Knepley             } else {
12964c67ea77SStefano Zampini               if (vx == 0 && cutLabel) {
12979566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, edge, 1));
12989566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, cone[0], 1));
1299c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMLabelSetValue(cutLabel, cone[1], 1));
13003dfda0b1SToby Isaac               }
1301d8211ee3SMatthew G. Knepley             }
1302d8211ee3SMatthew G. Knepley           } else {
13033dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
13043dfda0b1SToby Isaac               if (vx == numXVertices - 1) {
13059566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
1306c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1307c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1308d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
13099566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
1310c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1311c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
13123dfda0b1SToby Isaac               }
13133dfda0b1SToby Isaac             }
13143dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
13153dfda0b1SToby Isaac               if (vz == numZVertices - 1) {
13169566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
1317c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1318c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1319d8211ee3SMatthew G. Knepley               } else if (vz == 0) {
13209566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
1321c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1322c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
13233dfda0b1SToby Isaac               }
13243dfda0b1SToby Isaac             }
13253dfda0b1SToby Isaac           }
13263dfda0b1SToby Isaac         }
13273dfda0b1SToby Isaac       }
13283dfda0b1SToby Isaac     }
13293dfda0b1SToby Isaac     /* Build X edges*/
13303dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
13313dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; vy++) {
13323dfda0b1SToby Isaac         for (ex = 0; ex < numXEdges; ex++) {
13333dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdX == DM_BOUNDARY_TWIST && ex == numXEdges - 1) ? (numYVertices - vy - 1) * numXVertices : (vz * numYVertices + vy) * numXVertices + (ex + 1) % numXVertices;
13343dfda0b1SToby Isaac           const PetscInt edge    = firstXEdge + (vz * numYVertices + vy) * numXEdges + ex;
13353dfda0b1SToby Isaac           const PetscInt vertexL = firstVertex + (vz * numYVertices + vy) * numXVertices + ex;
13363dfda0b1SToby Isaac           const PetscInt vertexR = firstVertex + nextv;
13373dfda0b1SToby Isaac           PetscInt       cone[2];
13383dfda0b1SToby Isaac 
13399371c9d4SSatish Balay           cone[0] = vertexL;
13409371c9d4SSatish Balay           cone[1] = vertexR;
13419566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, edge, cone));
13423dfda0b1SToby Isaac           if (dim == 2) {
13433dfda0b1SToby Isaac             if ((bdY != DM_BOUNDARY_PERIODIC) && (bdY != DM_BOUNDARY_TWIST)) {
13443dfda0b1SToby Isaac               if (vy == numYVertices - 1) {
13459566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerTop));
13469566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
13479566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1348c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1349d8211ee3SMatthew G. Knepley               } else if (vy == 0) {
13509566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerBottom));
13519566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
13529566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1353c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
13543dfda0b1SToby Isaac               }
1355d8211ee3SMatthew G. Knepley             } else {
13564c67ea77SStefano Zampini               if (vy == 0 && cutLabel) {
13579566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, edge, 1));
13589566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, cone[0], 1));
1359c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMLabelSetValue(cutLabel, cone[1], 1));
13603dfda0b1SToby Isaac               }
1361d8211ee3SMatthew G. Knepley             }
1362d8211ee3SMatthew G. Knepley           } else {
13633dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
13643dfda0b1SToby Isaac               if (vy == numYVertices - 1) {
13659566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBack));
1366c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBack));
1367c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBack));
1368c2df9bbfSMatthew G. Knepley               } else if (vy == 0) {
13699566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerFront));
1370c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerFront));
1371c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerFront));
13723dfda0b1SToby Isaac               }
13733dfda0b1SToby Isaac             }
13743dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
13753dfda0b1SToby Isaac               if (vz == numZVertices - 1) {
13769566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
1377c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1378c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1379c2df9bbfSMatthew G. Knepley               } else if (vz == 0) {
13809566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
1381c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1382c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
13833dfda0b1SToby Isaac               }
13843dfda0b1SToby Isaac             }
13853dfda0b1SToby Isaac           }
13863dfda0b1SToby Isaac         }
13873dfda0b1SToby Isaac       }
13883dfda0b1SToby Isaac     }
13899566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
13909566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
13913dfda0b1SToby Isaac     /* Build coordinates */
13929566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
13939566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
13949566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
13959566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numVertices));
13963dfda0b1SToby Isaac     for (v = firstVertex; v < firstVertex + numVertices; ++v) {
13979566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
13989566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
13993dfda0b1SToby Isaac     }
14009566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
14019566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
14029566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
14039566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
14049566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
14059566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
14069566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates, VECSTANDARD));
14079566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
14083dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; ++vz) {
14093dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; ++vy) {
14103dfda0b1SToby Isaac         for (vx = 0; vx < numXVertices; ++vx) {
14113dfda0b1SToby Isaac           coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 0] = lower[0] + ((upper[0] - lower[0]) / numXEdges) * vx;
14123dfda0b1SToby Isaac           coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 1] = lower[1] + ((upper[1] - lower[1]) / numYEdges) * vy;
1413ad540459SPierre Jolivet           if (dim == 3) coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 2] = lower[2] + ((upper[2] - lower[2]) / numZEdges) * vz;
14143dfda0b1SToby Isaac         }
14153dfda0b1SToby Isaac       }
14163dfda0b1SToby Isaac     }
14179566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
14189566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
14199566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
14203dfda0b1SToby Isaac   }
14213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
14223dfda0b1SToby Isaac }
14233dfda0b1SToby Isaac 
1424d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxMesh_Tensor_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
1425d71ae5a4SJacob Faibussowitsch {
14269318fe57SMatthew G. Knepley   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
14279318fe57SMatthew G. Knepley   PetscInt       fac[3] = {0, 0, 0}, d;
1428552f7358SJed Brown 
1429552f7358SJed Brown   PetscFunctionBegin;
14304f572ea9SToby Isaac   PetscAssertPointer(dm, 1);
14319318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
14329566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
14339371c9d4SSatish Balay   for (d = 0; d < dim; ++d) {
14349371c9d4SSatish Balay     fac[d] = faces[d];
14359371c9d4SSatish Balay     bdt[d] = periodicity[d];
14369371c9d4SSatish Balay   }
14379566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateCubeMesh_Internal(dm, lower, upper, fac, bdt[0], bdt[1], bdt[2]));
14389371c9d4SSatish 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))) {
14396858538eSMatthew G. Knepley     PetscReal L[3]       = {-1., -1., 0.};
14406858538eSMatthew G. Knepley     PetscReal maxCell[3] = {-1., -1., 0.};
1441552f7358SJed Brown 
14429318fe57SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
14436858538eSMatthew G. Knepley       if (periodicity[d] != DM_BOUNDARY_NONE) {
14449318fe57SMatthew G. Knepley         L[d]       = upper[d] - lower[d];
14459318fe57SMatthew G. Knepley         maxCell[d] = 1.1 * (L[d] / PetscMax(1, faces[d]));
1446768d5fceSMatthew G. Knepley       }
14476858538eSMatthew G. Knepley     }
14484fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, maxCell, lower, L));
1449768d5fceSMatthew G. Knepley   }
14509566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
14513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
14529318fe57SMatthew G. Knepley }
14539318fe57SMatthew G. Knepley 
14545dca41c3SJed 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)
1455d71ae5a4SJacob Faibussowitsch {
14569318fe57SMatthew G. Knepley   PetscFunctionBegin;
145746139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
14585dca41c3SJed Brown   if (shape == DM_SHAPE_ZBOX) PetscCall(DMPlexCreateBoxMesh_Tensor_SFC_Internal(dm, dim, faces, lower, upper, periodicity, interpolate));
14596725e60dSJed Brown   else if (dim == 1) PetscCall(DMPlexCreateLineMesh_Internal(dm, faces[0], lower[0], upper[0], periodicity[0]));
14609566063dSJacob Faibussowitsch   else if (simplex) PetscCall(DMPlexCreateBoxMesh_Simplex_Internal(dm, dim, faces, lower, upper, periodicity, interpolate));
14619566063dSJacob Faibussowitsch   else PetscCall(DMPlexCreateBoxMesh_Tensor_Internal(dm, dim, faces, lower, upper, periodicity));
14629318fe57SMatthew G. Knepley   if (!interpolate && dim > 1 && !simplex) {
1463768d5fceSMatthew G. Knepley     DM udm;
1464768d5fceSMatthew G. Knepley 
14659566063dSJacob Faibussowitsch     PetscCall(DMPlexUninterpolate(dm, &udm));
14669566063dSJacob Faibussowitsch     PetscCall(DMPlexCopyCoordinates(dm, udm));
146769d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &udm));
1468768d5fceSMatthew G. Knepley   }
146946139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
14703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1471c8c68bd8SToby Isaac }
1472c8c68bd8SToby Isaac 
14735d83a8b1SBarry Smith /*@
1474768d5fceSMatthew G. Knepley   DMPlexCreateBoxMesh - Creates a mesh on the tensor product of unit intervals (box) using simplices or tensor cells (hexahedra).
1475768d5fceSMatthew G. Knepley 
1476d083f849SBarry Smith   Collective
1477768d5fceSMatthew G. Knepley 
1478768d5fceSMatthew G. Knepley   Input Parameters:
1479a1cb98faSBarry Smith + comm               - The communicator for the `DM` object
1480768d5fceSMatthew G. Knepley . dim                - The spatial dimension
1481a1cb98faSBarry Smith . simplex            - `PETSC_TRUE` for simplices, `PETSC_FALSE` for tensor cells
148220f4b53cSBarry Smith . faces              - Number of faces per dimension, or `NULL` for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
148320f4b53cSBarry Smith . lower              - The lower left corner, or `NULL` for (0, 0, 0)
148420f4b53cSBarry Smith . upper              - The upper right corner, or `NULL` for (1, 1, 1)
148520f4b53cSBarry Smith . periodicity        - The boundary type for the X,Y,Z direction, or `NULL` for `DM_BOUNDARY_NONE`
148642108689Sksagiyam . interpolate        - Flag to create intermediate mesh pieces (edges, faces)
148742108689Sksagiyam . localizationHeight - Flag to localize edges and faces in addition to cells; only significant for periodic meshes
148842108689Sksagiyam - sparseLocalize     - Flag to localize coordinates only for cells near the periodic boundary; only significant for periodic meshes
1489768d5fceSMatthew G. Knepley 
1490768d5fceSMatthew G. Knepley   Output Parameter:
1491a1cb98faSBarry Smith . dm - The `DM` object
1492768d5fceSMatthew G. Knepley 
1493768d5fceSMatthew G. Knepley   Level: beginner
1494768d5fceSMatthew G. Knepley 
1495a1cb98faSBarry Smith   Note:
1496a1cb98faSBarry Smith   To customize this mesh using options, use
1497a1cb98faSBarry Smith .vb
1498a1cb98faSBarry Smith   DMCreate(comm, &dm);
1499a1cb98faSBarry Smith   DMSetType(dm, DMPLEX);
1500a1cb98faSBarry Smith   DMSetFromOptions(dm);
1501a1cb98faSBarry Smith .ve
1502a1cb98faSBarry Smith   and use the options in `DMSetFromOptions()`.
1503a1cb98faSBarry Smith 
1504a4e35b19SJacob Faibussowitsch   Here is the numbering returned for 2 faces in each direction for tensor cells\:
1505a1cb98faSBarry Smith .vb
1506a1cb98faSBarry Smith  10---17---11---18----12
1507a1cb98faSBarry Smith   |         |         |
1508a1cb98faSBarry Smith   |         |         |
1509a1cb98faSBarry Smith  20    2   22    3    24
1510a1cb98faSBarry Smith   |         |         |
1511a1cb98faSBarry Smith   |         |         |
1512a1cb98faSBarry Smith   7---15----8---16----9
1513a1cb98faSBarry Smith   |         |         |
1514a1cb98faSBarry Smith   |         |         |
1515a1cb98faSBarry Smith  19    0   21    1   23
1516a1cb98faSBarry Smith   |         |         |
1517a1cb98faSBarry Smith   |         |         |
1518a1cb98faSBarry Smith   4---13----5---14----6
1519a1cb98faSBarry Smith .ve
1520a1cb98faSBarry Smith   and for simplicial cells
1521a1cb98faSBarry Smith .vb
1522a1cb98faSBarry Smith  14----8---15----9----16
1523a1cb98faSBarry Smith   |\     5  |\      7 |
1524a1cb98faSBarry Smith   | \       | \       |
1525a1cb98faSBarry Smith  13   2    14    3    15
1526a1cb98faSBarry Smith   | 4   \   | 6   \   |
1527a1cb98faSBarry Smith   |       \ |       \ |
1528a1cb98faSBarry Smith  11----6---12----7----13
1529a1cb98faSBarry Smith   |\        |\        |
1530a1cb98faSBarry Smith   | \    1  | \     3 |
1531a1cb98faSBarry Smith  10   0    11    1    12
1532a1cb98faSBarry Smith   | 0   \   | 2   \   |
1533a1cb98faSBarry Smith   |       \ |       \ |
1534a1cb98faSBarry Smith   8----4----9----5----10
1535a1cb98faSBarry Smith .ve
1536a1cb98faSBarry Smith 
15371cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMSetFromOptions()`, `DMPlexCreateFromFile()`, `DMPlexCreateHexCylinderMesh()`, `DMSetType()`, `DMCreate()`
1538768d5fceSMatthew G. Knepley @*/
153942108689Sksagiyam PetscErrorCode DMPlexCreateBoxMesh(MPI_Comm comm, PetscInt dim, PetscBool simplex, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate, PetscInt localizationHeight, PetscBool sparseLocalize, DM *dm)
1540d71ae5a4SJacob Faibussowitsch {
15419318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
1542fdbf62faSLisandro Dalcin   PetscReal      low[3] = {0, 0, 0};
1543fdbf62faSLisandro Dalcin   PetscReal      upp[3] = {1, 1, 1};
1544fdbf62faSLisandro Dalcin   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
1545552f7358SJed Brown 
1546768d5fceSMatthew G. Knepley   PetscFunctionBegin;
15479566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
15489566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
15495dca41c3SJed Brown   PetscCall(DMPlexCreateBoxMesh_Internal(*dm, DM_SHAPE_BOX, dim, simplex, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt, interpolate));
155042108689Sksagiyam   if (periodicity) {
155142108689Sksagiyam     DM cdm;
155242108689Sksagiyam 
155342108689Sksagiyam     PetscCall(DMGetCoordinateDM(*dm, &cdm));
155442108689Sksagiyam     PetscCall(DMPlexSetMaxProjectionHeight(cdm, localizationHeight));
155542108689Sksagiyam     PetscCall(DMSetSparseLocalize(*dm, sparseLocalize));
155642108689Sksagiyam     PetscCall(DMLocalizeCoordinates(*dm));
155742108689Sksagiyam   }
15583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
15599318fe57SMatthew G. Knepley }
1560fdbf62faSLisandro Dalcin 
1561d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateWedgeBoxMesh_Internal(DM dm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
1562d71ae5a4SJacob Faibussowitsch {
15639318fe57SMatthew G. Knepley   DM       bdm, vol;
15649318fe57SMatthew G. Knepley   PetscInt i;
15659318fe57SMatthew G. Knepley 
15669318fe57SMatthew G. Knepley   PetscFunctionBegin;
15671fcf445aSMatthew G. Knepley   // TODO Now we can support periodicity
156808401ef6SPierre Jolivet   for (i = 0; i < 3; ++i) PetscCheck(periodicity[i] == DM_BOUNDARY_NONE, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Periodicity not yet supported");
15699566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &bdm));
15709566063dSJacob Faibussowitsch   PetscCall(DMSetType(bdm, DMPLEX));
15719566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(bdm, 2));
157246139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, bdm, 0, 0, 0));
15739566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxMesh_Simplex_Internal(bdm, 2, faces, lower, upper, periodicity, PETSC_TRUE));
15741fcf445aSMatthew G. Knepley   PetscCall(DMPlexExtrude(bdm, faces[2], upper[2] - lower[2], PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, NULL, NULL, &vol));
157546139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, bdm, 0, 0, 0));
15769566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&bdm));
157769d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &vol));
15789318fe57SMatthew G. Knepley   if (lower[2] != 0.0) {
15799318fe57SMatthew G. Knepley     Vec          v;
15809318fe57SMatthew G. Knepley     PetscScalar *x;
15819318fe57SMatthew G. Knepley     PetscInt     cDim, n;
15829318fe57SMatthew G. Knepley 
15839566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &v));
15849566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(v, &cDim));
15859566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(v, &n));
15869566063dSJacob Faibussowitsch     PetscCall(VecGetArray(v, &x));
15879318fe57SMatthew G. Knepley     x += cDim;
15889318fe57SMatthew G. Knepley     for (i = 0; i < n; i += cDim) x[i] += lower[2];
15899566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(v, &x));
15909566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, v));
15919318fe57SMatthew G. Knepley   }
15923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1593552f7358SJed Brown }
1594552f7358SJed Brown 
159500dabe28SStefano Zampini /*@
159639f4f5dbSPierre Jolivet   DMPlexCreateWedgeBoxMesh - Creates a 3-D mesh tessellating the (x,y) plane and extruding in the third direction using wedge cells.
159700dabe28SStefano Zampini 
1598d083f849SBarry Smith   Collective
159900dabe28SStefano Zampini 
160000dabe28SStefano Zampini   Input Parameters:
1601a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
160220f4b53cSBarry Smith . faces       - Number of faces per dimension, or `NULL` for (1, 1, 1)
160320f4b53cSBarry Smith . lower       - The lower left corner, or `NULL` for (0, 0, 0)
160420f4b53cSBarry Smith . upper       - The upper right corner, or `NULL` for (1, 1, 1)
160520f4b53cSBarry Smith . periodicity - The boundary type for the X,Y,Z direction, or `NULL` for `DM_BOUNDARY_NONE`
1606a1cb98faSBarry Smith . orderHeight - If `PETSC_TRUE`, orders the extruded cells in the height first. Otherwise, orders the cell on the layers first
160700dabe28SStefano Zampini - interpolate - Flag to create intermediate mesh pieces (edges, faces)
160800dabe28SStefano Zampini 
160900dabe28SStefano Zampini   Output Parameter:
1610a1cb98faSBarry Smith . dm - The `DM` object
161100dabe28SStefano Zampini 
161200dabe28SStefano Zampini   Level: beginner
161300dabe28SStefano Zampini 
16141cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateHexCylinderMesh()`, `DMPlexCreateWedgeCylinderMesh()`, `DMExtrude()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
161500dabe28SStefano Zampini @*/
1616d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateWedgeBoxMesh(MPI_Comm comm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool orderHeight, PetscBool interpolate, DM *dm)
1617d71ae5a4SJacob Faibussowitsch {
16189318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
161900dabe28SStefano Zampini   PetscReal      low[3] = {0, 0, 0};
162000dabe28SStefano Zampini   PetscReal      upp[3] = {1, 1, 1};
162100dabe28SStefano Zampini   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
162200dabe28SStefano Zampini 
162300dabe28SStefano Zampini   PetscFunctionBegin;
16249566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
16259566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
16269566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateWedgeBoxMesh_Internal(*dm, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt));
1627d410b0cfSMatthew G. Knepley   if (!interpolate) {
1628d410b0cfSMatthew G. Knepley     DM udm;
162900dabe28SStefano Zampini 
16309566063dSJacob Faibussowitsch     PetscCall(DMPlexUninterpolate(*dm, &udm));
163169d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(*dm, &udm));
163200dabe28SStefano Zampini   }
16337ff04441SMatthew G. Knepley   if (periodicity) PetscCall(DMLocalizeCoordinates(*dm));
16343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
163500dabe28SStefano Zampini }
163600dabe28SStefano Zampini 
1637cfb853baSMatthew G. Knepley /*
1638cfb853baSMatthew 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.
1639cfb853baSMatthew G. Knepley 
1640cfb853baSMatthew G. Knepley   Input Parameters:
1641cfb853baSMatthew G. Knepley + len - The length of the tuple
1642cfb853baSMatthew G. Knepley . max - The maximum for each dimension, so values are in [0, max)
1643cfb853baSMatthew G. Knepley - tup - A tuple of length len+1: tup[len] > 0 indicates a stopping condition
1644cfb853baSMatthew G. Knepley 
1645cfb853baSMatthew G. Knepley   Output Parameter:
164620f4b53cSBarry Smith . tup - A tuple of `len` integers whose entries are at most `max`
1647cfb853baSMatthew G. Knepley 
1648cfb853baSMatthew G. Knepley   Level: developer
1649cfb853baSMatthew G. Knepley 
165020f4b53cSBarry Smith   Note:
165120f4b53cSBarry Smith   Ordering is lexicographic with lowest index as least significant in ordering.
165220f4b53cSBarry 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}.
165320f4b53cSBarry Smith 
1654cfb853baSMatthew G. Knepley .seealso: PetscDualSpaceTensorPointLexicographic_Internal(), PetscDualSpaceLatticePointLexicographic_Internal()
1655cfb853baSMatthew G. Knepley */
1656cfb853baSMatthew G. Knepley static PetscErrorCode DMPlexTensorPointLexicographic_Private(PetscInt len, const PetscInt max[], PetscInt tup[])
1657cfb853baSMatthew G. Knepley {
1658cfb853baSMatthew G. Knepley   PetscInt i;
1659cfb853baSMatthew G. Knepley 
1660cfb853baSMatthew G. Knepley   PetscFunctionBegin;
1661cfb853baSMatthew G. Knepley   for (i = 0; i < len; ++i) {
1662cfb853baSMatthew G. Knepley     if (tup[i] < max[i] - 1) {
1663cfb853baSMatthew G. Knepley       break;
1664cfb853baSMatthew G. Knepley     } else {
1665cfb853baSMatthew G. Knepley       tup[i] = 0;
1666cfb853baSMatthew G. Knepley     }
1667cfb853baSMatthew G. Knepley   }
1668cfb853baSMatthew G. Knepley   if (i == len) tup[i - 1] = max[i - 1];
1669cfb853baSMatthew G. Knepley   else ++tup[i];
16703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1671cfb853baSMatthew G. Knepley }
1672cfb853baSMatthew G. Knepley 
1673cfb853baSMatthew G. Knepley static PetscInt TupleToIndex_Private(PetscInt len, const PetscInt max[], const PetscInt tup[])
1674cfb853baSMatthew G. Knepley {
1675cfb853baSMatthew G. Knepley   PetscInt i, idx = tup[len - 1];
1676cfb853baSMatthew G. Knepley 
1677cfb853baSMatthew G. Knepley   for (i = len - 2; i >= 0; --i) {
1678cfb853baSMatthew G. Knepley     idx *= max[i];
1679cfb853baSMatthew G. Knepley     idx += tup[i];
1680cfb853baSMatthew G. Knepley   }
1681cfb853baSMatthew G. Knepley   return idx;
1682cfb853baSMatthew G. Knepley }
1683cfb853baSMatthew G. Knepley 
1684cfb853baSMatthew G. Knepley static PetscErrorCode DMPlexCreateHypercubicMesh_Internal(DM dm, PetscInt dim, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], const DMBoundaryType bd[])
1685cfb853baSMatthew G. Knepley {
1686cfb853baSMatthew G. Knepley   Vec          coordinates;
1687cfb853baSMatthew G. Knepley   PetscSection coordSection;
1688cfb853baSMatthew G. Knepley   DMLabel      cutLabel    = NULL;
1689cfb853baSMatthew G. Knepley   PetscBool    cutMarker   = PETSC_FALSE;
1690cfb853baSMatthew G. Knepley   PetscBool    periodic    = PETSC_FALSE;
1691cfb853baSMatthew G. Knepley   PetscInt     numCells    = 1, c;
1692cfb853baSMatthew G. Knepley   PetscInt     numVertices = 1, v;
1693cfb853baSMatthew G. Knepley   PetscScalar *coords;
1694cfb853baSMatthew G. Knepley   PetscInt    *vertices, *vert, *vtmp, *supp, cone[2];
1695cfb853baSMatthew G. Knepley   PetscInt     d, e, cell = 0, coordSize;
1696cfb853baSMatthew G. Knepley   PetscMPIInt  rank;
1697cfb853baSMatthew G. Knepley 
1698cfb853baSMatthew G. Knepley   PetscFunctionBegin;
1699cfb853baSMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
1700cfb853baSMatthew G. Knepley   PetscCall(DMSetDimension(dm, dim));
1701cfb853baSMatthew G. Knepley   PetscCall(PetscCalloc4(dim, &vertices, dim, &vert, dim, &vtmp, 2 * dim, &supp));
1702cfb853baSMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "marker"));
1703cfb853baSMatthew G. Knepley   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL));
1704cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) periodic = (periodic || bd[d] == DM_BOUNDARY_PERIODIC) ? PETSC_TRUE : PETSC_FALSE;
1705cfb853baSMatthew G. Knepley   if (periodic && cutMarker) {
1706cfb853baSMatthew G. Knepley     PetscCall(DMCreateLabel(dm, "periodic_cut"));
1707cfb853baSMatthew G. Knepley     PetscCall(DMGetLabel(dm, "periodic_cut", &cutLabel));
1708cfb853baSMatthew G. Knepley   }
1709cfb853baSMatthew 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");
1710cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) {
1711cfb853baSMatthew G. Knepley     vertices[d] = edges[d];
1712cfb853baSMatthew G. Knepley     numVertices *= vertices[d];
1713cfb853baSMatthew G. Knepley   }
1714cfb853baSMatthew G. Knepley   numCells = numVertices * dim;
1715cfb853baSMatthew G. Knepley   PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
1716cfb853baSMatthew G. Knepley   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, 2));
1717cfb853baSMatthew G. Knepley   for (v = numCells; v < numCells + numVertices; ++v) PetscCall(DMPlexSetSupportSize(dm, v, 2 * dim));
1718cfb853baSMatthew G. Knepley   /* TODO Loop over boundary and reset support sizes */
1719cfb853baSMatthew G. Knepley   PetscCall(DMSetUp(dm)); /* Allocate space for cones and supports */
1720cfb853baSMatthew G. Knepley   /* Build cell cones and vertex supports */
1721cfb853baSMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "celltype"));
1722cfb853baSMatthew G. Knepley   while (vert[dim - 1] < vertices[dim - 1]) {
1723cfb853baSMatthew G. Knepley     const PetscInt vertex = TupleToIndex_Private(dim, vertices, vert) + numCells;
1724cfb853baSMatthew G. Knepley     PetscInt       s      = 0;
1725cfb853baSMatthew G. Knepley 
17263ba16761SJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "Vertex %" PetscInt_FMT ":", vertex));
17273ba16761SJacob Faibussowitsch     for (d = 0; d < dim; ++d) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, vert[d]));
17283ba16761SJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
1729cfb853baSMatthew G. Knepley     PetscCall(DMPlexSetCellType(dm, vertex, DM_POLYTOPE_POINT));
1730cfb853baSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
1731cfb853baSMatthew G. Knepley       for (e = 0; e < dim; ++e) vtmp[e] = vert[e];
1732cfb853baSMatthew G. Knepley       vtmp[d] = (vert[d] + 1) % vertices[d];
1733cfb853baSMatthew G. Knepley       cone[0] = vertex;
1734cfb853baSMatthew G. Knepley       cone[1] = TupleToIndex_Private(dim, vertices, vtmp) + numCells;
17353ba16761SJacob Faibussowitsch       PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Vertex %" PetscInt_FMT ":", cone[1]));
17363ba16761SJacob Faibussowitsch       for (e = 0; e < dim; ++e) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, vtmp[e]));
17373ba16761SJacob Faibussowitsch       PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
1738cfb853baSMatthew G. Knepley       PetscCall(DMPlexSetCone(dm, cell, cone));
1739cfb853baSMatthew G. Knepley       PetscCall(DMPlexSetCellType(dm, cell, DM_POLYTOPE_SEGMENT));
17403ba16761SJacob Faibussowitsch       PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Edge %" PetscInt_FMT " (%" PetscInt_FMT " %" PetscInt_FMT ")\n", cell, cone[0], cone[1]));
1741cfb853baSMatthew G. Knepley       ++cell;
1742cfb853baSMatthew G. Knepley     }
1743cfb853baSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
1744cfb853baSMatthew G. Knepley       for (e = 0; e < dim; ++e) vtmp[e] = vert[e];
1745cfb853baSMatthew G. Knepley       vtmp[d]   = (vert[d] + vertices[d] - 1) % vertices[d];
1746cfb853baSMatthew G. Knepley       supp[s++] = TupleToIndex_Private(dim, vertices, vtmp) * dim + d;
1747cfb853baSMatthew G. Knepley       supp[s++] = (vertex - numCells) * dim + d;
1748cfb853baSMatthew G. Knepley       PetscCall(DMPlexSetSupport(dm, vertex, supp));
1749cfb853baSMatthew G. Knepley     }
1750cfb853baSMatthew G. Knepley     PetscCall(DMPlexTensorPointLexicographic_Private(dim, vertices, vert));
1751cfb853baSMatthew G. Knepley   }
1752cfb853baSMatthew G. Knepley   PetscCall(DMPlexStratify(dm));
1753cfb853baSMatthew G. Knepley   /* Build coordinates */
1754cfb853baSMatthew G. Knepley   PetscCall(DMGetCoordinateSection(dm, &coordSection));
1755cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetNumFields(coordSection, 1));
1756cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
1757cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices));
1758cfb853baSMatthew G. Knepley   for (v = numCells; v < numCells + numVertices; ++v) {
1759cfb853baSMatthew G. Knepley     PetscCall(PetscSectionSetDof(coordSection, v, dim));
1760cfb853baSMatthew G. Knepley     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
1761cfb853baSMatthew G. Knepley   }
1762cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetUp(coordSection));
1763cfb853baSMatthew G. Knepley   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
1764cfb853baSMatthew G. Knepley   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
1765cfb853baSMatthew G. Knepley   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
1766cfb853baSMatthew G. Knepley   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
1767cfb853baSMatthew G. Knepley   PetscCall(VecSetBlockSize(coordinates, dim));
1768cfb853baSMatthew G. Knepley   PetscCall(VecSetType(coordinates, VECSTANDARD));
1769cfb853baSMatthew G. Knepley   PetscCall(VecGetArray(coordinates, &coords));
1770cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) vert[d] = 0;
1771cfb853baSMatthew G. Knepley   while (vert[dim - 1] < vertices[dim - 1]) {
1772cfb853baSMatthew G. Knepley     const PetscInt vertex = TupleToIndex_Private(dim, vertices, vert);
1773cfb853baSMatthew G. Knepley 
1774cfb853baSMatthew G. Knepley     for (d = 0; d < dim; ++d) coords[vertex * dim + d] = lower[d] + ((upper[d] - lower[d]) / vertices[d]) * vert[d];
17753ba16761SJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "Vertex %" PetscInt_FMT ":", vertex));
17763ba16761SJacob Faibussowitsch     for (d = 0; d < dim; ++d) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, vert[d]));
17773ba16761SJacob Faibussowitsch     for (d = 0; d < dim; ++d) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %g", (double)PetscRealPart(coords[vertex * dim + d])));
17783ba16761SJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
1779cfb853baSMatthew G. Knepley     PetscCall(DMPlexTensorPointLexicographic_Private(dim, vertices, vert));
1780cfb853baSMatthew G. Knepley   }
1781cfb853baSMatthew G. Knepley   PetscCall(VecRestoreArray(coordinates, &coords));
1782cfb853baSMatthew G. Knepley   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
1783cfb853baSMatthew G. Knepley   PetscCall(VecDestroy(&coordinates));
1784cfb853baSMatthew G. Knepley   PetscCall(PetscFree4(vertices, vert, vtmp, supp));
1785cfb853baSMatthew G. Knepley   //PetscCall(DMSetPeriodicity(dm, NULL, lower, upper));
1786cfb853baSMatthew G. Knepley   // Attach the extent
1787cfb853baSMatthew G. Knepley   {
1788cfb853baSMatthew G. Knepley     PetscContainer c;
1789cfb853baSMatthew G. Knepley     PetscInt      *extent;
1790cfb853baSMatthew G. Knepley 
1791cfb853baSMatthew G. Knepley     PetscCall(PetscMalloc1(dim, &extent));
1792cfb853baSMatthew G. Knepley     for (PetscInt d = 0; d < dim; ++d) extent[d] = edges[d];
1793cfb853baSMatthew G. Knepley     PetscCall(PetscContainerCreate(PETSC_COMM_SELF, &c));
179449abdd8aSBarry Smith     PetscCall(PetscContainerSetCtxDestroy(c, PetscCtxDestroyDefault));
1795cfb853baSMatthew G. Knepley     PetscCall(PetscContainerSetPointer(c, extent));
1796cfb853baSMatthew G. Knepley     PetscCall(PetscObjectCompose((PetscObject)dm, "_extent", (PetscObject)c));
1797cfb853baSMatthew G. Knepley     PetscCall(PetscContainerDestroy(&c));
1798cfb853baSMatthew G. Knepley   }
17993ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1800cfb853baSMatthew G. Knepley }
1801cfb853baSMatthew G. Knepley 
1802cfb853baSMatthew G. Knepley /*@C
1803aaa8cc7dSPierre Jolivet   DMPlexCreateHypercubicMesh - Creates a periodic mesh on the tensor product of unit intervals using only vertices and edges.
1804cfb853baSMatthew G. Knepley 
1805cfb853baSMatthew G. Knepley   Collective
1806cfb853baSMatthew G. Knepley 
1807cfb853baSMatthew G. Knepley   Input Parameters:
1808cfb853baSMatthew G. Knepley + comm  - The communicator for the DM object
1809cfb853baSMatthew G. Knepley . dim   - The spatial dimension
181020f4b53cSBarry Smith . edges - Number of edges per dimension, or `NULL` for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
181120f4b53cSBarry Smith . lower - The lower left corner, or `NULL` for (0, 0, 0)
181220f4b53cSBarry Smith - upper - The upper right corner, or `NULL` for (1, 1, 1)
1813cfb853baSMatthew G. Knepley 
1814cfb853baSMatthew G. Knepley   Output Parameter:
1815cfb853baSMatthew G. Knepley . dm - The DM object
1816cfb853baSMatthew G. Knepley 
181720f4b53cSBarry Smith   Level: beginner
181820f4b53cSBarry Smith 
181920f4b53cSBarry Smith   Note:
182020f4b53cSBarry Smith   If you want to customize this mesh using options, you just need to
182120f4b53cSBarry Smith .vb
182220f4b53cSBarry Smith   DMCreate(comm, &dm);
182320f4b53cSBarry Smith   DMSetType(dm, DMPLEX);
182420f4b53cSBarry Smith   DMSetFromOptions(dm);
182520f4b53cSBarry Smith .ve
182620f4b53cSBarry Smith   and use the options on the `DMSetFromOptions()` page.
1827cfb853baSMatthew G. Knepley 
1828cfb853baSMatthew G. Knepley   The vertices are numbered is lexicographic order, and the dim edges exiting a vertex in the positive orthant are number consecutively,
182920f4b53cSBarry Smith .vb
183020f4b53cSBarry Smith  18--0-19--2-20--4-18
183120f4b53cSBarry Smith   |     |     |     |
183220f4b53cSBarry Smith  13    15    17    13
183320f4b53cSBarry Smith   |     |     |     |
183420f4b53cSBarry Smith  24-12-25-14-26-16-24
183520f4b53cSBarry Smith   |     |     |     |
183620f4b53cSBarry Smith   7     9    11     7
183720f4b53cSBarry Smith   |     |     |     |
183820f4b53cSBarry Smith  21--6-22--8-23-10-21
183920f4b53cSBarry Smith   |     |     |     |
184020f4b53cSBarry Smith   1     3     5     1
184120f4b53cSBarry Smith   |     |     |     |
184220f4b53cSBarry Smith  18--0-19--2-20--4-18
184320f4b53cSBarry Smith .ve
1844cfb853baSMatthew G. Knepley 
184576fbde31SPierre Jolivet .seealso: `DMSetFromOptions()`, `DMPlexCreateFromFile()`, `DMPlexCreateHexCylinderMesh()`, `DMSetType()`, `DMCreate()`
1846cfb853baSMatthew G. Knepley @*/
1847cfb853baSMatthew G. Knepley PetscErrorCode DMPlexCreateHypercubicMesh(MPI_Comm comm, PetscInt dim, const PetscInt edges[], const PetscReal lower[], const PetscReal upper[], DM *dm)
1848cfb853baSMatthew G. Knepley {
1849cfb853baSMatthew G. Knepley   PetscInt       *edg;
1850cfb853baSMatthew G. Knepley   PetscReal      *low, *upp;
1851cfb853baSMatthew G. Knepley   DMBoundaryType *bdt;
1852cfb853baSMatthew G. Knepley   PetscInt        d;
1853cfb853baSMatthew G. Knepley 
1854cfb853baSMatthew G. Knepley   PetscFunctionBegin;
1855cfb853baSMatthew G. Knepley   PetscCall(DMCreate(comm, dm));
1856cfb853baSMatthew G. Knepley   PetscCall(DMSetType(*dm, DMPLEX));
1857cfb853baSMatthew G. Knepley   PetscCall(PetscMalloc4(dim, &edg, dim, &low, dim, &upp, dim, &bdt));
1858cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) {
1859cfb853baSMatthew G. Knepley     edg[d] = edges ? edges[d] : 1;
1860cfb853baSMatthew G. Knepley     low[d] = lower ? lower[d] : 0.;
1861cfb853baSMatthew G. Knepley     upp[d] = upper ? upper[d] : 1.;
1862cfb853baSMatthew G. Knepley     bdt[d] = DM_BOUNDARY_PERIODIC;
1863cfb853baSMatthew G. Knepley   }
1864cfb853baSMatthew G. Knepley   PetscCall(DMPlexCreateHypercubicMesh_Internal(*dm, dim, low, upp, edg, bdt));
1865cfb853baSMatthew G. Knepley   PetscCall(PetscFree4(edg, low, upp, bdt));
18663ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1867cfb853baSMatthew G. Knepley }
1868cfb853baSMatthew G. Knepley 
1869cc4c1da9SBarry Smith /*@
1870a1cb98faSBarry Smith   DMPlexSetOptionsPrefix - Sets the prefix used for searching for all `DM` options in the database.
1871a9074c1eSMatthew G. Knepley 
187220f4b53cSBarry Smith   Logically Collective
1873a9074c1eSMatthew G. Knepley 
1874a9074c1eSMatthew G. Knepley   Input Parameters:
187520f4b53cSBarry Smith + dm     - the `DM` context
1876a9074c1eSMatthew G. Knepley - prefix - the prefix to prepend to all option names
1877a9074c1eSMatthew G. Knepley 
1878a1cb98faSBarry Smith   Level: advanced
1879a1cb98faSBarry Smith 
1880a1cb98faSBarry Smith   Note:
1881a9074c1eSMatthew G. Knepley   A hyphen (-) must NOT be given at the beginning of the prefix name.
1882a9074c1eSMatthew G. Knepley   The first character of all runtime options is AUTOMATICALLY the hyphen.
1883a9074c1eSMatthew G. Knepley 
18841cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `SNESSetFromOptions()`
1885a9074c1eSMatthew G. Knepley @*/
1886d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetOptionsPrefix(DM dm, const char prefix[])
1887d71ae5a4SJacob Faibussowitsch {
1888a9074c1eSMatthew G. Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
1889a9074c1eSMatthew G. Knepley 
1890a9074c1eSMatthew G. Knepley   PetscFunctionBegin;
1891a9074c1eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18929566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, prefix));
18939566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)mesh->partitioner, prefix));
18943ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1895a9074c1eSMatthew G. Knepley }
1896a9074c1eSMatthew G. Knepley 
18979318fe57SMatthew G. Knepley /* Remap geometry to cylinder
189861a622f3SMatthew G. Knepley    TODO: This only works for a single refinement, then it is broken
189961a622f3SMatthew G. Knepley 
19009318fe57SMatthew G. Knepley      Interior square: Linear interpolation is correct
19019318fe57SMatthew G. Knepley      The other cells all have vertices on rays from the origin. We want to uniformly expand the spacing
19029318fe57SMatthew G. Knepley      such that the last vertex is on the unit circle. So the closest and farthest vertices are at distance
19030510c589SMatthew G. Knepley 
19049318fe57SMatthew G. Knepley        phi     = arctan(y/x)
19059318fe57SMatthew G. Knepley        d_close = sqrt(1/8 + 1/4 sin^2(phi))
19069318fe57SMatthew G. Knepley        d_far   = sqrt(1/2 + sin^2(phi))
19070510c589SMatthew G. Knepley 
19089318fe57SMatthew G. Knepley      so we remap them using
19090510c589SMatthew G. Knepley 
19109318fe57SMatthew G. Knepley        x_new = x_close + (x - x_close) (1 - d_close) / (d_far - d_close)
19119318fe57SMatthew G. Knepley        y_new = y_close + (y - y_close) (1 - d_close) / (d_far - d_close)
19120510c589SMatthew G. Knepley 
19139318fe57SMatthew G. Knepley      If pi/4 < phi < 3pi/4 or -3pi/4 < phi < -pi/4, then we switch x and y.
19149318fe57SMatthew G. Knepley */
1915d71ae5a4SJacob 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[])
1916d71ae5a4SJacob Faibussowitsch {
19179318fe57SMatthew G. Knepley   const PetscReal dis = 1.0 / PetscSqrtReal(2.0);
19189318fe57SMatthew G. Knepley   const PetscReal ds2 = 0.5 * dis;
191922cc497dSMatthew G. Knepley 
19209318fe57SMatthew G. Knepley   if ((PetscAbsScalar(u[0]) <= ds2) && (PetscAbsScalar(u[1]) <= ds2)) {
19219318fe57SMatthew G. Knepley     f0[0] = u[0];
19229318fe57SMatthew G. Knepley     f0[1] = u[1];
19239318fe57SMatthew G. Knepley   } else {
19249318fe57SMatthew G. Knepley     PetscReal phi, sinp, cosp, dc, df, x, y, xc, yc;
19250510c589SMatthew G. Knepley 
19269318fe57SMatthew G. Knepley     x    = PetscRealPart(u[0]);
19279318fe57SMatthew G. Knepley     y    = PetscRealPart(u[1]);
19289318fe57SMatthew G. Knepley     phi  = PetscAtan2Real(y, x);
19299318fe57SMatthew G. Knepley     sinp = PetscSinReal(phi);
19309318fe57SMatthew G. Knepley     cosp = PetscCosReal(phi);
19319318fe57SMatthew G. Knepley     if ((PetscAbsReal(phi) > PETSC_PI / 4.0) && (PetscAbsReal(phi) < 3.0 * PETSC_PI / 4.0)) {
19329318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2 / sinp);
19339318fe57SMatthew G. Knepley       df = PetscAbsReal(dis / sinp);
19349318fe57SMatthew G. Knepley       xc = ds2 * x / PetscAbsReal(y);
19359318fe57SMatthew G. Knepley       yc = ds2 * PetscSignReal(y);
19369318fe57SMatthew G. Knepley     } else {
19379318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2 / cosp);
19389318fe57SMatthew G. Knepley       df = PetscAbsReal(dis / cosp);
19399318fe57SMatthew G. Knepley       xc = ds2 * PetscSignReal(x);
19409318fe57SMatthew G. Knepley       yc = ds2 * y / PetscAbsReal(x);
19419318fe57SMatthew G. Knepley     }
19429318fe57SMatthew G. Knepley     f0[0] = xc + (u[0] - xc) * (1.0 - dc) / (df - dc);
19439318fe57SMatthew G. Knepley     f0[1] = yc + (u[1] - yc) * (1.0 - dc) / (df - dc);
19449318fe57SMatthew G. Knepley   }
19459318fe57SMatthew G. Knepley   f0[2] = u[2];
19469318fe57SMatthew G. Knepley }
19470510c589SMatthew G. Knepley 
194849704ca5SMatthew G. Knepley static PetscErrorCode DMPlexCreateHexCylinderMesh_Internal(DM dm, DMBoundaryType periodicZ, PetscInt Nr)
1949d71ae5a4SJacob Faibussowitsch {
19500510c589SMatthew G. Knepley   const PetscInt dim = 3;
19519318fe57SMatthew G. Knepley   PetscInt       numCells, numVertices;
1952d8c47e87SMatthew G. Knepley   PetscMPIInt    rank;
19530510c589SMatthew G. Knepley 
19540510c589SMatthew G. Knepley   PetscFunctionBegin;
195546139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
19569566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
19579566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
19580510c589SMatthew G. Knepley   /* Create topology */
19590510c589SMatthew G. Knepley   {
19600510c589SMatthew G. Knepley     PetscInt cone[8], c;
19610510c589SMatthew G. Knepley 
1962dd400576SPatrick Sanan     numCells    = rank == 0 ? 5 : 0;
1963dd400576SPatrick Sanan     numVertices = rank == 0 ? 16 : 0;
1964006a8963SMatthew G. Knepley     if (periodicZ == DM_BOUNDARY_PERIODIC) {
1965ae8bcbbbSMatthew G. Knepley       numCells *= 3;
1966dd400576SPatrick Sanan       numVertices = rank == 0 ? 24 : 0;
1967006a8963SMatthew G. Knepley     }
19689566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
19699566063dSJacob Faibussowitsch     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 8));
19709566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm));
1971dd400576SPatrick Sanan     if (rank == 0) {
1972006a8963SMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
19739371c9d4SSatish Balay         cone[0] = 15;
19749371c9d4SSatish Balay         cone[1] = 18;
19759371c9d4SSatish Balay         cone[2] = 17;
19769371c9d4SSatish Balay         cone[3] = 16;
19779371c9d4SSatish Balay         cone[4] = 31;
19789371c9d4SSatish Balay         cone[5] = 32;
19799371c9d4SSatish Balay         cone[6] = 33;
19809371c9d4SSatish Balay         cone[7] = 34;
19819566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
19829371c9d4SSatish Balay         cone[0] = 16;
19839371c9d4SSatish Balay         cone[1] = 17;
19849371c9d4SSatish Balay         cone[2] = 24;
19859371c9d4SSatish Balay         cone[3] = 23;
19869371c9d4SSatish Balay         cone[4] = 32;
19879371c9d4SSatish Balay         cone[5] = 36;
19889371c9d4SSatish Balay         cone[6] = 37;
19899371c9d4SSatish Balay         cone[7] = 33; /* 22 25 26 21 */
19909566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
19919371c9d4SSatish Balay         cone[0] = 18;
19929371c9d4SSatish Balay         cone[1] = 27;
19939371c9d4SSatish Balay         cone[2] = 24;
19949371c9d4SSatish Balay         cone[3] = 17;
19959371c9d4SSatish Balay         cone[4] = 34;
19969371c9d4SSatish Balay         cone[5] = 33;
19979371c9d4SSatish Balay         cone[6] = 37;
19989371c9d4SSatish Balay         cone[7] = 38;
19999566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
20009371c9d4SSatish Balay         cone[0] = 29;
20019371c9d4SSatish Balay         cone[1] = 27;
20029371c9d4SSatish Balay         cone[2] = 18;
20039371c9d4SSatish Balay         cone[3] = 15;
20049371c9d4SSatish Balay         cone[4] = 35;
20059371c9d4SSatish Balay         cone[5] = 31;
20069371c9d4SSatish Balay         cone[6] = 34;
20079371c9d4SSatish Balay         cone[7] = 38;
20089566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
20099371c9d4SSatish Balay         cone[0] = 29;
20109371c9d4SSatish Balay         cone[1] = 15;
20119371c9d4SSatish Balay         cone[2] = 16;
20129371c9d4SSatish Balay         cone[3] = 23;
20139371c9d4SSatish Balay         cone[4] = 35;
20149371c9d4SSatish Balay         cone[5] = 36;
20159371c9d4SSatish Balay         cone[6] = 32;
20169371c9d4SSatish Balay         cone[7] = 31;
20179566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
2018006a8963SMatthew G. Knepley 
20199371c9d4SSatish Balay         cone[0] = 31;
20209371c9d4SSatish Balay         cone[1] = 34;
20219371c9d4SSatish Balay         cone[2] = 33;
20229371c9d4SSatish Balay         cone[3] = 32;
20239371c9d4SSatish Balay         cone[4] = 19;
20249371c9d4SSatish Balay         cone[5] = 22;
20259371c9d4SSatish Balay         cone[6] = 21;
20269371c9d4SSatish Balay         cone[7] = 20;
20279566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 5, cone));
20289371c9d4SSatish Balay         cone[0] = 32;
20299371c9d4SSatish Balay         cone[1] = 33;
20309371c9d4SSatish Balay         cone[2] = 37;
20319371c9d4SSatish Balay         cone[3] = 36;
20329371c9d4SSatish Balay         cone[4] = 22;
20339371c9d4SSatish Balay         cone[5] = 25;
20349371c9d4SSatish Balay         cone[6] = 26;
20359371c9d4SSatish Balay         cone[7] = 21;
20369566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 6, cone));
20379371c9d4SSatish Balay         cone[0] = 34;
20389371c9d4SSatish Balay         cone[1] = 38;
20399371c9d4SSatish Balay         cone[2] = 37;
20409371c9d4SSatish Balay         cone[3] = 33;
20419371c9d4SSatish Balay         cone[4] = 20;
20429371c9d4SSatish Balay         cone[5] = 21;
20439371c9d4SSatish Balay         cone[6] = 26;
20449371c9d4SSatish Balay         cone[7] = 28;
20459566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 7, cone));
20469371c9d4SSatish Balay         cone[0] = 35;
20479371c9d4SSatish Balay         cone[1] = 38;
20489371c9d4SSatish Balay         cone[2] = 34;
20499371c9d4SSatish Balay         cone[3] = 31;
20509371c9d4SSatish Balay         cone[4] = 30;
20519371c9d4SSatish Balay         cone[5] = 19;
20529371c9d4SSatish Balay         cone[6] = 20;
20539371c9d4SSatish Balay         cone[7] = 28;
20549566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 8, cone));
20559371c9d4SSatish Balay         cone[0] = 35;
20569371c9d4SSatish Balay         cone[1] = 31;
20579371c9d4SSatish Balay         cone[2] = 32;
20589371c9d4SSatish Balay         cone[3] = 36;
20599371c9d4SSatish Balay         cone[4] = 30;
20609371c9d4SSatish Balay         cone[5] = 25;
20619371c9d4SSatish Balay         cone[6] = 22;
20629371c9d4SSatish Balay         cone[7] = 19;
20639566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 9, cone));
2064ae8bcbbbSMatthew G. Knepley 
20659371c9d4SSatish Balay         cone[0] = 19;
20669371c9d4SSatish Balay         cone[1] = 20;
20679371c9d4SSatish Balay         cone[2] = 21;
20689371c9d4SSatish Balay         cone[3] = 22;
20699371c9d4SSatish Balay         cone[4] = 15;
20709371c9d4SSatish Balay         cone[5] = 16;
20719371c9d4SSatish Balay         cone[6] = 17;
20729371c9d4SSatish Balay         cone[7] = 18;
20739566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 10, cone));
20749371c9d4SSatish Balay         cone[0] = 22;
20759371c9d4SSatish Balay         cone[1] = 21;
20769371c9d4SSatish Balay         cone[2] = 26;
20779371c9d4SSatish Balay         cone[3] = 25;
20789371c9d4SSatish Balay         cone[4] = 16;
20799371c9d4SSatish Balay         cone[5] = 23;
20809371c9d4SSatish Balay         cone[6] = 24;
20819371c9d4SSatish Balay         cone[7] = 17;
20829566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 11, cone));
20839371c9d4SSatish Balay         cone[0] = 20;
20849371c9d4SSatish Balay         cone[1] = 28;
20859371c9d4SSatish Balay         cone[2] = 26;
20869371c9d4SSatish Balay         cone[3] = 21;
20879371c9d4SSatish Balay         cone[4] = 18;
20889371c9d4SSatish Balay         cone[5] = 17;
20899371c9d4SSatish Balay         cone[6] = 24;
20909371c9d4SSatish Balay         cone[7] = 27;
20919566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 12, cone));
20929371c9d4SSatish Balay         cone[0] = 30;
20939371c9d4SSatish Balay         cone[1] = 28;
20949371c9d4SSatish Balay         cone[2] = 20;
20959371c9d4SSatish Balay         cone[3] = 19;
20969371c9d4SSatish Balay         cone[4] = 29;
20979371c9d4SSatish Balay         cone[5] = 15;
20989371c9d4SSatish Balay         cone[6] = 18;
20999371c9d4SSatish Balay         cone[7] = 27;
21009566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 13, cone));
21019371c9d4SSatish Balay         cone[0] = 30;
21029371c9d4SSatish Balay         cone[1] = 19;
21039371c9d4SSatish Balay         cone[2] = 22;
21049371c9d4SSatish Balay         cone[3] = 25;
21059371c9d4SSatish Balay         cone[4] = 29;
21069371c9d4SSatish Balay         cone[5] = 23;
21079371c9d4SSatish Balay         cone[6] = 16;
21089371c9d4SSatish Balay         cone[7] = 15;
21099566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 14, cone));
2110006a8963SMatthew G. Knepley       } else {
21119371c9d4SSatish Balay         cone[0] = 5;
21129371c9d4SSatish Balay         cone[1] = 8;
21139371c9d4SSatish Balay         cone[2] = 7;
21149371c9d4SSatish Balay         cone[3] = 6;
21159371c9d4SSatish Balay         cone[4] = 9;
21169371c9d4SSatish Balay         cone[5] = 12;
21179371c9d4SSatish Balay         cone[6] = 11;
21189371c9d4SSatish Balay         cone[7] = 10;
21199566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
21209371c9d4SSatish Balay         cone[0] = 6;
21219371c9d4SSatish Balay         cone[1] = 7;
21229371c9d4SSatish Balay         cone[2] = 14;
21239371c9d4SSatish Balay         cone[3] = 13;
21249371c9d4SSatish Balay         cone[4] = 12;
21259371c9d4SSatish Balay         cone[5] = 15;
21269371c9d4SSatish Balay         cone[6] = 16;
21279371c9d4SSatish Balay         cone[7] = 11;
21289566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
21299371c9d4SSatish Balay         cone[0] = 8;
21309371c9d4SSatish Balay         cone[1] = 17;
21319371c9d4SSatish Balay         cone[2] = 14;
21329371c9d4SSatish Balay         cone[3] = 7;
21339371c9d4SSatish Balay         cone[4] = 10;
21349371c9d4SSatish Balay         cone[5] = 11;
21359371c9d4SSatish Balay         cone[6] = 16;
21369371c9d4SSatish Balay         cone[7] = 18;
21379566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
21389371c9d4SSatish Balay         cone[0] = 19;
21399371c9d4SSatish Balay         cone[1] = 17;
21409371c9d4SSatish Balay         cone[2] = 8;
21419371c9d4SSatish Balay         cone[3] = 5;
21429371c9d4SSatish Balay         cone[4] = 20;
21439371c9d4SSatish Balay         cone[5] = 9;
21449371c9d4SSatish Balay         cone[6] = 10;
21459371c9d4SSatish Balay         cone[7] = 18;
21469566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
21479371c9d4SSatish Balay         cone[0] = 19;
21489371c9d4SSatish Balay         cone[1] = 5;
21499371c9d4SSatish Balay         cone[2] = 6;
21509371c9d4SSatish Balay         cone[3] = 13;
21519371c9d4SSatish Balay         cone[4] = 20;
21529371c9d4SSatish Balay         cone[5] = 15;
21539371c9d4SSatish Balay         cone[6] = 12;
21549371c9d4SSatish Balay         cone[7] = 9;
21559566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
2156006a8963SMatthew G. Knepley       }
2157d8c47e87SMatthew G. Knepley     }
21589566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
21599566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
21600510c589SMatthew G. Knepley   }
2161dbc1dc17SMatthew G. Knepley   /* Create cube geometry */
21620510c589SMatthew G. Knepley   {
21630510c589SMatthew G. Knepley     Vec             coordinates;
21640510c589SMatthew G. Knepley     PetscSection    coordSection;
21650510c589SMatthew G. Knepley     PetscScalar    *coords;
21660510c589SMatthew G. Knepley     PetscInt        coordSize, v;
21670510c589SMatthew G. Knepley     const PetscReal dis = 1.0 / PetscSqrtReal(2.0);
21680510c589SMatthew G. Knepley     const PetscReal ds2 = dis / 2.0;
21690510c589SMatthew G. Knepley 
21700510c589SMatthew G. Knepley     /* Build coordinates */
21719566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
21729566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
21739566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
21749566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices));
21750510c589SMatthew G. Knepley     for (v = numCells; v < numCells + numVertices; ++v) {
21769566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
21779566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
21780510c589SMatthew G. Knepley     }
21799566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
21809566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
21819566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
21829566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
21839566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
21849566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
21859566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates, VECSTANDARD));
21869566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
2187dd400576SPatrick Sanan     if (rank == 0) {
21889371c9d4SSatish Balay       coords[0 * dim + 0]  = -ds2;
21899371c9d4SSatish Balay       coords[0 * dim + 1]  = -ds2;
21909371c9d4SSatish Balay       coords[0 * dim + 2]  = 0.0;
21919371c9d4SSatish Balay       coords[1 * dim + 0]  = ds2;
21929371c9d4SSatish Balay       coords[1 * dim + 1]  = -ds2;
21939371c9d4SSatish Balay       coords[1 * dim + 2]  = 0.0;
21949371c9d4SSatish Balay       coords[2 * dim + 0]  = ds2;
21959371c9d4SSatish Balay       coords[2 * dim + 1]  = ds2;
21969371c9d4SSatish Balay       coords[2 * dim + 2]  = 0.0;
21979371c9d4SSatish Balay       coords[3 * dim + 0]  = -ds2;
21989371c9d4SSatish Balay       coords[3 * dim + 1]  = ds2;
21999371c9d4SSatish Balay       coords[3 * dim + 2]  = 0.0;
22009371c9d4SSatish Balay       coords[4 * dim + 0]  = -ds2;
22019371c9d4SSatish Balay       coords[4 * dim + 1]  = -ds2;
22029371c9d4SSatish Balay       coords[4 * dim + 2]  = 1.0;
22039371c9d4SSatish Balay       coords[5 * dim + 0]  = -ds2;
22049371c9d4SSatish Balay       coords[5 * dim + 1]  = ds2;
22059371c9d4SSatish Balay       coords[5 * dim + 2]  = 1.0;
22069371c9d4SSatish Balay       coords[6 * dim + 0]  = ds2;
22079371c9d4SSatish Balay       coords[6 * dim + 1]  = ds2;
22089371c9d4SSatish Balay       coords[6 * dim + 2]  = 1.0;
22099371c9d4SSatish Balay       coords[7 * dim + 0]  = ds2;
22109371c9d4SSatish Balay       coords[7 * dim + 1]  = -ds2;
22119371c9d4SSatish Balay       coords[7 * dim + 2]  = 1.0;
22129371c9d4SSatish Balay       coords[8 * dim + 0]  = dis;
22139371c9d4SSatish Balay       coords[8 * dim + 1]  = -dis;
22149371c9d4SSatish Balay       coords[8 * dim + 2]  = 0.0;
22159371c9d4SSatish Balay       coords[9 * dim + 0]  = dis;
22169371c9d4SSatish Balay       coords[9 * dim + 1]  = dis;
22179371c9d4SSatish Balay       coords[9 * dim + 2]  = 0.0;
22189371c9d4SSatish Balay       coords[10 * dim + 0] = dis;
22199371c9d4SSatish Balay       coords[10 * dim + 1] = -dis;
22209371c9d4SSatish Balay       coords[10 * dim + 2] = 1.0;
22219371c9d4SSatish Balay       coords[11 * dim + 0] = dis;
22229371c9d4SSatish Balay       coords[11 * dim + 1] = dis;
22239371c9d4SSatish Balay       coords[11 * dim + 2] = 1.0;
22249371c9d4SSatish Balay       coords[12 * dim + 0] = -dis;
22259371c9d4SSatish Balay       coords[12 * dim + 1] = dis;
22269371c9d4SSatish Balay       coords[12 * dim + 2] = 0.0;
22279371c9d4SSatish Balay       coords[13 * dim + 0] = -dis;
22289371c9d4SSatish Balay       coords[13 * dim + 1] = dis;
22299371c9d4SSatish Balay       coords[13 * dim + 2] = 1.0;
22309371c9d4SSatish Balay       coords[14 * dim + 0] = -dis;
22319371c9d4SSatish Balay       coords[14 * dim + 1] = -dis;
22329371c9d4SSatish Balay       coords[14 * dim + 2] = 0.0;
22339371c9d4SSatish Balay       coords[15 * dim + 0] = -dis;
22349371c9d4SSatish Balay       coords[15 * dim + 1] = -dis;
22359371c9d4SSatish Balay       coords[15 * dim + 2] = 1.0;
2236ae8bcbbbSMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
22379371c9d4SSatish Balay         /* 15 31 19 */ coords[16 * dim + 0] = -ds2;
22389371c9d4SSatish Balay         coords[16 * dim + 1]                = -ds2;
22399371c9d4SSatish Balay         coords[16 * dim + 2]                = 0.5;
22409371c9d4SSatish Balay         /* 16 32 22 */ coords[17 * dim + 0] = ds2;
22419371c9d4SSatish Balay         coords[17 * dim + 1]                = -ds2;
22429371c9d4SSatish Balay         coords[17 * dim + 2]                = 0.5;
22439371c9d4SSatish Balay         /* 17 33 21 */ coords[18 * dim + 0] = ds2;
22449371c9d4SSatish Balay         coords[18 * dim + 1]                = ds2;
22459371c9d4SSatish Balay         coords[18 * dim + 2]                = 0.5;
22469371c9d4SSatish Balay         /* 18 34 20 */ coords[19 * dim + 0] = -ds2;
22479371c9d4SSatish Balay         coords[19 * dim + 1]                = ds2;
22489371c9d4SSatish Balay         coords[19 * dim + 2]                = 0.5;
22499371c9d4SSatish Balay         /* 29 35 30 */ coords[20 * dim + 0] = -dis;
22509371c9d4SSatish Balay         coords[20 * dim + 1]                = -dis;
22519371c9d4SSatish Balay         coords[20 * dim + 2]                = 0.5;
22529371c9d4SSatish Balay         /* 23 36 25 */ coords[21 * dim + 0] = dis;
22539371c9d4SSatish Balay         coords[21 * dim + 1]                = -dis;
22549371c9d4SSatish Balay         coords[21 * dim + 2]                = 0.5;
22559371c9d4SSatish Balay         /* 24 37 26 */ coords[22 * dim + 0] = dis;
22569371c9d4SSatish Balay         coords[22 * dim + 1]                = dis;
22579371c9d4SSatish Balay         coords[22 * dim + 2]                = 0.5;
22589371c9d4SSatish Balay         /* 27 38 28 */ coords[23 * dim + 0] = -dis;
22599371c9d4SSatish Balay         coords[23 * dim + 1]                = dis;
22609371c9d4SSatish Balay         coords[23 * dim + 2]                = 0.5;
2261ae8bcbbbSMatthew G. Knepley       }
2262d8c47e87SMatthew G. Knepley     }
22639566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
22649566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
22659566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
22660510c589SMatthew G. Knepley   }
2267006a8963SMatthew G. Knepley   /* Create periodicity */
2268006a8963SMatthew G. Knepley   if (periodicZ == DM_BOUNDARY_PERIODIC || periodicZ == DM_BOUNDARY_TWIST) {
22696858538eSMatthew G. Knepley     PetscReal L[3]       = {-1., -1., 0.};
22706858538eSMatthew G. Knepley     PetscReal maxCell[3] = {-1., -1., 0.};
2271006a8963SMatthew G. Knepley     PetscReal lower[3]   = {0.0, 0.0, 0.0};
2272ae8bcbbbSMatthew G. Knepley     PetscReal upper[3]   = {1.0, 1.0, 1.5};
22736858538eSMatthew G. Knepley     PetscInt  numZCells  = 3;
2274006a8963SMatthew G. Knepley 
22756858538eSMatthew G. Knepley     L[2]       = upper[2] - lower[2];
22766858538eSMatthew G. Knepley     maxCell[2] = 1.1 * (L[2] / numZCells);
22774fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, maxCell, lower, L));
2278006a8963SMatthew G. Knepley   }
2279dbc1dc17SMatthew G. Knepley   {
22809318fe57SMatthew G. Knepley     DM          cdm;
22819318fe57SMatthew G. Knepley     PetscDS     cds;
22829318fe57SMatthew G. Knepley     PetscScalar c[2] = {1.0, 1.0};
2283dbc1dc17SMatthew G. Knepley 
228449704ca5SMatthew G. Knepley     PetscCall(DMPlexCreateCoordinateSpace(dm, 1, PETSC_TRUE, NULL));
22859566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
22869566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
22879566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, 2, c));
2288dbc1dc17SMatthew G. Knepley   }
228946139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
229046139095SJed Brown 
22919318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
22929566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolateInPlace_Internal(dm));
229349704ca5SMatthew G. Knepley 
229449704ca5SMatthew G. Knepley   char        oldprefix[PETSC_MAX_PATH_LEN];
229549704ca5SMatthew G. Knepley   const char *prefix;
229649704ca5SMatthew G. Knepley 
229749704ca5SMatthew G. Knepley   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, &prefix));
229849704ca5SMatthew G. Knepley   PetscCall(PetscStrncpy(oldprefix, prefix, PETSC_MAX_PATH_LEN));
229949704ca5SMatthew G. Knepley   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, "petsc_cyl_ref_"));
230049704ca5SMatthew G. Knepley   for (PetscInt r = 0; r < PetscMax(0, Nr); ++r) {
230149704ca5SMatthew G. Knepley     DM rdm;
230249704ca5SMatthew G. Knepley 
230349704ca5SMatthew G. Knepley     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &rdm));
230449704ca5SMatthew G. Knepley     PetscCall(DMPlexReplace_Internal(dm, &rdm));
230549704ca5SMatthew G. Knepley   }
230649704ca5SMatthew G. Knepley   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, oldprefix));
230749704ca5SMatthew G. Knepley   PetscCall(DMPlexRemapGeometry(dm, 0.0, snapToCylinder));
230849704ca5SMatthew G. Knepley 
230949704ca5SMatthew G. Knepley   DMLabel         bdlabel, edgelabel;
231049704ca5SMatthew G. Knepley   IS              faceIS;
231149704ca5SMatthew G. Knepley   const PetscInt *faces;
231249704ca5SMatthew G. Knepley   PetscInt        Nf;
231349704ca5SMatthew G. Knepley 
231449704ca5SMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "marker"));
231549704ca5SMatthew G. Knepley   PetscCall(DMGetLabel(dm, "marker", &bdlabel));
231649704ca5SMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "generatrix"));
231749704ca5SMatthew G. Knepley   PetscCall(DMGetLabel(dm, "generatrix", &edgelabel));
231849704ca5SMatthew G. Knepley   PetscCall(DMPlexMarkBoundaryFaces(dm, PETSC_DETERMINE, bdlabel));
231949704ca5SMatthew G. Knepley   // Remove faces on top and bottom
232049704ca5SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(bdlabel, 1, &faceIS));
2321ba1b3593SJeremy L Thompson   if (faceIS) {
232249704ca5SMatthew G. Knepley     PetscCall(ISGetLocalSize(faceIS, &Nf));
232349704ca5SMatthew G. Knepley     PetscCall(ISGetIndices(faceIS, &faces));
232449704ca5SMatthew G. Knepley     for (PetscInt f = 0; f < Nf; ++f) {
232549704ca5SMatthew G. Knepley       PetscReal vol, normal[3];
232649704ca5SMatthew G. Knepley 
232749704ca5SMatthew G. Knepley       PetscCall(DMPlexComputeCellGeometryFVM(dm, faces[f], &vol, NULL, normal));
232849704ca5SMatthew G. Knepley       if (PetscAbsReal(normal[2]) < PETSC_SMALL) PetscCall(DMLabelSetValue(edgelabel, faces[f], 1));
232949704ca5SMatthew G. Knepley     }
233049704ca5SMatthew G. Knepley     PetscCall(ISRestoreIndices(faceIS, &faces));
233149704ca5SMatthew G. Knepley     PetscCall(ISDestroy(&faceIS));
2332ba1b3593SJeremy L Thompson   }
233349704ca5SMatthew G. Knepley   PetscCall(DMPlexLabelComplete(dm, bdlabel));
233449704ca5SMatthew G. Knepley   PetscCall(DMPlexLabelComplete(dm, edgelabel));
23353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
23360510c589SMatthew G. Knepley }
23370510c589SMatthew G. Knepley 
233824119c2aSMatthew G. Knepley /*@
23399318fe57SMatthew G. Knepley   DMPlexCreateHexCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using hexahedra.
234024119c2aSMatthew G. Knepley 
2341d083f849SBarry Smith   Collective
234224119c2aSMatthew G. Knepley 
234324119c2aSMatthew G. Knepley   Input Parameters:
2344a1cb98faSBarry Smith + comm      - The communicator for the `DM` object
234549704ca5SMatthew G. Knepley . periodicZ - The boundary type for the Z direction
234649704ca5SMatthew G. Knepley - Nr        - The number of refinements to carry out
234724119c2aSMatthew G. Knepley 
234824119c2aSMatthew G. Knepley   Output Parameter:
234920f4b53cSBarry Smith . dm - The `DM` object
235024119c2aSMatthew G. Knepley 
235124119c2aSMatthew G. Knepley   Level: beginner
235224119c2aSMatthew G. Knepley 
2353a1cb98faSBarry Smith   Note:
2354a4e35b19SJacob Faibussowitsch   Here is the output numbering looking from the bottom of the cylinder\:
2355a1cb98faSBarry Smith .vb
2356a1cb98faSBarry Smith        17-----14
2357a1cb98faSBarry Smith         |     |
2358a1cb98faSBarry Smith         |  2  |
2359a1cb98faSBarry Smith         |     |
2360a1cb98faSBarry Smith  17-----8-----7-----14
2361a1cb98faSBarry Smith   |     |     |     |
2362a1cb98faSBarry Smith   |  3  |  0  |  1  |
2363a1cb98faSBarry Smith   |     |     |     |
2364a1cb98faSBarry Smith  19-----5-----6-----13
2365a1cb98faSBarry Smith         |     |
2366a1cb98faSBarry Smith         |  4  |
2367a1cb98faSBarry Smith         |     |
2368a1cb98faSBarry Smith        19-----13
2369a1cb98faSBarry Smith 
2370a1cb98faSBarry Smith  and up through the top
2371a1cb98faSBarry Smith 
2372a1cb98faSBarry Smith        18-----16
2373a1cb98faSBarry Smith         |     |
2374a1cb98faSBarry Smith         |  2  |
2375a1cb98faSBarry Smith         |     |
2376a1cb98faSBarry Smith  18----10----11-----16
2377a1cb98faSBarry Smith   |     |     |     |
2378a1cb98faSBarry Smith   |  3  |  0  |  1  |
2379a1cb98faSBarry Smith   |     |     |     |
2380a1cb98faSBarry Smith  20-----9----12-----15
2381a1cb98faSBarry Smith         |     |
2382a1cb98faSBarry Smith         |  4  |
2383a1cb98faSBarry Smith         |     |
2384a1cb98faSBarry Smith        20-----15
2385a1cb98faSBarry Smith .ve
2386a1cb98faSBarry Smith 
23871cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
238824119c2aSMatthew G. Knepley @*/
238949704ca5SMatthew G. Knepley PetscErrorCode DMPlexCreateHexCylinderMesh(MPI_Comm comm, DMBoundaryType periodicZ, PetscInt Nr, DM *dm)
2390d71ae5a4SJacob Faibussowitsch {
23919318fe57SMatthew G. Knepley   PetscFunctionBegin;
239249704ca5SMatthew G. Knepley   PetscAssertPointer(dm, 4);
23939566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
23949566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
239549704ca5SMatthew G. Knepley   PetscCall(DMPlexCreateHexCylinderMesh_Internal(*dm, periodicZ, Nr));
23963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
23979318fe57SMatthew G. Knepley }
23989318fe57SMatthew G. Knepley 
2399d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateWedgeCylinderMesh_Internal(DM dm, PetscInt n, PetscBool interpolate)
2400d71ae5a4SJacob Faibussowitsch {
240124119c2aSMatthew G. Knepley   const PetscInt dim = 3;
2402412e9a14SMatthew G. Knepley   PetscInt       numCells, numVertices, v;
24039fe9f049SMatthew G. Knepley   PetscMPIInt    rank;
240424119c2aSMatthew G. Knepley 
240524119c2aSMatthew G. Knepley   PetscFunctionBegin;
240663a3b9bcSJacob Faibussowitsch   PetscCheck(n >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of wedges %" PetscInt_FMT " cannot be negative", n);
240746139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
24089566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
24099566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
2410412e9a14SMatthew G. Knepley   /* Must create the celltype label here so that we do not automatically try to compute the types */
24119566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "celltype"));
241224119c2aSMatthew G. Knepley   /* Create topology */
241324119c2aSMatthew G. Knepley   {
241424119c2aSMatthew G. Knepley     PetscInt cone[6], c;
241524119c2aSMatthew G. Knepley 
2416dd400576SPatrick Sanan     numCells    = rank == 0 ? n : 0;
2417dd400576SPatrick Sanan     numVertices = rank == 0 ? 2 * (n + 1) : 0;
24189566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
24199566063dSJacob Faibussowitsch     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 6));
24209566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm));
242124119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
24229371c9d4SSatish Balay       cone[0] = c + n * 1;
24239371c9d4SSatish Balay       cone[1] = (c + 1) % n + n * 1;
24249371c9d4SSatish Balay       cone[2] = 0 + 3 * n;
24259371c9d4SSatish Balay       cone[3] = c + n * 2;
24269371c9d4SSatish Balay       cone[4] = (c + 1) % n + n * 2;
24279371c9d4SSatish Balay       cone[5] = 1 + 3 * n;
24289566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(dm, c, cone));
24299566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(dm, c, DM_POLYTOPE_TRI_PRISM_TENSOR));
243024119c2aSMatthew G. Knepley     }
24319566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
24329566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
243324119c2aSMatthew G. Knepley   }
243448a46eb9SPierre Jolivet   for (v = numCells; v < numCells + numVertices; ++v) PetscCall(DMPlexSetCellType(dm, v, DM_POLYTOPE_POINT));
243524119c2aSMatthew G. Knepley   /* Create cylinder geometry */
243624119c2aSMatthew G. Knepley   {
243724119c2aSMatthew G. Knepley     Vec          coordinates;
243824119c2aSMatthew G. Knepley     PetscSection coordSection;
243924119c2aSMatthew G. Knepley     PetscScalar *coords;
2440412e9a14SMatthew G. Knepley     PetscInt     coordSize, c;
244124119c2aSMatthew G. Knepley 
244224119c2aSMatthew G. Knepley     /* Build coordinates */
24439566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
24449566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
24459566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
24469566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices));
244724119c2aSMatthew G. Knepley     for (v = numCells; v < numCells + numVertices; ++v) {
24489566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
24499566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
245024119c2aSMatthew G. Knepley     }
24519566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
24529566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
24539566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
24549566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
24559566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
24569566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
24579566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates, VECSTANDARD));
24589566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
245924119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
24609371c9d4SSatish Balay       coords[(c + 0 * n) * dim + 0] = PetscCosReal(2.0 * c * PETSC_PI / n);
24619371c9d4SSatish Balay       coords[(c + 0 * n) * dim + 1] = PetscSinReal(2.0 * c * PETSC_PI / n);
24629371c9d4SSatish Balay       coords[(c + 0 * n) * dim + 2] = 1.0;
24639371c9d4SSatish Balay       coords[(c + 1 * n) * dim + 0] = PetscCosReal(2.0 * c * PETSC_PI / n);
24649371c9d4SSatish Balay       coords[(c + 1 * n) * dim + 1] = PetscSinReal(2.0 * c * PETSC_PI / n);
24659371c9d4SSatish Balay       coords[(c + 1 * n) * dim + 2] = 0.0;
246624119c2aSMatthew G. Knepley     }
2467dd400576SPatrick Sanan     if (rank == 0) {
24689371c9d4SSatish Balay       coords[(2 * n + 0) * dim + 0] = 0.0;
24699371c9d4SSatish Balay       coords[(2 * n + 0) * dim + 1] = 0.0;
24709371c9d4SSatish Balay       coords[(2 * n + 0) * dim + 2] = 1.0;
24719371c9d4SSatish Balay       coords[(2 * n + 1) * dim + 0] = 0.0;
24729371c9d4SSatish Balay       coords[(2 * n + 1) * dim + 1] = 0.0;
24739371c9d4SSatish Balay       coords[(2 * n + 1) * dim + 2] = 0.0;
24749fe9f049SMatthew G. Knepley     }
24759566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
24769566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
24779566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
247824119c2aSMatthew G. Knepley   }
247946139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
24809318fe57SMatthew G. Knepley   /* Interpolate */
24819566063dSJacob Faibussowitsch   if (interpolate) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
24823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24839318fe57SMatthew G. Knepley }
24849318fe57SMatthew G. Knepley 
24859318fe57SMatthew G. Knepley /*@
24869318fe57SMatthew G. Knepley   DMPlexCreateWedgeCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using wedges.
24879318fe57SMatthew G. Knepley 
24889318fe57SMatthew G. Knepley   Collective
24899318fe57SMatthew G. Knepley 
24909318fe57SMatthew G. Knepley   Input Parameters:
2491a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
24929318fe57SMatthew G. Knepley . n           - The number of wedges around the origin
24939318fe57SMatthew G. Knepley - interpolate - Create edges and faces
24949318fe57SMatthew G. Knepley 
24959318fe57SMatthew G. Knepley   Output Parameter:
2496a1cb98faSBarry Smith . dm - The `DM` object
24979318fe57SMatthew G. Knepley 
24989318fe57SMatthew G. Knepley   Level: beginner
24999318fe57SMatthew G. Knepley 
25001cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateHexCylinderMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
25019318fe57SMatthew G. Knepley @*/
2502d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateWedgeCylinderMesh(MPI_Comm comm, PetscInt n, PetscBool interpolate, DM *dm)
2503d71ae5a4SJacob Faibussowitsch {
25049318fe57SMatthew G. Knepley   PetscFunctionBegin;
25054f572ea9SToby Isaac   PetscAssertPointer(dm, 4);
25069566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
25079566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
25089566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateWedgeCylinderMesh_Internal(*dm, n, interpolate));
25093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
251024119c2aSMatthew G. Knepley }
251124119c2aSMatthew G. Knepley 
2512d71ae5a4SJacob Faibussowitsch static inline PetscReal DiffNormReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
2513d71ae5a4SJacob Faibussowitsch {
251465a81367SMatthew G. Knepley   PetscReal prod = 0.0;
251565a81367SMatthew G. Knepley   PetscInt  i;
251665a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += PetscSqr(x[i] - y[i]);
251765a81367SMatthew G. Knepley   return PetscSqrtReal(prod);
251865a81367SMatthew G. Knepley }
2519dd2b43ebSStefano Zampini 
2520d71ae5a4SJacob Faibussowitsch static inline PetscReal DotReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
2521d71ae5a4SJacob Faibussowitsch {
252265a81367SMatthew G. Knepley   PetscReal prod = 0.0;
252365a81367SMatthew G. Knepley   PetscInt  i;
252465a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += x[i] * y[i];
252565a81367SMatthew G. Knepley   return prod;
252665a81367SMatthew G. Knepley }
252765a81367SMatthew G. Knepley 
252851a74b61SMatthew G. Knepley /* The first constant is the sphere radius */
2529d71ae5a4SJacob 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[])
2530d71ae5a4SJacob Faibussowitsch {
253151a74b61SMatthew G. Knepley   PetscReal r     = PetscRealPart(constants[0]);
253251a74b61SMatthew G. Knepley   PetscReal norm2 = 0.0, fac;
253351a74b61SMatthew G. Knepley   PetscInt  n     = uOff[1] - uOff[0], d;
253451a74b61SMatthew G. Knepley 
253551a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) norm2 += PetscSqr(PetscRealPart(u[d]));
253651a74b61SMatthew G. Knepley   fac = r / PetscSqrtReal(norm2);
253751a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) f0[d] = u[d] * fac;
253851a74b61SMatthew G. Knepley }
253951a74b61SMatthew G. Knepley 
2540d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSphereMesh_Internal(DM dm, PetscInt dim, PetscBool simplex, PetscReal R)
2541d71ae5a4SJacob Faibussowitsch {
254265a81367SMatthew G. Knepley   const PetscInt embedDim = dim + 1;
254365a81367SMatthew G. Knepley   PetscSection   coordSection;
254465a81367SMatthew G. Knepley   Vec            coordinates;
254565a81367SMatthew G. Knepley   PetscScalar   *coords;
254665a81367SMatthew G. Knepley   PetscReal     *coordsIn;
254707c565c5SJose E. Roman   PetscInt       numCells, numEdges, numVerts = 0, firstVertex = 0, v, firstEdge, coordSize, d, e;
254865a81367SMatthew G. Knepley   PetscMPIInt    rank;
254965a81367SMatthew G. Knepley 
255065a81367SMatthew G. Knepley   PetscFunctionBegin;
25519318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveBool(dm, simplex, 3);
255246139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
25539566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
25549566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, dim + 1));
25559566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
255665a81367SMatthew G. Knepley   switch (dim) {
25575c344501SMatthew G. Knepley   case 1:
25585c344501SMatthew G. Knepley     numCells = 16;
25595c344501SMatthew G. Knepley     numVerts = numCells;
25605c344501SMatthew G. Knepley 
25615c344501SMatthew G. Knepley     // Build Topology
25625c344501SMatthew G. Knepley     PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts));
25635c344501SMatthew G. Knepley     for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim));
25645c344501SMatthew G. Knepley     PetscCall(DMSetUp(dm));
25655c344501SMatthew G. Knepley     for (PetscInt c = 0; c < numCells; ++c) {
25665c344501SMatthew G. Knepley       PetscInt cone[2];
25675c344501SMatthew G. Knepley 
25685c344501SMatthew G. Knepley       cone[0] = c + numCells;
25695c344501SMatthew G. Knepley       cone[1] = (c + 1) % numVerts + numCells;
25705c344501SMatthew G. Knepley       PetscCall(DMPlexSetCone(dm, c, cone));
25715c344501SMatthew G. Knepley     }
25725c344501SMatthew G. Knepley     PetscCall(DMPlexSymmetrize(dm));
25735c344501SMatthew G. Knepley     PetscCall(DMPlexStratify(dm));
25745c344501SMatthew G. Knepley     PetscCall(PetscMalloc1(numVerts * embedDim, &coordsIn));
25755c344501SMatthew G. Knepley     for (PetscInt v = 0; v < numVerts; ++v) {
25765c344501SMatthew G. Knepley       const PetscReal rad = 2. * PETSC_PI * v / numVerts;
25775c344501SMatthew G. Knepley 
25785c344501SMatthew G. Knepley       coordsIn[v * embedDim + 0] = PetscCosReal(rad);
25795c344501SMatthew G. Knepley       coordsIn[v * embedDim + 1] = PetscSinReal(rad);
25805c344501SMatthew G. Knepley     }
25815c344501SMatthew G. Knepley     break;
258265a81367SMatthew G. Knepley   case 2:
258365a81367SMatthew G. Knepley     if (simplex) {
258451a74b61SMatthew G. Knepley       const PetscReal radius    = PetscSqrtReal(1 + PETSC_PHI * PETSC_PHI) / (1.0 + PETSC_PHI);
258551a74b61SMatthew G. Knepley       const PetscReal edgeLen   = 2.0 / (1.0 + PETSC_PHI) * (R / radius);
258665a81367SMatthew G. Knepley       const PetscInt  degree    = 5;
258751a74b61SMatthew G. Knepley       PetscReal       vertex[3] = {0.0, 1.0 / (1.0 + PETSC_PHI), PETSC_PHI / (1.0 + PETSC_PHI)};
258865a81367SMatthew G. Knepley       PetscInt        s[3]      = {1, 1, 1};
258965a81367SMatthew G. Knepley       PetscInt        cone[3];
259007c565c5SJose E. Roman       PetscInt       *graph;
259165a81367SMatthew G. Knepley 
25929371c9d4SSatish Balay       vertex[0] *= R / radius;
25939371c9d4SSatish Balay       vertex[1] *= R / radius;
25949371c9d4SSatish Balay       vertex[2] *= R / radius;
2595dd400576SPatrick Sanan       numCells    = rank == 0 ? 20 : 0;
2596dd400576SPatrick Sanan       numVerts    = rank == 0 ? 12 : 0;
259765a81367SMatthew G. Knepley       firstVertex = numCells;
259851a74b61SMatthew G. Knepley       /* Use icosahedron, which for a R-sphere has coordinates which are all cyclic permutations of
259965a81367SMatthew G. Knepley 
260065a81367SMatthew G. Knepley            (0, \pm 1/\phi+1, \pm \phi/\phi+1)
260165a81367SMatthew G. Knepley 
260265a81367SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
260351a74b61SMatthew G. Knepley          length is then given by 2/(1+\phi) = 2 * 0.38197 = 0.76393.
260465a81367SMatthew G. Knepley       */
260565a81367SMatthew G. Knepley       /* Construct vertices */
26069566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
2607dd400576SPatrick Sanan       if (rank == 0) {
260807c565c5SJose E. Roman         for (PetscInt p = 0, i = 0; p < embedDim; ++p) {
260965a81367SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
261065a81367SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
261165a81367SMatthew G. Knepley               for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[(d + p) % embedDim] * vertex[(d + p) % embedDim];
261265a81367SMatthew G. Knepley               ++i;
261365a81367SMatthew G. Knepley             }
261465a81367SMatthew G. Knepley           }
261565a81367SMatthew G. Knepley         }
261645da822fSValeria Barra       }
261765a81367SMatthew G. Knepley       /* Construct graph */
26189566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * numVerts, &graph));
261907c565c5SJose E. Roman       for (PetscInt i = 0; i < numVerts; ++i) {
262007c565c5SJose E. Roman         PetscInt k = 0;
262107c565c5SJose E. Roman         for (PetscInt j = 0; j < numVerts; ++j) {
26229371c9d4SSatish Balay           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i * embedDim], &coordsIn[j * embedDim]) - edgeLen) < PETSC_SMALL) {
26239371c9d4SSatish Balay             graph[i * numVerts + j] = 1;
26249371c9d4SSatish Balay             ++k;
26259371c9d4SSatish Balay           }
262665a81367SMatthew G. Knepley         }
262763a3b9bcSJacob Faibussowitsch         PetscCheck(k == degree, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid icosahedron, vertex %" PetscInt_FMT " degree %" PetscInt_FMT " != %" PetscInt_FMT, i, k, degree);
262865a81367SMatthew G. Knepley       }
262965a81367SMatthew G. Knepley       /* Build Topology */
26309566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts));
263107c565c5SJose E. Roman       for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim));
26329566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
263365a81367SMatthew G. Knepley       /* Cells */
263407c565c5SJose E. Roman       for (PetscInt i = 0, c = 0; i < numVerts; ++i) {
263507c565c5SJose E. Roman         for (PetscInt j = 0; j < i; ++j) {
263607c565c5SJose E. Roman           for (PetscInt k = 0; k < j; ++k) {
263765a81367SMatthew G. Knepley             if (graph[i * numVerts + j] && graph[j * numVerts + k] && graph[k * numVerts + i]) {
26389371c9d4SSatish Balay               cone[0] = firstVertex + i;
26399371c9d4SSatish Balay               cone[1] = firstVertex + j;
26409371c9d4SSatish Balay               cone[2] = firstVertex + k;
264165a81367SMatthew G. Knepley               /* Check orientation */
264265a81367SMatthew G. Knepley               {
26439371c9d4SSatish Balay                 const PetscInt epsilon[3][3][3] = {
26449371c9d4SSatish Balay                   {{0, 0, 0},  {0, 0, 1},  {0, -1, 0}},
26459371c9d4SSatish Balay                   {{0, 0, -1}, {0, 0, 0},  {1, 0, 0} },
26469371c9d4SSatish Balay                   {{0, 1, 0},  {-1, 0, 0}, {0, 0, 0} }
26479371c9d4SSatish Balay                 };
264865a81367SMatthew G. Knepley                 PetscReal normal[3];
264965a81367SMatthew G. Knepley                 PetscInt  e, f;
265065a81367SMatthew G. Knepley 
265165a81367SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) {
265265a81367SMatthew G. Knepley                   normal[d] = 0.0;
265365a81367SMatthew G. Knepley                   for (e = 0; e < embedDim; ++e) {
2654ad540459SPierre 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]);
265565a81367SMatthew G. Knepley                   }
265665a81367SMatthew G. Knepley                 }
26579371c9d4SSatish Balay                 if (DotReal(embedDim, normal, &coordsIn[i * embedDim]) < 0) {
26589371c9d4SSatish Balay                   PetscInt tmp = cone[1];
26599371c9d4SSatish Balay                   cone[1]      = cone[2];
26609371c9d4SSatish Balay                   cone[2]      = tmp;
266165a81367SMatthew G. Knepley                 }
266265a81367SMatthew G. Knepley               }
26639566063dSJacob Faibussowitsch               PetscCall(DMPlexSetCone(dm, c++, cone));
266465a81367SMatthew G. Knepley             }
266565a81367SMatthew G. Knepley           }
266665a81367SMatthew G. Knepley         }
266765a81367SMatthew G. Knepley       }
26689566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
26699566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
26709566063dSJacob Faibussowitsch       PetscCall(PetscFree(graph));
267165a81367SMatthew G. Knepley     } else {
26722829fed8SMatthew G. Knepley       /*
26732829fed8SMatthew G. Knepley         12-21--13
26742829fed8SMatthew G. Knepley          |     |
26752829fed8SMatthew G. Knepley         25  4  24
26762829fed8SMatthew G. Knepley          |     |
26772829fed8SMatthew G. Knepley   12-25--9-16--8-24--13
26782829fed8SMatthew G. Knepley    |     |     |     |
26792829fed8SMatthew G. Knepley   23  5 17  0 15  3  22
26802829fed8SMatthew G. Knepley    |     |     |     |
26812829fed8SMatthew G. Knepley   10-20--6-14--7-19--11
26822829fed8SMatthew G. Knepley          |     |
26832829fed8SMatthew G. Knepley         20  1  19
26842829fed8SMatthew G. Knepley          |     |
26852829fed8SMatthew G. Knepley         10-18--11
26862829fed8SMatthew G. Knepley          |     |
26872829fed8SMatthew G. Knepley         23  2  22
26882829fed8SMatthew G. Knepley          |     |
26892829fed8SMatthew G. Knepley         12-21--13
26902829fed8SMatthew G. Knepley        */
26912829fed8SMatthew G. Knepley       PetscInt cone[4], ornt[4];
26922829fed8SMatthew G. Knepley 
2693dd400576SPatrick Sanan       numCells    = rank == 0 ? 6 : 0;
2694dd400576SPatrick Sanan       numEdges    = rank == 0 ? 12 : 0;
2695dd400576SPatrick Sanan       numVerts    = rank == 0 ? 8 : 0;
269665a81367SMatthew G. Knepley       firstVertex = numCells;
269765a81367SMatthew G. Knepley       firstEdge   = numCells + numVerts;
26982829fed8SMatthew G. Knepley       /* Build Topology */
26999566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells + numEdges + numVerts));
270007c565c5SJose E. Roman       for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 4));
270148a46eb9SPierre Jolivet       for (e = firstEdge; e < firstEdge + numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2));
27029566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
2703dd400576SPatrick Sanan       if (rank == 0) {
27042829fed8SMatthew G. Knepley         /* Cell 0 */
27059371c9d4SSatish Balay         cone[0] = 14;
27069371c9d4SSatish Balay         cone[1] = 15;
27079371c9d4SSatish Balay         cone[2] = 16;
27089371c9d4SSatish Balay         cone[3] = 17;
27099566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
27109371c9d4SSatish Balay         ornt[0] = 0;
27119371c9d4SSatish Balay         ornt[1] = 0;
27129371c9d4SSatish Balay         ornt[2] = 0;
27139371c9d4SSatish Balay         ornt[3] = 0;
27149566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 0, ornt));
27152829fed8SMatthew G. Knepley         /* Cell 1 */
27169371c9d4SSatish Balay         cone[0] = 18;
27179371c9d4SSatish Balay         cone[1] = 19;
27189371c9d4SSatish Balay         cone[2] = 14;
27199371c9d4SSatish Balay         cone[3] = 20;
27209566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
27219371c9d4SSatish Balay         ornt[0] = 0;
27229371c9d4SSatish Balay         ornt[1] = 0;
27239371c9d4SSatish Balay         ornt[2] = -1;
27249371c9d4SSatish Balay         ornt[3] = 0;
27259566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 1, ornt));
27262829fed8SMatthew G. Knepley         /* Cell 2 */
27279371c9d4SSatish Balay         cone[0] = 21;
27289371c9d4SSatish Balay         cone[1] = 22;
27299371c9d4SSatish Balay         cone[2] = 18;
27309371c9d4SSatish Balay         cone[3] = 23;
27319566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
27329371c9d4SSatish Balay         ornt[0] = 0;
27339371c9d4SSatish Balay         ornt[1] = 0;
27349371c9d4SSatish Balay         ornt[2] = -1;
27359371c9d4SSatish Balay         ornt[3] = 0;
27369566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 2, ornt));
27372829fed8SMatthew G. Knepley         /* Cell 3 */
27389371c9d4SSatish Balay         cone[0] = 19;
27399371c9d4SSatish Balay         cone[1] = 22;
27409371c9d4SSatish Balay         cone[2] = 24;
27419371c9d4SSatish Balay         cone[3] = 15;
27429566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
27439371c9d4SSatish Balay         ornt[0] = -1;
27449371c9d4SSatish Balay         ornt[1] = -1;
27459371c9d4SSatish Balay         ornt[2] = 0;
27469371c9d4SSatish Balay         ornt[3] = -1;
27479566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 3, ornt));
27482829fed8SMatthew G. Knepley         /* Cell 4 */
27499371c9d4SSatish Balay         cone[0] = 16;
27509371c9d4SSatish Balay         cone[1] = 24;
27519371c9d4SSatish Balay         cone[2] = 21;
27529371c9d4SSatish Balay         cone[3] = 25;
27539566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
27549371c9d4SSatish Balay         ornt[0] = -1;
27559371c9d4SSatish Balay         ornt[1] = -1;
27569371c9d4SSatish Balay         ornt[2] = -1;
27579371c9d4SSatish Balay         ornt[3] = 0;
27589566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 4, ornt));
27592829fed8SMatthew G. Knepley         /* Cell 5 */
27609371c9d4SSatish Balay         cone[0] = 20;
27619371c9d4SSatish Balay         cone[1] = 17;
27629371c9d4SSatish Balay         cone[2] = 25;
27639371c9d4SSatish Balay         cone[3] = 23;
27649566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 5, cone));
27659371c9d4SSatish Balay         ornt[0] = -1;
27669371c9d4SSatish Balay         ornt[1] = -1;
27679371c9d4SSatish Balay         ornt[2] = -1;
27689371c9d4SSatish Balay         ornt[3] = -1;
27699566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 5, ornt));
27702829fed8SMatthew G. Knepley         /* Edges */
27719371c9d4SSatish Balay         cone[0] = 6;
27729371c9d4SSatish Balay         cone[1] = 7;
27739566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 14, cone));
27749371c9d4SSatish Balay         cone[0] = 7;
27759371c9d4SSatish Balay         cone[1] = 8;
27769566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 15, cone));
27779371c9d4SSatish Balay         cone[0] = 8;
27789371c9d4SSatish Balay         cone[1] = 9;
27799566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 16, cone));
27809371c9d4SSatish Balay         cone[0] = 9;
27819371c9d4SSatish Balay         cone[1] = 6;
27829566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 17, cone));
27839371c9d4SSatish Balay         cone[0] = 10;
27849371c9d4SSatish Balay         cone[1] = 11;
27859566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 18, cone));
27869371c9d4SSatish Balay         cone[0] = 11;
27879371c9d4SSatish Balay         cone[1] = 7;
27889566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 19, cone));
27899371c9d4SSatish Balay         cone[0] = 6;
27909371c9d4SSatish Balay         cone[1] = 10;
27919566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 20, cone));
27929371c9d4SSatish Balay         cone[0] = 12;
27939371c9d4SSatish Balay         cone[1] = 13;
27949566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 21, cone));
27959371c9d4SSatish Balay         cone[0] = 13;
27969371c9d4SSatish Balay         cone[1] = 11;
27979566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 22, cone));
27989371c9d4SSatish Balay         cone[0] = 10;
27999371c9d4SSatish Balay         cone[1] = 12;
28009566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 23, cone));
28019371c9d4SSatish Balay         cone[0] = 13;
28029371c9d4SSatish Balay         cone[1] = 8;
28039566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 24, cone));
28049371c9d4SSatish Balay         cone[0] = 12;
28059371c9d4SSatish Balay         cone[1] = 9;
28069566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 25, cone));
280745da822fSValeria Barra       }
28089566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
28099566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
28102829fed8SMatthew G. Knepley       /* Build coordinates */
28119566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
2812dd400576SPatrick Sanan       if (rank == 0) {
28139371c9d4SSatish Balay         coordsIn[0 * embedDim + 0] = -R;
28149371c9d4SSatish Balay         coordsIn[0 * embedDim + 1] = R;
28159371c9d4SSatish Balay         coordsIn[0 * embedDim + 2] = -R;
28169371c9d4SSatish Balay         coordsIn[1 * embedDim + 0] = R;
28179371c9d4SSatish Balay         coordsIn[1 * embedDim + 1] = R;
28189371c9d4SSatish Balay         coordsIn[1 * embedDim + 2] = -R;
28199371c9d4SSatish Balay         coordsIn[2 * embedDim + 0] = R;
28209371c9d4SSatish Balay         coordsIn[2 * embedDim + 1] = -R;
28219371c9d4SSatish Balay         coordsIn[2 * embedDim + 2] = -R;
28229371c9d4SSatish Balay         coordsIn[3 * embedDim + 0] = -R;
28239371c9d4SSatish Balay         coordsIn[3 * embedDim + 1] = -R;
28249371c9d4SSatish Balay         coordsIn[3 * embedDim + 2] = -R;
28259371c9d4SSatish Balay         coordsIn[4 * embedDim + 0] = -R;
28269371c9d4SSatish Balay         coordsIn[4 * embedDim + 1] = R;
28279371c9d4SSatish Balay         coordsIn[4 * embedDim + 2] = R;
28289371c9d4SSatish Balay         coordsIn[5 * embedDim + 0] = R;
28299371c9d4SSatish Balay         coordsIn[5 * embedDim + 1] = R;
28309371c9d4SSatish Balay         coordsIn[5 * embedDim + 2] = R;
28319371c9d4SSatish Balay         coordsIn[6 * embedDim + 0] = -R;
28329371c9d4SSatish Balay         coordsIn[6 * embedDim + 1] = -R;
28339371c9d4SSatish Balay         coordsIn[6 * embedDim + 2] = R;
28349371c9d4SSatish Balay         coordsIn[7 * embedDim + 0] = R;
28359371c9d4SSatish Balay         coordsIn[7 * embedDim + 1] = -R;
28369371c9d4SSatish Balay         coordsIn[7 * embedDim + 2] = R;
283765a81367SMatthew G. Knepley       }
283845da822fSValeria Barra     }
283965a81367SMatthew G. Knepley     break;
284065a81367SMatthew G. Knepley   case 3:
2841116ded15SMatthew G. Knepley     if (simplex) {
2842116ded15SMatthew G. Knepley       const PetscReal edgeLen         = 1.0 / PETSC_PHI;
284351a74b61SMatthew G. Knepley       PetscReal       vertexA[4]      = {0.5, 0.5, 0.5, 0.5};
284451a74b61SMatthew G. Knepley       PetscReal       vertexB[4]      = {1.0, 0.0, 0.0, 0.0};
284551a74b61SMatthew G. Knepley       PetscReal       vertexC[4]      = {0.5, 0.5 * PETSC_PHI, 0.5 / PETSC_PHI, 0.0};
2846116ded15SMatthew G. Knepley       const PetscInt  degree          = 12;
2847116ded15SMatthew G. Knepley       PetscInt        s[4]            = {1, 1, 1};
28489371c9d4SSatish Balay       PetscInt        evenPerm[12][4] = {
28499371c9d4SSatish Balay         {0, 1, 2, 3},
28509371c9d4SSatish Balay         {0, 2, 3, 1},
28519371c9d4SSatish Balay         {0, 3, 1, 2},
28529371c9d4SSatish Balay         {1, 0, 3, 2},
28539371c9d4SSatish Balay         {1, 2, 0, 3},
28549371c9d4SSatish Balay         {1, 3, 2, 0},
28559371c9d4SSatish Balay         {2, 0, 1, 3},
28569371c9d4SSatish Balay         {2, 1, 3, 0},
28579371c9d4SSatish Balay         {2, 3, 0, 1},
28589371c9d4SSatish Balay         {3, 0, 2, 1},
28599371c9d4SSatish Balay         {3, 1, 0, 2},
28609371c9d4SSatish Balay         {3, 2, 1, 0}
28619371c9d4SSatish Balay       };
2862116ded15SMatthew G. Knepley       PetscInt  cone[4];
2863116ded15SMatthew G. Knepley       PetscInt *graph, p, i, j, k, l;
2864116ded15SMatthew G. Knepley 
28659371c9d4SSatish Balay       vertexA[0] *= R;
28669371c9d4SSatish Balay       vertexA[1] *= R;
28679371c9d4SSatish Balay       vertexA[2] *= R;
28689371c9d4SSatish Balay       vertexA[3] *= R;
28699371c9d4SSatish Balay       vertexB[0] *= R;
28709371c9d4SSatish Balay       vertexB[1] *= R;
28719371c9d4SSatish Balay       vertexB[2] *= R;
28729371c9d4SSatish Balay       vertexB[3] *= R;
28739371c9d4SSatish Balay       vertexC[0] *= R;
28749371c9d4SSatish Balay       vertexC[1] *= R;
28759371c9d4SSatish Balay       vertexC[2] *= R;
28769371c9d4SSatish Balay       vertexC[3] *= R;
2877dd400576SPatrick Sanan       numCells    = rank == 0 ? 600 : 0;
2878dd400576SPatrick Sanan       numVerts    = rank == 0 ? 120 : 0;
2879116ded15SMatthew G. Knepley       firstVertex = numCells;
2880116ded15SMatthew G. Knepley       /* Use the 600-cell, which for a unit sphere has coordinates which are
2881116ded15SMatthew G. Knepley 
2882116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm 1,    \pm 1, \pm 1)                          16
2883116ded15SMatthew G. Knepley                (\pm 1,    0,       0,      0)  all cyclic permutations   8
2884116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm phi, \pm 1/phi, 0)  all even permutations    96
2885116ded15SMatthew G. Knepley 
2886116ded15SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
28876333ae4fSvaleriabarra          length is then given by 1/\phi = 0.61803.
2888116ded15SMatthew G. Knepley 
2889116ded15SMatthew G. Knepley          http://buzzard.pugetsound.edu/sage-practice/ch03s03.html
2890116ded15SMatthew G. Knepley          http://mathworld.wolfram.com/600-Cell.html
2891116ded15SMatthew G. Knepley       */
2892116ded15SMatthew G. Knepley       /* Construct vertices */
28939566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
2894116ded15SMatthew G. Knepley       i = 0;
2895dd400576SPatrick Sanan       if (rank == 0) {
2896116ded15SMatthew G. Knepley         for (s[0] = -1; s[0] < 2; s[0] += 2) {
2897116ded15SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
2898116ded15SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
2899116ded15SMatthew G. Knepley               for (s[3] = -1; s[3] < 2; s[3] += 2) {
2900116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[d] * vertexA[d];
2901116ded15SMatthew G. Knepley                 ++i;
2902116ded15SMatthew G. Knepley               }
2903116ded15SMatthew G. Knepley             }
2904116ded15SMatthew G. Knepley           }
2905116ded15SMatthew G. Knepley         }
2906116ded15SMatthew G. Knepley         for (p = 0; p < embedDim; ++p) {
2907116ded15SMatthew G. Knepley           s[1] = s[2] = s[3] = 1;
2908116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
2909116ded15SMatthew G. Knepley             for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[(d + p) % embedDim] * vertexB[(d + p) % embedDim];
2910116ded15SMatthew G. Knepley             ++i;
2911116ded15SMatthew G. Knepley           }
2912116ded15SMatthew G. Knepley         }
2913116ded15SMatthew G. Knepley         for (p = 0; p < 12; ++p) {
2914116ded15SMatthew G. Knepley           s[3] = 1;
2915116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
2916116ded15SMatthew G. Knepley             for (s[1] = -1; s[1] < 2; s[1] += 2) {
2917116ded15SMatthew G. Knepley               for (s[2] = -1; s[2] < 2; s[2] += 2) {
2918116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[evenPerm[p][d]] * vertexC[evenPerm[p][d]];
2919116ded15SMatthew G. Knepley                 ++i;
2920116ded15SMatthew G. Knepley               }
2921116ded15SMatthew G. Knepley             }
2922116ded15SMatthew G. Knepley           }
2923116ded15SMatthew G. Knepley         }
292445da822fSValeria Barra       }
292563a3b9bcSJacob Faibussowitsch       PetscCheck(i == numVerts, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertices %" PetscInt_FMT " != %" PetscInt_FMT, i, numVerts);
2926116ded15SMatthew G. Knepley       /* Construct graph */
29279566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * numVerts, &graph));
2928116ded15SMatthew G. Knepley       for (i = 0; i < numVerts; ++i) {
2929116ded15SMatthew G. Knepley         for (j = 0, k = 0; j < numVerts; ++j) {
29309371c9d4SSatish Balay           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i * embedDim], &coordsIn[j * embedDim]) - edgeLen) < PETSC_SMALL) {
29319371c9d4SSatish Balay             graph[i * numVerts + j] = 1;
29329371c9d4SSatish Balay             ++k;
29339371c9d4SSatish Balay           }
2934116ded15SMatthew G. Knepley         }
293563a3b9bcSJacob Faibussowitsch         PetscCheck(k == degree, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertex %" PetscInt_FMT " degree %" PetscInt_FMT " != %" PetscInt_FMT, i, k, degree);
2936116ded15SMatthew G. Knepley       }
2937116ded15SMatthew G. Knepley       /* Build Topology */
29389566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts));
293907c565c5SJose E. Roman       for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim));
29409566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
2941116ded15SMatthew G. Knepley       /* Cells */
2942dd400576SPatrick Sanan       if (rank == 0) {
294307c565c5SJose E. Roman         for (PetscInt i = 0, c = 0; i < numVerts; ++i) {
2944116ded15SMatthew G. Knepley           for (j = 0; j < i; ++j) {
2945116ded15SMatthew G. Knepley             for (k = 0; k < j; ++k) {
2946116ded15SMatthew G. Knepley               for (l = 0; l < k; ++l) {
29479371c9d4SSatish 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]) {
29489371c9d4SSatish Balay                   cone[0] = firstVertex + i;
29499371c9d4SSatish Balay                   cone[1] = firstVertex + j;
29509371c9d4SSatish Balay                   cone[2] = firstVertex + k;
29519371c9d4SSatish Balay                   cone[3] = firstVertex + l;
2952116ded15SMatthew G. Knepley                   /* Check orientation: https://ef.gy/linear-algebra:normal-vectors-in-higher-dimensional-spaces */
2953116ded15SMatthew G. Knepley                   {
29549371c9d4SSatish Balay                     const PetscInt epsilon[4][4][4][4] = {
29559371c9d4SSatish 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}}},
2956116ded15SMatthew G. Knepley 
29579371c9d4SSatish 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}}},
2958116ded15SMatthew G. Knepley 
29599371c9d4SSatish 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}}},
2960116ded15SMatthew G. Knepley 
29619371c9d4SSatish 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}} }
29629371c9d4SSatish Balay                     };
2963116ded15SMatthew G. Knepley                     PetscReal normal[4];
2964116ded15SMatthew G. Knepley                     PetscInt  e, f, g;
2965116ded15SMatthew G. Knepley 
2966116ded15SMatthew G. Knepley                     for (d = 0; d < embedDim; ++d) {
2967116ded15SMatthew G. Knepley                       normal[d] = 0.0;
2968116ded15SMatthew G. Knepley                       for (e = 0; e < embedDim; ++e) {
2969116ded15SMatthew G. Knepley                         for (f = 0; f < embedDim; ++f) {
2970116ded15SMatthew G. Knepley                           for (g = 0; g < embedDim; ++g) {
2971116ded15SMatthew 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]);
2972116ded15SMatthew G. Knepley                           }
2973116ded15SMatthew G. Knepley                         }
2974116ded15SMatthew G. Knepley                       }
2975116ded15SMatthew G. Knepley                     }
29769371c9d4SSatish Balay                     if (DotReal(embedDim, normal, &coordsIn[i * embedDim]) < 0) {
29779371c9d4SSatish Balay                       PetscInt tmp = cone[1];
29789371c9d4SSatish Balay                       cone[1]      = cone[2];
29799371c9d4SSatish Balay                       cone[2]      = tmp;
29809371c9d4SSatish Balay                     }
2981116ded15SMatthew G. Knepley                   }
29829566063dSJacob Faibussowitsch                   PetscCall(DMPlexSetCone(dm, c++, cone));
2983116ded15SMatthew G. Knepley                 }
2984116ded15SMatthew G. Knepley               }
2985116ded15SMatthew G. Knepley             }
2986116ded15SMatthew G. Knepley           }
2987116ded15SMatthew G. Knepley         }
298845da822fSValeria Barra       }
29899566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
29909566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
29919566063dSJacob Faibussowitsch       PetscCall(PetscFree(graph));
2992116ded15SMatthew G. Knepley     }
2993f4d061e9SPierre Jolivet     break;
2994d71ae5a4SJacob Faibussowitsch   default:
2995d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension for sphere: %" PetscInt_FMT, dim);
299665a81367SMatthew G. Knepley   }
299765a81367SMatthew G. Knepley   /* Create coordinates */
29989566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
29999566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
30009566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, embedDim));
30019566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numVerts));
30022829fed8SMatthew G. Knepley   for (v = firstVertex; v < firstVertex + numVerts; ++v) {
30039566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, embedDim));
30049566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, embedDim));
30052829fed8SMatthew G. Knepley   }
30069566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
30079566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
30089566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
30099566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, embedDim));
30109566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
30119566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
30129566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
30139566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
30149371c9d4SSatish Balay   for (v = 0; v < numVerts; ++v)
3015ad540459SPierre Jolivet     for (d = 0; d < embedDim; ++d) coords[v * embedDim + d] = coordsIn[v * embedDim + d];
30169566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
30179566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
30189566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
30199566063dSJacob Faibussowitsch   PetscCall(PetscFree(coordsIn));
302051a74b61SMatthew G. Knepley   {
302151a74b61SMatthew G. Knepley     DM          cdm;
302251a74b61SMatthew G. Knepley     PetscDS     cds;
30239318fe57SMatthew G. Knepley     PetscScalar c = R;
302451a74b61SMatthew G. Knepley 
3025e44f6aebSMatthew G. Knepley     PetscCall(DMPlexCreateCoordinateSpace(dm, 1, PETSC_TRUE, snapToSphere));
30269566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
30279566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
30289566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, 1, &c));
302951a74b61SMatthew G. Knepley   }
303046139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
30319318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
30329566063dSJacob Faibussowitsch   if (simplex) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
30333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
30349318fe57SMatthew G. Knepley }
30359318fe57SMatthew G. Knepley 
3036b7f5c055SJed Brown typedef void (*TPSEvaluateFunc)(const PetscReal[], PetscReal *, PetscReal[], PetscReal (*)[3]);
3037b7f5c055SJed Brown 
3038b7f5c055SJed Brown /*
3039b7f5c055SJed Brown  The Schwarz P implicit surface is
3040b7f5c055SJed Brown 
3041b7f5c055SJed Brown      f(x) = cos(x0) + cos(x1) + cos(x2) = 0
3042b7f5c055SJed Brown */
3043d71ae5a4SJacob Faibussowitsch static void TPSEvaluate_SchwarzP(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3])
3044d71ae5a4SJacob Faibussowitsch {
3045b7f5c055SJed Brown   PetscReal c[3] = {PetscCosReal(y[0] * PETSC_PI), PetscCosReal(y[1] * PETSC_PI), PetscCosReal(y[2] * PETSC_PI)};
3046b7f5c055SJed Brown   PetscReal g[3] = {-PetscSinReal(y[0] * PETSC_PI), -PetscSinReal(y[1] * PETSC_PI), -PetscSinReal(y[2] * PETSC_PI)};
3047b7f5c055SJed Brown   f[0]           = c[0] + c[1] + c[2];
3048b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) {
3049b7f5c055SJed Brown     grad[i] = PETSC_PI * g[i];
3050ad540459SPierre Jolivet     for (PetscInt j = 0; j < 3; j++) hess[i][j] = (i == j) ? -PetscSqr(PETSC_PI) * c[i] : 0.;
3051b7f5c055SJed Brown   }
3052b7f5c055SJed Brown }
3053b7f5c055SJed Brown 
30544663dae6SJed Brown // u[] is a tentative normal on input. Replace with the implicit function gradient in the same direction
3055d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSExtrudeNormalFunc_SchwarzP(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt r, PetscScalar u[], void *ctx)
3056d71ae5a4SJacob Faibussowitsch {
3057ad540459SPierre Jolivet   for (PetscInt i = 0; i < 3; i++) u[i] = -PETSC_PI * PetscSinReal(x[i] * PETSC_PI);
30583ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
30594663dae6SJed Brown }
30604663dae6SJed Brown 
3061b7f5c055SJed Brown /*
3062b7f5c055SJed Brown  The Gyroid implicit surface is
3063b7f5c055SJed Brown 
3064b7f5c055SJed 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)
3065b7f5c055SJed Brown 
3066b7f5c055SJed Brown */
3067d71ae5a4SJacob Faibussowitsch static void TPSEvaluate_Gyroid(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3])
3068d71ae5a4SJacob Faibussowitsch {
3069b7f5c055SJed Brown   PetscReal s[3] = {PetscSinReal(PETSC_PI * y[0]), PetscSinReal(PETSC_PI * (y[1] + .5)), PetscSinReal(PETSC_PI * (y[2] + .25))};
3070b7f5c055SJed Brown   PetscReal c[3] = {PetscCosReal(PETSC_PI * y[0]), PetscCosReal(PETSC_PI * (y[1] + .5)), PetscCosReal(PETSC_PI * (y[2] + .25))};
3071b7f5c055SJed Brown   f[0]           = s[0] * c[1] + s[1] * c[2] + s[2] * c[0];
3072b7f5c055SJed Brown   grad[0]        = PETSC_PI * (c[0] * c[1] - s[2] * s[0]);
3073b7f5c055SJed Brown   grad[1]        = PETSC_PI * (c[1] * c[2] - s[0] * s[1]);
3074b7f5c055SJed Brown   grad[2]        = PETSC_PI * (c[2] * c[0] - s[1] * s[2]);
3075b7f5c055SJed Brown   hess[0][0]     = -PetscSqr(PETSC_PI) * (s[0] * c[1] + s[2] * c[0]);
3076b7f5c055SJed Brown   hess[0][1]     = -PetscSqr(PETSC_PI) * (c[0] * s[1]);
3077b7f5c055SJed Brown   hess[0][2]     = -PetscSqr(PETSC_PI) * (c[2] * s[0]);
3078b7f5c055SJed Brown   hess[1][0]     = -PetscSqr(PETSC_PI) * (s[1] * c[2] + s[0] * c[1]);
3079b7f5c055SJed Brown   hess[1][1]     = -PetscSqr(PETSC_PI) * (c[1] * s[2]);
3080b7f5c055SJed Brown   hess[2][2]     = -PetscSqr(PETSC_PI) * (c[0] * s[1]);
3081b7f5c055SJed Brown   hess[2][0]     = -PetscSqr(PETSC_PI) * (s[2] * c[0] + s[1] * c[2]);
3082b7f5c055SJed Brown   hess[2][1]     = -PetscSqr(PETSC_PI) * (c[2] * s[0]);
3083b7f5c055SJed Brown   hess[2][2]     = -PetscSqr(PETSC_PI) * (c[1] * s[2]);
3084b7f5c055SJed Brown }
3085b7f5c055SJed Brown 
30864663dae6SJed Brown // u[] is a tentative normal on input. Replace with the implicit function gradient in the same direction
3087d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSExtrudeNormalFunc_Gyroid(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt r, PetscScalar u[], void *ctx)
3088d71ae5a4SJacob Faibussowitsch {
30894663dae6SJed Brown   PetscReal s[3] = {PetscSinReal(PETSC_PI * x[0]), PetscSinReal(PETSC_PI * (x[1] + .5)), PetscSinReal(PETSC_PI * (x[2] + .25))};
30904663dae6SJed Brown   PetscReal c[3] = {PetscCosReal(PETSC_PI * x[0]), PetscCosReal(PETSC_PI * (x[1] + .5)), PetscCosReal(PETSC_PI * (x[2] + .25))};
30914663dae6SJed Brown   u[0]           = PETSC_PI * (c[0] * c[1] - s[2] * s[0]);
30924663dae6SJed Brown   u[1]           = PETSC_PI * (c[1] * c[2] - s[0] * s[1]);
30934663dae6SJed Brown   u[2]           = PETSC_PI * (c[2] * c[0] - s[1] * s[2]);
30943ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
30954663dae6SJed Brown }
30964663dae6SJed Brown 
3097b7f5c055SJed Brown /*
3098b7f5c055SJed Brown    We wish to solve
3099b7f5c055SJed Brown 
3100b7f5c055SJed Brown          min_y || y - x ||^2  subject to f(y) = 0
3101b7f5c055SJed Brown 
3102b7f5c055SJed Brown    Let g(y) = grad(f).  The minimization problem is equivalent to asking to satisfy
3103b7f5c055SJed 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
3104b7f5c055SJed Brown    tangent space and ask for both components in the tangent space to be zero.
3105b7f5c055SJed Brown 
3106b7f5c055SJed Brown    Take g to be a column vector and compute the "full QR" factorization Q R = g,
3107b7f5c055SJed Brown    where Q = I - 2 n n^T is a symmetric orthogonal matrix.
3108b7f5c055SJed Brown    The first column of Q is parallel to g so the remaining two columns span the null space.
3109b7f5c055SJed Brown    Let Qn = Q[:,1:] be those remaining columns.  Then Qn Qn^T is an orthogonal projector into the tangent space.
3110da81f932SPierre Jolivet    Since Q is symmetric, this is equivalent to multiplying by Q and taking the last two entries.
3111b7f5c055SJed Brown    In total, we have a system of 3 equations in 3 unknowns:
3112b7f5c055SJed Brown 
3113b7f5c055SJed Brown      f(y) = 0                       1 equation
3114b7f5c055SJed Brown      Qn^T (y - x) = 0               2 equations
3115b7f5c055SJed Brown 
3116b7f5c055SJed Brown    Here, we compute the residual and Jacobian of this system.
3117b7f5c055SJed Brown */
3118d71ae5a4SJacob Faibussowitsch static void TPSNearestPointResJac(TPSEvaluateFunc feval, const PetscScalar x[], const PetscScalar y[], PetscScalar res[], PetscScalar J[])
3119d71ae5a4SJacob Faibussowitsch {
3120b7f5c055SJed Brown   PetscReal yreal[3] = {PetscRealPart(y[0]), PetscRealPart(y[1]), PetscRealPart(y[2])};
3121b7f5c055SJed Brown   PetscReal d[3]     = {PetscRealPart(y[0] - x[0]), PetscRealPart(y[1] - x[1]), PetscRealPart(y[2] - x[2])};
31222f0490c0SSatish Balay   PetscReal f, grad[3], n[3], norm, norm_y[3], nd, nd_y[3], sign;
31239371c9d4SSatish Balay   PetscReal n_y[3][3] = {
31249371c9d4SSatish Balay     {0, 0, 0},
31259371c9d4SSatish Balay     {0, 0, 0},
31269371c9d4SSatish Balay     {0, 0, 0}
31279371c9d4SSatish Balay   };
3128b7f5c055SJed Brown 
3129b7f5c055SJed Brown   feval(yreal, &f, grad, n_y);
3130b7f5c055SJed Brown 
3131b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) n[i] = grad[i];
3132b7f5c055SJed Brown   norm = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2]));
3133ad540459SPierre Jolivet   for (PetscInt i = 0; i < 3; i++) norm_y[i] = 1. / norm * n[i] * n_y[i][i];
3134b7f5c055SJed Brown 
3135b7f5c055SJed Brown   // Define the Householder reflector
3136b7f5c055SJed Brown   sign = n[0] >= 0 ? 1. : -1.;
3137b7f5c055SJed Brown   n[0] += norm * sign;
3138b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) n_y[0][i] += norm_y[i] * sign;
3139b7f5c055SJed Brown 
3140b7f5c055SJed Brown   norm      = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2]));
3141b7f5c055SJed Brown   norm_y[0] = 1. / norm * (n[0] * n_y[0][0]);
3142b7f5c055SJed Brown   norm_y[1] = 1. / norm * (n[0] * n_y[0][1] + n[1] * n_y[1][1]);
3143b7f5c055SJed Brown   norm_y[2] = 1. / norm * (n[0] * n_y[0][2] + n[2] * n_y[2][2]);
3144b7f5c055SJed Brown 
3145b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) {
3146b7f5c055SJed Brown     n[i] /= norm;
3147b7f5c055SJed Brown     for (PetscInt j = 0; j < 3; j++) {
3148b7f5c055SJed Brown       // note that n[i] is n_old[i]/norm when executing the code below
3149b7f5c055SJed Brown       n_y[i][j] = n_y[i][j] / norm - n[i] / norm * norm_y[j];
3150b7f5c055SJed Brown     }
3151b7f5c055SJed Brown   }
3152b7f5c055SJed Brown 
3153b7f5c055SJed Brown   nd = n[0] * d[0] + n[1] * d[1] + n[2] * d[2];
3154b7f5c055SJed 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];
3155b7f5c055SJed Brown 
3156b7f5c055SJed Brown   res[0] = f;
3157b7f5c055SJed Brown   res[1] = d[1] - 2 * n[1] * nd;
3158b7f5c055SJed Brown   res[2] = d[2] - 2 * n[2] * nd;
3159b7f5c055SJed Brown   // J[j][i] is J_{ij} (column major)
3160b7f5c055SJed Brown   for (PetscInt j = 0; j < 3; j++) {
3161b7f5c055SJed Brown     J[0 + j * 3] = grad[j];
3162b7f5c055SJed Brown     J[1 + j * 3] = (j == 1) * 1. - 2 * (n_y[1][j] * nd + n[1] * nd_y[j]);
3163b7f5c055SJed Brown     J[2 + j * 3] = (j == 2) * 1. - 2 * (n_y[2][j] * nd + n[2] * nd_y[j]);
3164b7f5c055SJed Brown   }
3165b7f5c055SJed Brown }
3166b7f5c055SJed Brown 
3167b7f5c055SJed Brown /*
3168b7f5c055SJed Brown    Project x to the nearest point on the implicit surface using Newton's method.
3169b7f5c055SJed Brown */
3170d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSNearestPoint(TPSEvaluateFunc feval, PetscScalar x[])
3171d71ae5a4SJacob Faibussowitsch {
3172b7f5c055SJed Brown   PetscScalar y[3] = {x[0], x[1], x[2]}; // Initial guess
3173b7f5c055SJed Brown 
3174b7f5c055SJed Brown   PetscFunctionBegin;
3175b7f5c055SJed Brown   for (PetscInt iter = 0; iter < 10; iter++) {
3176b7f5c055SJed Brown     PetscScalar res[3], J[9];
3177b7f5c055SJed Brown     PetscReal   resnorm;
3178b7f5c055SJed Brown     TPSNearestPointResJac(feval, x, y, res, J);
3179b7f5c055SJed Brown     resnorm = PetscSqrtReal(PetscSqr(PetscRealPart(res[0])) + PetscSqr(PetscRealPart(res[1])) + PetscSqr(PetscRealPart(res[2])));
3180b7f5c055SJed Brown     if (0) { // Turn on this monitor if you need to confirm quadratic convergence
318163a3b9bcSJacob 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])));
3182b7f5c055SJed Brown     }
3183b7f5c055SJed Brown     if (resnorm < PETSC_SMALL) break;
3184b7f5c055SJed Brown 
3185b7f5c055SJed Brown     // Take the Newton step
31869566063dSJacob Faibussowitsch     PetscCall(PetscKernel_A_gets_inverse_A_3(J, 0., PETSC_FALSE, NULL));
3187b7f5c055SJed Brown     PetscKernel_v_gets_v_minus_A_times_w_3(y, J, res);
3188b7f5c055SJed Brown   }
3189b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) x[i] = y[i];
31903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3191b7f5c055SJed Brown }
3192b7f5c055SJed Brown 
3193b7f5c055SJed Brown const char *const DMPlexTPSTypes[] = {"SCHWARZ_P", "GYROID", "DMPlexTPSType", "DMPLEX_TPS_", NULL};
3194b7f5c055SJed Brown 
3195d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateTPSMesh_Internal(DM dm, DMPlexTPSType tpstype, const PetscInt extent[], const DMBoundaryType periodic[], PetscBool tps_distribute, PetscInt refinements, PetscInt layers, PetscReal thickness)
3196d71ae5a4SJacob Faibussowitsch {
3197b7f5c055SJed Brown   PetscMPIInt rank;
3198b7f5c055SJed Brown   PetscInt    topoDim = 2, spaceDim = 3, numFaces = 0, numVertices = 0, numEdges = 0;
3199b7f5c055SJed Brown   PetscInt(*edges)[2] = NULL, *edgeSets = NULL;
3200b7f5c055SJed Brown   PetscInt           *cells_flat = NULL;
3201b7f5c055SJed Brown   PetscReal          *vtxCoords  = NULL;
3202b7f5c055SJed Brown   TPSEvaluateFunc     evalFunc   = NULL;
32038434afd1SBarry Smith   PetscSimplePointFn *normalFunc = NULL;
3204b7f5c055SJed Brown   DMLabel             label;
3205b7f5c055SJed Brown 
3206b7f5c055SJed Brown   PetscFunctionBegin;
320746139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
32089566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
320963a3b9bcSJacob 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);
3210b7f5c055SJed Brown   switch (tpstype) {
3211b7f5c055SJed Brown   case DMPLEX_TPS_SCHWARZ_P:
3212b7f5c055SJed 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");
3213c5853193SPierre Jolivet     if (rank == 0) {
3214b7f5c055SJed Brown       PetscInt(*cells)[6][4][4] = NULL; // [junction, junction-face, cell, conn]
3215b7f5c055SJed Brown       PetscInt  Njunctions = 0, Ncuts = 0, Npipes[3], vcount;
3216b7f5c055SJed Brown       PetscReal L = 1;
3217b7f5c055SJed Brown 
3218b7f5c055SJed Brown       Npipes[0]   = (extent[0] + 1) * extent[1] * extent[2];
3219b7f5c055SJed Brown       Npipes[1]   = extent[0] * (extent[1] + 1) * extent[2];
3220b7f5c055SJed Brown       Npipes[2]   = extent[0] * extent[1] * (extent[2] + 1);
3221b7f5c055SJed Brown       Njunctions  = extent[0] * extent[1] * extent[2];
3222b7f5c055SJed Brown       Ncuts       = 2 * (extent[0] * extent[1] + extent[1] * extent[2] + extent[2] * extent[0]);
3223b7f5c055SJed Brown       numVertices = 4 * (Npipes[0] + Npipes[1] + Npipes[2]) + 8 * Njunctions;
32249566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(3 * numVertices, &vtxCoords));
32259566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Njunctions, &cells));
32269566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Ncuts * 4, &edges));
32279566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Ncuts * 4, &edgeSets));
3228b7f5c055SJed Brown       // x-normal pipes
3229b7f5c055SJed Brown       vcount = 0;
3230b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0] + 1; i++) {
3231b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3232b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2]; k++) {
3233b7f5c055SJed Brown             for (PetscInt l = 0; l < 4; l++) {
3234b7f5c055SJed Brown               vtxCoords[vcount++] = (2 * i - 1) * L;
3235b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * j * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3236b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * k * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3237b7f5c055SJed Brown             }
3238b7f5c055SJed Brown           }
3239b7f5c055SJed Brown         }
3240b7f5c055SJed Brown       }
3241b7f5c055SJed Brown       // y-normal pipes
3242b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0]; i++) {
3243b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1] + 1; j++) {
3244b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2]; k++) {
3245b7f5c055SJed Brown             for (PetscInt l = 0; l < 4; l++) {
3246b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * i * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3247b7f5c055SJed Brown               vtxCoords[vcount++] = (2 * j - 1) * L;
3248b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * k * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3249b7f5c055SJed Brown             }
3250b7f5c055SJed Brown           }
3251b7f5c055SJed Brown         }
3252b7f5c055SJed Brown       }
3253b7f5c055SJed Brown       // z-normal pipes
3254b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0]; i++) {
3255b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3256b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2] + 1; k++) {
3257b7f5c055SJed Brown             for (PetscInt l = 0; l < 4; l++) {
3258b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * i * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3259b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * j * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3260b7f5c055SJed Brown               vtxCoords[vcount++] = (2 * k - 1) * L;
3261b7f5c055SJed Brown             }
3262b7f5c055SJed Brown           }
3263b7f5c055SJed Brown         }
3264b7f5c055SJed Brown       }
3265b7f5c055SJed Brown       // junctions
3266b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0]; i++) {
3267b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3268b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2]; k++) {
3269b7f5c055SJed Brown             const PetscInt J = (i * extent[1] + j) * extent[2] + k, Jvoff = (Npipes[0] + Npipes[1] + Npipes[2]) * 4 + J * 8;
3270b7f5c055SJed Brown             PetscCheck(vcount / 3 == Jvoff, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unexpected vertex count");
3271b7f5c055SJed Brown             for (PetscInt ii = 0; ii < 2; ii++) {
3272b7f5c055SJed Brown               for (PetscInt jj = 0; jj < 2; jj++) {
3273b7f5c055SJed Brown                 for (PetscInt kk = 0; kk < 2; kk++) {
3274b7f5c055SJed Brown                   double Ls           = (1 - sqrt(2) / 4) * L;
3275b7f5c055SJed Brown                   vtxCoords[vcount++] = 2 * i * L + (2 * ii - 1) * Ls;
3276b7f5c055SJed Brown                   vtxCoords[vcount++] = 2 * j * L + (2 * jj - 1) * Ls;
3277b7f5c055SJed Brown                   vtxCoords[vcount++] = 2 * k * L + (2 * kk - 1) * Ls;
3278b7f5c055SJed Brown                 }
3279b7f5c055SJed Brown               }
3280b7f5c055SJed Brown             }
3281b7f5c055SJed Brown             const PetscInt jfaces[3][2][4] = {
3282b7f5c055SJed Brown               {{3, 1, 0, 2}, {7, 5, 4, 6}}, // x-aligned
3283b7f5c055SJed Brown               {{5, 4, 0, 1}, {7, 6, 2, 3}}, // y-aligned
3284b7f5c055SJed Brown               {{6, 2, 0, 4}, {7, 3, 1, 5}}  // z-aligned
3285b7f5c055SJed Brown             };
3286b7f5c055SJed Brown             const PetscInt pipe_lo[3] = {// vertex numbers of pipes
32879371c9d4SSatish 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};
3288b7f5c055SJed Brown             const PetscInt pipe_hi[3] = {// vertex numbers of pipes
32899371c9d4SSatish 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};
3290b7f5c055SJed Brown             for (PetscInt dir = 0; dir < 3; dir++) { // x,y,z
3291b7f5c055SJed Brown               const PetscInt ijk[3] = {i, j, k};
3292b7f5c055SJed Brown               for (PetscInt l = 0; l < 4; l++) { // rotations
3293b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][0] = pipe_lo[dir] + l;
3294b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][1] = Jvoff + jfaces[dir][0][l];
3295b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][2] = Jvoff + jfaces[dir][0][(l - 1 + 4) % 4];
3296b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][3] = pipe_lo[dir] + (l - 1 + 4) % 4;
3297b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][0] = Jvoff + jfaces[dir][1][l];
3298b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][1] = pipe_hi[dir] + l;
3299b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][2] = pipe_hi[dir] + (l - 1 + 4) % 4;
3300b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][3] = Jvoff + jfaces[dir][1][(l - 1 + 4) % 4];
3301b7f5c055SJed Brown                 if (ijk[dir] == 0) {
3302b7f5c055SJed Brown                   edges[numEdges][0] = pipe_lo[dir] + l;
3303b7f5c055SJed Brown                   edges[numEdges][1] = pipe_lo[dir] + (l + 1) % 4;
3304b7f5c055SJed Brown                   edgeSets[numEdges] = dir * 2 + 1;
3305b7f5c055SJed Brown                   numEdges++;
3306b7f5c055SJed Brown                 }
3307b7f5c055SJed Brown                 if (ijk[dir] + 1 == extent[dir]) {
3308b7f5c055SJed Brown                   edges[numEdges][0] = pipe_hi[dir] + l;
3309b7f5c055SJed Brown                   edges[numEdges][1] = pipe_hi[dir] + (l + 1) % 4;
3310b7f5c055SJed Brown                   edgeSets[numEdges] = dir * 2 + 2;
3311b7f5c055SJed Brown                   numEdges++;
3312b7f5c055SJed Brown                 }
3313b7f5c055SJed Brown               }
3314b7f5c055SJed Brown             }
3315b7f5c055SJed Brown           }
3316b7f5c055SJed Brown         }
3317b7f5c055SJed Brown       }
331863a3b9bcSJacob Faibussowitsch       PetscCheck(numEdges == Ncuts * 4, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Edge count %" PetscInt_FMT " incompatible with number of cuts %" PetscInt_FMT, numEdges, Ncuts);
3319b7f5c055SJed Brown       numFaces   = 24 * Njunctions;
3320b7f5c055SJed Brown       cells_flat = cells[0][0][0];
3321b7f5c055SJed Brown     }
3322b7f5c055SJed Brown     evalFunc   = TPSEvaluate_SchwarzP;
33234663dae6SJed Brown     normalFunc = TPSExtrudeNormalFunc_SchwarzP;
3324b7f5c055SJed Brown     break;
3325b7f5c055SJed Brown   case DMPLEX_TPS_GYROID:
3326c5853193SPierre Jolivet     if (rank == 0) {
3327b7f5c055SJed Brown       // This is a coarse mesh approximation of the gyroid shifted to being the zero of the level set
3328b7f5c055SJed Brown       //
3329b7f5c055SJed 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)
3330b7f5c055SJed Brown       //
3331b7f5c055SJed Brown       // on the cell [0,2]^3.
3332b7f5c055SJed Brown       //
3333b7f5c055SJed Brown       // Think about dividing that cell into four columns, and focus on the column [0,1]x[0,1]x[0,2].
3334b7f5c055SJed Brown       // If you looked at the gyroid in that column at different slices of z you would see that it kind of spins
3335b7f5c055SJed Brown       // like a boomerang:
3336b7f5c055SJed Brown       //
3337b7f5c055SJed Brown       //     z = 0          z = 1/4        z = 1/2        z = 3/4     //
3338b7f5c055SJed Brown       //     -----          -------        -------        -------     //
3339b7f5c055SJed Brown       //                                                              //
3340b7f5c055SJed Brown       //     +       +      +       +      +       +      +   \   +   //
3341b7f5c055SJed Brown       //      \                                   /            \      //
3342b7f5c055SJed Brown       //       \            `-_   _-'            /              }     //
3343b7f5c055SJed Brown       //        *-_            `-'            _-'              /      //
3344b7f5c055SJed Brown       //     +     `-+      +       +      +-'     +      +   /   +   //
3345b7f5c055SJed Brown       //                                                              //
3346b7f5c055SJed Brown       //                                                              //
3347b7f5c055SJed Brown       //     z = 1          z = 5/4        z = 3/2        z = 7/4     //
3348b7f5c055SJed Brown       //     -----          -------        -------        -------     //
3349b7f5c055SJed Brown       //                                                              //
3350b7f5c055SJed Brown       //     +-_     +      +       +      +     _-+      +   /   +   //
3351b7f5c055SJed Brown       //        `-_            _-_            _-`            /        //
3352b7f5c055SJed Brown       //           \        _-'   `-_        /              {         //
3353b7f5c055SJed Brown       //            \                       /                \        //
3354b7f5c055SJed Brown       //     +       +      +       +      +       +      +   \   +   //
3355b7f5c055SJed Brown       //
3356b7f5c055SJed Brown       //
3357b7f5c055SJed Brown       // This course mesh approximates each of these slices by two line segments,
3358b7f5c055SJed Brown       // and then connects the segments in consecutive layers with quadrilateral faces.
3359b7f5c055SJed Brown       // All of the end points of the segments are multiples of 1/4 except for the
3360b7f5c055SJed Brown       // point * in the picture for z = 0 above and the similar points in other layers.
3361b7f5c055SJed Brown       // That point is at (gamma, gamma, 0), where gamma is calculated below.
3362b7f5c055SJed Brown       //
3363b7f5c055SJed Brown       // The column  [1,2]x[1,2]x[0,2] looks the same as this column;
3364b7f5c055SJed Brown       // The columns [1,2]x[0,1]x[0,2] and [0,1]x[1,2]x[0,2] are mirror images.
3365b7f5c055SJed Brown       //
3366b7f5c055SJed Brown       // As for how this method turned into the names given to the vertices:
3367b7f5c055SJed Brown       // that was not systematic, it was just the way it worked out in my handwritten notes.
3368b7f5c055SJed Brown 
3369b7f5c055SJed Brown       PetscInt facesPerBlock = 64;
3370b7f5c055SJed Brown       PetscInt vertsPerBlock = 56;
3371b7f5c055SJed Brown       PetscInt extentPlus[3];
3372b7f5c055SJed Brown       PetscInt numBlocks, numBlocksPlus;
33739371c9d4SSatish 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;
33749371c9d4SSatish Balay       const PetscInt pattern[64][4] = {
33759371c9d4SSatish Balay         /* face to vertex within the coarse discretization of a single gyroid block */
3376b7f5c055SJed Brown         /* layer 0 */
33779371c9d4SSatish Balay         {A,           C,           K,           G          },
33789371c9d4SSatish Balay         {C,           B,           II,          K          },
33799371c9d4SSatish Balay         {D,           A,           H,           L          },
33809371c9d4SSatish Balay         {B + 56 * 1,  D,           L,           J          },
33819371c9d4SSatish Balay         {E,           B + 56 * 1,  J,           N          },
33829371c9d4SSatish Balay         {A + 56 * 2,  E,           N,           H + 56 * 2 },
33839371c9d4SSatish Balay         {F,           A + 56 * 2,  G + 56 * 2,  M          },
33849371c9d4SSatish Balay         {B,           F,           M,           II         },
3385b7f5c055SJed Brown         /* layer 1 */
33869371c9d4SSatish Balay         {G,           K,           Q,           O          },
33879371c9d4SSatish Balay         {K,           II,          P,           Q          },
33889371c9d4SSatish Balay         {L,           H,           O + 56 * 1,  R          },
33899371c9d4SSatish Balay         {J,           L,           R,           P          },
33909371c9d4SSatish Balay         {N,           J,           P,           S          },
33919371c9d4SSatish Balay         {H + 56 * 2,  N,           S,           O + 56 * 3 },
33929371c9d4SSatish Balay         {M,           G + 56 * 2,  O + 56 * 2,  T          },
33939371c9d4SSatish Balay         {II,          M,           T,           P          },
3394b7f5c055SJed Brown         /* layer 2 */
33959371c9d4SSatish Balay         {O,           Q,           Y,           U          },
33969371c9d4SSatish Balay         {Q,           P,           W,           Y          },
33979371c9d4SSatish Balay         {R,           O + 56 * 1,  U + 56 * 1,  Ap         },
33989371c9d4SSatish Balay         {P,           R,           Ap,          W          },
33999371c9d4SSatish Balay         {S,           P,           X,           Bp         },
34009371c9d4SSatish Balay         {O + 56 * 3,  S,           Bp,          V + 56 * 1 },
34019371c9d4SSatish Balay         {T,           O + 56 * 2,  V,           Z          },
34029371c9d4SSatish Balay         {P,           T,           Z,           X          },
3403b7f5c055SJed Brown         /* layer 3 */
34049371c9d4SSatish Balay         {U,           Y,           Ep,          Dp         },
34059371c9d4SSatish Balay         {Y,           W,           Cp,          Ep         },
34069371c9d4SSatish Balay         {Ap,          U + 56 * 1,  Dp + 56 * 1, Gp         },
34079371c9d4SSatish Balay         {W,           Ap,          Gp,          Cp         },
34089371c9d4SSatish Balay         {Bp,          X,           Cp + 56 * 2, Fp         },
34099371c9d4SSatish Balay         {V + 56 * 1,  Bp,          Fp,          Dp + 56 * 1},
34109371c9d4SSatish Balay         {Z,           V,           Dp,          Hp         },
34119371c9d4SSatish Balay         {X,           Z,           Hp,          Cp + 56 * 2},
3412b7f5c055SJed Brown         /* layer 4 */
34139371c9d4SSatish Balay         {Dp,          Ep,          Mp,          Kp         },
34149371c9d4SSatish Balay         {Ep,          Cp,          Ip,          Mp         },
34159371c9d4SSatish Balay         {Gp,          Dp + 56 * 1, Lp,          Np         },
34169371c9d4SSatish Balay         {Cp,          Gp,          Np,          Jp         },
34179371c9d4SSatish Balay         {Fp,          Cp + 56 * 2, Jp + 56 * 2, Pp         },
34189371c9d4SSatish Balay         {Dp + 56 * 1, Fp,          Pp,          Lp         },
34199371c9d4SSatish Balay         {Hp,          Dp,          Kp,          Op         },
34209371c9d4SSatish Balay         {Cp + 56 * 2, Hp,          Op,          Ip + 56 * 2},
3421b7f5c055SJed Brown         /* layer 5 */
34229371c9d4SSatish Balay         {Kp,          Mp,          Sp,          Rp         },
34239371c9d4SSatish Balay         {Mp,          Ip,          Qp,          Sp         },
34249371c9d4SSatish Balay         {Np,          Lp,          Rp,          Tp         },
34259371c9d4SSatish Balay         {Jp,          Np,          Tp,          Qp + 56 * 1},
34269371c9d4SSatish Balay         {Pp,          Jp + 56 * 2, Qp + 56 * 3, Up         },
34279371c9d4SSatish Balay         {Lp,          Pp,          Up,          Rp         },
34289371c9d4SSatish Balay         {Op,          Kp,          Rp,          Vp         },
34299371c9d4SSatish Balay         {Ip + 56 * 2, Op,          Vp,          Qp + 56 * 2},
3430b7f5c055SJed Brown         /* layer 6 */
34319371c9d4SSatish Balay         {Rp,          Sp,          Aq,          Yp         },
34329371c9d4SSatish Balay         {Sp,          Qp,          Wp,          Aq         },
34339371c9d4SSatish Balay         {Tp,          Rp,          Yp,          Cq         },
34349371c9d4SSatish Balay         {Qp + 56 * 1, Tp,          Cq,          Wp + 56 * 1},
34359371c9d4SSatish Balay         {Up,          Qp + 56 * 3, Xp + 56 * 1, Dq         },
34369371c9d4SSatish Balay         {Rp,          Up,          Dq,          Zp         },
34379371c9d4SSatish Balay         {Vp,          Rp,          Zp,          Bq         },
34389371c9d4SSatish Balay         {Qp + 56 * 2, Vp,          Bq,          Xp         },
3439b7f5c055SJed Brown         /* layer 7 (the top is the periodic image of the bottom of layer 0) */
34409371c9d4SSatish Balay         {Yp,          Aq,          C + 56 * 4,  A + 56 * 4 },
34419371c9d4SSatish Balay         {Aq,          Wp,          B + 56 * 4,  C + 56 * 4 },
34429371c9d4SSatish Balay         {Cq,          Yp,          A + 56 * 4,  D + 56 * 4 },
34439371c9d4SSatish Balay         {Wp + 56 * 1, Cq,          D + 56 * 4,  B + 56 * 5 },
34449371c9d4SSatish Balay         {Dq,          Xp + 56 * 1, B + 56 * 5,  E + 56 * 4 },
34459371c9d4SSatish Balay         {Zp,          Dq,          E + 56 * 4,  A + 56 * 6 },
34469371c9d4SSatish Balay         {Bq,          Zp,          A + 56 * 6,  F + 56 * 4 },
34479371c9d4SSatish Balay         {Xp,          Bq,          F + 56 * 4,  B + 56 * 4 }
3448b7f5c055SJed Brown       };
3449b7f5c055SJed Brown       const PetscReal gamma                = PetscAcosReal((PetscSqrtReal(3.) - 1.) / PetscSqrtReal(2.)) / PETSC_PI;
34509371c9d4SSatish Balay       const PetscReal patternCoords[56][3] = {
3451bee3fc89SBarry Smith         {1.,        0.,        0.  }, /* A  */
3452bee3fc89SBarry Smith         {0.,        1.,        0.  }, /* B  */
3453bee3fc89SBarry Smith         {gamma,     gamma,     0.  }, /* C  */
3454bee3fc89SBarry Smith         {1 + gamma, 1 - gamma, 0.  }, /* D  */
3455bee3fc89SBarry Smith         {2 - gamma, 2 - gamma, 0.  }, /* E  */
3456bee3fc89SBarry Smith         {1 - gamma, 1 + gamma, 0.  }, /* F  */
3457b7f5c055SJed Brown 
3458bee3fc89SBarry Smith         {.5,        0,         .25 }, /* G  */
3459bee3fc89SBarry Smith         {1.5,       0.,        .25 }, /* H  */
3460bee3fc89SBarry Smith         {.5,        1.,        .25 }, /* II */
3461bee3fc89SBarry Smith         {1.5,       1.,        .25 }, /* J  */
3462bee3fc89SBarry Smith         {.25,       .5,        .25 }, /* K  */
3463bee3fc89SBarry Smith         {1.25,      .5,        .25 }, /* L  */
3464bee3fc89SBarry Smith         {.75,       1.5,       .25 }, /* M  */
3465bee3fc89SBarry Smith         {1.75,      1.5,       .25 }, /* N  */
3466b7f5c055SJed Brown 
3467bee3fc89SBarry Smith         {0.,        0.,        .5  }, /* O  */
3468bee3fc89SBarry Smith         {1.,        1.,        .5  }, /* P  */
3469bee3fc89SBarry Smith         {gamma,     1 - gamma, .5  }, /* Q  */
3470bee3fc89SBarry Smith         {1 + gamma, gamma,     .5  }, /* R  */
3471bee3fc89SBarry Smith         {2 - gamma, 1 + gamma, .5  }, /* S  */
3472bee3fc89SBarry Smith         {1 - gamma, 2 - gamma, .5  }, /* T  */
3473b7f5c055SJed Brown 
3474bee3fc89SBarry Smith         {0.,        .5,        .75 }, /* U  */
3475bee3fc89SBarry Smith         {0.,        1.5,       .75 }, /* V  */
3476bee3fc89SBarry Smith         {1.,        .5,        .75 }, /* W  */
3477bee3fc89SBarry Smith         {1.,        1.5,       .75 }, /* X  */
3478bee3fc89SBarry Smith         {.5,        .75,       .75 }, /* Y  */
3479bee3fc89SBarry Smith         {.5,        1.75,      .75 }, /* Z  */
3480bee3fc89SBarry Smith         {1.5,       .25,       .75 }, /* Ap */
3481bee3fc89SBarry Smith         {1.5,       1.25,      .75 }, /* Bp */
3482b7f5c055SJed Brown 
3483bee3fc89SBarry Smith         {1.,        0.,        1.  }, /* Cp */
3484bee3fc89SBarry Smith         {0.,        1.,        1.  }, /* Dp */
3485bee3fc89SBarry Smith         {1 - gamma, 1 - gamma, 1.  }, /* Ep */
3486bee3fc89SBarry Smith         {1 + gamma, 1 + gamma, 1.  }, /* Fp */
3487bee3fc89SBarry Smith         {2 - gamma, gamma,     1.  }, /* Gp */
3488bee3fc89SBarry Smith         {gamma,     2 - gamma, 1.  }, /* Hp */
3489b7f5c055SJed Brown 
3490bee3fc89SBarry Smith         {.5,        0.,        1.25}, /* Ip */
3491bee3fc89SBarry Smith         {1.5,       0.,        1.25}, /* Jp */
3492bee3fc89SBarry Smith         {.5,        1.,        1.25}, /* Kp */
3493bee3fc89SBarry Smith         {1.5,       1.,        1.25}, /* Lp */
3494bee3fc89SBarry Smith         {.75,       .5,        1.25}, /* Mp */
3495bee3fc89SBarry Smith         {1.75,      .5,        1.25}, /* Np */
3496bee3fc89SBarry Smith         {.25,       1.5,       1.25}, /* Op */
3497bee3fc89SBarry Smith         {1.25,      1.5,       1.25}, /* Pp */
3498b7f5c055SJed Brown 
3499bee3fc89SBarry Smith         {0.,        0.,        1.5 }, /* Qp */
3500bee3fc89SBarry Smith         {1.,        1.,        1.5 }, /* Rp */
3501bee3fc89SBarry Smith         {1 - gamma, gamma,     1.5 }, /* Sp */
3502bee3fc89SBarry Smith         {2 - gamma, 1 - gamma, 1.5 }, /* Tp */
3503bee3fc89SBarry Smith         {1 + gamma, 2 - gamma, 1.5 }, /* Up */
3504bee3fc89SBarry Smith         {gamma,     1 + gamma, 1.5 }, /* Vp */
3505b7f5c055SJed Brown 
3506bee3fc89SBarry Smith         {0.,        .5,        1.75}, /* Wp */
3507bee3fc89SBarry Smith         {0.,        1.5,       1.75}, /* Xp */
3508bee3fc89SBarry Smith         {1.,        .5,        1.75}, /* Yp */
3509bee3fc89SBarry Smith         {1.,        1.5,       1.75}, /* Zp */
3510bee3fc89SBarry Smith         {.5,        .25,       1.75}, /* Aq */
3511bee3fc89SBarry Smith         {.5,        1.25,      1.75}, /* Bq */
3512bee3fc89SBarry Smith         {1.5,       .75,       1.75}, /* Cq */
3513bee3fc89SBarry Smith         {1.5,       1.75,      1.75}, /* Dq */
3514b7f5c055SJed Brown       };
3515b7f5c055SJed Brown       PetscInt(*cells)[64][4] = NULL;
3516b7f5c055SJed Brown       PetscBool *seen;
3517b7f5c055SJed Brown       PetscInt  *vertToTrueVert;
3518b7f5c055SJed Brown       PetscInt   count;
3519b7f5c055SJed Brown 
3520b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) extentPlus[i] = extent[i] + 1;
3521b7f5c055SJed Brown       numBlocks = 1;
3522b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) numBlocks *= extent[i];
3523b7f5c055SJed Brown       numBlocksPlus = 1;
3524b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) numBlocksPlus *= extentPlus[i];
3525b7f5c055SJed Brown       numFaces = numBlocks * facesPerBlock;
35269566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numBlocks, &cells));
35279566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numBlocksPlus * vertsPerBlock, &seen));
3528b7f5c055SJed Brown       for (PetscInt k = 0; k < extent[2]; k++) {
3529b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3530b7f5c055SJed Brown           for (PetscInt i = 0; i < extent[0]; i++) {
3531b7f5c055SJed Brown             for (PetscInt f = 0; f < facesPerBlock; f++) {
3532b7f5c055SJed Brown               for (PetscInt v = 0; v < 4; v++) {
3533b7f5c055SJed Brown                 PetscInt vertRaw     = pattern[f][v];
3534b7f5c055SJed Brown                 PetscInt blockidx    = vertRaw / 56;
3535b7f5c055SJed Brown                 PetscInt patternvert = vertRaw % 56;
3536b7f5c055SJed Brown                 PetscInt xplus       = (blockidx & 1);
3537b7f5c055SJed Brown                 PetscInt yplus       = (blockidx & 2) >> 1;
3538b7f5c055SJed Brown                 PetscInt zplus       = (blockidx & 4) >> 2;
3539b7f5c055SJed Brown                 PetscInt zcoord      = (periodic && periodic[2] == DM_BOUNDARY_PERIODIC) ? ((k + zplus) % extent[2]) : (k + zplus);
3540b7f5c055SJed Brown                 PetscInt ycoord      = (periodic && periodic[1] == DM_BOUNDARY_PERIODIC) ? ((j + yplus) % extent[1]) : (j + yplus);
3541b7f5c055SJed Brown                 PetscInt xcoord      = (periodic && periodic[0] == DM_BOUNDARY_PERIODIC) ? ((i + xplus) % extent[0]) : (i + xplus);
3542b7f5c055SJed Brown                 PetscInt vert        = ((zcoord * extentPlus[1] + ycoord) * extentPlus[0] + xcoord) * 56 + patternvert;
3543b7f5c055SJed Brown 
3544b7f5c055SJed Brown                 cells[(k * extent[1] + j) * extent[0] + i][f][v] = vert;
3545b7f5c055SJed Brown                 seen[vert]                                       = PETSC_TRUE;
3546b7f5c055SJed Brown               }
3547b7f5c055SJed Brown             }
3548b7f5c055SJed Brown           }
3549b7f5c055SJed Brown         }
3550b7f5c055SJed Brown       }
35519371c9d4SSatish Balay       for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++)
35529371c9d4SSatish Balay         if (seen[i]) numVertices++;
3553b7f5c055SJed Brown       count = 0;
35549566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numBlocksPlus * vertsPerBlock, &vertToTrueVert));
35559566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numVertices * 3, &vtxCoords));
3556b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++) vertToTrueVert[i] = -1;
3557b7f5c055SJed Brown       for (PetscInt k = 0; k < extentPlus[2]; k++) {
3558b7f5c055SJed Brown         for (PetscInt j = 0; j < extentPlus[1]; j++) {
3559b7f5c055SJed Brown           for (PetscInt i = 0; i < extentPlus[0]; i++) {
3560b7f5c055SJed Brown             for (PetscInt v = 0; v < vertsPerBlock; v++) {
3561b7f5c055SJed Brown               PetscInt vIdx = ((k * extentPlus[1] + j) * extentPlus[0] + i) * vertsPerBlock + v;
3562b7f5c055SJed Brown 
3563b7f5c055SJed Brown               if (seen[vIdx]) {
3564b7f5c055SJed Brown                 PetscInt thisVert;
3565b7f5c055SJed Brown 
3566b7f5c055SJed Brown                 vertToTrueVert[vIdx] = thisVert = count++;
3567b7f5c055SJed Brown 
3568b7f5c055SJed Brown                 for (PetscInt d = 0; d < 3; d++) vtxCoords[3 * thisVert + d] = patternCoords[v][d];
3569b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 0] += i * 2;
3570b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 1] += j * 2;
3571b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 2] += k * 2;
3572b7f5c055SJed Brown               }
3573b7f5c055SJed Brown             }
3574b7f5c055SJed Brown           }
3575b7f5c055SJed Brown         }
3576b7f5c055SJed Brown       }
3577b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocks; i++) {
3578b7f5c055SJed Brown         for (PetscInt f = 0; f < facesPerBlock; f++) {
3579ad540459SPierre Jolivet           for (PetscInt v = 0; v < 4; v++) cells[i][f][v] = vertToTrueVert[cells[i][f][v]];
3580b7f5c055SJed Brown         }
3581b7f5c055SJed Brown       }
35829566063dSJacob Faibussowitsch       PetscCall(PetscFree(vertToTrueVert));
35839566063dSJacob Faibussowitsch       PetscCall(PetscFree(seen));
3584b7f5c055SJed Brown       cells_flat = cells[0][0];
3585b7f5c055SJed Brown       numEdges   = 0;
3586b7f5c055SJed Brown       for (PetscInt i = 0; i < numFaces; i++) {
3587b7f5c055SJed Brown         for (PetscInt e = 0; e < 4; e++) {
3588b7f5c055SJed Brown           PetscInt         ev[]       = {cells_flat[i * 4 + e], cells_flat[i * 4 + ((e + 1) % 4)]};
3589b7f5c055SJed Brown           const PetscReal *evCoords[] = {&vtxCoords[3 * ev[0]], &vtxCoords[3 * ev[1]]};
3590b7f5c055SJed Brown 
3591b7f5c055SJed Brown           for (PetscInt d = 0; d < 3; d++) {
3592b7f5c055SJed Brown             if (!periodic || periodic[0] != DM_BOUNDARY_PERIODIC) {
3593b7f5c055SJed Brown               if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) numEdges++;
3594b7f5c055SJed Brown               if (evCoords[0][d] == 2. * extent[d] && evCoords[1][d] == 2. * extent[d]) numEdges++;
3595b7f5c055SJed Brown             }
3596b7f5c055SJed Brown           }
3597b7f5c055SJed Brown         }
3598b7f5c055SJed Brown       }
35999566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numEdges, &edges));
36009566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numEdges, &edgeSets));
3601b7f5c055SJed Brown       for (PetscInt edge = 0, i = 0; i < numFaces; i++) {
3602b7f5c055SJed Brown         for (PetscInt e = 0; e < 4; e++) {
3603b7f5c055SJed Brown           PetscInt         ev[]       = {cells_flat[i * 4 + e], cells_flat[i * 4 + ((e + 1) % 4)]};
3604b7f5c055SJed Brown           const PetscReal *evCoords[] = {&vtxCoords[3 * ev[0]], &vtxCoords[3 * ev[1]]};
3605b7f5c055SJed Brown 
3606b7f5c055SJed Brown           for (PetscInt d = 0; d < 3; d++) {
3607b7f5c055SJed Brown             if (!periodic || periodic[d] != DM_BOUNDARY_PERIODIC) {
3608b7f5c055SJed Brown               if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) {
3609b7f5c055SJed Brown                 edges[edge][0]   = ev[0];
3610b7f5c055SJed Brown                 edges[edge][1]   = ev[1];
3611b7f5c055SJed Brown                 edgeSets[edge++] = 2 * d;
3612b7f5c055SJed Brown               }
3613b7f5c055SJed Brown               if (evCoords[0][d] == 2. * extent[d] && evCoords[1][d] == 2. * extent[d]) {
3614b7f5c055SJed Brown                 edges[edge][0]   = ev[0];
3615b7f5c055SJed Brown                 edges[edge][1]   = ev[1];
3616b7f5c055SJed Brown                 edgeSets[edge++] = 2 * d + 1;
3617b7f5c055SJed Brown               }
3618b7f5c055SJed Brown             }
3619b7f5c055SJed Brown           }
3620b7f5c055SJed Brown         }
3621b7f5c055SJed Brown       }
3622b7f5c055SJed Brown     }
3623b7f5c055SJed Brown     evalFunc   = TPSEvaluate_Gyroid;
36244663dae6SJed Brown     normalFunc = TPSExtrudeNormalFunc_Gyroid;
3625b7f5c055SJed Brown     break;
3626b7f5c055SJed Brown   }
3627b7f5c055SJed Brown 
36289566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, topoDim));
3629c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildFromCellList(dm, numFaces, numVertices, 4, cells_flat));
36309566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildFromCellList(dm, 0, 0, 0, NULL));
36319566063dSJacob Faibussowitsch   PetscCall(PetscFree(cells_flat));
3632b7f5c055SJed Brown   {
3633b7f5c055SJed Brown     DM idm;
36349566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(dm, &idm));
363569d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &idm));
3636b7f5c055SJed Brown   }
3637c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildCoordinatesFromCellList(dm, spaceDim, vtxCoords));
36389566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildCoordinatesFromCellList(dm, spaceDim, NULL));
36399566063dSJacob Faibussowitsch   PetscCall(PetscFree(vtxCoords));
3640b7f5c055SJed Brown 
36419566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
36429566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "Face Sets", &label));
3643b7f5c055SJed Brown   for (PetscInt e = 0; e < numEdges; e++) {
3644b7f5c055SJed Brown     PetscInt        njoin;
3645b7f5c055SJed Brown     const PetscInt *join, verts[] = {numFaces + edges[e][0], numFaces + edges[e][1]};
36469566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, 2, verts, &njoin, &join));
364763a3b9bcSJacob 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]);
36489566063dSJacob Faibussowitsch     PetscCall(DMLabelSetValue(label, join[0], edgeSets[e]));
36499566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, 2, verts, &njoin, &join));
3650b7f5c055SJed Brown   }
36519566063dSJacob Faibussowitsch   PetscCall(PetscFree(edges));
36529566063dSJacob Faibussowitsch   PetscCall(PetscFree(edgeSets));
36531436d7faSJed Brown   if (tps_distribute) {
36541436d7faSJed Brown     DM               pdm = NULL;
36551436d7faSJed Brown     PetscPartitioner part;
36561436d7faSJed Brown 
36579566063dSJacob Faibussowitsch     PetscCall(DMPlexGetPartitioner(dm, &part));
36589566063dSJacob Faibussowitsch     PetscCall(PetscPartitionerSetFromOptions(part));
36599566063dSJacob Faibussowitsch     PetscCall(DMPlexDistribute(dm, 0, NULL, &pdm));
366048a46eb9SPierre Jolivet     if (pdm) PetscCall(DMPlexReplace_Internal(dm, &pdm));
36611436d7faSJed Brown     // Do not auto-distribute again
36629566063dSJacob Faibussowitsch     PetscCall(DMPlexDistributeSetDefault(dm, PETSC_FALSE));
36631436d7faSJed Brown   }
3664b7f5c055SJed Brown 
36659566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
3666b7f5c055SJed Brown   for (PetscInt refine = 0; refine < refinements; refine++) {
3667b7f5c055SJed Brown     PetscInt     m;
3668b7f5c055SJed Brown     DM           dmf;
3669b7f5c055SJed Brown     Vec          X;
3670b7f5c055SJed Brown     PetscScalar *x;
36719566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, MPI_COMM_NULL, &dmf));
367269d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &dmf));
3673b7f5c055SJed Brown 
36749566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &X));
36759566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(X, &m));
36769566063dSJacob Faibussowitsch     PetscCall(VecGetArray(X, &x));
367748a46eb9SPierre Jolivet     for (PetscInt i = 0; i < m; i += 3) PetscCall(TPSNearestPoint(evalFunc, &x[i]));
36789566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(X, &x));
3679b7f5c055SJed Brown   }
3680b7f5c055SJed Brown 
3681b7f5c055SJed Brown   // Face Sets has already been propagated to new vertices during refinement; this propagates to the initial vertices.
36829566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "Face Sets", &label));
36839566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete(dm, label));
3684b7f5c055SJed Brown 
368546139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
368646139095SJed Brown 
3687b7f5c055SJed Brown   if (thickness > 0) {
36884663dae6SJed Brown     DM              edm, cdm, ecdm;
36894663dae6SJed Brown     DMPlexTransform tr;
36904663dae6SJed Brown     const char     *prefix;
36914663dae6SJed Brown     PetscOptions    options;
36924663dae6SJed Brown     // Code from DMPlexExtrude
36934663dae6SJed Brown     PetscCall(DMPlexTransformCreate(PetscObjectComm((PetscObject)dm), &tr));
36944663dae6SJed Brown     PetscCall(DMPlexTransformSetDM(tr, dm));
36954663dae6SJed Brown     PetscCall(DMPlexTransformSetType(tr, DMPLEXEXTRUDE));
36964663dae6SJed Brown     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, &prefix));
36974663dae6SJed Brown     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)tr, prefix));
36984663dae6SJed Brown     PetscCall(PetscObjectGetOptions((PetscObject)dm, &options));
36994663dae6SJed Brown     PetscCall(PetscObjectSetOptions((PetscObject)tr, options));
37004663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetLayers(tr, layers));
37014663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetThickness(tr, thickness));
37024663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetTensor(tr, PETSC_FALSE));
37034663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetSymmetric(tr, PETSC_TRUE));
37044663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetNormalFunction(tr, normalFunc));
37054663dae6SJed Brown     PetscCall(DMPlexTransformSetFromOptions(tr));
37064663dae6SJed Brown     PetscCall(PetscObjectSetOptions((PetscObject)tr, NULL));
37074663dae6SJed Brown     PetscCall(DMPlexTransformSetUp(tr));
37084663dae6SJed Brown     PetscCall(PetscObjectViewFromOptions((PetscObject)tr, NULL, "-dm_plex_tps_transform_view"));
37094663dae6SJed Brown     PetscCall(DMPlexTransformApply(tr, dm, &edm));
37104663dae6SJed Brown     PetscCall(DMCopyDisc(dm, edm));
37114663dae6SJed Brown     PetscCall(DMGetCoordinateDM(dm, &cdm));
37124663dae6SJed Brown     PetscCall(DMGetCoordinateDM(edm, &ecdm));
37134663dae6SJed Brown     PetscCall(DMCopyDisc(cdm, ecdm));
37144663dae6SJed Brown     PetscCall(DMPlexTransformCreateDiscLabels(tr, edm));
37154663dae6SJed Brown     PetscCall(DMPlexTransformDestroy(&tr));
3716a77a5016SMatthew G. Knepley     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, edm));
371769d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &edm));
3718b7f5c055SJed Brown   }
37193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3720b7f5c055SJed Brown }
3721b7f5c055SJed Brown 
3722b7f5c055SJed Brown /*@
3723b7f5c055SJed Brown   DMPlexCreateTPSMesh - Create a distributed, interpolated mesh of a triply-periodic surface
3724b7f5c055SJed Brown 
3725b7f5c055SJed Brown   Collective
3726b7f5c055SJed Brown 
3727b7f5c055SJed Brown   Input Parameters:
3728a1cb98faSBarry Smith + comm           - The communicator for the `DM` object
3729b7f5c055SJed Brown . tpstype        - Type of triply-periodic surface
3730b7f5c055SJed Brown . extent         - Array of length 3 containing number of periods in each direction
373120f4b53cSBarry Smith . periodic       - array of length 3 with periodicity, or `NULL` for non-periodic
37321436d7faSJed Brown . tps_distribute - Distribute 2D manifold mesh prior to refinement and extrusion (more scalable)
3733817da375SSatish Balay . refinements    - Number of factor-of-2 refinements of 2D manifold mesh
37341436d7faSJed Brown . layers         - Number of cell layers extruded in normal direction
3735817da375SSatish Balay - thickness      - Thickness in normal direction
3736b7f5c055SJed Brown 
3737b7f5c055SJed Brown   Output Parameter:
3738a1cb98faSBarry Smith . dm - The `DM` object
3739a1cb98faSBarry Smith 
3740a1cb98faSBarry Smith   Level: beginner
3741b7f5c055SJed Brown 
3742b7f5c055SJed Brown   Notes:
374315229ffcSPierre Jolivet   This meshes the surface of the Schwarz P or Gyroid surfaces.  Schwarz P is the simplest member of the triply-periodic minimal surfaces.
37441d27aa22SBarry Smith   <https://en.wikipedia.org/wiki/Schwarz_minimal_surface#Schwarz_P_(%22Primitive%22)> and can be cut with "clean" boundaries.
37451d27aa22SBarry 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.
3746b7f5c055SJed Brown   Our implementation creates a very coarse mesh of the surface and refines (by 4-way splitting) as many times as requested.
3747b7f5c055SJed Brown   On each refinement, all vertices are projected to their nearest point on the surface.
3748b7f5c055SJed Brown   This projection could readily be extended to related surfaces.
3749b7f5c055SJed Brown 
37501d27aa22SBarry Smith   See {cite}`maskery2018insights`
37511d27aa22SBarry Smith 
37521d27aa22SBarry Smith   The face (edge) sets for the Schwarz P surface are numbered $1(-x), 2(+x), 3(-y), 4(+y), 5(-z), 6(+z)$.
37531d27aa22SBarry Smith   When the mesh is refined, "Face Sets" contain the new vertices (created during refinement).
37541d27aa22SBarry Smith   Use `DMPlexLabelComplete()` to propagate to coarse-level vertices.
3755b7f5c055SJed Brown 
375660225df5SJacob Faibussowitsch   Developer Notes:
3757b7f5c055SJed Brown   The Gyroid mesh does not currently mark boundary sets.
3758b7f5c055SJed Brown 
37591cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSphereMesh()`, `DMSetType()`, `DMCreate()`
3760b7f5c055SJed Brown @*/
3761d71ae5a4SJacob 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)
3762d71ae5a4SJacob Faibussowitsch {
3763b7f5c055SJed Brown   PetscFunctionBegin;
37649566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
37659566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
37669566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateTPSMesh_Internal(*dm, tpstype, extent, periodic, tps_distribute, refinements, layers, thickness));
37673ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3768b7f5c055SJed Brown }
3769b7f5c055SJed Brown 
37709318fe57SMatthew G. Knepley /*@
37719318fe57SMatthew G. Knepley   DMPlexCreateSphereMesh - Creates a mesh on the d-dimensional sphere, S^d.
37729318fe57SMatthew G. Knepley 
37739318fe57SMatthew G. Knepley   Collective
37749318fe57SMatthew G. Knepley 
37759318fe57SMatthew G. Knepley   Input Parameters:
3776a1cb98faSBarry Smith + comm    - The communicator for the `DM` object
37779318fe57SMatthew G. Knepley . dim     - The dimension
37789318fe57SMatthew G. Knepley . simplex - Use simplices, or tensor product cells
37799318fe57SMatthew G. Knepley - R       - The radius
37809318fe57SMatthew G. Knepley 
37819318fe57SMatthew G. Knepley   Output Parameter:
3782a1cb98faSBarry Smith . dm - The `DM` object
37839318fe57SMatthew G. Knepley 
37849318fe57SMatthew G. Knepley   Level: beginner
37859318fe57SMatthew G. Knepley 
37861cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBallMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
37879318fe57SMatthew G. Knepley @*/
3788d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateSphereMesh(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscReal R, DM *dm)
3789d71ae5a4SJacob Faibussowitsch {
37909318fe57SMatthew G. Knepley   PetscFunctionBegin;
37914f572ea9SToby Isaac   PetscAssertPointer(dm, 5);
37929566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
37939566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
37949566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSphereMesh_Internal(*dm, dim, simplex, R));
37953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
37969318fe57SMatthew G. Knepley }
37979318fe57SMatthew G. Knepley 
3798d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBallMesh_Internal(DM dm, PetscInt dim, PetscReal R)
3799d71ae5a4SJacob Faibussowitsch {
38009318fe57SMatthew G. Knepley   DM          sdm, vol;
38019318fe57SMatthew G. Knepley   DMLabel     bdlabel;
3802dd2b43ebSStefano Zampini   const char *prefix;
38039318fe57SMatthew G. Knepley 
38049318fe57SMatthew G. Knepley   PetscFunctionBegin;
38059566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &sdm));
38069566063dSJacob Faibussowitsch   PetscCall(DMSetType(sdm, DMPLEX));
3807dd2b43ebSStefano Zampini   PetscCall(DMGetOptionsPrefix(dm, &prefix));
3808dd2b43ebSStefano Zampini   PetscCall(DMSetOptionsPrefix(sdm, prefix));
3809dd2b43ebSStefano Zampini   PetscCall(DMAppendOptionsPrefix(sdm, "bd_"));
3810dd2b43ebSStefano Zampini   PetscCall(DMPlexDistributeSetDefault(sdm, PETSC_FALSE));
38119566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSphereMesh_Internal(sdm, dim - 1, PETSC_TRUE, R));
38129566063dSJacob Faibussowitsch   PetscCall(DMSetFromOptions(sdm));
38139566063dSJacob Faibussowitsch   PetscCall(DMViewFromOptions(sdm, NULL, "-dm_view"));
38149566063dSJacob Faibussowitsch   PetscCall(DMPlexGenerate(sdm, NULL, PETSC_TRUE, &vol));
38159566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&sdm));
381669d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &vol));
38179566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
38189566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "marker", &bdlabel));
38199566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkBoundaryFaces(dm, PETSC_DETERMINE, bdlabel));
38209566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete(dm, bdlabel));
38213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
382251a74b61SMatthew G. Knepley }
382351a74b61SMatthew G. Knepley 
382451a74b61SMatthew G. Knepley /*@
382551a74b61SMatthew G. Knepley   DMPlexCreateBallMesh - Creates a simplex mesh on the d-dimensional ball, B^d.
382651a74b61SMatthew G. Knepley 
382751a74b61SMatthew G. Knepley   Collective
382851a74b61SMatthew G. Knepley 
382951a74b61SMatthew G. Knepley   Input Parameters:
3830a1cb98faSBarry Smith + comm - The communicator for the `DM` object
383151a74b61SMatthew G. Knepley . dim  - The dimension
383251a74b61SMatthew G. Knepley - R    - The radius
383351a74b61SMatthew G. Knepley 
383451a74b61SMatthew G. Knepley   Output Parameter:
3835a1cb98faSBarry Smith . dm - The `DM` object
383651a74b61SMatthew G. Knepley 
3837a1cb98faSBarry Smith   Options Database Key:
383860225df5SJacob Faibussowitsch . bd_dm_refine - This will refine the surface mesh preserving the sphere geometry
383951a74b61SMatthew G. Knepley 
384051a74b61SMatthew G. Knepley   Level: beginner
384151a74b61SMatthew G. Knepley 
38421cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSphereMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
384351a74b61SMatthew G. Knepley @*/
3844d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateBallMesh(MPI_Comm comm, PetscInt dim, PetscReal R, DM *dm)
3845d71ae5a4SJacob Faibussowitsch {
384651a74b61SMatthew G. Knepley   PetscFunctionBegin;
38479566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
38489566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
38499566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBallMesh_Internal(*dm, dim, R));
38503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
38512829fed8SMatthew G. Knepley }
38522829fed8SMatthew G. Knepley 
3853d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateReferenceCell_Internal(DM rdm, DMPolytopeType ct)
3854d71ae5a4SJacob Faibussowitsch {
38550a6ba040SMatthew G. Knepley   PetscFunctionBegin;
38569318fe57SMatthew G. Knepley   switch (ct) {
38579371c9d4SSatish Balay   case DM_POLYTOPE_POINT: {
38589318fe57SMatthew G. Knepley     PetscInt    numPoints[1]        = {1};
38599318fe57SMatthew G. Knepley     PetscInt    coneSize[1]         = {0};
38609318fe57SMatthew G. Knepley     PetscInt    cones[1]            = {0};
38619318fe57SMatthew G. Knepley     PetscInt    coneOrientations[1] = {0};
38629318fe57SMatthew G. Knepley     PetscScalar vertexCoords[1]     = {0.0};
38639318fe57SMatthew G. Knepley 
38649566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 0));
38659566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 0, numPoints, coneSize, cones, coneOrientations, vertexCoords));
38669371c9d4SSatish Balay   } break;
38679371c9d4SSatish Balay   case DM_POLYTOPE_SEGMENT: {
38689318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {2, 1};
38699318fe57SMatthew G. Knepley     PetscInt    coneSize[3]         = {2, 0, 0};
38709318fe57SMatthew G. Knepley     PetscInt    cones[2]            = {1, 2};
38719318fe57SMatthew G. Knepley     PetscInt    coneOrientations[2] = {0, 0};
38729318fe57SMatthew G. Knepley     PetscScalar vertexCoords[2]     = {-1.0, 1.0};
38739318fe57SMatthew G. Knepley 
38749566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 1));
38759566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
38769371c9d4SSatish Balay   } break;
38779371c9d4SSatish Balay   case DM_POLYTOPE_POINT_PRISM_TENSOR: {
3878b5a892a1SMatthew G. Knepley     PetscInt    numPoints[2]        = {2, 1};
3879b5a892a1SMatthew G. Knepley     PetscInt    coneSize[3]         = {2, 0, 0};
3880b5a892a1SMatthew G. Knepley     PetscInt    cones[2]            = {1, 2};
3881b5a892a1SMatthew G. Knepley     PetscInt    coneOrientations[2] = {0, 0};
3882b5a892a1SMatthew G. Knepley     PetscScalar vertexCoords[2]     = {-1.0, 1.0};
3883b5a892a1SMatthew G. Knepley 
38849566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 1));
38859566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
38869371c9d4SSatish Balay   } break;
38879371c9d4SSatish Balay   case DM_POLYTOPE_TRIANGLE: {
38889318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {3, 1};
38899318fe57SMatthew G. Knepley     PetscInt    coneSize[4]         = {3, 0, 0, 0};
38909318fe57SMatthew G. Knepley     PetscInt    cones[3]            = {1, 2, 3};
38919318fe57SMatthew G. Knepley     PetscInt    coneOrientations[3] = {0, 0, 0};
38929318fe57SMatthew G. Knepley     PetscScalar vertexCoords[6]     = {-1.0, -1.0, 1.0, -1.0, -1.0, 1.0};
38939318fe57SMatthew G. Knepley 
38949566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 2));
38959566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
38969371c9d4SSatish Balay   } break;
38979371c9d4SSatish Balay   case DM_POLYTOPE_QUADRILATERAL: {
38989318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {4, 1};
38999318fe57SMatthew G. Knepley     PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
39009318fe57SMatthew G. Knepley     PetscInt    cones[4]            = {1, 2, 3, 4};
39019318fe57SMatthew G. Knepley     PetscInt    coneOrientations[4] = {0, 0, 0, 0};
39029318fe57SMatthew G. Knepley     PetscScalar vertexCoords[8]     = {-1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0};
39039318fe57SMatthew G. Knepley 
39049566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 2));
39059566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
39069371c9d4SSatish Balay   } break;
39079371c9d4SSatish Balay   case DM_POLYTOPE_SEG_PRISM_TENSOR: {
39089318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {4, 1};
39099318fe57SMatthew G. Knepley     PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
39109318fe57SMatthew G. Knepley     PetscInt    cones[4]            = {1, 2, 3, 4};
39119318fe57SMatthew G. Knepley     PetscInt    coneOrientations[4] = {0, 0, 0, 0};
39129318fe57SMatthew G. Knepley     PetscScalar vertexCoords[8]     = {-1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0};
39139318fe57SMatthew G. Knepley 
39149566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 2));
39159566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
39169371c9d4SSatish Balay   } break;
39179371c9d4SSatish Balay   case DM_POLYTOPE_TETRAHEDRON: {
39189318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {4, 1};
39199318fe57SMatthew G. Knepley     PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
3920f0edb160SMatthew G. Knepley     PetscInt    cones[4]            = {1, 2, 3, 4};
39219318fe57SMatthew G. Knepley     PetscInt    coneOrientations[4] = {0, 0, 0, 0};
3922f0edb160SMatthew 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};
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_HEXAHEDRON: {
39289318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {8, 1};
39299318fe57SMatthew G. Knepley     PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
3930f0edb160SMatthew 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_TRI_PRISM: {
39389318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {6, 1};
39399318fe57SMatthew G. Knepley     PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
3940f0edb160SMatthew G. Knepley     PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
39419318fe57SMatthew G. Knepley     PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
39429371c9d4SSatish 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};
39439318fe57SMatthew G. Knepley 
39449566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
39459566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
39469371c9d4SSatish Balay   } break;
39479371c9d4SSatish Balay   case DM_POLYTOPE_TRI_PRISM_TENSOR: {
39489318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {6, 1};
39499318fe57SMatthew G. Knepley     PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
39509318fe57SMatthew G. Knepley     PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
39519318fe57SMatthew G. Knepley     PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
39529371c9d4SSatish 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};
39539318fe57SMatthew G. Knepley 
39549566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
39559566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
39569371c9d4SSatish Balay   } break;
39579371c9d4SSatish Balay   case DM_POLYTOPE_QUAD_PRISM_TENSOR: {
39589318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {8, 1};
39599318fe57SMatthew G. Knepley     PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
39609318fe57SMatthew G. Knepley     PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
39619318fe57SMatthew G. Knepley     PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
39629371c9d4SSatish 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};
39639318fe57SMatthew G. Knepley 
39649566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
39659566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
39669371c9d4SSatish Balay   } break;
39679371c9d4SSatish Balay   case DM_POLYTOPE_PYRAMID: {
39689318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {5, 1};
39699318fe57SMatthew G. Knepley     PetscInt    coneSize[6]         = {5, 0, 0, 0, 0, 0};
3970f0edb160SMatthew G. Knepley     PetscInt    cones[5]            = {1, 2, 3, 4, 5};
39719318fe57SMatthew G. Knepley     PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
39729371c9d4SSatish 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};
39739318fe57SMatthew G. Knepley 
39749566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
39759566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
39769371c9d4SSatish Balay   } break;
3977d71ae5a4SJacob Faibussowitsch   default:
3978d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)rdm), PETSC_ERR_ARG_WRONG, "Cannot create reference cell for cell type %s", DMPolytopeTypes[ct]);
39799318fe57SMatthew G. Knepley   }
39809318fe57SMatthew G. Knepley   {
39819318fe57SMatthew G. Knepley     PetscInt Nv, v;
39829318fe57SMatthew G. Knepley 
39839318fe57SMatthew G. Knepley     /* Must create the celltype label here so that we do not automatically try to compute the types */
39849566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(rdm, "celltype"));
39859566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCellType(rdm, 0, ct));
39869566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(rdm, NULL, &Nv));
39879566063dSJacob Faibussowitsch     for (v = 1; v < Nv; ++v) PetscCall(DMPlexSetCellType(rdm, v, DM_POLYTOPE_POINT));
39889318fe57SMatthew G. Knepley   }
39899566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolateInPlace_Internal(rdm));
39909566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)rdm, DMPolytopeTypes[ct]));
39913ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
39920a6ba040SMatthew G. Knepley }
39930a6ba040SMatthew G. Knepley 
39949318fe57SMatthew G. Knepley /*@
3995a1cb98faSBarry Smith   DMPlexCreateReferenceCell - Create a `DMPLEX` with the appropriate FEM reference cell
39969318fe57SMatthew G. Knepley 
39979318fe57SMatthew G. Knepley   Collective
39989318fe57SMatthew G. Knepley 
39999318fe57SMatthew G. Knepley   Input Parameters:
40009318fe57SMatthew G. Knepley + comm - The communicator
40019318fe57SMatthew G. Knepley - ct   - The cell type of the reference cell
40029318fe57SMatthew G. Knepley 
40039318fe57SMatthew G. Knepley   Output Parameter:
40049318fe57SMatthew G. Knepley . refdm - The reference cell
40059318fe57SMatthew G. Knepley 
40069318fe57SMatthew G. Knepley   Level: intermediate
40079318fe57SMatthew G. Knepley 
400842747ad1SJacob Faibussowitsch .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBoxMesh()`
40099318fe57SMatthew G. Knepley @*/
4010d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateReferenceCell(MPI_Comm comm, DMPolytopeType ct, DM *refdm)
4011d71ae5a4SJacob Faibussowitsch {
40120a6ba040SMatthew G. Knepley   PetscFunctionBegin;
40139566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, refdm));
40149566063dSJacob Faibussowitsch   PetscCall(DMSetType(*refdm, DMPLEX));
40159566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateReferenceCell_Internal(*refdm, ct));
40163ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
40179318fe57SMatthew G. Knepley }
401879a015ccSMatthew G. Knepley 
4019d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoundaryLabel_Private(DM dm, const char name[])
4020d71ae5a4SJacob Faibussowitsch {
40219318fe57SMatthew G. Knepley   DM        plex;
40229318fe57SMatthew G. Knepley   DMLabel   label;
40239318fe57SMatthew G. Knepley   PetscBool hasLabel;
40240a6ba040SMatthew G. Knepley 
4025c22d3578SMatthew G. Knepley   PetscFunctionBegin;
40269566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &hasLabel));
40273ba16761SJacob Faibussowitsch   if (hasLabel) PetscFunctionReturn(PETSC_SUCCESS);
40289566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, name));
40299566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
40309566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
40319566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkBoundaryFaces(plex, 1, label));
40321c8afea9SMatthew G. Knepley   PetscCall(DMPlexLabelComplete(plex, label));
40339566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plex));
40343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
40359318fe57SMatthew G. Knepley }
4036acdc6f61SToby Isaac 
4037669647acSMatthew G. Knepley /*
4038669647acSMatthew 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.
4039669647acSMatthew G. Knepley 
4040669647acSMatthew G. Knepley     (x, y) -> (r, theta) = (x[1], (x[0] - lower[0]) * 2\pi/(upper[0] - lower[0]))
4041669647acSMatthew G. Knepley */
4042d71ae5a4SJacob 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[])
4043d71ae5a4SJacob Faibussowitsch {
4044669647acSMatthew G. Knepley   const PetscReal low = PetscRealPart(constants[0]);
4045669647acSMatthew G. Knepley   const PetscReal upp = PetscRealPart(constants[1]);
4046669647acSMatthew G. Knepley   const PetscReal r   = PetscRealPart(u[1]);
4047669647acSMatthew G. Knepley   const PetscReal th  = 2. * PETSC_PI * (PetscRealPart(u[0]) - low) / (upp - low);
4048669647acSMatthew G. Knepley 
4049669647acSMatthew G. Knepley   f0[0] = r * PetscCosReal(th);
4050669647acSMatthew G. Knepley   f0[1] = r * PetscSinReal(th);
4051669647acSMatthew G. Knepley }
4052669647acSMatthew G. Knepley 
40535390be7dSMatthew G. Knepley // Insert vertices and their joins, marked by depth
40545390be7dSMatthew G. Knepley static PetscErrorCode ProcessCohesiveLabel_Vertices(DM dm, DMLabel label, DMLabel vlabel, PetscInt val, PetscInt n, const PetscInt vertices[])
40555390be7dSMatthew G. Knepley {
40565390be7dSMatthew G. Knepley   PetscFunctionBegin;
40575390be7dSMatthew G. Knepley   PetscCall(DMPlexMarkSubmesh_Interpolated(dm, vlabel, val, PETSC_FALSE, PETSC_FALSE, label, NULL));
40585390be7dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
40595390be7dSMatthew G. Knepley }
40605390be7dSMatthew G. Knepley 
40615390be7dSMatthew G. Knepley // Insert faces and their closures, marked by depth
40625390be7dSMatthew G. Knepley static PetscErrorCode ProcessCohesiveLabel_Faces(DM dm, DMLabel label, PetscInt n, const PetscInt faces[])
40635390be7dSMatthew G. Knepley {
40645390be7dSMatthew G. Knepley   PetscFunctionBegin;
40655390be7dSMatthew G. Knepley   for (PetscInt p = 0; p < n; ++p) {
40665390be7dSMatthew G. Knepley     const PetscInt point   = faces[p];
40675390be7dSMatthew G. Knepley     PetscInt      *closure = NULL;
40685390be7dSMatthew G. Knepley     PetscInt       clSize, pdepth;
40695390be7dSMatthew G. Knepley 
40705390be7dSMatthew G. Knepley     PetscCall(DMPlexGetPointDepth(dm, point, &pdepth));
40715390be7dSMatthew G. Knepley     PetscCall(DMLabelSetValue(label, point, pdepth));
40725390be7dSMatthew G. Knepley     PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &clSize, &closure));
40735390be7dSMatthew G. Knepley     for (PetscInt cl = 0; cl < clSize * 2; cl += 2) {
40745390be7dSMatthew G. Knepley       PetscCall(DMPlexGetPointDepth(dm, closure[cl], &pdepth));
40755390be7dSMatthew G. Knepley       PetscCall(DMLabelSetValue(label, closure[cl], pdepth));
40765390be7dSMatthew G. Knepley     }
40775390be7dSMatthew G. Knepley     PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &clSize, &closure));
40785390be7dSMatthew G. Knepley   }
40795390be7dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
40805390be7dSMatthew G. Knepley }
40815390be7dSMatthew G. Knepley 
40824e22dd4cSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscOptionsFindPairPrefix_Private(PetscOptions, const char pre[], const char name[], const char *option[], const char *value[], PetscBool *flg);
40834e22dd4cSMatthew G. Knepley 
40845dca41c3SJed Brown const char *const DMPlexShapes[] = {"box", "box_surface", "ball", "sphere", "cylinder", "schwarz_p", "gyroid", "doublet", "annulus", "hypercubic", "zbox", "unknown", "DMPlexShape", "DM_SHAPE_", NULL};
40859318fe57SMatthew G. Knepley 
4086d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateFromOptions_Internal(PetscOptionItems *PetscOptionsObject, PetscBool *useCoordSpace, DM dm)
4087d71ae5a4SJacob Faibussowitsch {
40889318fe57SMatthew G. Knepley   DMPlexShape    shape   = DM_SHAPE_BOX;
40899318fe57SMatthew G. Knepley   DMPolytopeType cell    = DM_POLYTOPE_TRIANGLE;
40909318fe57SMatthew G. Knepley   PetscInt       dim     = 2;
4091*b9da1bb3SMatthew G. Knepley   PetscBool      simplex = PETSC_TRUE, interpolate = PETSC_TRUE, orient = PETSC_FALSE, adjCone = PETSC_FALSE, adjClosure = PETSC_TRUE, refDomain = PETSC_FALSE;
4092d0812dedSMatthew G. Knepley   PetscBool      flg, flg2, fflg, strflg, bdfflg, nameflg;
40939318fe57SMatthew G. Knepley   MPI_Comm       comm;
4094ed5e4e85SVaclav Hapla   char           filename[PETSC_MAX_PATH_LEN]   = "<unspecified>";
4095ed5e4e85SVaclav Hapla   char           bdFilename[PETSC_MAX_PATH_LEN] = "<unspecified>";
4096ed5e4e85SVaclav Hapla   char           plexname[PETSC_MAX_PATH_LEN]   = "";
40974e22dd4cSMatthew G. Knepley   const char    *option;
40989318fe57SMatthew G. Knepley 
40999318fe57SMatthew G. Knepley   PetscFunctionBegin;
4100708be2fdSJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_CreateFromOptions, dm, 0, 0, 0));
41019566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
41029318fe57SMatthew G. Knepley   /* TODO Turn this into a registration interface */
41039566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_filename", "File containing a mesh", "DMPlexCreateFromFile", filename, filename, sizeof(filename), &fflg));
4104d0812dedSMatthew G. Knepley   PetscCall(PetscOptionsString("-dm_plex_file_contents", "Contents of a file format in a string", "DMPlexCreateFromFile", filename, filename, sizeof(filename), &strflg));
41059566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_boundary_filename", "File containing a mesh boundary", "DMPlexCreateFromFile", bdFilename, bdFilename, sizeof(bdFilename), &bdfflg));
41069566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_name", "Name of the mesh in the file", "DMPlexCreateFromFile", plexname, plexname, sizeof(plexname), &nameflg));
41079566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_plex_cell", "Cell shape", "", DMPolytopeTypes, (PetscEnum)cell, (PetscEnum *)&cell, NULL));
41089566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_reference_cell_domain", "Use a reference cell domain", "", refDomain, &refDomain, NULL));
41099566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_plex_shape", "Shape for built-in mesh", "", DMPlexShapes, (PetscEnum)shape, (PetscEnum *)&shape, &flg));
41109566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_dim", "Topological dimension of the mesh", "DMGetDimension", dim, &dim, &flg, 0));
41119566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_simplex", "Mesh cell shape", "", simplex, &simplex, &flg));
41129566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_interpolate", "Flag to create edges and faces automatically", "", interpolate, &interpolate, &flg));
4113*b9da1bb3SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_plex_orient", "Orient the constructed mesh", "DMPlexOrient", orient, &orient, &flg));
41149566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_adj_cone", "Set adjacency direction", "DMSetBasicAdjacency", adjCone, &adjCone, &flg));
41159566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_adj_closure", "Set adjacency size", "DMSetBasicAdjacency", adjClosure, &adjClosure, &flg2));
41169566063dSJacob Faibussowitsch   if (flg || flg2) PetscCall(DMSetBasicAdjacency(dm, adjCone, adjClosure));
41179318fe57SMatthew G. Knepley 
411861a622f3SMatthew G. Knepley   switch (cell) {
411961a622f3SMatthew G. Knepley   case DM_POLYTOPE_POINT:
412061a622f3SMatthew G. Knepley   case DM_POLYTOPE_SEGMENT:
412161a622f3SMatthew G. Knepley   case DM_POLYTOPE_POINT_PRISM_TENSOR:
412261a622f3SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
412361a622f3SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
412461a622f3SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
4125d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_HEXAHEDRON:
4126d71ae5a4SJacob Faibussowitsch     *useCoordSpace = PETSC_TRUE;
4127d71ae5a4SJacob Faibussowitsch     break;
4128d71ae5a4SJacob Faibussowitsch   default:
4129d71ae5a4SJacob Faibussowitsch     *useCoordSpace = PETSC_FALSE;
4130d71ae5a4SJacob Faibussowitsch     break;
413161a622f3SMatthew G. Knepley   }
413261a622f3SMatthew G. Knepley 
41339318fe57SMatthew G. Knepley   if (fflg) {
41349318fe57SMatthew G. Knepley     DM          dmnew;
41351e4a82c4SMatthew G. Knepley     const char *name;
41369318fe57SMatthew G. Knepley 
41371e4a82c4SMatthew G. Knepley     PetscCall(PetscObjectGetName((PetscObject)dm, &name));
41381e4a82c4SMatthew G. Knepley     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), filename, nameflg ? plexname : name, interpolate, &dmnew));
41395de52c6dSVaclav Hapla     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
414069d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &dmnew));
41419318fe57SMatthew G. Knepley   } else if (refDomain) {
41429566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateReferenceCell_Internal(dm, cell));
41439318fe57SMatthew G. Knepley   } else if (bdfflg) {
41449318fe57SMatthew G. Knepley     DM          bdm, dmnew;
41451e4a82c4SMatthew G. Knepley     const char *name;
41469318fe57SMatthew G. Knepley 
41471e4a82c4SMatthew G. Knepley     PetscCall(PetscObjectGetName((PetscObject)dm, &name));
41481e4a82c4SMatthew G. Knepley     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), bdFilename, nameflg ? plexname : name, interpolate, &bdm));
41499566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)bdm, "bd_"));
41509566063dSJacob Faibussowitsch     PetscCall(DMSetFromOptions(bdm));
41519566063dSJacob Faibussowitsch     PetscCall(DMPlexGenerate(bdm, NULL, interpolate, &dmnew));
41529566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&bdm));
41535de52c6dSVaclav Hapla     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
415469d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &dmnew));
4155d0812dedSMatthew G. Knepley   } else if (strflg) {
4156d0812dedSMatthew G. Knepley     DM          dmnew;
4157d0812dedSMatthew G. Knepley     PetscViewer viewer;
4158d0812dedSMatthew G. Knepley     const char *contents;
4159d0812dedSMatthew G. Knepley     char       *strname;
4160d0812dedSMatthew G. Knepley     char        tmpdir[PETSC_MAX_PATH_LEN];
4161d0812dedSMatthew G. Knepley     char        tmpfilename[PETSC_MAX_PATH_LEN];
4162d0812dedSMatthew G. Knepley     char        name[PETSC_MAX_PATH_LEN];
4163d0812dedSMatthew G. Knepley     MPI_Comm    comm;
4164d0812dedSMatthew G. Knepley     PetscMPIInt rank;
4165d0812dedSMatthew G. Knepley 
4166d0812dedSMatthew G. Knepley     PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4167d0812dedSMatthew G. Knepley     PetscCallMPI(MPI_Comm_rank(comm, &rank));
4168d0812dedSMatthew G. Knepley     PetscCall(PetscStrchr(filename, ':', &strname));
4169d0812dedSMatthew G. Knepley     PetscCheck(strname, comm, PETSC_ERR_ARG_WRONG, "File contents must have the form \"ext:string_name\", not %s", filename);
4170d0812dedSMatthew G. Knepley     strname[0] = '\0';
4171d0812dedSMatthew G. Knepley     ++strname;
4172d0812dedSMatthew G. Knepley     PetscCall(PetscDLSym(NULL, strname, (void **)&contents));
4173d0812dedSMatthew G. Knepley     PetscCheck(contents, comm, PETSC_ERR_ARG_WRONG, "Could not locate mesh string %s", strname);
4174d0812dedSMatthew G. Knepley     PetscCall(PetscGetTmp(comm, tmpdir, PETSC_MAX_PATH_LEN));
4175ed32af8cSMatthew G. Knepley     PetscCall(PetscStrlcat(tmpdir, "/meshXXXXXX", PETSC_MAX_PATH_LEN));
4176ed32af8cSMatthew G. Knepley     PetscCall(PetscMkdtemp(tmpdir));
4177ed32af8cSMatthew G. Knepley     PetscCall(PetscSNPrintf(tmpfilename, PETSC_MAX_PATH_LEN, "%s/mesh.%s", tmpdir, filename));
4178d0812dedSMatthew G. Knepley     PetscCall(PetscViewerASCIIOpen(comm, tmpfilename, &viewer));
4179d0812dedSMatthew G. Knepley     PetscCall(PetscViewerASCIIPrintf(viewer, "%s\n", contents));
4180d0812dedSMatthew G. Knepley     PetscCall(PetscViewerDestroy(&viewer));
4181d0812dedSMatthew G. Knepley     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), tmpfilename, plexname, interpolate, &dmnew));
4182ed32af8cSMatthew G. Knepley     PetscCall(PetscRMTree(tmpdir));
4183d0812dedSMatthew G. Knepley     PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN, "%s Mesh", strname));
4184d0812dedSMatthew G. Knepley     PetscCall(PetscObjectSetName((PetscObject)dm, name));
4185d0812dedSMatthew G. Knepley     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
4186d0812dedSMatthew G. Knepley     PetscCall(DMPlexReplace_Internal(dm, &dmnew));
41879318fe57SMatthew G. Knepley   } else {
41889566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)dm, DMPlexShapes[shape]));
41899318fe57SMatthew G. Knepley     switch (shape) {
4190669647acSMatthew G. Knepley     case DM_SHAPE_BOX:
41915dca41c3SJed Brown     case DM_SHAPE_ZBOX:
4192669647acSMatthew G. Knepley     case DM_SHAPE_ANNULUS: {
41939318fe57SMatthew G. Knepley       PetscInt       faces[3]  = {0, 0, 0};
41949318fe57SMatthew G. Knepley       PetscReal      lower[3]  = {0, 0, 0};
41959318fe57SMatthew G. Knepley       PetscReal      upper[3]  = {1, 1, 1};
41969318fe57SMatthew G. Knepley       DMBoundaryType bdt[3]    = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
4197669647acSMatthew G. Knepley       PetscBool      isAnnular = shape == DM_SHAPE_ANNULUS ? PETSC_TRUE : PETSC_FALSE;
41989318fe57SMatthew G. Knepley       PetscInt       i, n;
41999318fe57SMatthew G. Knepley 
42009318fe57SMatthew G. Knepley       n = dim;
42019318fe57SMatthew G. Knepley       for (i = 0; i < dim; ++i) faces[i] = (dim == 1 ? 1 : 4 - dim);
42029566063dSJacob Faibussowitsch       PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg));
42039318fe57SMatthew G. Knepley       n = 3;
42049566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
420563a3b9bcSJacob Faibussowitsch       PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Lower box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
42069318fe57SMatthew G. Knepley       n = 3;
42079566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
420863a3b9bcSJacob Faibussowitsch       PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Upper box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
42099318fe57SMatthew G. Knepley       n = 3;
42109566063dSJacob Faibussowitsch       PetscCall(PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *)bdt, &n, &flg));
421163a3b9bcSJacob Faibussowitsch       PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Box boundary types had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
4212669647acSMatthew G. Knepley 
4213669647acSMatthew G. Knepley       PetscCheck(!isAnnular || dim == 2, comm, PETSC_ERR_ARG_OUTOFRANGE, "Only two dimensional annuli have been implemented");
4214669647acSMatthew G. Knepley       if (isAnnular)
4215669647acSMatthew G. Knepley         for (i = 0; i < dim - 1; ++i) bdt[i] = DM_BOUNDARY_PERIODIC;
4216669647acSMatthew G. Knepley 
42179318fe57SMatthew G. Knepley       switch (cell) {
421861a622f3SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
42199566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateWedgeBoxMesh_Internal(dm, faces, lower, upper, bdt));
4220d410b0cfSMatthew G. Knepley         if (!interpolate) {
4221d410b0cfSMatthew G. Knepley           DM udm;
4222d410b0cfSMatthew G. Knepley 
42239566063dSJacob Faibussowitsch           PetscCall(DMPlexUninterpolate(dm, &udm));
422469d8a87bSksagiyam           PetscCall(DMPlexReplace_Internal(dm, &udm));
4225d410b0cfSMatthew G. Knepley         }
42269318fe57SMatthew G. Knepley         break;
4227d71ae5a4SJacob Faibussowitsch       default:
42285dca41c3SJed Brown         PetscCall(DMPlexCreateBoxMesh_Internal(dm, shape, dim, simplex, faces, lower, upper, bdt, interpolate));
4229d71ae5a4SJacob Faibussowitsch         break;
42309318fe57SMatthew G. Knepley       }
4231669647acSMatthew G. Knepley       if (isAnnular) {
4232669647acSMatthew G. Knepley         DM          cdm;
4233669647acSMatthew G. Knepley         PetscDS     cds;
4234669647acSMatthew G. Knepley         PetscScalar bounds[2] = {lower[0], upper[0]};
4235669647acSMatthew G. Knepley 
4236669647acSMatthew G. Knepley         // Fix coordinates for annular region
4237669647acSMatthew G. Knepley         PetscCall(DMSetPeriodicity(dm, NULL, NULL, NULL));
4238669647acSMatthew G. Knepley         PetscCall(DMSetCellCoordinatesLocal(dm, NULL));
4239669647acSMatthew G. Knepley         PetscCall(DMSetCellCoordinates(dm, NULL));
4240e44f6aebSMatthew G. Knepley         PetscCall(DMPlexCreateCoordinateSpace(dm, 1, PETSC_TRUE, NULL));
4241669647acSMatthew G. Knepley         PetscCall(DMGetCoordinateDM(dm, &cdm));
4242669647acSMatthew G. Knepley         PetscCall(DMGetDS(cdm, &cds));
4243669647acSMatthew G. Knepley         PetscCall(PetscDSSetConstants(cds, 2, bounds));
4244669647acSMatthew G. Knepley         PetscCall(DMPlexRemapGeometry(dm, 0.0, boxToAnnulus));
4245669647acSMatthew G. Knepley       }
42469371c9d4SSatish Balay     } break;
42479371c9d4SSatish Balay     case DM_SHAPE_BOX_SURFACE: {
42489318fe57SMatthew G. Knepley       PetscInt  faces[3] = {0, 0, 0};
42499318fe57SMatthew G. Knepley       PetscReal lower[3] = {0, 0, 0};
42509318fe57SMatthew G. Knepley       PetscReal upper[3] = {1, 1, 1};
42519318fe57SMatthew G. Knepley       PetscInt  i, n;
42529318fe57SMatthew G. Knepley 
42539318fe57SMatthew G. Knepley       n = dim + 1;
42549318fe57SMatthew G. Knepley       for (i = 0; i < dim + 1; ++i) faces[i] = (dim + 1 == 1 ? 1 : 4 - (dim + 1));
42559566063dSJacob Faibussowitsch       PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg));
42569318fe57SMatthew G. Knepley       n = 3;
42579566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
425863a3b9bcSJacob 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);
42599318fe57SMatthew G. Knepley       n = 3;
42609566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
426163a3b9bcSJacob 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);
42629566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(dm, dim + 1, faces, lower, upper, interpolate));
42639371c9d4SSatish Balay     } break;
42649371c9d4SSatish Balay     case DM_SHAPE_SPHERE: {
42659318fe57SMatthew G. Knepley       PetscReal R = 1.0;
42669318fe57SMatthew G. Knepley 
42679566063dSJacob Faibussowitsch       PetscCall(PetscOptionsReal("-dm_plex_sphere_radius", "Radius of the sphere", "", R, &R, &flg));
42689566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateSphereMesh_Internal(dm, dim, simplex, R));
42699371c9d4SSatish Balay     } break;
42709371c9d4SSatish Balay     case DM_SHAPE_BALL: {
42719318fe57SMatthew G. Knepley       PetscReal R = 1.0;
42729318fe57SMatthew G. Knepley 
42739566063dSJacob Faibussowitsch       PetscCall(PetscOptionsReal("-dm_plex_ball_radius", "Radius of the ball", "", R, &R, &flg));
42749566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateBallMesh_Internal(dm, dim, R));
42759371c9d4SSatish Balay     } break;
42769371c9d4SSatish Balay     case DM_SHAPE_CYLINDER: {
42779318fe57SMatthew G. Knepley       DMBoundaryType bdt = DM_BOUNDARY_NONE;
42789318fe57SMatthew G. Knepley       PetscInt       Nw  = 6;
427949704ca5SMatthew G. Knepley       PetscInt       Nr  = 0;
42809318fe57SMatthew G. Knepley 
42819566063dSJacob Faibussowitsch       PetscCall(PetscOptionsEnum("-dm_plex_cylinder_bd", "Boundary type in the z direction", "", DMBoundaryTypes, (PetscEnum)bdt, (PetscEnum *)&bdt, NULL));
42829566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-dm_plex_cylinder_num_wedges", "Number of wedges around the cylinder", "", Nw, &Nw, NULL));
428349704ca5SMatthew G. Knepley       PetscCall(PetscOptionsInt("-dm_plex_cylinder_num_refine", "Number of refinements before projection", "", Nr, &Nr, NULL));
42849318fe57SMatthew G. Knepley       switch (cell) {
4285d71ae5a4SJacob Faibussowitsch       case DM_POLYTOPE_TRI_PRISM_TENSOR:
4286d71ae5a4SJacob Faibussowitsch         PetscCall(DMPlexCreateWedgeCylinderMesh_Internal(dm, Nw, interpolate));
4287d71ae5a4SJacob Faibussowitsch         break;
4288d71ae5a4SJacob Faibussowitsch       default:
428949704ca5SMatthew G. Knepley         PetscCall(DMPlexCreateHexCylinderMesh_Internal(dm, bdt, Nr));
4290d71ae5a4SJacob Faibussowitsch         break;
42919318fe57SMatthew G. Knepley       }
42929371c9d4SSatish Balay     } break;
4293b7f5c055SJed Brown     case DM_SHAPE_SCHWARZ_P: // fallthrough
42949371c9d4SSatish Balay     case DM_SHAPE_GYROID: {
4295b7f5c055SJed Brown       PetscInt       extent[3] = {1, 1, 1}, refine = 0, layers = 0, three;
4296b7f5c055SJed Brown       PetscReal      thickness   = 0.;
4297b7f5c055SJed Brown       DMBoundaryType periodic[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
4298b7f5c055SJed Brown       DMPlexTPSType  tps_type    = shape == DM_SHAPE_SCHWARZ_P ? DMPLEX_TPS_SCHWARZ_P : DMPLEX_TPS_GYROID;
42991436d7faSJed Brown       PetscBool      tps_distribute;
43009566063dSJacob Faibussowitsch       PetscCall(PetscOptionsIntArray("-dm_plex_tps_extent", "Number of replicas for each of three dimensions", NULL, extent, (three = 3, &three), NULL));
43019566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-dm_plex_tps_refine", "Number of refinements", NULL, refine, &refine, NULL));
43029566063dSJacob Faibussowitsch       PetscCall(PetscOptionsEnumArray("-dm_plex_tps_periodic", "Periodicity in each of three dimensions", NULL, DMBoundaryTypes, (PetscEnum *)periodic, (three = 3, &three), NULL));
43039566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-dm_plex_tps_layers", "Number of layers in volumetric extrusion (or zero to not extrude)", NULL, layers, &layers, NULL));
43049566063dSJacob Faibussowitsch       PetscCall(PetscOptionsReal("-dm_plex_tps_thickness", "Thickness of volumetric extrusion", NULL, thickness, &thickness, NULL));
43059566063dSJacob Faibussowitsch       PetscCall(DMPlexDistributeGetDefault(dm, &tps_distribute));
43069566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_tps_distribute", "Distribute the 2D mesh prior to refinement and extrusion", NULL, tps_distribute, &tps_distribute, NULL));
43079566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateTPSMesh_Internal(dm, tps_type, extent, periodic, tps_distribute, refine, layers, thickness));
43089371c9d4SSatish Balay     } break;
43099371c9d4SSatish Balay     case DM_SHAPE_DOUBLET: {
431005bd46c0SStefano Zampini       DM        dmnew;
431105bd46c0SStefano Zampini       PetscReal rl = 0.0;
431205bd46c0SStefano Zampini 
431305bd46c0SStefano Zampini       PetscCall(PetscOptionsReal("-dm_plex_doublet_refinementlimit", "Refinement limit", NULL, rl, &rl, NULL));
431405bd46c0SStefano Zampini       PetscCall(DMPlexCreateDoublet(PetscObjectComm((PetscObject)dm), dim, simplex, interpolate, rl, &dmnew));
43155de52c6dSVaclav Hapla       PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
431669d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &dmnew));
43179371c9d4SSatish Balay     } break;
4318cfb853baSMatthew G. Knepley     case DM_SHAPE_HYPERCUBIC: {
4319cfb853baSMatthew G. Knepley       PetscInt       *edges;
4320cfb853baSMatthew G. Knepley       PetscReal      *lower, *upper;
4321cfb853baSMatthew G. Knepley       DMBoundaryType *bdt;
4322cfb853baSMatthew G. Knepley       PetscInt        n, d;
4323cfb853baSMatthew G. Knepley 
4324cfb853baSMatthew G. Knepley       *useCoordSpace = PETSC_FALSE;
4325cfb853baSMatthew G. Knepley       PetscCall(PetscMalloc4(dim, &edges, dim, &lower, dim, &upper, dim, &bdt));
4326cfb853baSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
4327cfb853baSMatthew G. Knepley         edges[d] = 1;
4328cfb853baSMatthew G. Knepley         lower[d] = 0.;
4329cfb853baSMatthew G. Knepley         upper[d] = 1.;
4330cfb853baSMatthew G. Knepley         bdt[d]   = DM_BOUNDARY_PERIODIC;
4331cfb853baSMatthew G. Knepley       }
4332cfb853baSMatthew G. Knepley       n = dim;
4333cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", edges, &n, &flg));
4334cfb853baSMatthew G. Knepley       n = dim;
4335cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
4336cfb853baSMatthew 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);
4337cfb853baSMatthew G. Knepley       n = dim;
4338cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
4339cfb853baSMatthew 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);
4340cfb853baSMatthew G. Knepley       n = dim;
4341cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *)bdt, &n, &flg));
4342cfb853baSMatthew 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);
4343cfb853baSMatthew G. Knepley       PetscCall(DMPlexCreateHypercubicMesh_Internal(dm, dim, lower, upper, edges, bdt));
4344cfb853baSMatthew G. Knepley       PetscCall(PetscFree4(edges, lower, upper, bdt));
4345cfb853baSMatthew G. Knepley     } break;
4346d71ae5a4SJacob Faibussowitsch     default:
4347d71ae5a4SJacob Faibussowitsch       SETERRQ(comm, PETSC_ERR_SUP, "Domain shape %s is unsupported", DMPlexShapes[shape]);
43489318fe57SMatthew G. Knepley     }
43499318fe57SMatthew G. Knepley   }
43509566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
435148a46eb9SPierre Jolivet   if (!((PetscObject)dm)->name && nameflg) PetscCall(PetscObjectSetName((PetscObject)dm, plexname));
4352*b9da1bb3SMatthew G. Knepley   if (orient) PetscCall(DMPlexOrient(dm));
43534e22dd4cSMatthew G. Knepley   // Allow label creation
43544e22dd4cSMatthew G. Knepley   PetscCall(PetscOptionsFindPairPrefix_Private(NULL, ((PetscObject)dm)->prefix, "-dm_plex_label_", &option, NULL, &flg));
43554e22dd4cSMatthew G. Knepley   if (flg) {
43564e22dd4cSMatthew G. Knepley     DMLabel     label;
43574e22dd4cSMatthew G. Knepley     PetscInt    points[1024], n = 1024;
43584e22dd4cSMatthew G. Knepley     char        fulloption[PETSC_MAX_PATH_LEN];
43594e22dd4cSMatthew G. Knepley     const char *name = &option[14];
43604e22dd4cSMatthew G. Knepley 
43614e22dd4cSMatthew G. Knepley     PetscCall(DMCreateLabel(dm, name));
43624e22dd4cSMatthew G. Knepley     PetscCall(DMGetLabel(dm, name, &label));
43634e22dd4cSMatthew G. Knepley     fulloption[0] = '-';
43644e22dd4cSMatthew G. Knepley     fulloption[1] = 0;
43654e22dd4cSMatthew G. Knepley     PetscCall(PetscStrlcat(fulloption, option, PETSC_MAX_PATH_LEN));
43664e22dd4cSMatthew G. Knepley     PetscCall(PetscOptionsGetIntArray(NULL, ((PetscObject)dm)->prefix, fulloption, points, &n, NULL));
43674e22dd4cSMatthew G. Knepley     for (PetscInt p = 0; p < n; ++p) PetscCall(DMLabelSetValue(label, points[p], 1));
43684e22dd4cSMatthew G. Knepley   }
4369dd0eeac9SMatthew G. Knepley   // Allow cohesive label creation
4370dd0eeac9SMatthew G. Knepley   //   Faces are input, completed, and all points are marked with their depth
4371dd0eeac9SMatthew G. Knepley   PetscCall(PetscOptionsFindPairPrefix_Private(NULL, ((PetscObject)dm)->prefix, "-dm_plex_cohesive_label_", &option, NULL, &flg));
4372dd0eeac9SMatthew G. Knepley   if (flg) {
4373dd0eeac9SMatthew G. Knepley     DMLabel   label;
4374dd0eeac9SMatthew G. Knepley     PetscInt  points[1024], n, pStart, pEnd, Nl = 1;
43755390be7dSMatthew G. Knepley     PetscBool noCreate = PETSC_FALSE;
4376dd0eeac9SMatthew G. Knepley     char      fulloption[PETSC_MAX_PATH_LEN];
4377dd0eeac9SMatthew G. Knepley     char      name[PETSC_MAX_PATH_LEN];
4378dd0eeac9SMatthew G. Knepley     size_t    len;
4379dd0eeac9SMatthew G. Knepley 
4380dd0eeac9SMatthew G. Knepley     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
4381dd0eeac9SMatthew G. Knepley     PetscCall(PetscStrncpy(name, &option[23], PETSC_MAX_PATH_LEN));
4382dd0eeac9SMatthew G. Knepley     PetscCall(PetscStrlen(name, &len));
4383dd0eeac9SMatthew G. Knepley     if (name[len - 1] == '0') Nl = 10;
4384dd0eeac9SMatthew G. Knepley     for (PetscInt l = 0; l < Nl; ++l) {
43856497c311SBarry Smith       if (l > 0) name[len - 1] = (char)('0' + l);
4386dd0eeac9SMatthew G. Knepley       fulloption[0] = 0;
4387dd0eeac9SMatthew G. Knepley       PetscCall(PetscStrlcat(fulloption, "-dm_plex_cohesive_label_", 32));
4388dd0eeac9SMatthew G. Knepley       PetscCall(PetscStrlcat(fulloption, name, PETSC_MAX_PATH_LEN - 32));
4389dd0eeac9SMatthew G. Knepley       n = 1024;
4390dd0eeac9SMatthew G. Knepley       PetscCall(PetscOptionsGetIntArray(NULL, ((PetscObject)dm)->prefix, fulloption, points, &n, &flg));
4391dd0eeac9SMatthew G. Knepley       if (!flg) break;
43925390be7dSMatthew G. Knepley       PetscCall(DMHasLabel(dm, name, &noCreate));
43935390be7dSMatthew G. Knepley       if (noCreate) {
43945390be7dSMatthew G. Knepley         DMLabel         inlabel;
43955390be7dSMatthew G. Knepley         IS              pointIS;
43965390be7dSMatthew G. Knepley         const PetscInt *lpoints;
43975390be7dSMatthew G. Knepley         PetscInt        pdep, ln, inval = points[0];
43985390be7dSMatthew G. Knepley         char            newname[PETSC_MAX_PATH_LEN];
43995390be7dSMatthew G. Knepley 
44005390be7dSMatthew G. Knepley         PetscCheck(n == 1, comm, PETSC_ERR_ARG_WRONG, "Must specify a label value with this option");
44015390be7dSMatthew G. Knepley         PetscCall(DMGetLabel(dm, name, &inlabel));
44025390be7dSMatthew G. Knepley         PetscCall(DMLabelGetStratumIS(inlabel, inval, &pointIS));
44035390be7dSMatthew G. Knepley         PetscCall(ISGetLocalSize(pointIS, &ln));
44045390be7dSMatthew G. Knepley         PetscCall(ISGetIndices(pointIS, &lpoints));
44055390be7dSMatthew G. Knepley         PetscCall(DMPlexGetPointDepth(dm, lpoints[0], &pdep));
44065390be7dSMatthew G. Knepley         PetscCall(PetscSNPrintf(newname, PETSC_MAX_PATH_LEN, "%s%" PetscInt_FMT, name, points[0]));
44075390be7dSMatthew G. Knepley         PetscCall(DMCreateLabel(dm, newname));
44085390be7dSMatthew G. Knepley         PetscCall(DMGetLabel(dm, newname, &label));
44095390be7dSMatthew G. Knepley         if (!pdep) PetscCall(ProcessCohesiveLabel_Vertices(dm, label, inlabel, inval, ln, lpoints));
44105390be7dSMatthew G. Knepley         else PetscCall(ProcessCohesiveLabel_Faces(dm, label, ln, lpoints));
44115390be7dSMatthew G. Knepley         PetscCall(ISRestoreIndices(pointIS, &lpoints));
44125390be7dSMatthew G. Knepley         PetscCall(ISDestroy(&pointIS));
44135390be7dSMatthew G. Knepley       } else {
4414dd0eeac9SMatthew G. Knepley         PetscCall(DMCreateLabel(dm, name));
4415dd0eeac9SMatthew G. Knepley         PetscCall(DMGetLabel(dm, name, &label));
4416dd0eeac9SMatthew G. Knepley         if (pStart >= pEnd) n = 0;
44175390be7dSMatthew G. Knepley         PetscCall(ProcessCohesiveLabel_Faces(dm, label, n, points));
4418dd0eeac9SMatthew G. Knepley       }
4419dd0eeac9SMatthew G. Knepley       PetscCall(DMPlexOrientLabel(dm, label));
44200542aa8cSMatthew G. Knepley       PetscCall(DMPlexLabelCohesiveComplete(dm, label, NULL, 1, PETSC_FALSE, PETSC_FALSE, NULL));
4421dd0eeac9SMatthew G. Knepley     }
4422dd0eeac9SMatthew G. Knepley   }
44235390be7dSMatthew G. Knepley   PetscCall(DMViewFromOptions(dm, NULL, "-created_dm_view"));
4424708be2fdSJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_CreateFromOptions, dm, 0, 0, 0));
44253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44260a6ba040SMatthew G. Knepley }
44270a6ba040SMatthew G. Knepley 
4428d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions_NonRefinement_Plex(DM dm, PetscOptionItems *PetscOptionsObject)
4429d71ae5a4SJacob Faibussowitsch {
44300a6ba040SMatthew G. Knepley   DM_Plex  *mesh = (DM_Plex *)dm->data;
44317f9d8d6cSVaclav Hapla   PetscBool flg, flg2;
44329318fe57SMatthew G. Knepley   char      bdLabel[PETSC_MAX_PATH_LEN];
4433adc21957SMatthew G. Knepley   char      method[PETSC_MAX_PATH_LEN];
44340a6ba040SMatthew G. Knepley 
44350a6ba040SMatthew G. Knepley   PetscFunctionBegin;
44360a6ba040SMatthew G. Knepley   /* Handle viewing */
44379566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_print_set_values", "Output all set values info", "DMPlexMatSetClosure", PETSC_FALSE, &mesh->printSetValues, NULL));
44385962854dSMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_fem", "Debug output level for all fem computations", "DMPlexSNESComputeResidualFEM", 0, &mesh->printFEM, NULL, 0));
44395962854dSMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_fvm", "Debug output level for all fvm computations", "DMPlexSNESComputeResidualFVM", 0, &mesh->printFVM, NULL, 0));
44409566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-dm_plex_print_tol", "Tolerance for FEM output", "DMPlexSNESComputeResidualFEM", mesh->printTol, &mesh->printTol, NULL));
44419566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_l2", "Debug output level all L2 diff computations", "DMComputeL2Diff", 0, &mesh->printL2, NULL, 0));
4442f5867de0SMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_locate", "Debug output level all point location computations", "DMLocatePoints", 0, &mesh->printLocate, NULL, 0));
4443a77a5016SMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_project", "Debug output level all projection computations", "DMPlexProject", 0, &mesh->printProject, NULL, 0));
44449566063dSJacob Faibussowitsch   PetscCall(DMMonitorSetFromOptions(dm, "-dm_plex_monitor_throughput", "Monitor the simulation throughput", "DMPlexMonitorThroughput", DMPlexMonitorThroughput, NULL, &flg));
44459566063dSJacob Faibussowitsch   if (flg) PetscCall(PetscLogDefaultBegin());
44469318fe57SMatthew G. Knepley   /* Labeling */
44479566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_boundary_label", "Label to mark the mesh boundary", "", bdLabel, bdLabel, sizeof(bdLabel), &flg));
44489566063dSJacob Faibussowitsch   if (flg) PetscCall(DMPlexCreateBoundaryLabel_Private(dm, bdLabel));
4449953fc75cSMatthew G. Knepley   /* Point Location */
44509566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_hash_location", "Use grid hashing for point location", "DMInterpolate", PETSC_FALSE, &mesh->useHashLocation, NULL));
44510848f4b5SMatthew G. Knepley   /* Partitioning and distribution */
44529566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_partition_balance", "Attempt to evenly divide points on partition boundary between processes", "DMPlexSetPartitionBalance", PETSC_FALSE, &mesh->partitionBalance, NULL));
4453d02c7345SMatthew G. Knepley   /* Reordering */
4454adc21957SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_reorder_section", "Compute point permutation for local section", "DMReorderSectionSetDefault", PETSC_FALSE, &flg2, &flg));
4455adc21957SMatthew G. Knepley   if (flg) PetscCall(DMReorderSectionSetDefault(dm, flg2 ? DM_REORDER_DEFAULT_TRUE : DM_REORDER_DEFAULT_FALSE));
4456adc21957SMatthew G. Knepley   PetscCall(PetscOptionsString("-dm_reorder_section_type", "Reordering method for local section", "DMReorderSectionSetType", method, method, PETSC_MAX_PATH_LEN, &flg));
4457adc21957SMatthew G. Knepley   if (flg) PetscCall(DMReorderSectionSetType(dm, method));
44582e62ab5aSMatthew G. Knepley   /* Generation and remeshing */
44599566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_remesh_bd", "Allow changes to the boundary on remeshing", "DMAdapt", PETSC_FALSE, &mesh->remeshBd, NULL));
4460b29cfa1cSToby Isaac   /* Projection behavior */
4461d5b43468SJose E. Roman   PetscCall(PetscOptionsBoundedInt("-dm_plex_max_projection_height", "Maximum mesh point height used to project locally", "DMPlexSetMaxProjectionHeight", 0, &mesh->maxProjectionHeight, NULL, 0));
44629566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_regular_refinement", "Use special nested projection algorithm for regular refinement", "DMPlexSetRegularRefinement", mesh->regularRefinement, &mesh->regularRefinement, NULL));
4463f12cf164SMatthew G. Knepley   /* Checking structure */
4464f12cf164SMatthew G. Knepley   {
44657f9d8d6cSVaclav Hapla     PetscBool all = PETSC_FALSE;
4466f12cf164SMatthew G. Knepley 
44677f9d8d6cSVaclav Hapla     PetscCall(PetscOptionsBool("-dm_plex_check_all", "Perform all basic checks", "DMPlexCheck", PETSC_FALSE, &all, NULL));
44687f9d8d6cSVaclav Hapla     if (all) {
44697f9d8d6cSVaclav Hapla       PetscCall(DMPlexCheck(dm));
44707f9d8d6cSVaclav Hapla     } else {
44719566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_symmetry", "Check that the adjacency information in the mesh is symmetric", "DMPlexCheckSymmetry", PETSC_FALSE, &flg, &flg2));
44727f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckSymmetry(dm));
44739566063dSJacob 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));
44747f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckSkeleton(dm, 0));
44759566063dSJacob 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));
44767f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckFaces(dm, 0));
44779566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_geometry", "Check that cells have positive volume", "DMPlexCheckGeometry", PETSC_FALSE, &flg, &flg2));
44787f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckGeometry(dm));
44799566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_pointsf", "Check some necessary conditions for PointSF", "DMPlexCheckPointSF", PETSC_FALSE, &flg, &flg2));
4480d7d32a9aSMatthew G. Knepley       if (flg && flg2) PetscCall(DMPlexCheckPointSF(dm, NULL, PETSC_FALSE));
44819566063dSJacob 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));
44827f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckInterfaceCones(dm));
44837f9d8d6cSVaclav Hapla     }
44849566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-dm_plex_check_cell_shape", "Check cell shape", "DMPlexCheckCellShape", PETSC_FALSE, &flg, &flg2));
44859566063dSJacob Faibussowitsch     if (flg && flg2) PetscCall(DMPlexCheckCellShape(dm, PETSC_TRUE, PETSC_DETERMINE));
4486f12cf164SMatthew G. Knepley   }
44879318fe57SMatthew G. Knepley   {
44889318fe57SMatthew G. Knepley     PetscReal scale = 1.0;
44894f3833eaSMatthew G. Knepley 
44909566063dSJacob Faibussowitsch     PetscCall(PetscOptionsReal("-dm_plex_scale", "Scale factor for mesh coordinates", "DMPlexScale", scale, &scale, &flg));
44919318fe57SMatthew G. Knepley     if (flg) {
44929318fe57SMatthew G. Knepley       Vec coordinates, coordinatesLocal;
44939318fe57SMatthew G. Knepley 
44949566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinates(dm, &coordinates));
44959566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinatesLocal(dm, &coordinatesLocal));
44969566063dSJacob Faibussowitsch       PetscCall(VecScale(coordinates, scale));
44979566063dSJacob Faibussowitsch       PetscCall(VecScale(coordinatesLocal, scale));
44989318fe57SMatthew G. Knepley     }
44999318fe57SMatthew G. Knepley   }
45009566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerSetFromOptions(mesh->partitioner));
45013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
450268d4fef7SMatthew G. Knepley }
450368d4fef7SMatthew G. Knepley 
4504d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions_Overlap_Plex(DM dm, PetscOptionItems *PetscOptionsObject, PetscInt *overlap)
4505d71ae5a4SJacob Faibussowitsch {
4506c506a872SMatthew G. Knepley   PetscInt  numOvLabels = 16, numOvExLabels = 16;
4507c506a872SMatthew G. Knepley   char     *ovLabelNames[16], *ovExLabelNames[16];
4508c506a872SMatthew G. Knepley   PetscInt  numOvValues = 16, numOvExValues = 16, l;
4509c506a872SMatthew G. Knepley   PetscBool flg;
4510c506a872SMatthew G. Knepley 
4511c506a872SMatthew G. Knepley   PetscFunctionBegin;
4512c506a872SMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_distribute_overlap", "The size of the overlap halo", "DMPlexDistribute", *overlap, overlap, NULL, 0));
4513c506a872SMatthew G. Knepley   PetscCall(PetscOptionsStringArray("-dm_distribute_overlap_labels", "List of overlap label names", "DMPlexDistribute", ovLabelNames, &numOvLabels, &flg));
4514c506a872SMatthew G. Knepley   if (!flg) numOvLabels = 0;
4515c506a872SMatthew G. Knepley   if (numOvLabels) {
4516c506a872SMatthew G. Knepley     ((DM_Plex *)dm->data)->numOvLabels = numOvLabels;
4517c506a872SMatthew G. Knepley     for (l = 0; l < numOvLabels; ++l) {
4518c506a872SMatthew G. Knepley       PetscCall(DMGetLabel(dm, ovLabelNames[l], &((DM_Plex *)dm->data)->ovLabels[l]));
4519c506a872SMatthew G. Knepley       PetscCheck(((DM_Plex *)dm->data)->ovLabels[l], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid label name %s", ovLabelNames[l]);
4520c506a872SMatthew G. Knepley       PetscCall(PetscFree(ovLabelNames[l]));
4521c506a872SMatthew G. Knepley     }
4522c506a872SMatthew G. Knepley     PetscCall(PetscOptionsIntArray("-dm_distribute_overlap_values", "List of overlap label values", "DMPlexDistribute", ((DM_Plex *)dm->data)->ovValues, &numOvValues, &flg));
4523c506a872SMatthew G. Knepley     if (!flg) numOvValues = 0;
4524c506a872SMatthew 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);
4525c506a872SMatthew G. Knepley 
4526c506a872SMatthew G. Knepley     PetscCall(PetscOptionsStringArray("-dm_distribute_overlap_exclude_labels", "List of overlap exclude label names", "DMPlexDistribute", ovExLabelNames, &numOvExLabels, &flg));
4527c506a872SMatthew G. Knepley     if (!flg) numOvExLabels = 0;
4528c506a872SMatthew G. Knepley     ((DM_Plex *)dm->data)->numOvExLabels = numOvExLabels;
4529c506a872SMatthew G. Knepley     for (l = 0; l < numOvExLabels; ++l) {
4530c506a872SMatthew G. Knepley       PetscCall(DMGetLabel(dm, ovExLabelNames[l], &((DM_Plex *)dm->data)->ovExLabels[l]));
4531c506a872SMatthew G. Knepley       PetscCheck(((DM_Plex *)dm->data)->ovExLabels[l], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid label name %s", ovExLabelNames[l]);
4532c506a872SMatthew G. Knepley       PetscCall(PetscFree(ovExLabelNames[l]));
4533c506a872SMatthew G. Knepley     }
4534c506a872SMatthew G. Knepley     PetscCall(PetscOptionsIntArray("-dm_distribute_overlap_exclude_values", "List of overlap exclude label values", "DMPlexDistribute", ((DM_Plex *)dm->data)->ovExValues, &numOvExValues, &flg));
4535c506a872SMatthew G. Knepley     if (!flg) numOvExValues = 0;
4536c506a872SMatthew 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);
4537c506a872SMatthew G. Knepley   }
45383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4539c506a872SMatthew G. Knepley }
4540c506a872SMatthew G. Knepley 
4541d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetFromOptions_Plex(DM dm, PetscOptionItems *PetscOptionsObject)
4542d71ae5a4SJacob Faibussowitsch {
4543bdf63967SMatthew G. Knepley   PetscFunctionList    ordlist;
4544bdf63967SMatthew G. Knepley   char                 oname[256];
45454e22dd4cSMatthew G. Knepley   char                 sublabelname[PETSC_MAX_PATH_LEN] = "";
4546adc21957SMatthew G. Knepley   DMReorderDefaultFlag reorder;
4547d410b0cfSMatthew G. Knepley   PetscReal            volume    = -1.0;
45489318fe57SMatthew G. Knepley   PetscInt             prerefine = 0, refine = 0, r, coarsen = 0, overlap = 0, extLayers = 0, dim;
45491b742f01SMatthew 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;
455068d4fef7SMatthew G. Knepley 
455168d4fef7SMatthew G. Knepley   PetscFunctionBegin;
4552d0609cedSBarry Smith   PetscOptionsHeadBegin(PetscOptionsObject, "DMPlex Options");
4553dd4c3f67SMatthew G. Knepley   if (dm->cloneOpts) goto non_refine;
45549318fe57SMatthew G. Knepley   /* Handle automatic creation */
45559566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
45566bc1bd01Sksagiyam   if (dim < 0) {
45576bc1bd01Sksagiyam     PetscCall(DMPlexCreateFromOptions_Internal(PetscOptionsObject, &coordSpace, dm));
45586bc1bd01Sksagiyam     created = PETSC_TRUE;
45596bc1bd01Sksagiyam   }
45606bc1bd01Sksagiyam   PetscCall(DMGetDimension(dm, &dim));
4561d89e6e46SMatthew G. Knepley   /* Handle interpolation before distribution */
45629566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_interpolate_pre", "Flag to interpolate mesh before distribution", "", interpolate, &interpolate, &flg));
4563d89e6e46SMatthew G. Knepley   if (flg) {
4564d89e6e46SMatthew G. Knepley     DMPlexInterpolatedFlag interpolated;
4565d89e6e46SMatthew G. Knepley 
45669566063dSJacob Faibussowitsch     PetscCall(DMPlexIsInterpolated(dm, &interpolated));
4567d89e6e46SMatthew G. Knepley     if (interpolated == DMPLEX_INTERPOLATED_FULL && !interpolate) {
4568d89e6e46SMatthew G. Knepley       DM udm;
4569d89e6e46SMatthew G. Knepley 
45709566063dSJacob Faibussowitsch       PetscCall(DMPlexUninterpolate(dm, &udm));
457169d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &udm));
4572d89e6e46SMatthew G. Knepley     } else if (interpolated != DMPLEX_INTERPOLATED_FULL && interpolate) {
4573d89e6e46SMatthew G. Knepley       DM idm;
4574d89e6e46SMatthew G. Knepley 
45759566063dSJacob Faibussowitsch       PetscCall(DMPlexInterpolate(dm, &idm));
457669d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &idm));
4577d89e6e46SMatthew G. Knepley     }
4578d89e6e46SMatthew G. Knepley   }
45794e22dd4cSMatthew G. Knepley   // Handle submesh selection before distribution
45804e22dd4cSMatthew G. Knepley   PetscCall(PetscOptionsString("-dm_plex_submesh", "Label to use for submesh selection", "", sublabelname, sublabelname, PETSC_MAX_PATH_LEN, &flg));
45814e22dd4cSMatthew G. Knepley   if (flg) {
45824e22dd4cSMatthew G. Knepley     DM              subdm;
45834e22dd4cSMatthew G. Knepley     DMLabel         label;
45844e22dd4cSMatthew G. Knepley     IS              valueIS, pointIS;
45854e22dd4cSMatthew G. Knepley     const PetscInt *values, *points;
45864e22dd4cSMatthew G. Knepley     PetscBool       markedFaces = PETSC_FALSE;
45874e22dd4cSMatthew G. Knepley     PetscInt        Nv, value, Np;
45884e22dd4cSMatthew G. Knepley 
45894e22dd4cSMatthew G. Knepley     PetscCall(DMGetLabel(dm, sublabelname, &label));
45904e22dd4cSMatthew G. Knepley     PetscCall(DMLabelGetNumValues(label, &Nv));
45914e22dd4cSMatthew 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);
45924e22dd4cSMatthew G. Knepley     PetscCall(DMLabelGetValueIS(label, &valueIS));
45934e22dd4cSMatthew G. Knepley     PetscCall(ISGetIndices(valueIS, &values));
45944e22dd4cSMatthew G. Knepley     value = values[0];
45954e22dd4cSMatthew G. Knepley     PetscCall(ISRestoreIndices(valueIS, &values));
45964e22dd4cSMatthew G. Knepley     PetscCall(ISDestroy(&valueIS));
45974e22dd4cSMatthew G. Knepley     PetscCall(DMLabelGetStratumSize(label, value, &Np));
45984e22dd4cSMatthew G. Knepley     PetscCall(DMLabelGetStratumIS(label, value, &pointIS));
45994e22dd4cSMatthew G. Knepley     PetscCall(ISGetIndices(pointIS, &points));
46004e22dd4cSMatthew G. Knepley     for (PetscInt p = 0; p < Np; ++p) {
46014e22dd4cSMatthew G. Knepley       PetscInt pdepth;
46024e22dd4cSMatthew G. Knepley 
46034e22dd4cSMatthew G. Knepley       PetscCall(DMPlexGetPointDepth(dm, points[p], &pdepth));
46044e22dd4cSMatthew G. Knepley       if (pdepth) {
46054e22dd4cSMatthew G. Knepley         markedFaces = PETSC_TRUE;
46064e22dd4cSMatthew G. Knepley         break;
46074e22dd4cSMatthew G. Knepley       }
46084e22dd4cSMatthew G. Knepley     }
46094e22dd4cSMatthew G. Knepley     PetscCall(ISRestoreIndices(pointIS, &points));
46104e22dd4cSMatthew G. Knepley     PetscCall(ISDestroy(&pointIS));
46114e22dd4cSMatthew G. Knepley     PetscCall(DMPlexCreateSubmesh(dm, label, value, markedFaces, &subdm));
46124e22dd4cSMatthew G. Knepley     PetscCall(DMPlexReplace_Internal(dm, &subdm));
46134e22dd4cSMatthew G. Knepley     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
46144e22dd4cSMatthew G. Knepley   }
46159b44eab4SMatthew G. Knepley   /* Handle DMPlex refinement before distribution */
46169566063dSJacob Faibussowitsch   PetscCall(DMPlexGetRefinementUniform(dm, &uniformOrig));
46179566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine_pre", "The number of refinements before distribution", "DMCreate", prerefine, &prerefine, NULL, 0));
46189566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_remap_pre", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL));
46199566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_uniform_pre", "Flag for uniform refinement before distribution", "DMCreate", uniform, &uniform, &flg));
46209566063dSJacob Faibussowitsch   if (flg) PetscCall(DMPlexSetRefinementUniform(dm, uniform));
46219566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-dm_refine_volume_limit_pre", "The maximum cell volume after refinement before distribution", "DMCreate", volume, &volume, &flg));
46229318fe57SMatthew G. Knepley   if (flg) {
46239566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementUniform(dm, PETSC_FALSE));
46249566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementLimit(dm, volume));
46259318fe57SMatthew G. Knepley     prerefine = PetscMax(prerefine, 1);
46269318fe57SMatthew G. Knepley   }
4627b23db253SStefano Zampini   if (prerefine) PetscCall(DMLocalizeCoordinates(dm));
46289b44eab4SMatthew G. Knepley   for (r = 0; r < prerefine; ++r) {
46299b44eab4SMatthew G. Knepley     DM             rdm;
46309b44eab4SMatthew G. Knepley     PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc;
46319b44eab4SMatthew G. Knepley 
4632dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
46339566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &rdm));
463469d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &rdm));
4635dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
463661a622f3SMatthew G. Knepley     if (coordFunc && remap) {
46379566063dSJacob Faibussowitsch       PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
46389b44eab4SMatthew G. Knepley       ((DM_Plex *)dm->data)->coordFunc = coordFunc;
46399b44eab4SMatthew G. Knepley     }
46409b44eab4SMatthew G. Knepley   }
46419566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, uniformOrig));
46429318fe57SMatthew G. Knepley   /* Handle DMPlex extrusion before distribution */
46439566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_extrude", "The number of layers to extrude", "", extLayers, &extLayers, NULL, 0));
46449318fe57SMatthew G. Knepley   if (extLayers) {
46459318fe57SMatthew G. Knepley     DM edm;
46469318fe57SMatthew G. Knepley 
46479566063dSJacob Faibussowitsch     PetscCall(DMExtrude(dm, extLayers, &edm));
464869d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &edm));
464948d16a33SMatthew G. Knepley     ((DM_Plex *)dm->data)->coordFunc = NULL;
4650dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
4651d410b0cfSMatthew G. Knepley     extLayers = 0;
46525e17fc22SAidan Hamilton     PetscCall(DMGetDimension(dm, &dim));
46539318fe57SMatthew G. Knepley   }
4654bdf63967SMatthew G. Knepley   /* Handle DMPlex reordering before distribution */
46556bc1bd01Sksagiyam   PetscCall(DMPlexReorderGetDefault(dm, &reorder));
46569566063dSJacob Faibussowitsch   PetscCall(MatGetOrderingList(&ordlist));
46576bc1bd01Sksagiyam   PetscCall(PetscStrncpy(oname, MATORDERINGNATURAL, sizeof(oname)));
46589566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_plex_reorder", "Set mesh reordering type", "DMPlexGetOrdering", ordlist, MATORDERINGNATURAL, oname, sizeof(oname), &flg));
4659adc21957SMatthew G. Knepley   if (reorder == DM_REORDER_DEFAULT_TRUE || flg) {
4660bdf63967SMatthew G. Knepley     DM pdm;
4661bdf63967SMatthew G. Knepley     IS perm;
4662bdf63967SMatthew G. Knepley 
46639566063dSJacob Faibussowitsch     PetscCall(DMPlexGetOrdering(dm, oname, NULL, &perm));
46649566063dSJacob Faibussowitsch     PetscCall(DMPlexPermute(dm, perm, &pdm));
46659566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&perm));
466669d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &pdm));
4667dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
4668bdf63967SMatthew G. Knepley   }
46699b44eab4SMatthew G. Knepley   /* Handle DMPlex distribution */
46709566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeGetDefault(dm, &distribute));
4671c506a872SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_distribute", "Flag to redistribute a mesh among processes", "DMPlexDistribute", distribute, &distribute, NULL));
4672a286e215SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_distribute_save_sf", "Flag to save the migration SF", "DMPlexSetMigrationSF", saveSF, &saveSF, NULL));
4673dbbe0bcdSBarry Smith   PetscCall(DMSetFromOptions_Overlap_Plex(dm, PetscOptionsObject, &overlap));
46749b44eab4SMatthew G. Knepley   if (distribute) {
46759b44eab4SMatthew G. Knepley     DM               pdm = NULL;
46769b44eab4SMatthew G. Knepley     PetscPartitioner part;
4677a286e215SMatthew G. Knepley     PetscSF          sfMigration;
46789b44eab4SMatthew G. Knepley 
46799566063dSJacob Faibussowitsch     PetscCall(DMPlexGetPartitioner(dm, &part));
46809566063dSJacob Faibussowitsch     PetscCall(PetscPartitionerSetFromOptions(part));
4681a286e215SMatthew G. Knepley     PetscCall(DMPlexDistribute(dm, overlap, &sfMigration, &pdm));
468248a46eb9SPierre Jolivet     if (pdm) PetscCall(DMPlexReplace_Internal(dm, &pdm));
4683a286e215SMatthew G. Knepley     if (saveSF) PetscCall(DMPlexSetMigrationSF(dm, sfMigration));
4684a286e215SMatthew G. Knepley     PetscCall(PetscSFDestroy(&sfMigration));
46859b44eab4SMatthew G. Knepley   }
46864054ae39SJames Wright 
46874054ae39SJames Wright   {
46884054ae39SJames Wright     PetscBool useBoxLabel = PETSC_FALSE;
46894054ae39SJames Wright     PetscCall(PetscOptionsBool("-dm_plex_box_label", "Create 'Face Sets' assuming boundary faces align with cartesian directions", "DMCreate", useBoxLabel, &useBoxLabel, NULL));
46904054ae39SJames Wright     if (useBoxLabel) PetscCall(DMPlexSetBoxLabel_Internal(dm));
46914054ae39SJames Wright   }
4692d2b2dc1eSMatthew G. Knepley   /* Must check CEED options before creating function space for coordinates */
4693d2b2dc1eSMatthew G. Knepley   {
4694d2b2dc1eSMatthew G. Knepley     PetscBool useCeed = PETSC_FALSE, flg;
4695d2b2dc1eSMatthew G. Knepley 
4696d2b2dc1eSMatthew G. Knepley     PetscCall(PetscOptionsBool("-dm_plex_use_ceed", "Use LibCEED as the FEM backend", "DMPlexSetUseCeed", useCeed, &useCeed, &flg));
4697d2b2dc1eSMatthew G. Knepley     if (flg) PetscCall(DMPlexSetUseCeed(dm, useCeed));
4698d2b2dc1eSMatthew G. Knepley   }
46999318fe57SMatthew G. Knepley   /* Create coordinate space */
47009318fe57SMatthew G. Knepley   if (created) {
470161a622f3SMatthew G. Knepley     DM_Plex  *mesh   = (DM_Plex *)dm->data;
4702e44f6aebSMatthew G. Knepley     PetscInt  degree = 1, deg;
47035515ebd3SMatthew G. Knepley     PetscInt  height = 0;
47045515ebd3SMatthew G. Knepley     DM        cdm;
4705c3db174cSMatthew G. Knepley     PetscBool flg, localize = PETSC_TRUE, sparseLocalize = PETSC_TRUE;
47069318fe57SMatthew G. Knepley 
47079566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-dm_coord_space", "Use an FEM space for coordinates", "", coordSpace, &coordSpace, &flg));
47089566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-dm_coord_petscspace_degree", "FEM degree for coordinate space", "", degree, &degree, NULL));
4709e44f6aebSMatthew G. Knepley     PetscCall(DMGetCoordinateDegree_Internal(dm, &deg));
4710e44f6aebSMatthew G. Knepley     if (coordSpace && deg <= 1) PetscCall(DMPlexCreateCoordinateSpace(dm, degree, PETSC_TRUE, mesh->coordFunc));
47115515ebd3SMatthew G. Knepley     PetscCall(DMGetCoordinateDM(dm, &cdm));
471261a622f3SMatthew G. Knepley     if (flg && !coordSpace) {
471361a622f3SMatthew G. Knepley       PetscDS      cds;
471461a622f3SMatthew G. Knepley       PetscObject  obj;
471561a622f3SMatthew G. Knepley       PetscClassId id;
471661a622f3SMatthew G. Knepley 
47179566063dSJacob Faibussowitsch       PetscCall(DMGetDS(cdm, &cds));
47189566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
47199566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
472061a622f3SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
472161a622f3SMatthew G. Knepley         PetscContainer dummy;
472261a622f3SMatthew G. Knepley 
47239566063dSJacob Faibussowitsch         PetscCall(PetscContainerCreate(PETSC_COMM_SELF, &dummy));
47249566063dSJacob Faibussowitsch         PetscCall(PetscObjectSetName((PetscObject)dummy, "coordinates"));
47259566063dSJacob Faibussowitsch         PetscCall(DMSetField(cdm, 0, NULL, (PetscObject)dummy));
47269566063dSJacob Faibussowitsch         PetscCall(PetscContainerDestroy(&dummy));
47279566063dSJacob Faibussowitsch         PetscCall(DMClearDS(cdm));
472861a622f3SMatthew G. Knepley       }
472961a622f3SMatthew G. Knepley       mesh->coordFunc = NULL;
473061a622f3SMatthew G. Knepley     }
4731c3db174cSMatthew G. Knepley     PetscCall(PetscOptionsBool("-dm_localize", "Localize mesh coordinates", "", localize, &localize, NULL));
4732c3db174cSMatthew G. Knepley     PetscCall(PetscOptionsBool("-dm_sparse_localize", "Localize only necessary cells", "DMSetSparseLocalize", sparseLocalize, &sparseLocalize, &flg));
4733c3db174cSMatthew G. Knepley     if (flg) PetscCall(DMSetSparseLocalize(dm, sparseLocalize));
47345515ebd3SMatthew G. Knepley     PetscCall(PetscOptionsInt("-dm_localize_height", "Localize edges and faces in addition to cells", "", height, &height, &flg));
47355515ebd3SMatthew G. Knepley     if (flg) PetscCall(DMPlexSetMaxProjectionHeight(cdm, height));
4736c3db174cSMatthew G. Knepley     if (localize) PetscCall(DMLocalizeCoordinates(dm));
47379318fe57SMatthew G. Knepley   }
473868d4fef7SMatthew G. Knepley   /* Handle DMPlex refinement */
473961a622f3SMatthew G. Knepley   remap = PETSC_TRUE;
47409566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine", "The number of uniform refinements", "DMCreate", refine, &refine, NULL, 0));
47419566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_remap", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL));
47429566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine_hierarchy", "The number of uniform refinements", "DMCreate", refine, &refine, &isHierarchy, 0));
47439566063dSJacob Faibussowitsch   if (refine) PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
474468d4fef7SMatthew G. Knepley   if (refine && isHierarchy) {
4745acdc6f61SToby Isaac     DM *dms, coarseDM;
474668d4fef7SMatthew G. Knepley 
47479566063dSJacob Faibussowitsch     PetscCall(DMGetCoarseDM(dm, &coarseDM));
47489566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)coarseDM));
47499566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(refine, &dms));
47509566063dSJacob Faibussowitsch     PetscCall(DMRefineHierarchy(dm, refine, dms));
475168d4fef7SMatthew G. Knepley     /* Total hack since we do not pass in a pointer */
47529566063dSJacob Faibussowitsch     PetscCall(DMPlexSwap_Static(dm, dms[refine - 1]));
475368d4fef7SMatthew G. Knepley     if (refine == 1) {
47549566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dm, dms[0]));
47559566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dm, PETSC_TRUE));
475668d4fef7SMatthew G. Knepley     } else {
47579566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dm, dms[refine - 2]));
47589566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dm, PETSC_TRUE));
47599566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dms[0], dms[refine - 1]));
47609566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dms[0], PETSC_TRUE));
476168d4fef7SMatthew G. Knepley     }
47629566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dms[refine - 1], coarseDM));
47639566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)coarseDM));
476468d4fef7SMatthew G. Knepley     /* Free DMs */
476568d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
4766dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dms[r], PetscOptionsObject));
47679566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dms[r]));
476868d4fef7SMatthew G. Knepley     }
47699566063dSJacob Faibussowitsch     PetscCall(PetscFree(dms));
477068d4fef7SMatthew G. Knepley   } else {
477168d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
47729318fe57SMatthew G. Knepley       DM             rdm;
477351a74b61SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc;
477468d4fef7SMatthew G. Knepley 
4775dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
47769566063dSJacob Faibussowitsch       PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &rdm));
477768d4fef7SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
477869d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &rdm));
4779dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
478061a622f3SMatthew G. Knepley       if (coordFunc && remap) {
47819566063dSJacob Faibussowitsch         PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
478251a74b61SMatthew G. Knepley         ((DM_Plex *)dm->data)->coordFunc = coordFunc;
478351a74b61SMatthew G. Knepley       }
478468d4fef7SMatthew G. Knepley     }
478568d4fef7SMatthew G. Knepley   }
47863cf6fe12SMatthew G. Knepley   /* Handle DMPlex coarsening */
47879566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_coarsen", "Coarsen the mesh", "DMCreate", coarsen, &coarsen, NULL, 0));
47889566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_coarsen_hierarchy", "The number of coarsenings", "DMCreate", coarsen, &coarsen, &isHierarchy, 0));
4789b653a561SMatthew G. Knepley   if (coarsen && isHierarchy) {
4790b653a561SMatthew G. Knepley     DM *dms;
4791b653a561SMatthew G. Knepley 
47929566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(coarsen, &dms));
47939566063dSJacob Faibussowitsch     PetscCall(DMCoarsenHierarchy(dm, coarsen, dms));
4794b653a561SMatthew G. Knepley     /* Free DMs */
4795b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
4796dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dms[r], PetscOptionsObject));
47979566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dms[r]));
4798b653a561SMatthew G. Knepley     }
47999566063dSJacob Faibussowitsch     PetscCall(PetscFree(dms));
4800b653a561SMatthew G. Knepley   } else {
4801b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
48029318fe57SMatthew G. Knepley       DM             cdm;
48039318fe57SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc;
48043cf6fe12SMatthew G. Knepley 
4805dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
48069566063dSJacob Faibussowitsch       PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &cdm));
48073cf6fe12SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
480869d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &cdm));
4809dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
48109318fe57SMatthew G. Knepley       if (coordFunc) {
48119566063dSJacob Faibussowitsch         PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
48129318fe57SMatthew G. Knepley         ((DM_Plex *)dm->data)->coordFunc = coordFunc;
48139318fe57SMatthew G. Knepley       }
48143cf6fe12SMatthew G. Knepley     }
4815b653a561SMatthew G. Knepley   }
4816be664eb1SMatthew G. Knepley   // Handle coordinate remapping
4817be664eb1SMatthew G. Knepley   remap = PETSC_FALSE;
4818be664eb1SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_coord_remap", "Flag to control coordinate remapping", "", remap, &remap, NULL));
4819be664eb1SMatthew G. Knepley   if (remap) {
4820be664eb1SMatthew G. Knepley     DMPlexCoordMap map     = DM_COORD_MAP_NONE;
4821be664eb1SMatthew G. Knepley     PetscPointFunc mapFunc = NULL;
4822be664eb1SMatthew G. Knepley     PetscScalar    params[16];
4823f45b553cSPierre Jolivet     PetscInt       Np = PETSC_STATIC_ARRAY_LENGTH(params), cdim;
4824be664eb1SMatthew G. Knepley     MPI_Comm       comm;
4825be664eb1SMatthew G. Knepley 
4826be664eb1SMatthew G. Knepley     PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4827be664eb1SMatthew G. Knepley     PetscCall(DMGetCoordinateDim(dm, &cdim));
4828be664eb1SMatthew G. Knepley     PetscCall(PetscOptionsScalarArray("-dm_coord_map_params", "Parameters for the coordinate remapping", "", params, &Np, &flg));
4829be664eb1SMatthew G. Knepley     if (!flg) Np = 0;
4830be664eb1SMatthew G. Knepley     // TODO Allow user to pass a map function by name
4831be664eb1SMatthew G. Knepley     PetscCall(PetscOptionsEnum("-dm_coord_map", "Coordinate mapping for built-in mesh", "", DMPlexCoordMaps, (PetscEnum)map, (PetscEnum *)&map, &flg));
4832be664eb1SMatthew G. Knepley     if (flg) {
4833be664eb1SMatthew G. Knepley       switch (map) {
4834be664eb1SMatthew G. Knepley       case DM_COORD_MAP_NONE:
4835be664eb1SMatthew G. Knepley         mapFunc = coordMap_identity;
4836be664eb1SMatthew G. Knepley         break;
4837be664eb1SMatthew G. Knepley       case DM_COORD_MAP_SHEAR:
4838be664eb1SMatthew G. Knepley         mapFunc = coordMap_shear;
4839be664eb1SMatthew G. Knepley         if (!Np) {
4840be664eb1SMatthew G. Knepley           Np        = cdim + 1;
4841be664eb1SMatthew G. Knepley           params[0] = 0;
4842be664eb1SMatthew G. Knepley           for (PetscInt d = 1; d <= cdim; ++d) params[d] = 1.0;
4843be664eb1SMatthew G. Knepley         }
4844be664eb1SMatthew 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);
4845be664eb1SMatthew G. Knepley         break;
4846be664eb1SMatthew G. Knepley       case DM_COORD_MAP_FLARE:
4847be664eb1SMatthew G. Knepley         mapFunc = coordMap_flare;
4848be664eb1SMatthew G. Knepley         if (!Np) {
4849be664eb1SMatthew G. Knepley           Np        = cdim + 1;
4850be664eb1SMatthew G. Knepley           params[0] = 0;
4851be664eb1SMatthew G. Knepley           for (PetscInt d = 1; d <= cdim; ++d) params[d] = 1.0;
4852be664eb1SMatthew G. Knepley         }
4853be664eb1SMatthew 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);
4854be664eb1SMatthew G. Knepley         break;
4855be664eb1SMatthew G. Knepley       case DM_COORD_MAP_ANNULUS:
4856be664eb1SMatthew G. Knepley         mapFunc = coordMap_annulus;
4857be664eb1SMatthew G. Knepley         if (!Np) {
4858be664eb1SMatthew G. Knepley           Np        = 2;
4859be664eb1SMatthew G. Knepley           params[0] = 1.;
4860be664eb1SMatthew G. Knepley           params[1] = 2.;
4861be664eb1SMatthew G. Knepley         }
4862be664eb1SMatthew G. Knepley         PetscCheck(Np == 2, comm, PETSC_ERR_ARG_WRONG, "The annulus coordinate map must have 2 parameters, not %" PetscInt_FMT, Np);
4863be664eb1SMatthew G. Knepley         break;
4864be664eb1SMatthew G. Knepley       case DM_COORD_MAP_SHELL:
4865be664eb1SMatthew G. Knepley         mapFunc = coordMap_shell;
4866be664eb1SMatthew G. Knepley         if (!Np) {
4867be664eb1SMatthew G. Knepley           Np        = 2;
4868be664eb1SMatthew G. Knepley           params[0] = 1.;
4869be664eb1SMatthew G. Knepley           params[1] = 2.;
4870be664eb1SMatthew G. Knepley         }
4871be664eb1SMatthew G. Knepley         PetscCheck(Np == 2, comm, PETSC_ERR_ARG_WRONG, "The spherical shell coordinate map must have 2 parameters, not %" PetscInt_FMT, Np);
4872be664eb1SMatthew G. Knepley         break;
4873be664eb1SMatthew G. Knepley       default:
4874be664eb1SMatthew G. Knepley         mapFunc = coordMap_identity;
4875be664eb1SMatthew G. Knepley       }
4876be664eb1SMatthew G. Knepley     }
4877be664eb1SMatthew G. Knepley     if (Np) {
4878be664eb1SMatthew G. Knepley       DM      cdm;
4879be664eb1SMatthew G. Knepley       PetscDS cds;
4880be664eb1SMatthew G. Knepley 
4881be664eb1SMatthew G. Knepley       PetscCall(DMGetCoordinateDM(dm, &cdm));
4882be664eb1SMatthew G. Knepley       PetscCall(DMGetDS(cdm, &cds));
4883be664eb1SMatthew G. Knepley       PetscCall(PetscDSSetConstants(cds, Np, params));
4884be664eb1SMatthew G. Knepley     }
4885be664eb1SMatthew G. Knepley     PetscCall(DMPlexRemapGeometry(dm, 0.0, mapFunc));
4886be664eb1SMatthew G. Knepley   }
4887909dfd52SMatthew G. Knepley   /* Handle ghost cells */
48889566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_create_fv_ghost_cells", "Flag to create finite volume ghost cells on the boundary", "DMCreate", ghostCells, &ghostCells, NULL));
4889909dfd52SMatthew G. Knepley   if (ghostCells) {
4890909dfd52SMatthew G. Knepley     DM   gdm;
4891909dfd52SMatthew G. Knepley     char lname[PETSC_MAX_PATH_LEN];
4892909dfd52SMatthew G. Knepley 
4893909dfd52SMatthew G. Knepley     lname[0] = '\0';
48949566063dSJacob Faibussowitsch     PetscCall(PetscOptionsString("-dm_plex_fv_ghost_cells_label", "Label name for ghost cells boundary", "DMCreate", lname, lname, sizeof(lname), &flg));
48959566063dSJacob Faibussowitsch     PetscCall(DMPlexConstructGhostCells(dm, flg ? lname : NULL, NULL, &gdm));
489669d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &gdm));
4897909dfd52SMatthew G. Knepley   }
48986913077dSMatthew G. Knepley   /* Handle 1D order */
4899adc21957SMatthew G. Knepley   if (reorder != DM_REORDER_DEFAULT_FALSE && dim == 1) {
49006913077dSMatthew G. Knepley     DM           cdm, rdm;
49016913077dSMatthew G. Knepley     PetscDS      cds;
49026913077dSMatthew G. Knepley     PetscObject  obj;
49036913077dSMatthew G. Knepley     PetscClassId id = PETSC_OBJECT_CLASSID;
49046913077dSMatthew G. Knepley     IS           perm;
49056bc1bd01Sksagiyam     PetscInt     Nf;
49066913077dSMatthew G. Knepley     PetscBool    distributed;
49076913077dSMatthew G. Knepley 
49089566063dSJacob Faibussowitsch     PetscCall(DMPlexIsDistributed(dm, &distributed));
49099566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
49109566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
49119566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(cds, &Nf));
49126913077dSMatthew G. Knepley     if (Nf) {
49139566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
49149566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
49156913077dSMatthew G. Knepley     }
49166bc1bd01Sksagiyam     if (!distributed && id != PETSCFE_CLASSID) {
49179566063dSJacob Faibussowitsch       PetscCall(DMPlexGetOrdering1D(dm, &perm));
49189566063dSJacob Faibussowitsch       PetscCall(DMPlexPermute(dm, perm, &rdm));
491969d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &rdm));
49209566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&perm));
49216913077dSMatthew G. Knepley     }
49226913077dSMatthew G. Knepley   }
49233cf6fe12SMatthew G. Knepley /* Handle */
4924dd4c3f67SMatthew G. Knepley non_refine:
4925dbbe0bcdSBarry Smith   PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
492622d6dc08SStefano Zampini   char    *phases[16];
492722d6dc08SStefano Zampini   PetscInt Nphases = 16;
492822d6dc08SStefano Zampini   PetscCall(PetscOptionsStringArray("-dm_plex_option_phases", "Option phase prefixes", "DMSetFromOptions", phases, &Nphases, &flg));
4929d0609cedSBarry Smith   PetscOptionsHeadEnd();
493022d6dc08SStefano Zampini 
493122d6dc08SStefano Zampini   // Phases
493222d6dc08SStefano Zampini   if (flg) {
493322d6dc08SStefano Zampini     const char *oldPrefix;
493422d6dc08SStefano Zampini 
493522d6dc08SStefano Zampini     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, &oldPrefix));
493622d6dc08SStefano Zampini     for (PetscInt ph = 0; ph < Nphases; ++ph) {
493722d6dc08SStefano Zampini       PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)dm, phases[ph]));
493822d6dc08SStefano Zampini       PetscCall(PetscInfo(dm, "Options phase %s for DM %s\n", phases[ph], dm->hdr.name));
493922d6dc08SStefano Zampini       PetscCall(DMSetFromOptions(dm));
494022d6dc08SStefano Zampini       PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, oldPrefix));
494122d6dc08SStefano Zampini       PetscCall(PetscFree(phases[ph]));
494222d6dc08SStefano Zampini     }
494322d6dc08SStefano Zampini   }
49443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
49450a6ba040SMatthew G. Knepley }
49460a6ba040SMatthew G. Knepley 
4947d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMCreateGlobalVector_Plex(DM dm, Vec *vec)
4948d71ae5a4SJacob Faibussowitsch {
4949552f7358SJed Brown   PetscFunctionBegin;
49509566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector_Section_Private(dm, vec));
49519566063dSJacob Faibussowitsch   /* PetscCall(VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM)); */
49529566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEW, (void (*)(void))VecView_Plex));
49539566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEWNATIVE, (void (*)(void))VecView_Plex_Native));
49549566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOAD, (void (*)(void))VecLoad_Plex));
49559566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOADNATIVE, (void (*)(void))VecLoad_Plex_Native));
49563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4957552f7358SJed Brown }
4958552f7358SJed Brown 
4959d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMCreateLocalVector_Plex(DM dm, Vec *vec)
4960d71ae5a4SJacob Faibussowitsch {
4961552f7358SJed Brown   PetscFunctionBegin;
49629566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector_Section_Private(dm, vec));
49639566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEW, (void (*)(void))VecView_Plex_Local));
49649566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOAD, (void (*)(void))VecLoad_Plex_Local));
49653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4966552f7358SJed Brown }
4967552f7358SJed Brown 
4968d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGetDimPoints_Plex(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
4969d71ae5a4SJacob Faibussowitsch {
4970793f3fe5SMatthew G. Knepley   PetscInt depth, d;
4971793f3fe5SMatthew G. Knepley 
4972793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
49739566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
4974793f3fe5SMatthew G. Knepley   if (depth == 1) {
49759566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(dm, &d));
49769566063dSJacob Faibussowitsch     if (dim == 0) PetscCall(DMPlexGetDepthStratum(dm, dim, pStart, pEnd));
49779566063dSJacob Faibussowitsch     else if (dim == d) PetscCall(DMPlexGetDepthStratum(dm, 1, pStart, pEnd));
49789371c9d4SSatish Balay     else {
49799371c9d4SSatish Balay       *pStart = 0;
49809371c9d4SSatish Balay       *pEnd   = 0;
49819371c9d4SSatish Balay     }
4982793f3fe5SMatthew G. Knepley   } else {
49839566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, dim, pStart, pEnd));
4984793f3fe5SMatthew G. Knepley   }
49853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4986793f3fe5SMatthew G. Knepley }
4987793f3fe5SMatthew G. Knepley 
4988d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGetNeighbors_Plex(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
4989d71ae5a4SJacob Faibussowitsch {
4990502a2867SDave May   PetscSF            sf;
49916497c311SBarry Smith   PetscMPIInt        niranks, njranks;
49926497c311SBarry Smith   PetscInt           n;
49930a19bb7dSprj-   const PetscMPIInt *iranks, *jranks;
49940a19bb7dSprj-   DM_Plex           *data = (DM_Plex *)dm->data;
4995502a2867SDave May 
49962f356facSMatthew G. Knepley   PetscFunctionBegin;
49979566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
49980a19bb7dSprj-   if (!data->neighbors) {
49999566063dSJacob Faibussowitsch     PetscCall(PetscSFSetUp(sf));
50009566063dSJacob Faibussowitsch     PetscCall(PetscSFGetRootRanks(sf, &njranks, &jranks, NULL, NULL, NULL));
50019566063dSJacob Faibussowitsch     PetscCall(PetscSFGetLeafRanks(sf, &niranks, &iranks, NULL, NULL));
50029566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(njranks + niranks + 1, &data->neighbors));
50039566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(data->neighbors + 1, jranks, njranks));
50049566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(data->neighbors + njranks + 1, iranks, niranks));
50050a19bb7dSprj-     n = njranks + niranks;
50069566063dSJacob Faibussowitsch     PetscCall(PetscSortRemoveDupsMPIInt(&n, data->neighbors + 1));
50070a19bb7dSprj-     /* The following cast should never fail: can't have more neighbors than PETSC_MPI_INT_MAX */
50089566063dSJacob Faibussowitsch     PetscCall(PetscMPIIntCast(n, data->neighbors));
50090a19bb7dSprj-   }
50100a19bb7dSprj-   if (nranks) *nranks = data->neighbors[0];
50110a19bb7dSprj-   if (ranks) {
50120a19bb7dSprj-     if (data->neighbors[0]) *ranks = data->neighbors + 1;
50130a19bb7dSprj-     else *ranks = NULL;
50140a19bb7dSprj-   }
50153ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5016502a2867SDave May }
5017502a2867SDave May 
50181eb70e55SToby Isaac PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM, DM, Mat, Vec, Vec);
50191eb70e55SToby Isaac 
5020d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMInitialize_Plex(DM dm)
5021d71ae5a4SJacob Faibussowitsch {
5022552f7358SJed Brown   PetscFunctionBegin;
5023552f7358SJed Brown   dm->ops->view                      = DMView_Plex;
50242c40f234SMatthew G. Knepley   dm->ops->load                      = DMLoad_Plex;
5025552f7358SJed Brown   dm->ops->setfromoptions            = DMSetFromOptions_Plex;
502638221697SMatthew G. Knepley   dm->ops->clone                     = DMClone_Plex;
5027552f7358SJed Brown   dm->ops->setup                     = DMSetUp_Plex;
50281bb6d2a8SBarry Smith   dm->ops->createlocalsection        = DMCreateLocalSection_Plex;
5029adc21957SMatthew G. Knepley   dm->ops->createsectionpermutation  = DMCreateSectionPermutation_Plex;
503066ad2231SToby Isaac   dm->ops->createdefaultconstraints  = DMCreateDefaultConstraints_Plex;
5031552f7358SJed Brown   dm->ops->createglobalvector        = DMCreateGlobalVector_Plex;
5032552f7358SJed Brown   dm->ops->createlocalvector         = DMCreateLocalVector_Plex;
5033184d77edSJed Brown   dm->ops->getlocaltoglobalmapping   = NULL;
50340298fd71SBarry Smith   dm->ops->createfieldis             = NULL;
5035552f7358SJed Brown   dm->ops->createcoordinatedm        = DMCreateCoordinateDM_Plex;
5036f19dbd58SToby Isaac   dm->ops->createcoordinatefield     = DMCreateCoordinateField_Plex;
50370a6ba040SMatthew G. Knepley   dm->ops->getcoloring               = NULL;
5038552f7358SJed Brown   dm->ops->creatematrix              = DMCreateMatrix_Plex;
5039bceba477SMatthew G. Knepley   dm->ops->createinterpolation       = DMCreateInterpolation_Plex;
5040bd041c0cSMatthew G. Knepley   dm->ops->createmassmatrix          = DMCreateMassMatrix_Plex;
5041b4937a87SMatthew G. Knepley   dm->ops->createmassmatrixlumped    = DMCreateMassMatrixLumped_Plex;
50425a84ad33SLisandro Dalcin   dm->ops->createinjection           = DMCreateInjection_Plex;
5043552f7358SJed Brown   dm->ops->refine                    = DMRefine_Plex;
50440a6ba040SMatthew G. Knepley   dm->ops->coarsen                   = DMCoarsen_Plex;
50450a6ba040SMatthew G. Knepley   dm->ops->refinehierarchy           = DMRefineHierarchy_Plex;
5046b653a561SMatthew G. Knepley   dm->ops->coarsenhierarchy          = DMCoarsenHierarchy_Plex;
5047d410b0cfSMatthew G. Knepley   dm->ops->extrude                   = DMExtrude_Plex;
50480298fd71SBarry Smith   dm->ops->globaltolocalbegin        = NULL;
50490298fd71SBarry Smith   dm->ops->globaltolocalend          = NULL;
50500298fd71SBarry Smith   dm->ops->localtoglobalbegin        = NULL;
50510298fd71SBarry Smith   dm->ops->localtoglobalend          = NULL;
5052552f7358SJed Brown   dm->ops->destroy                   = DMDestroy_Plex;
5053552f7358SJed Brown   dm->ops->createsubdm               = DMCreateSubDM_Plex;
50542adcc780SMatthew G. Knepley   dm->ops->createsuperdm             = DMCreateSuperDM_Plex;
5055793f3fe5SMatthew G. Knepley   dm->ops->getdimpoints              = DMGetDimPoints_Plex;
5056552f7358SJed Brown   dm->ops->locatepoints              = DMLocatePoints_Plex;
50570709b2feSToby Isaac   dm->ops->projectfunctionlocal      = DMProjectFunctionLocal_Plex;
50580709b2feSToby Isaac   dm->ops->projectfunctionlabellocal = DMProjectFunctionLabelLocal_Plex;
5059bfc4295aSToby Isaac   dm->ops->projectfieldlocal         = DMProjectFieldLocal_Plex;
50608c6c5593SMatthew G. Knepley   dm->ops->projectfieldlabellocal    = DMProjectFieldLabelLocal_Plex;
5061ece3a9fcSMatthew G. Knepley   dm->ops->projectbdfieldlabellocal  = DMProjectBdFieldLabelLocal_Plex;
50620709b2feSToby Isaac   dm->ops->computel2diff             = DMComputeL2Diff_Plex;
5063b698f381SToby Isaac   dm->ops->computel2gradientdiff     = DMComputeL2GradientDiff_Plex;
50642a16baeaSToby Isaac   dm->ops->computel2fielddiff        = DMComputeL2FieldDiff_Plex;
506528d58a37SPierre Jolivet   dm->ops->getneighbors              = DMGetNeighbors_Plex;
50666c6a6b79SMatthew G. Knepley   dm->ops->getlocalboundingbox       = DMGetLocalBoundingBox_Coordinates;
5067907a3e9cSStefano Zampini   dm->ops->createdomaindecomposition = DMCreateDomainDecomposition_Plex;
5068907a3e9cSStefano Zampini   dm->ops->createddscatters          = DMCreateDomainDecompositionScatters_Plex;
50699566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertBoundaryValues_C", DMPlexInsertBoundaryValues_Plex));
50706c51210dSStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertTimeDerivativeBoundaryValues_C", DMPlexInsertTimeDerivativeBoundaryValues_Plex));
50719566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMSetUpGLVisViewer_C", DMSetUpGLVisViewer_Plex));
50729566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMCreateNeumannOverlap_C", DMCreateNeumannOverlap_Plex));
50739566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeGetDefault_C", DMPlexDistributeGetDefault_Plex));
50749566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeSetDefault_C", DMPlexDistributeSetDefault_Plex));
50756bc1bd01Sksagiyam   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderGetDefault_C", DMPlexReorderGetDefault_Plex));
50766bc1bd01Sksagiyam   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderSetDefault_C", DMPlexReorderSetDefault_Plex));
5077adc21957SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionGetDefault_C", DMReorderSectionGetDefault_Plex));
5078adc21957SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionSetDefault_C", DMReorderSectionSetDefault_Plex));
5079adc21957SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionGetType_C", DMReorderSectionGetType_Plex));
5080adc21957SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionSetType_C", DMReorderSectionSetType_Plex));
50819566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMInterpolateSolution_C", DMInterpolateSolution_Plex));
5082c506a872SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetOverlap_C", DMPlexGetOverlap_Plex));
5083c506a872SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexSetOverlap_C", DMPlexSetOverlap_Plex));
5084d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetUseCeed_C", DMPlexGetUseCeed_Plex));
5085d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexSetUseCeed_C", DMPlexSetUseCeed_Plex));
50863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5087552f7358SJed Brown }
5088552f7358SJed Brown 
5089d71ae5a4SJacob Faibussowitsch PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm)
5090d71ae5a4SJacob Faibussowitsch {
509163a16f15SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *)dm->data;
50921fca310dSJames Wright   const PetscSF *face_sfs;
50931fca310dSJames Wright   PetscInt       num_face_sfs;
509463a16f15SMatthew G. Knepley 
509563a16f15SMatthew G. Knepley   PetscFunctionBegin;
509663a16f15SMatthew G. Knepley   mesh->refct++;
509763a16f15SMatthew G. Knepley   (*newdm)->data = mesh;
50981fca310dSJames Wright   PetscCall(DMPlexGetIsoperiodicFaceSF(dm, &num_face_sfs, &face_sfs));
50991fca310dSJames Wright   PetscCall(DMPlexSetIsoperiodicFaceSF(*newdm, num_face_sfs, (PetscSF *)face_sfs));
51009566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)*newdm, DMPLEX));
51019566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(*newdm));
51023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
510363a16f15SMatthew G. Knepley }
510463a16f15SMatthew G. Knepley 
51058818961aSMatthew G Knepley /*MC
5106a1cb98faSBarry Smith   DMPLEX = "plex" - A `DM` object that encapsulates an unstructured mesh, or CW Complex, which can be expressed using a Hasse Diagram.
510720f4b53cSBarry Smith                     In the local representation, `Vec`s contain all unknowns in the interior and shared boundary. This is
51088818961aSMatthew G Knepley                     specified by a PetscSection object. Ownership in the global representation is determined by
5109a1cb98faSBarry Smith                     ownership of the underlying `DMPLEX` points. This is specified by another `PetscSection` object.
51108818961aSMatthew G Knepley 
5111e5893cccSMatthew G. Knepley   Options Database Keys:
5112250712c9SMatthew G. Knepley + -dm_refine_pre                     - Refine mesh before distribution
5113250712c9SMatthew G. Knepley + -dm_refine_uniform_pre             - Choose uniform or generator-based refinement
5114250712c9SMatthew G. Knepley + -dm_refine_volume_limit_pre        - Cell volume limit after pre-refinement using generator
5115250712c9SMatthew G. Knepley . -dm_distribute                     - Distribute mesh across processes
5116250712c9SMatthew G. Knepley . -dm_distribute_overlap             - Number of cells to overlap for distribution
5117250712c9SMatthew G. Knepley . -dm_refine                         - Refine mesh after distribution
5118c3db174cSMatthew G. Knepley . -dm_localize <bool>                - Whether to localize coordinates for periodic meshes
5119c3db174cSMatthew G. Knepley . -dm_sparse_localize <bool>         - Whether to only localize cells on the periodic boundary
5120250712c9SMatthew G. Knepley . -dm_plex_hash_location             - Use grid hashing for point location
5121ddce0771SMatthew G. Knepley . -dm_plex_hash_box_faces <n,m,p>    - The number of divisions in each direction of the grid hash
5122f12cf164SMatthew G. Knepley . -dm_plex_partition_balance         - Attempt to evenly divide points on partition boundary between processes
5123f12cf164SMatthew G. Knepley . -dm_plex_remesh_bd                 - Allow changes to the boundary on remeshing
5124d5b43468SJose E. Roman . -dm_plex_max_projection_height     - Maximum mesh point height used to project locally
5125f12cf164SMatthew G. Knepley . -dm_plex_regular_refinement        - Use special nested projection algorithm for regular refinement
5126d02c7345SMatthew G. Knepley . -dm_plex_reorder_section           - Use specialized blocking if available
5127aaa8cc7dSPierre Jolivet . -dm_plex_check_all                 - Perform all checks below
5128f12cf164SMatthew G. Knepley . -dm_plex_check_symmetry            - Check that the adjacency information in the mesh is symmetric
5129f12cf164SMatthew G. Knepley . -dm_plex_check_skeleton <celltype> - Check that each cell has the correct number of vertices
5130f12cf164SMatthew 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
5131f12cf164SMatthew G. Knepley . -dm_plex_check_geometry            - Check that cells have positive volume
5132f12cf164SMatthew G. Knepley . -dm_view :mesh.tex:ascii_latex     - View the mesh in LaTeX/TikZ
5133e5893cccSMatthew G. Knepley . -dm_plex_view_scale <num>          - Scale the TikZ
51345962854dSMatthew G. Knepley . -dm_plex_print_fem <num>           - View FEM assembly information, such as element vectors and matrices
51355962854dSMatthew G. Knepley - -dm_plex_print_fvm <num>           - View FVM assembly information, such as flux updates
5136e5893cccSMatthew G. Knepley 
51378818961aSMatthew G Knepley   Level: intermediate
51388818961aSMatthew G Knepley 
51391cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMType`, `DMPlexCreate()`, `DMCreate()`, `DMSetType()`, `PetscSection`
51408818961aSMatthew G Knepley M*/
51418818961aSMatthew G Knepley 
5142d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode DMCreate_Plex(DM dm)
5143d71ae5a4SJacob Faibussowitsch {
5144552f7358SJed Brown   DM_Plex *mesh;
5145412e9a14SMatthew G. Knepley   PetscInt unit;
5146552f7358SJed Brown 
5147552f7358SJed Brown   PetscFunctionBegin;
5148f39ec787SMatthew G. Knepley   PetscCall(PetscCitationsRegister(PlexCitation, &Plexcite));
5149552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
51504dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&mesh));
5151adc21957SMatthew G. Knepley   dm->reorderSection = DM_REORDER_DEFAULT_NOTSET;
5152552f7358SJed Brown   dm->data           = mesh;
5153552f7358SJed Brown 
5154552f7358SJed Brown   mesh->refct = 1;
51559566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->coneSection));
51569566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->supportSection));
5157552f7358SJed Brown   mesh->refinementUniform      = PETSC_TRUE;
5158552f7358SJed Brown   mesh->refinementLimit        = -1.0;
5159e600fa54SMatthew G. Knepley   mesh->distDefault            = PETSC_TRUE;
5160adc21957SMatthew G. Knepley   mesh->reorderDefault         = DM_REORDER_DEFAULT_NOTSET;
51611d1f2f2aSksagiyam   mesh->distributionName       = NULL;
51627d0f5628SVaclav Hapla   mesh->interpolated           = DMPLEX_INTERPOLATED_INVALID;
51637d0f5628SVaclav Hapla   mesh->interpolatedCollective = DMPLEX_INTERPOLATED_INVALID;
5164552f7358SJed Brown 
51659566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerCreate(PetscObjectComm((PetscObject)dm), &mesh->partitioner));
51662e62ab5aSMatthew G. Knepley   mesh->remeshBd = PETSC_FALSE;
5167d9deefdfSMatthew G. Knepley 
51688865f1eaSKarl Rupp   for (unit = 0; unit < NUM_PETSC_UNITS; ++unit) mesh->scale[unit] = 1.0;
5169552f7358SJed Brown 
5170df0420ecSMatthew G. Knepley   mesh->depthState    = -1;
5171ba2698f1SMatthew G. Knepley   mesh->celltypeState = -1;
51726113b454SMatthew G. Knepley   mesh->printTol      = 1.0e-10;
5173c29ce622SStefano Zampini   mesh->nonempty_comm = MPI_COMM_SELF;
5174552f7358SJed Brown 
51759566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(dm));
51763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5177552f7358SJed Brown }
5178552f7358SJed Brown 
5179552f7358SJed Brown /*@
5180a1cb98faSBarry Smith   DMPlexCreate - Creates a `DMPLEX` object, which encapsulates an unstructured mesh, or CW complex, which can be expressed using a Hasse Diagram.
5181552f7358SJed Brown 
5182d083f849SBarry Smith   Collective
5183552f7358SJed Brown 
5184552f7358SJed Brown   Input Parameter:
5185a1cb98faSBarry Smith . comm - The communicator for the `DMPLEX` object
5186552f7358SJed Brown 
5187552f7358SJed Brown   Output Parameter:
5188a1cb98faSBarry Smith . mesh - The `DMPLEX` object
5189552f7358SJed Brown 
5190552f7358SJed Brown   Level: beginner
5191552f7358SJed Brown 
519242747ad1SJacob Faibussowitsch .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMType`, `DMCreate()`, `DMSetType()`
5193552f7358SJed Brown @*/
5194d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreate(MPI_Comm comm, DM *mesh)
5195d71ae5a4SJacob Faibussowitsch {
5196552f7358SJed Brown   PetscFunctionBegin;
51974f572ea9SToby Isaac   PetscAssertPointer(mesh, 2);
51989566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, mesh));
51999566063dSJacob Faibussowitsch   PetscCall(DMSetType(*mesh, DMPLEX));
52003ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5201552f7358SJed Brown }
5202552f7358SJed Brown 
5203b09969d6SVaclav Hapla /*@C
5204b0fe842aSMatthew G. Knepley   DMPlexBuildFromCellListParallel - Build distributed `DMPLEX` topology from a list of vertices for each cell (common mesh generator output) where all cells have the same celltype
5205a1cb98faSBarry Smith 
520620f4b53cSBarry Smith   Collective; No Fortran Support
5207b09969d6SVaclav Hapla 
5208b09969d6SVaclav Hapla   Input Parameters:
5209a1cb98faSBarry Smith + dm          - The `DM`
5210b09969d6SVaclav Hapla . numCells    - The number of cells owned by this process
5211a1cb98faSBarry Smith . numVertices - The number of vertices to be owned by this process, or `PETSC_DECIDE`
5212a1cb98faSBarry Smith . NVertices   - The global number of vertices, or `PETSC_DETERMINE`
5213b09969d6SVaclav Hapla . numCorners  - The number of vertices for each cell
52145e488331SVaclav Hapla - cells       - An array of numCells*numCorners numbers, the global vertex numbers for each cell
5215b09969d6SVaclav Hapla 
5216be8c289dSNicolas Barral   Output Parameters:
5217a1cb98faSBarry Smith + vertexSF         - (Optional) `PetscSF` describing complete vertex ownership
5218be8c289dSNicolas Barral - verticesAdjSaved - (Optional) vertex adjacency array
5219b09969d6SVaclav Hapla 
5220b09969d6SVaclav Hapla   Level: advanced
5221b09969d6SVaclav Hapla 
5222a1cb98faSBarry Smith   Notes:
5223a1cb98faSBarry Smith   Two triangles sharing a face
5224a1cb98faSBarry Smith .vb
5225a1cb98faSBarry Smith 
5226a1cb98faSBarry Smith         2
5227a1cb98faSBarry Smith       / | \
5228a1cb98faSBarry Smith      /  |  \
5229a1cb98faSBarry Smith     /   |   \
5230a1cb98faSBarry Smith    0  0 | 1  3
5231a1cb98faSBarry Smith     \   |   /
5232a1cb98faSBarry Smith      \  |  /
5233a1cb98faSBarry Smith       \ | /
5234a1cb98faSBarry Smith         1
5235a1cb98faSBarry Smith .ve
5236a1cb98faSBarry Smith   would have input
5237a1cb98faSBarry Smith .vb
5238a1cb98faSBarry Smith   numCells = 2, numVertices = 4
5239a1cb98faSBarry Smith   cells = [0 1 2  1 3 2]
5240a1cb98faSBarry Smith .ve
5241a1cb98faSBarry Smith   which would result in the `DMPLEX`
5242a1cb98faSBarry Smith .vb
5243a1cb98faSBarry Smith 
5244a1cb98faSBarry Smith         4
5245a1cb98faSBarry Smith       / | \
5246a1cb98faSBarry Smith      /  |  \
5247a1cb98faSBarry Smith     /   |   \
5248a1cb98faSBarry Smith    2  0 | 1  5
5249a1cb98faSBarry Smith     \   |   /
5250a1cb98faSBarry Smith      \  |  /
5251a1cb98faSBarry Smith       \ | /
5252a1cb98faSBarry Smith         3
5253a1cb98faSBarry Smith .ve
5254a1cb98faSBarry Smith 
5255a1cb98faSBarry Smith   Vertices are implicitly numbered consecutively 0,...,NVertices.
5256a1cb98faSBarry Smith   Each rank owns a chunk of numVertices consecutive vertices.
5257a1cb98faSBarry Smith   If numVertices is `PETSC_DECIDE`, PETSc will distribute them as evenly as possible using PetscLayout.
5258a1cb98faSBarry Smith   If NVertices is `PETSC_DETERMINE` and numVertices is PETSC_DECIDE, NVertices is computed by PETSc as the maximum vertex index in cells + 1.
5259a1cb98faSBarry Smith   If only NVertices is `PETSC_DETERMINE`, it is computed as the sum of numVertices over all ranks.
5260a1cb98faSBarry Smith 
5261a1cb98faSBarry Smith   The cell distribution is arbitrary non-overlapping, independent of the vertex distribution.
5262a1cb98faSBarry Smith 
52631cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildFromCellList()`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildCoordinatesFromCellListParallel()`,
5264a1cb98faSBarry Smith           `PetscSF`
5265b09969d6SVaclav Hapla @*/
5266d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildFromCellListParallel(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt NVertices, PetscInt numCorners, const PetscInt cells[], PetscSF *vertexSF, PetscInt **verticesAdjSaved)
5267d71ae5a4SJacob Faibussowitsch {
52682464107aSksagiyam   PetscSF     sfPoint;
52692464107aSksagiyam   PetscLayout layout;
527082fb893eSVaclav Hapla   PetscInt    numVerticesAdj, *verticesAdj, *cones, c, p;
5271a47d0d45SMatthew G. Knepley 
5272a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
527325b6865aSVaclav Hapla   PetscValidLogicalCollectiveInt(dm, NVertices, 4);
52749566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
527525b6865aSVaclav Hapla   /* Get/check global number of vertices */
527625b6865aSVaclav Hapla   {
527725b6865aSVaclav Hapla     PetscInt       NVerticesInCells, i;
527825b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
527925b6865aSVaclav Hapla 
528025b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
52811690c2aeSBarry Smith     NVerticesInCells = PETSC_INT_MIN;
52829371c9d4SSatish Balay     for (i = 0; i < len; i++)
52839371c9d4SSatish Balay       if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
528425b6865aSVaclav Hapla     ++NVerticesInCells;
5285462c564dSBarry Smith     PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &NVerticesInCells, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
528625b6865aSVaclav Hapla 
528725b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE && NVertices == PETSC_DECIDE) NVertices = NVerticesInCells;
52889371c9d4SSatish Balay     else
52899371c9d4SSatish 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);
529025b6865aSVaclav Hapla   }
52919079aca8SVaclav Hapla   /* Count locally unique vertices */
52929079aca8SVaclav Hapla   {
52939079aca8SVaclav Hapla     PetscHSetI vhash;
52949079aca8SVaclav Hapla     PetscInt   off = 0;
52959079aca8SVaclav Hapla 
52969566063dSJacob Faibussowitsch     PetscCall(PetscHSetICreate(&vhash));
5297a47d0d45SMatthew G. Knepley     for (c = 0; c < numCells; ++c) {
529848a46eb9SPierre Jolivet       for (p = 0; p < numCorners; ++p) PetscCall(PetscHSetIAdd(vhash, cells[c * numCorners + p]));
5299a47d0d45SMatthew G. Knepley     }
53009566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetSize(vhash, &numVerticesAdj));
53019566063dSJacob Faibussowitsch     if (!verticesAdjSaved) PetscCall(PetscMalloc1(numVerticesAdj, &verticesAdj));
5302ad540459SPierre Jolivet     else verticesAdj = *verticesAdjSaved;
53039566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetElems(vhash, &off, verticesAdj));
53049566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&vhash));
530563a3b9bcSJacob Faibussowitsch     PetscCheck(off == numVerticesAdj, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid number of local vertices %" PetscInt_FMT " should be %" PetscInt_FMT, off, numVerticesAdj);
5306a47d0d45SMatthew G. Knepley   }
53079566063dSJacob Faibussowitsch   PetscCall(PetscSortInt(numVerticesAdj, verticesAdj));
5308a47d0d45SMatthew G. Knepley   /* Create cones */
53099566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, 0, numCells + numVerticesAdj));
53109566063dSJacob Faibussowitsch   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, numCorners));
53119566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm));
53129566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCones(dm, &cones));
5313a47d0d45SMatthew G. Knepley   for (c = 0; c < numCells; ++c) {
5314a47d0d45SMatthew G. Knepley     for (p = 0; p < numCorners; ++p) {
5315a47d0d45SMatthew G. Knepley       const PetscInt gv = cells[c * numCorners + p];
5316a47d0d45SMatthew G. Knepley       PetscInt       lv;
5317a47d0d45SMatthew G. Knepley 
53189079aca8SVaclav Hapla       /* Positions within verticesAdj form 0-based local vertex numbering;
53199079aca8SVaclav Hapla          we need to shift it by numCells to get correct DAG points (cells go first) */
53209566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv));
532163a3b9bcSJacob Faibussowitsch       PetscCheck(lv >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not find global vertex %" PetscInt_FMT " in local connectivity", gv);
5322961cfab0SVaclav Hapla       cones[c * numCorners + p] = lv + numCells;
5323a47d0d45SMatthew G. Knepley     }
5324a47d0d45SMatthew G. Knepley   }
53252464107aSksagiyam   /* Build point sf */
53269566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(PetscObjectComm((PetscObject)dm), &layout));
53279566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetSize(layout, NVertices));
53289566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, numVertices));
53299566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
53309566063dSJacob Faibussowitsch   PetscCall(PetscSFCreateByMatchingIndices(layout, numVerticesAdj, verticesAdj, NULL, numCells, numVerticesAdj, verticesAdj, NULL, numCells, vertexSF, &sfPoint));
53319566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
53329566063dSJacob Faibussowitsch   if (!verticesAdjSaved) PetscCall(PetscFree(verticesAdj));
53339566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)sfPoint, "point SF"));
53342464107aSksagiyam   if (dm->sf) {
53352464107aSksagiyam     const char *prefix;
53362464107aSksagiyam 
53379566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm->sf, &prefix));
53389566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)sfPoint, prefix));
53392464107aSksagiyam   }
53409566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dm, sfPoint));
53419566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&sfPoint));
5342f4f49eeaSPierre Jolivet   if (vertexSF) PetscCall(PetscObjectSetName((PetscObject)*vertexSF, "Vertex Ownership SF"));
5343a47d0d45SMatthew G. Knepley   /* Fill in the rest of the topology structure */
53449566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
53459566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
53469566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
53473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5348a47d0d45SMatthew G. Knepley }
5349a47d0d45SMatthew G. Knepley 
5350b0fe842aSMatthew G. Knepley /*@C
5351b0fe842aSMatthew G. Knepley   DMPlexBuildFromCellSectionParallel - Build distributed `DMPLEX` topology from a list of vertices for each cell (common mesh generator output) allowing multiple celltypes
5352b0fe842aSMatthew G. Knepley 
5353b0fe842aSMatthew G. Knepley   Collective; No Fortran Support
5354b0fe842aSMatthew G. Knepley 
5355b0fe842aSMatthew G. Knepley   Input Parameters:
5356b0fe842aSMatthew G. Knepley + dm          - The `DM`
5357b0fe842aSMatthew G. Knepley . numCells    - The number of cells owned by this process
5358b0fe842aSMatthew G. Knepley . numVertices - The number of vertices to be owned by this process, or `PETSC_DECIDE`
5359b0fe842aSMatthew G. Knepley . NVertices   - The global number of vertices, or `PETSC_DETERMINE`
5360b0fe842aSMatthew G. Knepley . cellSection - The `PetscSection` giving the number of vertices for each cell (layout of cells)
5361b0fe842aSMatthew G. Knepley - cells       - An array of the global vertex numbers for each cell
5362b0fe842aSMatthew G. Knepley 
5363b0fe842aSMatthew G. Knepley   Output Parameters:
5364b0fe842aSMatthew G. Knepley + vertexSF         - (Optional) `PetscSF` describing complete vertex ownership
5365b0fe842aSMatthew G. Knepley - verticesAdjSaved - (Optional) vertex adjacency array
5366b0fe842aSMatthew G. Knepley 
5367b0fe842aSMatthew G. Knepley   Level: advanced
5368b0fe842aSMatthew G. Knepley 
5369b0fe842aSMatthew G. Knepley   Notes:
5370b0fe842aSMatthew G. Knepley   A triangle and quadrilateral sharing a face
5371b0fe842aSMatthew G. Knepley .vb
5372b0fe842aSMatthew G. Knepley         2----------3
5373b0fe842aSMatthew G. Knepley       / |          |
5374b0fe842aSMatthew G. Knepley      /  |          |
5375b0fe842aSMatthew G. Knepley     /   |          |
5376b0fe842aSMatthew G. Knepley    0  0 |     1    |
5377b0fe842aSMatthew G. Knepley     \   |          |
5378b0fe842aSMatthew G. Knepley      \  |          |
5379b0fe842aSMatthew G. Knepley       \ |          |
5380b0fe842aSMatthew G. Knepley         1----------4
5381b0fe842aSMatthew G. Knepley .ve
5382b0fe842aSMatthew G. Knepley   would have input
5383b0fe842aSMatthew G. Knepley .vb
5384b0fe842aSMatthew G. Knepley   numCells = 2, numVertices = 5
5385b0fe842aSMatthew G. Knepley   cells = [0 1 2  1 4 3 2]
5386b0fe842aSMatthew G. Knepley .ve
5387b0fe842aSMatthew G. Knepley   which would result in the `DMPLEX`
5388b0fe842aSMatthew G. Knepley .vb
5389b0fe842aSMatthew G. Knepley         4----------5
5390b0fe842aSMatthew G. Knepley       / |          |
5391b0fe842aSMatthew G. Knepley      /  |          |
5392b0fe842aSMatthew G. Knepley     /   |          |
5393b0fe842aSMatthew G. Knepley    2  0 |     1    |
5394b0fe842aSMatthew G. Knepley     \   |          |
5395b0fe842aSMatthew G. Knepley      \  |          |
5396b0fe842aSMatthew G. Knepley       \ |          |
5397b0fe842aSMatthew G. Knepley         3----------6
5398b0fe842aSMatthew G. Knepley .ve
5399b0fe842aSMatthew G. Knepley 
5400b0fe842aSMatthew G. Knepley   Vertices are implicitly numbered consecutively 0,...,NVertices.
5401b0fe842aSMatthew G. Knepley   Each rank owns a chunk of numVertices consecutive vertices.
5402b0fe842aSMatthew G. Knepley   If numVertices is `PETSC_DECIDE`, PETSc will distribute them as evenly as possible using PetscLayout.
5403b0fe842aSMatthew G. Knepley   If NVertices is `PETSC_DETERMINE` and numVertices is PETSC_DECIDE, NVertices is computed by PETSc as the maximum vertex index in cells + 1.
5404b0fe842aSMatthew G. Knepley   If only NVertices is `PETSC_DETERMINE`, it is computed as the sum of numVertices over all ranks.
5405b0fe842aSMatthew G. Knepley 
5406b0fe842aSMatthew G. Knepley   The cell distribution is arbitrary non-overlapping, independent of the vertex distribution.
5407b0fe842aSMatthew G. Knepley 
5408b0fe842aSMatthew G. Knepley .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildFromCellListParallel()`, `DMPlexCreateFromCellSectionParallel()`, `DMPlexBuildCoordinatesFromCellListParallel()`,
5409b0fe842aSMatthew G. Knepley           `PetscSF`
5410b0fe842aSMatthew G. Knepley @*/
5411b0fe842aSMatthew G. Knepley PetscErrorCode DMPlexBuildFromCellSectionParallel(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt NVertices, PetscSection cellSection, const PetscInt cells[], PetscSF *vertexSF, PetscInt **verticesAdjSaved)
5412b0fe842aSMatthew G. Knepley {
5413b0fe842aSMatthew G. Knepley   PetscSF     sfPoint;
5414b0fe842aSMatthew G. Knepley   PetscLayout layout;
5415b0fe842aSMatthew G. Knepley   PetscInt    numVerticesAdj, *verticesAdj, *cones, cStart, cEnd, len;
5416b0fe842aSMatthew G. Knepley 
5417b0fe842aSMatthew G. Knepley   PetscFunctionBegin;
5418b0fe842aSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, NVertices, 4);
5419b0fe842aSMatthew G. Knepley   PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
5420b0fe842aSMatthew G. Knepley   PetscCall(PetscSectionGetChart(cellSection, &cStart, &cEnd));
5421b0fe842aSMatthew G. Knepley   PetscCall(PetscSectionGetStorageSize(cellSection, &len));
5422b0fe842aSMatthew G. Knepley   PetscCheck(cStart == 0 && cEnd == numCells, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Section chart [%" PetscInt_FMT ", %" PetscInt_FMT ") should be [0, %" PetscInt_FMT ")", cStart, cEnd, numCells);
5423b0fe842aSMatthew G. Knepley   /* Get/check global number of vertices */
5424b0fe842aSMatthew G. Knepley   {
5425b0fe842aSMatthew G. Knepley     PetscInt NVerticesInCells;
5426b0fe842aSMatthew G. Knepley 
5427b0fe842aSMatthew G. Knepley     /* NVerticesInCells = max(cells) + 1 */
5428b0fe842aSMatthew G. Knepley     NVerticesInCells = PETSC_MIN_INT;
5429b0fe842aSMatthew G. Knepley     for (PetscInt i = 0; i < len; i++)
5430b0fe842aSMatthew G. Knepley       if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
5431b0fe842aSMatthew G. Knepley     ++NVerticesInCells;
5432b0fe842aSMatthew G. Knepley     PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &NVerticesInCells, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
5433b0fe842aSMatthew G. Knepley 
5434b0fe842aSMatthew G. Knepley     if (numVertices == PETSC_DECIDE && NVertices == PETSC_DECIDE) NVertices = NVerticesInCells;
5435b0fe842aSMatthew G. Knepley     else
5436b0fe842aSMatthew G. Knepley       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);
5437b0fe842aSMatthew G. Knepley   }
5438b0fe842aSMatthew G. Knepley   /* Count locally unique vertices */
5439b0fe842aSMatthew G. Knepley   {
5440b0fe842aSMatthew G. Knepley     PetscHSetI vhash;
5441b0fe842aSMatthew G. Knepley     PetscInt   off = 0;
5442b0fe842aSMatthew G. Knepley 
5443b0fe842aSMatthew G. Knepley     PetscCall(PetscHSetICreate(&vhash));
5444b0fe842aSMatthew G. Knepley     for (PetscInt i = 0; i < len; i++) PetscCall(PetscHSetIAdd(vhash, cells[i]));
5445b0fe842aSMatthew G. Knepley     PetscCall(PetscHSetIGetSize(vhash, &numVerticesAdj));
5446b0fe842aSMatthew G. Knepley     if (!verticesAdjSaved) PetscCall(PetscMalloc1(numVerticesAdj, &verticesAdj));
5447b0fe842aSMatthew G. Knepley     else verticesAdj = *verticesAdjSaved;
5448b0fe842aSMatthew G. Knepley     PetscCall(PetscHSetIGetElems(vhash, &off, verticesAdj));
5449b0fe842aSMatthew G. Knepley     PetscCall(PetscHSetIDestroy(&vhash));
5450b0fe842aSMatthew G. Knepley     PetscCheck(off == numVerticesAdj, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid number of local vertices %" PetscInt_FMT " should be %" PetscInt_FMT, off, numVerticesAdj);
5451b0fe842aSMatthew G. Knepley   }
5452b0fe842aSMatthew G. Knepley   PetscCall(PetscSortInt(numVerticesAdj, verticesAdj));
5453b0fe842aSMatthew G. Knepley   /* Create cones */
5454b0fe842aSMatthew G. Knepley   PetscCall(DMPlexSetChart(dm, 0, numCells + numVerticesAdj));
5455b0fe842aSMatthew G. Knepley   for (PetscInt c = 0; c < numCells; ++c) {
5456b0fe842aSMatthew G. Knepley     PetscInt dof;
5457b0fe842aSMatthew G. Knepley 
5458b0fe842aSMatthew G. Knepley     PetscCall(PetscSectionGetDof(cellSection, c, &dof));
5459b0fe842aSMatthew G. Knepley     PetscCall(DMPlexSetConeSize(dm, c, dof));
5460b0fe842aSMatthew G. Knepley   }
5461b0fe842aSMatthew G. Knepley   PetscCall(DMSetUp(dm));
5462b0fe842aSMatthew G. Knepley   PetscCall(DMPlexGetCones(dm, &cones));
5463b0fe842aSMatthew G. Knepley   for (PetscInt c = 0; c < numCells; ++c) {
5464b0fe842aSMatthew G. Knepley     PetscInt dof, off;
5465b0fe842aSMatthew G. Knepley 
5466b0fe842aSMatthew G. Knepley     PetscCall(PetscSectionGetDof(cellSection, c, &dof));
5467b0fe842aSMatthew G. Knepley     PetscCall(PetscSectionGetOffset(cellSection, c, &off));
5468b0fe842aSMatthew G. Knepley     for (PetscInt p = off; p < off + dof; ++p) {
5469b0fe842aSMatthew G. Knepley       const PetscInt gv = cells[p];
5470b0fe842aSMatthew G. Knepley       PetscInt       lv;
5471b0fe842aSMatthew G. Knepley 
5472b0fe842aSMatthew G. Knepley       /* Positions within verticesAdj form 0-based local vertex numbering;
5473b0fe842aSMatthew G. Knepley          we need to shift it by numCells to get correct DAG points (cells go first) */
5474b0fe842aSMatthew G. Knepley       PetscCall(PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv));
5475b0fe842aSMatthew G. Knepley       PetscCheck(lv >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not find global vertex %" PetscInt_FMT " in local connectivity", gv);
5476b0fe842aSMatthew G. Knepley       cones[p] = lv + numCells;
5477b0fe842aSMatthew G. Knepley     }
5478b0fe842aSMatthew G. Knepley   }
5479b0fe842aSMatthew G. Knepley   /* Build point sf */
5480b0fe842aSMatthew G. Knepley   PetscCall(PetscLayoutCreate(PetscObjectComm((PetscObject)dm), &layout));
5481b0fe842aSMatthew G. Knepley   PetscCall(PetscLayoutSetSize(layout, NVertices));
5482b0fe842aSMatthew G. Knepley   PetscCall(PetscLayoutSetLocalSize(layout, numVertices));
5483b0fe842aSMatthew G. Knepley   PetscCall(PetscLayoutSetBlockSize(layout, 1));
5484b0fe842aSMatthew G. Knepley   PetscCall(PetscSFCreateByMatchingIndices(layout, numVerticesAdj, verticesAdj, NULL, numCells, numVerticesAdj, verticesAdj, NULL, numCells, vertexSF, &sfPoint));
5485b0fe842aSMatthew G. Knepley   PetscCall(PetscLayoutDestroy(&layout));
5486b0fe842aSMatthew G. Knepley   if (!verticesAdjSaved) PetscCall(PetscFree(verticesAdj));
5487b0fe842aSMatthew G. Knepley   PetscCall(PetscObjectSetName((PetscObject)sfPoint, "point SF"));
5488b0fe842aSMatthew G. Knepley   if (dm->sf) {
5489b0fe842aSMatthew G. Knepley     const char *prefix;
5490b0fe842aSMatthew G. Knepley 
5491b0fe842aSMatthew G. Knepley     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm->sf, &prefix));
5492b0fe842aSMatthew G. Knepley     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)sfPoint, prefix));
5493b0fe842aSMatthew G. Knepley   }
5494b0fe842aSMatthew G. Knepley   PetscCall(DMSetPointSF(dm, sfPoint));
5495b0fe842aSMatthew G. Knepley   PetscCall(PetscSFDestroy(&sfPoint));
5496b0fe842aSMatthew G. Knepley   if (vertexSF) PetscCall(PetscObjectSetName((PetscObject)*vertexSF, "Vertex Ownership SF"));
5497b0fe842aSMatthew G. Knepley   /* Fill in the rest of the topology structure */
5498b0fe842aSMatthew G. Knepley   PetscCall(DMPlexSymmetrize(dm));
5499b0fe842aSMatthew G. Knepley   PetscCall(DMPlexStratify(dm));
5500b0fe842aSMatthew G. Knepley   PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
5501b0fe842aSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
5502b0fe842aSMatthew G. Knepley }
5503b0fe842aSMatthew G. Knepley 
5504cc4c1da9SBarry Smith /*@
5505a1cb98faSBarry Smith   DMPlexBuildCoordinatesFromCellListParallel - Build `DM` coordinates from a list of coordinates for each owned vertex (common mesh generator output)
5506a1cb98faSBarry Smith 
550720f4b53cSBarry Smith   Collective; No Fortran Support
5508b09969d6SVaclav Hapla 
5509b09969d6SVaclav Hapla   Input Parameters:
5510a1cb98faSBarry Smith + dm           - The `DM`
5511b09969d6SVaclav Hapla . spaceDim     - The spatial dimension used for coordinates
5512a1cb98faSBarry Smith . sfVert       - `PetscSF` describing complete vertex ownership
5513b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
5514b09969d6SVaclav Hapla 
5515b09969d6SVaclav Hapla   Level: advanced
5516b09969d6SVaclav Hapla 
55171cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildFromCellListParallel()`
5518b09969d6SVaclav Hapla @*/
5519d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildCoordinatesFromCellListParallel(DM dm, PetscInt spaceDim, PetscSF sfVert, const PetscReal vertexCoords[])
5520d71ae5a4SJacob Faibussowitsch {
5521a47d0d45SMatthew G. Knepley   PetscSection coordSection;
5522a47d0d45SMatthew G. Knepley   Vec          coordinates;
5523a47d0d45SMatthew G. Knepley   PetscScalar *coords;
55241edcf0b2SVaclav Hapla   PetscInt     numVertices, numVerticesAdj, coordSize, v, vStart, vEnd;
5525835f2295SStefano Zampini   PetscMPIInt  spaceDimi;
5526a47d0d45SMatthew G. Knepley 
5527a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
55289566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
55299566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
55301dca8a05SBarry Smith   PetscCheck(vStart >= 0 && vEnd >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
55319566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, spaceDim));
55329566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfVert, &numVertices, &numVerticesAdj, NULL, NULL));
55331dca8a05SBarry 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);
55349566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
55359566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
55369566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, spaceDim));
55379566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, vStart, vEnd));
55381edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
55399566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, spaceDim));
55409566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, spaceDim));
5541a47d0d45SMatthew G. Knepley   }
55429566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
55439566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
55449566063dSJacob Faibussowitsch   PetscCall(VecCreate(PetscObjectComm((PetscObject)dm), &coordinates));
55459566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, spaceDim));
55469566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
55479566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
55489566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
55499566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
5550a47d0d45SMatthew G. Knepley   {
5551a47d0d45SMatthew G. Knepley     MPI_Datatype coordtype;
5552a47d0d45SMatthew G. Knepley 
5553a47d0d45SMatthew G. Knepley     /* Need a temp buffer for coords if we have complex/single */
5554835f2295SStefano Zampini     PetscCall(PetscMPIIntCast(spaceDim, &spaceDimi));
5555835f2295SStefano Zampini     PetscCallMPI(MPI_Type_contiguous(spaceDimi, MPIU_SCALAR, &coordtype));
55569566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_commit(&coordtype));
555721016a8bSBarry Smith #if defined(PETSC_USE_COMPLEX)
555821016a8bSBarry Smith     {
555921016a8bSBarry Smith       PetscScalar *svertexCoords;
556021016a8bSBarry Smith       PetscInt     i;
55619566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numVertices * spaceDim, &svertexCoords));
55623612f820SVaclav Hapla       for (i = 0; i < numVertices * spaceDim; i++) svertexCoords[i] = vertexCoords[i];
55639566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(sfVert, coordtype, svertexCoords, coords, MPI_REPLACE));
55649566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(sfVert, coordtype, svertexCoords, coords, MPI_REPLACE));
55659566063dSJacob Faibussowitsch       PetscCall(PetscFree(svertexCoords));
556621016a8bSBarry Smith     }
556721016a8bSBarry Smith #else
55689566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sfVert, coordtype, vertexCoords, coords, MPI_REPLACE));
55699566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sfVert, coordtype, vertexCoords, coords, MPI_REPLACE));
557021016a8bSBarry Smith #endif
55719566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_free(&coordtype));
5572a47d0d45SMatthew G. Knepley   }
55739566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
55749566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
55759566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
55769566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
55773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5578a47d0d45SMatthew G. Knepley }
5579a47d0d45SMatthew G. Knepley 
5580c3edce3dSSatish Balay /*@
5581b0fe842aSMatthew G. Knepley   DMPlexCreateFromCellListParallelPetsc - Create distributed `DMPLEX` from a list of vertices for each cell (common mesh generator output) where all cells have the same celltype
5582a1cb98faSBarry Smith 
5583a1cb98faSBarry Smith   Collective
5584a47d0d45SMatthew G. Knepley 
5585a47d0d45SMatthew G. Knepley   Input Parameters:
5586a47d0d45SMatthew G. Knepley + comm         - The communicator
5587a47d0d45SMatthew G. Knepley . dim          - The topological dimension of the mesh
5588a47d0d45SMatthew G. Knepley . numCells     - The number of cells owned by this process
5589a1cb98faSBarry Smith . numVertices  - The number of vertices owned by this process, or `PETSC_DECIDE`
5590a1cb98faSBarry Smith . NVertices    - The global number of vertices, or `PETSC_DECIDE`
5591a47d0d45SMatthew G. Knepley . numCorners   - The number of vertices for each cell
5592a47d0d45SMatthew G. Knepley . interpolate  - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
5593a47d0d45SMatthew G. Knepley . cells        - An array of numCells*numCorners numbers, the global vertex numbers for each cell
5594a47d0d45SMatthew G. Knepley . spaceDim     - The spatial dimension used for coordinates
5595a47d0d45SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
5596a47d0d45SMatthew G. Knepley 
5597d8d19677SJose E. Roman   Output Parameters:
5598a1cb98faSBarry Smith + dm          - The `DM`
5599a1cb98faSBarry Smith . vertexSF    - (Optional) `PetscSF` describing complete vertex ownership
560060225df5SJacob Faibussowitsch - verticesAdj - (Optional) vertex adjacency array
5601a47d0d45SMatthew G. Knepley 
5602b09969d6SVaclav Hapla   Level: intermediate
5603a47d0d45SMatthew G. Knepley 
5604a1cb98faSBarry Smith   Notes:
5605a1cb98faSBarry Smith   This function is just a convenient sequence of `DMCreate()`, `DMSetType()`, `DMSetDimension()`,
5606a1cb98faSBarry Smith   `DMPlexBuildFromCellListParallel()`, `DMPlexInterpolate()`, `DMPlexBuildCoordinatesFromCellListParallel()`
5607a1cb98faSBarry Smith 
5608a1cb98faSBarry Smith   See `DMPlexBuildFromCellListParallel()` for an example and details about the topology-related parameters.
5609a1cb98faSBarry Smith 
5610a1cb98faSBarry Smith   See `DMPlexBuildCoordinatesFromCellListParallel()` for details about the geometry-related parameters.
5611a1cb98faSBarry Smith 
56121cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()`
5613a47d0d45SMatthew G. Knepley @*/
5614d71ae5a4SJacob 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)
5615d71ae5a4SJacob Faibussowitsch {
5616a47d0d45SMatthew G. Knepley   PetscSF sfVert;
5617a47d0d45SMatthew G. Knepley 
5618a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
56199566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
56209566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
5621a47d0d45SMatthew G. Knepley   PetscValidLogicalCollectiveInt(*dm, dim, 2);
5622064a246eSJacob Faibussowitsch   PetscValidLogicalCollectiveInt(*dm, spaceDim, 9);
56239566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
56249566063dSJacob Faibussowitsch   PetscCall(DMPlexBuildFromCellListParallel(*dm, numCells, numVertices, NVertices, numCorners, cells, &sfVert, verticesAdj));
5625a47d0d45SMatthew G. Knepley   if (interpolate) {
56265fd9971aSMatthew G. Knepley     DM idm;
5627a47d0d45SMatthew G. Knepley 
56289566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
56299566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
5630a47d0d45SMatthew G. Knepley     *dm = idm;
5631a47d0d45SMatthew G. Knepley   }
56329566063dSJacob Faibussowitsch   PetscCall(DMPlexBuildCoordinatesFromCellListParallel(*dm, spaceDim, sfVert, vertexCoords));
563318d54ad4SMichael Lange   if (vertexSF) *vertexSF = sfVert;
56349566063dSJacob Faibussowitsch   else PetscCall(PetscSFDestroy(&sfVert));
56353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5636a47d0d45SMatthew G. Knepley }
5637a47d0d45SMatthew G. Knepley 
5638cc4c1da9SBarry Smith /*@
5639b0fe842aSMatthew G. Knepley   DMPlexCreateFromCellSectionParallel - Create distributed `DMPLEX` from a list of vertices for each cell (common mesh generator output) and supports multiple celltypes
5640b0fe842aSMatthew G. Knepley 
5641b0fe842aSMatthew G. Knepley   Collective
5642b0fe842aSMatthew G. Knepley 
5643b0fe842aSMatthew G. Knepley   Input Parameters:
5644b0fe842aSMatthew G. Knepley + comm         - The communicator
5645b0fe842aSMatthew G. Knepley . dim          - The topological dimension of the mesh
5646b0fe842aSMatthew G. Knepley . numCells     - The number of cells owned by this process
5647b0fe842aSMatthew G. Knepley . numVertices  - The number of vertices owned by this process, or `PETSC_DECIDE`
5648b0fe842aSMatthew G. Knepley . NVertices    - The global number of vertices, or `PETSC_DECIDE`
5649b0fe842aSMatthew G. Knepley . cellSection  - The `PetscSection` giving the number of vertices for each cell (layout of cells)
5650b0fe842aSMatthew G. Knepley . interpolate  - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
5651b0fe842aSMatthew G. Knepley . cells        - An array of the global vertex numbers for each cell
5652b0fe842aSMatthew G. Knepley . spaceDim     - The spatial dimension used for coordinates
5653b0fe842aSMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
5654b0fe842aSMatthew G. Knepley 
5655b0fe842aSMatthew G. Knepley   Output Parameters:
5656b0fe842aSMatthew G. Knepley + dm          - The `DM`
5657b0fe842aSMatthew G. Knepley . vertexSF    - (Optional) `PetscSF` describing complete vertex ownership
5658b0fe842aSMatthew G. Knepley - verticesAdj - (Optional) vertex adjacency array
5659b0fe842aSMatthew G. Knepley 
5660b0fe842aSMatthew G. Knepley   Level: intermediate
5661b0fe842aSMatthew G. Knepley 
5662b0fe842aSMatthew G. Knepley   Notes:
5663b0fe842aSMatthew G. Knepley   This function is just a convenient sequence of `DMCreate()`, `DMSetType()`, `DMSetDimension()`,
5664b0fe842aSMatthew G. Knepley   `DMPlexBuildFromCellSectionParallel()`, `DMPlexInterpolate()`, `DMPlexBuildCoordinatesFromCellListParallel()`
5665b0fe842aSMatthew G. Knepley 
5666b0fe842aSMatthew G. Knepley   See `DMPlexBuildFromCellSectionParallel()` for an example and details about the topology-related parameters.
5667b0fe842aSMatthew G. Knepley 
5668b0fe842aSMatthew G. Knepley   See `DMPlexBuildCoordinatesFromCellListParallel()` for details about the geometry-related parameters.
5669b0fe842aSMatthew G. Knepley 
5670b0fe842aSMatthew G. Knepley .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()`
5671b0fe842aSMatthew G. Knepley @*/
5672b0fe842aSMatthew G. Knepley PetscErrorCode DMPlexCreateFromCellSectionParallel(MPI_Comm comm, PetscInt dim, PetscInt numCells, PetscInt numVertices, PetscInt NVertices, PetscSection cellSection, PetscBool interpolate, const PetscInt cells[], PetscInt spaceDim, const PetscReal vertexCoords[], PetscSF *vertexSF, PetscInt **verticesAdj, DM *dm)
5673b0fe842aSMatthew G. Knepley {
5674b0fe842aSMatthew G. Knepley   PetscSF sfVert;
5675b0fe842aSMatthew G. Knepley 
5676b0fe842aSMatthew G. Knepley   PetscFunctionBegin;
5677b0fe842aSMatthew G. Knepley   PetscCall(DMCreate(comm, dm));
5678b0fe842aSMatthew G. Knepley   PetscCall(DMSetType(*dm, DMPLEX));
5679b0fe842aSMatthew G. Knepley   PetscValidLogicalCollectiveInt(*dm, dim, 2);
5680b0fe842aSMatthew G. Knepley   PetscValidLogicalCollectiveInt(*dm, spaceDim, 9);
5681b0fe842aSMatthew G. Knepley   PetscCall(DMSetDimension(*dm, dim));
5682b0fe842aSMatthew G. Knepley   PetscCall(DMPlexBuildFromCellSectionParallel(*dm, numCells, numVertices, NVertices, cellSection, cells, &sfVert, verticesAdj));
5683b0fe842aSMatthew G. Knepley   if (interpolate) {
5684b0fe842aSMatthew G. Knepley     DM idm;
5685b0fe842aSMatthew G. Knepley 
5686b0fe842aSMatthew G. Knepley     PetscCall(DMPlexInterpolate(*dm, &idm));
5687b0fe842aSMatthew G. Knepley     PetscCall(DMDestroy(dm));
5688b0fe842aSMatthew G. Knepley     *dm = idm;
5689b0fe842aSMatthew G. Knepley   }
5690b0fe842aSMatthew G. Knepley   PetscCall(DMPlexBuildCoordinatesFromCellListParallel(*dm, spaceDim, sfVert, vertexCoords));
5691b0fe842aSMatthew G. Knepley   if (vertexSF) *vertexSF = sfVert;
5692b0fe842aSMatthew G. Knepley   else PetscCall(PetscSFDestroy(&sfVert));
5693b0fe842aSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
5694b0fe842aSMatthew G. Knepley }
5695b0fe842aSMatthew G. Knepley 
5696b0fe842aSMatthew G. Knepley /*@
5697a1cb98faSBarry Smith   DMPlexBuildFromCellList - Build `DMPLEX` topology from a list of vertices for each cell (common mesh generator output)
5698a1cb98faSBarry Smith 
569920f4b53cSBarry Smith   Collective; No Fortran Support
57009298eaa6SMatthew G Knepley 
57019298eaa6SMatthew G Knepley   Input Parameters:
5702a1cb98faSBarry Smith + dm          - The `DM`
5703b09969d6SVaclav Hapla . numCells    - The number of cells owned by this process
5704a1cb98faSBarry Smith . numVertices - The number of vertices owned by this process, or `PETSC_DETERMINE`
57059298eaa6SMatthew G Knepley . numCorners  - The number of vertices for each cell
5706a3b724e8SBarry Smith - cells       - An array of `numCells` x `numCorners` numbers, the global vertex numbers for each cell
57079298eaa6SMatthew G Knepley 
5708b09969d6SVaclav Hapla   Level: advanced
57099298eaa6SMatthew G Knepley 
5710b09969d6SVaclav Hapla   Notes:
5711b09969d6SVaclav Hapla   Two triangles sharing a face
5712a1cb98faSBarry Smith .vb
57139298eaa6SMatthew G Knepley 
5714a1cb98faSBarry Smith         2
5715a1cb98faSBarry Smith       / | \
5716a1cb98faSBarry Smith      /  |  \
5717a1cb98faSBarry Smith     /   |   \
5718a1cb98faSBarry Smith    0  0 | 1  3
5719a1cb98faSBarry Smith     \   |   /
5720a1cb98faSBarry Smith      \  |  /
5721a1cb98faSBarry Smith       \ | /
5722a1cb98faSBarry Smith         1
5723a1cb98faSBarry Smith .ve
5724a1cb98faSBarry Smith   would have input
5725a1cb98faSBarry Smith .vb
5726a1cb98faSBarry Smith   numCells = 2, numVertices = 4
5727a1cb98faSBarry Smith   cells = [0 1 2  1 3 2]
5728a1cb98faSBarry Smith .ve
5729a1cb98faSBarry Smith   which would result in the `DMPLEX`
5730a1cb98faSBarry Smith .vb
5731a1cb98faSBarry Smith 
5732a1cb98faSBarry Smith         4
5733a1cb98faSBarry Smith       / | \
5734a1cb98faSBarry Smith      /  |  \
5735a1cb98faSBarry Smith     /   |   \
5736a1cb98faSBarry Smith    2  0 | 1  5
5737a1cb98faSBarry Smith     \   |   /
5738a1cb98faSBarry Smith      \  |  /
5739a1cb98faSBarry Smith       \ | /
5740a1cb98faSBarry Smith         3
5741a1cb98faSBarry Smith .ve
5742a1cb98faSBarry Smith 
5743a1cb98faSBarry Smith   If numVertices is `PETSC_DETERMINE`, it is computed by PETSc as the maximum vertex index in cells + 1.
574425b6865aSVaclav Hapla 
57451cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromCellListPetsc()`
5746b09969d6SVaclav Hapla @*/
5747d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildFromCellList(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const PetscInt cells[])
5748d71ae5a4SJacob Faibussowitsch {
5749961cfab0SVaclav Hapla   PetscInt *cones, c, p, dim;
5750b09969d6SVaclav Hapla 
5751b09969d6SVaclav Hapla   PetscFunctionBegin;
57529566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
57539566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
575425b6865aSVaclav Hapla   /* Get/check global number of vertices */
575525b6865aSVaclav Hapla   {
575625b6865aSVaclav Hapla     PetscInt       NVerticesInCells, i;
575725b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
575825b6865aSVaclav Hapla 
575925b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
57601690c2aeSBarry Smith     NVerticesInCells = PETSC_INT_MIN;
57619371c9d4SSatish Balay     for (i = 0; i < len; i++)
57629371c9d4SSatish Balay       if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
576325b6865aSVaclav Hapla     ++NVerticesInCells;
576425b6865aSVaclav Hapla 
576525b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE) numVertices = NVerticesInCells;
57669371c9d4SSatish Balay     else
57679371c9d4SSatish 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);
576825b6865aSVaclav Hapla   }
57699566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
577048a46eb9SPierre Jolivet   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, numCorners));
57719566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm));
57729566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCones(dm, &cones));
5773b09969d6SVaclav Hapla   for (c = 0; c < numCells; ++c) {
5774ad540459SPierre Jolivet     for (p = 0; p < numCorners; ++p) cones[c * numCorners + p] = cells[c * numCorners + p] + numCells;
5775b09969d6SVaclav Hapla   }
57769566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
57779566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
57789566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
57793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5780b09969d6SVaclav Hapla }
5781b09969d6SVaclav Hapla 
5782cc4c1da9SBarry Smith /*@
5783a1cb98faSBarry Smith   DMPlexBuildCoordinatesFromCellList - Build `DM` coordinates from a list of coordinates for each owned vertex (common mesh generator output)
5784a1cb98faSBarry Smith 
5785cc4c1da9SBarry Smith   Collective
5786b09969d6SVaclav Hapla 
5787b09969d6SVaclav Hapla   Input Parameters:
5788a1cb98faSBarry Smith + dm           - The `DM`
5789b09969d6SVaclav Hapla . spaceDim     - The spatial dimension used for coordinates
5790b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
5791b09969d6SVaclav Hapla 
5792b09969d6SVaclav Hapla   Level: advanced
5793b09969d6SVaclav Hapla 
57941cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellList()`
5795b09969d6SVaclav Hapla @*/
5796d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildCoordinatesFromCellList(DM dm, PetscInt spaceDim, const PetscReal vertexCoords[])
5797d71ae5a4SJacob Faibussowitsch {
5798b09969d6SVaclav Hapla   PetscSection coordSection;
5799b09969d6SVaclav Hapla   Vec          coordinates;
5800b09969d6SVaclav Hapla   DM           cdm;
5801b09969d6SVaclav Hapla   PetscScalar *coords;
58021edcf0b2SVaclav Hapla   PetscInt     v, vStart, vEnd, d;
5803b09969d6SVaclav Hapla 
5804b09969d6SVaclav Hapla   PetscFunctionBegin;
58059566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
58069566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
58071dca8a05SBarry Smith   PetscCheck(vStart >= 0 && vEnd >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
58089566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, spaceDim));
58099566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
58109566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
58119566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, spaceDim));
58129566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, vStart, vEnd));
58131edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
58149566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, spaceDim));
58159566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, spaceDim));
5816b09969d6SVaclav Hapla   }
58179566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
5818b09969d6SVaclav Hapla 
58199566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
58209566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(cdm, &coordinates));
58219566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, spaceDim));
58229566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
58239566063dSJacob Faibussowitsch   PetscCall(VecGetArrayWrite(coordinates, &coords));
58241edcf0b2SVaclav Hapla   for (v = 0; v < vEnd - vStart; ++v) {
5825ad540459SPierre Jolivet     for (d = 0; d < spaceDim; ++d) coords[v * spaceDim + d] = vertexCoords[v * spaceDim + d];
5826b09969d6SVaclav Hapla   }
58279566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayWrite(coordinates, &coords));
58289566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
58299566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
58309566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
58313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5832b09969d6SVaclav Hapla }
5833b09969d6SVaclav Hapla 
5834b09969d6SVaclav Hapla /*@
5835a1cb98faSBarry Smith   DMPlexCreateFromCellListPetsc - Create `DMPLEX` from a list of vertices for each cell (common mesh generator output), but only process 0 takes in the input
58363df08285SMatthew G. Knepley 
5837a1cb98faSBarry Smith   Collective
5838b09969d6SVaclav Hapla 
5839b09969d6SVaclav Hapla   Input Parameters:
5840b09969d6SVaclav Hapla + comm         - The communicator
5841b09969d6SVaclav Hapla . dim          - The topological dimension of the mesh
58423df08285SMatthew G. Knepley . numCells     - The number of cells, only on process 0
5843a1cb98faSBarry Smith . numVertices  - The number of vertices owned by this process, or `PETSC_DECIDE`, only on process 0
58443df08285SMatthew G. Knepley . numCorners   - The number of vertices for each cell, only on process 0
5845b09969d6SVaclav Hapla . interpolate  - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
58463df08285SMatthew G. Knepley . cells        - An array of numCells*numCorners numbers, the vertices for each cell, only on process 0
5847b09969d6SVaclav Hapla . spaceDim     - The spatial dimension used for coordinates
58483df08285SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex, only on process 0
5849b09969d6SVaclav Hapla 
5850b09969d6SVaclav Hapla   Output Parameter:
5851a1cb98faSBarry Smith . dm - The `DM`, which only has points on process 0
585225b6865aSVaclav Hapla 
5853b09969d6SVaclav Hapla   Level: intermediate
5854b09969d6SVaclav Hapla 
5855a1cb98faSBarry Smith   Notes:
5856a1cb98faSBarry Smith   This function is just a convenient sequence of `DMCreate()`, `DMSetType()`, `DMSetDimension()`, `DMPlexBuildFromCellList()`,
5857a1cb98faSBarry Smith   `DMPlexInterpolate()`, `DMPlexBuildCoordinatesFromCellList()`
5858a1cb98faSBarry Smith 
5859a1cb98faSBarry Smith   See `DMPlexBuildFromCellList()` for an example and details about the topology-related parameters.
5860a1cb98faSBarry Smith   See `DMPlexBuildCoordinatesFromCellList()` for details about the geometry-related parameters.
5861a1cb98faSBarry Smith   See `DMPlexCreateFromCellListParallelPetsc()` for parallel input
5862a1cb98faSBarry Smith 
58631cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildFromCellList()`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()`
58649298eaa6SMatthew G Knepley @*/
5865d71ae5a4SJacob 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)
5866d71ae5a4SJacob Faibussowitsch {
58673df08285SMatthew G. Knepley   PetscMPIInt rank;
58689298eaa6SMatthew G Knepley 
58699298eaa6SMatthew G Knepley   PetscFunctionBegin;
587028b400f6SJacob 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.");
58719566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
58729566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
58739566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
58749566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
5875c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildFromCellList(*dm, numCells, numVertices, numCorners, cells));
58769566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildFromCellList(*dm, 0, 0, 0, NULL));
58779298eaa6SMatthew G Knepley   if (interpolate) {
58785fd9971aSMatthew G. Knepley     DM idm;
58799298eaa6SMatthew G Knepley 
58809566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
58819566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
58829298eaa6SMatthew G Knepley     *dm = idm;
58839298eaa6SMatthew G Knepley   }
5884c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, vertexCoords));
58859566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, NULL));
58863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
58879298eaa6SMatthew G Knepley }
58889298eaa6SMatthew G Knepley 
5889939f6067SMatthew G. Knepley /*@
589020f4b53cSBarry Smith   DMPlexCreateFromDAG - This takes as input the adjacency-list representation of the Directed Acyclic Graph (Hasse Diagram) encoding a mesh, and produces a `DM`
5891939f6067SMatthew G. Knepley 
5892939f6067SMatthew G. Knepley   Input Parameters:
589320f4b53cSBarry Smith + dm               - The empty `DM` object, usually from `DMCreate()` and `DMSetDimension()`
5894939f6067SMatthew G. Knepley . depth            - The depth of the DAG
589520f4b53cSBarry Smith . numPoints        - Array of size depth + 1 containing the number of points at each `depth`
5896939f6067SMatthew G. Knepley . coneSize         - The cone size of each point
5897939f6067SMatthew G. Knepley . cones            - The concatenation of the cone points for each point, the cone list must be oriented correctly for each point
5898939f6067SMatthew G. Knepley . coneOrientations - The orientation of each cone point
589920f4b53cSBarry Smith - vertexCoords     - An array of `numPoints`[0]*spacedim numbers representing the coordinates of each vertex, with spacedim the value set via `DMSetCoordinateDim()`
5900939f6067SMatthew G. Knepley 
5901939f6067SMatthew G. Knepley   Output Parameter:
590220f4b53cSBarry Smith . dm - The `DM`
590320f4b53cSBarry Smith 
590420f4b53cSBarry Smith   Level: advanced
5905939f6067SMatthew G. Knepley 
5906a1cb98faSBarry Smith   Note:
5907a1cb98faSBarry Smith   Two triangles sharing a face would have input
5908a1cb98faSBarry Smith .vb
5909a1cb98faSBarry Smith   depth = 1, numPoints = [4 2], coneSize = [3 3 0 0 0 0]
5910a1cb98faSBarry Smith   cones = [2 3 4  3 5 4], coneOrientations = [0 0 0  0 0 0]
5911a1cb98faSBarry Smith  vertexCoords = [-1.0 0.0  0.0 -1.0  0.0 1.0  1.0 0.0]
5912a1cb98faSBarry Smith .ve
5913939f6067SMatthew G. Knepley   which would result in the DMPlex
5914a1cb98faSBarry Smith .vb
5915a1cb98faSBarry Smith         4
5916a1cb98faSBarry Smith       / | \
5917a1cb98faSBarry Smith      /  |  \
5918a1cb98faSBarry Smith     /   |   \
5919a1cb98faSBarry Smith    2  0 | 1  5
5920a1cb98faSBarry Smith     \   |   /
5921a1cb98faSBarry Smith      \  |  /
5922a1cb98faSBarry Smith       \ | /
5923a1cb98faSBarry Smith         3
5924a1cb98faSBarry Smith .ve
5925a1cb98faSBarry Smith   Notice that all points are numbered consecutively, unlike `DMPlexCreateFromCellListPetsc()`
5926939f6067SMatthew G. Knepley 
59271cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`
5928939f6067SMatthew G. Knepley @*/
5929d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateFromDAG(DM dm, PetscInt depth, const PetscInt numPoints[], const PetscInt coneSize[], const PetscInt cones[], const PetscInt coneOrientations[], const PetscScalar vertexCoords[])
5930d71ae5a4SJacob Faibussowitsch {
59319298eaa6SMatthew G Knepley   Vec          coordinates;
59329298eaa6SMatthew G Knepley   PetscSection coordSection;
59339298eaa6SMatthew G Knepley   PetscScalar *coords;
5934811e8653SToby Isaac   PetscInt     coordSize, firstVertex = -1, pStart = 0, pEnd = 0, p, v, dim, dimEmbed, d, off;
59359298eaa6SMatthew G Knepley 
59369298eaa6SMatthew G Knepley   PetscFunctionBegin;
59379566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
59389566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dimEmbed));
593963a3b9bcSJacob Faibussowitsch   PetscCheck(dimEmbed >= dim, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Embedding dimension %" PetscInt_FMT " cannot be less than intrinsic dimension %" PetscInt_FMT, dimEmbed, dim);
59409298eaa6SMatthew G Knepley   for (d = 0; d <= depth; ++d) pEnd += numPoints[d];
59419566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, pStart, pEnd));
59429298eaa6SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
59439566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeSize(dm, p, coneSize[p - pStart]));
5944ad540459SPierre Jolivet     if (firstVertex < 0 && !coneSize[p - pStart]) firstVertex = p - pStart;
594597e052ccSToby Isaac   }
59461dca8a05SBarry Smith   PetscCheck(firstVertex >= 0 || !numPoints[0], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Expected %" PetscInt_FMT " vertices but could not find any", numPoints[0]);
59479566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm)); /* Allocate space for cones */
59489298eaa6SMatthew G Knepley   for (p = pStart, off = 0; p < pEnd; off += coneSize[p - pStart], ++p) {
59499566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(dm, p, &cones[off]));
59509566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeOrientation(dm, p, &coneOrientations[off]));
59519298eaa6SMatthew G Knepley   }
59529566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
59539566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
59549298eaa6SMatthew G Knepley   /* Build coordinates */
59559566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
59569566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
59579566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dimEmbed));
59589566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numPoints[0]));
59599298eaa6SMatthew G Knepley   for (v = firstVertex; v < firstVertex + numPoints[0]; ++v) {
59609566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, dimEmbed));
59619566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dimEmbed));
59629298eaa6SMatthew G Knepley   }
59639566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
59649566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
59659566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
59669566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
59679566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
59689566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, dimEmbed));
59699566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
59709318fe57SMatthew G. Knepley   if (vertexCoords) {
59719566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
59729298eaa6SMatthew G Knepley     for (v = 0; v < numPoints[0]; ++v) {
59739298eaa6SMatthew G Knepley       PetscInt off;
59749298eaa6SMatthew G Knepley 
59759566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, v + firstVertex, &off));
5976ad540459SPierre Jolivet       for (d = 0; d < dimEmbed; ++d) coords[off + d] = vertexCoords[v * dimEmbed + d];
59779298eaa6SMatthew G Knepley     }
59789318fe57SMatthew G. Knepley   }
59799566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
59809566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
59819566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
59823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
59839298eaa6SMatthew G Knepley }
59848415267dSToby Isaac 
5985a4e35b19SJacob Faibussowitsch /*
5986a1cb98faSBarry Smith   DMPlexCreateCellVertexFromFile - Create a `DMPLEX` mesh from a simple cell-vertex file.
5987a1cb98faSBarry Smith 
5988a1cb98faSBarry Smith   Collective
59898ca92349SMatthew G. Knepley 
59908ca92349SMatthew G. Knepley + comm        - The MPI communicator
59918ca92349SMatthew G. Knepley . filename    - Name of the .dat file
59928ca92349SMatthew G. Knepley - interpolate - Create faces and edges in the mesh
59938ca92349SMatthew G. Knepley 
59948ca92349SMatthew G. Knepley   Output Parameter:
5995a1cb98faSBarry Smith . dm  - The `DM` object representing the mesh
59968ca92349SMatthew G. Knepley 
59978ca92349SMatthew G. Knepley   Level: beginner
59988ca92349SMatthew G. Knepley 
5999a1cb98faSBarry Smith   Note:
6000a1cb98faSBarry Smith   The format is the simplest possible:
6001a1cb98faSBarry Smith .vb
6002d0812dedSMatthew G. Knepley   dim Ne Nv Nc Nl
6003d0812dedSMatthew G. Knepley   v_1 v_2 ... v_Nc
6004d0812dedSMatthew G. Knepley   ...
6005d0812dedSMatthew G. Knepley   x y z marker_1 ... marker_Nl
6006a1cb98faSBarry Smith .ve
6007a1cb98faSBarry Smith 
6008a1cb98faSBarry Smith   Developer Note:
6009a1cb98faSBarry Smith   Should use a `PetscViewer` not a filename
6010a1cb98faSBarry Smith 
60116afe31f6SMartin Diehl .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromFile()`, `DMPlexCreateGmsh()`, `DMPlexCreate()`
6012a4e35b19SJacob Faibussowitsch */
6013ff6a9541SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCellVertexFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
6014d71ae5a4SJacob Faibussowitsch {
60158ca92349SMatthew G. Knepley   DMLabel      marker;
60168ca92349SMatthew G. Knepley   PetscViewer  viewer;
60178ca92349SMatthew G. Knepley   Vec          coordinates;
60188ca92349SMatthew G. Knepley   PetscSection coordSection;
60198ca92349SMatthew G. Knepley   PetscScalar *coords;
60208ca92349SMatthew G. Knepley   char         line[PETSC_MAX_PATH_LEN];
6021d0812dedSMatthew G. Knepley   PetscInt     cdim, coordSize, v, c, d;
60228ca92349SMatthew G. Knepley   PetscMPIInt  rank;
6023d0812dedSMatthew G. Knepley   int          snum, dim, Nv, Nc, Ncn, Nl;
60248ca92349SMatthew G. Knepley 
60258ca92349SMatthew G. Knepley   PetscFunctionBegin;
60269566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
60279566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, &viewer));
60289566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(viewer, PETSCVIEWERASCII));
60299566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetMode(viewer, FILE_MODE_READ));
60309566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetName(viewer, filename));
6031dd400576SPatrick Sanan   if (rank == 0) {
6032d0812dedSMatthew G. Knepley     PetscCall(PetscViewerRead(viewer, line, 5, NULL, PETSC_STRING));
6033d0812dedSMatthew G. Knepley     snum = sscanf(line, "%d %d %d %d %d", &dim, &Nc, &Nv, &Ncn, &Nl);
6034d0812dedSMatthew G. Knepley     PetscCheck(snum == 5, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
603525ce1634SJed Brown   } else {
6036f8d5e320SMatthew G. Knepley     Nc = Nv = Ncn = Nl = 0;
60378ca92349SMatthew G. Knepley   }
6038d0812dedSMatthew G. Knepley   PetscCallMPI(MPI_Bcast(&dim, 1, MPI_INT, 0, comm));
6039835f2295SStefano Zampini   cdim = dim;
60409566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
60419566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
60429566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(*dm, 0, Nc + Nv));
6043835f2295SStefano Zampini   PetscCall(DMSetDimension(*dm, dim));
60449566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*dm, cdim));
60458ca92349SMatthew G. Knepley   /* Read topology */
6046dd400576SPatrick Sanan   if (rank == 0) {
6047f8d5e320SMatthew G. Knepley     char     format[PETSC_MAX_PATH_LEN];
6048f8d5e320SMatthew G. Knepley     PetscInt cone[8];
60498ca92349SMatthew G. Knepley     int      vbuf[8], v;
60508ca92349SMatthew G. Knepley 
60519371c9d4SSatish Balay     for (c = 0; c < Ncn; ++c) {
60529371c9d4SSatish Balay       format[c * 3 + 0] = '%';
60539371c9d4SSatish Balay       format[c * 3 + 1] = 'd';
60549371c9d4SSatish Balay       format[c * 3 + 2] = ' ';
60559371c9d4SSatish Balay     }
6056f8d5e320SMatthew G. Knepley     format[Ncn * 3 - 1] = '\0';
60579566063dSJacob Faibussowitsch     for (c = 0; c < Nc; ++c) PetscCall(DMPlexSetConeSize(*dm, c, Ncn));
60589566063dSJacob Faibussowitsch     PetscCall(DMSetUp(*dm));
60598ca92349SMatthew G. Knepley     for (c = 0; c < Nc; ++c) {
60609566063dSJacob Faibussowitsch       PetscCall(PetscViewerRead(viewer, line, Ncn, NULL, PETSC_STRING));
6061f8d5e320SMatthew G. Knepley       switch (Ncn) {
6062d71ae5a4SJacob Faibussowitsch       case 2:
6063d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1]);
6064d71ae5a4SJacob Faibussowitsch         break;
6065d71ae5a4SJacob Faibussowitsch       case 3:
6066d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2]);
6067d71ae5a4SJacob Faibussowitsch         break;
6068d71ae5a4SJacob Faibussowitsch       case 4:
6069d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3]);
6070d71ae5a4SJacob Faibussowitsch         break;
6071d71ae5a4SJacob Faibussowitsch       case 6:
6072d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5]);
6073d71ae5a4SJacob Faibussowitsch         break;
6074d71ae5a4SJacob Faibussowitsch       case 8:
6075d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5], &vbuf[6], &vbuf[7]);
6076d71ae5a4SJacob Faibussowitsch         break;
6077d71ae5a4SJacob Faibussowitsch       default:
6078d71ae5a4SJacob Faibussowitsch         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No cell shape with %d vertices", Ncn);
6079f8d5e320SMatthew G. Knepley       }
608008401ef6SPierre Jolivet       PetscCheck(snum == Ncn, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
6081f8d5e320SMatthew G. Knepley       for (v = 0; v < Ncn; ++v) cone[v] = vbuf[v] + Nc;
60828ca92349SMatthew G. Knepley       /* Hexahedra are inverted */
6083f8d5e320SMatthew G. Knepley       if (Ncn == 8) {
60848ca92349SMatthew G. Knepley         PetscInt tmp = cone[1];
60858ca92349SMatthew G. Knepley         cone[1]      = cone[3];
60868ca92349SMatthew G. Knepley         cone[3]      = tmp;
60878ca92349SMatthew G. Knepley       }
60889566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(*dm, c, cone));
60898ca92349SMatthew G. Knepley     }
60908ca92349SMatthew G. Knepley   }
60919566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(*dm));
60929566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(*dm));
60938ca92349SMatthew G. Knepley   /* Read coordinates */
60949566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(*dm, &coordSection));
60959566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
60969566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, cdim));
60979566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, Nc, Nc + Nv));
60988ca92349SMatthew G. Knepley   for (v = Nc; v < Nc + Nv; ++v) {
60999566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, cdim));
61009566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, cdim));
61018ca92349SMatthew G. Knepley   }
61029566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
61039566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
61049566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
61059566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
61069566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
61079566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, cdim));
61089566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
61099566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
6110dd400576SPatrick Sanan   if (rank == 0) {
6111f8d5e320SMatthew G. Knepley     char   format[PETSC_MAX_PATH_LEN];
61128ca92349SMatthew G. Knepley     double x[3];
6113f8d5e320SMatthew G. Knepley     int    l, val[3];
61148ca92349SMatthew G. Knepley 
6115f8d5e320SMatthew G. Knepley     if (Nl) {
61169371c9d4SSatish Balay       for (l = 0; l < Nl; ++l) {
61179371c9d4SSatish Balay         format[l * 3 + 0] = '%';
61189371c9d4SSatish Balay         format[l * 3 + 1] = 'd';
61199371c9d4SSatish Balay         format[l * 3 + 2] = ' ';
61209371c9d4SSatish Balay       }
6121f8d5e320SMatthew G. Knepley       format[Nl * 3 - 1] = '\0';
61229566063dSJacob Faibussowitsch       PetscCall(DMCreateLabel(*dm, "marker"));
61239566063dSJacob Faibussowitsch       PetscCall(DMGetLabel(*dm, "marker", &marker));
6124f8d5e320SMatthew G. Knepley     }
61258ca92349SMatthew G. Knepley     for (v = 0; v < Nv; ++v) {
61269566063dSJacob Faibussowitsch       PetscCall(PetscViewerRead(viewer, line, 3 + Nl, NULL, PETSC_STRING));
6127f8d5e320SMatthew G. Knepley       snum = sscanf(line, "%lg %lg %lg", &x[0], &x[1], &x[2]);
612808401ef6SPierre Jolivet       PetscCheck(snum == 3, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
6129f8d5e320SMatthew G. Knepley       switch (Nl) {
6130d71ae5a4SJacob Faibussowitsch       case 0:
6131d71ae5a4SJacob Faibussowitsch         snum = 0;
6132d71ae5a4SJacob Faibussowitsch         break;
6133d71ae5a4SJacob Faibussowitsch       case 1:
6134d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &val[0]);
6135d71ae5a4SJacob Faibussowitsch         break;
6136d71ae5a4SJacob Faibussowitsch       case 2:
6137d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &val[0], &val[1]);
6138d71ae5a4SJacob Faibussowitsch         break;
6139d71ae5a4SJacob Faibussowitsch       case 3:
6140d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &val[0], &val[1], &val[2]);
6141d71ae5a4SJacob Faibussowitsch         break;
6142d71ae5a4SJacob Faibussowitsch       default:
6143d71ae5a4SJacob Faibussowitsch         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Request support for %d labels", Nl);
6144f8d5e320SMatthew G. Knepley       }
614508401ef6SPierre Jolivet       PetscCheck(snum == Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
61468ca92349SMatthew G. Knepley       for (d = 0; d < cdim; ++d) coords[v * cdim + d] = x[d];
61479566063dSJacob Faibussowitsch       for (l = 0; l < Nl; ++l) PetscCall(DMLabelSetValue(marker, v + Nc, val[l]));
61488ca92349SMatthew G. Knepley     }
61498ca92349SMatthew G. Knepley   }
61509566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
61519566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(*dm, coordinates));
61529566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
61539566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(&viewer));
61548ca92349SMatthew G. Knepley   if (interpolate) {
61558ca92349SMatthew G. Knepley     DM      idm;
61568ca92349SMatthew G. Knepley     DMLabel bdlabel;
61578ca92349SMatthew G. Knepley 
61589566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
61599566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
61608ca92349SMatthew G. Knepley     *dm = idm;
61618ca92349SMatthew G. Knepley 
6162f8d5e320SMatthew G. Knepley     if (!Nl) {
61639566063dSJacob Faibussowitsch       PetscCall(DMCreateLabel(*dm, "marker"));
61649566063dSJacob Faibussowitsch       PetscCall(DMGetLabel(*dm, "marker", &bdlabel));
61659566063dSJacob Faibussowitsch       PetscCall(DMPlexMarkBoundaryFaces(*dm, PETSC_DETERMINE, bdlabel));
61669566063dSJacob Faibussowitsch       PetscCall(DMPlexLabelComplete(*dm, bdlabel));
61678ca92349SMatthew G. Knepley     }
6168f8d5e320SMatthew G. Knepley   }
61693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
61708ca92349SMatthew G. Knepley }
61718ca92349SMatthew G. Knepley 
6172cc4c1da9SBarry Smith /*@
6173a1cb98faSBarry Smith   DMPlexCreateFromFile - This takes a filename and produces a `DM`
6174a1cb98faSBarry Smith 
6175a1cb98faSBarry Smith   Collective
6176ca522641SMatthew G. Knepley 
6177ca522641SMatthew G. Knepley   Input Parameters:
6178ca522641SMatthew G. Knepley + comm        - The communicator
6179ca522641SMatthew G. Knepley . filename    - A file name
6180a1cb98faSBarry Smith . plexname    - The object name of the resulting `DM`, also used for intra-datafile lookup by some formats
6181ca522641SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
6182ca522641SMatthew G. Knepley 
6183ca522641SMatthew G. Knepley   Output Parameter:
6184a1cb98faSBarry Smith . dm - The `DM`
6185ca522641SMatthew G. Knepley 
6186a1cb98faSBarry Smith   Options Database Key:
6187a1cb98faSBarry Smith . -dm_plex_create_from_hdf5_xdmf - use the `PETSC_VIEWER_HDF5_XDMF` format for reading HDF5
618802ef0d99SVaclav Hapla 
618937fdd005SBarry Smith   Use `-dm_plex_create_ prefix` to pass options to the internal `PetscViewer`, e.g.
6190bca97951SVaclav Hapla $ -dm_plex_create_viewer_hdf5_collective
6191bca97951SVaclav Hapla 
6192ca522641SMatthew G. Knepley   Level: beginner
6193ca522641SMatthew G. Knepley 
6194a1cb98faSBarry Smith   Notes:
6195a1cb98faSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
6196a1cb98faSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
6197a1cb98faSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
6198a1cb98faSBarry Smith   The input parameter name is thus used to name the `DMPLEX` object when `DMPlexCreateFromFile()` internally
6199a1cb98faSBarry Smith   calls `DMLoad()`. Currently, name is ignored for other viewer types and/or formats.
6200a1cb98faSBarry Smith 
62011cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromDAG()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`, `PetscObjectSetName()`, `DMView()`, `DMLoad()`
6202ca522641SMatthew G. Knepley @*/
6203d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateFromFile(MPI_Comm comm, const char filename[], const char plexname[], PetscBool interpolate, DM *dm)
6204d71ae5a4SJacob Faibussowitsch {
6205ef3a5affSJacob Faibussowitsch   const char  extGmsh[]      = ".msh";
6206ef3a5affSJacob Faibussowitsch   const char  extGmsh2[]     = ".msh2";
6207ef3a5affSJacob Faibussowitsch   const char  extGmsh4[]     = ".msh4";
6208ef3a5affSJacob Faibussowitsch   const char  extCGNS[]      = ".cgns";
6209ef3a5affSJacob Faibussowitsch   const char  extExodus[]    = ".exo";
6210ef3a5affSJacob Faibussowitsch   const char  extExodus_e[]  = ".e";
6211ef3a5affSJacob Faibussowitsch   const char  extGenesis[]   = ".gen";
6212ef3a5affSJacob Faibussowitsch   const char  extFluent[]    = ".cas";
6213ef3a5affSJacob Faibussowitsch   const char  extHDF5[]      = ".h5";
62146f2c871aSStefano Zampini   const char  extXDMFHDF5[]  = ".xdmf.h5";
6215ef3a5affSJacob Faibussowitsch   const char  extPLY[]       = ".ply";
6216ef3a5affSJacob Faibussowitsch   const char  extEGADSLite[] = ".egadslite";
6217ef3a5affSJacob Faibussowitsch   const char  extEGADS[]     = ".egads";
6218ef3a5affSJacob Faibussowitsch   const char  extIGES[]      = ".igs";
6219ef3a5affSJacob Faibussowitsch   const char  extSTEP[]      = ".stp";
6220ef3a5affSJacob Faibussowitsch   const char  extCV[]        = ".dat";
6221ca522641SMatthew G. Knepley   size_t      len;
62226afe31f6SMartin Diehl   PetscBool   isGmsh, isGmsh2, isGmsh4, isCGNS, isExodus, isGenesis, isFluent, isHDF5, isPLY, isEGADSLite, isEGADS, isIGES, isSTEP, isCV, isXDMFHDF5;
6223ca522641SMatthew G. Knepley   PetscMPIInt rank;
6224ca522641SMatthew G. Knepley 
6225ca522641SMatthew G. Knepley   PetscFunctionBegin;
62264f572ea9SToby Isaac   PetscAssertPointer(filename, 2);
62274f572ea9SToby Isaac   if (plexname) PetscAssertPointer(plexname, 3);
62284f572ea9SToby Isaac   PetscAssertPointer(dm, 5);
62299566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
62309566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_CreateFromFile, 0, 0, 0, 0));
62319566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
62329566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(filename, &len));
623328b400f6SJacob Faibussowitsch   PetscCheck(len, comm, PETSC_ERR_ARG_WRONG, "Filename must be a valid path");
6234ef3a5affSJacob Faibussowitsch 
62359371c9d4SSatish Balay #define CheckExtension(extension__, is_extension__) \
62369371c9d4SSatish Balay   do { \
6237274aaeaaSJacob Faibussowitsch     PetscAssert(sizeof(extension__), comm, PETSC_ERR_PLIB, "Zero-size extension: %s", extension__); \
6238274aaeaaSJacob Faibussowitsch     /* don't count the null-terminator at the end */ \
6239274aaeaaSJacob Faibussowitsch     const size_t ext_len = sizeof(extension__) - 1; \
6240274aaeaaSJacob Faibussowitsch     if (len < ext_len) { \
6241ef3a5affSJacob Faibussowitsch       is_extension__ = PETSC_FALSE; \
6242ef3a5affSJacob Faibussowitsch     } else { \
6243274aaeaaSJacob Faibussowitsch       PetscCall(PetscStrncmp(filename + len - ext_len, extension__, ext_len, &is_extension__)); \
6244ef3a5affSJacob Faibussowitsch     } \
6245ef3a5affSJacob Faibussowitsch   } while (0)
6246ef3a5affSJacob Faibussowitsch 
6247ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh, isGmsh);
6248ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh2, isGmsh2);
6249ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh4, isGmsh4);
6250ef3a5affSJacob Faibussowitsch   CheckExtension(extCGNS, isCGNS);
6251ef3a5affSJacob Faibussowitsch   CheckExtension(extExodus, isExodus);
6252ef3a5affSJacob Faibussowitsch   if (!isExodus) CheckExtension(extExodus_e, isExodus);
6253ef3a5affSJacob Faibussowitsch   CheckExtension(extGenesis, isGenesis);
6254ef3a5affSJacob Faibussowitsch   CheckExtension(extFluent, isFluent);
6255ef3a5affSJacob Faibussowitsch   CheckExtension(extHDF5, isHDF5);
6256ef3a5affSJacob Faibussowitsch   CheckExtension(extPLY, isPLY);
6257ef3a5affSJacob Faibussowitsch   CheckExtension(extEGADSLite, isEGADSLite);
6258ef3a5affSJacob Faibussowitsch   CheckExtension(extEGADS, isEGADS);
6259ef3a5affSJacob Faibussowitsch   CheckExtension(extIGES, isIGES);
6260ef3a5affSJacob Faibussowitsch   CheckExtension(extSTEP, isSTEP);
6261ef3a5affSJacob Faibussowitsch   CheckExtension(extCV, isCV);
62626f2c871aSStefano Zampini   CheckExtension(extXDMFHDF5, isXDMFHDF5);
6263ef3a5affSJacob Faibussowitsch 
6264ef3a5affSJacob Faibussowitsch #undef CheckExtension
6265ef3a5affSJacob Faibussowitsch 
6266de78e4feSLisandro Dalcin   if (isGmsh || isGmsh2 || isGmsh4) {
62679566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateGmshFromFile(comm, filename, interpolate, dm));
6268ca522641SMatthew G. Knepley   } else if (isCGNS) {
62699566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCGNSFromFile(comm, filename, interpolate, dm));
627090c68965SMatthew G. Knepley   } else if (isExodus || isGenesis) {
62719566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateExodusFromFile(comm, filename, interpolate, dm));
62722f0bd6dcSMichael Lange   } else if (isFluent) {
62739566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFluentFromFile(comm, filename, interpolate, dm));
6274cc2f8f65SMatthew G. Knepley   } else if (isHDF5) {
6275cc2f8f65SMatthew G. Knepley     PetscViewer viewer;
6276cc2f8f65SMatthew G. Knepley 
627743b242b4SVaclav 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 */
62786f2c871aSStefano Zampini     PetscCall(PetscOptionsGetBool(NULL, NULL, "-dm_plex_create_from_hdf5_xdmf", &isXDMFHDF5, NULL));
62799566063dSJacob Faibussowitsch     PetscCall(PetscViewerCreate(comm, &viewer));
62809566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetType(viewer, PETSCVIEWERHDF5));
62819566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetOptionsPrefix(viewer, "dm_plex_create_"));
62829566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetFromOptions(viewer));
62839566063dSJacob Faibussowitsch     PetscCall(PetscViewerFileSetMode(viewer, FILE_MODE_READ));
62849566063dSJacob Faibussowitsch     PetscCall(PetscViewerFileSetName(viewer, filename));
6285cd7e8a5eSksagiyam 
62869566063dSJacob Faibussowitsch     PetscCall(DMCreate(comm, dm));
6287f4f49eeaSPierre Jolivet     PetscCall(PetscObjectSetName((PetscObject)*dm, plexname));
62889566063dSJacob Faibussowitsch     PetscCall(DMSetType(*dm, DMPLEX));
62896f2c871aSStefano Zampini     if (isXDMFHDF5) PetscCall(PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_XDMF));
62909566063dSJacob Faibussowitsch     PetscCall(DMLoad(*dm, viewer));
62916f2c871aSStefano Zampini     if (isXDMFHDF5) PetscCall(PetscViewerPopFormat(viewer));
62929566063dSJacob Faibussowitsch     PetscCall(PetscViewerDestroy(&viewer));
62935fd9971aSMatthew G. Knepley 
62945fd9971aSMatthew G. Knepley     if (interpolate) {
62955fd9971aSMatthew G. Knepley       DM idm;
62965fd9971aSMatthew G. Knepley 
62979566063dSJacob Faibussowitsch       PetscCall(DMPlexInterpolate(*dm, &idm));
62989566063dSJacob Faibussowitsch       PetscCall(DMDestroy(dm));
62995fd9971aSMatthew G. Knepley       *dm = idm;
63005fd9971aSMatthew G. Knepley     }
6301f2801cd6SMatthew G. Knepley   } else if (isPLY) {
63029566063dSJacob Faibussowitsch     PetscCall(DMPlexCreatePLYFromFile(comm, filename, interpolate, dm));
6303c1cad2e7SMatthew G. Knepley   } else if (isEGADSLite || isEGADS || isIGES || isSTEP) {
63049566063dSJacob Faibussowitsch     if (isEGADSLite) PetscCall(DMPlexCreateEGADSLiteFromFile(comm, filename, dm));
63059566063dSJacob Faibussowitsch     else PetscCall(DMPlexCreateEGADSFromFile(comm, filename, dm));
63067bee2925SMatthew Knepley     if (!interpolate) {
63077bee2925SMatthew Knepley       DM udm;
63087bee2925SMatthew Knepley 
63099566063dSJacob Faibussowitsch       PetscCall(DMPlexUninterpolate(*dm, &udm));
63109566063dSJacob Faibussowitsch       PetscCall(DMDestroy(dm));
63117bee2925SMatthew Knepley       *dm = udm;
63127bee2925SMatthew Knepley     }
63138ca92349SMatthew G. Knepley   } else if (isCV) {
63149566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCellVertexFromFile(comm, filename, interpolate, dm));
631598921bdaSJacob Faibussowitsch   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot load file %s: unrecognized extension", filename);
63169566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(plexname, &len));
6317f4f49eeaSPierre Jolivet   if (len) PetscCall(PetscObjectSetName((PetscObject)*dm, plexname));
63189566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_CreateFromFile, 0, 0, 0, 0));
63193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6320ca522641SMatthew G. Knepley }
632112b8a6daSStefano Zampini 
6322cc4c1da9SBarry Smith /*@
63239f6c5813SMatthew 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.
63249f6c5813SMatthew G. Knepley 
63250528010dSStefano Zampini   Input Parameters:
63260528010dSStefano Zampini + tr     - The `DMPlexTransform`
63270528010dSStefano Zampini - prefix - An options prefix, or NULL
63289f6c5813SMatthew G. Knepley 
63299f6c5813SMatthew G. Knepley   Output Parameter:
63309f6c5813SMatthew G. Knepley . dm - The `DM`
63319f6c5813SMatthew G. Knepley 
63329f6c5813SMatthew G. Knepley   Level: beginner
63339f6c5813SMatthew G. Knepley 
633420f4b53cSBarry Smith   Notes:
633520f4b53cSBarry 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.
633620f4b53cSBarry Smith 
63379f6c5813SMatthew G. Knepley .seealso: `DMPlexCreateFromFile`, `DMPlexCreateFromDAG()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`
63389f6c5813SMatthew G. Knepley @*/
63390528010dSStefano Zampini PetscErrorCode DMPlexCreateEphemeral(DMPlexTransform tr, const char prefix[], DM *dm)
63409f6c5813SMatthew G. Knepley {
63410528010dSStefano Zampini   DM           bdm, bcdm, cdm;
63420528010dSStefano Zampini   Vec          coordinates, coordinatesNew;
63430528010dSStefano Zampini   PetscSection cs;
6344817b2c36SMatthew G. Knepley   PetscInt     cdim, Nl;
63459f6c5813SMatthew G. Knepley 
63469f6c5813SMatthew G. Knepley   PetscFunctionBegin;
63479f6c5813SMatthew G. Knepley   PetscCall(DMCreate(PetscObjectComm((PetscObject)tr), dm));
63489f6c5813SMatthew G. Knepley   PetscCall(DMSetType(*dm, DMPLEX));
63490528010dSStefano Zampini   ((DM_Plex *)(*dm)->data)->interpolated = DMPLEX_INTERPOLATED_FULL;
63500528010dSStefano Zampini   // Handle coordinates
63510528010dSStefano Zampini   PetscCall(DMPlexTransformGetDM(tr, &bdm));
6352817b2c36SMatthew G. Knepley   PetscCall(DMPlexTransformSetDimensions(tr, bdm, *dm));
6353817b2c36SMatthew G. Knepley   PetscCall(DMGetCoordinateDim(*dm, &cdim));
63540528010dSStefano Zampini   PetscCall(DMGetCoordinateDM(bdm, &bcdm));
63550528010dSStefano Zampini   PetscCall(DMGetCoordinateDM(*dm, &cdm));
63560528010dSStefano Zampini   PetscCall(DMCopyDisc(bcdm, cdm));
63570528010dSStefano Zampini   PetscCall(DMGetLocalSection(cdm, &cs));
63580528010dSStefano Zampini   PetscCall(PetscSectionSetNumFields(cs, 1));
63590528010dSStefano Zampini   PetscCall(PetscSectionSetFieldComponents(cs, 0, cdim));
63600528010dSStefano Zampini   PetscCall(DMGetCoordinatesLocal(bdm, &coordinates));
63610528010dSStefano Zampini   PetscCall(VecDuplicate(coordinates, &coordinatesNew));
63620528010dSStefano Zampini   PetscCall(VecCopy(coordinates, coordinatesNew));
63630528010dSStefano Zampini   PetscCall(DMSetCoordinatesLocal(*dm, coordinatesNew));
63640528010dSStefano Zampini   PetscCall(VecDestroy(&coordinatesNew));
63659f6c5813SMatthew G. Knepley 
63669f6c5813SMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)tr));
63679f6c5813SMatthew G. Knepley   PetscCall(DMPlexTransformDestroy(&((DM_Plex *)(*dm)->data)->tr));
63689f6c5813SMatthew G. Knepley   ((DM_Plex *)(*dm)->data)->tr = tr;
63690528010dSStefano Zampini   PetscCall(DMPlexDistributeSetDefault(*dm, PETSC_FALSE));
63700528010dSStefano Zampini   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)*dm, prefix));
63710528010dSStefano Zampini   PetscCall(DMSetFromOptions(*dm));
63729f6c5813SMatthew G. Knepley 
63739f6c5813SMatthew G. Knepley   PetscCall(DMGetNumLabels(bdm, &Nl));
63749f6c5813SMatthew G. Knepley   for (PetscInt l = 0; l < Nl; ++l) {
63759f6c5813SMatthew G. Knepley     DMLabel     label, labelNew;
63769f6c5813SMatthew G. Knepley     const char *lname;
63779f6c5813SMatthew G. Knepley     PetscBool   isDepth, isCellType;
63789f6c5813SMatthew G. Knepley 
63799f6c5813SMatthew G. Knepley     PetscCall(DMGetLabelName(bdm, l, &lname));
63809f6c5813SMatthew G. Knepley     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
63819f6c5813SMatthew G. Knepley     if (isDepth) continue;
63829f6c5813SMatthew G. Knepley     PetscCall(PetscStrcmp(lname, "celltype", &isCellType));
63839f6c5813SMatthew G. Knepley     if (isCellType) continue;
63849f6c5813SMatthew G. Knepley     PetscCall(DMCreateLabel(*dm, lname));
63859f6c5813SMatthew G. Knepley     PetscCall(DMGetLabel(bdm, lname, &label));
63869f6c5813SMatthew G. Knepley     PetscCall(DMGetLabel(*dm, lname, &labelNew));
63879f6c5813SMatthew G. Knepley     PetscCall(DMLabelSetType(labelNew, DMLABELEPHEMERAL));
63889f6c5813SMatthew G. Knepley     PetscCall(DMLabelEphemeralSetLabel(labelNew, label));
63899f6c5813SMatthew G. Knepley     PetscCall(DMLabelEphemeralSetTransform(labelNew, tr));
63909f6c5813SMatthew G. Knepley     PetscCall(DMLabelSetUp(labelNew));
63919f6c5813SMatthew G. Knepley   }
63923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
63939f6c5813SMatthew G. Knepley }
6394