xref: /petsc/src/dm/impls/plex/plexcreate.c (revision d7d2d1d2babbf975e9544da80073779625dd96d9)
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
849*d7d2d1d2SJames Wright static PetscErrorCode DMPlexSetBoxLabel_Internal(DM dm, const DMBoundaryType periodicity[])
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));
972*d7d2d1d2SJames Wright 
973*d7d2d1d2SJames Wright   // Create Isoperiodic SF from newly-created face labels
974*d7d2d1d2SJames Wright   PetscSF     periodicsfs[3];
975*d7d2d1d2SJames Wright   PetscInt    periodic_sf_index  = 0;
976*d7d2d1d2SJames Wright   PetscScalar transform[3][4][4] = {{{0.}}};
977*d7d2d1d2SJames Wright   for (PetscInt d = 0; d < dim; d++) {
978*d7d2d1d2SJames Wright     IS              donor_is, periodic_is;
979*d7d2d1d2SJames Wright     const PetscInt *donor_faces = NULL, *periodic_faces = NULL;
980*d7d2d1d2SJames Wright     PetscInt        num_donor = 0, num_periodic = 0;
981*d7d2d1d2SJames Wright     PetscSF         centroidsf;
982*d7d2d1d2SJames Wright     PetscReal       donor_to_periodic_distance;
983*d7d2d1d2SJames Wright     const PetscInt  face_pairings[2][3][2] = {
984*d7d2d1d2SJames Wright       // 2D face pairings, {donor, periodic}
985*d7d2d1d2SJames Wright       {{4, 2}, {1, 3}},
986*d7d2d1d2SJames Wright       // 3D face pairings
987*d7d2d1d2SJames Wright       {{5, 6}, {3, 4}, {1, 2}}
988*d7d2d1d2SJames Wright     };
989*d7d2d1d2SJames Wright 
990*d7d2d1d2SJames Wright     if (periodicity[d] != DM_BOUNDARY_PERIODIC) continue;
991*d7d2d1d2SJames Wright     {
992*d7d2d1d2SJames Wright       // Compute centroidsf, which is the mapping from donor faces to periodic faces
993*d7d2d1d2SJames Wright       // Matches the centroid of the faces together, ignoring the periodic direction component (which should not match between donor and periodic face)
994*d7d2d1d2SJames Wright       PetscInt     coords_size, centroid_comps = dim - 1;
995*d7d2d1d2SJames Wright       PetscScalar *coords = NULL;
996*d7d2d1d2SJames Wright       PetscReal   *donor_centroids, *periodic_centroids;
997*d7d2d1d2SJames Wright       PetscReal    loc_periodic[2] = {PETSC_MIN_REAL, PETSC_MIN_REAL}, loc_periodic_global[2]; // Location of donor (0) and periodic (1) faces in periodic direction
998*d7d2d1d2SJames Wright 
999*d7d2d1d2SJames Wright       PetscCall(DMGetStratumIS(dm, "Face Sets", face_pairings[dim - 2][d][0], &donor_is));
1000*d7d2d1d2SJames Wright       PetscCall(DMGetStratumIS(dm, "Face Sets", face_pairings[dim - 2][d][1], &periodic_is));
1001*d7d2d1d2SJames Wright       if (donor_is) {
1002*d7d2d1d2SJames Wright         PetscCall(ISGetLocalSize(donor_is, &num_donor));
1003*d7d2d1d2SJames Wright         PetscCall(ISGetIndices(donor_is, &donor_faces));
1004*d7d2d1d2SJames Wright       }
1005*d7d2d1d2SJames Wright       if (periodic_is) {
1006*d7d2d1d2SJames Wright         PetscCall(ISGetLocalSize(periodic_is, &num_periodic));
1007*d7d2d1d2SJames Wright         PetscCall(ISGetIndices(periodic_is, &periodic_faces));
1008*d7d2d1d2SJames Wright       }
1009*d7d2d1d2SJames Wright       PetscCall(PetscCalloc2(num_donor * centroid_comps, &donor_centroids, num_periodic * centroid_comps, &periodic_centroids));
1010*d7d2d1d2SJames Wright       for (PetscInt f = 0; f < num_donor; f++) {
1011*d7d2d1d2SJames Wright         PetscInt face = donor_faces[f], num_coords;
1012*d7d2d1d2SJames Wright         PetscCall(DMPlexVecGetClosureAtDepth_Internal(cdm, csection, coordinates, face, 0, &coords_size, &coords));
1013*d7d2d1d2SJames Wright         num_coords = coords_size / dim;
1014*d7d2d1d2SJames Wright         for (PetscInt c = 0; c < num_coords; c++) {
1015*d7d2d1d2SJames Wright           PetscInt comp_index = 0;
1016*d7d2d1d2SJames Wright           loc_periodic[0]     = PetscRealPart(coords[c * dim + d]);
1017*d7d2d1d2SJames Wright           for (PetscInt i = 0; i < dim; i++) {
1018*d7d2d1d2SJames Wright             if (i == d) continue; // Periodic direction not used for centroid calculation
1019*d7d2d1d2SJames Wright             donor_centroids[f * centroid_comps + comp_index] += PetscRealPart(coords[c * dim + i]) / num_coords;
1020*d7d2d1d2SJames Wright             comp_index++;
1021*d7d2d1d2SJames Wright           }
1022*d7d2d1d2SJames Wright         }
1023*d7d2d1d2SJames Wright         PetscCall(DMPlexVecRestoreClosure(cdm, csection, coordinates, face, &coords_size, &coords));
1024*d7d2d1d2SJames Wright       }
1025*d7d2d1d2SJames Wright 
1026*d7d2d1d2SJames Wright       for (PetscInt f = 0; f < num_periodic; f++) {
1027*d7d2d1d2SJames Wright         PetscInt face = periodic_faces[f], num_coords;
1028*d7d2d1d2SJames Wright         PetscCall(DMPlexVecGetClosureAtDepth_Internal(cdm, csection, coordinates, face, 0, &coords_size, &coords));
1029*d7d2d1d2SJames Wright         num_coords = coords_size / dim;
1030*d7d2d1d2SJames Wright         for (PetscInt c = 0; c < num_coords; c++) {
1031*d7d2d1d2SJames Wright           PetscInt comp_index = 0;
1032*d7d2d1d2SJames Wright           loc_periodic[1]     = PetscRealPart(coords[c * dim + d]);
1033*d7d2d1d2SJames Wright           for (PetscInt i = 0; i < dim; i++) {
1034*d7d2d1d2SJames Wright             if (i == d) continue; // Periodic direction not used for centroid calculation
1035*d7d2d1d2SJames Wright             periodic_centroids[f * centroid_comps + comp_index] += PetscRealPart(coords[c * dim + i]) / num_coords;
1036*d7d2d1d2SJames Wright             comp_index++;
1037*d7d2d1d2SJames Wright           }
1038*d7d2d1d2SJames Wright         }
1039*d7d2d1d2SJames Wright         PetscCall(DMPlexVecRestoreClosure(cdm, csection, coordinates, face, &coords_size, &coords));
1040*d7d2d1d2SJames Wright       }
1041*d7d2d1d2SJames Wright       PetscCallMPI(MPIU_Allreduce(loc_periodic, loc_periodic_global, 2, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject)dm)));
1042*d7d2d1d2SJames Wright       donor_to_periodic_distance = loc_periodic_global[1] - loc_periodic_global[0];
1043*d7d2d1d2SJames Wright 
1044*d7d2d1d2SJames Wright       PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &centroidsf));
1045*d7d2d1d2SJames Wright       PetscCall(PetscSFSetGraphFromCoordinates(centroidsf, num_donor, num_periodic, centroid_comps, 1e-10, donor_centroids, periodic_centroids));
1046*d7d2d1d2SJames Wright       PetscCall(PetscSFViewFromOptions(centroidsf, NULL, "-dm_plex_box_label_centroid_sf_view"));
1047*d7d2d1d2SJames Wright       PetscCall(PetscFree2(donor_centroids, periodic_centroids));
1048*d7d2d1d2SJames Wright     }
1049*d7d2d1d2SJames Wright 
1050*d7d2d1d2SJames Wright     { // Create Isoperiodic SF using centroidsSF
1051*d7d2d1d2SJames Wright       PetscInt           pStart, pEnd;
1052*d7d2d1d2SJames Wright       PetscInt          *leaf_faces;
1053*d7d2d1d2SJames Wright       const PetscSFNode *firemote;
1054*d7d2d1d2SJames Wright       PetscSFNode       *isoperiodic_leaves;
1055*d7d2d1d2SJames Wright 
1056*d7d2d1d2SJames Wright       PetscCall(PetscMalloc1(num_periodic, &leaf_faces));
1057*d7d2d1d2SJames Wright       PetscCall(PetscSFBcastBegin(centroidsf, MPIU_INT, donor_faces, leaf_faces, MPI_REPLACE));
1058*d7d2d1d2SJames Wright       PetscCall(PetscSFBcastEnd(centroidsf, MPIU_INT, donor_faces, leaf_faces, MPI_REPLACE));
1059*d7d2d1d2SJames Wright 
1060*d7d2d1d2SJames Wright       PetscCall(PetscMalloc1(num_periodic, &isoperiodic_leaves));
1061*d7d2d1d2SJames Wright       PetscCall(PetscSFGetGraph(centroidsf, NULL, NULL, NULL, &firemote));
1062*d7d2d1d2SJames Wright       for (PetscInt l = 0; l < num_periodic; ++l) {
1063*d7d2d1d2SJames Wright         isoperiodic_leaves[l].index = leaf_faces[l];
1064*d7d2d1d2SJames Wright         isoperiodic_leaves[l].rank  = firemote[l].rank;
1065*d7d2d1d2SJames Wright       }
1066*d7d2d1d2SJames Wright 
1067*d7d2d1d2SJames Wright       PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
1068*d7d2d1d2SJames Wright       PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &periodicsfs[periodic_sf_index]));
1069*d7d2d1d2SJames Wright       PetscCall(PetscSFSetGraph(periodicsfs[periodic_sf_index], pEnd - pStart, num_periodic, (PetscInt *)periodic_faces, PETSC_COPY_VALUES, isoperiodic_leaves, PETSC_OWN_POINTER));
1070*d7d2d1d2SJames Wright       PetscCall(PetscSFViewFromOptions(periodicsfs[periodic_sf_index], NULL, "-dm_plex_box_label_periodic_sf_view"));
1071*d7d2d1d2SJames Wright       PetscCall(PetscFree(leaf_faces));
1072*d7d2d1d2SJames Wright     }
1073*d7d2d1d2SJames Wright 
1074*d7d2d1d2SJames Wright     transform[periodic_sf_index][0][0] = 1;
1075*d7d2d1d2SJames Wright     transform[periodic_sf_index][1][1] = 1;
1076*d7d2d1d2SJames Wright     transform[periodic_sf_index][2][2] = 1;
1077*d7d2d1d2SJames Wright     transform[periodic_sf_index][3][3] = 1;
1078*d7d2d1d2SJames Wright     transform[periodic_sf_index][d][3] = donor_to_periodic_distance;
1079*d7d2d1d2SJames Wright 
1080*d7d2d1d2SJames Wright     periodic_sf_index++;
1081*d7d2d1d2SJames Wright     PetscCall(PetscSFDestroy(&centroidsf));
1082*d7d2d1d2SJames Wright     if (donor_is) {
1083*d7d2d1d2SJames Wright       PetscCall(ISRestoreIndices(donor_is, &donor_faces));
1084*d7d2d1d2SJames Wright       PetscCall(ISDestroy(&donor_is));
1085*d7d2d1d2SJames Wright     }
1086*d7d2d1d2SJames Wright     if (periodic_is) {
1087*d7d2d1d2SJames Wright       PetscCall(ISRestoreIndices(periodic_is, &periodic_faces));
1088*d7d2d1d2SJames Wright       PetscCall(ISDestroy(&periodic_is));
1089*d7d2d1d2SJames Wright     }
1090*d7d2d1d2SJames Wright     PetscCall(DMClearLabelStratum(dm, "Face Sets", face_pairings[dim - 2][d][0]));
1091*d7d2d1d2SJames Wright     PetscCall(DMClearLabelStratum(dm, "Face Sets", face_pairings[dim - 2][d][1]));
1092*d7d2d1d2SJames Wright   }
1093*d7d2d1d2SJames Wright   PetscCall(DMPlexSetIsoperiodicFaceSF(dm, periodic_sf_index, periodicsfs));
1094*d7d2d1d2SJames Wright   PetscCall(DMPlexSetIsoperiodicFaceTransform(dm, periodic_sf_index, (const PetscScalar *)transform));
1095*d7d2d1d2SJames Wright   for (PetscInt p = 0; p < periodic_sf_index; p++) PetscCall(PetscSFDestroy(&periodicsfs[p]));
1096*d7d2d1d2SJames Wright 
1097*d7d2d1d2SJames Wright   { // Update coordinate DM with new Face Sets label
1098*d7d2d1d2SJames Wright     DM      cdm;
1099*d7d2d1d2SJames Wright     DMLabel oldFaceSets, newFaceSets;
1100*d7d2d1d2SJames Wright     PetscCall(DMGetCoordinateDM(dm, &cdm));
1101*d7d2d1d2SJames Wright     PetscCall(DMGetLabel(cdm, "Face Sets", &oldFaceSets));
1102*d7d2d1d2SJames Wright     if (oldFaceSets) PetscCall(DMRemoveLabelBySelf(cdm, &oldFaceSets, PETSC_FALSE));
1103*d7d2d1d2SJames Wright     PetscCall(DMLabelDuplicate(label, &newFaceSets));
1104*d7d2d1d2SJames Wright     PetscCall(DMAddLabel(cdm, newFaceSets));
1105*d7d2d1d2SJames Wright     PetscCall(DMLabelDestroy(&newFaceSets));
1106*d7d2d1d2SJames Wright   }
11074054ae39SJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
11084054ae39SJames Wright }
11094054ae39SJames Wright 
1110d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxMesh_Simplex_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate)
1111d71ae5a4SJacob Faibussowitsch {
11129318fe57SMatthew G. Knepley   DM      boundary, vol;
1113c22d3578SMatthew G. Knepley   DMLabel bdlabel;
1114d6218766SMatthew G. Knepley 
1115d6218766SMatthew G. Knepley   PetscFunctionBegin;
11164f572ea9SToby Isaac   PetscAssertPointer(dm, 1);
1117c22d3578SMatthew 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");
11189566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &boundary));
11199566063dSJacob Faibussowitsch   PetscCall(DMSetType(boundary, DMPLEX));
11209566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(boundary, dim, faces, lower, upper, PETSC_FALSE));
11219566063dSJacob Faibussowitsch   PetscCall(DMPlexGenerate(boundary, NULL, interpolate, &vol));
1122c22d3578SMatthew G. Knepley   PetscCall(DMGetLabel(vol, "marker", &bdlabel));
1123c22d3578SMatthew G. Knepley   if (bdlabel) PetscCall(DMPlexLabelComplete(vol, bdlabel));
11245de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_FALSE, vol));
112569d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &vol));
11264054ae39SJames Wright   if (interpolate) {
11274054ae39SJames Wright     PetscCall(DMPlexInterpolateInPlace_Internal(dm));
1128*d7d2d1d2SJames Wright     PetscCall(DMPlexSetBoxLabel_Internal(dm, periodicity));
11294054ae39SJames Wright   }
11309566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&boundary));
11313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1132d6218766SMatthew G. Knepley }
1133d6218766SMatthew G. Knepley 
1134d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCubeMesh_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], DMBoundaryType bdX, DMBoundaryType bdY, DMBoundaryType bdZ)
1135d71ae5a4SJacob Faibussowitsch {
1136ed0e4b50SMatthew G. Knepley   DMLabel     cutLabel  = NULL;
1137f4eb4c5dSMatthew G. Knepley   PetscInt    markerTop = 1, faceMarkerTop = 1;
1138f4eb4c5dSMatthew G. Knepley   PetscInt    markerBottom = 1, faceMarkerBottom = 1;
1139f4eb4c5dSMatthew G. Knepley   PetscInt    markerFront = 1, faceMarkerFront = 1;
1140f4eb4c5dSMatthew G. Knepley   PetscInt    markerBack = 1, faceMarkerBack = 1;
1141f4eb4c5dSMatthew G. Knepley   PetscInt    markerRight = 1, faceMarkerRight = 1;
1142f4eb4c5dSMatthew G. Knepley   PetscInt    markerLeft = 1, faceMarkerLeft = 1;
11433dfda0b1SToby Isaac   PetscInt    dim;
1144d8211ee3SMatthew G. Knepley   PetscBool   markerSeparate = PETSC_FALSE, cutMarker = PETSC_FALSE;
11453dfda0b1SToby Isaac   PetscMPIInt rank;
11463dfda0b1SToby Isaac 
11473dfda0b1SToby Isaac   PetscFunctionBegin;
11489566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
11499566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
11509566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
11519566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
11529566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL));
11539371c9d4SSatish 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) {
11549371c9d4SSatish Balay     if (cutMarker) {
11559371c9d4SSatish Balay       PetscCall(DMCreateLabel(dm, "periodic_cut"));
11569371c9d4SSatish Balay       PetscCall(DMGetLabel(dm, "periodic_cut", &cutLabel));
11579371c9d4SSatish Balay     }
1158d8211ee3SMatthew G. Knepley   }
11593dfda0b1SToby Isaac   switch (dim) {
11603dfda0b1SToby Isaac   case 2:
1161f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 3;
1162f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
1163f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 2;
1164f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 4;
11653dfda0b1SToby Isaac     break;
11663dfda0b1SToby Isaac   case 3:
1167f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
1168f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 2;
1169f4eb4c5dSMatthew G. Knepley     faceMarkerFront  = 3;
1170f4eb4c5dSMatthew G. Knepley     faceMarkerBack   = 4;
1171f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 5;
1172f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 6;
11733dfda0b1SToby Isaac     break;
1174d71ae5a4SJacob Faibussowitsch   default:
1175d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Dimension %" PetscInt_FMT " not supported", dim);
11763dfda0b1SToby Isaac   }
11779566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
1178f4eb4c5dSMatthew G. Knepley   if (markerSeparate) {
1179f4eb4c5dSMatthew G. Knepley     markerBottom = faceMarkerBottom;
1180f4eb4c5dSMatthew G. Knepley     markerTop    = faceMarkerTop;
1181f4eb4c5dSMatthew G. Knepley     markerFront  = faceMarkerFront;
1182f4eb4c5dSMatthew G. Knepley     markerBack   = faceMarkerBack;
1183f4eb4c5dSMatthew G. Knepley     markerRight  = faceMarkerRight;
1184f4eb4c5dSMatthew G. Knepley     markerLeft   = faceMarkerLeft;
11853dfda0b1SToby Isaac   }
11863dfda0b1SToby Isaac   {
1187dd400576SPatrick Sanan     const PetscInt numXEdges    = rank == 0 ? edges[0] : 0;
1188dd400576SPatrick Sanan     const PetscInt numYEdges    = rank == 0 ? edges[1] : 0;
1189dd400576SPatrick Sanan     const PetscInt numZEdges    = rank == 0 ? edges[2] : 0;
1190dd400576SPatrick Sanan     const PetscInt numXVertices = rank == 0 ? (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ? edges[0] : edges[0] + 1) : 0;
1191dd400576SPatrick Sanan     const PetscInt numYVertices = rank == 0 ? (bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ? edges[1] : edges[1] + 1) : 0;
1192dd400576SPatrick Sanan     const PetscInt numZVertices = rank == 0 ? (bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST ? edges[2] : edges[2] + 1) : 0;
11933dfda0b1SToby Isaac     const PetscInt numCells     = numXEdges * numYEdges * numZEdges;
11943dfda0b1SToby Isaac     const PetscInt numXFaces    = numYEdges * numZEdges;
11953dfda0b1SToby Isaac     const PetscInt numYFaces    = numXEdges * numZEdges;
11963dfda0b1SToby Isaac     const PetscInt numZFaces    = numXEdges * numYEdges;
11973dfda0b1SToby Isaac     const PetscInt numTotXFaces = numXVertices * numXFaces;
11983dfda0b1SToby Isaac     const PetscInt numTotYFaces = numYVertices * numYFaces;
11993dfda0b1SToby Isaac     const PetscInt numTotZFaces = numZVertices * numZFaces;
12003dfda0b1SToby Isaac     const PetscInt numFaces     = numTotXFaces + numTotYFaces + numTotZFaces;
12013dfda0b1SToby Isaac     const PetscInt numTotXEdges = numXEdges * numYVertices * numZVertices;
12023dfda0b1SToby Isaac     const PetscInt numTotYEdges = numYEdges * numXVertices * numZVertices;
12033dfda0b1SToby Isaac     const PetscInt numTotZEdges = numZEdges * numXVertices * numYVertices;
12043dfda0b1SToby Isaac     const PetscInt numVertices  = numXVertices * numYVertices * numZVertices;
12053dfda0b1SToby Isaac     const PetscInt numEdges     = numTotXEdges + numTotYEdges + numTotZEdges;
12063dfda0b1SToby Isaac     const PetscInt firstVertex  = (dim == 2) ? numFaces : numCells;
12073dfda0b1SToby Isaac     const PetscInt firstXFace   = (dim == 2) ? 0 : numCells + numVertices;
12083dfda0b1SToby Isaac     const PetscInt firstYFace   = firstXFace + numTotXFaces;
12093dfda0b1SToby Isaac     const PetscInt firstZFace   = firstYFace + numTotYFaces;
12103dfda0b1SToby Isaac     const PetscInt firstXEdge   = numCells + numFaces + numVertices;
12113dfda0b1SToby Isaac     const PetscInt firstYEdge   = firstXEdge + numTotXEdges;
12123dfda0b1SToby Isaac     const PetscInt firstZEdge   = firstYEdge + numTotYEdges;
12133dfda0b1SToby Isaac     Vec            coordinates;
12143dfda0b1SToby Isaac     PetscSection   coordSection;
12153dfda0b1SToby Isaac     PetscScalar   *coords;
12163dfda0b1SToby Isaac     PetscInt       coordSize;
12173dfda0b1SToby Isaac     PetscInt       v, vx, vy, vz;
12183dfda0b1SToby Isaac     PetscInt       c, f, fx, fy, fz, e, ex, ey, ez;
12193dfda0b1SToby Isaac 
12209566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells + numFaces + numEdges + numVertices));
122148a46eb9SPierre Jolivet     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 6));
122248a46eb9SPierre Jolivet     for (f = firstXFace; f < firstXFace + numFaces; ++f) PetscCall(DMPlexSetConeSize(dm, f, 4));
122348a46eb9SPierre Jolivet     for (e = firstXEdge; e < firstXEdge + numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2));
12249566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
12253dfda0b1SToby Isaac     /* Build cells */
12263dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
12273dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
12283dfda0b1SToby Isaac         for (fx = 0; fx < numXEdges; ++fx) {
12293dfda0b1SToby Isaac           PetscInt cell  = (fz * numYEdges + fy) * numXEdges + fx;
12303dfda0b1SToby Isaac           PetscInt faceB = firstZFace + (fy * numXEdges + fx) * numZVertices + fz;
12313dfda0b1SToby Isaac           PetscInt faceT = firstZFace + (fy * numXEdges + fx) * numZVertices + ((fz + 1) % numZVertices);
12323dfda0b1SToby Isaac           PetscInt faceF = firstYFace + (fz * numXEdges + fx) * numYVertices + fy;
12333dfda0b1SToby Isaac           PetscInt faceK = firstYFace + (fz * numXEdges + fx) * numYVertices + ((fy + 1) % numYVertices);
12343dfda0b1SToby Isaac           PetscInt faceL = firstXFace + (fz * numYEdges + fy) * numXVertices + fx;
12353dfda0b1SToby Isaac           PetscInt faceR = firstXFace + (fz * numYEdges + fy) * numXVertices + ((fx + 1) % numXVertices);
12363dfda0b1SToby Isaac           /* B,  T,  F,  K,  R,  L */
1237b5a892a1SMatthew G. Knepley           PetscInt ornt[6] = {-2, 0, 0, -3, 0, -2}; /* ??? */
123842206facSLisandro Dalcin           PetscInt cone[6];
12393dfda0b1SToby Isaac 
12403dfda0b1SToby Isaac           /* no boundary twisting in 3D */
12419371c9d4SSatish Balay           cone[0] = faceB;
12429371c9d4SSatish Balay           cone[1] = faceT;
12439371c9d4SSatish Balay           cone[2] = faceF;
12449371c9d4SSatish Balay           cone[3] = faceK;
12459371c9d4SSatish Balay           cone[4] = faceR;
12469371c9d4SSatish Balay           cone[5] = faceL;
12479566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, cell, cone));
12489566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, cell, ornt));
12499566063dSJacob Faibussowitsch           if (bdX != DM_BOUNDARY_NONE && fx == numXEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
12509566063dSJacob Faibussowitsch           if (bdY != DM_BOUNDARY_NONE && fy == numYEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
12519566063dSJacob Faibussowitsch           if (bdZ != DM_BOUNDARY_NONE && fz == numZEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
12523dfda0b1SToby Isaac         }
12533dfda0b1SToby Isaac       }
12543dfda0b1SToby Isaac     }
12553dfda0b1SToby Isaac     /* Build x faces */
12563dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
12573dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
12583dfda0b1SToby Isaac         for (fx = 0; fx < numXVertices; ++fx) {
12593dfda0b1SToby Isaac           PetscInt face    = firstXFace + (fz * numYEdges + fy) * numXVertices + fx;
12603dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy * numXVertices + fx) * numZEdges + fz;
12613dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (((fy + 1) % numYVertices) * numXVertices + fx) * numZEdges + fz;
12623dfda0b1SToby Isaac           PetscInt edgeB   = firstYEdge + (fz * numXVertices + fx) * numYEdges + fy;
12633dfda0b1SToby Isaac           PetscInt edgeT   = firstYEdge + (((fz + 1) % numZVertices) * numXVertices + fx) * numYEdges + fy;
1264b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
12653dfda0b1SToby Isaac           PetscInt cone[4];
12663dfda0b1SToby Isaac 
12673dfda0b1SToby Isaac           if (dim == 3) {
12683dfda0b1SToby Isaac             /* markers */
12693dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
12703dfda0b1SToby Isaac               if (fx == numXVertices - 1) {
12719566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerRight));
12729566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerRight));
12739371c9d4SSatish Balay               } else if (fx == 0) {
12749566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerLeft));
12759566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerLeft));
12763dfda0b1SToby Isaac               }
12773dfda0b1SToby Isaac             }
12783dfda0b1SToby Isaac           }
12799371c9d4SSatish Balay           cone[0] = edgeB;
12809371c9d4SSatish Balay           cone[1] = edgeR;
12819371c9d4SSatish Balay           cone[2] = edgeT;
12829371c9d4SSatish Balay           cone[3] = edgeL;
12839566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
12849566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
12853dfda0b1SToby Isaac         }
12863dfda0b1SToby Isaac       }
12873dfda0b1SToby Isaac     }
12883dfda0b1SToby Isaac     /* Build y faces */
12893dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
129042206facSLisandro Dalcin       for (fx = 0; fx < numXEdges; ++fx) {
12913dfda0b1SToby Isaac         for (fy = 0; fy < numYVertices; ++fy) {
12923dfda0b1SToby Isaac           PetscInt face    = firstYFace + (fz * numXEdges + fx) * numYVertices + fy;
12933dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy * numXVertices + fx) * numZEdges + fz;
12943dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (fy * numXVertices + ((fx + 1) % numXVertices)) * numZEdges + fz;
12953dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz * numYVertices + fy) * numXEdges + fx;
12963dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (((fz + 1) % numZVertices) * numYVertices + fy) * numXEdges + fx;
1297b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
12983dfda0b1SToby Isaac           PetscInt cone[4];
12993dfda0b1SToby Isaac 
13003dfda0b1SToby Isaac           if (dim == 3) {
13013dfda0b1SToby Isaac             /* markers */
13023dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
13033dfda0b1SToby Isaac               if (fy == numYVertices - 1) {
13049566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerBack));
13059566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerBack));
13069371c9d4SSatish Balay               } else if (fy == 0) {
13079566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerFront));
13089566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerFront));
13093dfda0b1SToby Isaac               }
13103dfda0b1SToby Isaac             }
13113dfda0b1SToby Isaac           }
13129371c9d4SSatish Balay           cone[0] = edgeB;
13139371c9d4SSatish Balay           cone[1] = edgeR;
13149371c9d4SSatish Balay           cone[2] = edgeT;
13159371c9d4SSatish Balay           cone[3] = edgeL;
13169566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
13179566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
13183dfda0b1SToby Isaac         }
13193dfda0b1SToby Isaac       }
13203dfda0b1SToby Isaac     }
13213dfda0b1SToby Isaac     /* Build z faces */
13223dfda0b1SToby Isaac     for (fy = 0; fy < numYEdges; ++fy) {
13233dfda0b1SToby Isaac       for (fx = 0; fx < numXEdges; ++fx) {
13243dfda0b1SToby Isaac         for (fz = 0; fz < numZVertices; fz++) {
13253dfda0b1SToby Isaac           PetscInt face    = firstZFace + (fy * numXEdges + fx) * numZVertices + fz;
13263dfda0b1SToby Isaac           PetscInt edgeL   = firstYEdge + (fz * numXVertices + fx) * numYEdges + fy;
13273dfda0b1SToby Isaac           PetscInt edgeR   = firstYEdge + (fz * numXVertices + ((fx + 1) % numXVertices)) * numYEdges + fy;
13283dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz * numYVertices + fy) * numXEdges + fx;
13293dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (fz * numYVertices + ((fy + 1) % numYVertices)) * numXEdges + fx;
1330b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
13313dfda0b1SToby Isaac           PetscInt cone[4];
13323dfda0b1SToby Isaac 
13333dfda0b1SToby Isaac           if (dim == 2) {
13349371c9d4SSatish Balay             if (bdX == DM_BOUNDARY_TWIST && fx == numXEdges - 1) {
13359371c9d4SSatish Balay               edgeR += numYEdges - 1 - 2 * fy;
13369371c9d4SSatish Balay               ornt[1] = -1;
13379371c9d4SSatish Balay             }
13389371c9d4SSatish Balay             if (bdY == DM_BOUNDARY_TWIST && fy == numYEdges - 1) {
13399371c9d4SSatish Balay               edgeT += numXEdges - 1 - 2 * fx;
13409371c9d4SSatish Balay               ornt[2] = 0;
13419371c9d4SSatish Balay             }
13429566063dSJacob Faibussowitsch             if (bdX != DM_BOUNDARY_NONE && fx == numXEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, face, 2));
13439566063dSJacob Faibussowitsch             if (bdY != DM_BOUNDARY_NONE && fy == numYEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, face, 2));
1344d1c88043SMatthew G. Knepley           } else {
13453dfda0b1SToby Isaac             /* markers */
13463dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
13473dfda0b1SToby Isaac               if (fz == numZVertices - 1) {
13489566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerTop));
13499566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerTop));
13509371c9d4SSatish Balay               } else if (fz == 0) {
13519566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerBottom));
13529566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerBottom));
13533dfda0b1SToby Isaac               }
13543dfda0b1SToby Isaac             }
13553dfda0b1SToby Isaac           }
13569371c9d4SSatish Balay           cone[0] = edgeB;
13579371c9d4SSatish Balay           cone[1] = edgeR;
13589371c9d4SSatish Balay           cone[2] = edgeT;
13599371c9d4SSatish Balay           cone[3] = edgeL;
13609566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
13619566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
13623dfda0b1SToby Isaac         }
13633dfda0b1SToby Isaac       }
13643dfda0b1SToby Isaac     }
13653dfda0b1SToby Isaac     /* Build Z edges*/
13663dfda0b1SToby Isaac     for (vy = 0; vy < numYVertices; vy++) {
13673dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
13683dfda0b1SToby Isaac         for (ez = 0; ez < numZEdges; ez++) {
13693dfda0b1SToby Isaac           const PetscInt edge    = firstZEdge + (vy * numXVertices + vx) * numZEdges + ez;
13703dfda0b1SToby Isaac           const PetscInt vertexB = firstVertex + (ez * numYVertices + vy) * numXVertices + vx;
13713dfda0b1SToby Isaac           const PetscInt vertexT = firstVertex + (((ez + 1) % numZVertices) * numYVertices + vy) * numXVertices + vx;
13723dfda0b1SToby Isaac           PetscInt       cone[2];
13733dfda0b1SToby Isaac 
13749371c9d4SSatish Balay           cone[0] = vertexB;
13759371c9d4SSatish Balay           cone[1] = vertexT;
1376c2df9bbfSMatthew G. Knepley           PetscCall(DMPlexSetCone(dm, edge, cone));
13773dfda0b1SToby Isaac           if (dim == 3) {
13783dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
13793dfda0b1SToby Isaac               if (vx == numXVertices - 1) {
13809566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
1381c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1382c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1383c2df9bbfSMatthew G. Knepley               } else if (vx == 0) {
13849566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
1385c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1386c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
13873dfda0b1SToby Isaac               }
13883dfda0b1SToby Isaac             }
13893dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
13903dfda0b1SToby Isaac               if (vy == numYVertices - 1) {
13919566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBack));
1392c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBack));
1393c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBack));
1394c2df9bbfSMatthew G. Knepley               } else if (vy == 0) {
13959566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerFront));
1396c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerFront));
1397c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerFront));
13983dfda0b1SToby Isaac               }
13993dfda0b1SToby Isaac             }
14003dfda0b1SToby Isaac           }
14013dfda0b1SToby Isaac         }
14023dfda0b1SToby Isaac       }
14033dfda0b1SToby Isaac     }
14043dfda0b1SToby Isaac     /* Build Y edges*/
14053dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
14063dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
14073dfda0b1SToby Isaac         for (ey = 0; ey < numYEdges; ey++) {
14083dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdY == DM_BOUNDARY_TWIST && ey == numYEdges - 1) ? (numXVertices - vx - 1) : (vz * numYVertices + ((ey + 1) % numYVertices)) * numXVertices + vx;
14093dfda0b1SToby Isaac           const PetscInt edge    = firstYEdge + (vz * numXVertices + vx) * numYEdges + ey;
14103dfda0b1SToby Isaac           const PetscInt vertexF = firstVertex + (vz * numYVertices + ey) * numXVertices + vx;
14113dfda0b1SToby Isaac           const PetscInt vertexK = firstVertex + nextv;
14123dfda0b1SToby Isaac           PetscInt       cone[2];
14133dfda0b1SToby Isaac 
14149371c9d4SSatish Balay           cone[0] = vertexF;
14159371c9d4SSatish Balay           cone[1] = vertexK;
14169566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, edge, cone));
14173dfda0b1SToby Isaac           if (dim == 2) {
14183dfda0b1SToby Isaac             if ((bdX != DM_BOUNDARY_PERIODIC) && (bdX != DM_BOUNDARY_TWIST)) {
14193dfda0b1SToby Isaac               if (vx == numXVertices - 1) {
14209566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerRight));
14219566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
14229566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1423c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1424d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
14259566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerLeft));
14269566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
14279566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1428c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
14293dfda0b1SToby Isaac               }
1430d8211ee3SMatthew G. Knepley             } else {
14314c67ea77SStefano Zampini               if (vx == 0 && cutLabel) {
14329566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, edge, 1));
14339566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, cone[0], 1));
1434c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMLabelSetValue(cutLabel, cone[1], 1));
14353dfda0b1SToby Isaac               }
1436d8211ee3SMatthew G. Knepley             }
1437d8211ee3SMatthew G. Knepley           } else {
14383dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
14393dfda0b1SToby Isaac               if (vx == numXVertices - 1) {
14409566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
1441c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1442c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1443d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
14449566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
1445c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1446c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
14473dfda0b1SToby Isaac               }
14483dfda0b1SToby Isaac             }
14493dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
14503dfda0b1SToby Isaac               if (vz == numZVertices - 1) {
14519566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
1452c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1453c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1454d8211ee3SMatthew G. Knepley               } else if (vz == 0) {
14559566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
1456c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1457c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
14583dfda0b1SToby Isaac               }
14593dfda0b1SToby Isaac             }
14603dfda0b1SToby Isaac           }
14613dfda0b1SToby Isaac         }
14623dfda0b1SToby Isaac       }
14633dfda0b1SToby Isaac     }
14643dfda0b1SToby Isaac     /* Build X edges*/
14653dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
14663dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; vy++) {
14673dfda0b1SToby Isaac         for (ex = 0; ex < numXEdges; ex++) {
14683dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdX == DM_BOUNDARY_TWIST && ex == numXEdges - 1) ? (numYVertices - vy - 1) * numXVertices : (vz * numYVertices + vy) * numXVertices + (ex + 1) % numXVertices;
14693dfda0b1SToby Isaac           const PetscInt edge    = firstXEdge + (vz * numYVertices + vy) * numXEdges + ex;
14703dfda0b1SToby Isaac           const PetscInt vertexL = firstVertex + (vz * numYVertices + vy) * numXVertices + ex;
14713dfda0b1SToby Isaac           const PetscInt vertexR = firstVertex + nextv;
14723dfda0b1SToby Isaac           PetscInt       cone[2];
14733dfda0b1SToby Isaac 
14749371c9d4SSatish Balay           cone[0] = vertexL;
14759371c9d4SSatish Balay           cone[1] = vertexR;
14769566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, edge, cone));
14773dfda0b1SToby Isaac           if (dim == 2) {
14783dfda0b1SToby Isaac             if ((bdY != DM_BOUNDARY_PERIODIC) && (bdY != DM_BOUNDARY_TWIST)) {
14793dfda0b1SToby Isaac               if (vy == numYVertices - 1) {
14809566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerTop));
14819566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
14829566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1483c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1484d8211ee3SMatthew G. Knepley               } else if (vy == 0) {
14859566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerBottom));
14869566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
14879566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1488c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
14893dfda0b1SToby Isaac               }
1490d8211ee3SMatthew G. Knepley             } else {
14914c67ea77SStefano Zampini               if (vy == 0 && cutLabel) {
14929566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, edge, 1));
14939566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, cone[0], 1));
1494c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMLabelSetValue(cutLabel, cone[1], 1));
14953dfda0b1SToby Isaac               }
1496d8211ee3SMatthew G. Knepley             }
1497d8211ee3SMatthew G. Knepley           } else {
14983dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
14993dfda0b1SToby Isaac               if (vy == numYVertices - 1) {
15009566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBack));
1501c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBack));
1502c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBack));
1503c2df9bbfSMatthew G. Knepley               } else if (vy == 0) {
15049566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerFront));
1505c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerFront));
1506c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerFront));
15073dfda0b1SToby Isaac               }
15083dfda0b1SToby Isaac             }
15093dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
15103dfda0b1SToby Isaac               if (vz == numZVertices - 1) {
15119566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
1512c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1513c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1514c2df9bbfSMatthew G. Knepley               } else if (vz == 0) {
15159566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
1516c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1517c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
15183dfda0b1SToby Isaac               }
15193dfda0b1SToby Isaac             }
15203dfda0b1SToby Isaac           }
15213dfda0b1SToby Isaac         }
15223dfda0b1SToby Isaac       }
15233dfda0b1SToby Isaac     }
15249566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
15259566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
15263dfda0b1SToby Isaac     /* Build coordinates */
15279566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
15289566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
15299566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
15309566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numVertices));
15313dfda0b1SToby Isaac     for (v = firstVertex; v < firstVertex + numVertices; ++v) {
15329566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
15339566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
15343dfda0b1SToby Isaac     }
15359566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
15369566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
15379566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
15389566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
15399566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
15409566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
15419566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates, VECSTANDARD));
15429566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
15433dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; ++vz) {
15443dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; ++vy) {
15453dfda0b1SToby Isaac         for (vx = 0; vx < numXVertices; ++vx) {
15463dfda0b1SToby Isaac           coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 0] = lower[0] + ((upper[0] - lower[0]) / numXEdges) * vx;
15473dfda0b1SToby Isaac           coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 1] = lower[1] + ((upper[1] - lower[1]) / numYEdges) * vy;
1548ad540459SPierre Jolivet           if (dim == 3) coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 2] = lower[2] + ((upper[2] - lower[2]) / numZEdges) * vz;
15493dfda0b1SToby Isaac         }
15503dfda0b1SToby Isaac       }
15513dfda0b1SToby Isaac     }
15529566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
15539566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
15549566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
15553dfda0b1SToby Isaac   }
15563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
15573dfda0b1SToby Isaac }
15583dfda0b1SToby Isaac 
1559d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxMesh_Tensor_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
1560d71ae5a4SJacob Faibussowitsch {
15619318fe57SMatthew G. Knepley   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
15629318fe57SMatthew G. Knepley   PetscInt       fac[3] = {0, 0, 0}, d;
1563552f7358SJed Brown 
1564552f7358SJed Brown   PetscFunctionBegin;
15654f572ea9SToby Isaac   PetscAssertPointer(dm, 1);
15669318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
15679566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
15689371c9d4SSatish Balay   for (d = 0; d < dim; ++d) {
15699371c9d4SSatish Balay     fac[d] = faces[d];
15709371c9d4SSatish Balay     bdt[d] = periodicity[d];
15719371c9d4SSatish Balay   }
15729566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateCubeMesh_Internal(dm, lower, upper, fac, bdt[0], bdt[1], bdt[2]));
15739371c9d4SSatish 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))) {
15746858538eSMatthew G. Knepley     PetscReal L[3]       = {-1., -1., 0.};
15756858538eSMatthew G. Knepley     PetscReal maxCell[3] = {-1., -1., 0.};
1576552f7358SJed Brown 
15779318fe57SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
15786858538eSMatthew G. Knepley       if (periodicity[d] != DM_BOUNDARY_NONE) {
15799318fe57SMatthew G. Knepley         L[d]       = upper[d] - lower[d];
15809318fe57SMatthew G. Knepley         maxCell[d] = 1.1 * (L[d] / PetscMax(1, faces[d]));
1581768d5fceSMatthew G. Knepley       }
15826858538eSMatthew G. Knepley     }
15834fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, maxCell, lower, L));
1584768d5fceSMatthew G. Knepley   }
15859566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
15863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
15879318fe57SMatthew G. Knepley }
15889318fe57SMatthew G. Knepley 
15895dca41c3SJed 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)
1590d71ae5a4SJacob Faibussowitsch {
15919318fe57SMatthew G. Knepley   PetscFunctionBegin;
159246139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
15935dca41c3SJed Brown   if (shape == DM_SHAPE_ZBOX) PetscCall(DMPlexCreateBoxMesh_Tensor_SFC_Internal(dm, dim, faces, lower, upper, periodicity, interpolate));
15946725e60dSJed Brown   else if (dim == 1) PetscCall(DMPlexCreateLineMesh_Internal(dm, faces[0], lower[0], upper[0], periodicity[0]));
15959566063dSJacob Faibussowitsch   else if (simplex) PetscCall(DMPlexCreateBoxMesh_Simplex_Internal(dm, dim, faces, lower, upper, periodicity, interpolate));
15969566063dSJacob Faibussowitsch   else PetscCall(DMPlexCreateBoxMesh_Tensor_Internal(dm, dim, faces, lower, upper, periodicity));
15979318fe57SMatthew G. Knepley   if (!interpolate && dim > 1 && !simplex) {
1598768d5fceSMatthew G. Knepley     DM udm;
1599768d5fceSMatthew G. Knepley 
16009566063dSJacob Faibussowitsch     PetscCall(DMPlexUninterpolate(dm, &udm));
16019566063dSJacob Faibussowitsch     PetscCall(DMPlexCopyCoordinates(dm, udm));
160269d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &udm));
1603768d5fceSMatthew G. Knepley   }
160446139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
16053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1606c8c68bd8SToby Isaac }
1607c8c68bd8SToby Isaac 
16085d83a8b1SBarry Smith /*@
1609768d5fceSMatthew G. Knepley   DMPlexCreateBoxMesh - Creates a mesh on the tensor product of unit intervals (box) using simplices or tensor cells (hexahedra).
1610768d5fceSMatthew G. Knepley 
1611d083f849SBarry Smith   Collective
1612768d5fceSMatthew G. Knepley 
1613768d5fceSMatthew G. Knepley   Input Parameters:
1614a1cb98faSBarry Smith + comm               - The communicator for the `DM` object
1615768d5fceSMatthew G. Knepley . dim                - The spatial dimension
1616a1cb98faSBarry Smith . simplex            - `PETSC_TRUE` for simplices, `PETSC_FALSE` for tensor cells
161720f4b53cSBarry Smith . faces              - Number of faces per dimension, or `NULL` for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
161820f4b53cSBarry Smith . lower              - The lower left corner, or `NULL` for (0, 0, 0)
161920f4b53cSBarry Smith . upper              - The upper right corner, or `NULL` for (1, 1, 1)
162020f4b53cSBarry Smith . periodicity        - The boundary type for the X,Y,Z direction, or `NULL` for `DM_BOUNDARY_NONE`
162142108689Sksagiyam . interpolate        - Flag to create intermediate mesh pieces (edges, faces)
162242108689Sksagiyam . localizationHeight - Flag to localize edges and faces in addition to cells; only significant for periodic meshes
162342108689Sksagiyam - sparseLocalize     - Flag to localize coordinates only for cells near the periodic boundary; only significant for periodic meshes
1624768d5fceSMatthew G. Knepley 
1625768d5fceSMatthew G. Knepley   Output Parameter:
1626a1cb98faSBarry Smith . dm - The `DM` object
1627768d5fceSMatthew G. Knepley 
1628768d5fceSMatthew G. Knepley   Level: beginner
1629768d5fceSMatthew G. Knepley 
1630a1cb98faSBarry Smith   Note:
1631a1cb98faSBarry Smith   To customize this mesh using options, use
1632a1cb98faSBarry Smith .vb
1633a1cb98faSBarry Smith   DMCreate(comm, &dm);
1634a1cb98faSBarry Smith   DMSetType(dm, DMPLEX);
1635a1cb98faSBarry Smith   DMSetFromOptions(dm);
1636a1cb98faSBarry Smith .ve
1637a1cb98faSBarry Smith   and use the options in `DMSetFromOptions()`.
1638a1cb98faSBarry Smith 
1639a4e35b19SJacob Faibussowitsch   Here is the numbering returned for 2 faces in each direction for tensor cells\:
1640a1cb98faSBarry Smith .vb
1641a1cb98faSBarry Smith  10---17---11---18----12
1642a1cb98faSBarry Smith   |         |         |
1643a1cb98faSBarry Smith   |         |         |
1644a1cb98faSBarry Smith  20    2   22    3    24
1645a1cb98faSBarry Smith   |         |         |
1646a1cb98faSBarry Smith   |         |         |
1647a1cb98faSBarry Smith   7---15----8---16----9
1648a1cb98faSBarry Smith   |         |         |
1649a1cb98faSBarry Smith   |         |         |
1650a1cb98faSBarry Smith  19    0   21    1   23
1651a1cb98faSBarry Smith   |         |         |
1652a1cb98faSBarry Smith   |         |         |
1653a1cb98faSBarry Smith   4---13----5---14----6
1654a1cb98faSBarry Smith .ve
1655a1cb98faSBarry Smith   and for simplicial cells
1656a1cb98faSBarry Smith .vb
1657a1cb98faSBarry Smith  14----8---15----9----16
1658a1cb98faSBarry Smith   |\     5  |\      7 |
1659a1cb98faSBarry Smith   | \       | \       |
1660a1cb98faSBarry Smith  13   2    14    3    15
1661a1cb98faSBarry Smith   | 4   \   | 6   \   |
1662a1cb98faSBarry Smith   |       \ |       \ |
1663a1cb98faSBarry Smith  11----6---12----7----13
1664a1cb98faSBarry Smith   |\        |\        |
1665a1cb98faSBarry Smith   | \    1  | \     3 |
1666a1cb98faSBarry Smith  10   0    11    1    12
1667a1cb98faSBarry Smith   | 0   \   | 2   \   |
1668a1cb98faSBarry Smith   |       \ |       \ |
1669a1cb98faSBarry Smith   8----4----9----5----10
1670a1cb98faSBarry Smith .ve
1671a1cb98faSBarry Smith 
16721cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMSetFromOptions()`, `DMPlexCreateFromFile()`, `DMPlexCreateHexCylinderMesh()`, `DMSetType()`, `DMCreate()`
1673768d5fceSMatthew G. Knepley @*/
167442108689Sksagiyam 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)
1675d71ae5a4SJacob Faibussowitsch {
16769318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
1677fdbf62faSLisandro Dalcin   PetscReal      low[3] = {0, 0, 0};
1678fdbf62faSLisandro Dalcin   PetscReal      upp[3] = {1, 1, 1};
1679fdbf62faSLisandro Dalcin   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
1680552f7358SJed Brown 
1681768d5fceSMatthew G. Knepley   PetscFunctionBegin;
16829566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
16839566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
16845dca41c3SJed Brown   PetscCall(DMPlexCreateBoxMesh_Internal(*dm, DM_SHAPE_BOX, dim, simplex, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt, interpolate));
168542108689Sksagiyam   if (periodicity) {
168642108689Sksagiyam     DM cdm;
168742108689Sksagiyam 
168842108689Sksagiyam     PetscCall(DMGetCoordinateDM(*dm, &cdm));
168942108689Sksagiyam     PetscCall(DMPlexSetMaxProjectionHeight(cdm, localizationHeight));
169042108689Sksagiyam     PetscCall(DMSetSparseLocalize(*dm, sparseLocalize));
169142108689Sksagiyam     PetscCall(DMLocalizeCoordinates(*dm));
169242108689Sksagiyam   }
16933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
16949318fe57SMatthew G. Knepley }
1695fdbf62faSLisandro Dalcin 
1696d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateWedgeBoxMesh_Internal(DM dm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
1697d71ae5a4SJacob Faibussowitsch {
16989318fe57SMatthew G. Knepley   DM       bdm, vol;
16999318fe57SMatthew G. Knepley   PetscInt i;
17009318fe57SMatthew G. Knepley 
17019318fe57SMatthew G. Knepley   PetscFunctionBegin;
17021fcf445aSMatthew G. Knepley   // TODO Now we can support periodicity
170308401ef6SPierre Jolivet   for (i = 0; i < 3; ++i) PetscCheck(periodicity[i] == DM_BOUNDARY_NONE, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Periodicity not yet supported");
17049566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &bdm));
17059566063dSJacob Faibussowitsch   PetscCall(DMSetType(bdm, DMPLEX));
17069566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(bdm, 2));
170746139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, bdm, 0, 0, 0));
17089566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxMesh_Simplex_Internal(bdm, 2, faces, lower, upper, periodicity, PETSC_TRUE));
17091fcf445aSMatthew G. Knepley   PetscCall(DMPlexExtrude(bdm, faces[2], upper[2] - lower[2], PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, NULL, NULL, &vol));
171046139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, bdm, 0, 0, 0));
17119566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&bdm));
171269d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &vol));
17139318fe57SMatthew G. Knepley   if (lower[2] != 0.0) {
17149318fe57SMatthew G. Knepley     Vec          v;
17159318fe57SMatthew G. Knepley     PetscScalar *x;
17169318fe57SMatthew G. Knepley     PetscInt     cDim, n;
17179318fe57SMatthew G. Knepley 
17189566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &v));
17199566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(v, &cDim));
17209566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(v, &n));
17219566063dSJacob Faibussowitsch     PetscCall(VecGetArray(v, &x));
17229318fe57SMatthew G. Knepley     x += cDim;
17239318fe57SMatthew G. Knepley     for (i = 0; i < n; i += cDim) x[i] += lower[2];
17249566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(v, &x));
17259566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, v));
17269318fe57SMatthew G. Knepley   }
17273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1728552f7358SJed Brown }
1729552f7358SJed Brown 
173000dabe28SStefano Zampini /*@
173139f4f5dbSPierre Jolivet   DMPlexCreateWedgeBoxMesh - Creates a 3-D mesh tessellating the (x,y) plane and extruding in the third direction using wedge cells.
173200dabe28SStefano Zampini 
1733d083f849SBarry Smith   Collective
173400dabe28SStefano Zampini 
173500dabe28SStefano Zampini   Input Parameters:
1736a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
173720f4b53cSBarry Smith . faces       - Number of faces per dimension, or `NULL` for (1, 1, 1)
173820f4b53cSBarry Smith . lower       - The lower left corner, or `NULL` for (0, 0, 0)
173920f4b53cSBarry Smith . upper       - The upper right corner, or `NULL` for (1, 1, 1)
174020f4b53cSBarry Smith . periodicity - The boundary type for the X,Y,Z direction, or `NULL` for `DM_BOUNDARY_NONE`
1741a1cb98faSBarry Smith . orderHeight - If `PETSC_TRUE`, orders the extruded cells in the height first. Otherwise, orders the cell on the layers first
174200dabe28SStefano Zampini - interpolate - Flag to create intermediate mesh pieces (edges, faces)
174300dabe28SStefano Zampini 
174400dabe28SStefano Zampini   Output Parameter:
1745a1cb98faSBarry Smith . dm - The `DM` object
174600dabe28SStefano Zampini 
174700dabe28SStefano Zampini   Level: beginner
174800dabe28SStefano Zampini 
17491cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateHexCylinderMesh()`, `DMPlexCreateWedgeCylinderMesh()`, `DMExtrude()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
175000dabe28SStefano Zampini @*/
1751d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateWedgeBoxMesh(MPI_Comm comm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool orderHeight, PetscBool interpolate, DM *dm)
1752d71ae5a4SJacob Faibussowitsch {
17539318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
175400dabe28SStefano Zampini   PetscReal      low[3] = {0, 0, 0};
175500dabe28SStefano Zampini   PetscReal      upp[3] = {1, 1, 1};
175600dabe28SStefano Zampini   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
175700dabe28SStefano Zampini 
175800dabe28SStefano Zampini   PetscFunctionBegin;
17599566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
17609566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
17619566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateWedgeBoxMesh_Internal(*dm, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt));
1762d410b0cfSMatthew G. Knepley   if (!interpolate) {
1763d410b0cfSMatthew G. Knepley     DM udm;
176400dabe28SStefano Zampini 
17659566063dSJacob Faibussowitsch     PetscCall(DMPlexUninterpolate(*dm, &udm));
176669d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(*dm, &udm));
176700dabe28SStefano Zampini   }
17687ff04441SMatthew G. Knepley   if (periodicity) PetscCall(DMLocalizeCoordinates(*dm));
17693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
177000dabe28SStefano Zampini }
177100dabe28SStefano Zampini 
1772cfb853baSMatthew G. Knepley /*
1773cfb853baSMatthew 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.
1774cfb853baSMatthew G. Knepley 
1775cfb853baSMatthew G. Knepley   Input Parameters:
1776cfb853baSMatthew G. Knepley + len - The length of the tuple
1777cfb853baSMatthew G. Knepley . max - The maximum for each dimension, so values are in [0, max)
1778cfb853baSMatthew G. Knepley - tup - A tuple of length len+1: tup[len] > 0 indicates a stopping condition
1779cfb853baSMatthew G. Knepley 
1780cfb853baSMatthew G. Knepley   Output Parameter:
178120f4b53cSBarry Smith . tup - A tuple of `len` integers whose entries are at most `max`
1782cfb853baSMatthew G. Knepley 
1783cfb853baSMatthew G. Knepley   Level: developer
1784cfb853baSMatthew G. Knepley 
178520f4b53cSBarry Smith   Note:
178620f4b53cSBarry Smith   Ordering is lexicographic with lowest index as least significant in ordering.
178720f4b53cSBarry 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}.
178820f4b53cSBarry Smith 
1789cfb853baSMatthew G. Knepley .seealso: PetscDualSpaceTensorPointLexicographic_Internal(), PetscDualSpaceLatticePointLexicographic_Internal()
1790cfb853baSMatthew G. Knepley */
1791cfb853baSMatthew G. Knepley static PetscErrorCode DMPlexTensorPointLexicographic_Private(PetscInt len, const PetscInt max[], PetscInt tup[])
1792cfb853baSMatthew G. Knepley {
1793cfb853baSMatthew G. Knepley   PetscInt i;
1794cfb853baSMatthew G. Knepley 
1795cfb853baSMatthew G. Knepley   PetscFunctionBegin;
1796cfb853baSMatthew G. Knepley   for (i = 0; i < len; ++i) {
1797cfb853baSMatthew G. Knepley     if (tup[i] < max[i] - 1) {
1798cfb853baSMatthew G. Knepley       break;
1799cfb853baSMatthew G. Knepley     } else {
1800cfb853baSMatthew G. Knepley       tup[i] = 0;
1801cfb853baSMatthew G. Knepley     }
1802cfb853baSMatthew G. Knepley   }
1803cfb853baSMatthew G. Knepley   if (i == len) tup[i - 1] = max[i - 1];
1804cfb853baSMatthew G. Knepley   else ++tup[i];
18053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1806cfb853baSMatthew G. Knepley }
1807cfb853baSMatthew G. Knepley 
1808cfb853baSMatthew G. Knepley static PetscInt TupleToIndex_Private(PetscInt len, const PetscInt max[], const PetscInt tup[])
1809cfb853baSMatthew G. Knepley {
1810cfb853baSMatthew G. Knepley   PetscInt i, idx = tup[len - 1];
1811cfb853baSMatthew G. Knepley 
1812cfb853baSMatthew G. Knepley   for (i = len - 2; i >= 0; --i) {
1813cfb853baSMatthew G. Knepley     idx *= max[i];
1814cfb853baSMatthew G. Knepley     idx += tup[i];
1815cfb853baSMatthew G. Knepley   }
1816cfb853baSMatthew G. Knepley   return idx;
1817cfb853baSMatthew G. Knepley }
1818cfb853baSMatthew G. Knepley 
1819cfb853baSMatthew G. Knepley static PetscErrorCode DMPlexCreateHypercubicMesh_Internal(DM dm, PetscInt dim, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], const DMBoundaryType bd[])
1820cfb853baSMatthew G. Knepley {
1821cfb853baSMatthew G. Knepley   Vec          coordinates;
1822cfb853baSMatthew G. Knepley   PetscSection coordSection;
1823cfb853baSMatthew G. Knepley   DMLabel      cutLabel    = NULL;
1824cfb853baSMatthew G. Knepley   PetscBool    cutMarker   = PETSC_FALSE;
1825cfb853baSMatthew G. Knepley   PetscBool    periodic    = PETSC_FALSE;
1826cfb853baSMatthew G. Knepley   PetscInt     numCells    = 1, c;
1827cfb853baSMatthew G. Knepley   PetscInt     numVertices = 1, v;
1828cfb853baSMatthew G. Knepley   PetscScalar *coords;
1829cfb853baSMatthew G. Knepley   PetscInt    *vertices, *vert, *vtmp, *supp, cone[2];
1830cfb853baSMatthew G. Knepley   PetscInt     d, e, cell = 0, coordSize;
1831cfb853baSMatthew G. Knepley   PetscMPIInt  rank;
1832cfb853baSMatthew G. Knepley 
1833cfb853baSMatthew G. Knepley   PetscFunctionBegin;
1834cfb853baSMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
1835cfb853baSMatthew G. Knepley   PetscCall(DMSetDimension(dm, dim));
1836cfb853baSMatthew G. Knepley   PetscCall(PetscCalloc4(dim, &vertices, dim, &vert, dim, &vtmp, 2 * dim, &supp));
1837cfb853baSMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "marker"));
1838cfb853baSMatthew G. Knepley   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL));
1839cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) periodic = (periodic || bd[d] == DM_BOUNDARY_PERIODIC) ? PETSC_TRUE : PETSC_FALSE;
1840cfb853baSMatthew G. Knepley   if (periodic && cutMarker) {
1841cfb853baSMatthew G. Knepley     PetscCall(DMCreateLabel(dm, "periodic_cut"));
1842cfb853baSMatthew G. Knepley     PetscCall(DMGetLabel(dm, "periodic_cut", &cutLabel));
1843cfb853baSMatthew G. Knepley   }
1844cfb853baSMatthew 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");
1845cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) {
1846cfb853baSMatthew G. Knepley     vertices[d] = edges[d];
1847cfb853baSMatthew G. Knepley     numVertices *= vertices[d];
1848cfb853baSMatthew G. Knepley   }
1849cfb853baSMatthew G. Knepley   numCells = numVertices * dim;
1850cfb853baSMatthew G. Knepley   PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
1851cfb853baSMatthew G. Knepley   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, 2));
1852cfb853baSMatthew G. Knepley   for (v = numCells; v < numCells + numVertices; ++v) PetscCall(DMPlexSetSupportSize(dm, v, 2 * dim));
1853cfb853baSMatthew G. Knepley   /* TODO Loop over boundary and reset support sizes */
1854cfb853baSMatthew G. Knepley   PetscCall(DMSetUp(dm)); /* Allocate space for cones and supports */
1855cfb853baSMatthew G. Knepley   /* Build cell cones and vertex supports */
1856cfb853baSMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "celltype"));
1857cfb853baSMatthew G. Knepley   while (vert[dim - 1] < vertices[dim - 1]) {
1858cfb853baSMatthew G. Knepley     const PetscInt vertex = TupleToIndex_Private(dim, vertices, vert) + numCells;
1859cfb853baSMatthew G. Knepley     PetscInt       s      = 0;
1860cfb853baSMatthew G. Knepley 
18613ba16761SJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "Vertex %" PetscInt_FMT ":", vertex));
18623ba16761SJacob Faibussowitsch     for (d = 0; d < dim; ++d) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, vert[d]));
18633ba16761SJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
1864cfb853baSMatthew G. Knepley     PetscCall(DMPlexSetCellType(dm, vertex, DM_POLYTOPE_POINT));
1865cfb853baSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
1866cfb853baSMatthew G. Knepley       for (e = 0; e < dim; ++e) vtmp[e] = vert[e];
1867cfb853baSMatthew G. Knepley       vtmp[d] = (vert[d] + 1) % vertices[d];
1868cfb853baSMatthew G. Knepley       cone[0] = vertex;
1869cfb853baSMatthew G. Knepley       cone[1] = TupleToIndex_Private(dim, vertices, vtmp) + numCells;
18703ba16761SJacob Faibussowitsch       PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Vertex %" PetscInt_FMT ":", cone[1]));
18713ba16761SJacob Faibussowitsch       for (e = 0; e < dim; ++e) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, vtmp[e]));
18723ba16761SJacob Faibussowitsch       PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
1873cfb853baSMatthew G. Knepley       PetscCall(DMPlexSetCone(dm, cell, cone));
1874cfb853baSMatthew G. Knepley       PetscCall(DMPlexSetCellType(dm, cell, DM_POLYTOPE_SEGMENT));
18753ba16761SJacob Faibussowitsch       PetscCall(PetscPrintf(PETSC_COMM_SELF, "  Edge %" PetscInt_FMT " (%" PetscInt_FMT " %" PetscInt_FMT ")\n", cell, cone[0], cone[1]));
1876cfb853baSMatthew G. Knepley       ++cell;
1877cfb853baSMatthew G. Knepley     }
1878cfb853baSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
1879cfb853baSMatthew G. Knepley       for (e = 0; e < dim; ++e) vtmp[e] = vert[e];
1880cfb853baSMatthew G. Knepley       vtmp[d]   = (vert[d] + vertices[d] - 1) % vertices[d];
1881cfb853baSMatthew G. Knepley       supp[s++] = TupleToIndex_Private(dim, vertices, vtmp) * dim + d;
1882cfb853baSMatthew G. Knepley       supp[s++] = (vertex - numCells) * dim + d;
1883cfb853baSMatthew G. Knepley       PetscCall(DMPlexSetSupport(dm, vertex, supp));
1884cfb853baSMatthew G. Knepley     }
1885cfb853baSMatthew G. Knepley     PetscCall(DMPlexTensorPointLexicographic_Private(dim, vertices, vert));
1886cfb853baSMatthew G. Knepley   }
1887cfb853baSMatthew G. Knepley   PetscCall(DMPlexStratify(dm));
1888cfb853baSMatthew G. Knepley   /* Build coordinates */
1889cfb853baSMatthew G. Knepley   PetscCall(DMGetCoordinateSection(dm, &coordSection));
1890cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetNumFields(coordSection, 1));
1891cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
1892cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices));
1893cfb853baSMatthew G. Knepley   for (v = numCells; v < numCells + numVertices; ++v) {
1894cfb853baSMatthew G. Knepley     PetscCall(PetscSectionSetDof(coordSection, v, dim));
1895cfb853baSMatthew G. Knepley     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
1896cfb853baSMatthew G. Knepley   }
1897cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetUp(coordSection));
1898cfb853baSMatthew G. Knepley   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
1899cfb853baSMatthew G. Knepley   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
1900cfb853baSMatthew G. Knepley   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
1901cfb853baSMatthew G. Knepley   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
1902cfb853baSMatthew G. Knepley   PetscCall(VecSetBlockSize(coordinates, dim));
1903cfb853baSMatthew G. Knepley   PetscCall(VecSetType(coordinates, VECSTANDARD));
1904cfb853baSMatthew G. Knepley   PetscCall(VecGetArray(coordinates, &coords));
1905cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) vert[d] = 0;
1906cfb853baSMatthew G. Knepley   while (vert[dim - 1] < vertices[dim - 1]) {
1907cfb853baSMatthew G. Knepley     const PetscInt vertex = TupleToIndex_Private(dim, vertices, vert);
1908cfb853baSMatthew G. Knepley 
1909cfb853baSMatthew G. Knepley     for (d = 0; d < dim; ++d) coords[vertex * dim + d] = lower[d] + ((upper[d] - lower[d]) / vertices[d]) * vert[d];
19103ba16761SJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "Vertex %" PetscInt_FMT ":", vertex));
19113ba16761SJacob Faibussowitsch     for (d = 0; d < dim; ++d) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, vert[d]));
19123ba16761SJacob Faibussowitsch     for (d = 0; d < dim; ++d) PetscCall(PetscPrintf(PETSC_COMM_SELF, " %g", (double)PetscRealPart(coords[vertex * dim + d])));
19133ba16761SJacob Faibussowitsch     PetscCall(PetscPrintf(PETSC_COMM_SELF, "\n"));
1914cfb853baSMatthew G. Knepley     PetscCall(DMPlexTensorPointLexicographic_Private(dim, vertices, vert));
1915cfb853baSMatthew G. Knepley   }
1916cfb853baSMatthew G. Knepley   PetscCall(VecRestoreArray(coordinates, &coords));
1917cfb853baSMatthew G. Knepley   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
1918cfb853baSMatthew G. Knepley   PetscCall(VecDestroy(&coordinates));
1919cfb853baSMatthew G. Knepley   PetscCall(PetscFree4(vertices, vert, vtmp, supp));
1920cfb853baSMatthew G. Knepley   //PetscCall(DMSetPeriodicity(dm, NULL, lower, upper));
1921cfb853baSMatthew G. Knepley   // Attach the extent
1922cfb853baSMatthew G. Knepley   {
1923cfb853baSMatthew G. Knepley     PetscContainer c;
1924cfb853baSMatthew G. Knepley     PetscInt      *extent;
1925cfb853baSMatthew G. Knepley 
1926cfb853baSMatthew G. Knepley     PetscCall(PetscMalloc1(dim, &extent));
1927cfb853baSMatthew G. Knepley     for (PetscInt d = 0; d < dim; ++d) extent[d] = edges[d];
1928cfb853baSMatthew G. Knepley     PetscCall(PetscContainerCreate(PETSC_COMM_SELF, &c));
192949abdd8aSBarry Smith     PetscCall(PetscContainerSetCtxDestroy(c, PetscCtxDestroyDefault));
1930cfb853baSMatthew G. Knepley     PetscCall(PetscContainerSetPointer(c, extent));
1931cfb853baSMatthew G. Knepley     PetscCall(PetscObjectCompose((PetscObject)dm, "_extent", (PetscObject)c));
1932cfb853baSMatthew G. Knepley     PetscCall(PetscContainerDestroy(&c));
1933cfb853baSMatthew G. Knepley   }
19343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1935cfb853baSMatthew G. Knepley }
1936cfb853baSMatthew G. Knepley 
1937cfb853baSMatthew G. Knepley /*@C
1938aaa8cc7dSPierre Jolivet   DMPlexCreateHypercubicMesh - Creates a periodic mesh on the tensor product of unit intervals using only vertices and edges.
1939cfb853baSMatthew G. Knepley 
1940cfb853baSMatthew G. Knepley   Collective
1941cfb853baSMatthew G. Knepley 
1942cfb853baSMatthew G. Knepley   Input Parameters:
1943cfb853baSMatthew G. Knepley + comm  - The communicator for the DM object
1944cfb853baSMatthew G. Knepley . dim   - The spatial dimension
194520f4b53cSBarry Smith . edges - Number of edges per dimension, or `NULL` for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
194620f4b53cSBarry Smith . lower - The lower left corner, or `NULL` for (0, 0, 0)
194720f4b53cSBarry Smith - upper - The upper right corner, or `NULL` for (1, 1, 1)
1948cfb853baSMatthew G. Knepley 
1949cfb853baSMatthew G. Knepley   Output Parameter:
1950cfb853baSMatthew G. Knepley . dm - The DM object
1951cfb853baSMatthew G. Knepley 
195220f4b53cSBarry Smith   Level: beginner
195320f4b53cSBarry Smith 
195420f4b53cSBarry Smith   Note:
195520f4b53cSBarry Smith   If you want to customize this mesh using options, you just need to
195620f4b53cSBarry Smith .vb
195720f4b53cSBarry Smith   DMCreate(comm, &dm);
195820f4b53cSBarry Smith   DMSetType(dm, DMPLEX);
195920f4b53cSBarry Smith   DMSetFromOptions(dm);
196020f4b53cSBarry Smith .ve
196120f4b53cSBarry Smith   and use the options on the `DMSetFromOptions()` page.
1962cfb853baSMatthew G. Knepley 
1963cfb853baSMatthew G. Knepley   The vertices are numbered is lexicographic order, and the dim edges exiting a vertex in the positive orthant are number consecutively,
196420f4b53cSBarry Smith .vb
196520f4b53cSBarry Smith  18--0-19--2-20--4-18
196620f4b53cSBarry Smith   |     |     |     |
196720f4b53cSBarry Smith  13    15    17    13
196820f4b53cSBarry Smith   |     |     |     |
196920f4b53cSBarry Smith  24-12-25-14-26-16-24
197020f4b53cSBarry Smith   |     |     |     |
197120f4b53cSBarry Smith   7     9    11     7
197220f4b53cSBarry Smith   |     |     |     |
197320f4b53cSBarry Smith  21--6-22--8-23-10-21
197420f4b53cSBarry Smith   |     |     |     |
197520f4b53cSBarry Smith   1     3     5     1
197620f4b53cSBarry Smith   |     |     |     |
197720f4b53cSBarry Smith  18--0-19--2-20--4-18
197820f4b53cSBarry Smith .ve
1979cfb853baSMatthew G. Knepley 
198076fbde31SPierre Jolivet .seealso: `DMSetFromOptions()`, `DMPlexCreateFromFile()`, `DMPlexCreateHexCylinderMesh()`, `DMSetType()`, `DMCreate()`
1981cfb853baSMatthew G. Knepley @*/
1982cfb853baSMatthew G. Knepley PetscErrorCode DMPlexCreateHypercubicMesh(MPI_Comm comm, PetscInt dim, const PetscInt edges[], const PetscReal lower[], const PetscReal upper[], DM *dm)
1983cfb853baSMatthew G. Knepley {
1984cfb853baSMatthew G. Knepley   PetscInt       *edg;
1985cfb853baSMatthew G. Knepley   PetscReal      *low, *upp;
1986cfb853baSMatthew G. Knepley   DMBoundaryType *bdt;
1987cfb853baSMatthew G. Knepley   PetscInt        d;
1988cfb853baSMatthew G. Knepley 
1989cfb853baSMatthew G. Knepley   PetscFunctionBegin;
1990cfb853baSMatthew G. Knepley   PetscCall(DMCreate(comm, dm));
1991cfb853baSMatthew G. Knepley   PetscCall(DMSetType(*dm, DMPLEX));
1992cfb853baSMatthew G. Knepley   PetscCall(PetscMalloc4(dim, &edg, dim, &low, dim, &upp, dim, &bdt));
1993cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) {
1994cfb853baSMatthew G. Knepley     edg[d] = edges ? edges[d] : 1;
1995cfb853baSMatthew G. Knepley     low[d] = lower ? lower[d] : 0.;
1996cfb853baSMatthew G. Knepley     upp[d] = upper ? upper[d] : 1.;
1997cfb853baSMatthew G. Knepley     bdt[d] = DM_BOUNDARY_PERIODIC;
1998cfb853baSMatthew G. Knepley   }
1999cfb853baSMatthew G. Knepley   PetscCall(DMPlexCreateHypercubicMesh_Internal(*dm, dim, low, upp, edg, bdt));
2000cfb853baSMatthew G. Knepley   PetscCall(PetscFree4(edg, low, upp, bdt));
20013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2002cfb853baSMatthew G. Knepley }
2003cfb853baSMatthew G. Knepley 
2004cc4c1da9SBarry Smith /*@
2005a1cb98faSBarry Smith   DMPlexSetOptionsPrefix - Sets the prefix used for searching for all `DM` options in the database.
2006a9074c1eSMatthew G. Knepley 
200720f4b53cSBarry Smith   Logically Collective
2008a9074c1eSMatthew G. Knepley 
2009a9074c1eSMatthew G. Knepley   Input Parameters:
201020f4b53cSBarry Smith + dm     - the `DM` context
2011a9074c1eSMatthew G. Knepley - prefix - the prefix to prepend to all option names
2012a9074c1eSMatthew G. Knepley 
2013a1cb98faSBarry Smith   Level: advanced
2014a1cb98faSBarry Smith 
2015a1cb98faSBarry Smith   Note:
2016a9074c1eSMatthew G. Knepley   A hyphen (-) must NOT be given at the beginning of the prefix name.
2017a9074c1eSMatthew G. Knepley   The first character of all runtime options is AUTOMATICALLY the hyphen.
2018a9074c1eSMatthew G. Knepley 
20191cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `SNESSetFromOptions()`
2020a9074c1eSMatthew G. Knepley @*/
2021d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetOptionsPrefix(DM dm, const char prefix[])
2022d71ae5a4SJacob Faibussowitsch {
2023a9074c1eSMatthew G. Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
2024a9074c1eSMatthew G. Knepley 
2025a9074c1eSMatthew G. Knepley   PetscFunctionBegin;
2026a9074c1eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20279566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, prefix));
20289566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)mesh->partitioner, prefix));
20293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2030a9074c1eSMatthew G. Knepley }
2031a9074c1eSMatthew G. Knepley 
20329318fe57SMatthew G. Knepley /* Remap geometry to cylinder
203361a622f3SMatthew G. Knepley    TODO: This only works for a single refinement, then it is broken
203461a622f3SMatthew G. Knepley 
20359318fe57SMatthew G. Knepley      Interior square: Linear interpolation is correct
20369318fe57SMatthew G. Knepley      The other cells all have vertices on rays from the origin. We want to uniformly expand the spacing
20379318fe57SMatthew G. Knepley      such that the last vertex is on the unit circle. So the closest and farthest vertices are at distance
20380510c589SMatthew G. Knepley 
20399318fe57SMatthew G. Knepley        phi     = arctan(y/x)
20409318fe57SMatthew G. Knepley        d_close = sqrt(1/8 + 1/4 sin^2(phi))
20419318fe57SMatthew G. Knepley        d_far   = sqrt(1/2 + sin^2(phi))
20420510c589SMatthew G. Knepley 
20439318fe57SMatthew G. Knepley      so we remap them using
20440510c589SMatthew G. Knepley 
20459318fe57SMatthew G. Knepley        x_new = x_close + (x - x_close) (1 - d_close) / (d_far - d_close)
20469318fe57SMatthew G. Knepley        y_new = y_close + (y - y_close) (1 - d_close) / (d_far - d_close)
20470510c589SMatthew G. Knepley 
20489318fe57SMatthew G. Knepley      If pi/4 < phi < 3pi/4 or -3pi/4 < phi < -pi/4, then we switch x and y.
20499318fe57SMatthew G. Knepley */
2050d71ae5a4SJacob 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[])
2051d71ae5a4SJacob Faibussowitsch {
20529318fe57SMatthew G. Knepley   const PetscReal dis = 1.0 / PetscSqrtReal(2.0);
20539318fe57SMatthew G. Knepley   const PetscReal ds2 = 0.5 * dis;
205422cc497dSMatthew G. Knepley 
20559318fe57SMatthew G. Knepley   if ((PetscAbsScalar(u[0]) <= ds2) && (PetscAbsScalar(u[1]) <= ds2)) {
20569318fe57SMatthew G. Knepley     f0[0] = u[0];
20579318fe57SMatthew G. Knepley     f0[1] = u[1];
20589318fe57SMatthew G. Knepley   } else {
20599318fe57SMatthew G. Knepley     PetscReal phi, sinp, cosp, dc, df, x, y, xc, yc;
20600510c589SMatthew G. Knepley 
20619318fe57SMatthew G. Knepley     x    = PetscRealPart(u[0]);
20629318fe57SMatthew G. Knepley     y    = PetscRealPart(u[1]);
20639318fe57SMatthew G. Knepley     phi  = PetscAtan2Real(y, x);
20649318fe57SMatthew G. Knepley     sinp = PetscSinReal(phi);
20659318fe57SMatthew G. Knepley     cosp = PetscCosReal(phi);
20669318fe57SMatthew G. Knepley     if ((PetscAbsReal(phi) > PETSC_PI / 4.0) && (PetscAbsReal(phi) < 3.0 * PETSC_PI / 4.0)) {
20679318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2 / sinp);
20689318fe57SMatthew G. Knepley       df = PetscAbsReal(dis / sinp);
20699318fe57SMatthew G. Knepley       xc = ds2 * x / PetscAbsReal(y);
20709318fe57SMatthew G. Knepley       yc = ds2 * PetscSignReal(y);
20719318fe57SMatthew G. Knepley     } else {
20729318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2 / cosp);
20739318fe57SMatthew G. Knepley       df = PetscAbsReal(dis / cosp);
20749318fe57SMatthew G. Knepley       xc = ds2 * PetscSignReal(x);
20759318fe57SMatthew G. Knepley       yc = ds2 * y / PetscAbsReal(x);
20769318fe57SMatthew G. Knepley     }
20779318fe57SMatthew G. Knepley     f0[0] = xc + (u[0] - xc) * (1.0 - dc) / (df - dc);
20789318fe57SMatthew G. Knepley     f0[1] = yc + (u[1] - yc) * (1.0 - dc) / (df - dc);
20799318fe57SMatthew G. Knepley   }
20809318fe57SMatthew G. Knepley   f0[2] = u[2];
20819318fe57SMatthew G. Knepley }
20820510c589SMatthew G. Knepley 
208349704ca5SMatthew G. Knepley static PetscErrorCode DMPlexCreateHexCylinderMesh_Internal(DM dm, DMBoundaryType periodicZ, PetscInt Nr)
2084d71ae5a4SJacob Faibussowitsch {
20850510c589SMatthew G. Knepley   const PetscInt dim = 3;
20869318fe57SMatthew G. Knepley   PetscInt       numCells, numVertices;
2087d8c47e87SMatthew G. Knepley   PetscMPIInt    rank;
20880510c589SMatthew G. Knepley 
20890510c589SMatthew G. Knepley   PetscFunctionBegin;
209046139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
20919566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
20929566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
20930510c589SMatthew G. Knepley   /* Create topology */
20940510c589SMatthew G. Knepley   {
20950510c589SMatthew G. Knepley     PetscInt cone[8], c;
20960510c589SMatthew G. Knepley 
2097dd400576SPatrick Sanan     numCells    = rank == 0 ? 5 : 0;
2098dd400576SPatrick Sanan     numVertices = rank == 0 ? 16 : 0;
2099006a8963SMatthew G. Knepley     if (periodicZ == DM_BOUNDARY_PERIODIC) {
2100ae8bcbbbSMatthew G. Knepley       numCells *= 3;
2101dd400576SPatrick Sanan       numVertices = rank == 0 ? 24 : 0;
2102006a8963SMatthew G. Knepley     }
21039566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
21049566063dSJacob Faibussowitsch     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 8));
21059566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm));
2106dd400576SPatrick Sanan     if (rank == 0) {
2107006a8963SMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
21089371c9d4SSatish Balay         cone[0] = 15;
21099371c9d4SSatish Balay         cone[1] = 18;
21109371c9d4SSatish Balay         cone[2] = 17;
21119371c9d4SSatish Balay         cone[3] = 16;
21129371c9d4SSatish Balay         cone[4] = 31;
21139371c9d4SSatish Balay         cone[5] = 32;
21149371c9d4SSatish Balay         cone[6] = 33;
21159371c9d4SSatish Balay         cone[7] = 34;
21169566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
21179371c9d4SSatish Balay         cone[0] = 16;
21189371c9d4SSatish Balay         cone[1] = 17;
21199371c9d4SSatish Balay         cone[2] = 24;
21209371c9d4SSatish Balay         cone[3] = 23;
21219371c9d4SSatish Balay         cone[4] = 32;
21229371c9d4SSatish Balay         cone[5] = 36;
21239371c9d4SSatish Balay         cone[6] = 37;
21249371c9d4SSatish Balay         cone[7] = 33; /* 22 25 26 21 */
21259566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
21269371c9d4SSatish Balay         cone[0] = 18;
21279371c9d4SSatish Balay         cone[1] = 27;
21289371c9d4SSatish Balay         cone[2] = 24;
21299371c9d4SSatish Balay         cone[3] = 17;
21309371c9d4SSatish Balay         cone[4] = 34;
21319371c9d4SSatish Balay         cone[5] = 33;
21329371c9d4SSatish Balay         cone[6] = 37;
21339371c9d4SSatish Balay         cone[7] = 38;
21349566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
21359371c9d4SSatish Balay         cone[0] = 29;
21369371c9d4SSatish Balay         cone[1] = 27;
21379371c9d4SSatish Balay         cone[2] = 18;
21389371c9d4SSatish Balay         cone[3] = 15;
21399371c9d4SSatish Balay         cone[4] = 35;
21409371c9d4SSatish Balay         cone[5] = 31;
21419371c9d4SSatish Balay         cone[6] = 34;
21429371c9d4SSatish Balay         cone[7] = 38;
21439566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
21449371c9d4SSatish Balay         cone[0] = 29;
21459371c9d4SSatish Balay         cone[1] = 15;
21469371c9d4SSatish Balay         cone[2] = 16;
21479371c9d4SSatish Balay         cone[3] = 23;
21489371c9d4SSatish Balay         cone[4] = 35;
21499371c9d4SSatish Balay         cone[5] = 36;
21509371c9d4SSatish Balay         cone[6] = 32;
21519371c9d4SSatish Balay         cone[7] = 31;
21529566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
2153006a8963SMatthew G. Knepley 
21549371c9d4SSatish Balay         cone[0] = 31;
21559371c9d4SSatish Balay         cone[1] = 34;
21569371c9d4SSatish Balay         cone[2] = 33;
21579371c9d4SSatish Balay         cone[3] = 32;
21589371c9d4SSatish Balay         cone[4] = 19;
21599371c9d4SSatish Balay         cone[5] = 22;
21609371c9d4SSatish Balay         cone[6] = 21;
21619371c9d4SSatish Balay         cone[7] = 20;
21629566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 5, cone));
21639371c9d4SSatish Balay         cone[0] = 32;
21649371c9d4SSatish Balay         cone[1] = 33;
21659371c9d4SSatish Balay         cone[2] = 37;
21669371c9d4SSatish Balay         cone[3] = 36;
21679371c9d4SSatish Balay         cone[4] = 22;
21689371c9d4SSatish Balay         cone[5] = 25;
21699371c9d4SSatish Balay         cone[6] = 26;
21709371c9d4SSatish Balay         cone[7] = 21;
21719566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 6, cone));
21729371c9d4SSatish Balay         cone[0] = 34;
21739371c9d4SSatish Balay         cone[1] = 38;
21749371c9d4SSatish Balay         cone[2] = 37;
21759371c9d4SSatish Balay         cone[3] = 33;
21769371c9d4SSatish Balay         cone[4] = 20;
21779371c9d4SSatish Balay         cone[5] = 21;
21789371c9d4SSatish Balay         cone[6] = 26;
21799371c9d4SSatish Balay         cone[7] = 28;
21809566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 7, cone));
21819371c9d4SSatish Balay         cone[0] = 35;
21829371c9d4SSatish Balay         cone[1] = 38;
21839371c9d4SSatish Balay         cone[2] = 34;
21849371c9d4SSatish Balay         cone[3] = 31;
21859371c9d4SSatish Balay         cone[4] = 30;
21869371c9d4SSatish Balay         cone[5] = 19;
21879371c9d4SSatish Balay         cone[6] = 20;
21889371c9d4SSatish Balay         cone[7] = 28;
21899566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 8, cone));
21909371c9d4SSatish Balay         cone[0] = 35;
21919371c9d4SSatish Balay         cone[1] = 31;
21929371c9d4SSatish Balay         cone[2] = 32;
21939371c9d4SSatish Balay         cone[3] = 36;
21949371c9d4SSatish Balay         cone[4] = 30;
21959371c9d4SSatish Balay         cone[5] = 25;
21969371c9d4SSatish Balay         cone[6] = 22;
21979371c9d4SSatish Balay         cone[7] = 19;
21989566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 9, cone));
2199ae8bcbbbSMatthew G. Knepley 
22009371c9d4SSatish Balay         cone[0] = 19;
22019371c9d4SSatish Balay         cone[1] = 20;
22029371c9d4SSatish Balay         cone[2] = 21;
22039371c9d4SSatish Balay         cone[3] = 22;
22049371c9d4SSatish Balay         cone[4] = 15;
22059371c9d4SSatish Balay         cone[5] = 16;
22069371c9d4SSatish Balay         cone[6] = 17;
22079371c9d4SSatish Balay         cone[7] = 18;
22089566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 10, cone));
22099371c9d4SSatish Balay         cone[0] = 22;
22109371c9d4SSatish Balay         cone[1] = 21;
22119371c9d4SSatish Balay         cone[2] = 26;
22129371c9d4SSatish Balay         cone[3] = 25;
22139371c9d4SSatish Balay         cone[4] = 16;
22149371c9d4SSatish Balay         cone[5] = 23;
22159371c9d4SSatish Balay         cone[6] = 24;
22169371c9d4SSatish Balay         cone[7] = 17;
22179566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 11, cone));
22189371c9d4SSatish Balay         cone[0] = 20;
22199371c9d4SSatish Balay         cone[1] = 28;
22209371c9d4SSatish Balay         cone[2] = 26;
22219371c9d4SSatish Balay         cone[3] = 21;
22229371c9d4SSatish Balay         cone[4] = 18;
22239371c9d4SSatish Balay         cone[5] = 17;
22249371c9d4SSatish Balay         cone[6] = 24;
22259371c9d4SSatish Balay         cone[7] = 27;
22269566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 12, cone));
22279371c9d4SSatish Balay         cone[0] = 30;
22289371c9d4SSatish Balay         cone[1] = 28;
22299371c9d4SSatish Balay         cone[2] = 20;
22309371c9d4SSatish Balay         cone[3] = 19;
22319371c9d4SSatish Balay         cone[4] = 29;
22329371c9d4SSatish Balay         cone[5] = 15;
22339371c9d4SSatish Balay         cone[6] = 18;
22349371c9d4SSatish Balay         cone[7] = 27;
22359566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 13, cone));
22369371c9d4SSatish Balay         cone[0] = 30;
22379371c9d4SSatish Balay         cone[1] = 19;
22389371c9d4SSatish Balay         cone[2] = 22;
22399371c9d4SSatish Balay         cone[3] = 25;
22409371c9d4SSatish Balay         cone[4] = 29;
22419371c9d4SSatish Balay         cone[5] = 23;
22429371c9d4SSatish Balay         cone[6] = 16;
22439371c9d4SSatish Balay         cone[7] = 15;
22449566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 14, cone));
2245006a8963SMatthew G. Knepley       } else {
22469371c9d4SSatish Balay         cone[0] = 5;
22479371c9d4SSatish Balay         cone[1] = 8;
22489371c9d4SSatish Balay         cone[2] = 7;
22499371c9d4SSatish Balay         cone[3] = 6;
22509371c9d4SSatish Balay         cone[4] = 9;
22519371c9d4SSatish Balay         cone[5] = 12;
22529371c9d4SSatish Balay         cone[6] = 11;
22539371c9d4SSatish Balay         cone[7] = 10;
22549566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
22559371c9d4SSatish Balay         cone[0] = 6;
22569371c9d4SSatish Balay         cone[1] = 7;
22579371c9d4SSatish Balay         cone[2] = 14;
22589371c9d4SSatish Balay         cone[3] = 13;
22599371c9d4SSatish Balay         cone[4] = 12;
22609371c9d4SSatish Balay         cone[5] = 15;
22619371c9d4SSatish Balay         cone[6] = 16;
22629371c9d4SSatish Balay         cone[7] = 11;
22639566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
22649371c9d4SSatish Balay         cone[0] = 8;
22659371c9d4SSatish Balay         cone[1] = 17;
22669371c9d4SSatish Balay         cone[2] = 14;
22679371c9d4SSatish Balay         cone[3] = 7;
22689371c9d4SSatish Balay         cone[4] = 10;
22699371c9d4SSatish Balay         cone[5] = 11;
22709371c9d4SSatish Balay         cone[6] = 16;
22719371c9d4SSatish Balay         cone[7] = 18;
22729566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
22739371c9d4SSatish Balay         cone[0] = 19;
22749371c9d4SSatish Balay         cone[1] = 17;
22759371c9d4SSatish Balay         cone[2] = 8;
22769371c9d4SSatish Balay         cone[3] = 5;
22779371c9d4SSatish Balay         cone[4] = 20;
22789371c9d4SSatish Balay         cone[5] = 9;
22799371c9d4SSatish Balay         cone[6] = 10;
22809371c9d4SSatish Balay         cone[7] = 18;
22819566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
22829371c9d4SSatish Balay         cone[0] = 19;
22839371c9d4SSatish Balay         cone[1] = 5;
22849371c9d4SSatish Balay         cone[2] = 6;
22859371c9d4SSatish Balay         cone[3] = 13;
22869371c9d4SSatish Balay         cone[4] = 20;
22879371c9d4SSatish Balay         cone[5] = 15;
22889371c9d4SSatish Balay         cone[6] = 12;
22899371c9d4SSatish Balay         cone[7] = 9;
22909566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
2291006a8963SMatthew G. Knepley       }
2292d8c47e87SMatthew G. Knepley     }
22939566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
22949566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
22950510c589SMatthew G. Knepley   }
2296dbc1dc17SMatthew G. Knepley   /* Create cube geometry */
22970510c589SMatthew G. Knepley   {
22980510c589SMatthew G. Knepley     Vec             coordinates;
22990510c589SMatthew G. Knepley     PetscSection    coordSection;
23000510c589SMatthew G. Knepley     PetscScalar    *coords;
23010510c589SMatthew G. Knepley     PetscInt        coordSize, v;
23020510c589SMatthew G. Knepley     const PetscReal dis = 1.0 / PetscSqrtReal(2.0);
23030510c589SMatthew G. Knepley     const PetscReal ds2 = dis / 2.0;
23040510c589SMatthew G. Knepley 
23050510c589SMatthew G. Knepley     /* Build coordinates */
23069566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
23079566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
23089566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
23099566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices));
23100510c589SMatthew G. Knepley     for (v = numCells; v < numCells + numVertices; ++v) {
23119566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
23129566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
23130510c589SMatthew G. Knepley     }
23149566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
23159566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
23169566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
23179566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
23189566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
23199566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
23209566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates, VECSTANDARD));
23219566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
2322dd400576SPatrick Sanan     if (rank == 0) {
23239371c9d4SSatish Balay       coords[0 * dim + 0]  = -ds2;
23249371c9d4SSatish Balay       coords[0 * dim + 1]  = -ds2;
23259371c9d4SSatish Balay       coords[0 * dim + 2]  = 0.0;
23269371c9d4SSatish Balay       coords[1 * dim + 0]  = ds2;
23279371c9d4SSatish Balay       coords[1 * dim + 1]  = -ds2;
23289371c9d4SSatish Balay       coords[1 * dim + 2]  = 0.0;
23299371c9d4SSatish Balay       coords[2 * dim + 0]  = ds2;
23309371c9d4SSatish Balay       coords[2 * dim + 1]  = ds2;
23319371c9d4SSatish Balay       coords[2 * dim + 2]  = 0.0;
23329371c9d4SSatish Balay       coords[3 * dim + 0]  = -ds2;
23339371c9d4SSatish Balay       coords[3 * dim + 1]  = ds2;
23349371c9d4SSatish Balay       coords[3 * dim + 2]  = 0.0;
23359371c9d4SSatish Balay       coords[4 * dim + 0]  = -ds2;
23369371c9d4SSatish Balay       coords[4 * dim + 1]  = -ds2;
23379371c9d4SSatish Balay       coords[4 * dim + 2]  = 1.0;
23389371c9d4SSatish Balay       coords[5 * dim + 0]  = -ds2;
23399371c9d4SSatish Balay       coords[5 * dim + 1]  = ds2;
23409371c9d4SSatish Balay       coords[5 * dim + 2]  = 1.0;
23419371c9d4SSatish Balay       coords[6 * dim + 0]  = ds2;
23429371c9d4SSatish Balay       coords[6 * dim + 1]  = ds2;
23439371c9d4SSatish Balay       coords[6 * dim + 2]  = 1.0;
23449371c9d4SSatish Balay       coords[7 * dim + 0]  = ds2;
23459371c9d4SSatish Balay       coords[7 * dim + 1]  = -ds2;
23469371c9d4SSatish Balay       coords[7 * dim + 2]  = 1.0;
23479371c9d4SSatish Balay       coords[8 * dim + 0]  = dis;
23489371c9d4SSatish Balay       coords[8 * dim + 1]  = -dis;
23499371c9d4SSatish Balay       coords[8 * dim + 2]  = 0.0;
23509371c9d4SSatish Balay       coords[9 * dim + 0]  = dis;
23519371c9d4SSatish Balay       coords[9 * dim + 1]  = dis;
23529371c9d4SSatish Balay       coords[9 * dim + 2]  = 0.0;
23539371c9d4SSatish Balay       coords[10 * dim + 0] = dis;
23549371c9d4SSatish Balay       coords[10 * dim + 1] = -dis;
23559371c9d4SSatish Balay       coords[10 * dim + 2] = 1.0;
23569371c9d4SSatish Balay       coords[11 * dim + 0] = dis;
23579371c9d4SSatish Balay       coords[11 * dim + 1] = dis;
23589371c9d4SSatish Balay       coords[11 * dim + 2] = 1.0;
23599371c9d4SSatish Balay       coords[12 * dim + 0] = -dis;
23609371c9d4SSatish Balay       coords[12 * dim + 1] = dis;
23619371c9d4SSatish Balay       coords[12 * dim + 2] = 0.0;
23629371c9d4SSatish Balay       coords[13 * dim + 0] = -dis;
23639371c9d4SSatish Balay       coords[13 * dim + 1] = dis;
23649371c9d4SSatish Balay       coords[13 * dim + 2] = 1.0;
23659371c9d4SSatish Balay       coords[14 * dim + 0] = -dis;
23669371c9d4SSatish Balay       coords[14 * dim + 1] = -dis;
23679371c9d4SSatish Balay       coords[14 * dim + 2] = 0.0;
23689371c9d4SSatish Balay       coords[15 * dim + 0] = -dis;
23699371c9d4SSatish Balay       coords[15 * dim + 1] = -dis;
23709371c9d4SSatish Balay       coords[15 * dim + 2] = 1.0;
2371ae8bcbbbSMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
23729371c9d4SSatish Balay         /* 15 31 19 */ coords[16 * dim + 0] = -ds2;
23739371c9d4SSatish Balay         coords[16 * dim + 1]                = -ds2;
23749371c9d4SSatish Balay         coords[16 * dim + 2]                = 0.5;
23759371c9d4SSatish Balay         /* 16 32 22 */ coords[17 * dim + 0] = ds2;
23769371c9d4SSatish Balay         coords[17 * dim + 1]                = -ds2;
23779371c9d4SSatish Balay         coords[17 * dim + 2]                = 0.5;
23789371c9d4SSatish Balay         /* 17 33 21 */ coords[18 * dim + 0] = ds2;
23799371c9d4SSatish Balay         coords[18 * dim + 1]                = ds2;
23809371c9d4SSatish Balay         coords[18 * dim + 2]                = 0.5;
23819371c9d4SSatish Balay         /* 18 34 20 */ coords[19 * dim + 0] = -ds2;
23829371c9d4SSatish Balay         coords[19 * dim + 1]                = ds2;
23839371c9d4SSatish Balay         coords[19 * dim + 2]                = 0.5;
23849371c9d4SSatish Balay         /* 29 35 30 */ coords[20 * dim + 0] = -dis;
23859371c9d4SSatish Balay         coords[20 * dim + 1]                = -dis;
23869371c9d4SSatish Balay         coords[20 * dim + 2]                = 0.5;
23879371c9d4SSatish Balay         /* 23 36 25 */ coords[21 * dim + 0] = dis;
23889371c9d4SSatish Balay         coords[21 * dim + 1]                = -dis;
23899371c9d4SSatish Balay         coords[21 * dim + 2]                = 0.5;
23909371c9d4SSatish Balay         /* 24 37 26 */ coords[22 * dim + 0] = dis;
23919371c9d4SSatish Balay         coords[22 * dim + 1]                = dis;
23929371c9d4SSatish Balay         coords[22 * dim + 2]                = 0.5;
23939371c9d4SSatish Balay         /* 27 38 28 */ coords[23 * dim + 0] = -dis;
23949371c9d4SSatish Balay         coords[23 * dim + 1]                = dis;
23959371c9d4SSatish Balay         coords[23 * dim + 2]                = 0.5;
2396ae8bcbbbSMatthew G. Knepley       }
2397d8c47e87SMatthew G. Knepley     }
23989566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
23999566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
24009566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
24010510c589SMatthew G. Knepley   }
2402006a8963SMatthew G. Knepley   /* Create periodicity */
2403006a8963SMatthew G. Knepley   if (periodicZ == DM_BOUNDARY_PERIODIC || periodicZ == DM_BOUNDARY_TWIST) {
24046858538eSMatthew G. Knepley     PetscReal L[3]       = {-1., -1., 0.};
24056858538eSMatthew G. Knepley     PetscReal maxCell[3] = {-1., -1., 0.};
2406006a8963SMatthew G. Knepley     PetscReal lower[3]   = {0.0, 0.0, 0.0};
2407ae8bcbbbSMatthew G. Knepley     PetscReal upper[3]   = {1.0, 1.0, 1.5};
24086858538eSMatthew G. Knepley     PetscInt  numZCells  = 3;
2409006a8963SMatthew G. Knepley 
24106858538eSMatthew G. Knepley     L[2]       = upper[2] - lower[2];
24116858538eSMatthew G. Knepley     maxCell[2] = 1.1 * (L[2] / numZCells);
24124fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, maxCell, lower, L));
2413006a8963SMatthew G. Knepley   }
2414dbc1dc17SMatthew G. Knepley   {
24159318fe57SMatthew G. Knepley     DM          cdm;
24169318fe57SMatthew G. Knepley     PetscDS     cds;
24179318fe57SMatthew G. Knepley     PetscScalar c[2] = {1.0, 1.0};
2418dbc1dc17SMatthew G. Knepley 
241949704ca5SMatthew G. Knepley     PetscCall(DMPlexCreateCoordinateSpace(dm, 1, PETSC_TRUE, NULL));
24209566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
24219566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
24229566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, 2, c));
2423dbc1dc17SMatthew G. Knepley   }
242446139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
242546139095SJed Brown 
24269318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
24279566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolateInPlace_Internal(dm));
242849704ca5SMatthew G. Knepley 
242949704ca5SMatthew G. Knepley   char        oldprefix[PETSC_MAX_PATH_LEN];
243049704ca5SMatthew G. Knepley   const char *prefix;
243149704ca5SMatthew G. Knepley 
243249704ca5SMatthew G. Knepley   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, &prefix));
243349704ca5SMatthew G. Knepley   PetscCall(PetscStrncpy(oldprefix, prefix, PETSC_MAX_PATH_LEN));
243449704ca5SMatthew G. Knepley   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, "petsc_cyl_ref_"));
243549704ca5SMatthew G. Knepley   for (PetscInt r = 0; r < PetscMax(0, Nr); ++r) {
243649704ca5SMatthew G. Knepley     DM rdm;
243749704ca5SMatthew G. Knepley 
243849704ca5SMatthew G. Knepley     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &rdm));
243949704ca5SMatthew G. Knepley     PetscCall(DMPlexReplace_Internal(dm, &rdm));
244049704ca5SMatthew G. Knepley   }
244149704ca5SMatthew G. Knepley   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, oldprefix));
244249704ca5SMatthew G. Knepley   PetscCall(DMPlexRemapGeometry(dm, 0.0, snapToCylinder));
244349704ca5SMatthew G. Knepley 
244449704ca5SMatthew G. Knepley   DMLabel         bdlabel, edgelabel;
244549704ca5SMatthew G. Knepley   IS              faceIS;
244649704ca5SMatthew G. Knepley   const PetscInt *faces;
244749704ca5SMatthew G. Knepley   PetscInt        Nf;
244849704ca5SMatthew G. Knepley 
244949704ca5SMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "marker"));
245049704ca5SMatthew G. Knepley   PetscCall(DMGetLabel(dm, "marker", &bdlabel));
245149704ca5SMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "generatrix"));
245249704ca5SMatthew G. Knepley   PetscCall(DMGetLabel(dm, "generatrix", &edgelabel));
245349704ca5SMatthew G. Knepley   PetscCall(DMPlexMarkBoundaryFaces(dm, PETSC_DETERMINE, bdlabel));
245449704ca5SMatthew G. Knepley   // Remove faces on top and bottom
245549704ca5SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(bdlabel, 1, &faceIS));
2456ba1b3593SJeremy L Thompson   if (faceIS) {
245749704ca5SMatthew G. Knepley     PetscCall(ISGetLocalSize(faceIS, &Nf));
245849704ca5SMatthew G. Knepley     PetscCall(ISGetIndices(faceIS, &faces));
245949704ca5SMatthew G. Knepley     for (PetscInt f = 0; f < Nf; ++f) {
246049704ca5SMatthew G. Knepley       PetscReal vol, normal[3];
246149704ca5SMatthew G. Knepley 
246249704ca5SMatthew G. Knepley       PetscCall(DMPlexComputeCellGeometryFVM(dm, faces[f], &vol, NULL, normal));
246349704ca5SMatthew G. Knepley       if (PetscAbsReal(normal[2]) < PETSC_SMALL) PetscCall(DMLabelSetValue(edgelabel, faces[f], 1));
246449704ca5SMatthew G. Knepley     }
246549704ca5SMatthew G. Knepley     PetscCall(ISRestoreIndices(faceIS, &faces));
246649704ca5SMatthew G. Knepley     PetscCall(ISDestroy(&faceIS));
2467ba1b3593SJeremy L Thompson   }
246849704ca5SMatthew G. Knepley   PetscCall(DMPlexLabelComplete(dm, bdlabel));
246949704ca5SMatthew G. Knepley   PetscCall(DMPlexLabelComplete(dm, edgelabel));
24703ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
24710510c589SMatthew G. Knepley }
24720510c589SMatthew G. Knepley 
247324119c2aSMatthew G. Knepley /*@
24749318fe57SMatthew G. Knepley   DMPlexCreateHexCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using hexahedra.
247524119c2aSMatthew G. Knepley 
2476d083f849SBarry Smith   Collective
247724119c2aSMatthew G. Knepley 
247824119c2aSMatthew G. Knepley   Input Parameters:
2479a1cb98faSBarry Smith + comm      - The communicator for the `DM` object
248049704ca5SMatthew G. Knepley . periodicZ - The boundary type for the Z direction
248149704ca5SMatthew G. Knepley - Nr        - The number of refinements to carry out
248224119c2aSMatthew G. Knepley 
248324119c2aSMatthew G. Knepley   Output Parameter:
248420f4b53cSBarry Smith . dm - The `DM` object
248524119c2aSMatthew G. Knepley 
248624119c2aSMatthew G. Knepley   Level: beginner
248724119c2aSMatthew G. Knepley 
2488a1cb98faSBarry Smith   Note:
2489a4e35b19SJacob Faibussowitsch   Here is the output numbering looking from the bottom of the cylinder\:
2490a1cb98faSBarry Smith .vb
2491a1cb98faSBarry Smith        17-----14
2492a1cb98faSBarry Smith         |     |
2493a1cb98faSBarry Smith         |  2  |
2494a1cb98faSBarry Smith         |     |
2495a1cb98faSBarry Smith  17-----8-----7-----14
2496a1cb98faSBarry Smith   |     |     |     |
2497a1cb98faSBarry Smith   |  3  |  0  |  1  |
2498a1cb98faSBarry Smith   |     |     |     |
2499a1cb98faSBarry Smith  19-----5-----6-----13
2500a1cb98faSBarry Smith         |     |
2501a1cb98faSBarry Smith         |  4  |
2502a1cb98faSBarry Smith         |     |
2503a1cb98faSBarry Smith        19-----13
2504a1cb98faSBarry Smith 
2505a1cb98faSBarry Smith  and up through the top
2506a1cb98faSBarry Smith 
2507a1cb98faSBarry Smith        18-----16
2508a1cb98faSBarry Smith         |     |
2509a1cb98faSBarry Smith         |  2  |
2510a1cb98faSBarry Smith         |     |
2511a1cb98faSBarry Smith  18----10----11-----16
2512a1cb98faSBarry Smith   |     |     |     |
2513a1cb98faSBarry Smith   |  3  |  0  |  1  |
2514a1cb98faSBarry Smith   |     |     |     |
2515a1cb98faSBarry Smith  20-----9----12-----15
2516a1cb98faSBarry Smith         |     |
2517a1cb98faSBarry Smith         |  4  |
2518a1cb98faSBarry Smith         |     |
2519a1cb98faSBarry Smith        20-----15
2520a1cb98faSBarry Smith .ve
2521a1cb98faSBarry Smith 
25221cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
252324119c2aSMatthew G. Knepley @*/
252449704ca5SMatthew G. Knepley PetscErrorCode DMPlexCreateHexCylinderMesh(MPI_Comm comm, DMBoundaryType periodicZ, PetscInt Nr, DM *dm)
2525d71ae5a4SJacob Faibussowitsch {
25269318fe57SMatthew G. Knepley   PetscFunctionBegin;
252749704ca5SMatthew G. Knepley   PetscAssertPointer(dm, 4);
25289566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
25299566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
253049704ca5SMatthew G. Knepley   PetscCall(DMPlexCreateHexCylinderMesh_Internal(*dm, periodicZ, Nr));
25313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
25329318fe57SMatthew G. Knepley }
25339318fe57SMatthew G. Knepley 
2534d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateWedgeCylinderMesh_Internal(DM dm, PetscInt n, PetscBool interpolate)
2535d71ae5a4SJacob Faibussowitsch {
253624119c2aSMatthew G. Knepley   const PetscInt dim = 3;
2537412e9a14SMatthew G. Knepley   PetscInt       numCells, numVertices, v;
25389fe9f049SMatthew G. Knepley   PetscMPIInt    rank;
253924119c2aSMatthew G. Knepley 
254024119c2aSMatthew G. Knepley   PetscFunctionBegin;
254163a3b9bcSJacob Faibussowitsch   PetscCheck(n >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of wedges %" PetscInt_FMT " cannot be negative", n);
254246139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
25439566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
25449566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
2545412e9a14SMatthew G. Knepley   /* Must create the celltype label here so that we do not automatically try to compute the types */
25469566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "celltype"));
254724119c2aSMatthew G. Knepley   /* Create topology */
254824119c2aSMatthew G. Knepley   {
254924119c2aSMatthew G. Knepley     PetscInt cone[6], c;
255024119c2aSMatthew G. Knepley 
2551dd400576SPatrick Sanan     numCells    = rank == 0 ? n : 0;
2552dd400576SPatrick Sanan     numVertices = rank == 0 ? 2 * (n + 1) : 0;
25539566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
25549566063dSJacob Faibussowitsch     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 6));
25559566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm));
255624119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
25579371c9d4SSatish Balay       cone[0] = c + n * 1;
25589371c9d4SSatish Balay       cone[1] = (c + 1) % n + n * 1;
25599371c9d4SSatish Balay       cone[2] = 0 + 3 * n;
25609371c9d4SSatish Balay       cone[3] = c + n * 2;
25619371c9d4SSatish Balay       cone[4] = (c + 1) % n + n * 2;
25629371c9d4SSatish Balay       cone[5] = 1 + 3 * n;
25639566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(dm, c, cone));
25649566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(dm, c, DM_POLYTOPE_TRI_PRISM_TENSOR));
256524119c2aSMatthew G. Knepley     }
25669566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
25679566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
256824119c2aSMatthew G. Knepley   }
256948a46eb9SPierre Jolivet   for (v = numCells; v < numCells + numVertices; ++v) PetscCall(DMPlexSetCellType(dm, v, DM_POLYTOPE_POINT));
257024119c2aSMatthew G. Knepley   /* Create cylinder geometry */
257124119c2aSMatthew G. Knepley   {
257224119c2aSMatthew G. Knepley     Vec          coordinates;
257324119c2aSMatthew G. Knepley     PetscSection coordSection;
257424119c2aSMatthew G. Knepley     PetscScalar *coords;
2575412e9a14SMatthew G. Knepley     PetscInt     coordSize, c;
257624119c2aSMatthew G. Knepley 
257724119c2aSMatthew G. Knepley     /* Build coordinates */
25789566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
25799566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
25809566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
25819566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices));
258224119c2aSMatthew G. Knepley     for (v = numCells; v < numCells + numVertices; ++v) {
25839566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
25849566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
258524119c2aSMatthew G. Knepley     }
25869566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
25879566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
25889566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
25899566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
25909566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
25919566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
25929566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates, VECSTANDARD));
25939566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
259424119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
25959371c9d4SSatish Balay       coords[(c + 0 * n) * dim + 0] = PetscCosReal(2.0 * c * PETSC_PI / n);
25969371c9d4SSatish Balay       coords[(c + 0 * n) * dim + 1] = PetscSinReal(2.0 * c * PETSC_PI / n);
25979371c9d4SSatish Balay       coords[(c + 0 * n) * dim + 2] = 1.0;
25989371c9d4SSatish Balay       coords[(c + 1 * n) * dim + 0] = PetscCosReal(2.0 * c * PETSC_PI / n);
25999371c9d4SSatish Balay       coords[(c + 1 * n) * dim + 1] = PetscSinReal(2.0 * c * PETSC_PI / n);
26009371c9d4SSatish Balay       coords[(c + 1 * n) * dim + 2] = 0.0;
260124119c2aSMatthew G. Knepley     }
2602dd400576SPatrick Sanan     if (rank == 0) {
26039371c9d4SSatish Balay       coords[(2 * n + 0) * dim + 0] = 0.0;
26049371c9d4SSatish Balay       coords[(2 * n + 0) * dim + 1] = 0.0;
26059371c9d4SSatish Balay       coords[(2 * n + 0) * dim + 2] = 1.0;
26069371c9d4SSatish Balay       coords[(2 * n + 1) * dim + 0] = 0.0;
26079371c9d4SSatish Balay       coords[(2 * n + 1) * dim + 1] = 0.0;
26089371c9d4SSatish Balay       coords[(2 * n + 1) * dim + 2] = 0.0;
26099fe9f049SMatthew G. Knepley     }
26109566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
26119566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
26129566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
261324119c2aSMatthew G. Knepley   }
261446139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
26159318fe57SMatthew G. Knepley   /* Interpolate */
26169566063dSJacob Faibussowitsch   if (interpolate) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
26173ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
26189318fe57SMatthew G. Knepley }
26199318fe57SMatthew G. Knepley 
26209318fe57SMatthew G. Knepley /*@
26219318fe57SMatthew G. Knepley   DMPlexCreateWedgeCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using wedges.
26229318fe57SMatthew G. Knepley 
26239318fe57SMatthew G. Knepley   Collective
26249318fe57SMatthew G. Knepley 
26259318fe57SMatthew G. Knepley   Input Parameters:
2626a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
26279318fe57SMatthew G. Knepley . n           - The number of wedges around the origin
26289318fe57SMatthew G. Knepley - interpolate - Create edges and faces
26299318fe57SMatthew G. Knepley 
26309318fe57SMatthew G. Knepley   Output Parameter:
2631a1cb98faSBarry Smith . dm - The `DM` object
26329318fe57SMatthew G. Knepley 
26339318fe57SMatthew G. Knepley   Level: beginner
26349318fe57SMatthew G. Knepley 
26351cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateHexCylinderMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
26369318fe57SMatthew G. Knepley @*/
2637d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateWedgeCylinderMesh(MPI_Comm comm, PetscInt n, PetscBool interpolate, DM *dm)
2638d71ae5a4SJacob Faibussowitsch {
26399318fe57SMatthew G. Knepley   PetscFunctionBegin;
26404f572ea9SToby Isaac   PetscAssertPointer(dm, 4);
26419566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
26429566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
26439566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateWedgeCylinderMesh_Internal(*dm, n, interpolate));
26443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
264524119c2aSMatthew G. Knepley }
264624119c2aSMatthew G. Knepley 
2647d71ae5a4SJacob Faibussowitsch static inline PetscReal DiffNormReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
2648d71ae5a4SJacob Faibussowitsch {
264965a81367SMatthew G. Knepley   PetscReal prod = 0.0;
265065a81367SMatthew G. Knepley   PetscInt  i;
265165a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += PetscSqr(x[i] - y[i]);
265265a81367SMatthew G. Knepley   return PetscSqrtReal(prod);
265365a81367SMatthew G. Knepley }
2654dd2b43ebSStefano Zampini 
2655d71ae5a4SJacob Faibussowitsch static inline PetscReal DotReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
2656d71ae5a4SJacob Faibussowitsch {
265765a81367SMatthew G. Knepley   PetscReal prod = 0.0;
265865a81367SMatthew G. Knepley   PetscInt  i;
265965a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += x[i] * y[i];
266065a81367SMatthew G. Knepley   return prod;
266165a81367SMatthew G. Knepley }
266265a81367SMatthew G. Knepley 
266351a74b61SMatthew G. Knepley /* The first constant is the sphere radius */
2664d71ae5a4SJacob 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[])
2665d71ae5a4SJacob Faibussowitsch {
266651a74b61SMatthew G. Knepley   PetscReal r     = PetscRealPart(constants[0]);
266751a74b61SMatthew G. Knepley   PetscReal norm2 = 0.0, fac;
266851a74b61SMatthew G. Knepley   PetscInt  n     = uOff[1] - uOff[0], d;
266951a74b61SMatthew G. Knepley 
267051a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) norm2 += PetscSqr(PetscRealPart(u[d]));
267151a74b61SMatthew G. Knepley   fac = r / PetscSqrtReal(norm2);
267251a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) f0[d] = u[d] * fac;
267351a74b61SMatthew G. Knepley }
267451a74b61SMatthew G. Knepley 
2675d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSphereMesh_Internal(DM dm, PetscInt dim, PetscBool simplex, PetscReal R)
2676d71ae5a4SJacob Faibussowitsch {
267765a81367SMatthew G. Knepley   const PetscInt embedDim = dim + 1;
267865a81367SMatthew G. Knepley   PetscSection   coordSection;
267965a81367SMatthew G. Knepley   Vec            coordinates;
268065a81367SMatthew G. Knepley   PetscScalar   *coords;
268165a81367SMatthew G. Knepley   PetscReal     *coordsIn;
268207c565c5SJose E. Roman   PetscInt       numCells, numEdges, numVerts = 0, firstVertex = 0, v, firstEdge, coordSize, d, e;
268365a81367SMatthew G. Knepley   PetscMPIInt    rank;
268465a81367SMatthew G. Knepley 
268565a81367SMatthew G. Knepley   PetscFunctionBegin;
26869318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveBool(dm, simplex, 3);
268746139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
26889566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
26899566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, dim + 1));
26909566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
269165a81367SMatthew G. Knepley   switch (dim) {
26925c344501SMatthew G. Knepley   case 1:
26935c344501SMatthew G. Knepley     numCells = 16;
26945c344501SMatthew G. Knepley     numVerts = numCells;
26955c344501SMatthew G. Knepley 
26965c344501SMatthew G. Knepley     // Build Topology
26975c344501SMatthew G. Knepley     PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts));
26985c344501SMatthew G. Knepley     for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim));
26995c344501SMatthew G. Knepley     PetscCall(DMSetUp(dm));
27005c344501SMatthew G. Knepley     for (PetscInt c = 0; c < numCells; ++c) {
27015c344501SMatthew G. Knepley       PetscInt cone[2];
27025c344501SMatthew G. Knepley 
27035c344501SMatthew G. Knepley       cone[0] = c + numCells;
27045c344501SMatthew G. Knepley       cone[1] = (c + 1) % numVerts + numCells;
27055c344501SMatthew G. Knepley       PetscCall(DMPlexSetCone(dm, c, cone));
27065c344501SMatthew G. Knepley     }
27075c344501SMatthew G. Knepley     PetscCall(DMPlexSymmetrize(dm));
27085c344501SMatthew G. Knepley     PetscCall(DMPlexStratify(dm));
27095c344501SMatthew G. Knepley     PetscCall(PetscMalloc1(numVerts * embedDim, &coordsIn));
27105c344501SMatthew G. Knepley     for (PetscInt v = 0; v < numVerts; ++v) {
27115c344501SMatthew G. Knepley       const PetscReal rad = 2. * PETSC_PI * v / numVerts;
27125c344501SMatthew G. Knepley 
27135c344501SMatthew G. Knepley       coordsIn[v * embedDim + 0] = PetscCosReal(rad);
27145c344501SMatthew G. Knepley       coordsIn[v * embedDim + 1] = PetscSinReal(rad);
27155c344501SMatthew G. Knepley     }
27165c344501SMatthew G. Knepley     break;
271765a81367SMatthew G. Knepley   case 2:
271865a81367SMatthew G. Knepley     if (simplex) {
271951a74b61SMatthew G. Knepley       const PetscReal radius    = PetscSqrtReal(1 + PETSC_PHI * PETSC_PHI) / (1.0 + PETSC_PHI);
272051a74b61SMatthew G. Knepley       const PetscReal edgeLen   = 2.0 / (1.0 + PETSC_PHI) * (R / radius);
272165a81367SMatthew G. Knepley       const PetscInt  degree    = 5;
272251a74b61SMatthew G. Knepley       PetscReal       vertex[3] = {0.0, 1.0 / (1.0 + PETSC_PHI), PETSC_PHI / (1.0 + PETSC_PHI)};
272365a81367SMatthew G. Knepley       PetscInt        s[3]      = {1, 1, 1};
272465a81367SMatthew G. Knepley       PetscInt        cone[3];
272507c565c5SJose E. Roman       PetscInt       *graph;
272665a81367SMatthew G. Knepley 
27279371c9d4SSatish Balay       vertex[0] *= R / radius;
27289371c9d4SSatish Balay       vertex[1] *= R / radius;
27299371c9d4SSatish Balay       vertex[2] *= R / radius;
2730dd400576SPatrick Sanan       numCells    = rank == 0 ? 20 : 0;
2731dd400576SPatrick Sanan       numVerts    = rank == 0 ? 12 : 0;
273265a81367SMatthew G. Knepley       firstVertex = numCells;
273351a74b61SMatthew G. Knepley       /* Use icosahedron, which for a R-sphere has coordinates which are all cyclic permutations of
273465a81367SMatthew G. Knepley 
273565a81367SMatthew G. Knepley            (0, \pm 1/\phi+1, \pm \phi/\phi+1)
273665a81367SMatthew G. Knepley 
273765a81367SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
273851a74b61SMatthew G. Knepley          length is then given by 2/(1+\phi) = 2 * 0.38197 = 0.76393.
273965a81367SMatthew G. Knepley       */
274065a81367SMatthew G. Knepley       /* Construct vertices */
27419566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
2742dd400576SPatrick Sanan       if (rank == 0) {
274307c565c5SJose E. Roman         for (PetscInt p = 0, i = 0; p < embedDim; ++p) {
274465a81367SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
274565a81367SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
274665a81367SMatthew G. Knepley               for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[(d + p) % embedDim] * vertex[(d + p) % embedDim];
274765a81367SMatthew G. Knepley               ++i;
274865a81367SMatthew G. Knepley             }
274965a81367SMatthew G. Knepley           }
275065a81367SMatthew G. Knepley         }
275145da822fSValeria Barra       }
275265a81367SMatthew G. Knepley       /* Construct graph */
27539566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * numVerts, &graph));
275407c565c5SJose E. Roman       for (PetscInt i = 0; i < numVerts; ++i) {
275507c565c5SJose E. Roman         PetscInt k = 0;
275607c565c5SJose E. Roman         for (PetscInt j = 0; j < numVerts; ++j) {
27579371c9d4SSatish Balay           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i * embedDim], &coordsIn[j * embedDim]) - edgeLen) < PETSC_SMALL) {
27589371c9d4SSatish Balay             graph[i * numVerts + j] = 1;
27599371c9d4SSatish Balay             ++k;
27609371c9d4SSatish Balay           }
276165a81367SMatthew G. Knepley         }
276263a3b9bcSJacob Faibussowitsch         PetscCheck(k == degree, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid icosahedron, vertex %" PetscInt_FMT " degree %" PetscInt_FMT " != %" PetscInt_FMT, i, k, degree);
276365a81367SMatthew G. Knepley       }
276465a81367SMatthew G. Knepley       /* Build Topology */
27659566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts));
276607c565c5SJose E. Roman       for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim));
27679566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
276865a81367SMatthew G. Knepley       /* Cells */
276907c565c5SJose E. Roman       for (PetscInt i = 0, c = 0; i < numVerts; ++i) {
277007c565c5SJose E. Roman         for (PetscInt j = 0; j < i; ++j) {
277107c565c5SJose E. Roman           for (PetscInt k = 0; k < j; ++k) {
277265a81367SMatthew G. Knepley             if (graph[i * numVerts + j] && graph[j * numVerts + k] && graph[k * numVerts + i]) {
27739371c9d4SSatish Balay               cone[0] = firstVertex + i;
27749371c9d4SSatish Balay               cone[1] = firstVertex + j;
27759371c9d4SSatish Balay               cone[2] = firstVertex + k;
277665a81367SMatthew G. Knepley               /* Check orientation */
277765a81367SMatthew G. Knepley               {
27789371c9d4SSatish Balay                 const PetscInt epsilon[3][3][3] = {
27799371c9d4SSatish Balay                   {{0, 0, 0},  {0, 0, 1},  {0, -1, 0}},
27809371c9d4SSatish Balay                   {{0, 0, -1}, {0, 0, 0},  {1, 0, 0} },
27819371c9d4SSatish Balay                   {{0, 1, 0},  {-1, 0, 0}, {0, 0, 0} }
27829371c9d4SSatish Balay                 };
278365a81367SMatthew G. Knepley                 PetscReal normal[3];
278465a81367SMatthew G. Knepley                 PetscInt  e, f;
278565a81367SMatthew G. Knepley 
278665a81367SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) {
278765a81367SMatthew G. Knepley                   normal[d] = 0.0;
278865a81367SMatthew G. Knepley                   for (e = 0; e < embedDim; ++e) {
2789ad540459SPierre 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]);
279065a81367SMatthew G. Knepley                   }
279165a81367SMatthew G. Knepley                 }
27929371c9d4SSatish Balay                 if (DotReal(embedDim, normal, &coordsIn[i * embedDim]) < 0) {
27939371c9d4SSatish Balay                   PetscInt tmp = cone[1];
27949371c9d4SSatish Balay                   cone[1]      = cone[2];
27959371c9d4SSatish Balay                   cone[2]      = tmp;
279665a81367SMatthew G. Knepley                 }
279765a81367SMatthew G. Knepley               }
27989566063dSJacob Faibussowitsch               PetscCall(DMPlexSetCone(dm, c++, cone));
279965a81367SMatthew G. Knepley             }
280065a81367SMatthew G. Knepley           }
280165a81367SMatthew G. Knepley         }
280265a81367SMatthew G. Knepley       }
28039566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
28049566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
28059566063dSJacob Faibussowitsch       PetscCall(PetscFree(graph));
280665a81367SMatthew G. Knepley     } else {
28072829fed8SMatthew G. Knepley       /*
28082829fed8SMatthew G. Knepley         12-21--13
28092829fed8SMatthew G. Knepley          |     |
28102829fed8SMatthew G. Knepley         25  4  24
28112829fed8SMatthew G. Knepley          |     |
28122829fed8SMatthew G. Knepley   12-25--9-16--8-24--13
28132829fed8SMatthew G. Knepley    |     |     |     |
28142829fed8SMatthew G. Knepley   23  5 17  0 15  3  22
28152829fed8SMatthew G. Knepley    |     |     |     |
28162829fed8SMatthew G. Knepley   10-20--6-14--7-19--11
28172829fed8SMatthew G. Knepley          |     |
28182829fed8SMatthew G. Knepley         20  1  19
28192829fed8SMatthew G. Knepley          |     |
28202829fed8SMatthew G. Knepley         10-18--11
28212829fed8SMatthew G. Knepley          |     |
28222829fed8SMatthew G. Knepley         23  2  22
28232829fed8SMatthew G. Knepley          |     |
28242829fed8SMatthew G. Knepley         12-21--13
28252829fed8SMatthew G. Knepley        */
28262829fed8SMatthew G. Knepley       PetscInt cone[4], ornt[4];
28272829fed8SMatthew G. Knepley 
2828dd400576SPatrick Sanan       numCells    = rank == 0 ? 6 : 0;
2829dd400576SPatrick Sanan       numEdges    = rank == 0 ? 12 : 0;
2830dd400576SPatrick Sanan       numVerts    = rank == 0 ? 8 : 0;
283165a81367SMatthew G. Knepley       firstVertex = numCells;
283265a81367SMatthew G. Knepley       firstEdge   = numCells + numVerts;
28332829fed8SMatthew G. Knepley       /* Build Topology */
28349566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells + numEdges + numVerts));
283507c565c5SJose E. Roman       for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 4));
283648a46eb9SPierre Jolivet       for (e = firstEdge; e < firstEdge + numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2));
28379566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
2838dd400576SPatrick Sanan       if (rank == 0) {
28392829fed8SMatthew G. Knepley         /* Cell 0 */
28409371c9d4SSatish Balay         cone[0] = 14;
28419371c9d4SSatish Balay         cone[1] = 15;
28429371c9d4SSatish Balay         cone[2] = 16;
28439371c9d4SSatish Balay         cone[3] = 17;
28449566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
28459371c9d4SSatish Balay         ornt[0] = 0;
28469371c9d4SSatish Balay         ornt[1] = 0;
28479371c9d4SSatish Balay         ornt[2] = 0;
28489371c9d4SSatish Balay         ornt[3] = 0;
28499566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 0, ornt));
28502829fed8SMatthew G. Knepley         /* Cell 1 */
28519371c9d4SSatish Balay         cone[0] = 18;
28529371c9d4SSatish Balay         cone[1] = 19;
28539371c9d4SSatish Balay         cone[2] = 14;
28549371c9d4SSatish Balay         cone[3] = 20;
28559566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
28569371c9d4SSatish Balay         ornt[0] = 0;
28579371c9d4SSatish Balay         ornt[1] = 0;
28589371c9d4SSatish Balay         ornt[2] = -1;
28599371c9d4SSatish Balay         ornt[3] = 0;
28609566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 1, ornt));
28612829fed8SMatthew G. Knepley         /* Cell 2 */
28629371c9d4SSatish Balay         cone[0] = 21;
28639371c9d4SSatish Balay         cone[1] = 22;
28649371c9d4SSatish Balay         cone[2] = 18;
28659371c9d4SSatish Balay         cone[3] = 23;
28669566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
28679371c9d4SSatish Balay         ornt[0] = 0;
28689371c9d4SSatish Balay         ornt[1] = 0;
28699371c9d4SSatish Balay         ornt[2] = -1;
28709371c9d4SSatish Balay         ornt[3] = 0;
28719566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 2, ornt));
28722829fed8SMatthew G. Knepley         /* Cell 3 */
28739371c9d4SSatish Balay         cone[0] = 19;
28749371c9d4SSatish Balay         cone[1] = 22;
28759371c9d4SSatish Balay         cone[2] = 24;
28769371c9d4SSatish Balay         cone[3] = 15;
28779566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
28789371c9d4SSatish Balay         ornt[0] = -1;
28799371c9d4SSatish Balay         ornt[1] = -1;
28809371c9d4SSatish Balay         ornt[2] = 0;
28819371c9d4SSatish Balay         ornt[3] = -1;
28829566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 3, ornt));
28832829fed8SMatthew G. Knepley         /* Cell 4 */
28849371c9d4SSatish Balay         cone[0] = 16;
28859371c9d4SSatish Balay         cone[1] = 24;
28869371c9d4SSatish Balay         cone[2] = 21;
28879371c9d4SSatish Balay         cone[3] = 25;
28889566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
28899371c9d4SSatish Balay         ornt[0] = -1;
28909371c9d4SSatish Balay         ornt[1] = -1;
28919371c9d4SSatish Balay         ornt[2] = -1;
28929371c9d4SSatish Balay         ornt[3] = 0;
28939566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 4, ornt));
28942829fed8SMatthew G. Knepley         /* Cell 5 */
28959371c9d4SSatish Balay         cone[0] = 20;
28969371c9d4SSatish Balay         cone[1] = 17;
28979371c9d4SSatish Balay         cone[2] = 25;
28989371c9d4SSatish Balay         cone[3] = 23;
28999566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 5, cone));
29009371c9d4SSatish Balay         ornt[0] = -1;
29019371c9d4SSatish Balay         ornt[1] = -1;
29029371c9d4SSatish Balay         ornt[2] = -1;
29039371c9d4SSatish Balay         ornt[3] = -1;
29049566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 5, ornt));
29052829fed8SMatthew G. Knepley         /* Edges */
29069371c9d4SSatish Balay         cone[0] = 6;
29079371c9d4SSatish Balay         cone[1] = 7;
29089566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 14, cone));
29099371c9d4SSatish Balay         cone[0] = 7;
29109371c9d4SSatish Balay         cone[1] = 8;
29119566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 15, cone));
29129371c9d4SSatish Balay         cone[0] = 8;
29139371c9d4SSatish Balay         cone[1] = 9;
29149566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 16, cone));
29159371c9d4SSatish Balay         cone[0] = 9;
29169371c9d4SSatish Balay         cone[1] = 6;
29179566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 17, cone));
29189371c9d4SSatish Balay         cone[0] = 10;
29199371c9d4SSatish Balay         cone[1] = 11;
29209566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 18, cone));
29219371c9d4SSatish Balay         cone[0] = 11;
29229371c9d4SSatish Balay         cone[1] = 7;
29239566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 19, cone));
29249371c9d4SSatish Balay         cone[0] = 6;
29259371c9d4SSatish Balay         cone[1] = 10;
29269566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 20, cone));
29279371c9d4SSatish Balay         cone[0] = 12;
29289371c9d4SSatish Balay         cone[1] = 13;
29299566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 21, cone));
29309371c9d4SSatish Balay         cone[0] = 13;
29319371c9d4SSatish Balay         cone[1] = 11;
29329566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 22, cone));
29339371c9d4SSatish Balay         cone[0] = 10;
29349371c9d4SSatish Balay         cone[1] = 12;
29359566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 23, cone));
29369371c9d4SSatish Balay         cone[0] = 13;
29379371c9d4SSatish Balay         cone[1] = 8;
29389566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 24, cone));
29399371c9d4SSatish Balay         cone[0] = 12;
29409371c9d4SSatish Balay         cone[1] = 9;
29419566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 25, cone));
294245da822fSValeria Barra       }
29439566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
29449566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
29452829fed8SMatthew G. Knepley       /* Build coordinates */
29469566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
2947dd400576SPatrick Sanan       if (rank == 0) {
29489371c9d4SSatish Balay         coordsIn[0 * embedDim + 0] = -R;
29499371c9d4SSatish Balay         coordsIn[0 * embedDim + 1] = R;
29509371c9d4SSatish Balay         coordsIn[0 * embedDim + 2] = -R;
29519371c9d4SSatish Balay         coordsIn[1 * embedDim + 0] = R;
29529371c9d4SSatish Balay         coordsIn[1 * embedDim + 1] = R;
29539371c9d4SSatish Balay         coordsIn[1 * embedDim + 2] = -R;
29549371c9d4SSatish Balay         coordsIn[2 * embedDim + 0] = R;
29559371c9d4SSatish Balay         coordsIn[2 * embedDim + 1] = -R;
29569371c9d4SSatish Balay         coordsIn[2 * embedDim + 2] = -R;
29579371c9d4SSatish Balay         coordsIn[3 * embedDim + 0] = -R;
29589371c9d4SSatish Balay         coordsIn[3 * embedDim + 1] = -R;
29599371c9d4SSatish Balay         coordsIn[3 * embedDim + 2] = -R;
29609371c9d4SSatish Balay         coordsIn[4 * embedDim + 0] = -R;
29619371c9d4SSatish Balay         coordsIn[4 * embedDim + 1] = R;
29629371c9d4SSatish Balay         coordsIn[4 * embedDim + 2] = R;
29639371c9d4SSatish Balay         coordsIn[5 * embedDim + 0] = R;
29649371c9d4SSatish Balay         coordsIn[5 * embedDim + 1] = R;
29659371c9d4SSatish Balay         coordsIn[5 * embedDim + 2] = R;
29669371c9d4SSatish Balay         coordsIn[6 * embedDim + 0] = -R;
29679371c9d4SSatish Balay         coordsIn[6 * embedDim + 1] = -R;
29689371c9d4SSatish Balay         coordsIn[6 * embedDim + 2] = R;
29699371c9d4SSatish Balay         coordsIn[7 * embedDim + 0] = R;
29709371c9d4SSatish Balay         coordsIn[7 * embedDim + 1] = -R;
29719371c9d4SSatish Balay         coordsIn[7 * embedDim + 2] = R;
297265a81367SMatthew G. Knepley       }
297345da822fSValeria Barra     }
297465a81367SMatthew G. Knepley     break;
297565a81367SMatthew G. Knepley   case 3:
2976116ded15SMatthew G. Knepley     if (simplex) {
2977116ded15SMatthew G. Knepley       const PetscReal edgeLen         = 1.0 / PETSC_PHI;
297851a74b61SMatthew G. Knepley       PetscReal       vertexA[4]      = {0.5, 0.5, 0.5, 0.5};
297951a74b61SMatthew G. Knepley       PetscReal       vertexB[4]      = {1.0, 0.0, 0.0, 0.0};
298051a74b61SMatthew G. Knepley       PetscReal       vertexC[4]      = {0.5, 0.5 * PETSC_PHI, 0.5 / PETSC_PHI, 0.0};
2981116ded15SMatthew G. Knepley       const PetscInt  degree          = 12;
2982116ded15SMatthew G. Knepley       PetscInt        s[4]            = {1, 1, 1};
29839371c9d4SSatish Balay       PetscInt        evenPerm[12][4] = {
29849371c9d4SSatish Balay         {0, 1, 2, 3},
29859371c9d4SSatish Balay         {0, 2, 3, 1},
29869371c9d4SSatish Balay         {0, 3, 1, 2},
29879371c9d4SSatish Balay         {1, 0, 3, 2},
29889371c9d4SSatish Balay         {1, 2, 0, 3},
29899371c9d4SSatish Balay         {1, 3, 2, 0},
29909371c9d4SSatish Balay         {2, 0, 1, 3},
29919371c9d4SSatish Balay         {2, 1, 3, 0},
29929371c9d4SSatish Balay         {2, 3, 0, 1},
29939371c9d4SSatish Balay         {3, 0, 2, 1},
29949371c9d4SSatish Balay         {3, 1, 0, 2},
29959371c9d4SSatish Balay         {3, 2, 1, 0}
29969371c9d4SSatish Balay       };
2997116ded15SMatthew G. Knepley       PetscInt  cone[4];
2998116ded15SMatthew G. Knepley       PetscInt *graph, p, i, j, k, l;
2999116ded15SMatthew G. Knepley 
30009371c9d4SSatish Balay       vertexA[0] *= R;
30019371c9d4SSatish Balay       vertexA[1] *= R;
30029371c9d4SSatish Balay       vertexA[2] *= R;
30039371c9d4SSatish Balay       vertexA[3] *= R;
30049371c9d4SSatish Balay       vertexB[0] *= R;
30059371c9d4SSatish Balay       vertexB[1] *= R;
30069371c9d4SSatish Balay       vertexB[2] *= R;
30079371c9d4SSatish Balay       vertexB[3] *= R;
30089371c9d4SSatish Balay       vertexC[0] *= R;
30099371c9d4SSatish Balay       vertexC[1] *= R;
30109371c9d4SSatish Balay       vertexC[2] *= R;
30119371c9d4SSatish Balay       vertexC[3] *= R;
3012dd400576SPatrick Sanan       numCells    = rank == 0 ? 600 : 0;
3013dd400576SPatrick Sanan       numVerts    = rank == 0 ? 120 : 0;
3014116ded15SMatthew G. Knepley       firstVertex = numCells;
3015116ded15SMatthew G. Knepley       /* Use the 600-cell, which for a unit sphere has coordinates which are
3016116ded15SMatthew G. Knepley 
3017116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm 1,    \pm 1, \pm 1)                          16
3018116ded15SMatthew G. Knepley                (\pm 1,    0,       0,      0)  all cyclic permutations   8
3019116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm phi, \pm 1/phi, 0)  all even permutations    96
3020116ded15SMatthew G. Knepley 
3021116ded15SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
30226333ae4fSvaleriabarra          length is then given by 1/\phi = 0.61803.
3023116ded15SMatthew G. Knepley 
3024116ded15SMatthew G. Knepley          http://buzzard.pugetsound.edu/sage-practice/ch03s03.html
3025116ded15SMatthew G. Knepley          http://mathworld.wolfram.com/600-Cell.html
3026116ded15SMatthew G. Knepley       */
3027116ded15SMatthew G. Knepley       /* Construct vertices */
30289566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
3029116ded15SMatthew G. Knepley       i = 0;
3030dd400576SPatrick Sanan       if (rank == 0) {
3031116ded15SMatthew G. Knepley         for (s[0] = -1; s[0] < 2; s[0] += 2) {
3032116ded15SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
3033116ded15SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
3034116ded15SMatthew G. Knepley               for (s[3] = -1; s[3] < 2; s[3] += 2) {
3035116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[d] * vertexA[d];
3036116ded15SMatthew G. Knepley                 ++i;
3037116ded15SMatthew G. Knepley               }
3038116ded15SMatthew G. Knepley             }
3039116ded15SMatthew G. Knepley           }
3040116ded15SMatthew G. Knepley         }
3041116ded15SMatthew G. Knepley         for (p = 0; p < embedDim; ++p) {
3042116ded15SMatthew G. Knepley           s[1] = s[2] = s[3] = 1;
3043116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
3044116ded15SMatthew G. Knepley             for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[(d + p) % embedDim] * vertexB[(d + p) % embedDim];
3045116ded15SMatthew G. Knepley             ++i;
3046116ded15SMatthew G. Knepley           }
3047116ded15SMatthew G. Knepley         }
3048116ded15SMatthew G. Knepley         for (p = 0; p < 12; ++p) {
3049116ded15SMatthew G. Knepley           s[3] = 1;
3050116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
3051116ded15SMatthew G. Knepley             for (s[1] = -1; s[1] < 2; s[1] += 2) {
3052116ded15SMatthew G. Knepley               for (s[2] = -1; s[2] < 2; s[2] += 2) {
3053116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[evenPerm[p][d]] * vertexC[evenPerm[p][d]];
3054116ded15SMatthew G. Knepley                 ++i;
3055116ded15SMatthew G. Knepley               }
3056116ded15SMatthew G. Knepley             }
3057116ded15SMatthew G. Knepley           }
3058116ded15SMatthew G. Knepley         }
305945da822fSValeria Barra       }
306063a3b9bcSJacob Faibussowitsch       PetscCheck(i == numVerts, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertices %" PetscInt_FMT " != %" PetscInt_FMT, i, numVerts);
3061116ded15SMatthew G. Knepley       /* Construct graph */
30629566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * numVerts, &graph));
3063116ded15SMatthew G. Knepley       for (i = 0; i < numVerts; ++i) {
3064116ded15SMatthew G. Knepley         for (j = 0, k = 0; j < numVerts; ++j) {
30659371c9d4SSatish Balay           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i * embedDim], &coordsIn[j * embedDim]) - edgeLen) < PETSC_SMALL) {
30669371c9d4SSatish Balay             graph[i * numVerts + j] = 1;
30679371c9d4SSatish Balay             ++k;
30689371c9d4SSatish Balay           }
3069116ded15SMatthew G. Knepley         }
307063a3b9bcSJacob Faibussowitsch         PetscCheck(k == degree, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertex %" PetscInt_FMT " degree %" PetscInt_FMT " != %" PetscInt_FMT, i, k, degree);
3071116ded15SMatthew G. Knepley       }
3072116ded15SMatthew G. Knepley       /* Build Topology */
30739566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts));
307407c565c5SJose E. Roman       for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim));
30759566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
3076116ded15SMatthew G. Knepley       /* Cells */
3077dd400576SPatrick Sanan       if (rank == 0) {
307807c565c5SJose E. Roman         for (PetscInt i = 0, c = 0; i < numVerts; ++i) {
3079116ded15SMatthew G. Knepley           for (j = 0; j < i; ++j) {
3080116ded15SMatthew G. Knepley             for (k = 0; k < j; ++k) {
3081116ded15SMatthew G. Knepley               for (l = 0; l < k; ++l) {
30829371c9d4SSatish 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]) {
30839371c9d4SSatish Balay                   cone[0] = firstVertex + i;
30849371c9d4SSatish Balay                   cone[1] = firstVertex + j;
30859371c9d4SSatish Balay                   cone[2] = firstVertex + k;
30869371c9d4SSatish Balay                   cone[3] = firstVertex + l;
3087116ded15SMatthew G. Knepley                   /* Check orientation: https://ef.gy/linear-algebra:normal-vectors-in-higher-dimensional-spaces */
3088116ded15SMatthew G. Knepley                   {
30899371c9d4SSatish Balay                     const PetscInt epsilon[4][4][4][4] = {
30909371c9d4SSatish 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}}},
3091116ded15SMatthew G. Knepley 
30929371c9d4SSatish 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}}},
3093116ded15SMatthew G. Knepley 
30949371c9d4SSatish 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}}},
3095116ded15SMatthew G. Knepley 
30969371c9d4SSatish 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}} }
30979371c9d4SSatish Balay                     };
3098116ded15SMatthew G. Knepley                     PetscReal normal[4];
3099116ded15SMatthew G. Knepley                     PetscInt  e, f, g;
3100116ded15SMatthew G. Knepley 
3101116ded15SMatthew G. Knepley                     for (d = 0; d < embedDim; ++d) {
3102116ded15SMatthew G. Knepley                       normal[d] = 0.0;
3103116ded15SMatthew G. Knepley                       for (e = 0; e < embedDim; ++e) {
3104116ded15SMatthew G. Knepley                         for (f = 0; f < embedDim; ++f) {
3105116ded15SMatthew G. Knepley                           for (g = 0; g < embedDim; ++g) {
3106116ded15SMatthew 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]);
3107116ded15SMatthew G. Knepley                           }
3108116ded15SMatthew G. Knepley                         }
3109116ded15SMatthew G. Knepley                       }
3110116ded15SMatthew G. Knepley                     }
31119371c9d4SSatish Balay                     if (DotReal(embedDim, normal, &coordsIn[i * embedDim]) < 0) {
31129371c9d4SSatish Balay                       PetscInt tmp = cone[1];
31139371c9d4SSatish Balay                       cone[1]      = cone[2];
31149371c9d4SSatish Balay                       cone[2]      = tmp;
31159371c9d4SSatish Balay                     }
3116116ded15SMatthew G. Knepley                   }
31179566063dSJacob Faibussowitsch                   PetscCall(DMPlexSetCone(dm, c++, cone));
3118116ded15SMatthew G. Knepley                 }
3119116ded15SMatthew G. Knepley               }
3120116ded15SMatthew G. Knepley             }
3121116ded15SMatthew G. Knepley           }
3122116ded15SMatthew G. Knepley         }
312345da822fSValeria Barra       }
31249566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
31259566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
31269566063dSJacob Faibussowitsch       PetscCall(PetscFree(graph));
3127116ded15SMatthew G. Knepley     }
3128f4d061e9SPierre Jolivet     break;
3129d71ae5a4SJacob Faibussowitsch   default:
3130d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension for sphere: %" PetscInt_FMT, dim);
313165a81367SMatthew G. Knepley   }
313265a81367SMatthew G. Knepley   /* Create coordinates */
31339566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
31349566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
31359566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, embedDim));
31369566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numVerts));
31372829fed8SMatthew G. Knepley   for (v = firstVertex; v < firstVertex + numVerts; ++v) {
31389566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, embedDim));
31399566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, embedDim));
31402829fed8SMatthew G. Knepley   }
31419566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
31429566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
31439566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
31449566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, embedDim));
31459566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
31469566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
31479566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
31489566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
31499371c9d4SSatish Balay   for (v = 0; v < numVerts; ++v)
3150ad540459SPierre Jolivet     for (d = 0; d < embedDim; ++d) coords[v * embedDim + d] = coordsIn[v * embedDim + d];
31519566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
31529566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
31539566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
31549566063dSJacob Faibussowitsch   PetscCall(PetscFree(coordsIn));
315551a74b61SMatthew G. Knepley   {
315651a74b61SMatthew G. Knepley     DM          cdm;
315751a74b61SMatthew G. Knepley     PetscDS     cds;
31589318fe57SMatthew G. Knepley     PetscScalar c = R;
315951a74b61SMatthew G. Knepley 
3160e44f6aebSMatthew G. Knepley     PetscCall(DMPlexCreateCoordinateSpace(dm, 1, PETSC_TRUE, snapToSphere));
31619566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
31629566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
31639566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, 1, &c));
316451a74b61SMatthew G. Knepley   }
316546139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
31669318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
31679566063dSJacob Faibussowitsch   if (simplex) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
31683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
31699318fe57SMatthew G. Knepley }
31709318fe57SMatthew G. Knepley 
3171b7f5c055SJed Brown typedef void (*TPSEvaluateFunc)(const PetscReal[], PetscReal *, PetscReal[], PetscReal (*)[3]);
3172b7f5c055SJed Brown 
3173b7f5c055SJed Brown /*
3174b7f5c055SJed Brown  The Schwarz P implicit surface is
3175b7f5c055SJed Brown 
3176b7f5c055SJed Brown      f(x) = cos(x0) + cos(x1) + cos(x2) = 0
3177b7f5c055SJed Brown */
3178d71ae5a4SJacob Faibussowitsch static void TPSEvaluate_SchwarzP(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3])
3179d71ae5a4SJacob Faibussowitsch {
3180b7f5c055SJed Brown   PetscReal c[3] = {PetscCosReal(y[0] * PETSC_PI), PetscCosReal(y[1] * PETSC_PI), PetscCosReal(y[2] * PETSC_PI)};
3181b7f5c055SJed Brown   PetscReal g[3] = {-PetscSinReal(y[0] * PETSC_PI), -PetscSinReal(y[1] * PETSC_PI), -PetscSinReal(y[2] * PETSC_PI)};
3182b7f5c055SJed Brown   f[0]           = c[0] + c[1] + c[2];
3183b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) {
3184b7f5c055SJed Brown     grad[i] = PETSC_PI * g[i];
3185ad540459SPierre Jolivet     for (PetscInt j = 0; j < 3; j++) hess[i][j] = (i == j) ? -PetscSqr(PETSC_PI) * c[i] : 0.;
3186b7f5c055SJed Brown   }
3187b7f5c055SJed Brown }
3188b7f5c055SJed Brown 
31894663dae6SJed Brown // u[] is a tentative normal on input. Replace with the implicit function gradient in the same direction
3190d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSExtrudeNormalFunc_SchwarzP(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt r, PetscScalar u[], void *ctx)
3191d71ae5a4SJacob Faibussowitsch {
3192ad540459SPierre Jolivet   for (PetscInt i = 0; i < 3; i++) u[i] = -PETSC_PI * PetscSinReal(x[i] * PETSC_PI);
31933ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
31944663dae6SJed Brown }
31954663dae6SJed Brown 
3196b7f5c055SJed Brown /*
3197b7f5c055SJed Brown  The Gyroid implicit surface is
3198b7f5c055SJed Brown 
3199b7f5c055SJed 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)
3200b7f5c055SJed Brown 
3201b7f5c055SJed Brown */
3202d71ae5a4SJacob Faibussowitsch static void TPSEvaluate_Gyroid(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3])
3203d71ae5a4SJacob Faibussowitsch {
3204b7f5c055SJed Brown   PetscReal s[3] = {PetscSinReal(PETSC_PI * y[0]), PetscSinReal(PETSC_PI * (y[1] + .5)), PetscSinReal(PETSC_PI * (y[2] + .25))};
3205b7f5c055SJed Brown   PetscReal c[3] = {PetscCosReal(PETSC_PI * y[0]), PetscCosReal(PETSC_PI * (y[1] + .5)), PetscCosReal(PETSC_PI * (y[2] + .25))};
3206b7f5c055SJed Brown   f[0]           = s[0] * c[1] + s[1] * c[2] + s[2] * c[0];
3207b7f5c055SJed Brown   grad[0]        = PETSC_PI * (c[0] * c[1] - s[2] * s[0]);
3208b7f5c055SJed Brown   grad[1]        = PETSC_PI * (c[1] * c[2] - s[0] * s[1]);
3209b7f5c055SJed Brown   grad[2]        = PETSC_PI * (c[2] * c[0] - s[1] * s[2]);
3210b7f5c055SJed Brown   hess[0][0]     = -PetscSqr(PETSC_PI) * (s[0] * c[1] + s[2] * c[0]);
3211b7f5c055SJed Brown   hess[0][1]     = -PetscSqr(PETSC_PI) * (c[0] * s[1]);
3212b7f5c055SJed Brown   hess[0][2]     = -PetscSqr(PETSC_PI) * (c[2] * s[0]);
3213b7f5c055SJed Brown   hess[1][0]     = -PetscSqr(PETSC_PI) * (s[1] * c[2] + s[0] * c[1]);
3214b7f5c055SJed Brown   hess[1][1]     = -PetscSqr(PETSC_PI) * (c[1] * s[2]);
3215b7f5c055SJed Brown   hess[2][2]     = -PetscSqr(PETSC_PI) * (c[0] * s[1]);
3216b7f5c055SJed Brown   hess[2][0]     = -PetscSqr(PETSC_PI) * (s[2] * c[0] + s[1] * c[2]);
3217b7f5c055SJed Brown   hess[2][1]     = -PetscSqr(PETSC_PI) * (c[2] * s[0]);
3218b7f5c055SJed Brown   hess[2][2]     = -PetscSqr(PETSC_PI) * (c[1] * s[2]);
3219b7f5c055SJed Brown }
3220b7f5c055SJed Brown 
32214663dae6SJed Brown // u[] is a tentative normal on input. Replace with the implicit function gradient in the same direction
3222d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSExtrudeNormalFunc_Gyroid(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt r, PetscScalar u[], void *ctx)
3223d71ae5a4SJacob Faibussowitsch {
32244663dae6SJed Brown   PetscReal s[3] = {PetscSinReal(PETSC_PI * x[0]), PetscSinReal(PETSC_PI * (x[1] + .5)), PetscSinReal(PETSC_PI * (x[2] + .25))};
32254663dae6SJed Brown   PetscReal c[3] = {PetscCosReal(PETSC_PI * x[0]), PetscCosReal(PETSC_PI * (x[1] + .5)), PetscCosReal(PETSC_PI * (x[2] + .25))};
32264663dae6SJed Brown   u[0]           = PETSC_PI * (c[0] * c[1] - s[2] * s[0]);
32274663dae6SJed Brown   u[1]           = PETSC_PI * (c[1] * c[2] - s[0] * s[1]);
32284663dae6SJed Brown   u[2]           = PETSC_PI * (c[2] * c[0] - s[1] * s[2]);
32293ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
32304663dae6SJed Brown }
32314663dae6SJed Brown 
3232b7f5c055SJed Brown /*
3233b7f5c055SJed Brown    We wish to solve
3234b7f5c055SJed Brown 
3235b7f5c055SJed Brown          min_y || y - x ||^2  subject to f(y) = 0
3236b7f5c055SJed Brown 
3237b7f5c055SJed Brown    Let g(y) = grad(f).  The minimization problem is equivalent to asking to satisfy
3238b7f5c055SJed 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
3239b7f5c055SJed Brown    tangent space and ask for both components in the tangent space to be zero.
3240b7f5c055SJed Brown 
3241b7f5c055SJed Brown    Take g to be a column vector and compute the "full QR" factorization Q R = g,
3242b7f5c055SJed Brown    where Q = I - 2 n n^T is a symmetric orthogonal matrix.
3243b7f5c055SJed Brown    The first column of Q is parallel to g so the remaining two columns span the null space.
3244b7f5c055SJed Brown    Let Qn = Q[:,1:] be those remaining columns.  Then Qn Qn^T is an orthogonal projector into the tangent space.
3245da81f932SPierre Jolivet    Since Q is symmetric, this is equivalent to multiplying by Q and taking the last two entries.
3246b7f5c055SJed Brown    In total, we have a system of 3 equations in 3 unknowns:
3247b7f5c055SJed Brown 
3248b7f5c055SJed Brown      f(y) = 0                       1 equation
3249b7f5c055SJed Brown      Qn^T (y - x) = 0               2 equations
3250b7f5c055SJed Brown 
3251b7f5c055SJed Brown    Here, we compute the residual and Jacobian of this system.
3252b7f5c055SJed Brown */
3253d71ae5a4SJacob Faibussowitsch static void TPSNearestPointResJac(TPSEvaluateFunc feval, const PetscScalar x[], const PetscScalar y[], PetscScalar res[], PetscScalar J[])
3254d71ae5a4SJacob Faibussowitsch {
3255b7f5c055SJed Brown   PetscReal yreal[3] = {PetscRealPart(y[0]), PetscRealPart(y[1]), PetscRealPart(y[2])};
3256b7f5c055SJed Brown   PetscReal d[3]     = {PetscRealPart(y[0] - x[0]), PetscRealPart(y[1] - x[1]), PetscRealPart(y[2] - x[2])};
32572f0490c0SSatish Balay   PetscReal f, grad[3], n[3], norm, norm_y[3], nd, nd_y[3], sign;
32589371c9d4SSatish Balay   PetscReal n_y[3][3] = {
32599371c9d4SSatish Balay     {0, 0, 0},
32609371c9d4SSatish Balay     {0, 0, 0},
32619371c9d4SSatish Balay     {0, 0, 0}
32629371c9d4SSatish Balay   };
3263b7f5c055SJed Brown 
3264b7f5c055SJed Brown   feval(yreal, &f, grad, n_y);
3265b7f5c055SJed Brown 
3266b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) n[i] = grad[i];
3267b7f5c055SJed Brown   norm = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2]));
3268ad540459SPierre Jolivet   for (PetscInt i = 0; i < 3; i++) norm_y[i] = 1. / norm * n[i] * n_y[i][i];
3269b7f5c055SJed Brown 
3270b7f5c055SJed Brown   // Define the Householder reflector
3271b7f5c055SJed Brown   sign = n[0] >= 0 ? 1. : -1.;
3272b7f5c055SJed Brown   n[0] += norm * sign;
3273b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) n_y[0][i] += norm_y[i] * sign;
3274b7f5c055SJed Brown 
3275b7f5c055SJed Brown   norm      = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2]));
3276b7f5c055SJed Brown   norm_y[0] = 1. / norm * (n[0] * n_y[0][0]);
3277b7f5c055SJed Brown   norm_y[1] = 1. / norm * (n[0] * n_y[0][1] + n[1] * n_y[1][1]);
3278b7f5c055SJed Brown   norm_y[2] = 1. / norm * (n[0] * n_y[0][2] + n[2] * n_y[2][2]);
3279b7f5c055SJed Brown 
3280b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) {
3281b7f5c055SJed Brown     n[i] /= norm;
3282b7f5c055SJed Brown     for (PetscInt j = 0; j < 3; j++) {
3283b7f5c055SJed Brown       // note that n[i] is n_old[i]/norm when executing the code below
3284b7f5c055SJed Brown       n_y[i][j] = n_y[i][j] / norm - n[i] / norm * norm_y[j];
3285b7f5c055SJed Brown     }
3286b7f5c055SJed Brown   }
3287b7f5c055SJed Brown 
3288b7f5c055SJed Brown   nd = n[0] * d[0] + n[1] * d[1] + n[2] * d[2];
3289b7f5c055SJed 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];
3290b7f5c055SJed Brown 
3291b7f5c055SJed Brown   res[0] = f;
3292b7f5c055SJed Brown   res[1] = d[1] - 2 * n[1] * nd;
3293b7f5c055SJed Brown   res[2] = d[2] - 2 * n[2] * nd;
3294b7f5c055SJed Brown   // J[j][i] is J_{ij} (column major)
3295b7f5c055SJed Brown   for (PetscInt j = 0; j < 3; j++) {
3296b7f5c055SJed Brown     J[0 + j * 3] = grad[j];
3297b7f5c055SJed Brown     J[1 + j * 3] = (j == 1) * 1. - 2 * (n_y[1][j] * nd + n[1] * nd_y[j]);
3298b7f5c055SJed Brown     J[2 + j * 3] = (j == 2) * 1. - 2 * (n_y[2][j] * nd + n[2] * nd_y[j]);
3299b7f5c055SJed Brown   }
3300b7f5c055SJed Brown }
3301b7f5c055SJed Brown 
3302b7f5c055SJed Brown /*
3303b7f5c055SJed Brown    Project x to the nearest point on the implicit surface using Newton's method.
3304b7f5c055SJed Brown */
3305d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSNearestPoint(TPSEvaluateFunc feval, PetscScalar x[])
3306d71ae5a4SJacob Faibussowitsch {
3307b7f5c055SJed Brown   PetscScalar y[3] = {x[0], x[1], x[2]}; // Initial guess
3308b7f5c055SJed Brown 
3309b7f5c055SJed Brown   PetscFunctionBegin;
3310b7f5c055SJed Brown   for (PetscInt iter = 0; iter < 10; iter++) {
3311b7f5c055SJed Brown     PetscScalar res[3], J[9];
3312b7f5c055SJed Brown     PetscReal   resnorm;
3313b7f5c055SJed Brown     TPSNearestPointResJac(feval, x, y, res, J);
3314b7f5c055SJed Brown     resnorm = PetscSqrtReal(PetscSqr(PetscRealPart(res[0])) + PetscSqr(PetscRealPart(res[1])) + PetscSqr(PetscRealPart(res[2])));
3315b7f5c055SJed Brown     if (0) { // Turn on this monitor if you need to confirm quadratic convergence
331663a3b9bcSJacob 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])));
3317b7f5c055SJed Brown     }
3318b7f5c055SJed Brown     if (resnorm < PETSC_SMALL) break;
3319b7f5c055SJed Brown 
3320b7f5c055SJed Brown     // Take the Newton step
33219566063dSJacob Faibussowitsch     PetscCall(PetscKernel_A_gets_inverse_A_3(J, 0., PETSC_FALSE, NULL));
3322b7f5c055SJed Brown     PetscKernel_v_gets_v_minus_A_times_w_3(y, J, res);
3323b7f5c055SJed Brown   }
3324b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) x[i] = y[i];
33253ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3326b7f5c055SJed Brown }
3327b7f5c055SJed Brown 
3328b7f5c055SJed Brown const char *const DMPlexTPSTypes[] = {"SCHWARZ_P", "GYROID", "DMPlexTPSType", "DMPLEX_TPS_", NULL};
3329b7f5c055SJed Brown 
3330d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateTPSMesh_Internal(DM dm, DMPlexTPSType tpstype, const PetscInt extent[], const DMBoundaryType periodic[], PetscBool tps_distribute, PetscInt refinements, PetscInt layers, PetscReal thickness)
3331d71ae5a4SJacob Faibussowitsch {
3332b7f5c055SJed Brown   PetscMPIInt rank;
3333b7f5c055SJed Brown   PetscInt    topoDim = 2, spaceDim = 3, numFaces = 0, numVertices = 0, numEdges = 0;
3334b7f5c055SJed Brown   PetscInt(*edges)[2] = NULL, *edgeSets = NULL;
3335b7f5c055SJed Brown   PetscInt           *cells_flat = NULL;
3336b7f5c055SJed Brown   PetscReal          *vtxCoords  = NULL;
3337b7f5c055SJed Brown   TPSEvaluateFunc     evalFunc   = NULL;
33388434afd1SBarry Smith   PetscSimplePointFn *normalFunc = NULL;
3339b7f5c055SJed Brown   DMLabel             label;
3340b7f5c055SJed Brown 
3341b7f5c055SJed Brown   PetscFunctionBegin;
334246139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
33439566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
334463a3b9bcSJacob 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);
3345b7f5c055SJed Brown   switch (tpstype) {
3346b7f5c055SJed Brown   case DMPLEX_TPS_SCHWARZ_P:
3347b7f5c055SJed 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");
3348c5853193SPierre Jolivet     if (rank == 0) {
3349b7f5c055SJed Brown       PetscInt(*cells)[6][4][4] = NULL; // [junction, junction-face, cell, conn]
3350b7f5c055SJed Brown       PetscInt  Njunctions = 0, Ncuts = 0, Npipes[3], vcount;
3351b7f5c055SJed Brown       PetscReal L = 1;
3352b7f5c055SJed Brown 
3353b7f5c055SJed Brown       Npipes[0]   = (extent[0] + 1) * extent[1] * extent[2];
3354b7f5c055SJed Brown       Npipes[1]   = extent[0] * (extent[1] + 1) * extent[2];
3355b7f5c055SJed Brown       Npipes[2]   = extent[0] * extent[1] * (extent[2] + 1);
3356b7f5c055SJed Brown       Njunctions  = extent[0] * extent[1] * extent[2];
3357b7f5c055SJed Brown       Ncuts       = 2 * (extent[0] * extent[1] + extent[1] * extent[2] + extent[2] * extent[0]);
3358b7f5c055SJed Brown       numVertices = 4 * (Npipes[0] + Npipes[1] + Npipes[2]) + 8 * Njunctions;
33599566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(3 * numVertices, &vtxCoords));
33609566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Njunctions, &cells));
33619566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Ncuts * 4, &edges));
33629566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Ncuts * 4, &edgeSets));
3363b7f5c055SJed Brown       // x-normal pipes
3364b7f5c055SJed Brown       vcount = 0;
3365b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0] + 1; i++) {
3366b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3367b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2]; k++) {
3368b7f5c055SJed Brown             for (PetscInt l = 0; l < 4; l++) {
3369b7f5c055SJed Brown               vtxCoords[vcount++] = (2 * i - 1) * L;
3370b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * j * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3371b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * k * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3372b7f5c055SJed Brown             }
3373b7f5c055SJed Brown           }
3374b7f5c055SJed Brown         }
3375b7f5c055SJed Brown       }
3376b7f5c055SJed Brown       // y-normal pipes
3377b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0]; i++) {
3378b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1] + 1; j++) {
3379b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2]; k++) {
3380b7f5c055SJed Brown             for (PetscInt l = 0; l < 4; l++) {
3381b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * i * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3382b7f5c055SJed Brown               vtxCoords[vcount++] = (2 * j - 1) * L;
3383b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * k * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3384b7f5c055SJed Brown             }
3385b7f5c055SJed Brown           }
3386b7f5c055SJed Brown         }
3387b7f5c055SJed Brown       }
3388b7f5c055SJed Brown       // z-normal pipes
3389b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0]; i++) {
3390b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3391b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2] + 1; k++) {
3392b7f5c055SJed Brown             for (PetscInt l = 0; l < 4; l++) {
3393b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * i * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3394b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * j * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3395b7f5c055SJed Brown               vtxCoords[vcount++] = (2 * k - 1) * L;
3396b7f5c055SJed Brown             }
3397b7f5c055SJed Brown           }
3398b7f5c055SJed Brown         }
3399b7f5c055SJed Brown       }
3400b7f5c055SJed Brown       // junctions
3401b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0]; i++) {
3402b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3403b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2]; k++) {
3404b7f5c055SJed Brown             const PetscInt J = (i * extent[1] + j) * extent[2] + k, Jvoff = (Npipes[0] + Npipes[1] + Npipes[2]) * 4 + J * 8;
3405b7f5c055SJed Brown             PetscCheck(vcount / 3 == Jvoff, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unexpected vertex count");
3406b7f5c055SJed Brown             for (PetscInt ii = 0; ii < 2; ii++) {
3407b7f5c055SJed Brown               for (PetscInt jj = 0; jj < 2; jj++) {
3408b7f5c055SJed Brown                 for (PetscInt kk = 0; kk < 2; kk++) {
3409b7f5c055SJed Brown                   double Ls           = (1 - sqrt(2) / 4) * L;
3410b7f5c055SJed Brown                   vtxCoords[vcount++] = 2 * i * L + (2 * ii - 1) * Ls;
3411b7f5c055SJed Brown                   vtxCoords[vcount++] = 2 * j * L + (2 * jj - 1) * Ls;
3412b7f5c055SJed Brown                   vtxCoords[vcount++] = 2 * k * L + (2 * kk - 1) * Ls;
3413b7f5c055SJed Brown                 }
3414b7f5c055SJed Brown               }
3415b7f5c055SJed Brown             }
3416b7f5c055SJed Brown             const PetscInt jfaces[3][2][4] = {
3417b7f5c055SJed Brown               {{3, 1, 0, 2}, {7, 5, 4, 6}}, // x-aligned
3418b7f5c055SJed Brown               {{5, 4, 0, 1}, {7, 6, 2, 3}}, // y-aligned
3419b7f5c055SJed Brown               {{6, 2, 0, 4}, {7, 3, 1, 5}}  // z-aligned
3420b7f5c055SJed Brown             };
3421b7f5c055SJed Brown             const PetscInt pipe_lo[3] = {// vertex numbers of pipes
34229371c9d4SSatish 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};
3423b7f5c055SJed Brown             const PetscInt pipe_hi[3] = {// vertex numbers of pipes
34249371c9d4SSatish 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};
3425b7f5c055SJed Brown             for (PetscInt dir = 0; dir < 3; dir++) { // x,y,z
3426b7f5c055SJed Brown               const PetscInt ijk[3] = {i, j, k};
3427b7f5c055SJed Brown               for (PetscInt l = 0; l < 4; l++) { // rotations
3428b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][0] = pipe_lo[dir] + l;
3429b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][1] = Jvoff + jfaces[dir][0][l];
3430b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][2] = Jvoff + jfaces[dir][0][(l - 1 + 4) % 4];
3431b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][3] = pipe_lo[dir] + (l - 1 + 4) % 4;
3432b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][0] = Jvoff + jfaces[dir][1][l];
3433b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][1] = pipe_hi[dir] + l;
3434b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][2] = pipe_hi[dir] + (l - 1 + 4) % 4;
3435b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][3] = Jvoff + jfaces[dir][1][(l - 1 + 4) % 4];
3436b7f5c055SJed Brown                 if (ijk[dir] == 0) {
3437b7f5c055SJed Brown                   edges[numEdges][0] = pipe_lo[dir] + l;
3438b7f5c055SJed Brown                   edges[numEdges][1] = pipe_lo[dir] + (l + 1) % 4;
3439b7f5c055SJed Brown                   edgeSets[numEdges] = dir * 2 + 1;
3440b7f5c055SJed Brown                   numEdges++;
3441b7f5c055SJed Brown                 }
3442b7f5c055SJed Brown                 if (ijk[dir] + 1 == extent[dir]) {
3443b7f5c055SJed Brown                   edges[numEdges][0] = pipe_hi[dir] + l;
3444b7f5c055SJed Brown                   edges[numEdges][1] = pipe_hi[dir] + (l + 1) % 4;
3445b7f5c055SJed Brown                   edgeSets[numEdges] = dir * 2 + 2;
3446b7f5c055SJed Brown                   numEdges++;
3447b7f5c055SJed Brown                 }
3448b7f5c055SJed Brown               }
3449b7f5c055SJed Brown             }
3450b7f5c055SJed Brown           }
3451b7f5c055SJed Brown         }
3452b7f5c055SJed Brown       }
345363a3b9bcSJacob Faibussowitsch       PetscCheck(numEdges == Ncuts * 4, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Edge count %" PetscInt_FMT " incompatible with number of cuts %" PetscInt_FMT, numEdges, Ncuts);
3454b7f5c055SJed Brown       numFaces   = 24 * Njunctions;
3455b7f5c055SJed Brown       cells_flat = cells[0][0][0];
3456b7f5c055SJed Brown     }
3457b7f5c055SJed Brown     evalFunc   = TPSEvaluate_SchwarzP;
34584663dae6SJed Brown     normalFunc = TPSExtrudeNormalFunc_SchwarzP;
3459b7f5c055SJed Brown     break;
3460b7f5c055SJed Brown   case DMPLEX_TPS_GYROID:
3461c5853193SPierre Jolivet     if (rank == 0) {
3462b7f5c055SJed Brown       // This is a coarse mesh approximation of the gyroid shifted to being the zero of the level set
3463b7f5c055SJed Brown       //
3464b7f5c055SJed 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)
3465b7f5c055SJed Brown       //
3466b7f5c055SJed Brown       // on the cell [0,2]^3.
3467b7f5c055SJed Brown       //
3468b7f5c055SJed Brown       // Think about dividing that cell into four columns, and focus on the column [0,1]x[0,1]x[0,2].
3469b7f5c055SJed Brown       // If you looked at the gyroid in that column at different slices of z you would see that it kind of spins
3470b7f5c055SJed Brown       // like a boomerang:
3471b7f5c055SJed Brown       //
3472b7f5c055SJed Brown       //     z = 0          z = 1/4        z = 1/2        z = 3/4     //
3473b7f5c055SJed Brown       //     -----          -------        -------        -------     //
3474b7f5c055SJed Brown       //                                                              //
3475b7f5c055SJed Brown       //     +       +      +       +      +       +      +   \   +   //
3476b7f5c055SJed Brown       //      \                                   /            \      //
3477b7f5c055SJed Brown       //       \            `-_   _-'            /              }     //
3478b7f5c055SJed Brown       //        *-_            `-'            _-'              /      //
3479b7f5c055SJed Brown       //     +     `-+      +       +      +-'     +      +   /   +   //
3480b7f5c055SJed Brown       //                                                              //
3481b7f5c055SJed Brown       //                                                              //
3482b7f5c055SJed Brown       //     z = 1          z = 5/4        z = 3/2        z = 7/4     //
3483b7f5c055SJed Brown       //     -----          -------        -------        -------     //
3484b7f5c055SJed Brown       //                                                              //
3485b7f5c055SJed Brown       //     +-_     +      +       +      +     _-+      +   /   +   //
3486b7f5c055SJed Brown       //        `-_            _-_            _-`            /        //
3487b7f5c055SJed Brown       //           \        _-'   `-_        /              {         //
3488b7f5c055SJed Brown       //            \                       /                \        //
3489b7f5c055SJed Brown       //     +       +      +       +      +       +      +   \   +   //
3490b7f5c055SJed Brown       //
3491b7f5c055SJed Brown       //
3492b7f5c055SJed Brown       // This course mesh approximates each of these slices by two line segments,
3493b7f5c055SJed Brown       // and then connects the segments in consecutive layers with quadrilateral faces.
3494b7f5c055SJed Brown       // All of the end points of the segments are multiples of 1/4 except for the
3495b7f5c055SJed Brown       // point * in the picture for z = 0 above and the similar points in other layers.
3496b7f5c055SJed Brown       // That point is at (gamma, gamma, 0), where gamma is calculated below.
3497b7f5c055SJed Brown       //
3498b7f5c055SJed Brown       // The column  [1,2]x[1,2]x[0,2] looks the same as this column;
3499b7f5c055SJed Brown       // The columns [1,2]x[0,1]x[0,2] and [0,1]x[1,2]x[0,2] are mirror images.
3500b7f5c055SJed Brown       //
3501b7f5c055SJed Brown       // As for how this method turned into the names given to the vertices:
3502b7f5c055SJed Brown       // that was not systematic, it was just the way it worked out in my handwritten notes.
3503b7f5c055SJed Brown 
3504b7f5c055SJed Brown       PetscInt facesPerBlock = 64;
3505b7f5c055SJed Brown       PetscInt vertsPerBlock = 56;
3506b7f5c055SJed Brown       PetscInt extentPlus[3];
3507b7f5c055SJed Brown       PetscInt numBlocks, numBlocksPlus;
35089371c9d4SSatish 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;
35099371c9d4SSatish Balay       const PetscInt pattern[64][4] = {
35109371c9d4SSatish Balay         /* face to vertex within the coarse discretization of a single gyroid block */
3511b7f5c055SJed Brown         /* layer 0 */
35129371c9d4SSatish Balay         {A,           C,           K,           G          },
35139371c9d4SSatish Balay         {C,           B,           II,          K          },
35149371c9d4SSatish Balay         {D,           A,           H,           L          },
35159371c9d4SSatish Balay         {B + 56 * 1,  D,           L,           J          },
35169371c9d4SSatish Balay         {E,           B + 56 * 1,  J,           N          },
35179371c9d4SSatish Balay         {A + 56 * 2,  E,           N,           H + 56 * 2 },
35189371c9d4SSatish Balay         {F,           A + 56 * 2,  G + 56 * 2,  M          },
35199371c9d4SSatish Balay         {B,           F,           M,           II         },
3520b7f5c055SJed Brown         /* layer 1 */
35219371c9d4SSatish Balay         {G,           K,           Q,           O          },
35229371c9d4SSatish Balay         {K,           II,          P,           Q          },
35239371c9d4SSatish Balay         {L,           H,           O + 56 * 1,  R          },
35249371c9d4SSatish Balay         {J,           L,           R,           P          },
35259371c9d4SSatish Balay         {N,           J,           P,           S          },
35269371c9d4SSatish Balay         {H + 56 * 2,  N,           S,           O + 56 * 3 },
35279371c9d4SSatish Balay         {M,           G + 56 * 2,  O + 56 * 2,  T          },
35289371c9d4SSatish Balay         {II,          M,           T,           P          },
3529b7f5c055SJed Brown         /* layer 2 */
35309371c9d4SSatish Balay         {O,           Q,           Y,           U          },
35319371c9d4SSatish Balay         {Q,           P,           W,           Y          },
35329371c9d4SSatish Balay         {R,           O + 56 * 1,  U + 56 * 1,  Ap         },
35339371c9d4SSatish Balay         {P,           R,           Ap,          W          },
35349371c9d4SSatish Balay         {S,           P,           X,           Bp         },
35359371c9d4SSatish Balay         {O + 56 * 3,  S,           Bp,          V + 56 * 1 },
35369371c9d4SSatish Balay         {T,           O + 56 * 2,  V,           Z          },
35379371c9d4SSatish Balay         {P,           T,           Z,           X          },
3538b7f5c055SJed Brown         /* layer 3 */
35399371c9d4SSatish Balay         {U,           Y,           Ep,          Dp         },
35409371c9d4SSatish Balay         {Y,           W,           Cp,          Ep         },
35419371c9d4SSatish Balay         {Ap,          U + 56 * 1,  Dp + 56 * 1, Gp         },
35429371c9d4SSatish Balay         {W,           Ap,          Gp,          Cp         },
35439371c9d4SSatish Balay         {Bp,          X,           Cp + 56 * 2, Fp         },
35449371c9d4SSatish Balay         {V + 56 * 1,  Bp,          Fp,          Dp + 56 * 1},
35459371c9d4SSatish Balay         {Z,           V,           Dp,          Hp         },
35469371c9d4SSatish Balay         {X,           Z,           Hp,          Cp + 56 * 2},
3547b7f5c055SJed Brown         /* layer 4 */
35489371c9d4SSatish Balay         {Dp,          Ep,          Mp,          Kp         },
35499371c9d4SSatish Balay         {Ep,          Cp,          Ip,          Mp         },
35509371c9d4SSatish Balay         {Gp,          Dp + 56 * 1, Lp,          Np         },
35519371c9d4SSatish Balay         {Cp,          Gp,          Np,          Jp         },
35529371c9d4SSatish Balay         {Fp,          Cp + 56 * 2, Jp + 56 * 2, Pp         },
35539371c9d4SSatish Balay         {Dp + 56 * 1, Fp,          Pp,          Lp         },
35549371c9d4SSatish Balay         {Hp,          Dp,          Kp,          Op         },
35559371c9d4SSatish Balay         {Cp + 56 * 2, Hp,          Op,          Ip + 56 * 2},
3556b7f5c055SJed Brown         /* layer 5 */
35579371c9d4SSatish Balay         {Kp,          Mp,          Sp,          Rp         },
35589371c9d4SSatish Balay         {Mp,          Ip,          Qp,          Sp         },
35599371c9d4SSatish Balay         {Np,          Lp,          Rp,          Tp         },
35609371c9d4SSatish Balay         {Jp,          Np,          Tp,          Qp + 56 * 1},
35619371c9d4SSatish Balay         {Pp,          Jp + 56 * 2, Qp + 56 * 3, Up         },
35629371c9d4SSatish Balay         {Lp,          Pp,          Up,          Rp         },
35639371c9d4SSatish Balay         {Op,          Kp,          Rp,          Vp         },
35649371c9d4SSatish Balay         {Ip + 56 * 2, Op,          Vp,          Qp + 56 * 2},
3565b7f5c055SJed Brown         /* layer 6 */
35669371c9d4SSatish Balay         {Rp,          Sp,          Aq,          Yp         },
35679371c9d4SSatish Balay         {Sp,          Qp,          Wp,          Aq         },
35689371c9d4SSatish Balay         {Tp,          Rp,          Yp,          Cq         },
35699371c9d4SSatish Balay         {Qp + 56 * 1, Tp,          Cq,          Wp + 56 * 1},
35709371c9d4SSatish Balay         {Up,          Qp + 56 * 3, Xp + 56 * 1, Dq         },
35719371c9d4SSatish Balay         {Rp,          Up,          Dq,          Zp         },
35729371c9d4SSatish Balay         {Vp,          Rp,          Zp,          Bq         },
35739371c9d4SSatish Balay         {Qp + 56 * 2, Vp,          Bq,          Xp         },
3574b7f5c055SJed Brown         /* layer 7 (the top is the periodic image of the bottom of layer 0) */
35759371c9d4SSatish Balay         {Yp,          Aq,          C + 56 * 4,  A + 56 * 4 },
35769371c9d4SSatish Balay         {Aq,          Wp,          B + 56 * 4,  C + 56 * 4 },
35779371c9d4SSatish Balay         {Cq,          Yp,          A + 56 * 4,  D + 56 * 4 },
35789371c9d4SSatish Balay         {Wp + 56 * 1, Cq,          D + 56 * 4,  B + 56 * 5 },
35799371c9d4SSatish Balay         {Dq,          Xp + 56 * 1, B + 56 * 5,  E + 56 * 4 },
35809371c9d4SSatish Balay         {Zp,          Dq,          E + 56 * 4,  A + 56 * 6 },
35819371c9d4SSatish Balay         {Bq,          Zp,          A + 56 * 6,  F + 56 * 4 },
35829371c9d4SSatish Balay         {Xp,          Bq,          F + 56 * 4,  B + 56 * 4 }
3583b7f5c055SJed Brown       };
3584b7f5c055SJed Brown       const PetscReal gamma                = PetscAcosReal((PetscSqrtReal(3.) - 1.) / PetscSqrtReal(2.)) / PETSC_PI;
35859371c9d4SSatish Balay       const PetscReal patternCoords[56][3] = {
3586bee3fc89SBarry Smith         {1.,        0.,        0.  }, /* A  */
3587bee3fc89SBarry Smith         {0.,        1.,        0.  }, /* B  */
3588bee3fc89SBarry Smith         {gamma,     gamma,     0.  }, /* C  */
3589bee3fc89SBarry Smith         {1 + gamma, 1 - gamma, 0.  }, /* D  */
3590bee3fc89SBarry Smith         {2 - gamma, 2 - gamma, 0.  }, /* E  */
3591bee3fc89SBarry Smith         {1 - gamma, 1 + gamma, 0.  }, /* F  */
3592b7f5c055SJed Brown 
3593bee3fc89SBarry Smith         {.5,        0,         .25 }, /* G  */
3594bee3fc89SBarry Smith         {1.5,       0.,        .25 }, /* H  */
3595bee3fc89SBarry Smith         {.5,        1.,        .25 }, /* II */
3596bee3fc89SBarry Smith         {1.5,       1.,        .25 }, /* J  */
3597bee3fc89SBarry Smith         {.25,       .5,        .25 }, /* K  */
3598bee3fc89SBarry Smith         {1.25,      .5,        .25 }, /* L  */
3599bee3fc89SBarry Smith         {.75,       1.5,       .25 }, /* M  */
3600bee3fc89SBarry Smith         {1.75,      1.5,       .25 }, /* N  */
3601b7f5c055SJed Brown 
3602bee3fc89SBarry Smith         {0.,        0.,        .5  }, /* O  */
3603bee3fc89SBarry Smith         {1.,        1.,        .5  }, /* P  */
3604bee3fc89SBarry Smith         {gamma,     1 - gamma, .5  }, /* Q  */
3605bee3fc89SBarry Smith         {1 + gamma, gamma,     .5  }, /* R  */
3606bee3fc89SBarry Smith         {2 - gamma, 1 + gamma, .5  }, /* S  */
3607bee3fc89SBarry Smith         {1 - gamma, 2 - gamma, .5  }, /* T  */
3608b7f5c055SJed Brown 
3609bee3fc89SBarry Smith         {0.,        .5,        .75 }, /* U  */
3610bee3fc89SBarry Smith         {0.,        1.5,       .75 }, /* V  */
3611bee3fc89SBarry Smith         {1.,        .5,        .75 }, /* W  */
3612bee3fc89SBarry Smith         {1.,        1.5,       .75 }, /* X  */
3613bee3fc89SBarry Smith         {.5,        .75,       .75 }, /* Y  */
3614bee3fc89SBarry Smith         {.5,        1.75,      .75 }, /* Z  */
3615bee3fc89SBarry Smith         {1.5,       .25,       .75 }, /* Ap */
3616bee3fc89SBarry Smith         {1.5,       1.25,      .75 }, /* Bp */
3617b7f5c055SJed Brown 
3618bee3fc89SBarry Smith         {1.,        0.,        1.  }, /* Cp */
3619bee3fc89SBarry Smith         {0.,        1.,        1.  }, /* Dp */
3620bee3fc89SBarry Smith         {1 - gamma, 1 - gamma, 1.  }, /* Ep */
3621bee3fc89SBarry Smith         {1 + gamma, 1 + gamma, 1.  }, /* Fp */
3622bee3fc89SBarry Smith         {2 - gamma, gamma,     1.  }, /* Gp */
3623bee3fc89SBarry Smith         {gamma,     2 - gamma, 1.  }, /* Hp */
3624b7f5c055SJed Brown 
3625bee3fc89SBarry Smith         {.5,        0.,        1.25}, /* Ip */
3626bee3fc89SBarry Smith         {1.5,       0.,        1.25}, /* Jp */
3627bee3fc89SBarry Smith         {.5,        1.,        1.25}, /* Kp */
3628bee3fc89SBarry Smith         {1.5,       1.,        1.25}, /* Lp */
3629bee3fc89SBarry Smith         {.75,       .5,        1.25}, /* Mp */
3630bee3fc89SBarry Smith         {1.75,      .5,        1.25}, /* Np */
3631bee3fc89SBarry Smith         {.25,       1.5,       1.25}, /* Op */
3632bee3fc89SBarry Smith         {1.25,      1.5,       1.25}, /* Pp */
3633b7f5c055SJed Brown 
3634bee3fc89SBarry Smith         {0.,        0.,        1.5 }, /* Qp */
3635bee3fc89SBarry Smith         {1.,        1.,        1.5 }, /* Rp */
3636bee3fc89SBarry Smith         {1 - gamma, gamma,     1.5 }, /* Sp */
3637bee3fc89SBarry Smith         {2 - gamma, 1 - gamma, 1.5 }, /* Tp */
3638bee3fc89SBarry Smith         {1 + gamma, 2 - gamma, 1.5 }, /* Up */
3639bee3fc89SBarry Smith         {gamma,     1 + gamma, 1.5 }, /* Vp */
3640b7f5c055SJed Brown 
3641bee3fc89SBarry Smith         {0.,        .5,        1.75}, /* Wp */
3642bee3fc89SBarry Smith         {0.,        1.5,       1.75}, /* Xp */
3643bee3fc89SBarry Smith         {1.,        .5,        1.75}, /* Yp */
3644bee3fc89SBarry Smith         {1.,        1.5,       1.75}, /* Zp */
3645bee3fc89SBarry Smith         {.5,        .25,       1.75}, /* Aq */
3646bee3fc89SBarry Smith         {.5,        1.25,      1.75}, /* Bq */
3647bee3fc89SBarry Smith         {1.5,       .75,       1.75}, /* Cq */
3648bee3fc89SBarry Smith         {1.5,       1.75,      1.75}, /* Dq */
3649b7f5c055SJed Brown       };
3650b7f5c055SJed Brown       PetscInt(*cells)[64][4] = NULL;
3651b7f5c055SJed Brown       PetscBool *seen;
3652b7f5c055SJed Brown       PetscInt  *vertToTrueVert;
3653b7f5c055SJed Brown       PetscInt   count;
3654b7f5c055SJed Brown 
3655b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) extentPlus[i] = extent[i] + 1;
3656b7f5c055SJed Brown       numBlocks = 1;
3657b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) numBlocks *= extent[i];
3658b7f5c055SJed Brown       numBlocksPlus = 1;
3659b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) numBlocksPlus *= extentPlus[i];
3660b7f5c055SJed Brown       numFaces = numBlocks * facesPerBlock;
36619566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numBlocks, &cells));
36629566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numBlocksPlus * vertsPerBlock, &seen));
3663b7f5c055SJed Brown       for (PetscInt k = 0; k < extent[2]; k++) {
3664b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3665b7f5c055SJed Brown           for (PetscInt i = 0; i < extent[0]; i++) {
3666b7f5c055SJed Brown             for (PetscInt f = 0; f < facesPerBlock; f++) {
3667b7f5c055SJed Brown               for (PetscInt v = 0; v < 4; v++) {
3668b7f5c055SJed Brown                 PetscInt vertRaw     = pattern[f][v];
3669b7f5c055SJed Brown                 PetscInt blockidx    = vertRaw / 56;
3670b7f5c055SJed Brown                 PetscInt patternvert = vertRaw % 56;
3671b7f5c055SJed Brown                 PetscInt xplus       = (blockidx & 1);
3672b7f5c055SJed Brown                 PetscInt yplus       = (blockidx & 2) >> 1;
3673b7f5c055SJed Brown                 PetscInt zplus       = (blockidx & 4) >> 2;
3674b7f5c055SJed Brown                 PetscInt zcoord      = (periodic && periodic[2] == DM_BOUNDARY_PERIODIC) ? ((k + zplus) % extent[2]) : (k + zplus);
3675b7f5c055SJed Brown                 PetscInt ycoord      = (periodic && periodic[1] == DM_BOUNDARY_PERIODIC) ? ((j + yplus) % extent[1]) : (j + yplus);
3676b7f5c055SJed Brown                 PetscInt xcoord      = (periodic && periodic[0] == DM_BOUNDARY_PERIODIC) ? ((i + xplus) % extent[0]) : (i + xplus);
3677b7f5c055SJed Brown                 PetscInt vert        = ((zcoord * extentPlus[1] + ycoord) * extentPlus[0] + xcoord) * 56 + patternvert;
3678b7f5c055SJed Brown 
3679b7f5c055SJed Brown                 cells[(k * extent[1] + j) * extent[0] + i][f][v] = vert;
3680b7f5c055SJed Brown                 seen[vert]                                       = PETSC_TRUE;
3681b7f5c055SJed Brown               }
3682b7f5c055SJed Brown             }
3683b7f5c055SJed Brown           }
3684b7f5c055SJed Brown         }
3685b7f5c055SJed Brown       }
36869371c9d4SSatish Balay       for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++)
36879371c9d4SSatish Balay         if (seen[i]) numVertices++;
3688b7f5c055SJed Brown       count = 0;
36899566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numBlocksPlus * vertsPerBlock, &vertToTrueVert));
36909566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numVertices * 3, &vtxCoords));
3691b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++) vertToTrueVert[i] = -1;
3692b7f5c055SJed Brown       for (PetscInt k = 0; k < extentPlus[2]; k++) {
3693b7f5c055SJed Brown         for (PetscInt j = 0; j < extentPlus[1]; j++) {
3694b7f5c055SJed Brown           for (PetscInt i = 0; i < extentPlus[0]; i++) {
3695b7f5c055SJed Brown             for (PetscInt v = 0; v < vertsPerBlock; v++) {
3696b7f5c055SJed Brown               PetscInt vIdx = ((k * extentPlus[1] + j) * extentPlus[0] + i) * vertsPerBlock + v;
3697b7f5c055SJed Brown 
3698b7f5c055SJed Brown               if (seen[vIdx]) {
3699b7f5c055SJed Brown                 PetscInt thisVert;
3700b7f5c055SJed Brown 
3701b7f5c055SJed Brown                 vertToTrueVert[vIdx] = thisVert = count++;
3702b7f5c055SJed Brown 
3703b7f5c055SJed Brown                 for (PetscInt d = 0; d < 3; d++) vtxCoords[3 * thisVert + d] = patternCoords[v][d];
3704b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 0] += i * 2;
3705b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 1] += j * 2;
3706b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 2] += k * 2;
3707b7f5c055SJed Brown               }
3708b7f5c055SJed Brown             }
3709b7f5c055SJed Brown           }
3710b7f5c055SJed Brown         }
3711b7f5c055SJed Brown       }
3712b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocks; i++) {
3713b7f5c055SJed Brown         for (PetscInt f = 0; f < facesPerBlock; f++) {
3714ad540459SPierre Jolivet           for (PetscInt v = 0; v < 4; v++) cells[i][f][v] = vertToTrueVert[cells[i][f][v]];
3715b7f5c055SJed Brown         }
3716b7f5c055SJed Brown       }
37179566063dSJacob Faibussowitsch       PetscCall(PetscFree(vertToTrueVert));
37189566063dSJacob Faibussowitsch       PetscCall(PetscFree(seen));
3719b7f5c055SJed Brown       cells_flat = cells[0][0];
3720b7f5c055SJed Brown       numEdges   = 0;
3721b7f5c055SJed Brown       for (PetscInt i = 0; i < numFaces; i++) {
3722b7f5c055SJed Brown         for (PetscInt e = 0; e < 4; e++) {
3723b7f5c055SJed Brown           PetscInt         ev[]       = {cells_flat[i * 4 + e], cells_flat[i * 4 + ((e + 1) % 4)]};
3724b7f5c055SJed Brown           const PetscReal *evCoords[] = {&vtxCoords[3 * ev[0]], &vtxCoords[3 * ev[1]]};
3725b7f5c055SJed Brown 
3726b7f5c055SJed Brown           for (PetscInt d = 0; d < 3; d++) {
3727b7f5c055SJed Brown             if (!periodic || periodic[0] != DM_BOUNDARY_PERIODIC) {
3728b7f5c055SJed Brown               if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) numEdges++;
3729b7f5c055SJed Brown               if (evCoords[0][d] == 2. * extent[d] && evCoords[1][d] == 2. * extent[d]) numEdges++;
3730b7f5c055SJed Brown             }
3731b7f5c055SJed Brown           }
3732b7f5c055SJed Brown         }
3733b7f5c055SJed Brown       }
37349566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numEdges, &edges));
37359566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numEdges, &edgeSets));
3736b7f5c055SJed Brown       for (PetscInt edge = 0, i = 0; i < numFaces; i++) {
3737b7f5c055SJed Brown         for (PetscInt e = 0; e < 4; e++) {
3738b7f5c055SJed Brown           PetscInt         ev[]       = {cells_flat[i * 4 + e], cells_flat[i * 4 + ((e + 1) % 4)]};
3739b7f5c055SJed Brown           const PetscReal *evCoords[] = {&vtxCoords[3 * ev[0]], &vtxCoords[3 * ev[1]]};
3740b7f5c055SJed Brown 
3741b7f5c055SJed Brown           for (PetscInt d = 0; d < 3; d++) {
3742b7f5c055SJed Brown             if (!periodic || periodic[d] != DM_BOUNDARY_PERIODIC) {
3743b7f5c055SJed Brown               if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) {
3744b7f5c055SJed Brown                 edges[edge][0]   = ev[0];
3745b7f5c055SJed Brown                 edges[edge][1]   = ev[1];
3746b7f5c055SJed Brown                 edgeSets[edge++] = 2 * d;
3747b7f5c055SJed Brown               }
3748b7f5c055SJed Brown               if (evCoords[0][d] == 2. * extent[d] && evCoords[1][d] == 2. * extent[d]) {
3749b7f5c055SJed Brown                 edges[edge][0]   = ev[0];
3750b7f5c055SJed Brown                 edges[edge][1]   = ev[1];
3751b7f5c055SJed Brown                 edgeSets[edge++] = 2 * d + 1;
3752b7f5c055SJed Brown               }
3753b7f5c055SJed Brown             }
3754b7f5c055SJed Brown           }
3755b7f5c055SJed Brown         }
3756b7f5c055SJed Brown       }
3757b7f5c055SJed Brown     }
3758b7f5c055SJed Brown     evalFunc   = TPSEvaluate_Gyroid;
37594663dae6SJed Brown     normalFunc = TPSExtrudeNormalFunc_Gyroid;
3760b7f5c055SJed Brown     break;
3761b7f5c055SJed Brown   }
3762b7f5c055SJed Brown 
37639566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, topoDim));
3764c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildFromCellList(dm, numFaces, numVertices, 4, cells_flat));
37659566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildFromCellList(dm, 0, 0, 0, NULL));
37669566063dSJacob Faibussowitsch   PetscCall(PetscFree(cells_flat));
3767b7f5c055SJed Brown   {
3768b7f5c055SJed Brown     DM idm;
37699566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(dm, &idm));
377069d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &idm));
3771b7f5c055SJed Brown   }
3772c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildCoordinatesFromCellList(dm, spaceDim, vtxCoords));
37739566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildCoordinatesFromCellList(dm, spaceDim, NULL));
37749566063dSJacob Faibussowitsch   PetscCall(PetscFree(vtxCoords));
3775b7f5c055SJed Brown 
37769566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
37779566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "Face Sets", &label));
3778b7f5c055SJed Brown   for (PetscInt e = 0; e < numEdges; e++) {
3779b7f5c055SJed Brown     PetscInt        njoin;
3780b7f5c055SJed Brown     const PetscInt *join, verts[] = {numFaces + edges[e][0], numFaces + edges[e][1]};
37819566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, 2, verts, &njoin, &join));
378263a3b9bcSJacob 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]);
37839566063dSJacob Faibussowitsch     PetscCall(DMLabelSetValue(label, join[0], edgeSets[e]));
37849566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, 2, verts, &njoin, &join));
3785b7f5c055SJed Brown   }
37869566063dSJacob Faibussowitsch   PetscCall(PetscFree(edges));
37879566063dSJacob Faibussowitsch   PetscCall(PetscFree(edgeSets));
37881436d7faSJed Brown   if (tps_distribute) {
37891436d7faSJed Brown     DM               pdm = NULL;
37901436d7faSJed Brown     PetscPartitioner part;
37911436d7faSJed Brown 
37929566063dSJacob Faibussowitsch     PetscCall(DMPlexGetPartitioner(dm, &part));
37939566063dSJacob Faibussowitsch     PetscCall(PetscPartitionerSetFromOptions(part));
37949566063dSJacob Faibussowitsch     PetscCall(DMPlexDistribute(dm, 0, NULL, &pdm));
379548a46eb9SPierre Jolivet     if (pdm) PetscCall(DMPlexReplace_Internal(dm, &pdm));
37961436d7faSJed Brown     // Do not auto-distribute again
37979566063dSJacob Faibussowitsch     PetscCall(DMPlexDistributeSetDefault(dm, PETSC_FALSE));
37981436d7faSJed Brown   }
3799b7f5c055SJed Brown 
38009566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
3801b7f5c055SJed Brown   for (PetscInt refine = 0; refine < refinements; refine++) {
3802b7f5c055SJed Brown     PetscInt     m;
3803b7f5c055SJed Brown     DM           dmf;
3804b7f5c055SJed Brown     Vec          X;
3805b7f5c055SJed Brown     PetscScalar *x;
38069566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, MPI_COMM_NULL, &dmf));
380769d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &dmf));
3808b7f5c055SJed Brown 
38099566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &X));
38109566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(X, &m));
38119566063dSJacob Faibussowitsch     PetscCall(VecGetArray(X, &x));
381248a46eb9SPierre Jolivet     for (PetscInt i = 0; i < m; i += 3) PetscCall(TPSNearestPoint(evalFunc, &x[i]));
38139566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(X, &x));
3814b7f5c055SJed Brown   }
3815b7f5c055SJed Brown 
3816b7f5c055SJed Brown   // Face Sets has already been propagated to new vertices during refinement; this propagates to the initial vertices.
38179566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "Face Sets", &label));
38189566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete(dm, label));
3819b7f5c055SJed Brown 
382046139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
382146139095SJed Brown 
3822b7f5c055SJed Brown   if (thickness > 0) {
38234663dae6SJed Brown     DM              edm, cdm, ecdm;
38244663dae6SJed Brown     DMPlexTransform tr;
38254663dae6SJed Brown     const char     *prefix;
38264663dae6SJed Brown     PetscOptions    options;
38274663dae6SJed Brown     // Code from DMPlexExtrude
38284663dae6SJed Brown     PetscCall(DMPlexTransformCreate(PetscObjectComm((PetscObject)dm), &tr));
38294663dae6SJed Brown     PetscCall(DMPlexTransformSetDM(tr, dm));
38304663dae6SJed Brown     PetscCall(DMPlexTransformSetType(tr, DMPLEXEXTRUDE));
38314663dae6SJed Brown     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, &prefix));
38324663dae6SJed Brown     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)tr, prefix));
38334663dae6SJed Brown     PetscCall(PetscObjectGetOptions((PetscObject)dm, &options));
38344663dae6SJed Brown     PetscCall(PetscObjectSetOptions((PetscObject)tr, options));
38354663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetLayers(tr, layers));
38364663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetThickness(tr, thickness));
38374663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetTensor(tr, PETSC_FALSE));
38384663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetSymmetric(tr, PETSC_TRUE));
38394663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetNormalFunction(tr, normalFunc));
38404663dae6SJed Brown     PetscCall(DMPlexTransformSetFromOptions(tr));
38414663dae6SJed Brown     PetscCall(PetscObjectSetOptions((PetscObject)tr, NULL));
38424663dae6SJed Brown     PetscCall(DMPlexTransformSetUp(tr));
38434663dae6SJed Brown     PetscCall(PetscObjectViewFromOptions((PetscObject)tr, NULL, "-dm_plex_tps_transform_view"));
38444663dae6SJed Brown     PetscCall(DMPlexTransformApply(tr, dm, &edm));
38454663dae6SJed Brown     PetscCall(DMCopyDisc(dm, edm));
38464663dae6SJed Brown     PetscCall(DMGetCoordinateDM(dm, &cdm));
38474663dae6SJed Brown     PetscCall(DMGetCoordinateDM(edm, &ecdm));
38484663dae6SJed Brown     PetscCall(DMCopyDisc(cdm, ecdm));
38494663dae6SJed Brown     PetscCall(DMPlexTransformCreateDiscLabels(tr, edm));
38504663dae6SJed Brown     PetscCall(DMPlexTransformDestroy(&tr));
3851a77a5016SMatthew G. Knepley     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, edm));
385269d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &edm));
3853b7f5c055SJed Brown   }
38543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3855b7f5c055SJed Brown }
3856b7f5c055SJed Brown 
3857b7f5c055SJed Brown /*@
3858b7f5c055SJed Brown   DMPlexCreateTPSMesh - Create a distributed, interpolated mesh of a triply-periodic surface
3859b7f5c055SJed Brown 
3860b7f5c055SJed Brown   Collective
3861b7f5c055SJed Brown 
3862b7f5c055SJed Brown   Input Parameters:
3863a1cb98faSBarry Smith + comm           - The communicator for the `DM` object
3864b7f5c055SJed Brown . tpstype        - Type of triply-periodic surface
3865b7f5c055SJed Brown . extent         - Array of length 3 containing number of periods in each direction
386620f4b53cSBarry Smith . periodic       - array of length 3 with periodicity, or `NULL` for non-periodic
38671436d7faSJed Brown . tps_distribute - Distribute 2D manifold mesh prior to refinement and extrusion (more scalable)
3868817da375SSatish Balay . refinements    - Number of factor-of-2 refinements of 2D manifold mesh
38691436d7faSJed Brown . layers         - Number of cell layers extruded in normal direction
3870817da375SSatish Balay - thickness      - Thickness in normal direction
3871b7f5c055SJed Brown 
3872b7f5c055SJed Brown   Output Parameter:
3873a1cb98faSBarry Smith . dm - The `DM` object
3874a1cb98faSBarry Smith 
3875a1cb98faSBarry Smith   Level: beginner
3876b7f5c055SJed Brown 
3877b7f5c055SJed Brown   Notes:
387815229ffcSPierre Jolivet   This meshes the surface of the Schwarz P or Gyroid surfaces.  Schwarz P is the simplest member of the triply-periodic minimal surfaces.
38791d27aa22SBarry Smith   <https://en.wikipedia.org/wiki/Schwarz_minimal_surface#Schwarz_P_(%22Primitive%22)> and can be cut with "clean" boundaries.
38801d27aa22SBarry 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.
3881b7f5c055SJed Brown   Our implementation creates a very coarse mesh of the surface and refines (by 4-way splitting) as many times as requested.
3882b7f5c055SJed Brown   On each refinement, all vertices are projected to their nearest point on the surface.
3883b7f5c055SJed Brown   This projection could readily be extended to related surfaces.
3884b7f5c055SJed Brown 
38851d27aa22SBarry Smith   See {cite}`maskery2018insights`
38861d27aa22SBarry Smith 
38871d27aa22SBarry Smith   The face (edge) sets for the Schwarz P surface are numbered $1(-x), 2(+x), 3(-y), 4(+y), 5(-z), 6(+z)$.
38881d27aa22SBarry Smith   When the mesh is refined, "Face Sets" contain the new vertices (created during refinement).
38891d27aa22SBarry Smith   Use `DMPlexLabelComplete()` to propagate to coarse-level vertices.
3890b7f5c055SJed Brown 
389160225df5SJacob Faibussowitsch   Developer Notes:
3892b7f5c055SJed Brown   The Gyroid mesh does not currently mark boundary sets.
3893b7f5c055SJed Brown 
38941cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSphereMesh()`, `DMSetType()`, `DMCreate()`
3895b7f5c055SJed Brown @*/
3896d71ae5a4SJacob 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)
3897d71ae5a4SJacob Faibussowitsch {
3898b7f5c055SJed Brown   PetscFunctionBegin;
38999566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
39009566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
39019566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateTPSMesh_Internal(*dm, tpstype, extent, periodic, tps_distribute, refinements, layers, thickness));
39023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3903b7f5c055SJed Brown }
3904b7f5c055SJed Brown 
39059318fe57SMatthew G. Knepley /*@
39069318fe57SMatthew G. Knepley   DMPlexCreateSphereMesh - Creates a mesh on the d-dimensional sphere, S^d.
39079318fe57SMatthew G. Knepley 
39089318fe57SMatthew G. Knepley   Collective
39099318fe57SMatthew G. Knepley 
39109318fe57SMatthew G. Knepley   Input Parameters:
3911a1cb98faSBarry Smith + comm    - The communicator for the `DM` object
39129318fe57SMatthew G. Knepley . dim     - The dimension
39139318fe57SMatthew G. Knepley . simplex - Use simplices, or tensor product cells
39149318fe57SMatthew G. Knepley - R       - The radius
39159318fe57SMatthew G. Knepley 
39169318fe57SMatthew G. Knepley   Output Parameter:
3917a1cb98faSBarry Smith . dm - The `DM` object
39189318fe57SMatthew G. Knepley 
39199318fe57SMatthew G. Knepley   Level: beginner
39209318fe57SMatthew G. Knepley 
39211cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBallMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
39229318fe57SMatthew G. Knepley @*/
3923d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateSphereMesh(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscReal R, DM *dm)
3924d71ae5a4SJacob Faibussowitsch {
39259318fe57SMatthew G. Knepley   PetscFunctionBegin;
39264f572ea9SToby Isaac   PetscAssertPointer(dm, 5);
39279566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
39289566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
39299566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSphereMesh_Internal(*dm, dim, simplex, R));
39303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
39319318fe57SMatthew G. Knepley }
39329318fe57SMatthew G. Knepley 
3933d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBallMesh_Internal(DM dm, PetscInt dim, PetscReal R)
3934d71ae5a4SJacob Faibussowitsch {
39359318fe57SMatthew G. Knepley   DM          sdm, vol;
39369318fe57SMatthew G. Knepley   DMLabel     bdlabel;
3937dd2b43ebSStefano Zampini   const char *prefix;
39389318fe57SMatthew G. Knepley 
39399318fe57SMatthew G. Knepley   PetscFunctionBegin;
39409566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &sdm));
39419566063dSJacob Faibussowitsch   PetscCall(DMSetType(sdm, DMPLEX));
3942dd2b43ebSStefano Zampini   PetscCall(DMGetOptionsPrefix(dm, &prefix));
3943dd2b43ebSStefano Zampini   PetscCall(DMSetOptionsPrefix(sdm, prefix));
3944dd2b43ebSStefano Zampini   PetscCall(DMAppendOptionsPrefix(sdm, "bd_"));
3945dd2b43ebSStefano Zampini   PetscCall(DMPlexDistributeSetDefault(sdm, PETSC_FALSE));
39469566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSphereMesh_Internal(sdm, dim - 1, PETSC_TRUE, R));
39479566063dSJacob Faibussowitsch   PetscCall(DMSetFromOptions(sdm));
39489566063dSJacob Faibussowitsch   PetscCall(DMViewFromOptions(sdm, NULL, "-dm_view"));
39499566063dSJacob Faibussowitsch   PetscCall(DMPlexGenerate(sdm, NULL, PETSC_TRUE, &vol));
39509566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&sdm));
395169d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &vol));
39529566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
39539566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "marker", &bdlabel));
39549566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkBoundaryFaces(dm, PETSC_DETERMINE, bdlabel));
39559566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete(dm, bdlabel));
39563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
395751a74b61SMatthew G. Knepley }
395851a74b61SMatthew G. Knepley 
395951a74b61SMatthew G. Knepley /*@
396051a74b61SMatthew G. Knepley   DMPlexCreateBallMesh - Creates a simplex mesh on the d-dimensional ball, B^d.
396151a74b61SMatthew G. Knepley 
396251a74b61SMatthew G. Knepley   Collective
396351a74b61SMatthew G. Knepley 
396451a74b61SMatthew G. Knepley   Input Parameters:
3965a1cb98faSBarry Smith + comm - The communicator for the `DM` object
396651a74b61SMatthew G. Knepley . dim  - The dimension
396751a74b61SMatthew G. Knepley - R    - The radius
396851a74b61SMatthew G. Knepley 
396951a74b61SMatthew G. Knepley   Output Parameter:
3970a1cb98faSBarry Smith . dm - The `DM` object
397151a74b61SMatthew G. Knepley 
3972a1cb98faSBarry Smith   Options Database Key:
397360225df5SJacob Faibussowitsch . bd_dm_refine - This will refine the surface mesh preserving the sphere geometry
397451a74b61SMatthew G. Knepley 
397551a74b61SMatthew G. Knepley   Level: beginner
397651a74b61SMatthew G. Knepley 
39771cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSphereMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
397851a74b61SMatthew G. Knepley @*/
3979d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateBallMesh(MPI_Comm comm, PetscInt dim, PetscReal R, DM *dm)
3980d71ae5a4SJacob Faibussowitsch {
398151a74b61SMatthew G. Knepley   PetscFunctionBegin;
39829566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
39839566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
39849566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBallMesh_Internal(*dm, dim, R));
39853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
39862829fed8SMatthew G. Knepley }
39872829fed8SMatthew G. Knepley 
3988d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateReferenceCell_Internal(DM rdm, DMPolytopeType ct)
3989d71ae5a4SJacob Faibussowitsch {
39900a6ba040SMatthew G. Knepley   PetscFunctionBegin;
39919318fe57SMatthew G. Knepley   switch (ct) {
39929371c9d4SSatish Balay   case DM_POLYTOPE_POINT: {
39939318fe57SMatthew G. Knepley     PetscInt    numPoints[1]        = {1};
39949318fe57SMatthew G. Knepley     PetscInt    coneSize[1]         = {0};
39959318fe57SMatthew G. Knepley     PetscInt    cones[1]            = {0};
39969318fe57SMatthew G. Knepley     PetscInt    coneOrientations[1] = {0};
39979318fe57SMatthew G. Knepley     PetscScalar vertexCoords[1]     = {0.0};
39989318fe57SMatthew G. Knepley 
39999566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 0));
40009566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 0, numPoints, coneSize, cones, coneOrientations, vertexCoords));
40019371c9d4SSatish Balay   } break;
40029371c9d4SSatish Balay   case DM_POLYTOPE_SEGMENT: {
40039318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {2, 1};
40049318fe57SMatthew G. Knepley     PetscInt    coneSize[3]         = {2, 0, 0};
40059318fe57SMatthew G. Knepley     PetscInt    cones[2]            = {1, 2};
40069318fe57SMatthew G. Knepley     PetscInt    coneOrientations[2] = {0, 0};
40079318fe57SMatthew G. Knepley     PetscScalar vertexCoords[2]     = {-1.0, 1.0};
40089318fe57SMatthew G. Knepley 
40099566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 1));
40109566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
40119371c9d4SSatish Balay   } break;
40129371c9d4SSatish Balay   case DM_POLYTOPE_POINT_PRISM_TENSOR: {
4013b5a892a1SMatthew G. Knepley     PetscInt    numPoints[2]        = {2, 1};
4014b5a892a1SMatthew G. Knepley     PetscInt    coneSize[3]         = {2, 0, 0};
4015b5a892a1SMatthew G. Knepley     PetscInt    cones[2]            = {1, 2};
4016b5a892a1SMatthew G. Knepley     PetscInt    coneOrientations[2] = {0, 0};
4017b5a892a1SMatthew G. Knepley     PetscScalar vertexCoords[2]     = {-1.0, 1.0};
4018b5a892a1SMatthew G. Knepley 
40199566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 1));
40209566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
40219371c9d4SSatish Balay   } break;
40229371c9d4SSatish Balay   case DM_POLYTOPE_TRIANGLE: {
40239318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {3, 1};
40249318fe57SMatthew G. Knepley     PetscInt    coneSize[4]         = {3, 0, 0, 0};
40259318fe57SMatthew G. Knepley     PetscInt    cones[3]            = {1, 2, 3};
40269318fe57SMatthew G. Knepley     PetscInt    coneOrientations[3] = {0, 0, 0};
40279318fe57SMatthew G. Knepley     PetscScalar vertexCoords[6]     = {-1.0, -1.0, 1.0, -1.0, -1.0, 1.0};
40289318fe57SMatthew G. Knepley 
40299566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 2));
40309566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
40319371c9d4SSatish Balay   } break;
40329371c9d4SSatish Balay   case DM_POLYTOPE_QUADRILATERAL: {
40339318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {4, 1};
40349318fe57SMatthew G. Knepley     PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
40359318fe57SMatthew G. Knepley     PetscInt    cones[4]            = {1, 2, 3, 4};
40369318fe57SMatthew G. Knepley     PetscInt    coneOrientations[4] = {0, 0, 0, 0};
40379318fe57SMatthew G. Knepley     PetscScalar vertexCoords[8]     = {-1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0};
40389318fe57SMatthew G. Knepley 
40399566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 2));
40409566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
40419371c9d4SSatish Balay   } break;
40429371c9d4SSatish Balay   case DM_POLYTOPE_SEG_PRISM_TENSOR: {
40439318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {4, 1};
40449318fe57SMatthew G. Knepley     PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
40459318fe57SMatthew G. Knepley     PetscInt    cones[4]            = {1, 2, 3, 4};
40469318fe57SMatthew G. Knepley     PetscInt    coneOrientations[4] = {0, 0, 0, 0};
40479318fe57SMatthew G. Knepley     PetscScalar vertexCoords[8]     = {-1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0};
40489318fe57SMatthew G. Knepley 
40499566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 2));
40509566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
40519371c9d4SSatish Balay   } break;
40529371c9d4SSatish Balay   case DM_POLYTOPE_TETRAHEDRON: {
40539318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {4, 1};
40549318fe57SMatthew G. Knepley     PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
4055f0edb160SMatthew G. Knepley     PetscInt    cones[4]            = {1, 2, 3, 4};
40569318fe57SMatthew G. Knepley     PetscInt    coneOrientations[4] = {0, 0, 0, 0};
4057f0edb160SMatthew 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};
40589318fe57SMatthew G. Knepley 
40599566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
40609566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
40619371c9d4SSatish Balay   } break;
40629371c9d4SSatish Balay   case DM_POLYTOPE_HEXAHEDRON: {
40639318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {8, 1};
40649318fe57SMatthew G. Knepley     PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
4065f0edb160SMatthew G. Knepley     PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
40669318fe57SMatthew G. Knepley     PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
40679371c9d4SSatish 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};
40689318fe57SMatthew G. Knepley 
40699566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
40709566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
40719371c9d4SSatish Balay   } break;
40729371c9d4SSatish Balay   case DM_POLYTOPE_TRI_PRISM: {
40739318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {6, 1};
40749318fe57SMatthew G. Knepley     PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
4075f0edb160SMatthew G. Knepley     PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
40769318fe57SMatthew G. Knepley     PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
40779371c9d4SSatish 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};
40789318fe57SMatthew G. Knepley 
40799566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
40809566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
40819371c9d4SSatish Balay   } break;
40829371c9d4SSatish Balay   case DM_POLYTOPE_TRI_PRISM_TENSOR: {
40839318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {6, 1};
40849318fe57SMatthew G. Knepley     PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
40859318fe57SMatthew G. Knepley     PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
40869318fe57SMatthew G. Knepley     PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
40879371c9d4SSatish 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};
40889318fe57SMatthew G. Knepley 
40899566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
40909566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
40919371c9d4SSatish Balay   } break;
40929371c9d4SSatish Balay   case DM_POLYTOPE_QUAD_PRISM_TENSOR: {
40939318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {8, 1};
40949318fe57SMatthew G. Knepley     PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
40959318fe57SMatthew G. Knepley     PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
40969318fe57SMatthew G. Knepley     PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
40979371c9d4SSatish 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};
40989318fe57SMatthew G. Knepley 
40999566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
41009566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
41019371c9d4SSatish Balay   } break;
41029371c9d4SSatish Balay   case DM_POLYTOPE_PYRAMID: {
41039318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {5, 1};
41049318fe57SMatthew G. Knepley     PetscInt    coneSize[6]         = {5, 0, 0, 0, 0, 0};
4105f0edb160SMatthew G. Knepley     PetscInt    cones[5]            = {1, 2, 3, 4, 5};
41069318fe57SMatthew G. Knepley     PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
41079371c9d4SSatish 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};
41089318fe57SMatthew G. Knepley 
41099566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
41109566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
41119371c9d4SSatish Balay   } break;
4112d71ae5a4SJacob Faibussowitsch   default:
4113d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)rdm), PETSC_ERR_ARG_WRONG, "Cannot create reference cell for cell type %s", DMPolytopeTypes[ct]);
41149318fe57SMatthew G. Knepley   }
41159318fe57SMatthew G. Knepley   {
41169318fe57SMatthew G. Knepley     PetscInt Nv, v;
41179318fe57SMatthew G. Knepley 
41189318fe57SMatthew G. Knepley     /* Must create the celltype label here so that we do not automatically try to compute the types */
41199566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(rdm, "celltype"));
41209566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCellType(rdm, 0, ct));
41219566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(rdm, NULL, &Nv));
41229566063dSJacob Faibussowitsch     for (v = 1; v < Nv; ++v) PetscCall(DMPlexSetCellType(rdm, v, DM_POLYTOPE_POINT));
41239318fe57SMatthew G. Knepley   }
41249566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolateInPlace_Internal(rdm));
41259566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)rdm, DMPolytopeTypes[ct]));
41263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
41270a6ba040SMatthew G. Knepley }
41280a6ba040SMatthew G. Knepley 
41299318fe57SMatthew G. Knepley /*@
4130a1cb98faSBarry Smith   DMPlexCreateReferenceCell - Create a `DMPLEX` with the appropriate FEM reference cell
41319318fe57SMatthew G. Knepley 
41329318fe57SMatthew G. Knepley   Collective
41339318fe57SMatthew G. Knepley 
41349318fe57SMatthew G. Knepley   Input Parameters:
41359318fe57SMatthew G. Knepley + comm - The communicator
41369318fe57SMatthew G. Knepley - ct   - The cell type of the reference cell
41379318fe57SMatthew G. Knepley 
41389318fe57SMatthew G. Knepley   Output Parameter:
41399318fe57SMatthew G. Knepley . refdm - The reference cell
41409318fe57SMatthew G. Knepley 
41419318fe57SMatthew G. Knepley   Level: intermediate
41429318fe57SMatthew G. Knepley 
414342747ad1SJacob Faibussowitsch .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBoxMesh()`
41449318fe57SMatthew G. Knepley @*/
4145d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateReferenceCell(MPI_Comm comm, DMPolytopeType ct, DM *refdm)
4146d71ae5a4SJacob Faibussowitsch {
41470a6ba040SMatthew G. Knepley   PetscFunctionBegin;
41489566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, refdm));
41499566063dSJacob Faibussowitsch   PetscCall(DMSetType(*refdm, DMPLEX));
41509566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateReferenceCell_Internal(*refdm, ct));
41513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
41529318fe57SMatthew G. Knepley }
415379a015ccSMatthew G. Knepley 
4154d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoundaryLabel_Private(DM dm, const char name[])
4155d71ae5a4SJacob Faibussowitsch {
41569318fe57SMatthew G. Knepley   DM        plex;
41579318fe57SMatthew G. Knepley   DMLabel   label;
41589318fe57SMatthew G. Knepley   PetscBool hasLabel;
41590a6ba040SMatthew G. Knepley 
4160c22d3578SMatthew G. Knepley   PetscFunctionBegin;
41619566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &hasLabel));
41623ba16761SJacob Faibussowitsch   if (hasLabel) PetscFunctionReturn(PETSC_SUCCESS);
41639566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, name));
41649566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
41659566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
41669566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkBoundaryFaces(plex, 1, label));
41671c8afea9SMatthew G. Knepley   PetscCall(DMPlexLabelComplete(plex, label));
41689566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plex));
41693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
41709318fe57SMatthew G. Knepley }
4171acdc6f61SToby Isaac 
4172669647acSMatthew G. Knepley /*
4173669647acSMatthew 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.
4174669647acSMatthew G. Knepley 
4175669647acSMatthew G. Knepley     (x, y) -> (r, theta) = (x[1], (x[0] - lower[0]) * 2\pi/(upper[0] - lower[0]))
4176669647acSMatthew G. Knepley */
4177d71ae5a4SJacob 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[])
4178d71ae5a4SJacob Faibussowitsch {
4179669647acSMatthew G. Knepley   const PetscReal low = PetscRealPart(constants[0]);
4180669647acSMatthew G. Knepley   const PetscReal upp = PetscRealPart(constants[1]);
4181669647acSMatthew G. Knepley   const PetscReal r   = PetscRealPart(u[1]);
4182669647acSMatthew G. Knepley   const PetscReal th  = 2. * PETSC_PI * (PetscRealPart(u[0]) - low) / (upp - low);
4183669647acSMatthew G. Knepley 
4184669647acSMatthew G. Knepley   f0[0] = r * PetscCosReal(th);
4185669647acSMatthew G. Knepley   f0[1] = r * PetscSinReal(th);
4186669647acSMatthew G. Knepley }
4187669647acSMatthew G. Knepley 
41885390be7dSMatthew G. Knepley // Insert vertices and their joins, marked by depth
41895390be7dSMatthew G. Knepley static PetscErrorCode ProcessCohesiveLabel_Vertices(DM dm, DMLabel label, DMLabel vlabel, PetscInt val, PetscInt n, const PetscInt vertices[])
41905390be7dSMatthew G. Knepley {
41915390be7dSMatthew G. Knepley   PetscFunctionBegin;
41925390be7dSMatthew G. Knepley   PetscCall(DMPlexMarkSubmesh_Interpolated(dm, vlabel, val, PETSC_FALSE, PETSC_FALSE, label, NULL));
41935390be7dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
41945390be7dSMatthew G. Knepley }
41955390be7dSMatthew G. Knepley 
41965390be7dSMatthew G. Knepley // Insert faces and their closures, marked by depth
41975390be7dSMatthew G. Knepley static PetscErrorCode ProcessCohesiveLabel_Faces(DM dm, DMLabel label, PetscInt n, const PetscInt faces[])
41985390be7dSMatthew G. Knepley {
41995390be7dSMatthew G. Knepley   PetscFunctionBegin;
42005390be7dSMatthew G. Knepley   for (PetscInt p = 0; p < n; ++p) {
42015390be7dSMatthew G. Knepley     const PetscInt point   = faces[p];
42025390be7dSMatthew G. Knepley     PetscInt      *closure = NULL;
42035390be7dSMatthew G. Knepley     PetscInt       clSize, pdepth;
42045390be7dSMatthew G. Knepley 
42055390be7dSMatthew G. Knepley     PetscCall(DMPlexGetPointDepth(dm, point, &pdepth));
42065390be7dSMatthew G. Knepley     PetscCall(DMLabelSetValue(label, point, pdepth));
42075390be7dSMatthew G. Knepley     PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &clSize, &closure));
42085390be7dSMatthew G. Knepley     for (PetscInt cl = 0; cl < clSize * 2; cl += 2) {
42095390be7dSMatthew G. Knepley       PetscCall(DMPlexGetPointDepth(dm, closure[cl], &pdepth));
42105390be7dSMatthew G. Knepley       PetscCall(DMLabelSetValue(label, closure[cl], pdepth));
42115390be7dSMatthew G. Knepley     }
42125390be7dSMatthew G. Knepley     PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &clSize, &closure));
42135390be7dSMatthew G. Knepley   }
42145390be7dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
42155390be7dSMatthew G. Knepley }
42165390be7dSMatthew G. Knepley 
42174e22dd4cSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscOptionsFindPairPrefix_Private(PetscOptions, const char pre[], const char name[], const char *option[], const char *value[], PetscBool *flg);
42184e22dd4cSMatthew G. Knepley 
42195dca41c3SJed Brown const char *const DMPlexShapes[] = {"box", "box_surface", "ball", "sphere", "cylinder", "schwarz_p", "gyroid", "doublet", "annulus", "hypercubic", "zbox", "unknown", "DMPlexShape", "DM_SHAPE_", NULL};
42209318fe57SMatthew G. Knepley 
4221d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateFromOptions_Internal(PetscOptionItems *PetscOptionsObject, PetscBool *useCoordSpace, DM dm)
4222d71ae5a4SJacob Faibussowitsch {
42239318fe57SMatthew G. Knepley   DMPlexShape    shape   = DM_SHAPE_BOX;
42249318fe57SMatthew G. Knepley   DMPolytopeType cell    = DM_POLYTOPE_TRIANGLE;
42259318fe57SMatthew G. Knepley   PetscInt       dim     = 2;
4226b9da1bb3SMatthew G. Knepley   PetscBool      simplex = PETSC_TRUE, interpolate = PETSC_TRUE, orient = PETSC_FALSE, adjCone = PETSC_FALSE, adjClosure = PETSC_TRUE, refDomain = PETSC_FALSE;
4227d0812dedSMatthew G. Knepley   PetscBool      flg, flg2, fflg, strflg, bdfflg, nameflg;
42289318fe57SMatthew G. Knepley   MPI_Comm       comm;
4229ed5e4e85SVaclav Hapla   char           filename[PETSC_MAX_PATH_LEN]   = "<unspecified>";
4230ed5e4e85SVaclav Hapla   char           bdFilename[PETSC_MAX_PATH_LEN] = "<unspecified>";
4231ed5e4e85SVaclav Hapla   char           plexname[PETSC_MAX_PATH_LEN]   = "";
42324e22dd4cSMatthew G. Knepley   const char    *option;
42339318fe57SMatthew G. Knepley 
42349318fe57SMatthew G. Knepley   PetscFunctionBegin;
4235708be2fdSJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_CreateFromOptions, dm, 0, 0, 0));
42369566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
42379318fe57SMatthew G. Knepley   /* TODO Turn this into a registration interface */
42389566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_filename", "File containing a mesh", "DMPlexCreateFromFile", filename, filename, sizeof(filename), &fflg));
4239d0812dedSMatthew G. Knepley   PetscCall(PetscOptionsString("-dm_plex_file_contents", "Contents of a file format in a string", "DMPlexCreateFromFile", filename, filename, sizeof(filename), &strflg));
42409566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_boundary_filename", "File containing a mesh boundary", "DMPlexCreateFromFile", bdFilename, bdFilename, sizeof(bdFilename), &bdfflg));
42419566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_name", "Name of the mesh in the file", "DMPlexCreateFromFile", plexname, plexname, sizeof(plexname), &nameflg));
42429566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_plex_cell", "Cell shape", "", DMPolytopeTypes, (PetscEnum)cell, (PetscEnum *)&cell, NULL));
42439566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_reference_cell_domain", "Use a reference cell domain", "", refDomain, &refDomain, NULL));
42449566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_plex_shape", "Shape for built-in mesh", "", DMPlexShapes, (PetscEnum)shape, (PetscEnum *)&shape, &flg));
42459566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_dim", "Topological dimension of the mesh", "DMGetDimension", dim, &dim, &flg, 0));
42469566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_simplex", "Mesh cell shape", "", simplex, &simplex, &flg));
42479566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_interpolate", "Flag to create edges and faces automatically", "", interpolate, &interpolate, &flg));
4248b9da1bb3SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_plex_orient", "Orient the constructed mesh", "DMPlexOrient", orient, &orient, &flg));
42499566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_adj_cone", "Set adjacency direction", "DMSetBasicAdjacency", adjCone, &adjCone, &flg));
42509566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_adj_closure", "Set adjacency size", "DMSetBasicAdjacency", adjClosure, &adjClosure, &flg2));
42519566063dSJacob Faibussowitsch   if (flg || flg2) PetscCall(DMSetBasicAdjacency(dm, adjCone, adjClosure));
42529318fe57SMatthew G. Knepley 
425361a622f3SMatthew G. Knepley   switch (cell) {
425461a622f3SMatthew G. Knepley   case DM_POLYTOPE_POINT:
425561a622f3SMatthew G. Knepley   case DM_POLYTOPE_SEGMENT:
425661a622f3SMatthew G. Knepley   case DM_POLYTOPE_POINT_PRISM_TENSOR:
425761a622f3SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
425861a622f3SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
425961a622f3SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
4260d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_HEXAHEDRON:
4261d71ae5a4SJacob Faibussowitsch     *useCoordSpace = PETSC_TRUE;
4262d71ae5a4SJacob Faibussowitsch     break;
4263d71ae5a4SJacob Faibussowitsch   default:
4264d71ae5a4SJacob Faibussowitsch     *useCoordSpace = PETSC_FALSE;
4265d71ae5a4SJacob Faibussowitsch     break;
426661a622f3SMatthew G. Knepley   }
426761a622f3SMatthew G. Knepley 
42689318fe57SMatthew G. Knepley   if (fflg) {
42699318fe57SMatthew G. Knepley     DM          dmnew;
42701e4a82c4SMatthew G. Knepley     const char *name;
42719318fe57SMatthew G. Knepley 
42721e4a82c4SMatthew G. Knepley     PetscCall(PetscObjectGetName((PetscObject)dm, &name));
42731e4a82c4SMatthew G. Knepley     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), filename, nameflg ? plexname : name, interpolate, &dmnew));
42745de52c6dSVaclav Hapla     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
427569d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &dmnew));
42769318fe57SMatthew G. Knepley   } else if (refDomain) {
42779566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateReferenceCell_Internal(dm, cell));
42789318fe57SMatthew G. Knepley   } else if (bdfflg) {
42799318fe57SMatthew G. Knepley     DM          bdm, dmnew;
42801e4a82c4SMatthew G. Knepley     const char *name;
42819318fe57SMatthew G. Knepley 
42821e4a82c4SMatthew G. Knepley     PetscCall(PetscObjectGetName((PetscObject)dm, &name));
42831e4a82c4SMatthew G. Knepley     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), bdFilename, nameflg ? plexname : name, interpolate, &bdm));
42849566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)bdm, "bd_"));
42859566063dSJacob Faibussowitsch     PetscCall(DMSetFromOptions(bdm));
42869566063dSJacob Faibussowitsch     PetscCall(DMPlexGenerate(bdm, NULL, interpolate, &dmnew));
42879566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&bdm));
42885de52c6dSVaclav Hapla     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
428969d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &dmnew));
4290d0812dedSMatthew G. Knepley   } else if (strflg) {
4291d0812dedSMatthew G. Knepley     DM          dmnew;
4292d0812dedSMatthew G. Knepley     PetscViewer viewer;
4293d0812dedSMatthew G. Knepley     const char *contents;
4294d0812dedSMatthew G. Knepley     char       *strname;
4295d0812dedSMatthew G. Knepley     char        tmpdir[PETSC_MAX_PATH_LEN];
4296d0812dedSMatthew G. Knepley     char        tmpfilename[PETSC_MAX_PATH_LEN];
4297d0812dedSMatthew G. Knepley     char        name[PETSC_MAX_PATH_LEN];
4298d0812dedSMatthew G. Knepley     MPI_Comm    comm;
4299d0812dedSMatthew G. Knepley     PetscMPIInt rank;
4300d0812dedSMatthew G. Knepley 
4301d0812dedSMatthew G. Knepley     PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4302d0812dedSMatthew G. Knepley     PetscCallMPI(MPI_Comm_rank(comm, &rank));
4303d0812dedSMatthew G. Knepley     PetscCall(PetscStrchr(filename, ':', &strname));
4304d0812dedSMatthew G. Knepley     PetscCheck(strname, comm, PETSC_ERR_ARG_WRONG, "File contents must have the form \"ext:string_name\", not %s", filename);
4305d0812dedSMatthew G. Knepley     strname[0] = '\0';
4306d0812dedSMatthew G. Knepley     ++strname;
4307d0812dedSMatthew G. Knepley     PetscCall(PetscDLSym(NULL, strname, (void **)&contents));
4308d0812dedSMatthew G. Knepley     PetscCheck(contents, comm, PETSC_ERR_ARG_WRONG, "Could not locate mesh string %s", strname);
4309d0812dedSMatthew G. Knepley     PetscCall(PetscGetTmp(comm, tmpdir, PETSC_MAX_PATH_LEN));
4310ed32af8cSMatthew G. Knepley     PetscCall(PetscStrlcat(tmpdir, "/meshXXXXXX", PETSC_MAX_PATH_LEN));
4311ed32af8cSMatthew G. Knepley     PetscCall(PetscMkdtemp(tmpdir));
4312ed32af8cSMatthew G. Knepley     PetscCall(PetscSNPrintf(tmpfilename, PETSC_MAX_PATH_LEN, "%s/mesh.%s", tmpdir, filename));
4313d0812dedSMatthew G. Knepley     PetscCall(PetscViewerASCIIOpen(comm, tmpfilename, &viewer));
4314d0812dedSMatthew G. Knepley     PetscCall(PetscViewerASCIIPrintf(viewer, "%s\n", contents));
4315d0812dedSMatthew G. Knepley     PetscCall(PetscViewerDestroy(&viewer));
4316d0812dedSMatthew G. Knepley     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), tmpfilename, plexname, interpolate, &dmnew));
4317ed32af8cSMatthew G. Knepley     PetscCall(PetscRMTree(tmpdir));
4318d0812dedSMatthew G. Knepley     PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN, "%s Mesh", strname));
4319d0812dedSMatthew G. Knepley     PetscCall(PetscObjectSetName((PetscObject)dm, name));
4320d0812dedSMatthew G. Knepley     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
4321d0812dedSMatthew G. Knepley     PetscCall(DMPlexReplace_Internal(dm, &dmnew));
43229318fe57SMatthew G. Knepley   } else {
43239566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)dm, DMPlexShapes[shape]));
43249318fe57SMatthew G. Knepley     switch (shape) {
4325669647acSMatthew G. Knepley     case DM_SHAPE_BOX:
43265dca41c3SJed Brown     case DM_SHAPE_ZBOX:
4327669647acSMatthew G. Knepley     case DM_SHAPE_ANNULUS: {
43289318fe57SMatthew G. Knepley       PetscInt       faces[3]  = {0, 0, 0};
43299318fe57SMatthew G. Knepley       PetscReal      lower[3]  = {0, 0, 0};
43309318fe57SMatthew G. Knepley       PetscReal      upper[3]  = {1, 1, 1};
43319318fe57SMatthew G. Knepley       DMBoundaryType bdt[3]    = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
4332669647acSMatthew G. Knepley       PetscBool      isAnnular = shape == DM_SHAPE_ANNULUS ? PETSC_TRUE : PETSC_FALSE;
43339318fe57SMatthew G. Knepley       PetscInt       i, n;
43349318fe57SMatthew G. Knepley 
43359318fe57SMatthew G. Knepley       n = dim;
43369318fe57SMatthew G. Knepley       for (i = 0; i < dim; ++i) faces[i] = (dim == 1 ? 1 : 4 - dim);
43379566063dSJacob Faibussowitsch       PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg));
43389318fe57SMatthew G. Knepley       n = 3;
43399566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
434063a3b9bcSJacob Faibussowitsch       PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Lower box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
43419318fe57SMatthew G. Knepley       n = 3;
43429566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
434363a3b9bcSJacob Faibussowitsch       PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Upper box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
43449318fe57SMatthew G. Knepley       n = 3;
43459566063dSJacob Faibussowitsch       PetscCall(PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *)bdt, &n, &flg));
434663a3b9bcSJacob Faibussowitsch       PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Box boundary types had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
4347669647acSMatthew G. Knepley 
4348669647acSMatthew G. Knepley       PetscCheck(!isAnnular || dim == 2, comm, PETSC_ERR_ARG_OUTOFRANGE, "Only two dimensional annuli have been implemented");
4349669647acSMatthew G. Knepley       if (isAnnular)
4350669647acSMatthew G. Knepley         for (i = 0; i < dim - 1; ++i) bdt[i] = DM_BOUNDARY_PERIODIC;
4351669647acSMatthew G. Knepley 
43529318fe57SMatthew G. Knepley       switch (cell) {
435361a622f3SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
43549566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateWedgeBoxMesh_Internal(dm, faces, lower, upper, bdt));
4355d410b0cfSMatthew G. Knepley         if (!interpolate) {
4356d410b0cfSMatthew G. Knepley           DM udm;
4357d410b0cfSMatthew G. Knepley 
43589566063dSJacob Faibussowitsch           PetscCall(DMPlexUninterpolate(dm, &udm));
435969d8a87bSksagiyam           PetscCall(DMPlexReplace_Internal(dm, &udm));
4360d410b0cfSMatthew G. Knepley         }
43619318fe57SMatthew G. Knepley         break;
4362d71ae5a4SJacob Faibussowitsch       default:
43635dca41c3SJed Brown         PetscCall(DMPlexCreateBoxMesh_Internal(dm, shape, dim, simplex, faces, lower, upper, bdt, interpolate));
4364d71ae5a4SJacob Faibussowitsch         break;
43659318fe57SMatthew G. Knepley       }
4366669647acSMatthew G. Knepley       if (isAnnular) {
4367669647acSMatthew G. Knepley         DM          cdm;
4368669647acSMatthew G. Knepley         PetscDS     cds;
4369669647acSMatthew G. Knepley         PetscScalar bounds[2] = {lower[0], upper[0]};
4370669647acSMatthew G. Knepley 
4371669647acSMatthew G. Knepley         // Fix coordinates for annular region
4372669647acSMatthew G. Knepley         PetscCall(DMSetPeriodicity(dm, NULL, NULL, NULL));
4373669647acSMatthew G. Knepley         PetscCall(DMSetCellCoordinatesLocal(dm, NULL));
4374669647acSMatthew G. Knepley         PetscCall(DMSetCellCoordinates(dm, NULL));
4375e44f6aebSMatthew G. Knepley         PetscCall(DMPlexCreateCoordinateSpace(dm, 1, PETSC_TRUE, NULL));
4376669647acSMatthew G. Knepley         PetscCall(DMGetCoordinateDM(dm, &cdm));
4377669647acSMatthew G. Knepley         PetscCall(DMGetDS(cdm, &cds));
4378669647acSMatthew G. Knepley         PetscCall(PetscDSSetConstants(cds, 2, bounds));
4379669647acSMatthew G. Knepley         PetscCall(DMPlexRemapGeometry(dm, 0.0, boxToAnnulus));
4380669647acSMatthew G. Knepley       }
43819371c9d4SSatish Balay     } break;
43829371c9d4SSatish Balay     case DM_SHAPE_BOX_SURFACE: {
43839318fe57SMatthew G. Knepley       PetscInt  faces[3] = {0, 0, 0};
43849318fe57SMatthew G. Knepley       PetscReal lower[3] = {0, 0, 0};
43859318fe57SMatthew G. Knepley       PetscReal upper[3] = {1, 1, 1};
43869318fe57SMatthew G. Knepley       PetscInt  i, n;
43879318fe57SMatthew G. Knepley 
43889318fe57SMatthew G. Knepley       n = dim + 1;
43899318fe57SMatthew G. Knepley       for (i = 0; i < dim + 1; ++i) faces[i] = (dim + 1 == 1 ? 1 : 4 - (dim + 1));
43909566063dSJacob Faibussowitsch       PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg));
43919318fe57SMatthew G. Knepley       n = 3;
43929566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
439363a3b9bcSJacob 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);
43949318fe57SMatthew G. Knepley       n = 3;
43959566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
439663a3b9bcSJacob 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);
43979566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(dm, dim + 1, faces, lower, upper, interpolate));
43989371c9d4SSatish Balay     } break;
43999371c9d4SSatish Balay     case DM_SHAPE_SPHERE: {
44009318fe57SMatthew G. Knepley       PetscReal R = 1.0;
44019318fe57SMatthew G. Knepley 
44029566063dSJacob Faibussowitsch       PetscCall(PetscOptionsReal("-dm_plex_sphere_radius", "Radius of the sphere", "", R, &R, &flg));
44039566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateSphereMesh_Internal(dm, dim, simplex, R));
44049371c9d4SSatish Balay     } break;
44059371c9d4SSatish Balay     case DM_SHAPE_BALL: {
44069318fe57SMatthew G. Knepley       PetscReal R = 1.0;
44079318fe57SMatthew G. Knepley 
44089566063dSJacob Faibussowitsch       PetscCall(PetscOptionsReal("-dm_plex_ball_radius", "Radius of the ball", "", R, &R, &flg));
44099566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateBallMesh_Internal(dm, dim, R));
44109371c9d4SSatish Balay     } break;
44119371c9d4SSatish Balay     case DM_SHAPE_CYLINDER: {
44129318fe57SMatthew G. Knepley       DMBoundaryType bdt = DM_BOUNDARY_NONE;
44139318fe57SMatthew G. Knepley       PetscInt       Nw  = 6;
441449704ca5SMatthew G. Knepley       PetscInt       Nr  = 0;
44159318fe57SMatthew G. Knepley 
44169566063dSJacob Faibussowitsch       PetscCall(PetscOptionsEnum("-dm_plex_cylinder_bd", "Boundary type in the z direction", "", DMBoundaryTypes, (PetscEnum)bdt, (PetscEnum *)&bdt, NULL));
44179566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-dm_plex_cylinder_num_wedges", "Number of wedges around the cylinder", "", Nw, &Nw, NULL));
441849704ca5SMatthew G. Knepley       PetscCall(PetscOptionsInt("-dm_plex_cylinder_num_refine", "Number of refinements before projection", "", Nr, &Nr, NULL));
44199318fe57SMatthew G. Knepley       switch (cell) {
4420d71ae5a4SJacob Faibussowitsch       case DM_POLYTOPE_TRI_PRISM_TENSOR:
4421d71ae5a4SJacob Faibussowitsch         PetscCall(DMPlexCreateWedgeCylinderMesh_Internal(dm, Nw, interpolate));
4422d71ae5a4SJacob Faibussowitsch         break;
4423d71ae5a4SJacob Faibussowitsch       default:
442449704ca5SMatthew G. Knepley         PetscCall(DMPlexCreateHexCylinderMesh_Internal(dm, bdt, Nr));
4425d71ae5a4SJacob Faibussowitsch         break;
44269318fe57SMatthew G. Knepley       }
44279371c9d4SSatish Balay     } break;
4428b7f5c055SJed Brown     case DM_SHAPE_SCHWARZ_P: // fallthrough
44299371c9d4SSatish Balay     case DM_SHAPE_GYROID: {
4430b7f5c055SJed Brown       PetscInt       extent[3] = {1, 1, 1}, refine = 0, layers = 0, three;
4431b7f5c055SJed Brown       PetscReal      thickness   = 0.;
4432b7f5c055SJed Brown       DMBoundaryType periodic[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
4433b7f5c055SJed Brown       DMPlexTPSType  tps_type    = shape == DM_SHAPE_SCHWARZ_P ? DMPLEX_TPS_SCHWARZ_P : DMPLEX_TPS_GYROID;
44341436d7faSJed Brown       PetscBool      tps_distribute;
44359566063dSJacob Faibussowitsch       PetscCall(PetscOptionsIntArray("-dm_plex_tps_extent", "Number of replicas for each of three dimensions", NULL, extent, (three = 3, &three), NULL));
44369566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-dm_plex_tps_refine", "Number of refinements", NULL, refine, &refine, NULL));
44379566063dSJacob Faibussowitsch       PetscCall(PetscOptionsEnumArray("-dm_plex_tps_periodic", "Periodicity in each of three dimensions", NULL, DMBoundaryTypes, (PetscEnum *)periodic, (three = 3, &three), NULL));
44389566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-dm_plex_tps_layers", "Number of layers in volumetric extrusion (or zero to not extrude)", NULL, layers, &layers, NULL));
44399566063dSJacob Faibussowitsch       PetscCall(PetscOptionsReal("-dm_plex_tps_thickness", "Thickness of volumetric extrusion", NULL, thickness, &thickness, NULL));
44409566063dSJacob Faibussowitsch       PetscCall(DMPlexDistributeGetDefault(dm, &tps_distribute));
44419566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_tps_distribute", "Distribute the 2D mesh prior to refinement and extrusion", NULL, tps_distribute, &tps_distribute, NULL));
44429566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateTPSMesh_Internal(dm, tps_type, extent, periodic, tps_distribute, refine, layers, thickness));
44439371c9d4SSatish Balay     } break;
44449371c9d4SSatish Balay     case DM_SHAPE_DOUBLET: {
444505bd46c0SStefano Zampini       DM        dmnew;
444605bd46c0SStefano Zampini       PetscReal rl = 0.0;
444705bd46c0SStefano Zampini 
444805bd46c0SStefano Zampini       PetscCall(PetscOptionsReal("-dm_plex_doublet_refinementlimit", "Refinement limit", NULL, rl, &rl, NULL));
444905bd46c0SStefano Zampini       PetscCall(DMPlexCreateDoublet(PetscObjectComm((PetscObject)dm), dim, simplex, interpolate, rl, &dmnew));
44505de52c6dSVaclav Hapla       PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
445169d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &dmnew));
44529371c9d4SSatish Balay     } break;
4453cfb853baSMatthew G. Knepley     case DM_SHAPE_HYPERCUBIC: {
4454cfb853baSMatthew G. Knepley       PetscInt       *edges;
4455cfb853baSMatthew G. Knepley       PetscReal      *lower, *upper;
4456cfb853baSMatthew G. Knepley       DMBoundaryType *bdt;
4457cfb853baSMatthew G. Knepley       PetscInt        n, d;
4458cfb853baSMatthew G. Knepley 
4459cfb853baSMatthew G. Knepley       *useCoordSpace = PETSC_FALSE;
4460cfb853baSMatthew G. Knepley       PetscCall(PetscMalloc4(dim, &edges, dim, &lower, dim, &upper, dim, &bdt));
4461cfb853baSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
4462cfb853baSMatthew G. Knepley         edges[d] = 1;
4463cfb853baSMatthew G. Knepley         lower[d] = 0.;
4464cfb853baSMatthew G. Knepley         upper[d] = 1.;
4465cfb853baSMatthew G. Knepley         bdt[d]   = DM_BOUNDARY_PERIODIC;
4466cfb853baSMatthew G. Knepley       }
4467cfb853baSMatthew G. Knepley       n = dim;
4468cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", edges, &n, &flg));
4469cfb853baSMatthew G. Knepley       n = dim;
4470cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
4471cfb853baSMatthew 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);
4472cfb853baSMatthew G. Knepley       n = dim;
4473cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
4474cfb853baSMatthew 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);
4475cfb853baSMatthew G. Knepley       n = dim;
4476cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *)bdt, &n, &flg));
4477cfb853baSMatthew 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);
4478cfb853baSMatthew G. Knepley       PetscCall(DMPlexCreateHypercubicMesh_Internal(dm, dim, lower, upper, edges, bdt));
4479cfb853baSMatthew G. Knepley       PetscCall(PetscFree4(edges, lower, upper, bdt));
4480cfb853baSMatthew G. Knepley     } break;
4481d71ae5a4SJacob Faibussowitsch     default:
4482d71ae5a4SJacob Faibussowitsch       SETERRQ(comm, PETSC_ERR_SUP, "Domain shape %s is unsupported", DMPlexShapes[shape]);
44839318fe57SMatthew G. Knepley     }
44849318fe57SMatthew G. Knepley   }
44859566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
448648a46eb9SPierre Jolivet   if (!((PetscObject)dm)->name && nameflg) PetscCall(PetscObjectSetName((PetscObject)dm, plexname));
4487b9da1bb3SMatthew G. Knepley   if (orient) PetscCall(DMPlexOrient(dm));
44884e22dd4cSMatthew G. Knepley   // Allow label creation
44894e22dd4cSMatthew G. Knepley   PetscCall(PetscOptionsFindPairPrefix_Private(NULL, ((PetscObject)dm)->prefix, "-dm_plex_label_", &option, NULL, &flg));
44904e22dd4cSMatthew G. Knepley   if (flg) {
44914e22dd4cSMatthew G. Knepley     DMLabel     label;
44924e22dd4cSMatthew G. Knepley     PetscInt    points[1024], n = 1024;
44934e22dd4cSMatthew G. Knepley     char        fulloption[PETSC_MAX_PATH_LEN];
44944e22dd4cSMatthew G. Knepley     const char *name = &option[14];
44954e22dd4cSMatthew G. Knepley 
44964e22dd4cSMatthew G. Knepley     PetscCall(DMCreateLabel(dm, name));
44974e22dd4cSMatthew G. Knepley     PetscCall(DMGetLabel(dm, name, &label));
44984e22dd4cSMatthew G. Knepley     fulloption[0] = '-';
44994e22dd4cSMatthew G. Knepley     fulloption[1] = 0;
45004e22dd4cSMatthew G. Knepley     PetscCall(PetscStrlcat(fulloption, option, PETSC_MAX_PATH_LEN));
45014e22dd4cSMatthew G. Knepley     PetscCall(PetscOptionsGetIntArray(NULL, ((PetscObject)dm)->prefix, fulloption, points, &n, NULL));
45024e22dd4cSMatthew G. Knepley     for (PetscInt p = 0; p < n; ++p) PetscCall(DMLabelSetValue(label, points[p], 1));
45034e22dd4cSMatthew G. Knepley   }
4504dd0eeac9SMatthew G. Knepley   // Allow cohesive label creation
4505dd0eeac9SMatthew G. Knepley   //   Faces are input, completed, and all points are marked with their depth
4506dd0eeac9SMatthew G. Knepley   PetscCall(PetscOptionsFindPairPrefix_Private(NULL, ((PetscObject)dm)->prefix, "-dm_plex_cohesive_label_", &option, NULL, &flg));
4507dd0eeac9SMatthew G. Knepley   if (flg) {
4508dd0eeac9SMatthew G. Knepley     DMLabel   label;
4509dd0eeac9SMatthew G. Knepley     PetscInt  points[1024], n, pStart, pEnd, Nl = 1;
45105390be7dSMatthew G. Knepley     PetscBool noCreate = PETSC_FALSE;
4511dd0eeac9SMatthew G. Knepley     char      fulloption[PETSC_MAX_PATH_LEN];
4512dd0eeac9SMatthew G. Knepley     char      name[PETSC_MAX_PATH_LEN];
4513dd0eeac9SMatthew G. Knepley     size_t    len;
4514dd0eeac9SMatthew G. Knepley 
4515dd0eeac9SMatthew G. Knepley     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
4516dd0eeac9SMatthew G. Knepley     PetscCall(PetscStrncpy(name, &option[23], PETSC_MAX_PATH_LEN));
4517dd0eeac9SMatthew G. Knepley     PetscCall(PetscStrlen(name, &len));
4518dd0eeac9SMatthew G. Knepley     if (name[len - 1] == '0') Nl = 10;
4519dd0eeac9SMatthew G. Knepley     for (PetscInt l = 0; l < Nl; ++l) {
45206497c311SBarry Smith       if (l > 0) name[len - 1] = (char)('0' + l);
4521dd0eeac9SMatthew G. Knepley       fulloption[0] = 0;
4522dd0eeac9SMatthew G. Knepley       PetscCall(PetscStrlcat(fulloption, "-dm_plex_cohesive_label_", 32));
4523dd0eeac9SMatthew G. Knepley       PetscCall(PetscStrlcat(fulloption, name, PETSC_MAX_PATH_LEN - 32));
4524dd0eeac9SMatthew G. Knepley       n = 1024;
4525dd0eeac9SMatthew G. Knepley       PetscCall(PetscOptionsGetIntArray(NULL, ((PetscObject)dm)->prefix, fulloption, points, &n, &flg));
4526dd0eeac9SMatthew G. Knepley       if (!flg) break;
45275390be7dSMatthew G. Knepley       PetscCall(DMHasLabel(dm, name, &noCreate));
45285390be7dSMatthew G. Knepley       if (noCreate) {
45295390be7dSMatthew G. Knepley         DMLabel         inlabel;
45305390be7dSMatthew G. Knepley         IS              pointIS;
45315390be7dSMatthew G. Knepley         const PetscInt *lpoints;
45325390be7dSMatthew G. Knepley         PetscInt        pdep, ln, inval = points[0];
45335390be7dSMatthew G. Knepley         char            newname[PETSC_MAX_PATH_LEN];
45345390be7dSMatthew G. Knepley 
45355390be7dSMatthew G. Knepley         PetscCheck(n == 1, comm, PETSC_ERR_ARG_WRONG, "Must specify a label value with this option");
45365390be7dSMatthew G. Knepley         PetscCall(DMGetLabel(dm, name, &inlabel));
45375390be7dSMatthew G. Knepley         PetscCall(DMLabelGetStratumIS(inlabel, inval, &pointIS));
45385390be7dSMatthew G. Knepley         PetscCall(ISGetLocalSize(pointIS, &ln));
45395390be7dSMatthew G. Knepley         PetscCall(ISGetIndices(pointIS, &lpoints));
45405390be7dSMatthew G. Knepley         PetscCall(DMPlexGetPointDepth(dm, lpoints[0], &pdep));
45415390be7dSMatthew G. Knepley         PetscCall(PetscSNPrintf(newname, PETSC_MAX_PATH_LEN, "%s%" PetscInt_FMT, name, points[0]));
45425390be7dSMatthew G. Knepley         PetscCall(DMCreateLabel(dm, newname));
45435390be7dSMatthew G. Knepley         PetscCall(DMGetLabel(dm, newname, &label));
45445390be7dSMatthew G. Knepley         if (!pdep) PetscCall(ProcessCohesiveLabel_Vertices(dm, label, inlabel, inval, ln, lpoints));
45455390be7dSMatthew G. Knepley         else PetscCall(ProcessCohesiveLabel_Faces(dm, label, ln, lpoints));
45465390be7dSMatthew G. Knepley         PetscCall(ISRestoreIndices(pointIS, &lpoints));
45475390be7dSMatthew G. Knepley         PetscCall(ISDestroy(&pointIS));
45485390be7dSMatthew G. Knepley       } else {
4549dd0eeac9SMatthew G. Knepley         PetscCall(DMCreateLabel(dm, name));
4550dd0eeac9SMatthew G. Knepley         PetscCall(DMGetLabel(dm, name, &label));
4551dd0eeac9SMatthew G. Knepley         if (pStart >= pEnd) n = 0;
45525390be7dSMatthew G. Knepley         PetscCall(ProcessCohesiveLabel_Faces(dm, label, n, points));
4553dd0eeac9SMatthew G. Knepley       }
4554dd0eeac9SMatthew G. Knepley       PetscCall(DMPlexOrientLabel(dm, label));
45550542aa8cSMatthew G. Knepley       PetscCall(DMPlexLabelCohesiveComplete(dm, label, NULL, 1, PETSC_FALSE, PETSC_FALSE, NULL));
4556dd0eeac9SMatthew G. Knepley     }
4557dd0eeac9SMatthew G. Knepley   }
45585390be7dSMatthew G. Knepley   PetscCall(DMViewFromOptions(dm, NULL, "-created_dm_view"));
4559708be2fdSJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_CreateFromOptions, dm, 0, 0, 0));
45603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
45610a6ba040SMatthew G. Knepley }
45620a6ba040SMatthew G. Knepley 
4563d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions_NonRefinement_Plex(DM dm, PetscOptionItems *PetscOptionsObject)
4564d71ae5a4SJacob Faibussowitsch {
45650a6ba040SMatthew G. Knepley   DM_Plex  *mesh = (DM_Plex *)dm->data;
45667f9d8d6cSVaclav Hapla   PetscBool flg, flg2;
45679318fe57SMatthew G. Knepley   char      bdLabel[PETSC_MAX_PATH_LEN];
4568adc21957SMatthew G. Knepley   char      method[PETSC_MAX_PATH_LEN];
45690a6ba040SMatthew G. Knepley 
45700a6ba040SMatthew G. Knepley   PetscFunctionBegin;
45710a6ba040SMatthew G. Knepley   /* Handle viewing */
45729566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_print_set_values", "Output all set values info", "DMPlexMatSetClosure", PETSC_FALSE, &mesh->printSetValues, NULL));
45735962854dSMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_fem", "Debug output level for all fem computations", "DMPlexSNESComputeResidualFEM", 0, &mesh->printFEM, NULL, 0));
45745962854dSMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_fvm", "Debug output level for all fvm computations", "DMPlexSNESComputeResidualFVM", 0, &mesh->printFVM, NULL, 0));
45759566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-dm_plex_print_tol", "Tolerance for FEM output", "DMPlexSNESComputeResidualFEM", mesh->printTol, &mesh->printTol, NULL));
45769566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_l2", "Debug output level all L2 diff computations", "DMComputeL2Diff", 0, &mesh->printL2, NULL, 0));
4577f5867de0SMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_locate", "Debug output level all point location computations", "DMLocatePoints", 0, &mesh->printLocate, NULL, 0));
4578a77a5016SMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_project", "Debug output level all projection computations", "DMPlexProject", 0, &mesh->printProject, NULL, 0));
45799566063dSJacob Faibussowitsch   PetscCall(DMMonitorSetFromOptions(dm, "-dm_plex_monitor_throughput", "Monitor the simulation throughput", "DMPlexMonitorThroughput", DMPlexMonitorThroughput, NULL, &flg));
45809566063dSJacob Faibussowitsch   if (flg) PetscCall(PetscLogDefaultBegin());
45819318fe57SMatthew G. Knepley   /* Labeling */
45829566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_boundary_label", "Label to mark the mesh boundary", "", bdLabel, bdLabel, sizeof(bdLabel), &flg));
45839566063dSJacob Faibussowitsch   if (flg) PetscCall(DMPlexCreateBoundaryLabel_Private(dm, bdLabel));
4584953fc75cSMatthew G. Knepley   /* Point Location */
45859566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_hash_location", "Use grid hashing for point location", "DMInterpolate", PETSC_FALSE, &mesh->useHashLocation, NULL));
45860848f4b5SMatthew G. Knepley   /* Partitioning and distribution */
45879566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_partition_balance", "Attempt to evenly divide points on partition boundary between processes", "DMPlexSetPartitionBalance", PETSC_FALSE, &mesh->partitionBalance, NULL));
4588d02c7345SMatthew G. Knepley   /* Reordering */
4589adc21957SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_reorder_section", "Compute point permutation for local section", "DMReorderSectionSetDefault", PETSC_FALSE, &flg2, &flg));
4590adc21957SMatthew G. Knepley   if (flg) PetscCall(DMReorderSectionSetDefault(dm, flg2 ? DM_REORDER_DEFAULT_TRUE : DM_REORDER_DEFAULT_FALSE));
4591adc21957SMatthew G. Knepley   PetscCall(PetscOptionsString("-dm_reorder_section_type", "Reordering method for local section", "DMReorderSectionSetType", method, method, PETSC_MAX_PATH_LEN, &flg));
4592adc21957SMatthew G. Knepley   if (flg) PetscCall(DMReorderSectionSetType(dm, method));
45932e62ab5aSMatthew G. Knepley   /* Generation and remeshing */
45949566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_remesh_bd", "Allow changes to the boundary on remeshing", "DMAdapt", PETSC_FALSE, &mesh->remeshBd, NULL));
4595b29cfa1cSToby Isaac   /* Projection behavior */
4596d5b43468SJose E. Roman   PetscCall(PetscOptionsBoundedInt("-dm_plex_max_projection_height", "Maximum mesh point height used to project locally", "DMPlexSetMaxProjectionHeight", 0, &mesh->maxProjectionHeight, NULL, 0));
45979566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_regular_refinement", "Use special nested projection algorithm for regular refinement", "DMPlexSetRegularRefinement", mesh->regularRefinement, &mesh->regularRefinement, NULL));
4598f12cf164SMatthew G. Knepley   /* Checking structure */
4599f12cf164SMatthew G. Knepley   {
46007f9d8d6cSVaclav Hapla     PetscBool all = PETSC_FALSE;
4601f12cf164SMatthew G. Knepley 
46027f9d8d6cSVaclav Hapla     PetscCall(PetscOptionsBool("-dm_plex_check_all", "Perform all basic checks", "DMPlexCheck", PETSC_FALSE, &all, NULL));
46037f9d8d6cSVaclav Hapla     if (all) {
46047f9d8d6cSVaclav Hapla       PetscCall(DMPlexCheck(dm));
46057f9d8d6cSVaclav Hapla     } else {
46069566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_symmetry", "Check that the adjacency information in the mesh is symmetric", "DMPlexCheckSymmetry", PETSC_FALSE, &flg, &flg2));
46077f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckSymmetry(dm));
46089566063dSJacob 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));
46097f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckSkeleton(dm, 0));
46109566063dSJacob 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));
46117f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckFaces(dm, 0));
46129566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_geometry", "Check that cells have positive volume", "DMPlexCheckGeometry", PETSC_FALSE, &flg, &flg2));
46137f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckGeometry(dm));
46149566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_pointsf", "Check some necessary conditions for PointSF", "DMPlexCheckPointSF", PETSC_FALSE, &flg, &flg2));
4615d7d32a9aSMatthew G. Knepley       if (flg && flg2) PetscCall(DMPlexCheckPointSF(dm, NULL, PETSC_FALSE));
46169566063dSJacob 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));
46177f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckInterfaceCones(dm));
46187f9d8d6cSVaclav Hapla     }
46199566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-dm_plex_check_cell_shape", "Check cell shape", "DMPlexCheckCellShape", PETSC_FALSE, &flg, &flg2));
46209566063dSJacob Faibussowitsch     if (flg && flg2) PetscCall(DMPlexCheckCellShape(dm, PETSC_TRUE, PETSC_DETERMINE));
4621f12cf164SMatthew G. Knepley   }
46229318fe57SMatthew G. Knepley   {
46239318fe57SMatthew G. Knepley     PetscReal scale = 1.0;
46244f3833eaSMatthew G. Knepley 
46259566063dSJacob Faibussowitsch     PetscCall(PetscOptionsReal("-dm_plex_scale", "Scale factor for mesh coordinates", "DMPlexScale", scale, &scale, &flg));
46269318fe57SMatthew G. Knepley     if (flg) {
46279318fe57SMatthew G. Knepley       Vec coordinates, coordinatesLocal;
46289318fe57SMatthew G. Knepley 
46299566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinates(dm, &coordinates));
46309566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinatesLocal(dm, &coordinatesLocal));
46319566063dSJacob Faibussowitsch       PetscCall(VecScale(coordinates, scale));
46329566063dSJacob Faibussowitsch       PetscCall(VecScale(coordinatesLocal, scale));
46339318fe57SMatthew G. Knepley     }
46349318fe57SMatthew G. Knepley   }
46359566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerSetFromOptions(mesh->partitioner));
46363ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
463768d4fef7SMatthew G. Knepley }
463868d4fef7SMatthew G. Knepley 
4639d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions_Overlap_Plex(DM dm, PetscOptionItems *PetscOptionsObject, PetscInt *overlap)
4640d71ae5a4SJacob Faibussowitsch {
4641c506a872SMatthew G. Knepley   PetscInt  numOvLabels = 16, numOvExLabels = 16;
4642c506a872SMatthew G. Knepley   char     *ovLabelNames[16], *ovExLabelNames[16];
4643c506a872SMatthew G. Knepley   PetscInt  numOvValues = 16, numOvExValues = 16, l;
4644c506a872SMatthew G. Knepley   PetscBool flg;
4645c506a872SMatthew G. Knepley 
4646c506a872SMatthew G. Knepley   PetscFunctionBegin;
4647c506a872SMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_distribute_overlap", "The size of the overlap halo", "DMPlexDistribute", *overlap, overlap, NULL, 0));
4648c506a872SMatthew G. Knepley   PetscCall(PetscOptionsStringArray("-dm_distribute_overlap_labels", "List of overlap label names", "DMPlexDistribute", ovLabelNames, &numOvLabels, &flg));
4649c506a872SMatthew G. Knepley   if (!flg) numOvLabels = 0;
4650c506a872SMatthew G. Knepley   if (numOvLabels) {
4651c506a872SMatthew G. Knepley     ((DM_Plex *)dm->data)->numOvLabels = numOvLabels;
4652c506a872SMatthew G. Knepley     for (l = 0; l < numOvLabels; ++l) {
4653c506a872SMatthew G. Knepley       PetscCall(DMGetLabel(dm, ovLabelNames[l], &((DM_Plex *)dm->data)->ovLabels[l]));
4654c506a872SMatthew G. Knepley       PetscCheck(((DM_Plex *)dm->data)->ovLabels[l], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid label name %s", ovLabelNames[l]);
4655c506a872SMatthew G. Knepley       PetscCall(PetscFree(ovLabelNames[l]));
4656c506a872SMatthew G. Knepley     }
4657c506a872SMatthew G. Knepley     PetscCall(PetscOptionsIntArray("-dm_distribute_overlap_values", "List of overlap label values", "DMPlexDistribute", ((DM_Plex *)dm->data)->ovValues, &numOvValues, &flg));
4658c506a872SMatthew G. Knepley     if (!flg) numOvValues = 0;
4659c506a872SMatthew 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);
4660c506a872SMatthew G. Knepley 
4661c506a872SMatthew G. Knepley     PetscCall(PetscOptionsStringArray("-dm_distribute_overlap_exclude_labels", "List of overlap exclude label names", "DMPlexDistribute", ovExLabelNames, &numOvExLabels, &flg));
4662c506a872SMatthew G. Knepley     if (!flg) numOvExLabels = 0;
4663c506a872SMatthew G. Knepley     ((DM_Plex *)dm->data)->numOvExLabels = numOvExLabels;
4664c506a872SMatthew G. Knepley     for (l = 0; l < numOvExLabels; ++l) {
4665c506a872SMatthew G. Knepley       PetscCall(DMGetLabel(dm, ovExLabelNames[l], &((DM_Plex *)dm->data)->ovExLabels[l]));
4666c506a872SMatthew G. Knepley       PetscCheck(((DM_Plex *)dm->data)->ovExLabels[l], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid label name %s", ovExLabelNames[l]);
4667c506a872SMatthew G. Knepley       PetscCall(PetscFree(ovExLabelNames[l]));
4668c506a872SMatthew G. Knepley     }
4669c506a872SMatthew G. Knepley     PetscCall(PetscOptionsIntArray("-dm_distribute_overlap_exclude_values", "List of overlap exclude label values", "DMPlexDistribute", ((DM_Plex *)dm->data)->ovExValues, &numOvExValues, &flg));
4670c506a872SMatthew G. Knepley     if (!flg) numOvExValues = 0;
4671c506a872SMatthew 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);
4672c506a872SMatthew G. Knepley   }
46733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4674c506a872SMatthew G. Knepley }
4675c506a872SMatthew G. Knepley 
4676d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetFromOptions_Plex(DM dm, PetscOptionItems *PetscOptionsObject)
4677d71ae5a4SJacob Faibussowitsch {
4678bdf63967SMatthew G. Knepley   PetscFunctionList    ordlist;
4679bdf63967SMatthew G. Knepley   char                 oname[256];
46804e22dd4cSMatthew G. Knepley   char                 sublabelname[PETSC_MAX_PATH_LEN] = "";
4681adc21957SMatthew G. Knepley   DMReorderDefaultFlag reorder;
4682d410b0cfSMatthew G. Knepley   PetscReal            volume    = -1.0;
46839318fe57SMatthew G. Knepley   PetscInt             prerefine = 0, refine = 0, r, coarsen = 0, overlap = 0, extLayers = 0, dim;
46841b742f01SMatthew 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;
468568d4fef7SMatthew G. Knepley 
468668d4fef7SMatthew G. Knepley   PetscFunctionBegin;
4687d0609cedSBarry Smith   PetscOptionsHeadBegin(PetscOptionsObject, "DMPlex Options");
4688dd4c3f67SMatthew G. Knepley   if (dm->cloneOpts) goto non_refine;
46899318fe57SMatthew G. Knepley   /* Handle automatic creation */
46909566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
46916bc1bd01Sksagiyam   if (dim < 0) {
46926bc1bd01Sksagiyam     PetscCall(DMPlexCreateFromOptions_Internal(PetscOptionsObject, &coordSpace, dm));
46936bc1bd01Sksagiyam     created = PETSC_TRUE;
46946bc1bd01Sksagiyam   }
46956bc1bd01Sksagiyam   PetscCall(DMGetDimension(dm, &dim));
4696d89e6e46SMatthew G. Knepley   /* Handle interpolation before distribution */
46979566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_interpolate_pre", "Flag to interpolate mesh before distribution", "", interpolate, &interpolate, &flg));
4698d89e6e46SMatthew G. Knepley   if (flg) {
4699d89e6e46SMatthew G. Knepley     DMPlexInterpolatedFlag interpolated;
4700d89e6e46SMatthew G. Knepley 
47019566063dSJacob Faibussowitsch     PetscCall(DMPlexIsInterpolated(dm, &interpolated));
4702d89e6e46SMatthew G. Knepley     if (interpolated == DMPLEX_INTERPOLATED_FULL && !interpolate) {
4703d89e6e46SMatthew G. Knepley       DM udm;
4704d89e6e46SMatthew G. Knepley 
47059566063dSJacob Faibussowitsch       PetscCall(DMPlexUninterpolate(dm, &udm));
470669d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &udm));
4707d89e6e46SMatthew G. Knepley     } else if (interpolated != DMPLEX_INTERPOLATED_FULL && interpolate) {
4708d89e6e46SMatthew G. Knepley       DM idm;
4709d89e6e46SMatthew G. Knepley 
47109566063dSJacob Faibussowitsch       PetscCall(DMPlexInterpolate(dm, &idm));
471169d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &idm));
4712d89e6e46SMatthew G. Knepley     }
4713d89e6e46SMatthew G. Knepley   }
47144e22dd4cSMatthew G. Knepley   // Handle submesh selection before distribution
47154e22dd4cSMatthew G. Knepley   PetscCall(PetscOptionsString("-dm_plex_submesh", "Label to use for submesh selection", "", sublabelname, sublabelname, PETSC_MAX_PATH_LEN, &flg));
47164e22dd4cSMatthew G. Knepley   if (flg) {
47174e22dd4cSMatthew G. Knepley     DM              subdm;
47184e22dd4cSMatthew G. Knepley     DMLabel         label;
47194e22dd4cSMatthew G. Knepley     IS              valueIS, pointIS;
47204e22dd4cSMatthew G. Knepley     const PetscInt *values, *points;
47214e22dd4cSMatthew G. Knepley     PetscBool       markedFaces = PETSC_FALSE;
47224e22dd4cSMatthew G. Knepley     PetscInt        Nv, value, Np;
47234e22dd4cSMatthew G. Knepley 
47244e22dd4cSMatthew G. Knepley     PetscCall(DMGetLabel(dm, sublabelname, &label));
47254e22dd4cSMatthew G. Knepley     PetscCall(DMLabelGetNumValues(label, &Nv));
47264e22dd4cSMatthew 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);
47274e22dd4cSMatthew G. Knepley     PetscCall(DMLabelGetValueIS(label, &valueIS));
47284e22dd4cSMatthew G. Knepley     PetscCall(ISGetIndices(valueIS, &values));
47294e22dd4cSMatthew G. Knepley     value = values[0];
47304e22dd4cSMatthew G. Knepley     PetscCall(ISRestoreIndices(valueIS, &values));
47314e22dd4cSMatthew G. Knepley     PetscCall(ISDestroy(&valueIS));
47324e22dd4cSMatthew G. Knepley     PetscCall(DMLabelGetStratumSize(label, value, &Np));
47334e22dd4cSMatthew G. Knepley     PetscCall(DMLabelGetStratumIS(label, value, &pointIS));
47344e22dd4cSMatthew G. Knepley     PetscCall(ISGetIndices(pointIS, &points));
47354e22dd4cSMatthew G. Knepley     for (PetscInt p = 0; p < Np; ++p) {
47364e22dd4cSMatthew G. Knepley       PetscInt pdepth;
47374e22dd4cSMatthew G. Knepley 
47384e22dd4cSMatthew G. Knepley       PetscCall(DMPlexGetPointDepth(dm, points[p], &pdepth));
47394e22dd4cSMatthew G. Knepley       if (pdepth) {
47404e22dd4cSMatthew G. Knepley         markedFaces = PETSC_TRUE;
47414e22dd4cSMatthew G. Knepley         break;
47424e22dd4cSMatthew G. Knepley       }
47434e22dd4cSMatthew G. Knepley     }
47444e22dd4cSMatthew G. Knepley     PetscCall(ISRestoreIndices(pointIS, &points));
47454e22dd4cSMatthew G. Knepley     PetscCall(ISDestroy(&pointIS));
47464e22dd4cSMatthew G. Knepley     PetscCall(DMPlexCreateSubmesh(dm, label, value, markedFaces, &subdm));
47474e22dd4cSMatthew G. Knepley     PetscCall(DMPlexReplace_Internal(dm, &subdm));
47484e22dd4cSMatthew G. Knepley     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
47494e22dd4cSMatthew G. Knepley   }
47509b44eab4SMatthew G. Knepley   /* Handle DMPlex refinement before distribution */
47519566063dSJacob Faibussowitsch   PetscCall(DMPlexGetRefinementUniform(dm, &uniformOrig));
47529566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine_pre", "The number of refinements before distribution", "DMCreate", prerefine, &prerefine, NULL, 0));
47539566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_remap_pre", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL));
47549566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_uniform_pre", "Flag for uniform refinement before distribution", "DMCreate", uniform, &uniform, &flg));
47559566063dSJacob Faibussowitsch   if (flg) PetscCall(DMPlexSetRefinementUniform(dm, uniform));
47569566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-dm_refine_volume_limit_pre", "The maximum cell volume after refinement before distribution", "DMCreate", volume, &volume, &flg));
47579318fe57SMatthew G. Knepley   if (flg) {
47589566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementUniform(dm, PETSC_FALSE));
47599566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementLimit(dm, volume));
47609318fe57SMatthew G. Knepley     prerefine = PetscMax(prerefine, 1);
47619318fe57SMatthew G. Knepley   }
4762b23db253SStefano Zampini   if (prerefine) PetscCall(DMLocalizeCoordinates(dm));
47639b44eab4SMatthew G. Knepley   for (r = 0; r < prerefine; ++r) {
47649b44eab4SMatthew G. Knepley     DM             rdm;
47659b44eab4SMatthew G. Knepley     PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc;
47669b44eab4SMatthew G. Knepley 
4767dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
47689566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &rdm));
476969d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &rdm));
4770dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
477161a622f3SMatthew G. Knepley     if (coordFunc && remap) {
47729566063dSJacob Faibussowitsch       PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
47739b44eab4SMatthew G. Knepley       ((DM_Plex *)dm->data)->coordFunc = coordFunc;
47749b44eab4SMatthew G. Knepley     }
47759b44eab4SMatthew G. Knepley   }
47769566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, uniformOrig));
47779318fe57SMatthew G. Knepley   /* Handle DMPlex extrusion before distribution */
47789566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_extrude", "The number of layers to extrude", "", extLayers, &extLayers, NULL, 0));
47799318fe57SMatthew G. Knepley   if (extLayers) {
47809318fe57SMatthew G. Knepley     DM edm;
47819318fe57SMatthew G. Knepley 
47829566063dSJacob Faibussowitsch     PetscCall(DMExtrude(dm, extLayers, &edm));
478369d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &edm));
478448d16a33SMatthew G. Knepley     ((DM_Plex *)dm->data)->coordFunc = NULL;
4785dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
4786d410b0cfSMatthew G. Knepley     extLayers = 0;
47875e17fc22SAidan Hamilton     PetscCall(DMGetDimension(dm, &dim));
47889318fe57SMatthew G. Knepley   }
4789bdf63967SMatthew G. Knepley   /* Handle DMPlex reordering before distribution */
47906bc1bd01Sksagiyam   PetscCall(DMPlexReorderGetDefault(dm, &reorder));
47919566063dSJacob Faibussowitsch   PetscCall(MatGetOrderingList(&ordlist));
47926bc1bd01Sksagiyam   PetscCall(PetscStrncpy(oname, MATORDERINGNATURAL, sizeof(oname)));
47939566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_plex_reorder", "Set mesh reordering type", "DMPlexGetOrdering", ordlist, MATORDERINGNATURAL, oname, sizeof(oname), &flg));
4794adc21957SMatthew G. Knepley   if (reorder == DM_REORDER_DEFAULT_TRUE || flg) {
4795bdf63967SMatthew G. Knepley     DM pdm;
4796bdf63967SMatthew G. Knepley     IS perm;
4797bdf63967SMatthew G. Knepley 
47989566063dSJacob Faibussowitsch     PetscCall(DMPlexGetOrdering(dm, oname, NULL, &perm));
47999566063dSJacob Faibussowitsch     PetscCall(DMPlexPermute(dm, perm, &pdm));
48009566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&perm));
480169d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &pdm));
4802dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
4803bdf63967SMatthew G. Knepley   }
48049b44eab4SMatthew G. Knepley   /* Handle DMPlex distribution */
48059566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeGetDefault(dm, &distribute));
4806c506a872SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_distribute", "Flag to redistribute a mesh among processes", "DMPlexDistribute", distribute, &distribute, NULL));
4807a286e215SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_distribute_save_sf", "Flag to save the migration SF", "DMPlexSetMigrationSF", saveSF, &saveSF, NULL));
4808dbbe0bcdSBarry Smith   PetscCall(DMSetFromOptions_Overlap_Plex(dm, PetscOptionsObject, &overlap));
48099b44eab4SMatthew G. Knepley   if (distribute) {
48109b44eab4SMatthew G. Knepley     DM               pdm = NULL;
48119b44eab4SMatthew G. Knepley     PetscPartitioner part;
4812a286e215SMatthew G. Knepley     PetscSF          sfMigration;
48139b44eab4SMatthew G. Knepley 
48149566063dSJacob Faibussowitsch     PetscCall(DMPlexGetPartitioner(dm, &part));
48159566063dSJacob Faibussowitsch     PetscCall(PetscPartitionerSetFromOptions(part));
4816a286e215SMatthew G. Knepley     PetscCall(DMPlexDistribute(dm, overlap, &sfMigration, &pdm));
481748a46eb9SPierre Jolivet     if (pdm) PetscCall(DMPlexReplace_Internal(dm, &pdm));
4818a286e215SMatthew G. Knepley     if (saveSF) PetscCall(DMPlexSetMigrationSF(dm, sfMigration));
4819a286e215SMatthew G. Knepley     PetscCall(PetscSFDestroy(&sfMigration));
48209b44eab4SMatthew G. Knepley   }
48214054ae39SJames Wright 
48224054ae39SJames Wright   {
48234054ae39SJames Wright     PetscBool useBoxLabel = PETSC_FALSE;
48244054ae39SJames Wright     PetscCall(PetscOptionsBool("-dm_plex_box_label", "Create 'Face Sets' assuming boundary faces align with cartesian directions", "DMCreate", useBoxLabel, &useBoxLabel, NULL));
4825*d7d2d1d2SJames Wright     if (useBoxLabel) {
4826*d7d2d1d2SJames Wright       PetscInt       n      = 3;
4827*d7d2d1d2SJames Wright       DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
4828*d7d2d1d2SJames Wright 
4829*d7d2d1d2SJames Wright       PetscCall(PetscOptionsEnumArray("-dm_plex_box_label_bd", "Boundary type for each dimension when using -dm_plex_box_label", "", DMBoundaryTypes, (PetscEnum *)bdt, &n, &flg));
4830*d7d2d1d2SJames Wright       PetscCheck(!flg || !(n != dim), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_SIZ, "Box boundary types had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
4831*d7d2d1d2SJames Wright       PetscCall(DMPlexSetBoxLabel_Internal(dm, bdt));
4832*d7d2d1d2SJames Wright     }
48334054ae39SJames Wright   }
4834d2b2dc1eSMatthew G. Knepley   /* Must check CEED options before creating function space for coordinates */
4835d2b2dc1eSMatthew G. Knepley   {
4836d2b2dc1eSMatthew G. Knepley     PetscBool useCeed = PETSC_FALSE, flg;
4837d2b2dc1eSMatthew G. Knepley 
4838d2b2dc1eSMatthew G. Knepley     PetscCall(PetscOptionsBool("-dm_plex_use_ceed", "Use LibCEED as the FEM backend", "DMPlexSetUseCeed", useCeed, &useCeed, &flg));
4839d2b2dc1eSMatthew G. Knepley     if (flg) PetscCall(DMPlexSetUseCeed(dm, useCeed));
4840d2b2dc1eSMatthew G. Knepley   }
48419318fe57SMatthew G. Knepley   /* Create coordinate space */
48429318fe57SMatthew G. Knepley   if (created) {
484361a622f3SMatthew G. Knepley     DM_Plex  *mesh   = (DM_Plex *)dm->data;
4844e44f6aebSMatthew G. Knepley     PetscInt  degree = 1, deg;
48455515ebd3SMatthew G. Knepley     PetscInt  height = 0;
48465515ebd3SMatthew G. Knepley     DM        cdm;
4847c3db174cSMatthew G. Knepley     PetscBool flg, localize = PETSC_TRUE, sparseLocalize = PETSC_TRUE;
48489318fe57SMatthew G. Knepley 
48499566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-dm_coord_space", "Use an FEM space for coordinates", "", coordSpace, &coordSpace, &flg));
48509566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-dm_coord_petscspace_degree", "FEM degree for coordinate space", "", degree, &degree, NULL));
4851e44f6aebSMatthew G. Knepley     PetscCall(DMGetCoordinateDegree_Internal(dm, &deg));
4852e44f6aebSMatthew G. Knepley     if (coordSpace && deg <= 1) PetscCall(DMPlexCreateCoordinateSpace(dm, degree, PETSC_TRUE, mesh->coordFunc));
48535515ebd3SMatthew G. Knepley     PetscCall(DMGetCoordinateDM(dm, &cdm));
485461a622f3SMatthew G. Knepley     if (flg && !coordSpace) {
485561a622f3SMatthew G. Knepley       PetscDS      cds;
485661a622f3SMatthew G. Knepley       PetscObject  obj;
485761a622f3SMatthew G. Knepley       PetscClassId id;
485861a622f3SMatthew G. Knepley 
48599566063dSJacob Faibussowitsch       PetscCall(DMGetDS(cdm, &cds));
48609566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
48619566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
486261a622f3SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
486361a622f3SMatthew G. Knepley         PetscContainer dummy;
486461a622f3SMatthew G. Knepley 
48659566063dSJacob Faibussowitsch         PetscCall(PetscContainerCreate(PETSC_COMM_SELF, &dummy));
48669566063dSJacob Faibussowitsch         PetscCall(PetscObjectSetName((PetscObject)dummy, "coordinates"));
48679566063dSJacob Faibussowitsch         PetscCall(DMSetField(cdm, 0, NULL, (PetscObject)dummy));
48689566063dSJacob Faibussowitsch         PetscCall(PetscContainerDestroy(&dummy));
48699566063dSJacob Faibussowitsch         PetscCall(DMClearDS(cdm));
487061a622f3SMatthew G. Knepley       }
487161a622f3SMatthew G. Knepley       mesh->coordFunc = NULL;
487261a622f3SMatthew G. Knepley     }
4873c3db174cSMatthew G. Knepley     PetscCall(PetscOptionsBool("-dm_localize", "Localize mesh coordinates", "", localize, &localize, NULL));
4874c3db174cSMatthew G. Knepley     PetscCall(PetscOptionsBool("-dm_sparse_localize", "Localize only necessary cells", "DMSetSparseLocalize", sparseLocalize, &sparseLocalize, &flg));
4875c3db174cSMatthew G. Knepley     if (flg) PetscCall(DMSetSparseLocalize(dm, sparseLocalize));
48765515ebd3SMatthew G. Knepley     PetscCall(PetscOptionsInt("-dm_localize_height", "Localize edges and faces in addition to cells", "", height, &height, &flg));
48775515ebd3SMatthew G. Knepley     if (flg) PetscCall(DMPlexSetMaxProjectionHeight(cdm, height));
4878c3db174cSMatthew G. Knepley     if (localize) PetscCall(DMLocalizeCoordinates(dm));
48799318fe57SMatthew G. Knepley   }
488068d4fef7SMatthew G. Knepley   /* Handle DMPlex refinement */
488161a622f3SMatthew G. Knepley   remap = PETSC_TRUE;
48829566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine", "The number of uniform refinements", "DMCreate", refine, &refine, NULL, 0));
48839566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_remap", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL));
48849566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine_hierarchy", "The number of uniform refinements", "DMCreate", refine, &refine, &isHierarchy, 0));
48859566063dSJacob Faibussowitsch   if (refine) PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
488668d4fef7SMatthew G. Knepley   if (refine && isHierarchy) {
4887acdc6f61SToby Isaac     DM *dms, coarseDM;
488868d4fef7SMatthew G. Knepley 
48899566063dSJacob Faibussowitsch     PetscCall(DMGetCoarseDM(dm, &coarseDM));
48909566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)coarseDM));
48919566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(refine, &dms));
48929566063dSJacob Faibussowitsch     PetscCall(DMRefineHierarchy(dm, refine, dms));
489368d4fef7SMatthew G. Knepley     /* Total hack since we do not pass in a pointer */
48949566063dSJacob Faibussowitsch     PetscCall(DMPlexSwap_Static(dm, dms[refine - 1]));
489568d4fef7SMatthew G. Knepley     if (refine == 1) {
48969566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dm, dms[0]));
48979566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dm, PETSC_TRUE));
489868d4fef7SMatthew G. Knepley     } else {
48999566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dm, dms[refine - 2]));
49009566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dm, PETSC_TRUE));
49019566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dms[0], dms[refine - 1]));
49029566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dms[0], PETSC_TRUE));
490368d4fef7SMatthew G. Knepley     }
49049566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dms[refine - 1], coarseDM));
49059566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)coarseDM));
490668d4fef7SMatthew G. Knepley     /* Free DMs */
490768d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
4908dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dms[r], PetscOptionsObject));
49099566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dms[r]));
491068d4fef7SMatthew G. Knepley     }
49119566063dSJacob Faibussowitsch     PetscCall(PetscFree(dms));
491268d4fef7SMatthew G. Knepley   } else {
491368d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
49149318fe57SMatthew G. Knepley       DM             rdm;
491551a74b61SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc;
491668d4fef7SMatthew G. Knepley 
4917dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
49189566063dSJacob Faibussowitsch       PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &rdm));
491968d4fef7SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
492069d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &rdm));
4921dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
492261a622f3SMatthew G. Knepley       if (coordFunc && remap) {
49239566063dSJacob Faibussowitsch         PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
492451a74b61SMatthew G. Knepley         ((DM_Plex *)dm->data)->coordFunc = coordFunc;
492551a74b61SMatthew G. Knepley       }
492668d4fef7SMatthew G. Knepley     }
492768d4fef7SMatthew G. Knepley   }
49283cf6fe12SMatthew G. Knepley   /* Handle DMPlex coarsening */
49299566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_coarsen", "Coarsen the mesh", "DMCreate", coarsen, &coarsen, NULL, 0));
49309566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_coarsen_hierarchy", "The number of coarsenings", "DMCreate", coarsen, &coarsen, &isHierarchy, 0));
4931b653a561SMatthew G. Knepley   if (coarsen && isHierarchy) {
4932b653a561SMatthew G. Knepley     DM *dms;
4933b653a561SMatthew G. Knepley 
49349566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(coarsen, &dms));
49359566063dSJacob Faibussowitsch     PetscCall(DMCoarsenHierarchy(dm, coarsen, dms));
4936b653a561SMatthew G. Knepley     /* Free DMs */
4937b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
4938dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dms[r], PetscOptionsObject));
49399566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dms[r]));
4940b653a561SMatthew G. Knepley     }
49419566063dSJacob Faibussowitsch     PetscCall(PetscFree(dms));
4942b653a561SMatthew G. Knepley   } else {
4943b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
49449318fe57SMatthew G. Knepley       DM             cdm;
49459318fe57SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc;
49463cf6fe12SMatthew G. Knepley 
4947dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
49489566063dSJacob Faibussowitsch       PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &cdm));
49493cf6fe12SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
495069d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &cdm));
4951dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
49529318fe57SMatthew G. Knepley       if (coordFunc) {
49539566063dSJacob Faibussowitsch         PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
49549318fe57SMatthew G. Knepley         ((DM_Plex *)dm->data)->coordFunc = coordFunc;
49559318fe57SMatthew G. Knepley       }
49563cf6fe12SMatthew G. Knepley     }
4957b653a561SMatthew G. Knepley   }
4958be664eb1SMatthew G. Knepley   // Handle coordinate remapping
4959be664eb1SMatthew G. Knepley   remap = PETSC_FALSE;
4960be664eb1SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_coord_remap", "Flag to control coordinate remapping", "", remap, &remap, NULL));
4961be664eb1SMatthew G. Knepley   if (remap) {
4962be664eb1SMatthew G. Knepley     DMPlexCoordMap map     = DM_COORD_MAP_NONE;
4963be664eb1SMatthew G. Knepley     PetscPointFunc mapFunc = NULL;
4964be664eb1SMatthew G. Knepley     PetscScalar    params[16];
4965f45b553cSPierre Jolivet     PetscInt       Np = PETSC_STATIC_ARRAY_LENGTH(params), cdim;
4966be664eb1SMatthew G. Knepley     MPI_Comm       comm;
4967be664eb1SMatthew G. Knepley 
4968be664eb1SMatthew G. Knepley     PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4969be664eb1SMatthew G. Knepley     PetscCall(DMGetCoordinateDim(dm, &cdim));
4970be664eb1SMatthew G. Knepley     PetscCall(PetscOptionsScalarArray("-dm_coord_map_params", "Parameters for the coordinate remapping", "", params, &Np, &flg));
4971be664eb1SMatthew G. Knepley     if (!flg) Np = 0;
4972be664eb1SMatthew G. Knepley     // TODO Allow user to pass a map function by name
4973be664eb1SMatthew G. Knepley     PetscCall(PetscOptionsEnum("-dm_coord_map", "Coordinate mapping for built-in mesh", "", DMPlexCoordMaps, (PetscEnum)map, (PetscEnum *)&map, &flg));
4974be664eb1SMatthew G. Knepley     if (flg) {
4975be664eb1SMatthew G. Knepley       switch (map) {
4976be664eb1SMatthew G. Knepley       case DM_COORD_MAP_NONE:
4977be664eb1SMatthew G. Knepley         mapFunc = coordMap_identity;
4978be664eb1SMatthew G. Knepley         break;
4979be664eb1SMatthew G. Knepley       case DM_COORD_MAP_SHEAR:
4980be664eb1SMatthew G. Knepley         mapFunc = coordMap_shear;
4981be664eb1SMatthew G. Knepley         if (!Np) {
4982be664eb1SMatthew G. Knepley           Np        = cdim + 1;
4983be664eb1SMatthew G. Knepley           params[0] = 0;
4984be664eb1SMatthew G. Knepley           for (PetscInt d = 1; d <= cdim; ++d) params[d] = 1.0;
4985be664eb1SMatthew G. Knepley         }
4986be664eb1SMatthew 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);
4987be664eb1SMatthew G. Knepley         break;
4988be664eb1SMatthew G. Knepley       case DM_COORD_MAP_FLARE:
4989be664eb1SMatthew G. Knepley         mapFunc = coordMap_flare;
4990be664eb1SMatthew G. Knepley         if (!Np) {
4991be664eb1SMatthew G. Knepley           Np        = cdim + 1;
4992be664eb1SMatthew G. Knepley           params[0] = 0;
4993be664eb1SMatthew G. Knepley           for (PetscInt d = 1; d <= cdim; ++d) params[d] = 1.0;
4994be664eb1SMatthew G. Knepley         }
4995be664eb1SMatthew 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);
4996be664eb1SMatthew G. Knepley         break;
4997be664eb1SMatthew G. Knepley       case DM_COORD_MAP_ANNULUS:
4998be664eb1SMatthew G. Knepley         mapFunc = coordMap_annulus;
4999be664eb1SMatthew G. Knepley         if (!Np) {
5000be664eb1SMatthew G. Knepley           Np        = 2;
5001be664eb1SMatthew G. Knepley           params[0] = 1.;
5002be664eb1SMatthew G. Knepley           params[1] = 2.;
5003be664eb1SMatthew G. Knepley         }
5004be664eb1SMatthew G. Knepley         PetscCheck(Np == 2, comm, PETSC_ERR_ARG_WRONG, "The annulus coordinate map must have 2 parameters, not %" PetscInt_FMT, Np);
5005be664eb1SMatthew G. Knepley         break;
5006be664eb1SMatthew G. Knepley       case DM_COORD_MAP_SHELL:
5007be664eb1SMatthew G. Knepley         mapFunc = coordMap_shell;
5008be664eb1SMatthew G. Knepley         if (!Np) {
5009be664eb1SMatthew G. Knepley           Np        = 2;
5010be664eb1SMatthew G. Knepley           params[0] = 1.;
5011be664eb1SMatthew G. Knepley           params[1] = 2.;
5012be664eb1SMatthew G. Knepley         }
5013be664eb1SMatthew G. Knepley         PetscCheck(Np == 2, comm, PETSC_ERR_ARG_WRONG, "The spherical shell coordinate map must have 2 parameters, not %" PetscInt_FMT, Np);
5014be664eb1SMatthew G. Knepley         break;
5015be664eb1SMatthew G. Knepley       default:
5016be664eb1SMatthew G. Knepley         mapFunc = coordMap_identity;
5017be664eb1SMatthew G. Knepley       }
5018be664eb1SMatthew G. Knepley     }
5019be664eb1SMatthew G. Knepley     if (Np) {
5020be664eb1SMatthew G. Knepley       DM      cdm;
5021be664eb1SMatthew G. Knepley       PetscDS cds;
5022be664eb1SMatthew G. Knepley 
5023be664eb1SMatthew G. Knepley       PetscCall(DMGetCoordinateDM(dm, &cdm));
5024be664eb1SMatthew G. Knepley       PetscCall(DMGetDS(cdm, &cds));
5025be664eb1SMatthew G. Knepley       PetscCall(PetscDSSetConstants(cds, Np, params));
5026be664eb1SMatthew G. Knepley     }
5027be664eb1SMatthew G. Knepley     PetscCall(DMPlexRemapGeometry(dm, 0.0, mapFunc));
5028be664eb1SMatthew G. Knepley   }
5029909dfd52SMatthew G. Knepley   /* Handle ghost cells */
50309566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_create_fv_ghost_cells", "Flag to create finite volume ghost cells on the boundary", "DMCreate", ghostCells, &ghostCells, NULL));
5031909dfd52SMatthew G. Knepley   if (ghostCells) {
5032909dfd52SMatthew G. Knepley     DM   gdm;
5033909dfd52SMatthew G. Knepley     char lname[PETSC_MAX_PATH_LEN];
5034909dfd52SMatthew G. Knepley 
5035909dfd52SMatthew G. Knepley     lname[0] = '\0';
50369566063dSJacob Faibussowitsch     PetscCall(PetscOptionsString("-dm_plex_fv_ghost_cells_label", "Label name for ghost cells boundary", "DMCreate", lname, lname, sizeof(lname), &flg));
50379566063dSJacob Faibussowitsch     PetscCall(DMPlexConstructGhostCells(dm, flg ? lname : NULL, NULL, &gdm));
503869d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &gdm));
5039909dfd52SMatthew G. Knepley   }
50406913077dSMatthew G. Knepley   /* Handle 1D order */
5041adc21957SMatthew G. Knepley   if (reorder != DM_REORDER_DEFAULT_FALSE && dim == 1) {
50426913077dSMatthew G. Knepley     DM           cdm, rdm;
50436913077dSMatthew G. Knepley     PetscDS      cds;
50446913077dSMatthew G. Knepley     PetscObject  obj;
50456913077dSMatthew G. Knepley     PetscClassId id = PETSC_OBJECT_CLASSID;
50466913077dSMatthew G. Knepley     IS           perm;
50476bc1bd01Sksagiyam     PetscInt     Nf;
50486913077dSMatthew G. Knepley     PetscBool    distributed;
50496913077dSMatthew G. Knepley 
50509566063dSJacob Faibussowitsch     PetscCall(DMPlexIsDistributed(dm, &distributed));
50519566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
50529566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
50539566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(cds, &Nf));
50546913077dSMatthew G. Knepley     if (Nf) {
50559566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
50569566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
50576913077dSMatthew G. Knepley     }
50586bc1bd01Sksagiyam     if (!distributed && id != PETSCFE_CLASSID) {
50599566063dSJacob Faibussowitsch       PetscCall(DMPlexGetOrdering1D(dm, &perm));
50609566063dSJacob Faibussowitsch       PetscCall(DMPlexPermute(dm, perm, &rdm));
506169d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &rdm));
50629566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&perm));
50636913077dSMatthew G. Knepley     }
50646913077dSMatthew G. Knepley   }
50653cf6fe12SMatthew G. Knepley /* Handle */
5066dd4c3f67SMatthew G. Knepley non_refine:
5067dbbe0bcdSBarry Smith   PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
506822d6dc08SStefano Zampini   char    *phases[16];
506922d6dc08SStefano Zampini   PetscInt Nphases = 16;
507022d6dc08SStefano Zampini   PetscCall(PetscOptionsStringArray("-dm_plex_option_phases", "Option phase prefixes", "DMSetFromOptions", phases, &Nphases, &flg));
5071d0609cedSBarry Smith   PetscOptionsHeadEnd();
507222d6dc08SStefano Zampini 
507322d6dc08SStefano Zampini   // Phases
507422d6dc08SStefano Zampini   if (flg) {
507522d6dc08SStefano Zampini     const char *oldPrefix;
507622d6dc08SStefano Zampini 
507722d6dc08SStefano Zampini     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, &oldPrefix));
507822d6dc08SStefano Zampini     for (PetscInt ph = 0; ph < Nphases; ++ph) {
507922d6dc08SStefano Zampini       PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)dm, phases[ph]));
508022d6dc08SStefano Zampini       PetscCall(PetscInfo(dm, "Options phase %s for DM %s\n", phases[ph], dm->hdr.name));
508122d6dc08SStefano Zampini       PetscCall(DMSetFromOptions(dm));
508222d6dc08SStefano Zampini       PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, oldPrefix));
508322d6dc08SStefano Zampini       PetscCall(PetscFree(phases[ph]));
508422d6dc08SStefano Zampini     }
508522d6dc08SStefano Zampini   }
50863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
50870a6ba040SMatthew G. Knepley }
50880a6ba040SMatthew G. Knepley 
5089d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMCreateGlobalVector_Plex(DM dm, Vec *vec)
5090d71ae5a4SJacob Faibussowitsch {
5091552f7358SJed Brown   PetscFunctionBegin;
50929566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector_Section_Private(dm, vec));
50939566063dSJacob Faibussowitsch   /* PetscCall(VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM)); */
50949566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEW, (void (*)(void))VecView_Plex));
50959566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEWNATIVE, (void (*)(void))VecView_Plex_Native));
50969566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOAD, (void (*)(void))VecLoad_Plex));
50979566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOADNATIVE, (void (*)(void))VecLoad_Plex_Native));
50983ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5099552f7358SJed Brown }
5100552f7358SJed Brown 
5101d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMCreateLocalVector_Plex(DM dm, Vec *vec)
5102d71ae5a4SJacob Faibussowitsch {
5103552f7358SJed Brown   PetscFunctionBegin;
51049566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector_Section_Private(dm, vec));
51059566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEW, (void (*)(void))VecView_Plex_Local));
51069566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOAD, (void (*)(void))VecLoad_Plex_Local));
51073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5108552f7358SJed Brown }
5109552f7358SJed Brown 
5110d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGetDimPoints_Plex(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
5111d71ae5a4SJacob Faibussowitsch {
5112793f3fe5SMatthew G. Knepley   PetscInt depth, d;
5113793f3fe5SMatthew G. Knepley 
5114793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
51159566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
5116793f3fe5SMatthew G. Knepley   if (depth == 1) {
51179566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(dm, &d));
51189566063dSJacob Faibussowitsch     if (dim == 0) PetscCall(DMPlexGetDepthStratum(dm, dim, pStart, pEnd));
51199566063dSJacob Faibussowitsch     else if (dim == d) PetscCall(DMPlexGetDepthStratum(dm, 1, pStart, pEnd));
51209371c9d4SSatish Balay     else {
51219371c9d4SSatish Balay       *pStart = 0;
51229371c9d4SSatish Balay       *pEnd   = 0;
51239371c9d4SSatish Balay     }
5124793f3fe5SMatthew G. Knepley   } else {
51259566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, dim, pStart, pEnd));
5126793f3fe5SMatthew G. Knepley   }
51273ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5128793f3fe5SMatthew G. Knepley }
5129793f3fe5SMatthew G. Knepley 
5130d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGetNeighbors_Plex(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
5131d71ae5a4SJacob Faibussowitsch {
5132502a2867SDave May   PetscSF            sf;
51336497c311SBarry Smith   PetscMPIInt        niranks, njranks;
51346497c311SBarry Smith   PetscInt           n;
51350a19bb7dSprj-   const PetscMPIInt *iranks, *jranks;
51360a19bb7dSprj-   DM_Plex           *data = (DM_Plex *)dm->data;
5137502a2867SDave May 
51382f356facSMatthew G. Knepley   PetscFunctionBegin;
51399566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
51400a19bb7dSprj-   if (!data->neighbors) {
51419566063dSJacob Faibussowitsch     PetscCall(PetscSFSetUp(sf));
51429566063dSJacob Faibussowitsch     PetscCall(PetscSFGetRootRanks(sf, &njranks, &jranks, NULL, NULL, NULL));
51439566063dSJacob Faibussowitsch     PetscCall(PetscSFGetLeafRanks(sf, &niranks, &iranks, NULL, NULL));
51449566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(njranks + niranks + 1, &data->neighbors));
51459566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(data->neighbors + 1, jranks, njranks));
51469566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(data->neighbors + njranks + 1, iranks, niranks));
51470a19bb7dSprj-     n = njranks + niranks;
51489566063dSJacob Faibussowitsch     PetscCall(PetscSortRemoveDupsMPIInt(&n, data->neighbors + 1));
51490a19bb7dSprj-     /* The following cast should never fail: can't have more neighbors than PETSC_MPI_INT_MAX */
51509566063dSJacob Faibussowitsch     PetscCall(PetscMPIIntCast(n, data->neighbors));
51510a19bb7dSprj-   }
51520a19bb7dSprj-   if (nranks) *nranks = data->neighbors[0];
51530a19bb7dSprj-   if (ranks) {
51540a19bb7dSprj-     if (data->neighbors[0]) *ranks = data->neighbors + 1;
51550a19bb7dSprj-     else *ranks = NULL;
51560a19bb7dSprj-   }
51573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5158502a2867SDave May }
5159502a2867SDave May 
51601eb70e55SToby Isaac PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM, DM, Mat, Vec, Vec);
51611eb70e55SToby Isaac 
5162d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMInitialize_Plex(DM dm)
5163d71ae5a4SJacob Faibussowitsch {
5164552f7358SJed Brown   PetscFunctionBegin;
5165552f7358SJed Brown   dm->ops->view                      = DMView_Plex;
51662c40f234SMatthew G. Knepley   dm->ops->load                      = DMLoad_Plex;
5167552f7358SJed Brown   dm->ops->setfromoptions            = DMSetFromOptions_Plex;
516838221697SMatthew G. Knepley   dm->ops->clone                     = DMClone_Plex;
5169552f7358SJed Brown   dm->ops->setup                     = DMSetUp_Plex;
51701bb6d2a8SBarry Smith   dm->ops->createlocalsection        = DMCreateLocalSection_Plex;
5171adc21957SMatthew G. Knepley   dm->ops->createsectionpermutation  = DMCreateSectionPermutation_Plex;
517266ad2231SToby Isaac   dm->ops->createdefaultconstraints  = DMCreateDefaultConstraints_Plex;
5173552f7358SJed Brown   dm->ops->createglobalvector        = DMCreateGlobalVector_Plex;
5174552f7358SJed Brown   dm->ops->createlocalvector         = DMCreateLocalVector_Plex;
5175184d77edSJed Brown   dm->ops->getlocaltoglobalmapping   = NULL;
51760298fd71SBarry Smith   dm->ops->createfieldis             = NULL;
5177552f7358SJed Brown   dm->ops->createcoordinatedm        = DMCreateCoordinateDM_Plex;
5178f19dbd58SToby Isaac   dm->ops->createcoordinatefield     = DMCreateCoordinateField_Plex;
51790a6ba040SMatthew G. Knepley   dm->ops->getcoloring               = NULL;
5180552f7358SJed Brown   dm->ops->creatematrix              = DMCreateMatrix_Plex;
5181bceba477SMatthew G. Knepley   dm->ops->createinterpolation       = DMCreateInterpolation_Plex;
5182bd041c0cSMatthew G. Knepley   dm->ops->createmassmatrix          = DMCreateMassMatrix_Plex;
5183b4937a87SMatthew G. Knepley   dm->ops->createmassmatrixlumped    = DMCreateMassMatrixLumped_Plex;
51845a84ad33SLisandro Dalcin   dm->ops->createinjection           = DMCreateInjection_Plex;
5185552f7358SJed Brown   dm->ops->refine                    = DMRefine_Plex;
51860a6ba040SMatthew G. Knepley   dm->ops->coarsen                   = DMCoarsen_Plex;
51870a6ba040SMatthew G. Knepley   dm->ops->refinehierarchy           = DMRefineHierarchy_Plex;
5188b653a561SMatthew G. Knepley   dm->ops->coarsenhierarchy          = DMCoarsenHierarchy_Plex;
5189d410b0cfSMatthew G. Knepley   dm->ops->extrude                   = DMExtrude_Plex;
51900298fd71SBarry Smith   dm->ops->globaltolocalbegin        = NULL;
51910298fd71SBarry Smith   dm->ops->globaltolocalend          = NULL;
51920298fd71SBarry Smith   dm->ops->localtoglobalbegin        = NULL;
51930298fd71SBarry Smith   dm->ops->localtoglobalend          = NULL;
5194552f7358SJed Brown   dm->ops->destroy                   = DMDestroy_Plex;
5195552f7358SJed Brown   dm->ops->createsubdm               = DMCreateSubDM_Plex;
51962adcc780SMatthew G. Knepley   dm->ops->createsuperdm             = DMCreateSuperDM_Plex;
5197793f3fe5SMatthew G. Knepley   dm->ops->getdimpoints              = DMGetDimPoints_Plex;
5198552f7358SJed Brown   dm->ops->locatepoints              = DMLocatePoints_Plex;
51990709b2feSToby Isaac   dm->ops->projectfunctionlocal      = DMProjectFunctionLocal_Plex;
52000709b2feSToby Isaac   dm->ops->projectfunctionlabellocal = DMProjectFunctionLabelLocal_Plex;
5201bfc4295aSToby Isaac   dm->ops->projectfieldlocal         = DMProjectFieldLocal_Plex;
52028c6c5593SMatthew G. Knepley   dm->ops->projectfieldlabellocal    = DMProjectFieldLabelLocal_Plex;
5203ece3a9fcSMatthew G. Knepley   dm->ops->projectbdfieldlabellocal  = DMProjectBdFieldLabelLocal_Plex;
52040709b2feSToby Isaac   dm->ops->computel2diff             = DMComputeL2Diff_Plex;
5205b698f381SToby Isaac   dm->ops->computel2gradientdiff     = DMComputeL2GradientDiff_Plex;
52062a16baeaSToby Isaac   dm->ops->computel2fielddiff        = DMComputeL2FieldDiff_Plex;
520728d58a37SPierre Jolivet   dm->ops->getneighbors              = DMGetNeighbors_Plex;
52086c6a6b79SMatthew G. Knepley   dm->ops->getlocalboundingbox       = DMGetLocalBoundingBox_Coordinates;
5209907a3e9cSStefano Zampini   dm->ops->createdomaindecomposition = DMCreateDomainDecomposition_Plex;
5210907a3e9cSStefano Zampini   dm->ops->createddscatters          = DMCreateDomainDecompositionScatters_Plex;
52119566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertBoundaryValues_C", DMPlexInsertBoundaryValues_Plex));
52126c51210dSStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertTimeDerivativeBoundaryValues_C", DMPlexInsertTimeDerivativeBoundaryValues_Plex));
52139566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMSetUpGLVisViewer_C", DMSetUpGLVisViewer_Plex));
52149566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMCreateNeumannOverlap_C", DMCreateNeumannOverlap_Plex));
52159566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeGetDefault_C", DMPlexDistributeGetDefault_Plex));
52169566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeSetDefault_C", DMPlexDistributeSetDefault_Plex));
52176bc1bd01Sksagiyam   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderGetDefault_C", DMPlexReorderGetDefault_Plex));
52186bc1bd01Sksagiyam   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderSetDefault_C", DMPlexReorderSetDefault_Plex));
5219adc21957SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionGetDefault_C", DMReorderSectionGetDefault_Plex));
5220adc21957SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionSetDefault_C", DMReorderSectionSetDefault_Plex));
5221adc21957SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionGetType_C", DMReorderSectionGetType_Plex));
5222adc21957SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionSetType_C", DMReorderSectionSetType_Plex));
52239566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMInterpolateSolution_C", DMInterpolateSolution_Plex));
5224c506a872SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetOverlap_C", DMPlexGetOverlap_Plex));
5225c506a872SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexSetOverlap_C", DMPlexSetOverlap_Plex));
5226d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetUseCeed_C", DMPlexGetUseCeed_Plex));
5227d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexSetUseCeed_C", DMPlexSetUseCeed_Plex));
52283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5229552f7358SJed Brown }
5230552f7358SJed Brown 
5231d71ae5a4SJacob Faibussowitsch PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm)
5232d71ae5a4SJacob Faibussowitsch {
523363a16f15SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *)dm->data;
52341fca310dSJames Wright   const PetscSF *face_sfs;
52351fca310dSJames Wright   PetscInt       num_face_sfs;
523663a16f15SMatthew G. Knepley 
523763a16f15SMatthew G. Knepley   PetscFunctionBegin;
523863a16f15SMatthew G. Knepley   mesh->refct++;
523963a16f15SMatthew G. Knepley   (*newdm)->data = mesh;
52401fca310dSJames Wright   PetscCall(DMPlexGetIsoperiodicFaceSF(dm, &num_face_sfs, &face_sfs));
52411fca310dSJames Wright   PetscCall(DMPlexSetIsoperiodicFaceSF(*newdm, num_face_sfs, (PetscSF *)face_sfs));
52429566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)*newdm, DMPLEX));
52439566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(*newdm));
52443ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
524563a16f15SMatthew G. Knepley }
524663a16f15SMatthew G. Knepley 
52478818961aSMatthew G Knepley /*MC
5248a1cb98faSBarry Smith   DMPLEX = "plex" - A `DM` object that encapsulates an unstructured mesh, or CW Complex, which can be expressed using a Hasse Diagram.
524920f4b53cSBarry Smith                     In the local representation, `Vec`s contain all unknowns in the interior and shared boundary. This is
52508818961aSMatthew G Knepley                     specified by a PetscSection object. Ownership in the global representation is determined by
5251a1cb98faSBarry Smith                     ownership of the underlying `DMPLEX` points. This is specified by another `PetscSection` object.
52528818961aSMatthew G Knepley 
5253e5893cccSMatthew G. Knepley   Options Database Keys:
5254250712c9SMatthew G. Knepley + -dm_refine_pre                     - Refine mesh before distribution
5255250712c9SMatthew G. Knepley + -dm_refine_uniform_pre             - Choose uniform or generator-based refinement
5256250712c9SMatthew G. Knepley + -dm_refine_volume_limit_pre        - Cell volume limit after pre-refinement using generator
5257250712c9SMatthew G. Knepley . -dm_distribute                     - Distribute mesh across processes
5258250712c9SMatthew G. Knepley . -dm_distribute_overlap             - Number of cells to overlap for distribution
5259250712c9SMatthew G. Knepley . -dm_refine                         - Refine mesh after distribution
5260c3db174cSMatthew G. Knepley . -dm_localize <bool>                - Whether to localize coordinates for periodic meshes
5261c3db174cSMatthew G. Knepley . -dm_sparse_localize <bool>         - Whether to only localize cells on the periodic boundary
5262250712c9SMatthew G. Knepley . -dm_plex_hash_location             - Use grid hashing for point location
5263ddce0771SMatthew G. Knepley . -dm_plex_hash_box_faces <n,m,p>    - The number of divisions in each direction of the grid hash
5264f12cf164SMatthew G. Knepley . -dm_plex_partition_balance         - Attempt to evenly divide points on partition boundary between processes
5265f12cf164SMatthew G. Knepley . -dm_plex_remesh_bd                 - Allow changes to the boundary on remeshing
5266d5b43468SJose E. Roman . -dm_plex_max_projection_height     - Maximum mesh point height used to project locally
5267f12cf164SMatthew G. Knepley . -dm_plex_regular_refinement        - Use special nested projection algorithm for regular refinement
5268d02c7345SMatthew G. Knepley . -dm_plex_reorder_section           - Use specialized blocking if available
5269aaa8cc7dSPierre Jolivet . -dm_plex_check_all                 - Perform all checks below
5270f12cf164SMatthew G. Knepley . -dm_plex_check_symmetry            - Check that the adjacency information in the mesh is symmetric
5271f12cf164SMatthew G. Knepley . -dm_plex_check_skeleton <celltype> - Check that each cell has the correct number of vertices
5272f12cf164SMatthew 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
5273f12cf164SMatthew G. Knepley . -dm_plex_check_geometry            - Check that cells have positive volume
5274f12cf164SMatthew G. Knepley . -dm_view :mesh.tex:ascii_latex     - View the mesh in LaTeX/TikZ
5275e5893cccSMatthew G. Knepley . -dm_plex_view_scale <num>          - Scale the TikZ
52765962854dSMatthew G. Knepley . -dm_plex_print_fem <num>           - View FEM assembly information, such as element vectors and matrices
52775962854dSMatthew G. Knepley - -dm_plex_print_fvm <num>           - View FVM assembly information, such as flux updates
5278e5893cccSMatthew G. Knepley 
52798818961aSMatthew G Knepley   Level: intermediate
52808818961aSMatthew G Knepley 
52811cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMType`, `DMPlexCreate()`, `DMCreate()`, `DMSetType()`, `PetscSection`
52828818961aSMatthew G Knepley M*/
52838818961aSMatthew G Knepley 
5284d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode DMCreate_Plex(DM dm)
5285d71ae5a4SJacob Faibussowitsch {
5286552f7358SJed Brown   DM_Plex *mesh;
5287412e9a14SMatthew G. Knepley   PetscInt unit;
5288552f7358SJed Brown 
5289552f7358SJed Brown   PetscFunctionBegin;
5290f39ec787SMatthew G. Knepley   PetscCall(PetscCitationsRegister(PlexCitation, &Plexcite));
5291552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52924dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&mesh));
5293adc21957SMatthew G. Knepley   dm->reorderSection = DM_REORDER_DEFAULT_NOTSET;
5294552f7358SJed Brown   dm->data           = mesh;
5295552f7358SJed Brown 
5296552f7358SJed Brown   mesh->refct = 1;
52979566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->coneSection));
52989566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->supportSection));
5299552f7358SJed Brown   mesh->refinementUniform      = PETSC_TRUE;
5300552f7358SJed Brown   mesh->refinementLimit        = -1.0;
5301e600fa54SMatthew G. Knepley   mesh->distDefault            = PETSC_TRUE;
5302adc21957SMatthew G. Knepley   mesh->reorderDefault         = DM_REORDER_DEFAULT_NOTSET;
53031d1f2f2aSksagiyam   mesh->distributionName       = NULL;
53047d0f5628SVaclav Hapla   mesh->interpolated           = DMPLEX_INTERPOLATED_INVALID;
53057d0f5628SVaclav Hapla   mesh->interpolatedCollective = DMPLEX_INTERPOLATED_INVALID;
5306552f7358SJed Brown 
53079566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerCreate(PetscObjectComm((PetscObject)dm), &mesh->partitioner));
53082e62ab5aSMatthew G. Knepley   mesh->remeshBd = PETSC_FALSE;
5309d9deefdfSMatthew G. Knepley 
53108865f1eaSKarl Rupp   for (unit = 0; unit < NUM_PETSC_UNITS; ++unit) mesh->scale[unit] = 1.0;
5311552f7358SJed Brown 
5312df0420ecSMatthew G. Knepley   mesh->depthState    = -1;
5313ba2698f1SMatthew G. Knepley   mesh->celltypeState = -1;
53146113b454SMatthew G. Knepley   mesh->printTol      = 1.0e-10;
5315c29ce622SStefano Zampini   mesh->nonempty_comm = MPI_COMM_SELF;
5316552f7358SJed Brown 
53179566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(dm));
53183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5319552f7358SJed Brown }
5320552f7358SJed Brown 
5321552f7358SJed Brown /*@
5322a1cb98faSBarry Smith   DMPlexCreate - Creates a `DMPLEX` object, which encapsulates an unstructured mesh, or CW complex, which can be expressed using a Hasse Diagram.
5323552f7358SJed Brown 
5324d083f849SBarry Smith   Collective
5325552f7358SJed Brown 
5326552f7358SJed Brown   Input Parameter:
5327a1cb98faSBarry Smith . comm - The communicator for the `DMPLEX` object
5328552f7358SJed Brown 
5329552f7358SJed Brown   Output Parameter:
5330a1cb98faSBarry Smith . mesh - The `DMPLEX` object
5331552f7358SJed Brown 
5332552f7358SJed Brown   Level: beginner
5333552f7358SJed Brown 
533442747ad1SJacob Faibussowitsch .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMType`, `DMCreate()`, `DMSetType()`
5335552f7358SJed Brown @*/
5336d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreate(MPI_Comm comm, DM *mesh)
5337d71ae5a4SJacob Faibussowitsch {
5338552f7358SJed Brown   PetscFunctionBegin;
53394f572ea9SToby Isaac   PetscAssertPointer(mesh, 2);
53409566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, mesh));
53419566063dSJacob Faibussowitsch   PetscCall(DMSetType(*mesh, DMPLEX));
53423ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5343552f7358SJed Brown }
5344552f7358SJed Brown 
5345b09969d6SVaclav Hapla /*@C
5346b0fe842aSMatthew 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
5347a1cb98faSBarry Smith 
534820f4b53cSBarry Smith   Collective; No Fortran Support
5349b09969d6SVaclav Hapla 
5350b09969d6SVaclav Hapla   Input Parameters:
5351a1cb98faSBarry Smith + dm          - The `DM`
5352b09969d6SVaclav Hapla . numCells    - The number of cells owned by this process
5353a1cb98faSBarry Smith . numVertices - The number of vertices to be owned by this process, or `PETSC_DECIDE`
5354a1cb98faSBarry Smith . NVertices   - The global number of vertices, or `PETSC_DETERMINE`
5355b09969d6SVaclav Hapla . numCorners  - The number of vertices for each cell
53565e488331SVaclav Hapla - cells       - An array of numCells*numCorners numbers, the global vertex numbers for each cell
5357b09969d6SVaclav Hapla 
5358be8c289dSNicolas Barral   Output Parameters:
5359a1cb98faSBarry Smith + vertexSF         - (Optional) `PetscSF` describing complete vertex ownership
5360be8c289dSNicolas Barral - verticesAdjSaved - (Optional) vertex adjacency array
5361b09969d6SVaclav Hapla 
5362b09969d6SVaclav Hapla   Level: advanced
5363b09969d6SVaclav Hapla 
5364a1cb98faSBarry Smith   Notes:
5365a1cb98faSBarry Smith   Two triangles sharing a face
5366a1cb98faSBarry Smith .vb
5367a1cb98faSBarry Smith 
5368a1cb98faSBarry Smith         2
5369a1cb98faSBarry Smith       / | \
5370a1cb98faSBarry Smith      /  |  \
5371a1cb98faSBarry Smith     /   |   \
5372a1cb98faSBarry Smith    0  0 | 1  3
5373a1cb98faSBarry Smith     \   |   /
5374a1cb98faSBarry Smith      \  |  /
5375a1cb98faSBarry Smith       \ | /
5376a1cb98faSBarry Smith         1
5377a1cb98faSBarry Smith .ve
5378a1cb98faSBarry Smith   would have input
5379a1cb98faSBarry Smith .vb
5380a1cb98faSBarry Smith   numCells = 2, numVertices = 4
5381a1cb98faSBarry Smith   cells = [0 1 2  1 3 2]
5382a1cb98faSBarry Smith .ve
5383a1cb98faSBarry Smith   which would result in the `DMPLEX`
5384a1cb98faSBarry Smith .vb
5385a1cb98faSBarry Smith 
5386a1cb98faSBarry Smith         4
5387a1cb98faSBarry Smith       / | \
5388a1cb98faSBarry Smith      /  |  \
5389a1cb98faSBarry Smith     /   |   \
5390a1cb98faSBarry Smith    2  0 | 1  5
5391a1cb98faSBarry Smith     \   |   /
5392a1cb98faSBarry Smith      \  |  /
5393a1cb98faSBarry Smith       \ | /
5394a1cb98faSBarry Smith         3
5395a1cb98faSBarry Smith .ve
5396a1cb98faSBarry Smith 
5397a1cb98faSBarry Smith   Vertices are implicitly numbered consecutively 0,...,NVertices.
5398a1cb98faSBarry Smith   Each rank owns a chunk of numVertices consecutive vertices.
5399a1cb98faSBarry Smith   If numVertices is `PETSC_DECIDE`, PETSc will distribute them as evenly as possible using PetscLayout.
5400a1cb98faSBarry Smith   If NVertices is `PETSC_DETERMINE` and numVertices is PETSC_DECIDE, NVertices is computed by PETSc as the maximum vertex index in cells + 1.
5401a1cb98faSBarry Smith   If only NVertices is `PETSC_DETERMINE`, it is computed as the sum of numVertices over all ranks.
5402a1cb98faSBarry Smith 
5403a1cb98faSBarry Smith   The cell distribution is arbitrary non-overlapping, independent of the vertex distribution.
5404a1cb98faSBarry Smith 
54051cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildFromCellList()`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildCoordinatesFromCellListParallel()`,
5406a1cb98faSBarry Smith           `PetscSF`
5407b09969d6SVaclav Hapla @*/
5408d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildFromCellListParallel(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt NVertices, PetscInt numCorners, const PetscInt cells[], PetscSF *vertexSF, PetscInt **verticesAdjSaved)
5409d71ae5a4SJacob Faibussowitsch {
54102464107aSksagiyam   PetscSF     sfPoint;
54112464107aSksagiyam   PetscLayout layout;
541282fb893eSVaclav Hapla   PetscInt    numVerticesAdj, *verticesAdj, *cones, c, p;
5413a47d0d45SMatthew G. Knepley 
5414a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
541525b6865aSVaclav Hapla   PetscValidLogicalCollectiveInt(dm, NVertices, 4);
54169566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
541725b6865aSVaclav Hapla   /* Get/check global number of vertices */
541825b6865aSVaclav Hapla   {
541925b6865aSVaclav Hapla     PetscInt       NVerticesInCells, i;
542025b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
542125b6865aSVaclav Hapla 
542225b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
54231690c2aeSBarry Smith     NVerticesInCells = PETSC_INT_MIN;
54249371c9d4SSatish Balay     for (i = 0; i < len; i++)
54259371c9d4SSatish Balay       if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
542625b6865aSVaclav Hapla     ++NVerticesInCells;
5427462c564dSBarry Smith     PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &NVerticesInCells, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
542825b6865aSVaclav Hapla 
542925b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE && NVertices == PETSC_DECIDE) NVertices = NVerticesInCells;
54309371c9d4SSatish Balay     else
54319371c9d4SSatish 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);
543225b6865aSVaclav Hapla   }
54339079aca8SVaclav Hapla   /* Count locally unique vertices */
54349079aca8SVaclav Hapla   {
54359079aca8SVaclav Hapla     PetscHSetI vhash;
54369079aca8SVaclav Hapla     PetscInt   off = 0;
54379079aca8SVaclav Hapla 
54389566063dSJacob Faibussowitsch     PetscCall(PetscHSetICreate(&vhash));
5439a47d0d45SMatthew G. Knepley     for (c = 0; c < numCells; ++c) {
544048a46eb9SPierre Jolivet       for (p = 0; p < numCorners; ++p) PetscCall(PetscHSetIAdd(vhash, cells[c * numCorners + p]));
5441a47d0d45SMatthew G. Knepley     }
54429566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetSize(vhash, &numVerticesAdj));
54439566063dSJacob Faibussowitsch     if (!verticesAdjSaved) PetscCall(PetscMalloc1(numVerticesAdj, &verticesAdj));
5444ad540459SPierre Jolivet     else verticesAdj = *verticesAdjSaved;
54459566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetElems(vhash, &off, verticesAdj));
54469566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&vhash));
544763a3b9bcSJacob Faibussowitsch     PetscCheck(off == numVerticesAdj, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid number of local vertices %" PetscInt_FMT " should be %" PetscInt_FMT, off, numVerticesAdj);
5448a47d0d45SMatthew G. Knepley   }
54499566063dSJacob Faibussowitsch   PetscCall(PetscSortInt(numVerticesAdj, verticesAdj));
5450a47d0d45SMatthew G. Knepley   /* Create cones */
54519566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, 0, numCells + numVerticesAdj));
54529566063dSJacob Faibussowitsch   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, numCorners));
54539566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm));
54549566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCones(dm, &cones));
5455a47d0d45SMatthew G. Knepley   for (c = 0; c < numCells; ++c) {
5456a47d0d45SMatthew G. Knepley     for (p = 0; p < numCorners; ++p) {
5457a47d0d45SMatthew G. Knepley       const PetscInt gv = cells[c * numCorners + p];
5458a47d0d45SMatthew G. Knepley       PetscInt       lv;
5459a47d0d45SMatthew G. Knepley 
54609079aca8SVaclav Hapla       /* Positions within verticesAdj form 0-based local vertex numbering;
54619079aca8SVaclav Hapla          we need to shift it by numCells to get correct DAG points (cells go first) */
54629566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv));
546363a3b9bcSJacob Faibussowitsch       PetscCheck(lv >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not find global vertex %" PetscInt_FMT " in local connectivity", gv);
5464961cfab0SVaclav Hapla       cones[c * numCorners + p] = lv + numCells;
5465a47d0d45SMatthew G. Knepley     }
5466a47d0d45SMatthew G. Knepley   }
54672464107aSksagiyam   /* Build point sf */
54689566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(PetscObjectComm((PetscObject)dm), &layout));
54699566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetSize(layout, NVertices));
54709566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, numVertices));
54719566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
54729566063dSJacob Faibussowitsch   PetscCall(PetscSFCreateByMatchingIndices(layout, numVerticesAdj, verticesAdj, NULL, numCells, numVerticesAdj, verticesAdj, NULL, numCells, vertexSF, &sfPoint));
54739566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
54749566063dSJacob Faibussowitsch   if (!verticesAdjSaved) PetscCall(PetscFree(verticesAdj));
54759566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)sfPoint, "point SF"));
54762464107aSksagiyam   if (dm->sf) {
54772464107aSksagiyam     const char *prefix;
54782464107aSksagiyam 
54799566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm->sf, &prefix));
54809566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)sfPoint, prefix));
54812464107aSksagiyam   }
54829566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dm, sfPoint));
54839566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&sfPoint));
5484f4f49eeaSPierre Jolivet   if (vertexSF) PetscCall(PetscObjectSetName((PetscObject)*vertexSF, "Vertex Ownership SF"));
5485a47d0d45SMatthew G. Knepley   /* Fill in the rest of the topology structure */
54869566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
54879566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
54889566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
54893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5490a47d0d45SMatthew G. Knepley }
5491a47d0d45SMatthew G. Knepley 
5492b0fe842aSMatthew G. Knepley /*@C
5493b0fe842aSMatthew G. Knepley   DMPlexBuildFromCellSectionParallel - Build distributed `DMPLEX` topology from a list of vertices for each cell (common mesh generator output) allowing multiple celltypes
5494b0fe842aSMatthew G. Knepley 
5495b0fe842aSMatthew G. Knepley   Collective; No Fortran Support
5496b0fe842aSMatthew G. Knepley 
5497b0fe842aSMatthew G. Knepley   Input Parameters:
5498b0fe842aSMatthew G. Knepley + dm          - The `DM`
5499b0fe842aSMatthew G. Knepley . numCells    - The number of cells owned by this process
5500b0fe842aSMatthew G. Knepley . numVertices - The number of vertices to be owned by this process, or `PETSC_DECIDE`
5501b0fe842aSMatthew G. Knepley . NVertices   - The global number of vertices, or `PETSC_DETERMINE`
5502b0fe842aSMatthew G. Knepley . cellSection - The `PetscSection` giving the number of vertices for each cell (layout of cells)
5503b0fe842aSMatthew G. Knepley - cells       - An array of the global vertex numbers for each cell
5504b0fe842aSMatthew G. Knepley 
5505b0fe842aSMatthew G. Knepley   Output Parameters:
5506b0fe842aSMatthew G. Knepley + vertexSF         - (Optional) `PetscSF` describing complete vertex ownership
5507b0fe842aSMatthew G. Knepley - verticesAdjSaved - (Optional) vertex adjacency array
5508b0fe842aSMatthew G. Knepley 
5509b0fe842aSMatthew G. Knepley   Level: advanced
5510b0fe842aSMatthew G. Knepley 
5511b0fe842aSMatthew G. Knepley   Notes:
5512b0fe842aSMatthew G. Knepley   A triangle and quadrilateral sharing a face
5513b0fe842aSMatthew G. Knepley .vb
5514b0fe842aSMatthew G. Knepley         2----------3
5515b0fe842aSMatthew G. Knepley       / |          |
5516b0fe842aSMatthew G. Knepley      /  |          |
5517b0fe842aSMatthew G. Knepley     /   |          |
5518b0fe842aSMatthew G. Knepley    0  0 |     1    |
5519b0fe842aSMatthew G. Knepley     \   |          |
5520b0fe842aSMatthew G. Knepley      \  |          |
5521b0fe842aSMatthew G. Knepley       \ |          |
5522b0fe842aSMatthew G. Knepley         1----------4
5523b0fe842aSMatthew G. Knepley .ve
5524b0fe842aSMatthew G. Knepley   would have input
5525b0fe842aSMatthew G. Knepley .vb
5526b0fe842aSMatthew G. Knepley   numCells = 2, numVertices = 5
5527b0fe842aSMatthew G. Knepley   cells = [0 1 2  1 4 3 2]
5528b0fe842aSMatthew G. Knepley .ve
5529b0fe842aSMatthew G. Knepley   which would result in the `DMPLEX`
5530b0fe842aSMatthew G. Knepley .vb
5531b0fe842aSMatthew G. Knepley         4----------5
5532b0fe842aSMatthew G. Knepley       / |          |
5533b0fe842aSMatthew G. Knepley      /  |          |
5534b0fe842aSMatthew G. Knepley     /   |          |
5535b0fe842aSMatthew G. Knepley    2  0 |     1    |
5536b0fe842aSMatthew G. Knepley     \   |          |
5537b0fe842aSMatthew G. Knepley      \  |          |
5538b0fe842aSMatthew G. Knepley       \ |          |
5539b0fe842aSMatthew G. Knepley         3----------6
5540b0fe842aSMatthew G. Knepley .ve
5541b0fe842aSMatthew G. Knepley 
5542b0fe842aSMatthew G. Knepley   Vertices are implicitly numbered consecutively 0,...,NVertices.
5543b0fe842aSMatthew G. Knepley   Each rank owns a chunk of numVertices consecutive vertices.
5544b0fe842aSMatthew G. Knepley   If numVertices is `PETSC_DECIDE`, PETSc will distribute them as evenly as possible using PetscLayout.
5545b0fe842aSMatthew 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.
5546b0fe842aSMatthew G. Knepley   If only NVertices is `PETSC_DETERMINE`, it is computed as the sum of numVertices over all ranks.
5547b0fe842aSMatthew G. Knepley 
5548b0fe842aSMatthew G. Knepley   The cell distribution is arbitrary non-overlapping, independent of the vertex distribution.
5549b0fe842aSMatthew G. Knepley 
5550b0fe842aSMatthew G. Knepley .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildFromCellListParallel()`, `DMPlexCreateFromCellSectionParallel()`, `DMPlexBuildCoordinatesFromCellListParallel()`,
5551b0fe842aSMatthew G. Knepley           `PetscSF`
5552b0fe842aSMatthew G. Knepley @*/
5553b0fe842aSMatthew G. Knepley PetscErrorCode DMPlexBuildFromCellSectionParallel(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt NVertices, PetscSection cellSection, const PetscInt cells[], PetscSF *vertexSF, PetscInt **verticesAdjSaved)
5554b0fe842aSMatthew G. Knepley {
5555b0fe842aSMatthew G. Knepley   PetscSF     sfPoint;
5556b0fe842aSMatthew G. Knepley   PetscLayout layout;
5557b0fe842aSMatthew G. Knepley   PetscInt    numVerticesAdj, *verticesAdj, *cones, cStart, cEnd, len;
5558b0fe842aSMatthew G. Knepley 
5559b0fe842aSMatthew G. Knepley   PetscFunctionBegin;
5560b0fe842aSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, NVertices, 4);
5561b0fe842aSMatthew G. Knepley   PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
5562b0fe842aSMatthew G. Knepley   PetscCall(PetscSectionGetChart(cellSection, &cStart, &cEnd));
5563b0fe842aSMatthew G. Knepley   PetscCall(PetscSectionGetStorageSize(cellSection, &len));
5564b0fe842aSMatthew 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);
5565b0fe842aSMatthew G. Knepley   /* Get/check global number of vertices */
5566b0fe842aSMatthew G. Knepley   {
5567b0fe842aSMatthew G. Knepley     PetscInt NVerticesInCells;
5568b0fe842aSMatthew G. Knepley 
5569b0fe842aSMatthew G. Knepley     /* NVerticesInCells = max(cells) + 1 */
5570b0fe842aSMatthew G. Knepley     NVerticesInCells = PETSC_MIN_INT;
5571b0fe842aSMatthew G. Knepley     for (PetscInt i = 0; i < len; i++)
5572b0fe842aSMatthew G. Knepley       if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
5573b0fe842aSMatthew G. Knepley     ++NVerticesInCells;
5574b0fe842aSMatthew G. Knepley     PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &NVerticesInCells, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
5575b0fe842aSMatthew G. Knepley 
5576b0fe842aSMatthew G. Knepley     if (numVertices == PETSC_DECIDE && NVertices == PETSC_DECIDE) NVertices = NVerticesInCells;
5577b0fe842aSMatthew G. Knepley     else
5578b0fe842aSMatthew 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);
5579b0fe842aSMatthew G. Knepley   }
5580b0fe842aSMatthew G. Knepley   /* Count locally unique vertices */
5581b0fe842aSMatthew G. Knepley   {
5582b0fe842aSMatthew G. Knepley     PetscHSetI vhash;
5583b0fe842aSMatthew G. Knepley     PetscInt   off = 0;
5584b0fe842aSMatthew G. Knepley 
5585b0fe842aSMatthew G. Knepley     PetscCall(PetscHSetICreate(&vhash));
5586b0fe842aSMatthew G. Knepley     for (PetscInt i = 0; i < len; i++) PetscCall(PetscHSetIAdd(vhash, cells[i]));
5587b0fe842aSMatthew G. Knepley     PetscCall(PetscHSetIGetSize(vhash, &numVerticesAdj));
5588b0fe842aSMatthew G. Knepley     if (!verticesAdjSaved) PetscCall(PetscMalloc1(numVerticesAdj, &verticesAdj));
5589b0fe842aSMatthew G. Knepley     else verticesAdj = *verticesAdjSaved;
5590b0fe842aSMatthew G. Knepley     PetscCall(PetscHSetIGetElems(vhash, &off, verticesAdj));
5591b0fe842aSMatthew G. Knepley     PetscCall(PetscHSetIDestroy(&vhash));
5592b0fe842aSMatthew 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);
5593b0fe842aSMatthew G. Knepley   }
5594b0fe842aSMatthew G. Knepley   PetscCall(PetscSortInt(numVerticesAdj, verticesAdj));
5595b0fe842aSMatthew G. Knepley   /* Create cones */
5596b0fe842aSMatthew G. Knepley   PetscCall(DMPlexSetChart(dm, 0, numCells + numVerticesAdj));
5597b0fe842aSMatthew G. Knepley   for (PetscInt c = 0; c < numCells; ++c) {
5598b0fe842aSMatthew G. Knepley     PetscInt dof;
5599b0fe842aSMatthew G. Knepley 
5600b0fe842aSMatthew G. Knepley     PetscCall(PetscSectionGetDof(cellSection, c, &dof));
5601b0fe842aSMatthew G. Knepley     PetscCall(DMPlexSetConeSize(dm, c, dof));
5602b0fe842aSMatthew G. Knepley   }
5603b0fe842aSMatthew G. Knepley   PetscCall(DMSetUp(dm));
5604b0fe842aSMatthew G. Knepley   PetscCall(DMPlexGetCones(dm, &cones));
5605b0fe842aSMatthew G. Knepley   for (PetscInt c = 0; c < numCells; ++c) {
5606b0fe842aSMatthew G. Knepley     PetscInt dof, off;
5607b0fe842aSMatthew G. Knepley 
5608b0fe842aSMatthew G. Knepley     PetscCall(PetscSectionGetDof(cellSection, c, &dof));
5609b0fe842aSMatthew G. Knepley     PetscCall(PetscSectionGetOffset(cellSection, c, &off));
5610b0fe842aSMatthew G. Knepley     for (PetscInt p = off; p < off + dof; ++p) {
5611b0fe842aSMatthew G. Knepley       const PetscInt gv = cells[p];
5612b0fe842aSMatthew G. Knepley       PetscInt       lv;
5613b0fe842aSMatthew G. Knepley 
5614b0fe842aSMatthew G. Knepley       /* Positions within verticesAdj form 0-based local vertex numbering;
5615b0fe842aSMatthew G. Knepley          we need to shift it by numCells to get correct DAG points (cells go first) */
5616b0fe842aSMatthew G. Knepley       PetscCall(PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv));
5617b0fe842aSMatthew G. Knepley       PetscCheck(lv >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not find global vertex %" PetscInt_FMT " in local connectivity", gv);
5618b0fe842aSMatthew G. Knepley       cones[p] = lv + numCells;
5619b0fe842aSMatthew G. Knepley     }
5620b0fe842aSMatthew G. Knepley   }
5621b0fe842aSMatthew G. Knepley   /* Build point sf */
5622b0fe842aSMatthew G. Knepley   PetscCall(PetscLayoutCreate(PetscObjectComm((PetscObject)dm), &layout));
5623b0fe842aSMatthew G. Knepley   PetscCall(PetscLayoutSetSize(layout, NVertices));
5624b0fe842aSMatthew G. Knepley   PetscCall(PetscLayoutSetLocalSize(layout, numVertices));
5625b0fe842aSMatthew G. Knepley   PetscCall(PetscLayoutSetBlockSize(layout, 1));
5626b0fe842aSMatthew G. Knepley   PetscCall(PetscSFCreateByMatchingIndices(layout, numVerticesAdj, verticesAdj, NULL, numCells, numVerticesAdj, verticesAdj, NULL, numCells, vertexSF, &sfPoint));
5627b0fe842aSMatthew G. Knepley   PetscCall(PetscLayoutDestroy(&layout));
5628b0fe842aSMatthew G. Knepley   if (!verticesAdjSaved) PetscCall(PetscFree(verticesAdj));
5629b0fe842aSMatthew G. Knepley   PetscCall(PetscObjectSetName((PetscObject)sfPoint, "point SF"));
5630b0fe842aSMatthew G. Knepley   if (dm->sf) {
5631b0fe842aSMatthew G. Knepley     const char *prefix;
5632b0fe842aSMatthew G. Knepley 
5633b0fe842aSMatthew G. Knepley     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm->sf, &prefix));
5634b0fe842aSMatthew G. Knepley     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)sfPoint, prefix));
5635b0fe842aSMatthew G. Knepley   }
5636b0fe842aSMatthew G. Knepley   PetscCall(DMSetPointSF(dm, sfPoint));
5637b0fe842aSMatthew G. Knepley   PetscCall(PetscSFDestroy(&sfPoint));
5638b0fe842aSMatthew G. Knepley   if (vertexSF) PetscCall(PetscObjectSetName((PetscObject)*vertexSF, "Vertex Ownership SF"));
5639b0fe842aSMatthew G. Knepley   /* Fill in the rest of the topology structure */
5640b0fe842aSMatthew G. Knepley   PetscCall(DMPlexSymmetrize(dm));
5641b0fe842aSMatthew G. Knepley   PetscCall(DMPlexStratify(dm));
5642b0fe842aSMatthew G. Knepley   PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
5643b0fe842aSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
5644b0fe842aSMatthew G. Knepley }
5645b0fe842aSMatthew G. Knepley 
5646cc4c1da9SBarry Smith /*@
5647a1cb98faSBarry Smith   DMPlexBuildCoordinatesFromCellListParallel - Build `DM` coordinates from a list of coordinates for each owned vertex (common mesh generator output)
5648a1cb98faSBarry Smith 
564920f4b53cSBarry Smith   Collective; No Fortran Support
5650b09969d6SVaclav Hapla 
5651b09969d6SVaclav Hapla   Input Parameters:
5652a1cb98faSBarry Smith + dm           - The `DM`
5653b09969d6SVaclav Hapla . spaceDim     - The spatial dimension used for coordinates
5654a1cb98faSBarry Smith . sfVert       - `PetscSF` describing complete vertex ownership
5655b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
5656b09969d6SVaclav Hapla 
5657b09969d6SVaclav Hapla   Level: advanced
5658b09969d6SVaclav Hapla 
56591cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildFromCellListParallel()`
5660b09969d6SVaclav Hapla @*/
5661d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildCoordinatesFromCellListParallel(DM dm, PetscInt spaceDim, PetscSF sfVert, const PetscReal vertexCoords[])
5662d71ae5a4SJacob Faibussowitsch {
5663a47d0d45SMatthew G. Knepley   PetscSection coordSection;
5664a47d0d45SMatthew G. Knepley   Vec          coordinates;
5665a47d0d45SMatthew G. Knepley   PetscScalar *coords;
56661edcf0b2SVaclav Hapla   PetscInt     numVertices, numVerticesAdj, coordSize, v, vStart, vEnd;
5667835f2295SStefano Zampini   PetscMPIInt  spaceDimi;
5668a47d0d45SMatthew G. Knepley 
5669a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
56709566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
56719566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
56721dca8a05SBarry Smith   PetscCheck(vStart >= 0 && vEnd >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
56739566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, spaceDim));
56749566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfVert, &numVertices, &numVerticesAdj, NULL, NULL));
56751dca8a05SBarry 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);
56769566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
56779566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
56789566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, spaceDim));
56799566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, vStart, vEnd));
56801edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
56819566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, spaceDim));
56829566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, spaceDim));
5683a47d0d45SMatthew G. Knepley   }
56849566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
56859566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
56869566063dSJacob Faibussowitsch   PetscCall(VecCreate(PetscObjectComm((PetscObject)dm), &coordinates));
56879566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, spaceDim));
56889566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
56899566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
56909566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
56919566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
5692a47d0d45SMatthew G. Knepley   {
5693a47d0d45SMatthew G. Knepley     MPI_Datatype coordtype;
5694a47d0d45SMatthew G. Knepley 
5695a47d0d45SMatthew G. Knepley     /* Need a temp buffer for coords if we have complex/single */
5696835f2295SStefano Zampini     PetscCall(PetscMPIIntCast(spaceDim, &spaceDimi));
5697835f2295SStefano Zampini     PetscCallMPI(MPI_Type_contiguous(spaceDimi, MPIU_SCALAR, &coordtype));
56989566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_commit(&coordtype));
569921016a8bSBarry Smith #if defined(PETSC_USE_COMPLEX)
570021016a8bSBarry Smith     {
570121016a8bSBarry Smith       PetscScalar *svertexCoords;
570221016a8bSBarry Smith       PetscInt     i;
57039566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numVertices * spaceDim, &svertexCoords));
57043612f820SVaclav Hapla       for (i = 0; i < numVertices * spaceDim; i++) svertexCoords[i] = vertexCoords[i];
57059566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(sfVert, coordtype, svertexCoords, coords, MPI_REPLACE));
57069566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(sfVert, coordtype, svertexCoords, coords, MPI_REPLACE));
57079566063dSJacob Faibussowitsch       PetscCall(PetscFree(svertexCoords));
570821016a8bSBarry Smith     }
570921016a8bSBarry Smith #else
57109566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sfVert, coordtype, vertexCoords, coords, MPI_REPLACE));
57119566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sfVert, coordtype, vertexCoords, coords, MPI_REPLACE));
571221016a8bSBarry Smith #endif
57139566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_free(&coordtype));
5714a47d0d45SMatthew G. Knepley   }
57159566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
57169566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
57179566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
57189566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
57193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5720a47d0d45SMatthew G. Knepley }
5721a47d0d45SMatthew G. Knepley 
5722c3edce3dSSatish Balay /*@
5723b0fe842aSMatthew 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
5724a1cb98faSBarry Smith 
5725a1cb98faSBarry Smith   Collective
5726a47d0d45SMatthew G. Knepley 
5727a47d0d45SMatthew G. Knepley   Input Parameters:
5728a47d0d45SMatthew G. Knepley + comm         - The communicator
5729a47d0d45SMatthew G. Knepley . dim          - The topological dimension of the mesh
5730a47d0d45SMatthew G. Knepley . numCells     - The number of cells owned by this process
5731a1cb98faSBarry Smith . numVertices  - The number of vertices owned by this process, or `PETSC_DECIDE`
5732a1cb98faSBarry Smith . NVertices    - The global number of vertices, or `PETSC_DECIDE`
5733a47d0d45SMatthew G. Knepley . numCorners   - The number of vertices for each cell
5734a47d0d45SMatthew G. Knepley . interpolate  - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
5735a47d0d45SMatthew G. Knepley . cells        - An array of numCells*numCorners numbers, the global vertex numbers for each cell
5736a47d0d45SMatthew G. Knepley . spaceDim     - The spatial dimension used for coordinates
5737a47d0d45SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
5738a47d0d45SMatthew G. Knepley 
5739d8d19677SJose E. Roman   Output Parameters:
5740a1cb98faSBarry Smith + dm          - The `DM`
5741a1cb98faSBarry Smith . vertexSF    - (Optional) `PetscSF` describing complete vertex ownership
574260225df5SJacob Faibussowitsch - verticesAdj - (Optional) vertex adjacency array
5743a47d0d45SMatthew G. Knepley 
5744b09969d6SVaclav Hapla   Level: intermediate
5745a47d0d45SMatthew G. Knepley 
5746a1cb98faSBarry Smith   Notes:
5747a1cb98faSBarry Smith   This function is just a convenient sequence of `DMCreate()`, `DMSetType()`, `DMSetDimension()`,
5748a1cb98faSBarry Smith   `DMPlexBuildFromCellListParallel()`, `DMPlexInterpolate()`, `DMPlexBuildCoordinatesFromCellListParallel()`
5749a1cb98faSBarry Smith 
5750a1cb98faSBarry Smith   See `DMPlexBuildFromCellListParallel()` for an example and details about the topology-related parameters.
5751a1cb98faSBarry Smith 
5752a1cb98faSBarry Smith   See `DMPlexBuildCoordinatesFromCellListParallel()` for details about the geometry-related parameters.
5753a1cb98faSBarry Smith 
57541cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()`
5755a47d0d45SMatthew G. Knepley @*/
5756d71ae5a4SJacob 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)
5757d71ae5a4SJacob Faibussowitsch {
5758a47d0d45SMatthew G. Knepley   PetscSF sfVert;
5759a47d0d45SMatthew G. Knepley 
5760a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
57619566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
57629566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
5763a47d0d45SMatthew G. Knepley   PetscValidLogicalCollectiveInt(*dm, dim, 2);
5764064a246eSJacob Faibussowitsch   PetscValidLogicalCollectiveInt(*dm, spaceDim, 9);
57659566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
57669566063dSJacob Faibussowitsch   PetscCall(DMPlexBuildFromCellListParallel(*dm, numCells, numVertices, NVertices, numCorners, cells, &sfVert, verticesAdj));
5767a47d0d45SMatthew G. Knepley   if (interpolate) {
57685fd9971aSMatthew G. Knepley     DM idm;
5769a47d0d45SMatthew G. Knepley 
57709566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
57719566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
5772a47d0d45SMatthew G. Knepley     *dm = idm;
5773a47d0d45SMatthew G. Knepley   }
57749566063dSJacob Faibussowitsch   PetscCall(DMPlexBuildCoordinatesFromCellListParallel(*dm, spaceDim, sfVert, vertexCoords));
577518d54ad4SMichael Lange   if (vertexSF) *vertexSF = sfVert;
57769566063dSJacob Faibussowitsch   else PetscCall(PetscSFDestroy(&sfVert));
57773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5778a47d0d45SMatthew G. Knepley }
5779a47d0d45SMatthew G. Knepley 
5780cc4c1da9SBarry Smith /*@
5781b0fe842aSMatthew G. Knepley   DMPlexCreateFromCellSectionParallel - Create distributed `DMPLEX` from a list of vertices for each cell (common mesh generator output) and supports multiple celltypes
5782b0fe842aSMatthew G. Knepley 
5783b0fe842aSMatthew G. Knepley   Collective
5784b0fe842aSMatthew G. Knepley 
5785b0fe842aSMatthew G. Knepley   Input Parameters:
5786b0fe842aSMatthew G. Knepley + comm         - The communicator
5787b0fe842aSMatthew G. Knepley . dim          - The topological dimension of the mesh
5788b0fe842aSMatthew G. Knepley . numCells     - The number of cells owned by this process
5789b0fe842aSMatthew G. Knepley . numVertices  - The number of vertices owned by this process, or `PETSC_DECIDE`
5790b0fe842aSMatthew G. Knepley . NVertices    - The global number of vertices, or `PETSC_DECIDE`
5791b0fe842aSMatthew G. Knepley . cellSection  - The `PetscSection` giving the number of vertices for each cell (layout of cells)
5792b0fe842aSMatthew G. Knepley . interpolate  - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
5793b0fe842aSMatthew G. Knepley . cells        - An array of the global vertex numbers for each cell
5794b0fe842aSMatthew G. Knepley . spaceDim     - The spatial dimension used for coordinates
5795b0fe842aSMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
5796b0fe842aSMatthew G. Knepley 
5797b0fe842aSMatthew G. Knepley   Output Parameters:
5798b0fe842aSMatthew G. Knepley + dm          - The `DM`
5799b0fe842aSMatthew G. Knepley . vertexSF    - (Optional) `PetscSF` describing complete vertex ownership
5800b0fe842aSMatthew G. Knepley - verticesAdj - (Optional) vertex adjacency array
5801b0fe842aSMatthew G. Knepley 
5802b0fe842aSMatthew G. Knepley   Level: intermediate
5803b0fe842aSMatthew G. Knepley 
5804b0fe842aSMatthew G. Knepley   Notes:
5805b0fe842aSMatthew G. Knepley   This function is just a convenient sequence of `DMCreate()`, `DMSetType()`, `DMSetDimension()`,
5806b0fe842aSMatthew G. Knepley   `DMPlexBuildFromCellSectionParallel()`, `DMPlexInterpolate()`, `DMPlexBuildCoordinatesFromCellListParallel()`
5807b0fe842aSMatthew G. Knepley 
5808b0fe842aSMatthew G. Knepley   See `DMPlexBuildFromCellSectionParallel()` for an example and details about the topology-related parameters.
5809b0fe842aSMatthew G. Knepley 
5810b0fe842aSMatthew G. Knepley   See `DMPlexBuildCoordinatesFromCellListParallel()` for details about the geometry-related parameters.
5811b0fe842aSMatthew G. Knepley 
5812b0fe842aSMatthew G. Knepley .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()`
5813b0fe842aSMatthew G. Knepley @*/
5814b0fe842aSMatthew 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)
5815b0fe842aSMatthew G. Knepley {
5816b0fe842aSMatthew G. Knepley   PetscSF sfVert;
5817b0fe842aSMatthew G. Knepley 
5818b0fe842aSMatthew G. Knepley   PetscFunctionBegin;
5819b0fe842aSMatthew G. Knepley   PetscCall(DMCreate(comm, dm));
5820b0fe842aSMatthew G. Knepley   PetscCall(DMSetType(*dm, DMPLEX));
5821b0fe842aSMatthew G. Knepley   PetscValidLogicalCollectiveInt(*dm, dim, 2);
5822b0fe842aSMatthew G. Knepley   PetscValidLogicalCollectiveInt(*dm, spaceDim, 9);
5823b0fe842aSMatthew G. Knepley   PetscCall(DMSetDimension(*dm, dim));
5824b0fe842aSMatthew G. Knepley   PetscCall(DMPlexBuildFromCellSectionParallel(*dm, numCells, numVertices, NVertices, cellSection, cells, &sfVert, verticesAdj));
5825b0fe842aSMatthew G. Knepley   if (interpolate) {
5826b0fe842aSMatthew G. Knepley     DM idm;
5827b0fe842aSMatthew G. Knepley 
5828b0fe842aSMatthew G. Knepley     PetscCall(DMPlexInterpolate(*dm, &idm));
5829b0fe842aSMatthew G. Knepley     PetscCall(DMDestroy(dm));
5830b0fe842aSMatthew G. Knepley     *dm = idm;
5831b0fe842aSMatthew G. Knepley   }
5832b0fe842aSMatthew G. Knepley   PetscCall(DMPlexBuildCoordinatesFromCellListParallel(*dm, spaceDim, sfVert, vertexCoords));
5833b0fe842aSMatthew G. Knepley   if (vertexSF) *vertexSF = sfVert;
5834b0fe842aSMatthew G. Knepley   else PetscCall(PetscSFDestroy(&sfVert));
5835b0fe842aSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
5836b0fe842aSMatthew G. Knepley }
5837b0fe842aSMatthew G. Knepley 
5838b0fe842aSMatthew G. Knepley /*@
5839a1cb98faSBarry Smith   DMPlexBuildFromCellList - Build `DMPLEX` topology from a list of vertices for each cell (common mesh generator output)
5840a1cb98faSBarry Smith 
584120f4b53cSBarry Smith   Collective; No Fortran Support
58429298eaa6SMatthew G Knepley 
58439298eaa6SMatthew G Knepley   Input Parameters:
5844a1cb98faSBarry Smith + dm          - The `DM`
5845b09969d6SVaclav Hapla . numCells    - The number of cells owned by this process
5846a1cb98faSBarry Smith . numVertices - The number of vertices owned by this process, or `PETSC_DETERMINE`
58479298eaa6SMatthew G Knepley . numCorners  - The number of vertices for each cell
5848a3b724e8SBarry Smith - cells       - An array of `numCells` x `numCorners` numbers, the global vertex numbers for each cell
58499298eaa6SMatthew G Knepley 
5850b09969d6SVaclav Hapla   Level: advanced
58519298eaa6SMatthew G Knepley 
5852b09969d6SVaclav Hapla   Notes:
5853b09969d6SVaclav Hapla   Two triangles sharing a face
5854a1cb98faSBarry Smith .vb
58559298eaa6SMatthew G Knepley 
5856a1cb98faSBarry Smith         2
5857a1cb98faSBarry Smith       / | \
5858a1cb98faSBarry Smith      /  |  \
5859a1cb98faSBarry Smith     /   |   \
5860a1cb98faSBarry Smith    0  0 | 1  3
5861a1cb98faSBarry Smith     \   |   /
5862a1cb98faSBarry Smith      \  |  /
5863a1cb98faSBarry Smith       \ | /
5864a1cb98faSBarry Smith         1
5865a1cb98faSBarry Smith .ve
5866a1cb98faSBarry Smith   would have input
5867a1cb98faSBarry Smith .vb
5868a1cb98faSBarry Smith   numCells = 2, numVertices = 4
5869a1cb98faSBarry Smith   cells = [0 1 2  1 3 2]
5870a1cb98faSBarry Smith .ve
5871a1cb98faSBarry Smith   which would result in the `DMPLEX`
5872a1cb98faSBarry Smith .vb
5873a1cb98faSBarry Smith 
5874a1cb98faSBarry Smith         4
5875a1cb98faSBarry Smith       / | \
5876a1cb98faSBarry Smith      /  |  \
5877a1cb98faSBarry Smith     /   |   \
5878a1cb98faSBarry Smith    2  0 | 1  5
5879a1cb98faSBarry Smith     \   |   /
5880a1cb98faSBarry Smith      \  |  /
5881a1cb98faSBarry Smith       \ | /
5882a1cb98faSBarry Smith         3
5883a1cb98faSBarry Smith .ve
5884a1cb98faSBarry Smith 
5885a1cb98faSBarry Smith   If numVertices is `PETSC_DETERMINE`, it is computed by PETSc as the maximum vertex index in cells + 1.
588625b6865aSVaclav Hapla 
58871cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromCellListPetsc()`
5888b09969d6SVaclav Hapla @*/
5889d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildFromCellList(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const PetscInt cells[])
5890d71ae5a4SJacob Faibussowitsch {
5891961cfab0SVaclav Hapla   PetscInt *cones, c, p, dim;
5892b09969d6SVaclav Hapla 
5893b09969d6SVaclav Hapla   PetscFunctionBegin;
58949566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
58959566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
589625b6865aSVaclav Hapla   /* Get/check global number of vertices */
589725b6865aSVaclav Hapla   {
589825b6865aSVaclav Hapla     PetscInt       NVerticesInCells, i;
589925b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
590025b6865aSVaclav Hapla 
590125b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
59021690c2aeSBarry Smith     NVerticesInCells = PETSC_INT_MIN;
59039371c9d4SSatish Balay     for (i = 0; i < len; i++)
59049371c9d4SSatish Balay       if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
590525b6865aSVaclav Hapla     ++NVerticesInCells;
590625b6865aSVaclav Hapla 
590725b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE) numVertices = NVerticesInCells;
59089371c9d4SSatish Balay     else
59099371c9d4SSatish 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);
591025b6865aSVaclav Hapla   }
59119566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
591248a46eb9SPierre Jolivet   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, numCorners));
59139566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm));
59149566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCones(dm, &cones));
5915b09969d6SVaclav Hapla   for (c = 0; c < numCells; ++c) {
5916ad540459SPierre Jolivet     for (p = 0; p < numCorners; ++p) cones[c * numCorners + p] = cells[c * numCorners + p] + numCells;
5917b09969d6SVaclav Hapla   }
59189566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
59199566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
59209566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
59213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5922b09969d6SVaclav Hapla }
5923b09969d6SVaclav Hapla 
5924cc4c1da9SBarry Smith /*@
5925a1cb98faSBarry Smith   DMPlexBuildCoordinatesFromCellList - Build `DM` coordinates from a list of coordinates for each owned vertex (common mesh generator output)
5926a1cb98faSBarry Smith 
5927cc4c1da9SBarry Smith   Collective
5928b09969d6SVaclav Hapla 
5929b09969d6SVaclav Hapla   Input Parameters:
5930a1cb98faSBarry Smith + dm           - The `DM`
5931b09969d6SVaclav Hapla . spaceDim     - The spatial dimension used for coordinates
5932b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
5933b09969d6SVaclav Hapla 
5934b09969d6SVaclav Hapla   Level: advanced
5935b09969d6SVaclav Hapla 
59361cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellList()`
5937b09969d6SVaclav Hapla @*/
5938d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildCoordinatesFromCellList(DM dm, PetscInt spaceDim, const PetscReal vertexCoords[])
5939d71ae5a4SJacob Faibussowitsch {
5940b09969d6SVaclav Hapla   PetscSection coordSection;
5941b09969d6SVaclav Hapla   Vec          coordinates;
5942b09969d6SVaclav Hapla   DM           cdm;
5943b09969d6SVaclav Hapla   PetscScalar *coords;
59441edcf0b2SVaclav Hapla   PetscInt     v, vStart, vEnd, d;
5945b09969d6SVaclav Hapla 
5946b09969d6SVaclav Hapla   PetscFunctionBegin;
59479566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
59489566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
59491dca8a05SBarry Smith   PetscCheck(vStart >= 0 && vEnd >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
59509566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, spaceDim));
59519566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
59529566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
59539566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, spaceDim));
59549566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, vStart, vEnd));
59551edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
59569566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, spaceDim));
59579566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, spaceDim));
5958b09969d6SVaclav Hapla   }
59599566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
5960b09969d6SVaclav Hapla 
59619566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
59629566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(cdm, &coordinates));
59639566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, spaceDim));
59649566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
59659566063dSJacob Faibussowitsch   PetscCall(VecGetArrayWrite(coordinates, &coords));
59661edcf0b2SVaclav Hapla   for (v = 0; v < vEnd - vStart; ++v) {
5967ad540459SPierre Jolivet     for (d = 0; d < spaceDim; ++d) coords[v * spaceDim + d] = vertexCoords[v * spaceDim + d];
5968b09969d6SVaclav Hapla   }
59699566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayWrite(coordinates, &coords));
59709566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
59719566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
59729566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
59733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5974b09969d6SVaclav Hapla }
5975b09969d6SVaclav Hapla 
5976b09969d6SVaclav Hapla /*@
5977a1cb98faSBarry Smith   DMPlexCreateFromCellListPetsc - Create `DMPLEX` from a list of vertices for each cell (common mesh generator output), but only process 0 takes in the input
59783df08285SMatthew G. Knepley 
5979a1cb98faSBarry Smith   Collective
5980b09969d6SVaclav Hapla 
5981b09969d6SVaclav Hapla   Input Parameters:
5982b09969d6SVaclav Hapla + comm         - The communicator
5983b09969d6SVaclav Hapla . dim          - The topological dimension of the mesh
59843df08285SMatthew G. Knepley . numCells     - The number of cells, only on process 0
5985a1cb98faSBarry Smith . numVertices  - The number of vertices owned by this process, or `PETSC_DECIDE`, only on process 0
59863df08285SMatthew G. Knepley . numCorners   - The number of vertices for each cell, only on process 0
5987b09969d6SVaclav Hapla . interpolate  - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
59883df08285SMatthew G. Knepley . cells        - An array of numCells*numCorners numbers, the vertices for each cell, only on process 0
5989b09969d6SVaclav Hapla . spaceDim     - The spatial dimension used for coordinates
59903df08285SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex, only on process 0
5991b09969d6SVaclav Hapla 
5992b09969d6SVaclav Hapla   Output Parameter:
5993a1cb98faSBarry Smith . dm - The `DM`, which only has points on process 0
599425b6865aSVaclav Hapla 
5995b09969d6SVaclav Hapla   Level: intermediate
5996b09969d6SVaclav Hapla 
5997a1cb98faSBarry Smith   Notes:
5998a1cb98faSBarry Smith   This function is just a convenient sequence of `DMCreate()`, `DMSetType()`, `DMSetDimension()`, `DMPlexBuildFromCellList()`,
5999a1cb98faSBarry Smith   `DMPlexInterpolate()`, `DMPlexBuildCoordinatesFromCellList()`
6000a1cb98faSBarry Smith 
6001a1cb98faSBarry Smith   See `DMPlexBuildFromCellList()` for an example and details about the topology-related parameters.
6002a1cb98faSBarry Smith   See `DMPlexBuildCoordinatesFromCellList()` for details about the geometry-related parameters.
6003a1cb98faSBarry Smith   See `DMPlexCreateFromCellListParallelPetsc()` for parallel input
6004a1cb98faSBarry Smith 
60051cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildFromCellList()`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()`
60069298eaa6SMatthew G Knepley @*/
6007d71ae5a4SJacob 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)
6008d71ae5a4SJacob Faibussowitsch {
60093df08285SMatthew G. Knepley   PetscMPIInt rank;
60109298eaa6SMatthew G Knepley 
60119298eaa6SMatthew G Knepley   PetscFunctionBegin;
601228b400f6SJacob 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.");
60139566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
60149566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
60159566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
60169566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
6017c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildFromCellList(*dm, numCells, numVertices, numCorners, cells));
60189566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildFromCellList(*dm, 0, 0, 0, NULL));
60199298eaa6SMatthew G Knepley   if (interpolate) {
60205fd9971aSMatthew G. Knepley     DM idm;
60219298eaa6SMatthew G Knepley 
60229566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
60239566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
60249298eaa6SMatthew G Knepley     *dm = idm;
60259298eaa6SMatthew G Knepley   }
6026c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, vertexCoords));
60279566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, NULL));
60283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
60299298eaa6SMatthew G Knepley }
60309298eaa6SMatthew G Knepley 
6031939f6067SMatthew G. Knepley /*@
603220f4b53cSBarry Smith   DMPlexCreateFromDAG - This takes as input the adjacency-list representation of the Directed Acyclic Graph (Hasse Diagram) encoding a mesh, and produces a `DM`
6033939f6067SMatthew G. Knepley 
6034939f6067SMatthew G. Knepley   Input Parameters:
603520f4b53cSBarry Smith + dm               - The empty `DM` object, usually from `DMCreate()` and `DMSetDimension()`
6036939f6067SMatthew G. Knepley . depth            - The depth of the DAG
603720f4b53cSBarry Smith . numPoints        - Array of size depth + 1 containing the number of points at each `depth`
6038939f6067SMatthew G. Knepley . coneSize         - The cone size of each point
6039939f6067SMatthew G. Knepley . cones            - The concatenation of the cone points for each point, the cone list must be oriented correctly for each point
6040939f6067SMatthew G. Knepley . coneOrientations - The orientation of each cone point
604120f4b53cSBarry Smith - vertexCoords     - An array of `numPoints`[0]*spacedim numbers representing the coordinates of each vertex, with spacedim the value set via `DMSetCoordinateDim()`
6042939f6067SMatthew G. Knepley 
6043939f6067SMatthew G. Knepley   Output Parameter:
604420f4b53cSBarry Smith . dm - The `DM`
604520f4b53cSBarry Smith 
604620f4b53cSBarry Smith   Level: advanced
6047939f6067SMatthew G. Knepley 
6048a1cb98faSBarry Smith   Note:
6049a1cb98faSBarry Smith   Two triangles sharing a face would have input
6050a1cb98faSBarry Smith .vb
6051a1cb98faSBarry Smith   depth = 1, numPoints = [4 2], coneSize = [3 3 0 0 0 0]
6052a1cb98faSBarry Smith   cones = [2 3 4  3 5 4], coneOrientations = [0 0 0  0 0 0]
6053a1cb98faSBarry Smith  vertexCoords = [-1.0 0.0  0.0 -1.0  0.0 1.0  1.0 0.0]
6054a1cb98faSBarry Smith .ve
6055939f6067SMatthew G. Knepley   which would result in the DMPlex
6056a1cb98faSBarry Smith .vb
6057a1cb98faSBarry Smith         4
6058a1cb98faSBarry Smith       / | \
6059a1cb98faSBarry Smith      /  |  \
6060a1cb98faSBarry Smith     /   |   \
6061a1cb98faSBarry Smith    2  0 | 1  5
6062a1cb98faSBarry Smith     \   |   /
6063a1cb98faSBarry Smith      \  |  /
6064a1cb98faSBarry Smith       \ | /
6065a1cb98faSBarry Smith         3
6066a1cb98faSBarry Smith .ve
6067a1cb98faSBarry Smith   Notice that all points are numbered consecutively, unlike `DMPlexCreateFromCellListPetsc()`
6068939f6067SMatthew G. Knepley 
60691cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`
6070939f6067SMatthew G. Knepley @*/
6071d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateFromDAG(DM dm, PetscInt depth, const PetscInt numPoints[], const PetscInt coneSize[], const PetscInt cones[], const PetscInt coneOrientations[], const PetscScalar vertexCoords[])
6072d71ae5a4SJacob Faibussowitsch {
60739298eaa6SMatthew G Knepley   Vec          coordinates;
60749298eaa6SMatthew G Knepley   PetscSection coordSection;
60759298eaa6SMatthew G Knepley   PetscScalar *coords;
6076811e8653SToby Isaac   PetscInt     coordSize, firstVertex = -1, pStart = 0, pEnd = 0, p, v, dim, dimEmbed, d, off;
60779298eaa6SMatthew G Knepley 
60789298eaa6SMatthew G Knepley   PetscFunctionBegin;
60799566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
60809566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dimEmbed));
608163a3b9bcSJacob Faibussowitsch   PetscCheck(dimEmbed >= dim, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Embedding dimension %" PetscInt_FMT " cannot be less than intrinsic dimension %" PetscInt_FMT, dimEmbed, dim);
60829298eaa6SMatthew G Knepley   for (d = 0; d <= depth; ++d) pEnd += numPoints[d];
60839566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, pStart, pEnd));
60849298eaa6SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
60859566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeSize(dm, p, coneSize[p - pStart]));
6086ad540459SPierre Jolivet     if (firstVertex < 0 && !coneSize[p - pStart]) firstVertex = p - pStart;
608797e052ccSToby Isaac   }
60881dca8a05SBarry Smith   PetscCheck(firstVertex >= 0 || !numPoints[0], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Expected %" PetscInt_FMT " vertices but could not find any", numPoints[0]);
60899566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm)); /* Allocate space for cones */
60909298eaa6SMatthew G Knepley   for (p = pStart, off = 0; p < pEnd; off += coneSize[p - pStart], ++p) {
60919566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(dm, p, &cones[off]));
60929566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeOrientation(dm, p, &coneOrientations[off]));
60939298eaa6SMatthew G Knepley   }
60949566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
60959566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
60969298eaa6SMatthew G Knepley   /* Build coordinates */
60979566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
60989566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
60999566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dimEmbed));
61009566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numPoints[0]));
61019298eaa6SMatthew G Knepley   for (v = firstVertex; v < firstVertex + numPoints[0]; ++v) {
61029566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, dimEmbed));
61039566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dimEmbed));
61049298eaa6SMatthew G Knepley   }
61059566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
61069566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
61079566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
61089566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
61099566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
61109566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, dimEmbed));
61119566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
61129318fe57SMatthew G. Knepley   if (vertexCoords) {
61139566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
61149298eaa6SMatthew G Knepley     for (v = 0; v < numPoints[0]; ++v) {
61159298eaa6SMatthew G Knepley       PetscInt off;
61169298eaa6SMatthew G Knepley 
61179566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, v + firstVertex, &off));
6118ad540459SPierre Jolivet       for (d = 0; d < dimEmbed; ++d) coords[off + d] = vertexCoords[v * dimEmbed + d];
61199298eaa6SMatthew G Knepley     }
61209318fe57SMatthew G. Knepley   }
61219566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
61229566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
61239566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
61243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
61259298eaa6SMatthew G Knepley }
61268415267dSToby Isaac 
6127a4e35b19SJacob Faibussowitsch /*
6128a1cb98faSBarry Smith   DMPlexCreateCellVertexFromFile - Create a `DMPLEX` mesh from a simple cell-vertex file.
6129a1cb98faSBarry Smith 
6130a1cb98faSBarry Smith   Collective
61318ca92349SMatthew G. Knepley 
61328ca92349SMatthew G. Knepley + comm        - The MPI communicator
61338ca92349SMatthew G. Knepley . filename    - Name of the .dat file
61348ca92349SMatthew G. Knepley - interpolate - Create faces and edges in the mesh
61358ca92349SMatthew G. Knepley 
61368ca92349SMatthew G. Knepley   Output Parameter:
6137a1cb98faSBarry Smith . dm  - The `DM` object representing the mesh
61388ca92349SMatthew G. Knepley 
61398ca92349SMatthew G. Knepley   Level: beginner
61408ca92349SMatthew G. Knepley 
6141a1cb98faSBarry Smith   Note:
6142a1cb98faSBarry Smith   The format is the simplest possible:
6143a1cb98faSBarry Smith .vb
6144d0812dedSMatthew G. Knepley   dim Ne Nv Nc Nl
6145d0812dedSMatthew G. Knepley   v_1 v_2 ... v_Nc
6146d0812dedSMatthew G. Knepley   ...
6147d0812dedSMatthew G. Knepley   x y z marker_1 ... marker_Nl
6148a1cb98faSBarry Smith .ve
6149a1cb98faSBarry Smith 
6150a1cb98faSBarry Smith   Developer Note:
6151a1cb98faSBarry Smith   Should use a `PetscViewer` not a filename
6152a1cb98faSBarry Smith 
61536afe31f6SMartin Diehl .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromFile()`, `DMPlexCreateGmsh()`, `DMPlexCreate()`
6154a4e35b19SJacob Faibussowitsch */
6155ff6a9541SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCellVertexFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
6156d71ae5a4SJacob Faibussowitsch {
61578ca92349SMatthew G. Knepley   DMLabel      marker;
61588ca92349SMatthew G. Knepley   PetscViewer  viewer;
61598ca92349SMatthew G. Knepley   Vec          coordinates;
61608ca92349SMatthew G. Knepley   PetscSection coordSection;
61618ca92349SMatthew G. Knepley   PetscScalar *coords;
61628ca92349SMatthew G. Knepley   char         line[PETSC_MAX_PATH_LEN];
6163d0812dedSMatthew G. Knepley   PetscInt     cdim, coordSize, v, c, d;
61648ca92349SMatthew G. Knepley   PetscMPIInt  rank;
6165d0812dedSMatthew G. Knepley   int          snum, dim, Nv, Nc, Ncn, Nl;
61668ca92349SMatthew G. Knepley 
61678ca92349SMatthew G. Knepley   PetscFunctionBegin;
61689566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
61699566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, &viewer));
61709566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(viewer, PETSCVIEWERASCII));
61719566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetMode(viewer, FILE_MODE_READ));
61729566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetName(viewer, filename));
6173dd400576SPatrick Sanan   if (rank == 0) {
6174d0812dedSMatthew G. Knepley     PetscCall(PetscViewerRead(viewer, line, 5, NULL, PETSC_STRING));
6175d0812dedSMatthew G. Knepley     snum = sscanf(line, "%d %d %d %d %d", &dim, &Nc, &Nv, &Ncn, &Nl);
6176d0812dedSMatthew G. Knepley     PetscCheck(snum == 5, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
617725ce1634SJed Brown   } else {
6178f8d5e320SMatthew G. Knepley     Nc = Nv = Ncn = Nl = 0;
61798ca92349SMatthew G. Knepley   }
6180d0812dedSMatthew G. Knepley   PetscCallMPI(MPI_Bcast(&dim, 1, MPI_INT, 0, comm));
6181835f2295SStefano Zampini   cdim = dim;
61829566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
61839566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
61849566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(*dm, 0, Nc + Nv));
6185835f2295SStefano Zampini   PetscCall(DMSetDimension(*dm, dim));
61869566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*dm, cdim));
61878ca92349SMatthew G. Knepley   /* Read topology */
6188dd400576SPatrick Sanan   if (rank == 0) {
6189f8d5e320SMatthew G. Knepley     char     format[PETSC_MAX_PATH_LEN];
6190f8d5e320SMatthew G. Knepley     PetscInt cone[8];
61918ca92349SMatthew G. Knepley     int      vbuf[8], v;
61928ca92349SMatthew G. Knepley 
61939371c9d4SSatish Balay     for (c = 0; c < Ncn; ++c) {
61949371c9d4SSatish Balay       format[c * 3 + 0] = '%';
61959371c9d4SSatish Balay       format[c * 3 + 1] = 'd';
61969371c9d4SSatish Balay       format[c * 3 + 2] = ' ';
61979371c9d4SSatish Balay     }
6198f8d5e320SMatthew G. Knepley     format[Ncn * 3 - 1] = '\0';
61999566063dSJacob Faibussowitsch     for (c = 0; c < Nc; ++c) PetscCall(DMPlexSetConeSize(*dm, c, Ncn));
62009566063dSJacob Faibussowitsch     PetscCall(DMSetUp(*dm));
62018ca92349SMatthew G. Knepley     for (c = 0; c < Nc; ++c) {
62029566063dSJacob Faibussowitsch       PetscCall(PetscViewerRead(viewer, line, Ncn, NULL, PETSC_STRING));
6203f8d5e320SMatthew G. Knepley       switch (Ncn) {
6204d71ae5a4SJacob Faibussowitsch       case 2:
6205d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1]);
6206d71ae5a4SJacob Faibussowitsch         break;
6207d71ae5a4SJacob Faibussowitsch       case 3:
6208d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2]);
6209d71ae5a4SJacob Faibussowitsch         break;
6210d71ae5a4SJacob Faibussowitsch       case 4:
6211d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3]);
6212d71ae5a4SJacob Faibussowitsch         break;
6213d71ae5a4SJacob Faibussowitsch       case 6:
6214d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5]);
6215d71ae5a4SJacob Faibussowitsch         break;
6216d71ae5a4SJacob Faibussowitsch       case 8:
6217d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5], &vbuf[6], &vbuf[7]);
6218d71ae5a4SJacob Faibussowitsch         break;
6219d71ae5a4SJacob Faibussowitsch       default:
6220d71ae5a4SJacob Faibussowitsch         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No cell shape with %d vertices", Ncn);
6221f8d5e320SMatthew G. Knepley       }
622208401ef6SPierre Jolivet       PetscCheck(snum == Ncn, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
6223f8d5e320SMatthew G. Knepley       for (v = 0; v < Ncn; ++v) cone[v] = vbuf[v] + Nc;
62248ca92349SMatthew G. Knepley       /* Hexahedra are inverted */
6225f8d5e320SMatthew G. Knepley       if (Ncn == 8) {
62268ca92349SMatthew G. Knepley         PetscInt tmp = cone[1];
62278ca92349SMatthew G. Knepley         cone[1]      = cone[3];
62288ca92349SMatthew G. Knepley         cone[3]      = tmp;
62298ca92349SMatthew G. Knepley       }
62309566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(*dm, c, cone));
62318ca92349SMatthew G. Knepley     }
62328ca92349SMatthew G. Knepley   }
62339566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(*dm));
62349566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(*dm));
62358ca92349SMatthew G. Knepley   /* Read coordinates */
62369566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(*dm, &coordSection));
62379566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
62389566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, cdim));
62399566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, Nc, Nc + Nv));
62408ca92349SMatthew G. Knepley   for (v = Nc; v < Nc + Nv; ++v) {
62419566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, cdim));
62429566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, cdim));
62438ca92349SMatthew G. Knepley   }
62449566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
62459566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
62469566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
62479566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
62489566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
62499566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, cdim));
62509566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
62519566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
6252dd400576SPatrick Sanan   if (rank == 0) {
6253f8d5e320SMatthew G. Knepley     char   format[PETSC_MAX_PATH_LEN];
62548ca92349SMatthew G. Knepley     double x[3];
6255f8d5e320SMatthew G. Knepley     int    l, val[3];
62568ca92349SMatthew G. Knepley 
6257f8d5e320SMatthew G. Knepley     if (Nl) {
62589371c9d4SSatish Balay       for (l = 0; l < Nl; ++l) {
62599371c9d4SSatish Balay         format[l * 3 + 0] = '%';
62609371c9d4SSatish Balay         format[l * 3 + 1] = 'd';
62619371c9d4SSatish Balay         format[l * 3 + 2] = ' ';
62629371c9d4SSatish Balay       }
6263f8d5e320SMatthew G. Knepley       format[Nl * 3 - 1] = '\0';
62649566063dSJacob Faibussowitsch       PetscCall(DMCreateLabel(*dm, "marker"));
62659566063dSJacob Faibussowitsch       PetscCall(DMGetLabel(*dm, "marker", &marker));
6266f8d5e320SMatthew G. Knepley     }
62678ca92349SMatthew G. Knepley     for (v = 0; v < Nv; ++v) {
62689566063dSJacob Faibussowitsch       PetscCall(PetscViewerRead(viewer, line, 3 + Nl, NULL, PETSC_STRING));
6269f8d5e320SMatthew G. Knepley       snum = sscanf(line, "%lg %lg %lg", &x[0], &x[1], &x[2]);
627008401ef6SPierre Jolivet       PetscCheck(snum == 3, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
6271f8d5e320SMatthew G. Knepley       switch (Nl) {
6272d71ae5a4SJacob Faibussowitsch       case 0:
6273d71ae5a4SJacob Faibussowitsch         snum = 0;
6274d71ae5a4SJacob Faibussowitsch         break;
6275d71ae5a4SJacob Faibussowitsch       case 1:
6276d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &val[0]);
6277d71ae5a4SJacob Faibussowitsch         break;
6278d71ae5a4SJacob Faibussowitsch       case 2:
6279d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &val[0], &val[1]);
6280d71ae5a4SJacob Faibussowitsch         break;
6281d71ae5a4SJacob Faibussowitsch       case 3:
6282d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &val[0], &val[1], &val[2]);
6283d71ae5a4SJacob Faibussowitsch         break;
6284d71ae5a4SJacob Faibussowitsch       default:
6285d71ae5a4SJacob Faibussowitsch         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Request support for %d labels", Nl);
6286f8d5e320SMatthew G. Knepley       }
628708401ef6SPierre Jolivet       PetscCheck(snum == Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
62888ca92349SMatthew G. Knepley       for (d = 0; d < cdim; ++d) coords[v * cdim + d] = x[d];
62899566063dSJacob Faibussowitsch       for (l = 0; l < Nl; ++l) PetscCall(DMLabelSetValue(marker, v + Nc, val[l]));
62908ca92349SMatthew G. Knepley     }
62918ca92349SMatthew G. Knepley   }
62929566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
62939566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(*dm, coordinates));
62949566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
62959566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(&viewer));
62968ca92349SMatthew G. Knepley   if (interpolate) {
62978ca92349SMatthew G. Knepley     DM      idm;
62988ca92349SMatthew G. Knepley     DMLabel bdlabel;
62998ca92349SMatthew G. Knepley 
63009566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
63019566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
63028ca92349SMatthew G. Knepley     *dm = idm;
63038ca92349SMatthew G. Knepley 
6304f8d5e320SMatthew G. Knepley     if (!Nl) {
63059566063dSJacob Faibussowitsch       PetscCall(DMCreateLabel(*dm, "marker"));
63069566063dSJacob Faibussowitsch       PetscCall(DMGetLabel(*dm, "marker", &bdlabel));
63079566063dSJacob Faibussowitsch       PetscCall(DMPlexMarkBoundaryFaces(*dm, PETSC_DETERMINE, bdlabel));
63089566063dSJacob Faibussowitsch       PetscCall(DMPlexLabelComplete(*dm, bdlabel));
63098ca92349SMatthew G. Knepley     }
6310f8d5e320SMatthew G. Knepley   }
63113ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
63128ca92349SMatthew G. Knepley }
63138ca92349SMatthew G. Knepley 
6314cc4c1da9SBarry Smith /*@
6315a1cb98faSBarry Smith   DMPlexCreateFromFile - This takes a filename and produces a `DM`
6316a1cb98faSBarry Smith 
6317a1cb98faSBarry Smith   Collective
6318ca522641SMatthew G. Knepley 
6319ca522641SMatthew G. Knepley   Input Parameters:
6320ca522641SMatthew G. Knepley + comm        - The communicator
6321ca522641SMatthew G. Knepley . filename    - A file name
6322a1cb98faSBarry Smith . plexname    - The object name of the resulting `DM`, also used for intra-datafile lookup by some formats
6323ca522641SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
6324ca522641SMatthew G. Knepley 
6325ca522641SMatthew G. Knepley   Output Parameter:
6326a1cb98faSBarry Smith . dm - The `DM`
6327ca522641SMatthew G. Knepley 
6328a1cb98faSBarry Smith   Options Database Key:
6329a1cb98faSBarry Smith . -dm_plex_create_from_hdf5_xdmf - use the `PETSC_VIEWER_HDF5_XDMF` format for reading HDF5
633002ef0d99SVaclav Hapla 
633137fdd005SBarry Smith   Use `-dm_plex_create_ prefix` to pass options to the internal `PetscViewer`, e.g.
6332bca97951SVaclav Hapla $ -dm_plex_create_viewer_hdf5_collective
6333bca97951SVaclav Hapla 
6334ca522641SMatthew G. Knepley   Level: beginner
6335ca522641SMatthew G. Knepley 
6336a1cb98faSBarry Smith   Notes:
6337a1cb98faSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
6338a1cb98faSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
6339a1cb98faSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
6340a1cb98faSBarry Smith   The input parameter name is thus used to name the `DMPLEX` object when `DMPlexCreateFromFile()` internally
6341a1cb98faSBarry Smith   calls `DMLoad()`. Currently, name is ignored for other viewer types and/or formats.
6342a1cb98faSBarry Smith 
63431cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromDAG()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`, `PetscObjectSetName()`, `DMView()`, `DMLoad()`
6344ca522641SMatthew G. Knepley @*/
6345d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateFromFile(MPI_Comm comm, const char filename[], const char plexname[], PetscBool interpolate, DM *dm)
6346d71ae5a4SJacob Faibussowitsch {
6347ef3a5affSJacob Faibussowitsch   const char  extGmsh[]      = ".msh";
6348ef3a5affSJacob Faibussowitsch   const char  extGmsh2[]     = ".msh2";
6349ef3a5affSJacob Faibussowitsch   const char  extGmsh4[]     = ".msh4";
6350ef3a5affSJacob Faibussowitsch   const char  extCGNS[]      = ".cgns";
6351ef3a5affSJacob Faibussowitsch   const char  extExodus[]    = ".exo";
6352ef3a5affSJacob Faibussowitsch   const char  extExodus_e[]  = ".e";
6353ef3a5affSJacob Faibussowitsch   const char  extGenesis[]   = ".gen";
6354ef3a5affSJacob Faibussowitsch   const char  extFluent[]    = ".cas";
6355ef3a5affSJacob Faibussowitsch   const char  extHDF5[]      = ".h5";
63566f2c871aSStefano Zampini   const char  extXDMFHDF5[]  = ".xdmf.h5";
6357ef3a5affSJacob Faibussowitsch   const char  extPLY[]       = ".ply";
6358ef3a5affSJacob Faibussowitsch   const char  extEGADSLite[] = ".egadslite";
6359ef3a5affSJacob Faibussowitsch   const char  extEGADS[]     = ".egads";
6360ef3a5affSJacob Faibussowitsch   const char  extIGES[]      = ".igs";
6361ef3a5affSJacob Faibussowitsch   const char  extSTEP[]      = ".stp";
6362ef3a5affSJacob Faibussowitsch   const char  extCV[]        = ".dat";
6363ca522641SMatthew G. Knepley   size_t      len;
63646afe31f6SMartin Diehl   PetscBool   isGmsh, isGmsh2, isGmsh4, isCGNS, isExodus, isGenesis, isFluent, isHDF5, isPLY, isEGADSLite, isEGADS, isIGES, isSTEP, isCV, isXDMFHDF5;
6365ca522641SMatthew G. Knepley   PetscMPIInt rank;
6366ca522641SMatthew G. Knepley 
6367ca522641SMatthew G. Knepley   PetscFunctionBegin;
63684f572ea9SToby Isaac   PetscAssertPointer(filename, 2);
63694f572ea9SToby Isaac   if (plexname) PetscAssertPointer(plexname, 3);
63704f572ea9SToby Isaac   PetscAssertPointer(dm, 5);
63719566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
63729566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_CreateFromFile, 0, 0, 0, 0));
63739566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
63749566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(filename, &len));
637528b400f6SJacob Faibussowitsch   PetscCheck(len, comm, PETSC_ERR_ARG_WRONG, "Filename must be a valid path");
6376ef3a5affSJacob Faibussowitsch 
63779371c9d4SSatish Balay #define CheckExtension(extension__, is_extension__) \
63789371c9d4SSatish Balay   do { \
6379274aaeaaSJacob Faibussowitsch     PetscAssert(sizeof(extension__), comm, PETSC_ERR_PLIB, "Zero-size extension: %s", extension__); \
6380274aaeaaSJacob Faibussowitsch     /* don't count the null-terminator at the end */ \
6381274aaeaaSJacob Faibussowitsch     const size_t ext_len = sizeof(extension__) - 1; \
6382274aaeaaSJacob Faibussowitsch     if (len < ext_len) { \
6383ef3a5affSJacob Faibussowitsch       is_extension__ = PETSC_FALSE; \
6384ef3a5affSJacob Faibussowitsch     } else { \
6385274aaeaaSJacob Faibussowitsch       PetscCall(PetscStrncmp(filename + len - ext_len, extension__, ext_len, &is_extension__)); \
6386ef3a5affSJacob Faibussowitsch     } \
6387ef3a5affSJacob Faibussowitsch   } while (0)
6388ef3a5affSJacob Faibussowitsch 
6389ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh, isGmsh);
6390ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh2, isGmsh2);
6391ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh4, isGmsh4);
6392ef3a5affSJacob Faibussowitsch   CheckExtension(extCGNS, isCGNS);
6393ef3a5affSJacob Faibussowitsch   CheckExtension(extExodus, isExodus);
6394ef3a5affSJacob Faibussowitsch   if (!isExodus) CheckExtension(extExodus_e, isExodus);
6395ef3a5affSJacob Faibussowitsch   CheckExtension(extGenesis, isGenesis);
6396ef3a5affSJacob Faibussowitsch   CheckExtension(extFluent, isFluent);
6397ef3a5affSJacob Faibussowitsch   CheckExtension(extHDF5, isHDF5);
6398ef3a5affSJacob Faibussowitsch   CheckExtension(extPLY, isPLY);
6399ef3a5affSJacob Faibussowitsch   CheckExtension(extEGADSLite, isEGADSLite);
6400ef3a5affSJacob Faibussowitsch   CheckExtension(extEGADS, isEGADS);
6401ef3a5affSJacob Faibussowitsch   CheckExtension(extIGES, isIGES);
6402ef3a5affSJacob Faibussowitsch   CheckExtension(extSTEP, isSTEP);
6403ef3a5affSJacob Faibussowitsch   CheckExtension(extCV, isCV);
64046f2c871aSStefano Zampini   CheckExtension(extXDMFHDF5, isXDMFHDF5);
6405ef3a5affSJacob Faibussowitsch 
6406ef3a5affSJacob Faibussowitsch #undef CheckExtension
6407ef3a5affSJacob Faibussowitsch 
6408de78e4feSLisandro Dalcin   if (isGmsh || isGmsh2 || isGmsh4) {
64099566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateGmshFromFile(comm, filename, interpolate, dm));
6410ca522641SMatthew G. Knepley   } else if (isCGNS) {
64119566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCGNSFromFile(comm, filename, interpolate, dm));
641290c68965SMatthew G. Knepley   } else if (isExodus || isGenesis) {
64139566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateExodusFromFile(comm, filename, interpolate, dm));
64142f0bd6dcSMichael Lange   } else if (isFluent) {
64159566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFluentFromFile(comm, filename, interpolate, dm));
6416cc2f8f65SMatthew G. Knepley   } else if (isHDF5) {
6417cc2f8f65SMatthew G. Knepley     PetscViewer viewer;
6418cc2f8f65SMatthew G. Knepley 
641943b242b4SVaclav 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 */
64206f2c871aSStefano Zampini     PetscCall(PetscOptionsGetBool(NULL, NULL, "-dm_plex_create_from_hdf5_xdmf", &isXDMFHDF5, NULL));
64219566063dSJacob Faibussowitsch     PetscCall(PetscViewerCreate(comm, &viewer));
64229566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetType(viewer, PETSCVIEWERHDF5));
64239566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetOptionsPrefix(viewer, "dm_plex_create_"));
64249566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetFromOptions(viewer));
64259566063dSJacob Faibussowitsch     PetscCall(PetscViewerFileSetMode(viewer, FILE_MODE_READ));
64269566063dSJacob Faibussowitsch     PetscCall(PetscViewerFileSetName(viewer, filename));
6427cd7e8a5eSksagiyam 
64289566063dSJacob Faibussowitsch     PetscCall(DMCreate(comm, dm));
6429f4f49eeaSPierre Jolivet     PetscCall(PetscObjectSetName((PetscObject)*dm, plexname));
64309566063dSJacob Faibussowitsch     PetscCall(DMSetType(*dm, DMPLEX));
64316f2c871aSStefano Zampini     if (isXDMFHDF5) PetscCall(PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_XDMF));
64329566063dSJacob Faibussowitsch     PetscCall(DMLoad(*dm, viewer));
64336f2c871aSStefano Zampini     if (isXDMFHDF5) PetscCall(PetscViewerPopFormat(viewer));
64349566063dSJacob Faibussowitsch     PetscCall(PetscViewerDestroy(&viewer));
64355fd9971aSMatthew G. Knepley 
64365fd9971aSMatthew G. Knepley     if (interpolate) {
64375fd9971aSMatthew G. Knepley       DM idm;
64385fd9971aSMatthew G. Knepley 
64399566063dSJacob Faibussowitsch       PetscCall(DMPlexInterpolate(*dm, &idm));
64409566063dSJacob Faibussowitsch       PetscCall(DMDestroy(dm));
64415fd9971aSMatthew G. Knepley       *dm = idm;
64425fd9971aSMatthew G. Knepley     }
6443f2801cd6SMatthew G. Knepley   } else if (isPLY) {
64449566063dSJacob Faibussowitsch     PetscCall(DMPlexCreatePLYFromFile(comm, filename, interpolate, dm));
6445c1cad2e7SMatthew G. Knepley   } else if (isEGADSLite || isEGADS || isIGES || isSTEP) {
64469566063dSJacob Faibussowitsch     if (isEGADSLite) PetscCall(DMPlexCreateEGADSLiteFromFile(comm, filename, dm));
64479566063dSJacob Faibussowitsch     else PetscCall(DMPlexCreateEGADSFromFile(comm, filename, dm));
64487bee2925SMatthew Knepley     if (!interpolate) {
64497bee2925SMatthew Knepley       DM udm;
64507bee2925SMatthew Knepley 
64519566063dSJacob Faibussowitsch       PetscCall(DMPlexUninterpolate(*dm, &udm));
64529566063dSJacob Faibussowitsch       PetscCall(DMDestroy(dm));
64537bee2925SMatthew Knepley       *dm = udm;
64547bee2925SMatthew Knepley     }
64558ca92349SMatthew G. Knepley   } else if (isCV) {
64569566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCellVertexFromFile(comm, filename, interpolate, dm));
645798921bdaSJacob Faibussowitsch   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot load file %s: unrecognized extension", filename);
64589566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(plexname, &len));
6459f4f49eeaSPierre Jolivet   if (len) PetscCall(PetscObjectSetName((PetscObject)*dm, plexname));
64609566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_CreateFromFile, 0, 0, 0, 0));
64613ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6462ca522641SMatthew G. Knepley }
646312b8a6daSStefano Zampini 
6464cc4c1da9SBarry Smith /*@
64659f6c5813SMatthew 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.
64669f6c5813SMatthew G. Knepley 
64670528010dSStefano Zampini   Input Parameters:
64680528010dSStefano Zampini + tr     - The `DMPlexTransform`
64690528010dSStefano Zampini - prefix - An options prefix, or NULL
64709f6c5813SMatthew G. Knepley 
64719f6c5813SMatthew G. Knepley   Output Parameter:
64729f6c5813SMatthew G. Knepley . dm - The `DM`
64739f6c5813SMatthew G. Knepley 
64749f6c5813SMatthew G. Knepley   Level: beginner
64759f6c5813SMatthew G. Knepley 
647620f4b53cSBarry Smith   Notes:
647720f4b53cSBarry 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.
647820f4b53cSBarry Smith 
64799f6c5813SMatthew G. Knepley .seealso: `DMPlexCreateFromFile`, `DMPlexCreateFromDAG()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`
64809f6c5813SMatthew G. Knepley @*/
64810528010dSStefano Zampini PetscErrorCode DMPlexCreateEphemeral(DMPlexTransform tr, const char prefix[], DM *dm)
64829f6c5813SMatthew G. Knepley {
64830528010dSStefano Zampini   DM           bdm, bcdm, cdm;
64840528010dSStefano Zampini   Vec          coordinates, coordinatesNew;
64850528010dSStefano Zampini   PetscSection cs;
6486817b2c36SMatthew G. Knepley   PetscInt     cdim, Nl;
64879f6c5813SMatthew G. Knepley 
64889f6c5813SMatthew G. Knepley   PetscFunctionBegin;
64899f6c5813SMatthew G. Knepley   PetscCall(DMCreate(PetscObjectComm((PetscObject)tr), dm));
64909f6c5813SMatthew G. Knepley   PetscCall(DMSetType(*dm, DMPLEX));
64910528010dSStefano Zampini   ((DM_Plex *)(*dm)->data)->interpolated = DMPLEX_INTERPOLATED_FULL;
64920528010dSStefano Zampini   // Handle coordinates
64930528010dSStefano Zampini   PetscCall(DMPlexTransformGetDM(tr, &bdm));
6494817b2c36SMatthew G. Knepley   PetscCall(DMPlexTransformSetDimensions(tr, bdm, *dm));
6495817b2c36SMatthew G. Knepley   PetscCall(DMGetCoordinateDim(*dm, &cdim));
64960528010dSStefano Zampini   PetscCall(DMGetCoordinateDM(bdm, &bcdm));
64970528010dSStefano Zampini   PetscCall(DMGetCoordinateDM(*dm, &cdm));
64980528010dSStefano Zampini   PetscCall(DMCopyDisc(bcdm, cdm));
64990528010dSStefano Zampini   PetscCall(DMGetLocalSection(cdm, &cs));
65000528010dSStefano Zampini   PetscCall(PetscSectionSetNumFields(cs, 1));
65010528010dSStefano Zampini   PetscCall(PetscSectionSetFieldComponents(cs, 0, cdim));
65020528010dSStefano Zampini   PetscCall(DMGetCoordinatesLocal(bdm, &coordinates));
65030528010dSStefano Zampini   PetscCall(VecDuplicate(coordinates, &coordinatesNew));
65040528010dSStefano Zampini   PetscCall(VecCopy(coordinates, coordinatesNew));
65050528010dSStefano Zampini   PetscCall(DMSetCoordinatesLocal(*dm, coordinatesNew));
65060528010dSStefano Zampini   PetscCall(VecDestroy(&coordinatesNew));
65079f6c5813SMatthew G. Knepley 
65089f6c5813SMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)tr));
65099f6c5813SMatthew G. Knepley   PetscCall(DMPlexTransformDestroy(&((DM_Plex *)(*dm)->data)->tr));
65109f6c5813SMatthew G. Knepley   ((DM_Plex *)(*dm)->data)->tr = tr;
65110528010dSStefano Zampini   PetscCall(DMPlexDistributeSetDefault(*dm, PETSC_FALSE));
65120528010dSStefano Zampini   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)*dm, prefix));
65130528010dSStefano Zampini   PetscCall(DMSetFromOptions(*dm));
65149f6c5813SMatthew G. Knepley 
65159f6c5813SMatthew G. Knepley   PetscCall(DMGetNumLabels(bdm, &Nl));
65169f6c5813SMatthew G. Knepley   for (PetscInt l = 0; l < Nl; ++l) {
65179f6c5813SMatthew G. Knepley     DMLabel     label, labelNew;
65189f6c5813SMatthew G. Knepley     const char *lname;
65199f6c5813SMatthew G. Knepley     PetscBool   isDepth, isCellType;
65209f6c5813SMatthew G. Knepley 
65219f6c5813SMatthew G. Knepley     PetscCall(DMGetLabelName(bdm, l, &lname));
65229f6c5813SMatthew G. Knepley     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
65239f6c5813SMatthew G. Knepley     if (isDepth) continue;
65249f6c5813SMatthew G. Knepley     PetscCall(PetscStrcmp(lname, "celltype", &isCellType));
65259f6c5813SMatthew G. Knepley     if (isCellType) continue;
65269f6c5813SMatthew G. Knepley     PetscCall(DMCreateLabel(*dm, lname));
65279f6c5813SMatthew G. Knepley     PetscCall(DMGetLabel(bdm, lname, &label));
65289f6c5813SMatthew G. Knepley     PetscCall(DMGetLabel(*dm, lname, &labelNew));
65299f6c5813SMatthew G. Knepley     PetscCall(DMLabelSetType(labelNew, DMLABELEPHEMERAL));
65309f6c5813SMatthew G. Knepley     PetscCall(DMLabelEphemeralSetLabel(labelNew, label));
65319f6c5813SMatthew G. Knepley     PetscCall(DMLabelEphemeralSetTransform(labelNew, tr));
65329f6c5813SMatthew G. Knepley     PetscCall(DMLabelSetUp(labelNew));
65339f6c5813SMatthew G. Knepley   }
65343ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
65359f6c5813SMatthew G. Knepley }
6536