xref: /petsc/src/dm/impls/plex/plexcreate.c (revision 5e2c55196112e5fe262340049dbd3227925472db)
1*5e2c5519SMatthew G. Knepley #include "petscsystypes.h"
2552f7358SJed Brown #define PETSCDM_DLL
3af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h> /*I   "petscdmplex.h"   I*/
4cc4c1da9SBarry Smith #include <petsc/private/hashseti.h>
50c312b8eSJed Brown #include <petscsf.h>
6cc4c1da9SBarry Smith #include <petscdmplextransform.h> /*I   "petscdmplextransform.h"   I*/
79f6c5813SMatthew G. Knepley #include <petscdmlabelephemeral.h>
8b7f5c055SJed Brown #include <petsc/private/kernels/blockmatmult.h>
9b7f5c055SJed Brown #include <petsc/private/kernels/blockinvert.h>
10552f7358SJed Brown 
11d0812dedSMatthew G. Knepley #ifdef PETSC_HAVE_UNISTD_H
12d0812dedSMatthew G. Knepley   #include <unistd.h>
13d0812dedSMatthew G. Knepley #endif
14d0812dedSMatthew G. Knepley #include <errno.h>
15d0812dedSMatthew G. Knepley 
16708be2fdSJed Brown PetscLogEvent DMPLEX_CreateFromFile, DMPLEX_CreateFromOptions, DMPLEX_BuildFromCellList, DMPLEX_BuildCoordinatesFromCellList;
1758cd63d5SVaclav Hapla 
189318fe57SMatthew G. Knepley /* External function declarations here */
199318fe57SMatthew G. Knepley static PetscErrorCode DMInitialize_Plex(DM dm);
209318fe57SMatthew G. Knepley 
21e600fa54SMatthew G. Knepley /* This copies internal things in the Plex structure that we generally want when making a new, related Plex */
22d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCopy_Internal(DM dmin, PetscBool copyPeriodicity, PetscBool copyOverlap, DM dmout)
23d71ae5a4SJacob Faibussowitsch {
244fb89dddSMatthew G. Knepley   const PetscReal     *maxCell, *Lstart, *L;
2512a88998SMatthew G. Knepley   VecType              vecType;
2612a88998SMatthew G. Knepley   MatType              matType;
27129f447cSJames Wright   PetscBool            dist, useCeed, balance_partition;
28adc21957SMatthew G. Knepley   DMReorderDefaultFlag reorder;
29e600fa54SMatthew G. Knepley 
30e600fa54SMatthew G. Knepley   PetscFunctionBegin;
31835f2295SStefano Zampini   if (dmin == dmout) PetscFunctionReturn(PETSC_SUCCESS);
3212a88998SMatthew G. Knepley   PetscCall(DMGetVecType(dmin, &vecType));
3312a88998SMatthew G. Knepley   PetscCall(DMSetVecType(dmout, vecType));
3412a88998SMatthew G. Knepley   PetscCall(DMGetMatType(dmin, &matType));
3512a88998SMatthew G. Knepley   PetscCall(DMSetMatType(dmout, matType));
36e600fa54SMatthew G. Knepley   if (copyPeriodicity) {
374fb89dddSMatthew G. Knepley     PetscCall(DMGetPeriodicity(dmin, &maxCell, &Lstart, &L));
384fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dmout, maxCell, Lstart, L));
393d0e8ed9SDavid Salac     PetscCall(DMLocalizeCoordinates(dmout));
40e600fa54SMatthew G. Knepley   }
419566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeGetDefault(dmin, &dist));
429566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeSetDefault(dmout, dist));
436bc1bd01Sksagiyam   PetscCall(DMPlexReorderGetDefault(dmin, &reorder));
446bc1bd01Sksagiyam   PetscCall(DMPlexReorderSetDefault(dmout, reorder));
455962854dSMatthew G. Knepley   PetscCall(DMPlexGetUseCeed(dmin, &useCeed));
465962854dSMatthew G. Knepley   PetscCall(DMPlexSetUseCeed(dmout, useCeed));
47129f447cSJames Wright   PetscCall(DMPlexGetPartitionBalance(dmin, &balance_partition));
48129f447cSJames Wright   PetscCall(DMPlexSetPartitionBalance(dmout, balance_partition));
49e600fa54SMatthew G. Knepley   ((DM_Plex *)dmout->data)->useHashLocation = ((DM_Plex *)dmin->data)->useHashLocation;
505962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printSetValues  = ((DM_Plex *)dmin->data)->printSetValues;
515962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printFEM        = ((DM_Plex *)dmin->data)->printFEM;
525962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printFVM        = ((DM_Plex *)dmin->data)->printFVM;
535962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printL2         = ((DM_Plex *)dmin->data)->printL2;
545962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printLocate     = ((DM_Plex *)dmin->data)->printLocate;
55a77a5016SMatthew G. Knepley   ((DM_Plex *)dmout->data)->printProject    = ((DM_Plex *)dmin->data)->printProject;
565962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printTol        = ((DM_Plex *)dmin->data)->printTol;
571baa6e33SBarry Smith   if (copyOverlap) PetscCall(DMPlexSetOverlap_Plex(dmout, dmin, 0));
583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
59e600fa54SMatthew G. Knepley }
60e600fa54SMatthew G. Knepley 
619318fe57SMatthew G. Knepley /* Replace dm with the contents of ndm, and then destroy ndm
629318fe57SMatthew G. Knepley    - Share the DM_Plex structure
639318fe57SMatthew G. Knepley    - Share the coordinates
649318fe57SMatthew G. Knepley    - Share the SF
659318fe57SMatthew G. Knepley */
66d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexReplace_Internal(DM dm, DM *ndm)
67d71ae5a4SJacob Faibussowitsch {
689318fe57SMatthew G. Knepley   PetscSF          sf;
699318fe57SMatthew G. Knepley   DM               dmNew = *ndm, coordDM, coarseDM;
709318fe57SMatthew G. Knepley   Vec              coords;
714fb89dddSMatthew G. Knepley   const PetscReal *maxCell, *Lstart, *L;
729318fe57SMatthew G. Knepley   PetscInt         dim, cdim;
73e535cce4SJames Wright   PetscBool        use_natural;
749318fe57SMatthew G. Knepley 
759318fe57SMatthew G. Knepley   PetscFunctionBegin;
769318fe57SMatthew G. Knepley   if (dm == dmNew) {
779566063dSJacob Faibussowitsch     PetscCall(DMDestroy(ndm));
783ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
799318fe57SMatthew G. Knepley   }
809318fe57SMatthew G. Knepley   dm->setupcalled = dmNew->setupcalled;
81d0812dedSMatthew G. Knepley   if (!dm->hdr.name) {
82d0812dedSMatthew G. Knepley     const char *name;
83d0812dedSMatthew G. Knepley 
84d0812dedSMatthew G. Knepley     PetscCall(PetscObjectGetName((PetscObject)*ndm, &name));
85d0812dedSMatthew G. Knepley     PetscCall(PetscObjectSetName((PetscObject)dm, name));
86d0812dedSMatthew G. Knepley   }
879566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dmNew, &dim));
889566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
899566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dmNew, &cdim));
909566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, cdim));
919566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmNew, &sf));
929566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dm, sf));
939566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmNew, &coordDM));
949566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmNew, &coords));
959566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dm, coordDM));
969566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coords));
976858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dmNew, &coordDM));
986858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dmNew, &coords));
996858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinateDM(dm, coordDM));
1006858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinatesLocal(dm, coords));
1019318fe57SMatthew G. Knepley   /* Do not want to create the coordinate field if it does not already exist, so do not call DMGetCoordinateField() */
1026858538eSMatthew G. Knepley   PetscCall(DMFieldDestroy(&dm->coordinates[0].field));
1036858538eSMatthew G. Knepley   dm->coordinates[0].field            = dmNew->coordinates[0].field;
10461a622f3SMatthew G. Knepley   ((DM_Plex *)dmNew->data)->coordFunc = ((DM_Plex *)dm->data)->coordFunc;
1054fb89dddSMatthew G. Knepley   PetscCall(DMGetPeriodicity(dmNew, &maxCell, &Lstart, &L));
1064fb89dddSMatthew G. Knepley   PetscCall(DMSetPeriodicity(dm, maxCell, Lstart, L));
107e535cce4SJames Wright   PetscCall(DMGetNaturalSF(dmNew, &sf));
108e535cce4SJames Wright   PetscCall(DMSetNaturalSF(dm, sf));
109e535cce4SJames Wright   PetscCall(DMGetUseNatural(dmNew, &use_natural));
110e535cce4SJames Wright   PetscCall(DMSetUseNatural(dm, use_natural));
1119566063dSJacob Faibussowitsch   PetscCall(DMDestroy_Plex(dm));
1129566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(dm));
1139318fe57SMatthew G. Knepley   dm->data = dmNew->data;
1149318fe57SMatthew G. Knepley   ((DM_Plex *)dmNew->data)->refct++;
1151fca310dSJames Wright   {
1161fca310dSJames Wright     PetscInt       num_face_sfs;
1171fca310dSJames Wright     const PetscSF *sfs;
1181fca310dSJames Wright     PetscCall(DMPlexGetIsoperiodicFaceSF(dm, &num_face_sfs, &sfs));
1191fca310dSJames Wright     PetscCall(DMPlexSetIsoperiodicFaceSF(dm, num_face_sfs, (PetscSF *)sfs)); // for the compose function effect on dm
1201fca310dSJames Wright   }
1219566063dSJacob Faibussowitsch   PetscCall(DMDestroyLabelLinkList_Internal(dm));
1229566063dSJacob Faibussowitsch   PetscCall(DMCopyLabels(dmNew, dm, PETSC_OWN_POINTER, PETSC_TRUE, DM_COPY_LABELS_FAIL));
1239566063dSJacob Faibussowitsch   PetscCall(DMGetCoarseDM(dmNew, &coarseDM));
1249566063dSJacob Faibussowitsch   PetscCall(DMSetCoarseDM(dm, coarseDM));
1259566063dSJacob Faibussowitsch   PetscCall(DMDestroy(ndm));
1263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1279318fe57SMatthew G. Knepley }
1289318fe57SMatthew G. Knepley 
1299318fe57SMatthew G. Knepley /* Swap dm with the contents of dmNew
1309318fe57SMatthew G. Knepley    - Swap the DM_Plex structure
1319318fe57SMatthew G. Knepley    - Swap the coordinates
1329318fe57SMatthew G. Knepley    - Swap the point PetscSF
1339318fe57SMatthew G. Knepley */
134d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexSwap_Static(DM dmA, DM dmB)
135d71ae5a4SJacob Faibussowitsch {
1369318fe57SMatthew G. Knepley   DM          coordDMA, coordDMB;
1379318fe57SMatthew G. Knepley   Vec         coordsA, coordsB;
1389318fe57SMatthew G. Knepley   PetscSF     sfA, sfB;
1399318fe57SMatthew G. Knepley   DMField     fieldTmp;
1409318fe57SMatthew G. Knepley   void       *tmp;
1419318fe57SMatthew G. Knepley   DMLabelLink listTmp;
1429318fe57SMatthew G. Knepley   DMLabel     depthTmp;
1439318fe57SMatthew G. Knepley   PetscInt    tmpI;
1449318fe57SMatthew G. Knepley 
1459318fe57SMatthew G. Knepley   PetscFunctionBegin;
1463ba16761SJacob Faibussowitsch   if (dmA == dmB) PetscFunctionReturn(PETSC_SUCCESS);
1479566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmA, &sfA));
1489566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmB, &sfB));
1499566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sfA));
1509566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dmA, sfB));
1519566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dmB, sfA));
1529566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)sfA));
1539318fe57SMatthew G. Knepley 
1549566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmA, &coordDMA));
1559566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmB, &coordDMB));
1569566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)coordDMA));
1579566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dmA, coordDMB));
1589566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dmB, coordDMA));
1599566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)coordDMA));
1609318fe57SMatthew G. Knepley 
1619566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmA, &coordsA));
1629566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmB, &coordsB));
1639566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)coordsA));
1649566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmA, coordsB));
1659566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmB, coordsA));
1669566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)coordsA));
1679318fe57SMatthew G. Knepley 
1686858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dmA, &coordDMA));
1696858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dmB, &coordDMB));
1706858538eSMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)coordDMA));
1716858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinateDM(dmA, coordDMB));
1726858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinateDM(dmB, coordDMA));
1736858538eSMatthew G. Knepley   PetscCall(PetscObjectDereference((PetscObject)coordDMA));
1746858538eSMatthew G. Knepley 
1756858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dmA, &coordsA));
1766858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dmB, &coordsB));
1776858538eSMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)coordsA));
1786858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinatesLocal(dmA, coordsB));
1796858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinatesLocal(dmB, coordsA));
1806858538eSMatthew G. Knepley   PetscCall(PetscObjectDereference((PetscObject)coordsA));
1816858538eSMatthew G. Knepley 
1826858538eSMatthew G. Knepley   fieldTmp                  = dmA->coordinates[0].field;
1836858538eSMatthew G. Knepley   dmA->coordinates[0].field = dmB->coordinates[0].field;
1846858538eSMatthew G. Knepley   dmB->coordinates[0].field = fieldTmp;
1856858538eSMatthew G. Knepley   fieldTmp                  = dmA->coordinates[1].field;
1866858538eSMatthew G. Knepley   dmA->coordinates[1].field = dmB->coordinates[1].field;
1876858538eSMatthew G. Knepley   dmB->coordinates[1].field = fieldTmp;
1889318fe57SMatthew G. Knepley   tmp                       = dmA->data;
1899318fe57SMatthew G. Knepley   dmA->data                 = dmB->data;
1909318fe57SMatthew G. Knepley   dmB->data                 = tmp;
1919318fe57SMatthew G. Knepley   listTmp                   = dmA->labels;
1929318fe57SMatthew G. Knepley   dmA->labels               = dmB->labels;
1939318fe57SMatthew G. Knepley   dmB->labels               = listTmp;
1949318fe57SMatthew G. Knepley   depthTmp                  = dmA->depthLabel;
1959318fe57SMatthew G. Knepley   dmA->depthLabel           = dmB->depthLabel;
1969318fe57SMatthew G. Knepley   dmB->depthLabel           = depthTmp;
1979318fe57SMatthew G. Knepley   depthTmp                  = dmA->celltypeLabel;
1989318fe57SMatthew G. Knepley   dmA->celltypeLabel        = dmB->celltypeLabel;
1999318fe57SMatthew G. Knepley   dmB->celltypeLabel        = depthTmp;
2009318fe57SMatthew G. Knepley   tmpI                      = dmA->levelup;
2019318fe57SMatthew G. Knepley   dmA->levelup              = dmB->levelup;
2029318fe57SMatthew G. Knepley   dmB->levelup              = tmpI;
2033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2049318fe57SMatthew G. Knepley }
2059318fe57SMatthew G. Knepley 
2063431e603SJed Brown PetscErrorCode DMPlexInterpolateInPlace_Internal(DM dm)
207d71ae5a4SJacob Faibussowitsch {
2089318fe57SMatthew G. Knepley   DM idm;
2099318fe57SMatthew G. Knepley 
2109318fe57SMatthew G. Knepley   PetscFunctionBegin;
2119566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolate(dm, &idm));
2129566063dSJacob Faibussowitsch   PetscCall(DMPlexCopyCoordinates(dm, idm));
21369d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &idm));
2143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2159318fe57SMatthew G. Knepley }
2169318fe57SMatthew G. Knepley 
2179318fe57SMatthew G. Knepley /*@C
2189318fe57SMatthew G. Knepley   DMPlexCreateCoordinateSpace - Creates a finite element space for the coordinates
2199318fe57SMatthew G. Knepley 
22020f4b53cSBarry Smith   Collective
2219318fe57SMatthew G. Knepley 
2229318fe57SMatthew G. Knepley   Input Parameters:
22360225df5SJacob Faibussowitsch + dm        - The `DMPLEX`
22420f4b53cSBarry Smith . degree    - The degree of the finite element or `PETSC_DECIDE`
225e44f6aebSMatthew G. Knepley . project   - Flag to project current coordinates into the space
2269318fe57SMatthew G. Knepley - coordFunc - An optional function to map new points from refinement to the surface
2279318fe57SMatthew G. Knepley 
2289318fe57SMatthew G. Knepley   Level: advanced
2299318fe57SMatthew G. Knepley 
2301cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `PetscPointFunc`, `PetscFECreateLagrange()`, `DMGetCoordinateDM()`
2319318fe57SMatthew G. Knepley @*/
232e44f6aebSMatthew G. Knepley PetscErrorCode DMPlexCreateCoordinateSpace(DM dm, PetscInt degree, PetscBool project, PetscPointFunc coordFunc)
233d71ae5a4SJacob Faibussowitsch {
2349318fe57SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
235e44f6aebSMatthew G. Knepley   PetscFE  fe   = NULL;
2369318fe57SMatthew G. Knepley   DM       cdm;
2371df12153SMatthew G. Knepley   PetscInt dim, dE, qorder, height;
2389318fe57SMatthew G. Knepley 
239e44f6aebSMatthew G. Knepley   PetscFunctionBegin;
2409566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
2419566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
2429318fe57SMatthew G. Knepley   qorder = degree;
243e44f6aebSMatthew G. Knepley   PetscCall(DMGetCoordinateDM(dm, &cdm));
244d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)cdm);
245dc431b0cSMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-default_quadrature_order", "Quadrature order is one less than quadrature points per edge", "DMPlexCreateCoordinateSpace", qorder, &qorder, NULL, 0));
246d0609cedSBarry Smith   PetscOptionsEnd();
2471df12153SMatthew G. Knepley   PetscCall(DMPlexGetVTKCellHeight(dm, &height));
248e44f6aebSMatthew G. Knepley   if (degree >= 0) {
249e44f6aebSMatthew G. Knepley     DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
250e44f6aebSMatthew G. Knepley     PetscInt       cStart, cEnd, gct;
251dc431b0cSMatthew G. Knepley 
2521df12153SMatthew G. Knepley     PetscCall(DMPlexGetHeightStratum(dm, height, &cStart, &cEnd));
253dc431b0cSMatthew G. Knepley     if (cEnd > cStart) PetscCall(DMPlexGetCellType(dm, cStart, &ct));
254e44f6aebSMatthew G. Knepley     gct = (PetscInt)ct;
255462c564dSBarry Smith     PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &gct, 1, MPIU_INT, MPI_MIN, PetscObjectComm((PetscObject)dm)));
256e44f6aebSMatthew G. Knepley     ct = (DMPolytopeType)gct;
257e44f6aebSMatthew G. Knepley     // Work around current bug in PetscDualSpaceSetUp_Lagrange()
258e44f6aebSMatthew G. Knepley     //   Can be seen in plex_tutorials-ex10_1
259e44f6aebSMatthew 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));
2604f9ab2b4SJed Brown   }
261e44f6aebSMatthew G. Knepley   PetscCall(DMSetCoordinateDisc(dm, fe, project));
2629566063dSJacob Faibussowitsch   PetscCall(PetscFEDestroy(&fe));
2639318fe57SMatthew G. Knepley   mesh->coordFunc = coordFunc;
2643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2659318fe57SMatthew G. Knepley }
2669318fe57SMatthew G. Knepley 
2671df5d5c5SMatthew G. Knepley /*@
2681df5d5c5SMatthew G. Knepley   DMPlexCreateDoublet - Creates a mesh of two cells of the specified type, optionally with later refinement.
2691df5d5c5SMatthew G. Knepley 
270d083f849SBarry Smith   Collective
2711df5d5c5SMatthew G. Knepley 
2721df5d5c5SMatthew G. Knepley   Input Parameters:
273a1cb98faSBarry Smith + comm            - The communicator for the `DM` object
2741df5d5c5SMatthew G. Knepley . dim             - The spatial dimension
2751df5d5c5SMatthew G. Knepley . simplex         - Flag for simplicial cells, otherwise they are tensor product cells
2761df5d5c5SMatthew G. Knepley . interpolate     - Flag to create intermediate mesh pieces (edges, faces)
2771df5d5c5SMatthew G. Knepley - refinementLimit - A nonzero number indicates the largest admissible volume for a refined cell
2781df5d5c5SMatthew G. Knepley 
2791df5d5c5SMatthew G. Knepley   Output Parameter:
28060225df5SJacob Faibussowitsch . newdm - The `DM` object
2811df5d5c5SMatthew G. Knepley 
2821df5d5c5SMatthew G. Knepley   Level: beginner
2831df5d5c5SMatthew G. Knepley 
2841cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMSetType()`, `DMCreate()`
2851df5d5c5SMatthew G. Knepley @*/
286d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateDoublet(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscBool interpolate, PetscReal refinementLimit, DM *newdm)
287d71ae5a4SJacob Faibussowitsch {
2881df5d5c5SMatthew G. Knepley   DM          dm;
2891df5d5c5SMatthew G. Knepley   PetscMPIInt rank;
2901df5d5c5SMatthew G. Knepley 
2911df5d5c5SMatthew G. Knepley   PetscFunctionBegin;
2929566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, &dm));
2939566063dSJacob Faibussowitsch   PetscCall(DMSetType(dm, DMPLEX));
2949566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
29546139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
2969566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
297ce78fa2fSMatthew G. Knepley   switch (dim) {
298ce78fa2fSMatthew G. Knepley   case 2:
2999566063dSJacob Faibussowitsch     if (simplex) PetscCall(PetscObjectSetName((PetscObject)dm, "triangular"));
3009566063dSJacob Faibussowitsch     else PetscCall(PetscObjectSetName((PetscObject)dm, "quadrilateral"));
301ce78fa2fSMatthew G. Knepley     break;
302ce78fa2fSMatthew G. Knepley   case 3:
3039566063dSJacob Faibussowitsch     if (simplex) PetscCall(PetscObjectSetName((PetscObject)dm, "tetrahedral"));
3049566063dSJacob Faibussowitsch     else PetscCall(PetscObjectSetName((PetscObject)dm, "hexahedral"));
305ce78fa2fSMatthew G. Knepley     break;
306d71ae5a4SJacob Faibussowitsch   default:
307d71ae5a4SJacob Faibussowitsch     SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %" PetscInt_FMT, dim);
308ce78fa2fSMatthew G. Knepley   }
3091df5d5c5SMatthew G. Knepley   if (rank) {
3101df5d5c5SMatthew G. Knepley     PetscInt numPoints[2] = {0, 0};
3119566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, NULL, NULL, NULL, NULL));
3121df5d5c5SMatthew G. Knepley   } else {
3131df5d5c5SMatthew G. Knepley     switch (dim) {
3141df5d5c5SMatthew G. Knepley     case 2:
3151df5d5c5SMatthew G. Knepley       if (simplex) {
3161df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {4, 2};
3171df5d5c5SMatthew G. Knepley         PetscInt    coneSize[6]         = {3, 3, 0, 0, 0, 0};
3181df5d5c5SMatthew G. Knepley         PetscInt    cones[6]            = {2, 3, 4, 5, 4, 3};
3191df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
3201df5d5c5SMatthew G. Knepley         PetscScalar vertexCoords[8]     = {-0.5, 0.5, 0.0, 0.0, 0.0, 1.0, 0.5, 0.5};
3211df5d5c5SMatthew G. Knepley 
3229566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
3231df5d5c5SMatthew G. Knepley       } else {
3241df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {6, 2};
3251df5d5c5SMatthew G. Knepley         PetscInt    coneSize[8]         = {4, 4, 0, 0, 0, 0, 0, 0};
3261df5d5c5SMatthew G. Knepley         PetscInt    cones[8]            = {2, 3, 4, 5, 3, 6, 7, 4};
3271df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
3281df5d5c5SMatthew 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};
3291df5d5c5SMatthew G. Knepley 
3309566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
3311df5d5c5SMatthew G. Knepley       }
3321df5d5c5SMatthew G. Knepley       break;
3331df5d5c5SMatthew G. Knepley     case 3:
3341df5d5c5SMatthew G. Knepley       if (simplex) {
3351df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {5, 2};
3361df5d5c5SMatthew G. Knepley         PetscInt    coneSize[7]         = {4, 4, 0, 0, 0, 0, 0};
3371df5d5c5SMatthew G. Knepley         PetscInt    cones[8]            = {4, 3, 5, 2, 5, 3, 4, 6};
3381df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
3391df5d5c5SMatthew 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};
3401df5d5c5SMatthew G. Knepley 
3419566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
3421df5d5c5SMatthew G. Knepley       } else {
3431df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]         = {12, 2};
3441df5d5c5SMatthew G. Knepley         PetscInt    coneSize[14]         = {8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
3451df5d5c5SMatthew G. Knepley         PetscInt    cones[16]            = {2, 3, 4, 5, 6, 7, 8, 9, 5, 4, 10, 11, 7, 12, 13, 8};
3461df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
3479371c9d4SSatish 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};
3481df5d5c5SMatthew G. Knepley 
3499566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
3501df5d5c5SMatthew G. Knepley       }
3511df5d5c5SMatthew G. Knepley       break;
352d71ae5a4SJacob Faibussowitsch     default:
353d71ae5a4SJacob Faibussowitsch       SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %" PetscInt_FMT, dim);
3541df5d5c5SMatthew G. Knepley     }
3551df5d5c5SMatthew G. Knepley   }
35646139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
3571df5d5c5SMatthew G. Knepley   *newdm = dm;
3581df5d5c5SMatthew G. Knepley   if (refinementLimit > 0.0) {
3591df5d5c5SMatthew G. Knepley     DM          rdm;
3601df5d5c5SMatthew G. Knepley     const char *name;
3611df5d5c5SMatthew G. Knepley 
3629566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementUniform(*newdm, PETSC_FALSE));
3639566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementLimit(*newdm, refinementLimit));
3649566063dSJacob Faibussowitsch     PetscCall(DMRefine(*newdm, comm, &rdm));
3659566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)*newdm, &name));
3669566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)rdm, name));
3679566063dSJacob Faibussowitsch     PetscCall(DMDestroy(newdm));
3681df5d5c5SMatthew G. Knepley     *newdm = rdm;
3691df5d5c5SMatthew G. Knepley   }
3701df5d5c5SMatthew G. Knepley   if (interpolate) {
3715fd9971aSMatthew G. Knepley     DM idm;
3721df5d5c5SMatthew G. Knepley 
3739566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*newdm, &idm));
3749566063dSJacob Faibussowitsch     PetscCall(DMDestroy(newdm));
3751df5d5c5SMatthew G. Knepley     *newdm = idm;
3761df5d5c5SMatthew G. Knepley   }
3773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3781df5d5c5SMatthew G. Knepley }
3791df5d5c5SMatthew G. Knepley 
380d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
381d71ae5a4SJacob Faibussowitsch {
3829318fe57SMatthew G. Knepley   const PetscInt numVertices    = 2;
3839318fe57SMatthew G. Knepley   PetscInt       markerRight    = 1;
3849318fe57SMatthew G. Knepley   PetscInt       markerLeft     = 1;
3859318fe57SMatthew G. Knepley   PetscBool      markerSeparate = PETSC_FALSE;
3869318fe57SMatthew G. Knepley   Vec            coordinates;
3879318fe57SMatthew G. Knepley   PetscSection   coordSection;
3889318fe57SMatthew G. Knepley   PetscScalar   *coords;
3899318fe57SMatthew G. Knepley   PetscInt       coordSize;
3909318fe57SMatthew G. Knepley   PetscMPIInt    rank;
3919318fe57SMatthew G. Knepley   PetscInt       cdim = 1, v;
392552f7358SJed Brown 
3939318fe57SMatthew G. Knepley   PetscFunctionBegin;
3949566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
3959318fe57SMatthew G. Knepley   if (markerSeparate) {
3969318fe57SMatthew G. Knepley     markerRight = 2;
3979318fe57SMatthew G. Knepley     markerLeft  = 1;
3989318fe57SMatthew G. Knepley   }
3999566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
400c5853193SPierre Jolivet   if (rank == 0) {
4019566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numVertices));
4029566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
4039566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", 0, markerLeft));
4049566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", 1, markerRight));
4059318fe57SMatthew G. Knepley   }
4069566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
4079566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
4089318fe57SMatthew G. Knepley   /* Build coordinates */
4099566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, cdim));
4109566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
4119566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
4129566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, 0, numVertices));
4139566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, cdim));
4149318fe57SMatthew G. Knepley   for (v = 0; v < numVertices; ++v) {
4159566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, cdim));
4169566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, cdim));
4179318fe57SMatthew G. Knepley   }
4189566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
4199566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
4209566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
4219566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
4229566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
4239566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, cdim));
4249566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
4259566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
4269318fe57SMatthew G. Knepley   coords[0] = lower[0];
4279318fe57SMatthew G. Knepley   coords[1] = upper[0];
4289566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
4299566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
4309566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
4313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4329318fe57SMatthew G. Knepley }
43326492d91SMatthew G. Knepley 
434d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
435d71ae5a4SJacob Faibussowitsch {
4361df21d24SMatthew G. Knepley   const PetscInt numVertices    = (edges[0] + 1) * (edges[1] + 1);
4371df21d24SMatthew G. Knepley   const PetscInt numEdges       = edges[0] * (edges[1] + 1) + (edges[0] + 1) * edges[1];
438552f7358SJed Brown   PetscInt       markerTop      = 1;
439552f7358SJed Brown   PetscInt       markerBottom   = 1;
440552f7358SJed Brown   PetscInt       markerRight    = 1;
441552f7358SJed Brown   PetscInt       markerLeft     = 1;
442552f7358SJed Brown   PetscBool      markerSeparate = PETSC_FALSE;
443552f7358SJed Brown   Vec            coordinates;
444552f7358SJed Brown   PetscSection   coordSection;
445552f7358SJed Brown   PetscScalar   *coords;
446552f7358SJed Brown   PetscInt       coordSize;
447552f7358SJed Brown   PetscMPIInt    rank;
448552f7358SJed Brown   PetscInt       v, vx, vy;
449552f7358SJed Brown 
450552f7358SJed Brown   PetscFunctionBegin;
4519566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
452552f7358SJed Brown   if (markerSeparate) {
4531df21d24SMatthew G. Knepley     markerTop    = 3;
4541df21d24SMatthew G. Knepley     markerBottom = 1;
4551df21d24SMatthew G. Knepley     markerRight  = 2;
4561df21d24SMatthew G. Knepley     markerLeft   = 4;
457552f7358SJed Brown   }
4589566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
459dd400576SPatrick Sanan   if (rank == 0) {
460552f7358SJed Brown     PetscInt e, ex, ey;
461552f7358SJed Brown 
4629566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numEdges + numVertices));
46348a46eb9SPierre Jolivet     for (e = 0; e < numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2));
4649566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
465552f7358SJed Brown     for (vx = 0; vx <= edges[0]; vx++) {
466552f7358SJed Brown       for (ey = 0; ey < edges[1]; ey++) {
467552f7358SJed Brown         PetscInt edge   = vx * edges[1] + ey + edges[0] * (edges[1] + 1);
468552f7358SJed Brown         PetscInt vertex = ey * (edges[0] + 1) + vx + numEdges;
469da80777bSKarl Rupp         PetscInt cone[2];
470552f7358SJed Brown 
4719371c9d4SSatish Balay         cone[0] = vertex;
4729371c9d4SSatish Balay         cone[1] = vertex + edges[0] + 1;
4739566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, edge, cone));
474552f7358SJed Brown         if (vx == edges[0]) {
4759566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
4769566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
477552f7358SJed Brown           if (ey == edges[1] - 1) {
4789566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
4799566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerRight));
480552f7358SJed Brown           }
481552f7358SJed Brown         } else if (vx == 0) {
4829566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
4839566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
484552f7358SJed Brown           if (ey == edges[1] - 1) {
4859566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
4869566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerLeft));
487552f7358SJed Brown           }
488552f7358SJed Brown         }
489552f7358SJed Brown       }
490552f7358SJed Brown     }
491552f7358SJed Brown     for (vy = 0; vy <= edges[1]; vy++) {
492552f7358SJed Brown       for (ex = 0; ex < edges[0]; ex++) {
493552f7358SJed Brown         PetscInt edge   = vy * edges[0] + ex;
494552f7358SJed Brown         PetscInt vertex = vy * (edges[0] + 1) + ex + numEdges;
495da80777bSKarl Rupp         PetscInt cone[2];
496552f7358SJed Brown 
4979371c9d4SSatish Balay         cone[0] = vertex;
4989371c9d4SSatish Balay         cone[1] = vertex + 1;
4999566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, edge, cone));
500552f7358SJed Brown         if (vy == edges[1]) {
5019566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
5029566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
503552f7358SJed Brown           if (ex == edges[0] - 1) {
5049566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
5059566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerTop));
506552f7358SJed Brown           }
507552f7358SJed Brown         } else if (vy == 0) {
5089566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
5099566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
510552f7358SJed Brown           if (ex == edges[0] - 1) {
5119566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
5129566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerBottom));
513552f7358SJed Brown           }
514552f7358SJed Brown         }
515552f7358SJed Brown       }
516552f7358SJed Brown     }
517552f7358SJed Brown   }
5189566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
5199566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
520552f7358SJed Brown   /* Build coordinates */
5219566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, 2));
5229566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
5239566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
5249566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, numEdges, numEdges + numVertices));
5259566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, 2));
526552f7358SJed Brown   for (v = numEdges; v < numEdges + numVertices; ++v) {
5279566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, 2));
5289566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, 2));
529552f7358SJed Brown   }
5309566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
5319566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
5329566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
5339566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
5349566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
5359566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, 2));
5369566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
5379566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
538552f7358SJed Brown   for (vy = 0; vy <= edges[1]; ++vy) {
539552f7358SJed Brown     for (vx = 0; vx <= edges[0]; ++vx) {
540552f7358SJed Brown       coords[(vy * (edges[0] + 1) + vx) * 2 + 0] = lower[0] + ((upper[0] - lower[0]) / edges[0]) * vx;
541552f7358SJed Brown       coords[(vy * (edges[0] + 1) + vx) * 2 + 1] = lower[1] + ((upper[1] - lower[1]) / edges[1]) * vy;
542552f7358SJed Brown     }
543552f7358SJed Brown   }
5449566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
5459566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
5469566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
5473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
548552f7358SJed Brown }
549552f7358SJed Brown 
550d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt faces[])
551d71ae5a4SJacob Faibussowitsch {
5529e8abbc3SMichael Lange   PetscInt     vertices[3], numVertices;
5537b59f5a9SMichael Lange   PetscInt     numFaces       = 2 * faces[0] * faces[1] + 2 * faces[1] * faces[2] + 2 * faces[0] * faces[2];
554c2df9bbfSMatthew G. Knepley   PetscInt     markerTop      = 1;
555c2df9bbfSMatthew G. Knepley   PetscInt     markerBottom   = 1;
556c2df9bbfSMatthew G. Knepley   PetscInt     markerFront    = 1;
557c2df9bbfSMatthew G. Knepley   PetscInt     markerBack     = 1;
558c2df9bbfSMatthew G. Knepley   PetscInt     markerRight    = 1;
559c2df9bbfSMatthew G. Knepley   PetscInt     markerLeft     = 1;
560c2df9bbfSMatthew G. Knepley   PetscBool    markerSeparate = PETSC_FALSE;
561552f7358SJed Brown   Vec          coordinates;
562552f7358SJed Brown   PetscSection coordSection;
563552f7358SJed Brown   PetscScalar *coords;
564552f7358SJed Brown   PetscInt     coordSize;
565552f7358SJed Brown   PetscMPIInt  rank;
566552f7358SJed Brown   PetscInt     v, vx, vy, vz;
5677b59f5a9SMichael Lange   PetscInt     voffset, iface = 0, cone[4];
568552f7358SJed Brown 
569552f7358SJed Brown   PetscFunctionBegin;
5701dca8a05SBarry 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");
5719566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
572c2df9bbfSMatthew G. Knepley   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
573c2df9bbfSMatthew G. Knepley   if (markerSeparate) {
574c2df9bbfSMatthew G. Knepley     markerBottom = 1;
575c2df9bbfSMatthew G. Knepley     markerTop    = 2;
576c2df9bbfSMatthew G. Knepley     markerFront  = 3;
577c2df9bbfSMatthew G. Knepley     markerBack   = 4;
578c2df9bbfSMatthew G. Knepley     markerRight  = 5;
579c2df9bbfSMatthew G. Knepley     markerLeft   = 6;
580c2df9bbfSMatthew G. Knepley   }
5819371c9d4SSatish Balay   vertices[0] = faces[0] + 1;
5829371c9d4SSatish Balay   vertices[1] = faces[1] + 1;
5839371c9d4SSatish Balay   vertices[2] = faces[2] + 1;
5849e8abbc3SMichael Lange   numVertices = vertices[0] * vertices[1] * vertices[2];
585dd400576SPatrick Sanan   if (rank == 0) {
586552f7358SJed Brown     PetscInt f;
587552f7358SJed Brown 
5889566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numFaces + numVertices));
58948a46eb9SPierre Jolivet     for (f = 0; f < numFaces; ++f) PetscCall(DMPlexSetConeSize(dm, f, 4));
5909566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
5917b59f5a9SMichael Lange 
5927b59f5a9SMichael Lange     /* Side 0 (Top) */
5937b59f5a9SMichael Lange     for (vy = 0; vy < faces[1]; vy++) {
5947b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5957b59f5a9SMichael Lange         voffset = numFaces + vertices[0] * vertices[1] * (vertices[2] - 1) + vy * vertices[0] + vx;
5969371c9d4SSatish Balay         cone[0] = voffset;
5979371c9d4SSatish Balay         cone[1] = voffset + 1;
5989371c9d4SSatish Balay         cone[2] = voffset + vertices[0] + 1;
5999371c9d4SSatish Balay         cone[3] = voffset + vertices[0];
6009566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
601c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerTop));
602c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerTop));
603c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerTop));
604c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerTop));
605c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 1, markerTop));
6067b59f5a9SMichael Lange         iface++;
607552f7358SJed Brown       }
608552f7358SJed Brown     }
6097b59f5a9SMichael Lange 
6107b59f5a9SMichael Lange     /* Side 1 (Bottom) */
6117b59f5a9SMichael Lange     for (vy = 0; vy < faces[1]; vy++) {
6127b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
6137b59f5a9SMichael Lange         voffset = numFaces + vy * (faces[0] + 1) + vx;
6149371c9d4SSatish Balay         cone[0] = voffset + 1;
6159371c9d4SSatish Balay         cone[1] = voffset;
6169371c9d4SSatish Balay         cone[2] = voffset + vertices[0];
6179371c9d4SSatish Balay         cone[3] = voffset + vertices[0] + 1;
6189566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
619c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerBottom));
620c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerBottom));
621c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerBottom));
622c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerBottom));
623c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 1, markerBottom));
6247b59f5a9SMichael Lange         iface++;
625552f7358SJed Brown       }
626552f7358SJed Brown     }
6277b59f5a9SMichael Lange 
6287b59f5a9SMichael Lange     /* Side 2 (Front) */
6297b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
6307b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
6317b59f5a9SMichael Lange         voffset = numFaces + vz * vertices[0] * vertices[1] + vx;
6329371c9d4SSatish Balay         cone[0] = voffset;
6339371c9d4SSatish Balay         cone[1] = voffset + 1;
6349371c9d4SSatish Balay         cone[2] = voffset + vertices[0] * vertices[1] + 1;
6359371c9d4SSatish Balay         cone[3] = voffset + vertices[0] * vertices[1];
6369566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
637c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerFront));
638c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerFront));
639c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerFront));
640c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 0, markerFront));
641c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 1, markerFront));
6427b59f5a9SMichael Lange         iface++;
643552f7358SJed Brown       }
6447b59f5a9SMichael Lange     }
6457b59f5a9SMichael Lange 
6467b59f5a9SMichael Lange     /* Side 3 (Back) */
6477b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
6487b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
6497b59f5a9SMichael Lange         voffset = numFaces + vz * vertices[0] * vertices[1] + vertices[0] * (vertices[1] - 1) + vx;
6509371c9d4SSatish Balay         cone[0] = voffset + vertices[0] * vertices[1];
6519371c9d4SSatish Balay         cone[1] = voffset + vertices[0] * vertices[1] + 1;
6529371c9d4SSatish Balay         cone[2] = voffset + 1;
6539371c9d4SSatish Balay         cone[3] = voffset;
6549566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
655c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerBack));
656c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerBack));
657c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerBack));
658c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 0, markerBack));
659c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 1, markerBack));
6607b59f5a9SMichael Lange         iface++;
6617b59f5a9SMichael Lange       }
6627b59f5a9SMichael Lange     }
6637b59f5a9SMichael Lange 
6647b59f5a9SMichael Lange     /* Side 4 (Left) */
6657b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
6667b59f5a9SMichael Lange       for (vy = 0; vy < faces[1]; vy++) {
6677b59f5a9SMichael Lange         voffset = numFaces + vz * vertices[0] * vertices[1] + vy * vertices[0];
6689371c9d4SSatish Balay         cone[0] = voffset;
6699371c9d4SSatish Balay         cone[1] = voffset + vertices[0] * vertices[1];
6709371c9d4SSatish Balay         cone[2] = voffset + vertices[0] * vertices[1] + vertices[0];
6719371c9d4SSatish Balay         cone[3] = voffset + vertices[0];
6729566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
673c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerLeft));
674c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerLeft));
675c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerLeft));
676c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[1] + 0, markerLeft));
677c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + vertices[0], markerLeft));
6787b59f5a9SMichael Lange         iface++;
6797b59f5a9SMichael Lange       }
6807b59f5a9SMichael Lange     }
6817b59f5a9SMichael Lange 
6827b59f5a9SMichael Lange     /* Side 5 (Right) */
6837b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
6847b59f5a9SMichael Lange       for (vy = 0; vy < faces[1]; vy++) {
685aab5bcd8SJed Brown         voffset = numFaces + vz * vertices[0] * vertices[1] + vy * vertices[0] + faces[0];
6869371c9d4SSatish Balay         cone[0] = voffset + vertices[0] * vertices[1];
6879371c9d4SSatish Balay         cone[1] = voffset;
6889371c9d4SSatish Balay         cone[2] = voffset + vertices[0];
6899371c9d4SSatish Balay         cone[3] = voffset + vertices[0] * vertices[1] + vertices[0];
6909566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
691c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerRight));
692c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerRight));
693c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerRight));
694c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 0, markerRight));
695c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + vertices[0], markerRight));
6967b59f5a9SMichael Lange         iface++;
6977b59f5a9SMichael Lange       }
698552f7358SJed Brown     }
699552f7358SJed Brown   }
7009566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
7019566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
702552f7358SJed Brown   /* Build coordinates */
7039566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, 3));
7049566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
7059566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
7069566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, numFaces, numFaces + numVertices));
7079566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, 3));
708552f7358SJed Brown   for (v = numFaces; v < numFaces + numVertices; ++v) {
7099566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, 3));
7109566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, 3));
711552f7358SJed Brown   }
7129566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
7139566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
7149566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
7159566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
7169566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
7179566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, 3));
7189566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
7199566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
720552f7358SJed Brown   for (vz = 0; vz <= faces[2]; ++vz) {
721552f7358SJed Brown     for (vy = 0; vy <= faces[1]; ++vy) {
722552f7358SJed Brown       for (vx = 0; vx <= faces[0]; ++vx) {
723552f7358SJed Brown         coords[((vz * (faces[1] + 1) + vy) * (faces[0] + 1) + vx) * 3 + 0] = lower[0] + ((upper[0] - lower[0]) / faces[0]) * vx;
724552f7358SJed Brown         coords[((vz * (faces[1] + 1) + vy) * (faces[0] + 1) + vx) * 3 + 1] = lower[1] + ((upper[1] - lower[1]) / faces[1]) * vy;
725552f7358SJed Brown         coords[((vz * (faces[1] + 1) + vy) * (faces[0] + 1) + vx) * 3 + 2] = lower[2] + ((upper[2] - lower[2]) / faces[2]) * vz;
726552f7358SJed Brown       }
727552f7358SJed Brown     }
728552f7358SJed Brown   }
7299566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
7309566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
7319566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
7323ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
733552f7358SJed Brown }
734552f7358SJed Brown 
735d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate)
736d71ae5a4SJacob Faibussowitsch {
7379318fe57SMatthew G. Knepley   PetscFunctionBegin;
7389318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
73946139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
7409566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim - 1));
7419566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, dim));
7429318fe57SMatthew G. Knepley   switch (dim) {
743d71ae5a4SJacob Faibussowitsch   case 1:
744d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(dm, lower, upper, faces));
745d71ae5a4SJacob Faibussowitsch     break;
746d71ae5a4SJacob Faibussowitsch   case 2:
747d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(dm, lower, upper, faces));
748d71ae5a4SJacob Faibussowitsch     break;
749d71ae5a4SJacob Faibussowitsch   case 3:
750d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(dm, lower, upper, faces));
751d71ae5a4SJacob Faibussowitsch     break;
752d71ae5a4SJacob Faibussowitsch   default:
753d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Dimension not supported: %" PetscInt_FMT, dim);
7549318fe57SMatthew G. Knepley   }
75546139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
7569566063dSJacob Faibussowitsch   if (interpolate) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
7573ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7589318fe57SMatthew G. Knepley }
7599318fe57SMatthew G. Knepley 
7609318fe57SMatthew G. Knepley /*@C
7619318fe57SMatthew G. Knepley   DMPlexCreateBoxSurfaceMesh - Creates a mesh on the surface of the tensor product of unit intervals (box) using tensor cells (hexahedra).
7629318fe57SMatthew G. Knepley 
7639318fe57SMatthew G. Knepley   Collective
7649318fe57SMatthew G. Knepley 
7659318fe57SMatthew G. Knepley   Input Parameters:
766a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
76720f4b53cSBarry Smith . dim         - The spatial dimension of the box, so the resulting mesh is has dimension `dim`-1
76820f4b53cSBarry Smith . faces       - Number of faces per dimension, or `NULL` for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
76920f4b53cSBarry Smith . lower       - The lower left corner, or `NULL` for (0, 0, 0)
77020f4b53cSBarry Smith . upper       - The upper right corner, or `NULL` for (1, 1, 1)
7719318fe57SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
7729318fe57SMatthew G. Knepley 
7739318fe57SMatthew G. Knepley   Output Parameter:
774a1cb98faSBarry Smith . dm - The `DM` object
7759318fe57SMatthew G. Knepley 
7769318fe57SMatthew G. Knepley   Level: beginner
7779318fe57SMatthew G. Knepley 
7781cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMSetFromOptions()`, `DMPlexCreateBoxMesh()`, `DMPlexCreateFromFile()`, `DMSetType()`, `DMCreate()`
7799318fe57SMatthew G. Knepley @*/
780d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateBoxSurfaceMesh(MPI_Comm comm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate, DM *dm)
781d71ae5a4SJacob Faibussowitsch {
7829318fe57SMatthew G. Knepley   PetscInt  fac[3] = {1, 1, 1};
7839318fe57SMatthew G. Knepley   PetscReal low[3] = {0, 0, 0};
7849318fe57SMatthew G. Knepley   PetscReal upp[3] = {1, 1, 1};
7859318fe57SMatthew G. Knepley 
7869318fe57SMatthew G. Knepley   PetscFunctionBegin;
7879566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
7889566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
7899566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(*dm, dim, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, interpolate));
7903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7919318fe57SMatthew G. Knepley }
7929318fe57SMatthew G. Knepley 
793d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateLineMesh_Internal(DM dm, PetscInt segments, PetscReal lower, PetscReal upper, DMBoundaryType bd)
794d71ae5a4SJacob Faibussowitsch {
795fdbf62faSLisandro Dalcin   PetscInt     i, fStart, fEnd, numCells = 0, numVerts = 0;
796fdbf62faSLisandro Dalcin   PetscInt     numPoints[2], *coneSize, *cones, *coneOrientations;
797fdbf62faSLisandro Dalcin   PetscScalar *vertexCoords;
798fdbf62faSLisandro Dalcin   PetscReal    L, maxCell;
799fdbf62faSLisandro Dalcin   PetscBool    markerSeparate = PETSC_FALSE;
800fdbf62faSLisandro Dalcin   PetscInt     markerLeft = 1, faceMarkerLeft = 1;
801fdbf62faSLisandro Dalcin   PetscInt     markerRight = 1, faceMarkerRight = 2;
802fdbf62faSLisandro Dalcin   PetscBool    wrap = (bd == DM_BOUNDARY_PERIODIC || bd == DM_BOUNDARY_TWIST) ? PETSC_TRUE : PETSC_FALSE;
803fdbf62faSLisandro Dalcin   PetscMPIInt  rank;
804fdbf62faSLisandro Dalcin 
805fdbf62faSLisandro Dalcin   PetscFunctionBegin;
8064f572ea9SToby Isaac   PetscAssertPointer(dm, 1);
807fdbf62faSLisandro Dalcin 
8089566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, 1));
8099566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
8109566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
811fdbf62faSLisandro Dalcin 
8129566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
813dd400576SPatrick Sanan   if (rank == 0) numCells = segments;
814dd400576SPatrick Sanan   if (rank == 0) numVerts = segments + (wrap ? 0 : 1);
815fdbf62faSLisandro Dalcin 
8169371c9d4SSatish Balay   numPoints[0] = numVerts;
8179371c9d4SSatish Balay   numPoints[1] = numCells;
8189566063dSJacob Faibussowitsch   PetscCall(PetscMalloc4(numCells + numVerts, &coneSize, numCells * 2, &cones, numCells + numVerts, &coneOrientations, numVerts, &vertexCoords));
8199566063dSJacob Faibussowitsch   PetscCall(PetscArrayzero(coneOrientations, numCells + numVerts));
820ad540459SPierre Jolivet   for (i = 0; i < numCells; ++i) coneSize[i] = 2;
821ad540459SPierre Jolivet   for (i = 0; i < numVerts; ++i) coneSize[numCells + i] = 0;
8229371c9d4SSatish Balay   for (i = 0; i < numCells; ++i) {
8239371c9d4SSatish Balay     cones[2 * i]     = numCells + i % numVerts;
8249371c9d4SSatish Balay     cones[2 * i + 1] = numCells + (i + 1) % numVerts;
8259371c9d4SSatish Balay   }
826ad540459SPierre Jolivet   for (i = 0; i < numVerts; ++i) vertexCoords[i] = lower + (upper - lower) * ((PetscReal)i / (PetscReal)numCells);
8279566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
8289566063dSJacob Faibussowitsch   PetscCall(PetscFree4(coneSize, cones, coneOrientations, vertexCoords));
829fdbf62faSLisandro Dalcin 
8309566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
8319371c9d4SSatish Balay   if (markerSeparate) {
8329371c9d4SSatish Balay     markerLeft  = faceMarkerLeft;
8339371c9d4SSatish Balay     markerRight = faceMarkerRight;
8349371c9d4SSatish Balay   }
835dd400576SPatrick Sanan   if (!wrap && rank == 0) {
8369566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
8379566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", fStart, markerLeft));
8389566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", fEnd - 1, markerRight));
8399566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "Face Sets", fStart, faceMarkerLeft));
8409566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "Face Sets", fEnd - 1, faceMarkerRight));
841fdbf62faSLisandro Dalcin   }
842fdbf62faSLisandro Dalcin   if (wrap) {
843fdbf62faSLisandro Dalcin     L       = upper - lower;
844fdbf62faSLisandro Dalcin     maxCell = (PetscReal)1.1 * (L / (PetscReal)PetscMax(1, segments));
8454fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, &maxCell, &lower, &L));
846fdbf62faSLisandro Dalcin   }
8479566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
8483ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
849fdbf62faSLisandro Dalcin }
850fdbf62faSLisandro Dalcin 
8514054ae39SJames Wright // Creates "Face Sets" label based on the standard box labeling conventions
852d7d2d1d2SJames Wright static PetscErrorCode DMPlexSetBoxLabel_Internal(DM dm, const DMBoundaryType periodicity[])
8534054ae39SJames Wright {
8546ff49feeSJames Wright   DM              cdm;
8556ff49feeSJames Wright   PetscSection    csection;
8566ff49feeSJames Wright   Vec             coordinates;
8574054ae39SJames Wright   DMLabel         label;
8586ff49feeSJames Wright   IS              faces_is;
8592b4f33d9SJames Wright   PetscInt        dim, num_face = 0;
8604054ae39SJames Wright   const PetscInt *faces;
8614054ae39SJames Wright   PetscInt        faceMarkerBottom, faceMarkerTop, faceMarkerFront, faceMarkerBack, faceMarkerRight, faceMarkerLeft;
8624054ae39SJames Wright 
8634054ae39SJames Wright   PetscFunctionBeginUser;
8644054ae39SJames Wright   PetscCall(DMGetDimension(dm, &dim));
865d7c1f440SPierre 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);
8664054ae39SJames Wright   // Get Face Sets label
8674054ae39SJames Wright   PetscCall(DMGetLabel(dm, "Face Sets", &label));
8684054ae39SJames Wright   if (label) {
8694054ae39SJames Wright     PetscCall(DMLabelReset(label));
8704054ae39SJames Wright   } else {
8714054ae39SJames Wright     PetscCall(DMCreateLabel(dm, "Face Sets"));
8724054ae39SJames Wright     PetscCall(DMGetLabel(dm, "Face Sets", &label));
8734054ae39SJames Wright   }
8744054ae39SJames Wright   PetscCall(DMPlexMarkBoundaryFaces(dm, 1, label));
8756ff49feeSJames Wright   PetscCall(DMGetStratumIS(dm, "Face Sets", 1, &faces_is));
8764054ae39SJames Wright 
8774054ae39SJames Wright   switch (dim) {
8784054ae39SJames Wright   case 2:
8794054ae39SJames Wright     faceMarkerTop    = 3;
8804054ae39SJames Wright     faceMarkerBottom = 1;
8814054ae39SJames Wright     faceMarkerRight  = 2;
8824054ae39SJames Wright     faceMarkerLeft   = 4;
8834054ae39SJames Wright     break;
8844054ae39SJames Wright   case 3:
8854054ae39SJames Wright     faceMarkerBottom = 1;
8864054ae39SJames Wright     faceMarkerTop    = 2;
8874054ae39SJames Wright     faceMarkerFront  = 3;
8884054ae39SJames Wright     faceMarkerBack   = 4;
8894054ae39SJames Wright     faceMarkerRight  = 5;
8904054ae39SJames Wright     faceMarkerLeft   = 6;
8914054ae39SJames Wright     break;
8924054ae39SJames Wright   default:
8934054ae39SJames Wright     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Dimension %" PetscInt_FMT " not supported", dim);
8944054ae39SJames Wright   }
8954054ae39SJames Wright 
8962b4f33d9SJames Wright   if (faces_is) PetscCall(ISGetLocalSize(faces_is, &num_face));
8972b4f33d9SJames Wright   if (faces_is) PetscCall(ISGetIndices(faces_is, &faces));
8986ff49feeSJames Wright   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
8996ff49feeSJames Wright   PetscCall(DMGetCoordinateDM(dm, &cdm));
9006ff49feeSJames Wright   PetscCall(DMGetLocalSection(cdm, &csection));
9014054ae39SJames Wright   for (PetscInt f = 0; f < num_face; ++f) {
9026ff49feeSJames Wright     PetscScalar *coords = NULL;
9036ff49feeSJames Wright     PetscInt     face = faces[f], flip = 1, label_value = -1, coords_size;
9044054ae39SJames Wright 
9054054ae39SJames Wright     { // Determine if orientation of face is flipped
9064054ae39SJames Wright       PetscInt        num_cells_support, num_faces, start = -1;
9074054ae39SJames Wright       const PetscInt *orients, *cell_faces, *cells;
9084054ae39SJames Wright 
9094054ae39SJames Wright       PetscCall(DMPlexGetSupport(dm, face, &cells));
9104054ae39SJames Wright       PetscCall(DMPlexGetSupportSize(dm, face, &num_cells_support));
9114054ae39SJames 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);
9124054ae39SJames Wright       PetscCall(DMPlexGetCone(dm, cells[0], &cell_faces));
9134054ae39SJames Wright       PetscCall(DMPlexGetConeSize(dm, cells[0], &num_faces));
9144054ae39SJames Wright       for (PetscInt i = 0; i < num_faces; i++) {
9154054ae39SJames Wright         if (cell_faces[i] == face) start = i;
9164054ae39SJames Wright       }
9174054ae39SJames Wright       PetscCheck(start >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "Could not find face %" PetscInt_FMT " in cone of its support", face);
9184054ae39SJames Wright       PetscCall(DMPlexGetConeOrientation(dm, cells[0], &orients));
9194054ae39SJames Wright       if (orients[start] < 0) flip = -1;
9204054ae39SJames Wright     }
9214054ae39SJames Wright 
9226ff49feeSJames Wright     // Cannot use DMPlexComputeCellGeometryFVM() for high-order geometry, so must calculate normal vectors manually
9236ff49feeSJames Wright     // Use the vertices (depth 0) of coordinate DM to calculate normal vector
9246ff49feeSJames Wright     PetscCall(DMPlexVecGetClosureAtDepth_Internal(cdm, csection, coordinates, face, 0, &coords_size, &coords));
9254054ae39SJames Wright     switch (dim) {
9264054ae39SJames Wright     case 2: {
9276ff49feeSJames Wright       PetscScalar vec[2];
9286ff49feeSJames Wright 
9296ff49feeSJames Wright       for (PetscInt d = 0; d < dim; ++d) vec[d] = flip * (PetscRealPart(coords[1 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
9306ff49feeSJames Wright       PetscScalar normal[] = {vec[1], -vec[0]};
9316ff49feeSJames Wright       if (PetscAbsScalar(normal[0]) > PetscAbsScalar(normal[1])) {
9326ff49feeSJames Wright         label_value = PetscRealPart(normal[0]) > 0 ? faceMarkerRight : faceMarkerLeft;
9334054ae39SJames Wright       } else {
9346ff49feeSJames Wright         label_value = PetscRealPart(normal[1]) > 0 ? faceMarkerTop : faceMarkerBottom;
9354054ae39SJames Wright       }
9364054ae39SJames Wright     } break;
9374054ae39SJames Wright     case 3: {
9386ff49feeSJames Wright       PetscScalar vec1[3], vec2[3], normal[3];
9396ff49feeSJames Wright 
9406ff49feeSJames Wright       for (PetscInt d = 0; d < dim; ++d) {
9416ff49feeSJames Wright         vec1[d] = PetscRealPart(coords[1 * dim + d]) - PetscRealPart(coords[0 * dim + d]);
9426ff49feeSJames Wright         vec2[d] = PetscRealPart(coords[2 * dim + d]) - PetscRealPart(coords[1 * dim + d]);
9436ff49feeSJames Wright       }
9446ff49feeSJames Wright 
9456ff49feeSJames Wright       // Calculate normal vector via cross-product
9466ff49feeSJames Wright       normal[0] = flip * ((vec1[1] * vec2[2]) - (vec1[2] * vec2[1]));
9476ff49feeSJames Wright       normal[1] = flip * ((vec1[2] * vec2[0]) - (vec1[0] * vec2[2]));
9486ff49feeSJames Wright       normal[2] = flip * ((vec1[0] * vec2[1]) - (vec1[1] * vec2[0]));
9496ff49feeSJames Wright 
9506ff49feeSJames Wright       if (PetscAbsScalar(normal[0]) > PetscAbsScalar(normal[1])) {
9516ff49feeSJames Wright         if (PetscAbsScalar(normal[0]) > PetscAbsScalar(normal[2])) {
9526ff49feeSJames Wright           label_value = PetscRealPart(normal[0]) > 0 ? faceMarkerRight : faceMarkerLeft;
9534054ae39SJames Wright         } else {
9546ff49feeSJames Wright           label_value = PetscRealPart(normal[2]) > 0 ? faceMarkerTop : faceMarkerBottom;
9554054ae39SJames Wright         }
9564054ae39SJames Wright       } else {
9576ff49feeSJames Wright         if (PetscAbsScalar(normal[1]) > PetscAbsScalar(normal[2])) {
9586ff49feeSJames Wright           label_value = PetscRealPart(normal[1]) > 0 ? faceMarkerBack : faceMarkerFront;
9594054ae39SJames Wright         } else {
9606ff49feeSJames Wright           label_value = PetscRealPart(normal[2]) > 0 ? faceMarkerTop : faceMarkerBottom;
9614054ae39SJames Wright         }
9624054ae39SJames Wright       }
9634054ae39SJames Wright     } break;
9644054ae39SJames Wright     }
9654054ae39SJames Wright 
9664054ae39SJames Wright     PetscInt previous_label_value; // always 1 due to DMPlexMarkBoundaryFaces call above
9674054ae39SJames Wright     PetscCall(DMGetLabelValue(dm, "Face Sets", face, &previous_label_value));
9684054ae39SJames Wright     PetscCall(DMClearLabelValue(dm, "Face Sets", face, previous_label_value));
9694054ae39SJames Wright     PetscCall(DMSetLabelValue(dm, "Face Sets", face, label_value));
9706ff49feeSJames Wright     PetscCall(DMPlexVecRestoreClosure(cdm, csection, coordinates, face, &coords_size, &coords));
9714054ae39SJames Wright   }
9722b4f33d9SJames Wright   if (faces_is) PetscCall(ISRestoreIndices(faces_is, &faces));
9736ff49feeSJames Wright   PetscCall(ISDestroy(&faces_is));
974d7d2d1d2SJames Wright 
975d7d2d1d2SJames Wright   // Create Isoperiodic SF from newly-created face labels
976d7d2d1d2SJames Wright   PetscSF     periodicsfs[3];
977d7d2d1d2SJames Wright   PetscInt    periodic_sf_index  = 0;
978d7d2d1d2SJames Wright   PetscScalar transform[3][4][4] = {{{0.}}};
979d7d2d1d2SJames Wright   for (PetscInt d = 0; d < dim; d++) {
980d7d2d1d2SJames Wright     IS              donor_is, periodic_is;
981d7d2d1d2SJames Wright     const PetscInt *donor_faces = NULL, *periodic_faces = NULL;
982d7d2d1d2SJames Wright     PetscInt        num_donor = 0, num_periodic = 0;
983d7d2d1d2SJames Wright     PetscSF         centroidsf;
984d7d2d1d2SJames Wright     PetscReal       donor_to_periodic_distance;
985d7d2d1d2SJames Wright     const PetscInt  face_pairings[2][3][2] = {
986d7d2d1d2SJames Wright       // 2D face pairings, {donor, periodic}
987d7d2d1d2SJames Wright       {{4, 2}, {1, 3}},
988d7d2d1d2SJames Wright       // 3D face pairings
989d7d2d1d2SJames Wright       {{5, 6}, {3, 4}, {1, 2}}
990d7d2d1d2SJames Wright     };
991d7d2d1d2SJames Wright 
992d7d2d1d2SJames Wright     if (periodicity[d] != DM_BOUNDARY_PERIODIC) continue;
993d7d2d1d2SJames Wright     {
994d7d2d1d2SJames Wright       // Compute centroidsf, which is the mapping from donor faces to periodic faces
995d7d2d1d2SJames Wright       // Matches the centroid of the faces together, ignoring the periodic direction component (which should not match between donor and periodic face)
996d7d2d1d2SJames Wright       PetscInt     coords_size, centroid_comps = dim - 1;
997d7d2d1d2SJames Wright       PetscScalar *coords = NULL;
998d7d2d1d2SJames Wright       PetscReal   *donor_centroids, *periodic_centroids;
999d7d2d1d2SJames 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
1000d7d2d1d2SJames Wright 
1001d7d2d1d2SJames Wright       PetscCall(DMGetStratumIS(dm, "Face Sets", face_pairings[dim - 2][d][0], &donor_is));
1002d7d2d1d2SJames Wright       PetscCall(DMGetStratumIS(dm, "Face Sets", face_pairings[dim - 2][d][1], &periodic_is));
1003d7d2d1d2SJames Wright       if (donor_is) {
1004d7d2d1d2SJames Wright         PetscCall(ISGetLocalSize(donor_is, &num_donor));
1005d7d2d1d2SJames Wright         PetscCall(ISGetIndices(donor_is, &donor_faces));
1006d7d2d1d2SJames Wright       }
1007d7d2d1d2SJames Wright       if (periodic_is) {
1008d7d2d1d2SJames Wright         PetscCall(ISGetLocalSize(periodic_is, &num_periodic));
1009d7d2d1d2SJames Wright         PetscCall(ISGetIndices(periodic_is, &periodic_faces));
1010d7d2d1d2SJames Wright       }
1011d7d2d1d2SJames Wright       PetscCall(PetscCalloc2(num_donor * centroid_comps, &donor_centroids, num_periodic * centroid_comps, &periodic_centroids));
1012d7d2d1d2SJames Wright       for (PetscInt f = 0; f < num_donor; f++) {
1013d7d2d1d2SJames Wright         PetscInt face = donor_faces[f], num_coords;
1014d7d2d1d2SJames Wright         PetscCall(DMPlexVecGetClosureAtDepth_Internal(cdm, csection, coordinates, face, 0, &coords_size, &coords));
1015d7d2d1d2SJames Wright         num_coords = coords_size / dim;
1016d7d2d1d2SJames Wright         for (PetscInt c = 0; c < num_coords; c++) {
1017d7d2d1d2SJames Wright           PetscInt comp_index = 0;
1018d7d2d1d2SJames Wright           loc_periodic[0]     = PetscRealPart(coords[c * dim + d]);
1019d7d2d1d2SJames Wright           for (PetscInt i = 0; i < dim; i++) {
1020d7d2d1d2SJames Wright             if (i == d) continue; // Periodic direction not used for centroid calculation
1021d7d2d1d2SJames Wright             donor_centroids[f * centroid_comps + comp_index] += PetscRealPart(coords[c * dim + i]) / num_coords;
1022d7d2d1d2SJames Wright             comp_index++;
1023d7d2d1d2SJames Wright           }
1024d7d2d1d2SJames Wright         }
1025d7d2d1d2SJames Wright         PetscCall(DMPlexVecRestoreClosure(cdm, csection, coordinates, face, &coords_size, &coords));
1026d7d2d1d2SJames Wright       }
1027d7d2d1d2SJames Wright 
1028d7d2d1d2SJames Wright       for (PetscInt f = 0; f < num_periodic; f++) {
1029d7d2d1d2SJames Wright         PetscInt face = periodic_faces[f], num_coords;
1030d7d2d1d2SJames Wright         PetscCall(DMPlexVecGetClosureAtDepth_Internal(cdm, csection, coordinates, face, 0, &coords_size, &coords));
1031d7d2d1d2SJames Wright         num_coords = coords_size / dim;
1032d7d2d1d2SJames Wright         for (PetscInt c = 0; c < num_coords; c++) {
1033d7d2d1d2SJames Wright           PetscInt comp_index = 0;
1034d7d2d1d2SJames Wright           loc_periodic[1]     = PetscRealPart(coords[c * dim + d]);
1035d7d2d1d2SJames Wright           for (PetscInt i = 0; i < dim; i++) {
1036d7d2d1d2SJames Wright             if (i == d) continue; // Periodic direction not used for centroid calculation
1037d7d2d1d2SJames Wright             periodic_centroids[f * centroid_comps + comp_index] += PetscRealPart(coords[c * dim + i]) / num_coords;
1038d7d2d1d2SJames Wright             comp_index++;
1039d7d2d1d2SJames Wright           }
1040d7d2d1d2SJames Wright         }
1041d7d2d1d2SJames Wright         PetscCall(DMPlexVecRestoreClosure(cdm, csection, coordinates, face, &coords_size, &coords));
1042d7d2d1d2SJames Wright       }
1043d7d2d1d2SJames Wright       PetscCallMPI(MPIU_Allreduce(loc_periodic, loc_periodic_global, 2, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject)dm)));
1044d7d2d1d2SJames Wright       donor_to_periodic_distance = loc_periodic_global[1] - loc_periodic_global[0];
1045d7d2d1d2SJames Wright 
1046d7d2d1d2SJames Wright       PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &centroidsf));
1047d7d2d1d2SJames Wright       PetscCall(PetscSFSetGraphFromCoordinates(centroidsf, num_donor, num_periodic, centroid_comps, 1e-10, donor_centroids, periodic_centroids));
1048d7d2d1d2SJames Wright       PetscCall(PetscSFViewFromOptions(centroidsf, NULL, "-dm_plex_box_label_centroid_sf_view"));
1049d7d2d1d2SJames Wright       PetscCall(PetscFree2(donor_centroids, periodic_centroids));
1050d7d2d1d2SJames Wright     }
1051d7d2d1d2SJames Wright 
1052d7d2d1d2SJames Wright     { // Create Isoperiodic SF using centroidsSF
1053d7d2d1d2SJames Wright       PetscInt           pStart, pEnd;
1054d7d2d1d2SJames Wright       PetscInt          *leaf_faces;
1055d7d2d1d2SJames Wright       const PetscSFNode *firemote;
1056d7d2d1d2SJames Wright       PetscSFNode       *isoperiodic_leaves;
1057d7d2d1d2SJames Wright 
1058d7d2d1d2SJames Wright       PetscCall(PetscMalloc1(num_periodic, &leaf_faces));
1059d7d2d1d2SJames Wright       PetscCall(PetscSFBcastBegin(centroidsf, MPIU_INT, donor_faces, leaf_faces, MPI_REPLACE));
1060d7d2d1d2SJames Wright       PetscCall(PetscSFBcastEnd(centroidsf, MPIU_INT, donor_faces, leaf_faces, MPI_REPLACE));
1061d7d2d1d2SJames Wright 
1062d7d2d1d2SJames Wright       PetscCall(PetscMalloc1(num_periodic, &isoperiodic_leaves));
1063d7d2d1d2SJames Wright       PetscCall(PetscSFGetGraph(centroidsf, NULL, NULL, NULL, &firemote));
1064d7d2d1d2SJames Wright       for (PetscInt l = 0; l < num_periodic; ++l) {
1065d7d2d1d2SJames Wright         isoperiodic_leaves[l].index = leaf_faces[l];
1066d7d2d1d2SJames Wright         isoperiodic_leaves[l].rank  = firemote[l].rank;
1067d7d2d1d2SJames Wright       }
1068d7d2d1d2SJames Wright 
1069d7d2d1d2SJames Wright       PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
1070d7d2d1d2SJames Wright       PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &periodicsfs[periodic_sf_index]));
1071d7d2d1d2SJames Wright       PetscCall(PetscSFSetGraph(periodicsfs[periodic_sf_index], pEnd - pStart, num_periodic, (PetscInt *)periodic_faces, PETSC_COPY_VALUES, isoperiodic_leaves, PETSC_OWN_POINTER));
1072d7d2d1d2SJames Wright       PetscCall(PetscSFViewFromOptions(periodicsfs[periodic_sf_index], NULL, "-dm_plex_box_label_periodic_sf_view"));
1073d7d2d1d2SJames Wright       PetscCall(PetscFree(leaf_faces));
1074d7d2d1d2SJames Wright     }
1075d7d2d1d2SJames Wright 
1076d7d2d1d2SJames Wright     transform[periodic_sf_index][0][0] = 1;
1077d7d2d1d2SJames Wright     transform[periodic_sf_index][1][1] = 1;
1078d7d2d1d2SJames Wright     transform[periodic_sf_index][2][2] = 1;
1079d7d2d1d2SJames Wright     transform[periodic_sf_index][3][3] = 1;
1080d7d2d1d2SJames Wright     transform[periodic_sf_index][d][3] = donor_to_periodic_distance;
1081d7d2d1d2SJames Wright 
1082d7d2d1d2SJames Wright     periodic_sf_index++;
1083d7d2d1d2SJames Wright     PetscCall(PetscSFDestroy(&centroidsf));
1084d7d2d1d2SJames Wright     if (donor_is) {
1085d7d2d1d2SJames Wright       PetscCall(ISRestoreIndices(donor_is, &donor_faces));
1086d7d2d1d2SJames Wright       PetscCall(ISDestroy(&donor_is));
1087d7d2d1d2SJames Wright     }
1088d7d2d1d2SJames Wright     if (periodic_is) {
1089d7d2d1d2SJames Wright       PetscCall(ISRestoreIndices(periodic_is, &periodic_faces));
1090d7d2d1d2SJames Wright       PetscCall(ISDestroy(&periodic_is));
1091d7d2d1d2SJames Wright     }
1092d7d2d1d2SJames Wright     PetscCall(DMClearLabelStratum(dm, "Face Sets", face_pairings[dim - 2][d][0]));
1093d7d2d1d2SJames Wright     PetscCall(DMClearLabelStratum(dm, "Face Sets", face_pairings[dim - 2][d][1]));
1094d7d2d1d2SJames Wright   }
1095d7d2d1d2SJames Wright   PetscCall(DMPlexSetIsoperiodicFaceSF(dm, periodic_sf_index, periodicsfs));
1096d7d2d1d2SJames Wright   PetscCall(DMPlexSetIsoperiodicFaceTransform(dm, periodic_sf_index, (const PetscScalar *)transform));
1097d7d2d1d2SJames Wright   for (PetscInt p = 0; p < periodic_sf_index; p++) PetscCall(PetscSFDestroy(&periodicsfs[p]));
1098d7d2d1d2SJames Wright 
1099d7d2d1d2SJames Wright   { // Update coordinate DM with new Face Sets label
1100d7d2d1d2SJames Wright     DM      cdm;
1101d7d2d1d2SJames Wright     DMLabel oldFaceSets, newFaceSets;
1102d7d2d1d2SJames Wright     PetscCall(DMGetCoordinateDM(dm, &cdm));
1103d7d2d1d2SJames Wright     PetscCall(DMGetLabel(cdm, "Face Sets", &oldFaceSets));
1104d7d2d1d2SJames Wright     if (oldFaceSets) PetscCall(DMRemoveLabelBySelf(cdm, &oldFaceSets, PETSC_FALSE));
1105d7d2d1d2SJames Wright     PetscCall(DMLabelDuplicate(label, &newFaceSets));
1106d7d2d1d2SJames Wright     PetscCall(DMAddLabel(cdm, newFaceSets));
1107d7d2d1d2SJames Wright     PetscCall(DMLabelDestroy(&newFaceSets));
1108d7d2d1d2SJames Wright   }
11094054ae39SJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
11104054ae39SJames Wright }
11114054ae39SJames Wright 
1112d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxMesh_Simplex_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate)
1113d71ae5a4SJacob Faibussowitsch {
11149318fe57SMatthew G. Knepley   DM      boundary, vol;
1115c22d3578SMatthew G. Knepley   DMLabel bdlabel;
1116d6218766SMatthew G. Knepley 
1117d6218766SMatthew G. Knepley   PetscFunctionBegin;
11184f572ea9SToby Isaac   PetscAssertPointer(dm, 1);
1119c22d3578SMatthew 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");
11209566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &boundary));
11219566063dSJacob Faibussowitsch   PetscCall(DMSetType(boundary, DMPLEX));
11229566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(boundary, dim, faces, lower, upper, PETSC_FALSE));
11239566063dSJacob Faibussowitsch   PetscCall(DMPlexGenerate(boundary, NULL, interpolate, &vol));
1124c22d3578SMatthew G. Knepley   PetscCall(DMGetLabel(vol, "marker", &bdlabel));
1125c22d3578SMatthew G. Knepley   if (bdlabel) PetscCall(DMPlexLabelComplete(vol, bdlabel));
11265de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_FALSE, vol));
112769d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &vol));
11284054ae39SJames Wright   if (interpolate) {
11294054ae39SJames Wright     PetscCall(DMPlexInterpolateInPlace_Internal(dm));
1130d7d2d1d2SJames Wright     PetscCall(DMPlexSetBoxLabel_Internal(dm, periodicity));
11314054ae39SJames Wright   }
11329566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&boundary));
11333ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1134d6218766SMatthew G. Knepley }
1135d6218766SMatthew G. Knepley 
1136d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCubeMesh_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], DMBoundaryType bdX, DMBoundaryType bdY, DMBoundaryType bdZ)
1137d71ae5a4SJacob Faibussowitsch {
1138ed0e4b50SMatthew G. Knepley   DMLabel     cutLabel  = NULL;
1139f4eb4c5dSMatthew G. Knepley   PetscInt    markerTop = 1, faceMarkerTop = 1;
1140f4eb4c5dSMatthew G. Knepley   PetscInt    markerBottom = 1, faceMarkerBottom = 1;
1141f4eb4c5dSMatthew G. Knepley   PetscInt    markerFront = 1, faceMarkerFront = 1;
1142f4eb4c5dSMatthew G. Knepley   PetscInt    markerBack = 1, faceMarkerBack = 1;
1143f4eb4c5dSMatthew G. Knepley   PetscInt    markerRight = 1, faceMarkerRight = 1;
1144f4eb4c5dSMatthew G. Knepley   PetscInt    markerLeft = 1, faceMarkerLeft = 1;
11453dfda0b1SToby Isaac   PetscInt    dim;
1146d8211ee3SMatthew G. Knepley   PetscBool   markerSeparate = PETSC_FALSE, cutMarker = PETSC_FALSE;
11473dfda0b1SToby Isaac   PetscMPIInt rank;
11483dfda0b1SToby Isaac 
11493dfda0b1SToby Isaac   PetscFunctionBegin;
11509566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
11519566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
11529566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
11539566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
11549566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL));
11559371c9d4SSatish 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) {
11569371c9d4SSatish Balay     if (cutMarker) {
11579371c9d4SSatish Balay       PetscCall(DMCreateLabel(dm, "periodic_cut"));
11589371c9d4SSatish Balay       PetscCall(DMGetLabel(dm, "periodic_cut", &cutLabel));
11599371c9d4SSatish Balay     }
1160d8211ee3SMatthew G. Knepley   }
11613dfda0b1SToby Isaac   switch (dim) {
11623dfda0b1SToby Isaac   case 2:
1163f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 3;
1164f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
1165f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 2;
1166f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 4;
11673dfda0b1SToby Isaac     break;
11683dfda0b1SToby Isaac   case 3:
1169f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
1170f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 2;
1171f4eb4c5dSMatthew G. Knepley     faceMarkerFront  = 3;
1172f4eb4c5dSMatthew G. Knepley     faceMarkerBack   = 4;
1173f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 5;
1174f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 6;
11753dfda0b1SToby Isaac     break;
1176d71ae5a4SJacob Faibussowitsch   default:
1177d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Dimension %" PetscInt_FMT " not supported", dim);
11783dfda0b1SToby Isaac   }
11799566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
1180f4eb4c5dSMatthew G. Knepley   if (markerSeparate) {
1181f4eb4c5dSMatthew G. Knepley     markerBottom = faceMarkerBottom;
1182f4eb4c5dSMatthew G. Knepley     markerTop    = faceMarkerTop;
1183f4eb4c5dSMatthew G. Knepley     markerFront  = faceMarkerFront;
1184f4eb4c5dSMatthew G. Knepley     markerBack   = faceMarkerBack;
1185f4eb4c5dSMatthew G. Knepley     markerRight  = faceMarkerRight;
1186f4eb4c5dSMatthew G. Knepley     markerLeft   = faceMarkerLeft;
11873dfda0b1SToby Isaac   }
11883dfda0b1SToby Isaac   {
1189dd400576SPatrick Sanan     const PetscInt numXEdges    = rank == 0 ? edges[0] : 0;
1190dd400576SPatrick Sanan     const PetscInt numYEdges    = rank == 0 ? edges[1] : 0;
1191dd400576SPatrick Sanan     const PetscInt numZEdges    = rank == 0 ? edges[2] : 0;
1192dd400576SPatrick Sanan     const PetscInt numXVertices = rank == 0 ? (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ? edges[0] : edges[0] + 1) : 0;
1193dd400576SPatrick Sanan     const PetscInt numYVertices = rank == 0 ? (bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ? edges[1] : edges[1] + 1) : 0;
1194dd400576SPatrick Sanan     const PetscInt numZVertices = rank == 0 ? (bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST ? edges[2] : edges[2] + 1) : 0;
11953dfda0b1SToby Isaac     const PetscInt numCells     = numXEdges * numYEdges * numZEdges;
11963dfda0b1SToby Isaac     const PetscInt numXFaces    = numYEdges * numZEdges;
11973dfda0b1SToby Isaac     const PetscInt numYFaces    = numXEdges * numZEdges;
11983dfda0b1SToby Isaac     const PetscInt numZFaces    = numXEdges * numYEdges;
11993dfda0b1SToby Isaac     const PetscInt numTotXFaces = numXVertices * numXFaces;
12003dfda0b1SToby Isaac     const PetscInt numTotYFaces = numYVertices * numYFaces;
12013dfda0b1SToby Isaac     const PetscInt numTotZFaces = numZVertices * numZFaces;
12023dfda0b1SToby Isaac     const PetscInt numFaces     = numTotXFaces + numTotYFaces + numTotZFaces;
12033dfda0b1SToby Isaac     const PetscInt numTotXEdges = numXEdges * numYVertices * numZVertices;
12043dfda0b1SToby Isaac     const PetscInt numTotYEdges = numYEdges * numXVertices * numZVertices;
12053dfda0b1SToby Isaac     const PetscInt numTotZEdges = numZEdges * numXVertices * numYVertices;
12063dfda0b1SToby Isaac     const PetscInt numVertices  = numXVertices * numYVertices * numZVertices;
12073dfda0b1SToby Isaac     const PetscInt numEdges     = numTotXEdges + numTotYEdges + numTotZEdges;
12083dfda0b1SToby Isaac     const PetscInt firstVertex  = (dim == 2) ? numFaces : numCells;
12093dfda0b1SToby Isaac     const PetscInt firstXFace   = (dim == 2) ? 0 : numCells + numVertices;
12103dfda0b1SToby Isaac     const PetscInt firstYFace   = firstXFace + numTotXFaces;
12113dfda0b1SToby Isaac     const PetscInt firstZFace   = firstYFace + numTotYFaces;
12123dfda0b1SToby Isaac     const PetscInt firstXEdge   = numCells + numFaces + numVertices;
12133dfda0b1SToby Isaac     const PetscInt firstYEdge   = firstXEdge + numTotXEdges;
12143dfda0b1SToby Isaac     const PetscInt firstZEdge   = firstYEdge + numTotYEdges;
12153dfda0b1SToby Isaac     Vec            coordinates;
12163dfda0b1SToby Isaac     PetscSection   coordSection;
12173dfda0b1SToby Isaac     PetscScalar   *coords;
12183dfda0b1SToby Isaac     PetscInt       coordSize;
12193dfda0b1SToby Isaac     PetscInt       v, vx, vy, vz;
12203dfda0b1SToby Isaac     PetscInt       c, f, fx, fy, fz, e, ex, ey, ez;
12213dfda0b1SToby Isaac 
12229566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells + numFaces + numEdges + numVertices));
122348a46eb9SPierre Jolivet     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 6));
122448a46eb9SPierre Jolivet     for (f = firstXFace; f < firstXFace + numFaces; ++f) PetscCall(DMPlexSetConeSize(dm, f, 4));
122548a46eb9SPierre Jolivet     for (e = firstXEdge; e < firstXEdge + numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2));
12269566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
12273dfda0b1SToby Isaac     /* Build cells */
12283dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
12293dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
12303dfda0b1SToby Isaac         for (fx = 0; fx < numXEdges; ++fx) {
12313dfda0b1SToby Isaac           PetscInt cell  = (fz * numYEdges + fy) * numXEdges + fx;
12323dfda0b1SToby Isaac           PetscInt faceB = firstZFace + (fy * numXEdges + fx) * numZVertices + fz;
12333dfda0b1SToby Isaac           PetscInt faceT = firstZFace + (fy * numXEdges + fx) * numZVertices + ((fz + 1) % numZVertices);
12343dfda0b1SToby Isaac           PetscInt faceF = firstYFace + (fz * numXEdges + fx) * numYVertices + fy;
12353dfda0b1SToby Isaac           PetscInt faceK = firstYFace + (fz * numXEdges + fx) * numYVertices + ((fy + 1) % numYVertices);
12363dfda0b1SToby Isaac           PetscInt faceL = firstXFace + (fz * numYEdges + fy) * numXVertices + fx;
12373dfda0b1SToby Isaac           PetscInt faceR = firstXFace + (fz * numYEdges + fy) * numXVertices + ((fx + 1) % numXVertices);
12383dfda0b1SToby Isaac           /* B,  T,  F,  K,  R,  L */
1239b5a892a1SMatthew G. Knepley           PetscInt ornt[6] = {-2, 0, 0, -3, 0, -2}; /* ??? */
124042206facSLisandro Dalcin           PetscInt cone[6];
12413dfda0b1SToby Isaac 
12423dfda0b1SToby Isaac           /* no boundary twisting in 3D */
12439371c9d4SSatish Balay           cone[0] = faceB;
12449371c9d4SSatish Balay           cone[1] = faceT;
12459371c9d4SSatish Balay           cone[2] = faceF;
12469371c9d4SSatish Balay           cone[3] = faceK;
12479371c9d4SSatish Balay           cone[4] = faceR;
12489371c9d4SSatish Balay           cone[5] = faceL;
12499566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, cell, cone));
12509566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, cell, ornt));
12519566063dSJacob Faibussowitsch           if (bdX != DM_BOUNDARY_NONE && fx == numXEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
12529566063dSJacob Faibussowitsch           if (bdY != DM_BOUNDARY_NONE && fy == numYEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
12539566063dSJacob Faibussowitsch           if (bdZ != DM_BOUNDARY_NONE && fz == numZEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
12543dfda0b1SToby Isaac         }
12553dfda0b1SToby Isaac       }
12563dfda0b1SToby Isaac     }
12573dfda0b1SToby Isaac     /* Build x faces */
12583dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
12593dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
12603dfda0b1SToby Isaac         for (fx = 0; fx < numXVertices; ++fx) {
12613dfda0b1SToby Isaac           PetscInt face    = firstXFace + (fz * numYEdges + fy) * numXVertices + fx;
12623dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy * numXVertices + fx) * numZEdges + fz;
12633dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (((fy + 1) % numYVertices) * numXVertices + fx) * numZEdges + fz;
12643dfda0b1SToby Isaac           PetscInt edgeB   = firstYEdge + (fz * numXVertices + fx) * numYEdges + fy;
12653dfda0b1SToby Isaac           PetscInt edgeT   = firstYEdge + (((fz + 1) % numZVertices) * numXVertices + fx) * numYEdges + fy;
1266b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
12673dfda0b1SToby Isaac           PetscInt cone[4];
12683dfda0b1SToby Isaac 
12693dfda0b1SToby Isaac           if (dim == 3) {
12703dfda0b1SToby Isaac             /* markers */
12713dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
12723dfda0b1SToby Isaac               if (fx == numXVertices - 1) {
12739566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerRight));
12749566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerRight));
12759371c9d4SSatish Balay               } else if (fx == 0) {
12769566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerLeft));
12779566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerLeft));
12783dfda0b1SToby Isaac               }
12793dfda0b1SToby Isaac             }
12803dfda0b1SToby Isaac           }
12819371c9d4SSatish Balay           cone[0] = edgeB;
12829371c9d4SSatish Balay           cone[1] = edgeR;
12839371c9d4SSatish Balay           cone[2] = edgeT;
12849371c9d4SSatish Balay           cone[3] = edgeL;
12859566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
12869566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
12873dfda0b1SToby Isaac         }
12883dfda0b1SToby Isaac       }
12893dfda0b1SToby Isaac     }
12903dfda0b1SToby Isaac     /* Build y faces */
12913dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
129242206facSLisandro Dalcin       for (fx = 0; fx < numXEdges; ++fx) {
12933dfda0b1SToby Isaac         for (fy = 0; fy < numYVertices; ++fy) {
12943dfda0b1SToby Isaac           PetscInt face    = firstYFace + (fz * numXEdges + fx) * numYVertices + fy;
12953dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy * numXVertices + fx) * numZEdges + fz;
12963dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (fy * numXVertices + ((fx + 1) % numXVertices)) * numZEdges + fz;
12973dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz * numYVertices + fy) * numXEdges + fx;
12983dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (((fz + 1) % numZVertices) * numYVertices + fy) * numXEdges + fx;
1299b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
13003dfda0b1SToby Isaac           PetscInt cone[4];
13013dfda0b1SToby Isaac 
13023dfda0b1SToby Isaac           if (dim == 3) {
13033dfda0b1SToby Isaac             /* markers */
13043dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
13053dfda0b1SToby Isaac               if (fy == numYVertices - 1) {
13069566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerBack));
13079566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerBack));
13089371c9d4SSatish Balay               } else if (fy == 0) {
13099566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerFront));
13109566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerFront));
13113dfda0b1SToby Isaac               }
13123dfda0b1SToby Isaac             }
13133dfda0b1SToby Isaac           }
13149371c9d4SSatish Balay           cone[0] = edgeB;
13159371c9d4SSatish Balay           cone[1] = edgeR;
13169371c9d4SSatish Balay           cone[2] = edgeT;
13179371c9d4SSatish Balay           cone[3] = edgeL;
13189566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
13199566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
13203dfda0b1SToby Isaac         }
13213dfda0b1SToby Isaac       }
13223dfda0b1SToby Isaac     }
13233dfda0b1SToby Isaac     /* Build z faces */
13243dfda0b1SToby Isaac     for (fy = 0; fy < numYEdges; ++fy) {
13253dfda0b1SToby Isaac       for (fx = 0; fx < numXEdges; ++fx) {
13263dfda0b1SToby Isaac         for (fz = 0; fz < numZVertices; fz++) {
13273dfda0b1SToby Isaac           PetscInt face    = firstZFace + (fy * numXEdges + fx) * numZVertices + fz;
13283dfda0b1SToby Isaac           PetscInt edgeL   = firstYEdge + (fz * numXVertices + fx) * numYEdges + fy;
13293dfda0b1SToby Isaac           PetscInt edgeR   = firstYEdge + (fz * numXVertices + ((fx + 1) % numXVertices)) * numYEdges + fy;
13303dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz * numYVertices + fy) * numXEdges + fx;
13313dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (fz * numYVertices + ((fy + 1) % numYVertices)) * numXEdges + fx;
1332b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
13333dfda0b1SToby Isaac           PetscInt cone[4];
13343dfda0b1SToby Isaac 
13353dfda0b1SToby Isaac           if (dim == 2) {
13369371c9d4SSatish Balay             if (bdX == DM_BOUNDARY_TWIST && fx == numXEdges - 1) {
13379371c9d4SSatish Balay               edgeR += numYEdges - 1 - 2 * fy;
13389371c9d4SSatish Balay               ornt[1] = -1;
13399371c9d4SSatish Balay             }
13409371c9d4SSatish Balay             if (bdY == DM_BOUNDARY_TWIST && fy == numYEdges - 1) {
13419371c9d4SSatish Balay               edgeT += numXEdges - 1 - 2 * fx;
13429371c9d4SSatish Balay               ornt[2] = 0;
13439371c9d4SSatish Balay             }
13449566063dSJacob Faibussowitsch             if (bdX != DM_BOUNDARY_NONE && fx == numXEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, face, 2));
13459566063dSJacob Faibussowitsch             if (bdY != DM_BOUNDARY_NONE && fy == numYEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, face, 2));
1346d1c88043SMatthew G. Knepley           } else {
13473dfda0b1SToby Isaac             /* markers */
13483dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
13493dfda0b1SToby Isaac               if (fz == numZVertices - 1) {
13509566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerTop));
13519566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerTop));
13529371c9d4SSatish Balay               } else if (fz == 0) {
13539566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerBottom));
13549566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerBottom));
13553dfda0b1SToby Isaac               }
13563dfda0b1SToby Isaac             }
13573dfda0b1SToby Isaac           }
13589371c9d4SSatish Balay           cone[0] = edgeB;
13599371c9d4SSatish Balay           cone[1] = edgeR;
13609371c9d4SSatish Balay           cone[2] = edgeT;
13619371c9d4SSatish Balay           cone[3] = edgeL;
13629566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
13639566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
13643dfda0b1SToby Isaac         }
13653dfda0b1SToby Isaac       }
13663dfda0b1SToby Isaac     }
13673dfda0b1SToby Isaac     /* Build Z edges*/
13683dfda0b1SToby Isaac     for (vy = 0; vy < numYVertices; vy++) {
13693dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
13703dfda0b1SToby Isaac         for (ez = 0; ez < numZEdges; ez++) {
13713dfda0b1SToby Isaac           const PetscInt edge    = firstZEdge + (vy * numXVertices + vx) * numZEdges + ez;
13723dfda0b1SToby Isaac           const PetscInt vertexB = firstVertex + (ez * numYVertices + vy) * numXVertices + vx;
13733dfda0b1SToby Isaac           const PetscInt vertexT = firstVertex + (((ez + 1) % numZVertices) * numYVertices + vy) * numXVertices + vx;
13743dfda0b1SToby Isaac           PetscInt       cone[2];
13753dfda0b1SToby Isaac 
13769371c9d4SSatish Balay           cone[0] = vertexB;
13779371c9d4SSatish Balay           cone[1] = vertexT;
1378c2df9bbfSMatthew G. Knepley           PetscCall(DMPlexSetCone(dm, edge, cone));
13793dfda0b1SToby Isaac           if (dim == 3) {
13803dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
13813dfda0b1SToby Isaac               if (vx == numXVertices - 1) {
13829566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
1383c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1384c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1385c2df9bbfSMatthew G. Knepley               } else if (vx == 0) {
13869566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
1387c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1388c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
13893dfda0b1SToby Isaac               }
13903dfda0b1SToby Isaac             }
13913dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
13923dfda0b1SToby Isaac               if (vy == numYVertices - 1) {
13939566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBack));
1394c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBack));
1395c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBack));
1396c2df9bbfSMatthew G. Knepley               } else if (vy == 0) {
13979566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerFront));
1398c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerFront));
1399c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerFront));
14003dfda0b1SToby Isaac               }
14013dfda0b1SToby Isaac             }
14023dfda0b1SToby Isaac           }
14033dfda0b1SToby Isaac         }
14043dfda0b1SToby Isaac       }
14053dfda0b1SToby Isaac     }
14063dfda0b1SToby Isaac     /* Build Y edges*/
14073dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
14083dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
14093dfda0b1SToby Isaac         for (ey = 0; ey < numYEdges; ey++) {
14103dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdY == DM_BOUNDARY_TWIST && ey == numYEdges - 1) ? (numXVertices - vx - 1) : (vz * numYVertices + ((ey + 1) % numYVertices)) * numXVertices + vx;
14113dfda0b1SToby Isaac           const PetscInt edge    = firstYEdge + (vz * numXVertices + vx) * numYEdges + ey;
14123dfda0b1SToby Isaac           const PetscInt vertexF = firstVertex + (vz * numYVertices + ey) * numXVertices + vx;
14133dfda0b1SToby Isaac           const PetscInt vertexK = firstVertex + nextv;
14143dfda0b1SToby Isaac           PetscInt       cone[2];
14153dfda0b1SToby Isaac 
14169371c9d4SSatish Balay           cone[0] = vertexF;
14179371c9d4SSatish Balay           cone[1] = vertexK;
14189566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, edge, cone));
14193dfda0b1SToby Isaac           if (dim == 2) {
14203dfda0b1SToby Isaac             if ((bdX != DM_BOUNDARY_PERIODIC) && (bdX != DM_BOUNDARY_TWIST)) {
14213dfda0b1SToby Isaac               if (vx == numXVertices - 1) {
14229566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerRight));
14239566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
14249566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1425c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1426d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
14279566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerLeft));
14289566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
14299566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1430c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
14313dfda0b1SToby Isaac               }
1432d8211ee3SMatthew G. Knepley             } else {
14334c67ea77SStefano Zampini               if (vx == 0 && cutLabel) {
14349566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, edge, 1));
14359566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, cone[0], 1));
1436c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMLabelSetValue(cutLabel, cone[1], 1));
14373dfda0b1SToby Isaac               }
1438d8211ee3SMatthew G. Knepley             }
1439d8211ee3SMatthew G. Knepley           } else {
14403dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
14413dfda0b1SToby Isaac               if (vx == numXVertices - 1) {
14429566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
1443c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1444c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1445d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
14469566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
1447c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1448c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
14493dfda0b1SToby Isaac               }
14503dfda0b1SToby Isaac             }
14513dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
14523dfda0b1SToby Isaac               if (vz == numZVertices - 1) {
14539566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
1454c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1455c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1456d8211ee3SMatthew G. Knepley               } else if (vz == 0) {
14579566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
1458c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1459c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
14603dfda0b1SToby Isaac               }
14613dfda0b1SToby Isaac             }
14623dfda0b1SToby Isaac           }
14633dfda0b1SToby Isaac         }
14643dfda0b1SToby Isaac       }
14653dfda0b1SToby Isaac     }
14663dfda0b1SToby Isaac     /* Build X edges*/
14673dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
14683dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; vy++) {
14693dfda0b1SToby Isaac         for (ex = 0; ex < numXEdges; ex++) {
14703dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdX == DM_BOUNDARY_TWIST && ex == numXEdges - 1) ? (numYVertices - vy - 1) * numXVertices : (vz * numYVertices + vy) * numXVertices + (ex + 1) % numXVertices;
14713dfda0b1SToby Isaac           const PetscInt edge    = firstXEdge + (vz * numYVertices + vy) * numXEdges + ex;
14723dfda0b1SToby Isaac           const PetscInt vertexL = firstVertex + (vz * numYVertices + vy) * numXVertices + ex;
14733dfda0b1SToby Isaac           const PetscInt vertexR = firstVertex + nextv;
14743dfda0b1SToby Isaac           PetscInt       cone[2];
14753dfda0b1SToby Isaac 
14769371c9d4SSatish Balay           cone[0] = vertexL;
14779371c9d4SSatish Balay           cone[1] = vertexR;
14789566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, edge, cone));
14793dfda0b1SToby Isaac           if (dim == 2) {
14803dfda0b1SToby Isaac             if ((bdY != DM_BOUNDARY_PERIODIC) && (bdY != DM_BOUNDARY_TWIST)) {
14813dfda0b1SToby Isaac               if (vy == numYVertices - 1) {
14829566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerTop));
14839566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
14849566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1485c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1486d8211ee3SMatthew G. Knepley               } else if (vy == 0) {
14879566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerBottom));
14889566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
14899566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1490c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
14913dfda0b1SToby Isaac               }
1492d8211ee3SMatthew G. Knepley             } else {
14934c67ea77SStefano Zampini               if (vy == 0 && cutLabel) {
14949566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, edge, 1));
14959566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, cone[0], 1));
1496c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMLabelSetValue(cutLabel, cone[1], 1));
14973dfda0b1SToby Isaac               }
1498d8211ee3SMatthew G. Knepley             }
1499d8211ee3SMatthew G. Knepley           } else {
15003dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
15013dfda0b1SToby Isaac               if (vy == numYVertices - 1) {
15029566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBack));
1503c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBack));
1504c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBack));
1505c2df9bbfSMatthew G. Knepley               } else if (vy == 0) {
15069566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerFront));
1507c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerFront));
1508c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerFront));
15093dfda0b1SToby Isaac               }
15103dfda0b1SToby Isaac             }
15113dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
15123dfda0b1SToby Isaac               if (vz == numZVertices - 1) {
15139566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
1514c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1515c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1516c2df9bbfSMatthew G. Knepley               } else if (vz == 0) {
15179566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
1518c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1519c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
15203dfda0b1SToby Isaac               }
15213dfda0b1SToby Isaac             }
15223dfda0b1SToby Isaac           }
15233dfda0b1SToby Isaac         }
15243dfda0b1SToby Isaac       }
15253dfda0b1SToby Isaac     }
15269566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
15279566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
15283dfda0b1SToby Isaac     /* Build coordinates */
15299566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
15309566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
15319566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
15329566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numVertices));
15333dfda0b1SToby Isaac     for (v = firstVertex; v < firstVertex + numVertices; ++v) {
15349566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
15359566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
15363dfda0b1SToby Isaac     }
15379566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
15389566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
15399566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
15409566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
15419566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
15429566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
15439566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates, VECSTANDARD));
15449566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
15453dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; ++vz) {
15463dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; ++vy) {
15473dfda0b1SToby Isaac         for (vx = 0; vx < numXVertices; ++vx) {
15483dfda0b1SToby Isaac           coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 0] = lower[0] + ((upper[0] - lower[0]) / numXEdges) * vx;
15493dfda0b1SToby Isaac           coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 1] = lower[1] + ((upper[1] - lower[1]) / numYEdges) * vy;
1550ad540459SPierre Jolivet           if (dim == 3) coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 2] = lower[2] + ((upper[2] - lower[2]) / numZEdges) * vz;
15513dfda0b1SToby Isaac         }
15523dfda0b1SToby Isaac       }
15533dfda0b1SToby Isaac     }
15549566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
15559566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
15569566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
15573dfda0b1SToby Isaac   }
15583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
15593dfda0b1SToby Isaac }
15603dfda0b1SToby Isaac 
1561d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxMesh_Tensor_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
1562d71ae5a4SJacob Faibussowitsch {
15639318fe57SMatthew G. Knepley   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
15649318fe57SMatthew G. Knepley   PetscInt       fac[3] = {0, 0, 0}, d;
1565552f7358SJed Brown 
1566552f7358SJed Brown   PetscFunctionBegin;
15674f572ea9SToby Isaac   PetscAssertPointer(dm, 1);
15689318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
15699566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
15709371c9d4SSatish Balay   for (d = 0; d < dim; ++d) {
15719371c9d4SSatish Balay     fac[d] = faces[d];
15729371c9d4SSatish Balay     bdt[d] = periodicity[d];
15739371c9d4SSatish Balay   }
15749566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateCubeMesh_Internal(dm, lower, upper, fac, bdt[0], bdt[1], bdt[2]));
15759371c9d4SSatish 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))) {
15766858538eSMatthew G. Knepley     PetscReal L[3]       = {-1., -1., 0.};
15776858538eSMatthew G. Knepley     PetscReal maxCell[3] = {-1., -1., 0.};
1578552f7358SJed Brown 
15799318fe57SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
15806858538eSMatthew G. Knepley       if (periodicity[d] != DM_BOUNDARY_NONE) {
15819318fe57SMatthew G. Knepley         L[d]       = upper[d] - lower[d];
15829318fe57SMatthew G. Knepley         maxCell[d] = 1.1 * (L[d] / PetscMax(1, faces[d]));
1583768d5fceSMatthew G. Knepley       }
15846858538eSMatthew G. Knepley     }
15854fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, maxCell, lower, L));
1586768d5fceSMatthew G. Knepley   }
15879566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
15883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
15899318fe57SMatthew G. Knepley }
15909318fe57SMatthew G. Knepley 
15915dca41c3SJed 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)
1592d71ae5a4SJacob Faibussowitsch {
15939318fe57SMatthew G. Knepley   PetscFunctionBegin;
159446139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
15955dca41c3SJed Brown   if (shape == DM_SHAPE_ZBOX) PetscCall(DMPlexCreateBoxMesh_Tensor_SFC_Internal(dm, dim, faces, lower, upper, periodicity, interpolate));
15966725e60dSJed Brown   else if (dim == 1) PetscCall(DMPlexCreateLineMesh_Internal(dm, faces[0], lower[0], upper[0], periodicity[0]));
15979566063dSJacob Faibussowitsch   else if (simplex) PetscCall(DMPlexCreateBoxMesh_Simplex_Internal(dm, dim, faces, lower, upper, periodicity, interpolate));
15989566063dSJacob Faibussowitsch   else PetscCall(DMPlexCreateBoxMesh_Tensor_Internal(dm, dim, faces, lower, upper, periodicity));
15999318fe57SMatthew G. Knepley   if (!interpolate && dim > 1 && !simplex) {
1600768d5fceSMatthew G. Knepley     DM udm;
1601768d5fceSMatthew G. Knepley 
16029566063dSJacob Faibussowitsch     PetscCall(DMPlexUninterpolate(dm, &udm));
16039566063dSJacob Faibussowitsch     PetscCall(DMPlexCopyCoordinates(dm, udm));
160469d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &udm));
1605768d5fceSMatthew G. Knepley   }
160646139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
16073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1608c8c68bd8SToby Isaac }
1609c8c68bd8SToby Isaac 
16105d83a8b1SBarry Smith /*@
1611768d5fceSMatthew G. Knepley   DMPlexCreateBoxMesh - Creates a mesh on the tensor product of unit intervals (box) using simplices or tensor cells (hexahedra).
1612768d5fceSMatthew G. Knepley 
1613d083f849SBarry Smith   Collective
1614768d5fceSMatthew G. Knepley 
1615768d5fceSMatthew G. Knepley   Input Parameters:
1616a1cb98faSBarry Smith + comm               - The communicator for the `DM` object
1617768d5fceSMatthew G. Knepley . dim                - The spatial dimension
1618a1cb98faSBarry Smith . simplex            - `PETSC_TRUE` for simplices, `PETSC_FALSE` for tensor cells
161920f4b53cSBarry Smith . faces              - Number of faces per dimension, or `NULL` for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
162020f4b53cSBarry Smith . lower              - The lower left corner, or `NULL` for (0, 0, 0)
162120f4b53cSBarry Smith . upper              - The upper right corner, or `NULL` for (1, 1, 1)
162220f4b53cSBarry Smith . periodicity        - The boundary type for the X,Y,Z direction, or `NULL` for `DM_BOUNDARY_NONE`
162342108689Sksagiyam . interpolate        - Flag to create intermediate mesh pieces (edges, faces)
162442108689Sksagiyam . localizationHeight - Flag to localize edges and faces in addition to cells; only significant for periodic meshes
162542108689Sksagiyam - sparseLocalize     - Flag to localize coordinates only for cells near the periodic boundary; only significant for periodic meshes
1626768d5fceSMatthew G. Knepley 
1627768d5fceSMatthew G. Knepley   Output Parameter:
1628a1cb98faSBarry Smith . dm - The `DM` object
1629768d5fceSMatthew G. Knepley 
1630768d5fceSMatthew G. Knepley   Level: beginner
1631768d5fceSMatthew G. Knepley 
1632a1cb98faSBarry Smith   Note:
1633a1cb98faSBarry Smith   To customize this mesh using options, use
1634a1cb98faSBarry Smith .vb
1635a1cb98faSBarry Smith   DMCreate(comm, &dm);
1636a1cb98faSBarry Smith   DMSetType(dm, DMPLEX);
1637a1cb98faSBarry Smith   DMSetFromOptions(dm);
1638a1cb98faSBarry Smith .ve
1639a1cb98faSBarry Smith   and use the options in `DMSetFromOptions()`.
1640a1cb98faSBarry Smith 
1641a4e35b19SJacob Faibussowitsch   Here is the numbering returned for 2 faces in each direction for tensor cells\:
1642a1cb98faSBarry Smith .vb
1643a1cb98faSBarry Smith  10---17---11---18----12
1644a1cb98faSBarry Smith   |         |         |
1645a1cb98faSBarry Smith   |         |         |
1646a1cb98faSBarry Smith  20    2   22    3    24
1647a1cb98faSBarry Smith   |         |         |
1648a1cb98faSBarry Smith   |         |         |
1649a1cb98faSBarry Smith   7---15----8---16----9
1650a1cb98faSBarry Smith   |         |         |
1651a1cb98faSBarry Smith   |         |         |
1652a1cb98faSBarry Smith  19    0   21    1   23
1653a1cb98faSBarry Smith   |         |         |
1654a1cb98faSBarry Smith   |         |         |
1655a1cb98faSBarry Smith   4---13----5---14----6
1656a1cb98faSBarry Smith .ve
1657a1cb98faSBarry Smith   and for simplicial cells
1658a1cb98faSBarry Smith .vb
1659a1cb98faSBarry Smith  14----8---15----9----16
1660a1cb98faSBarry Smith   |\     5  |\      7 |
1661a1cb98faSBarry Smith   | \       | \       |
1662a1cb98faSBarry Smith  13   2    14    3    15
1663a1cb98faSBarry Smith   | 4   \   | 6   \   |
1664a1cb98faSBarry Smith   |       \ |       \ |
1665a1cb98faSBarry Smith  11----6---12----7----13
1666a1cb98faSBarry Smith   |\        |\        |
1667a1cb98faSBarry Smith   | \    1  | \     3 |
1668a1cb98faSBarry Smith  10   0    11    1    12
1669a1cb98faSBarry Smith   | 0   \   | 2   \   |
1670a1cb98faSBarry Smith   |       \ |       \ |
1671a1cb98faSBarry Smith   8----4----9----5----10
1672a1cb98faSBarry Smith .ve
1673a1cb98faSBarry Smith 
16741cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMSetFromOptions()`, `DMPlexCreateFromFile()`, `DMPlexCreateHexCylinderMesh()`, `DMSetType()`, `DMCreate()`
1675768d5fceSMatthew G. Knepley @*/
167642108689Sksagiyam 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)
1677d71ae5a4SJacob Faibussowitsch {
16789318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
1679fdbf62faSLisandro Dalcin   PetscReal      low[3] = {0, 0, 0};
1680fdbf62faSLisandro Dalcin   PetscReal      upp[3] = {1, 1, 1};
1681fdbf62faSLisandro Dalcin   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
1682552f7358SJed Brown 
1683768d5fceSMatthew G. Knepley   PetscFunctionBegin;
16849566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
16859566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
16865dca41c3SJed Brown   PetscCall(DMPlexCreateBoxMesh_Internal(*dm, DM_SHAPE_BOX, dim, simplex, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt, interpolate));
168742108689Sksagiyam   if (periodicity) {
168842108689Sksagiyam     DM cdm;
168942108689Sksagiyam 
169042108689Sksagiyam     PetscCall(DMGetCoordinateDM(*dm, &cdm));
169142108689Sksagiyam     PetscCall(DMPlexSetMaxProjectionHeight(cdm, localizationHeight));
169242108689Sksagiyam     PetscCall(DMSetSparseLocalize(*dm, sparseLocalize));
169342108689Sksagiyam     PetscCall(DMLocalizeCoordinates(*dm));
169442108689Sksagiyam   }
16953ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
16969318fe57SMatthew G. Knepley }
1697fdbf62faSLisandro Dalcin 
1698d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateWedgeBoxMesh_Internal(DM dm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
1699d71ae5a4SJacob Faibussowitsch {
17009318fe57SMatthew G. Knepley   DM       bdm, vol;
17019318fe57SMatthew G. Knepley   PetscInt i;
17029318fe57SMatthew G. Knepley 
17039318fe57SMatthew G. Knepley   PetscFunctionBegin;
17041fcf445aSMatthew G. Knepley   // TODO Now we can support periodicity
170508401ef6SPierre Jolivet   for (i = 0; i < 3; ++i) PetscCheck(periodicity[i] == DM_BOUNDARY_NONE, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Periodicity not yet supported");
17069566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &bdm));
17079566063dSJacob Faibussowitsch   PetscCall(DMSetType(bdm, DMPLEX));
17089566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(bdm, 2));
170946139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, bdm, 0, 0, 0));
17109566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxMesh_Simplex_Internal(bdm, 2, faces, lower, upper, periodicity, PETSC_TRUE));
17111fcf445aSMatthew G. Knepley   PetscCall(DMPlexExtrude(bdm, faces[2], upper[2] - lower[2], PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, NULL, NULL, &vol));
171246139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, bdm, 0, 0, 0));
17139566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&bdm));
171469d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &vol));
17159318fe57SMatthew G. Knepley   if (lower[2] != 0.0) {
17169318fe57SMatthew G. Knepley     Vec          v;
17179318fe57SMatthew G. Knepley     PetscScalar *x;
17189318fe57SMatthew G. Knepley     PetscInt     cDim, n;
17199318fe57SMatthew G. Knepley 
17209566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &v));
17219566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(v, &cDim));
17229566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(v, &n));
17239566063dSJacob Faibussowitsch     PetscCall(VecGetArray(v, &x));
17249318fe57SMatthew G. Knepley     x += cDim;
17259318fe57SMatthew G. Knepley     for (i = 0; i < n; i += cDim) x[i] += lower[2];
17269566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(v, &x));
17279566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, v));
17289318fe57SMatthew G. Knepley   }
17293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1730552f7358SJed Brown }
1731552f7358SJed Brown 
173200dabe28SStefano Zampini /*@
173339f4f5dbSPierre Jolivet   DMPlexCreateWedgeBoxMesh - Creates a 3-D mesh tessellating the (x,y) plane and extruding in the third direction using wedge cells.
173400dabe28SStefano Zampini 
1735d083f849SBarry Smith   Collective
173600dabe28SStefano Zampini 
173700dabe28SStefano Zampini   Input Parameters:
1738a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
173920f4b53cSBarry Smith . faces       - Number of faces per dimension, or `NULL` for (1, 1, 1)
174020f4b53cSBarry Smith . lower       - The lower left corner, or `NULL` for (0, 0, 0)
174120f4b53cSBarry Smith . upper       - The upper right corner, or `NULL` for (1, 1, 1)
174220f4b53cSBarry Smith . periodicity - The boundary type for the X,Y,Z direction, or `NULL` for `DM_BOUNDARY_NONE`
1743a1cb98faSBarry Smith . orderHeight - If `PETSC_TRUE`, orders the extruded cells in the height first. Otherwise, orders the cell on the layers first
174400dabe28SStefano Zampini - interpolate - Flag to create intermediate mesh pieces (edges, faces)
174500dabe28SStefano Zampini 
174600dabe28SStefano Zampini   Output Parameter:
1747a1cb98faSBarry Smith . dm - The `DM` object
174800dabe28SStefano Zampini 
174900dabe28SStefano Zampini   Level: beginner
175000dabe28SStefano Zampini 
17511cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateHexCylinderMesh()`, `DMPlexCreateWedgeCylinderMesh()`, `DMExtrude()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
175200dabe28SStefano Zampini @*/
1753d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateWedgeBoxMesh(MPI_Comm comm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool orderHeight, PetscBool interpolate, DM *dm)
1754d71ae5a4SJacob Faibussowitsch {
17559318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
175600dabe28SStefano Zampini   PetscReal      low[3] = {0, 0, 0};
175700dabe28SStefano Zampini   PetscReal      upp[3] = {1, 1, 1};
175800dabe28SStefano Zampini   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
175900dabe28SStefano Zampini 
176000dabe28SStefano Zampini   PetscFunctionBegin;
17619566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
17629566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
17639566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateWedgeBoxMesh_Internal(*dm, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt));
1764d410b0cfSMatthew G. Knepley   if (!interpolate) {
1765d410b0cfSMatthew G. Knepley     DM udm;
176600dabe28SStefano Zampini 
17679566063dSJacob Faibussowitsch     PetscCall(DMPlexUninterpolate(*dm, &udm));
176869d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(*dm, &udm));
176900dabe28SStefano Zampini   }
17707ff04441SMatthew G. Knepley   if (periodicity) PetscCall(DMLocalizeCoordinates(*dm));
17713ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
177200dabe28SStefano Zampini }
177300dabe28SStefano Zampini 
1774cfb853baSMatthew G. Knepley /*
1775cfb853baSMatthew 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.
1776cfb853baSMatthew G. Knepley 
1777cfb853baSMatthew G. Knepley   Input Parameters:
1778cfb853baSMatthew G. Knepley + len - The length of the tuple
1779cfb853baSMatthew G. Knepley . max - The maximum for each dimension, so values are in [0, max)
1780cfb853baSMatthew G. Knepley - tup - A tuple of length len+1: tup[len] > 0 indicates a stopping condition
1781cfb853baSMatthew G. Knepley 
1782cfb853baSMatthew G. Knepley   Output Parameter:
178320f4b53cSBarry Smith . tup - A tuple of `len` integers whose entries are at most `max`
1784cfb853baSMatthew G. Knepley 
1785cfb853baSMatthew G. Knepley   Level: developer
1786cfb853baSMatthew G. Knepley 
178720f4b53cSBarry Smith   Note:
178820f4b53cSBarry Smith   Ordering is lexicographic with lowest index as least significant in ordering.
178920f4b53cSBarry 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}.
179020f4b53cSBarry Smith 
1791cfb853baSMatthew G. Knepley .seealso: PetscDualSpaceTensorPointLexicographic_Internal(), PetscDualSpaceLatticePointLexicographic_Internal()
1792cfb853baSMatthew G. Knepley */
1793cfb853baSMatthew G. Knepley static PetscErrorCode DMPlexTensorPointLexicographic_Private(PetscInt len, const PetscInt max[], PetscInt tup[])
1794cfb853baSMatthew G. Knepley {
1795cfb853baSMatthew G. Knepley   PetscInt i;
1796cfb853baSMatthew G. Knepley 
1797cfb853baSMatthew G. Knepley   PetscFunctionBegin;
1798cfb853baSMatthew G. Knepley   for (i = 0; i < len; ++i) {
1799cfb853baSMatthew G. Knepley     if (tup[i] < max[i] - 1) {
1800cfb853baSMatthew G. Knepley       break;
1801cfb853baSMatthew G. Knepley     } else {
1802cfb853baSMatthew G. Knepley       tup[i] = 0;
1803cfb853baSMatthew G. Knepley     }
1804cfb853baSMatthew G. Knepley   }
1805cfb853baSMatthew G. Knepley   if (i == len) tup[i - 1] = max[i - 1];
1806cfb853baSMatthew G. Knepley   else ++tup[i];
18073ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1808cfb853baSMatthew G. Knepley }
1809cfb853baSMatthew G. Knepley 
1810cfb853baSMatthew G. Knepley static PetscInt TupleToIndex_Private(PetscInt len, const PetscInt max[], const PetscInt tup[])
1811cfb853baSMatthew G. Knepley {
18128d2ec52aSSatish Balay   PetscInt idx = tup[len - 1];
1813cfb853baSMatthew G. Knepley 
18148d2ec52aSSatish Balay   for (PetscInt i = len - 2; i >= 0; --i) {
1815cfb853baSMatthew G. Knepley     idx *= max[i];
1816cfb853baSMatthew G. Knepley     idx += tup[i];
1817cfb853baSMatthew G. Knepley   }
1818cfb853baSMatthew G. Knepley   return idx;
1819cfb853baSMatthew G. Knepley }
1820cfb853baSMatthew G. Knepley 
18218d2ec52aSSatish Balay static void IndexToTuple_Private(PetscInt len, const PetscInt max[], PetscInt idx, PetscInt tup[])
1822cfb853baSMatthew G. Knepley {
18238d2ec52aSSatish Balay   for (PetscInt i = 0; i < len; ++i) {
18248d2ec52aSSatish Balay     tup[i] = idx % max[i];
18258d2ec52aSSatish Balay     idx    = (idx - tup[i]) / max[i];
18268d2ec52aSSatish Balay   }
18278d2ec52aSSatish Balay }
18288d2ec52aSSatish Balay 
18298d2ec52aSSatish Balay static void TupleToRanks_Private(PetscInt len, const PetscInt max[], const PetscInt procs[], const PetscInt tup[], PetscInt ranks[])
18308d2ec52aSSatish Balay {
18318d2ec52aSSatish Balay   for (PetscInt i = 0; i < len; ++i) {
18328d2ec52aSSatish Balay     const PetscInt div = max[i] / procs[i];
18338d2ec52aSSatish Balay     const PetscInt rem = max[i] % procs[i];
18348d2ec52aSSatish Balay     const PetscInt idx = (tup[i] < 0 ? max[i] + tup[i] : tup[i]) % max[i];
18358d2ec52aSSatish Balay 
18368d2ec52aSSatish Balay     if (idx < rem * (div + 1)) ranks[i] = idx / (div + 1);
18378d2ec52aSSatish Balay     else ranks[i] = rem + (idx - rem * (div + 1)) / div;
18388d2ec52aSSatish Balay   }
18398d2ec52aSSatish Balay }
18408d2ec52aSSatish Balay 
18418d2ec52aSSatish Balay static void RanksToSizes_Private(PetscInt len, const PetscInt max[], const PetscInt procs[], const PetscInt ranks[], PetscInt sizes[])
18428d2ec52aSSatish Balay {
18438d2ec52aSSatish Balay   for (PetscInt i = 0; i < len; ++i) {
18448d2ec52aSSatish Balay     const PetscInt div = max[i] / procs[i];
18458d2ec52aSSatish Balay     const PetscInt rem = max[i] % procs[i];
18468d2ec52aSSatish Balay 
18478d2ec52aSSatish Balay     sizes[i] = ranks[i] < rem ? div + 1 : div;
18488d2ec52aSSatish Balay   }
18498d2ec52aSSatish Balay }
18508d2ec52aSSatish Balay 
18518d2ec52aSSatish Balay /*
18528d2ec52aSSatish Balay   In serial, the mesh is completely periodic. In parallel, we will include a layer of ghost vertices around the patch, so our edges will not wrap around in parallel. This will instead be handled by the SF.
18538d2ec52aSSatish Balay 
18548d2ec52aSSatish Balay   The cone for all edges will always be complete. Even in the presence of boundaries, we will keep all support sizes constant. When an edge would not exist in the support, we will create one to wrap back periodically. This allows for uniform stencils, and that edge will not be used for computation.
18558d2ec52aSSatish Balay 
18568d2ec52aSSatish Balay   All point which do not attain the vertex lower or upper bound in any dimension are owned, the rest are leaves owned by another process and present in the SF.
1857dfe9cfe5SMatthew Knepley 
1858dfe9cfe5SMatthew Knepley   Parallel Layout:
1859dfe9cfe5SMatthew Knepley 
1860dfe9cfe5SMatthew Knepley   We create a cubic process grid of dimension P^{1/d} on each side. The IndexToTuple_Private() function maps the global rank to the local rank in each dimension. We divide edges using the PETSc distribution rule in each dimension, and then add overlap. TupleToRanks_Private() returns the local rank in ech dimension for a tuple. RanksToSizes_Private() gives the size in each dimension for the domain with those local ranks.
18618d2ec52aSSatish Balay */
18628d2ec52aSSatish Balay static PetscErrorCode DMPlexCreateHypercubicMesh_Internal(DM dm, PetscInt dim, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], PetscInt overlap, const DMBoundaryType bd[])
18638d2ec52aSSatish Balay {
18648d2ec52aSSatish Balay   const PetscInt debug = ((DM_Plex *)dm->data)->printAdj;
18658d2ec52aSSatish Balay   PetscSF        sf;
1866cfb853baSMatthew G. Knepley   Vec            coordinates;
1867cfb853baSMatthew G. Knepley   PetscSection   coordSection;
1868cfb853baSMatthew G. Knepley   DMLabel        cutLabel    = NULL;
1869cfb853baSMatthew G. Knepley   PetscBool      cutMarker   = PETSC_FALSE;
1870cfb853baSMatthew G. Knepley   PetscBool      periodic    = PETSC_FALSE;
18718d2ec52aSSatish Balay   PetscInt       numCells    = 1;
18728d2ec52aSSatish Balay   PetscInt       numVertices = 1;
18738d2ec52aSSatish Balay   PetscSFNode   *remotes;
1874cfb853baSMatthew G. Knepley   PetscScalar   *coords;
18758d2ec52aSSatish Balay   PetscInt      *procs;     // The number of processes along each dimension
18768d2ec52aSSatish Balay   PetscInt      *lrank;     // Rank in each dimension, lrank[d] \in [0, procs[d])
18778d2ec52aSSatish Balay   PetscInt      *ledges;    // The number of edges along each dimension for this process
18788d2ec52aSSatish Balay   PetscInt      *vstart;    // The first vertex along each dimension on this processes
18798d2ec52aSSatish Balay   PetscInt      *vertices;  // The number of vertices along each dimension on this process
18808d2ec52aSSatish Balay   PetscInt      *rvert;     // The global (not local) vertex number along each dimension
18818d2ec52aSSatish Balay   PetscInt      *rrank;     // The rank along each dimension for the process owning rvert[]
18828d2ec52aSSatish Balay   PetscInt      *rvertices; // The number of vertices along each dimension for the process rrank[]
18838d2ec52aSSatish Balay   PetscInt      *vert, *vtmp, *supp, cone[2], *leaves;
18848d2ec52aSSatish Balay   PetscInt       cell = 0, coordSize, Nl = 0, Nl2 = 0;
18858d2ec52aSSatish Balay   PetscMPIInt    rank, size;
18868d2ec52aSSatish Balay   MPI_Comm       comm;
1887cfb853baSMatthew G. Knepley 
1888cfb853baSMatthew G. Knepley   PetscFunctionBegin;
18898d2ec52aSSatish Balay   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
18908d2ec52aSSatish Balay   PetscCallMPI(MPI_Comm_rank(comm, &rank));
18918d2ec52aSSatish Balay   PetscCallMPI(MPI_Comm_size(comm, &size));
1892cfb853baSMatthew G. Knepley   PetscCall(DMSetDimension(dm, dim));
18938d2ec52aSSatish Balay   PetscCall(DMPlexDistributeSetDefault(dm, PETSC_FALSE));
18948d2ec52aSSatish Balay   PetscCall(PetscCalloc4(dim, &procs, dim, &lrank, dim, &rrank, 2 * dim, &supp));
18958d2ec52aSSatish Balay   PetscCall(PetscCalloc7(dim, &ledges, dim, &vertices, dim, &rvertices, dim, &vert, dim, &rvert, dim, &vstart, dim, &vtmp));
1896cfb853baSMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "marker"));
1897cfb853baSMatthew G. Knepley   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL));
18988d2ec52aSSatish Balay   for (PetscInt d = 0; d < dim; ++d) periodic = (periodic || bd[d] == DM_BOUNDARY_PERIODIC) ? PETSC_TRUE : PETSC_FALSE;
1899cfb853baSMatthew G. Knepley   if (periodic && cutMarker) {
1900cfb853baSMatthew G. Knepley     PetscCall(DMCreateLabel(dm, "periodic_cut"));
1901cfb853baSMatthew G. Knepley     PetscCall(DMGetLabel(dm, "periodic_cut", &cutLabel));
1902cfb853baSMatthew G. Knepley   }
19038d2ec52aSSatish Balay   for (PetscInt d = 0; d < dim; ++d) PetscCheck(bd[d] == DM_BOUNDARY_PERIODIC, comm, PETSC_ERR_SUP, "Hypercubic mesh must be periodic now");
19048d2ec52aSSatish Balay   overlap = overlap == PETSC_DETERMINE ? 1 : overlap;
19058d2ec52aSSatish Balay   PetscCheck(overlap >= 1, comm, PETSC_ERR_SUP, "Overlap %" PetscInt_FMT " must be greater than 0", overlap);
19068d2ec52aSSatish Balay   if (size > 1) {
19078d2ec52aSSatish Balay     PetscInt Npr = 1;
19088d2ec52aSSatish Balay 
19098d2ec52aSSatish Balay     // Make process grid
19108d2ec52aSSatish Balay     if (debug) PetscCall(PetscPrintf(comm, "Process grid:"));
19118d2ec52aSSatish Balay     for (PetscInt d = 0; d < dim; ++d) {
19128d2ec52aSSatish Balay       procs[d] = PetscRintReal(PetscPowReal(size, 1. / dim));
19138d2ec52aSSatish Balay       Npr *= procs[d];
19148d2ec52aSSatish Balay       if (debug) PetscCall(PetscPrintf(comm, " %" PetscInt_FMT, procs[d]));
19158d2ec52aSSatish Balay     }
19168d2ec52aSSatish Balay     if (debug) PetscCall(PetscPrintf(comm, "\n"));
19178d2ec52aSSatish Balay     PetscCheck(Npr == size, comm, PETSC_ERR_PLIB, "Process grid size %" PetscInt_FMT " != %d comm size", Npr, size);
19188d2ec52aSSatish Balay     IndexToTuple_Private(dim, procs, rank, lrank);
19198d2ec52aSSatish Balay     for (PetscInt d = 0; d < dim; ++d) {
19208d2ec52aSSatish Balay       ledges[d] = edges[d] / procs[d] + (edges[d] % procs[d] > lrank[d] ? 1 : 0);
19218d2ec52aSSatish Balay       vstart[d] = 0;
19228d2ec52aSSatish Balay       for (PetscInt r = 0; r < lrank[d]; ++r) vstart[d] += edges[d] / procs[d] + (edges[d] % procs[d] > r ? 1 : 0);
19238d2ec52aSSatish Balay       vstart[d] -= overlap; // For halo
19248d2ec52aSSatish Balay     }
19258d2ec52aSSatish Balay   } else {
19268d2ec52aSSatish Balay     for (PetscInt d = 0; d < dim; ++d) {
19278d2ec52aSSatish Balay       procs[d]  = 1;
19288d2ec52aSSatish Balay       ledges[d] = edges[d];
19298d2ec52aSSatish Balay     }
19308d2ec52aSSatish Balay   }
19318d2ec52aSSatish Balay   // Calculate local patch size
19328d2ec52aSSatish Balay   for (PetscInt d = 0; d < dim; ++d) {
19338d2ec52aSSatish Balay     vertices[d] = ledges[d] + (procs[d] > 1 ? 2 * overlap : 0);
1934cfb853baSMatthew G. Knepley     numVertices *= vertices[d];
1935cfb853baSMatthew G. Knepley   }
1936cfb853baSMatthew G. Knepley   numCells = numVertices * dim;
1937cfb853baSMatthew G. Knepley   PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
19388d2ec52aSSatish Balay   for (PetscInt c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, 2));
19398d2ec52aSSatish Balay   for (PetscInt v = numCells; v < numCells + numVertices; ++v) PetscCall(DMPlexSetSupportSize(dm, v, 2 * dim));
1940cfb853baSMatthew G. Knepley   PetscCall(DMSetUp(dm)); /* Allocate space for cones and supports */
1941cfb853baSMatthew G. Knepley   /* Build cell cones and vertex supports */
1942cfb853baSMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "celltype"));
19438d2ec52aSSatish Balay   if (debug) PetscCall(PetscSynchronizedPrintf(comm, "Topology for rank %d:\n", rank));
1944cfb853baSMatthew G. Knepley   while (vert[dim - 1] < vertices[dim - 1]) {
1945cfb853baSMatthew G. Knepley     const PetscInt vertex = TupleToIndex_Private(dim, vertices, vert) + numCells;
1946cfb853baSMatthew G. Knepley     PetscInt       s      = 0;
19478d2ec52aSSatish Balay     PetscBool      leaf   = PETSC_FALSE;
1948cfb853baSMatthew G. Knepley 
19498d2ec52aSSatish Balay     if (debug) {
19508d2ec52aSSatish Balay       PetscCall(PetscSynchronizedPrintf(comm, "Vertex %" PetscInt_FMT ":", vertex));
19518d2ec52aSSatish Balay       for (PetscInt d = 0; d < dim; ++d) PetscCall(PetscSynchronizedPrintf(comm, " %" PetscInt_FMT, vert[d]));
19528d2ec52aSSatish Balay       PetscCall(PetscSynchronizedPrintf(comm, "\n"));
19538d2ec52aSSatish Balay     }
1954cfb853baSMatthew G. Knepley     PetscCall(DMPlexSetCellType(dm, vertex, DM_POLYTOPE_POINT));
19558d2ec52aSSatish Balay     // Define edge cones
19568d2ec52aSSatish Balay     for (PetscInt d = 0; d < dim; ++d) {
19578d2ec52aSSatish Balay       for (PetscInt e = 0; e < dim; ++e) vtmp[e] = vert[e];
1958cfb853baSMatthew G. Knepley       vtmp[d] = (vert[d] + 1) % vertices[d];
1959cfb853baSMatthew G. Knepley       cone[0] = vertex;
1960cfb853baSMatthew G. Knepley       cone[1] = TupleToIndex_Private(dim, vertices, vtmp) + numCells;
19618d2ec52aSSatish Balay       if (debug) {
19628d2ec52aSSatish Balay         PetscCall(PetscSynchronizedPrintf(comm, "  Vertex %" PetscInt_FMT ":", cone[1]));
19638d2ec52aSSatish Balay         for (PetscInt e = 0; e < dim; ++e) PetscCall(PetscSynchronizedPrintf(comm, " %" PetscInt_FMT, vtmp[e]));
19648d2ec52aSSatish Balay         PetscCall(PetscSynchronizedPrintf(comm, "\n"));
19658d2ec52aSSatish Balay       }
1966cfb853baSMatthew G. Knepley       PetscCall(DMPlexSetCone(dm, cell, cone));
1967cfb853baSMatthew G. Knepley       PetscCall(DMPlexSetCellType(dm, cell, DM_POLYTOPE_SEGMENT));
19688d2ec52aSSatish Balay       if (debug) PetscCall(PetscSynchronizedPrintf(comm, "  Edge %" PetscInt_FMT " (%" PetscInt_FMT " %" PetscInt_FMT ")\n", cell, cone[0], cone[1]));
1969cfb853baSMatthew G. Knepley       ++cell;
19708d2ec52aSSatish Balay       // Shared vertices are any in the first or last overlap layers
19718d2ec52aSSatish Balay       if (vert[d] < overlap || vert[d] >= vertices[d] - overlap) leaf = PETSC_TRUE;
1972cfb853baSMatthew G. Knepley     }
19738d2ec52aSSatish Balay     if (size > 1 && leaf) ++Nl;
19748d2ec52aSSatish Balay     // Define vertex supports
19758d2ec52aSSatish Balay     for (PetscInt d = 0; d < dim; ++d) {
19768d2ec52aSSatish Balay       for (PetscInt e = 0; e < dim; ++e) vtmp[e] = vert[e];
1977cfb853baSMatthew G. Knepley       vtmp[d]   = (vert[d] + vertices[d] - 1) % vertices[d];
1978cfb853baSMatthew G. Knepley       supp[s++] = TupleToIndex_Private(dim, vertices, vtmp) * dim + d;
1979cfb853baSMatthew G. Knepley       supp[s++] = (vertex - numCells) * dim + d;
1980cfb853baSMatthew G. Knepley       PetscCall(DMPlexSetSupport(dm, vertex, supp));
1981cfb853baSMatthew G. Knepley     }
1982cfb853baSMatthew G. Knepley     PetscCall(DMPlexTensorPointLexicographic_Private(dim, vertices, vert));
1983cfb853baSMatthew G. Knepley   }
19848d2ec52aSSatish Balay   if (debug) PetscCall(PetscSynchronizedFlush(comm, NULL));
1985cfb853baSMatthew G. Knepley   PetscCall(DMPlexStratify(dm));
19868d2ec52aSSatish Balay   // Allocate for SF
19878d2ec52aSSatish Balay   PetscCall(PetscMalloc1(Nl, &leaves));
19888d2ec52aSSatish Balay   PetscCall(PetscMalloc1(Nl, &remotes));
19898d2ec52aSSatish Balay   // Build coordinates
1990cfb853baSMatthew G. Knepley   PetscCall(DMGetCoordinateSection(dm, &coordSection));
1991cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetNumFields(coordSection, 1));
1992cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
1993cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices));
19948d2ec52aSSatish Balay   for (PetscInt v = numCells; v < numCells + numVertices; ++v) {
1995cfb853baSMatthew G. Knepley     PetscCall(PetscSectionSetDof(coordSection, v, dim));
1996cfb853baSMatthew G. Knepley     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
1997cfb853baSMatthew G. Knepley   }
1998cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetUp(coordSection));
1999cfb853baSMatthew G. Knepley   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
20008d2ec52aSSatish Balay   PetscCall(VecCreate(comm, &coordinates));
2001cfb853baSMatthew G. Knepley   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
2002cfb853baSMatthew G. Knepley   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
2003cfb853baSMatthew G. Knepley   PetscCall(VecSetBlockSize(coordinates, dim));
2004cfb853baSMatthew G. Knepley   PetscCall(VecSetType(coordinates, VECSTANDARD));
2005cfb853baSMatthew G. Knepley   PetscCall(VecGetArray(coordinates, &coords));
20068d2ec52aSSatish Balay   if (debug) PetscCall(PetscSynchronizedPrintf(comm, "Geometry for rank %d:\n", rank));
20078d2ec52aSSatish Balay   for (PetscInt d = 0; d < dim; ++d) vert[d] = 0;
2008cfb853baSMatthew G. Knepley   while (vert[dim - 1] < vertices[dim - 1]) {
2009cfb853baSMatthew G. Knepley     const PetscInt vertex = TupleToIndex_Private(dim, vertices, vert);
20108d2ec52aSSatish Balay     PetscBool      leaf   = PETSC_FALSE;
2011cfb853baSMatthew G. Knepley 
20128d2ec52aSSatish Balay     for (PetscInt d = 0; d < dim; ++d) {
20138d2ec52aSSatish Balay       coords[vertex * dim + d] = lower[d] + ((upper[d] - lower[d]) / edges[d]) * (vert[d] + vstart[d]);
20148d2ec52aSSatish Balay       if (vert[d] < overlap || vert[d] >= vertices[d] - overlap) leaf = PETSC_TRUE;
20158d2ec52aSSatish Balay     }
20168d2ec52aSSatish Balay     if (size > 1 && leaf) {
20178d2ec52aSSatish Balay       PetscInt rnumCells = 1;
20188d2ec52aSSatish Balay 
20198d2ec52aSSatish Balay       for (PetscInt d = 0; d < dim; ++d) rvert[d] = vert[d] + vstart[d];
20208d2ec52aSSatish Balay       TupleToRanks_Private(dim, edges, procs, rvert, rrank);
20218d2ec52aSSatish Balay       leaves[Nl2]       = vertex + numCells;
20228d2ec52aSSatish Balay       remotes[Nl2].rank = TupleToIndex_Private(dim, procs, rrank);
20238d2ec52aSSatish Balay       RanksToSizes_Private(dim, edges, procs, rrank, rvertices);
20248d2ec52aSSatish Balay       for (PetscInt d = 0; d < dim; ++d) {
20258d2ec52aSSatish Balay         rvertices[d] += 2 * overlap; // Add halo
20268d2ec52aSSatish Balay         rnumCells *= rvertices[d];
20278d2ec52aSSatish Balay       }
20288d2ec52aSSatish Balay       rnumCells *= dim;
20298d2ec52aSSatish Balay       for (PetscInt d = 0; d < dim; ++d) {
20308d2ec52aSSatish Balay         const PetscInt diff = rrank[d] - lrank[d];
20318d2ec52aSSatish Balay 
20328d2ec52aSSatish Balay         if (!diff) rvert[d] = vert[d];                                     // Vertex is local
20338d2ec52aSSatish Balay         else if (rvert[d] < 0) rvert[d] = rvertices[d] - 1 + rvert[d];     // Wrap around at the bottom
20348d2ec52aSSatish Balay         else if (rvert[d] >= edges[d]) rvert[d] = rvert[d] - edges[d] + 1; // Wrap around at the top
20358d2ec52aSSatish Balay         else if (diff == -1) rvert[d] = rvertices[d] - 1 + (vert[d] - overlap);
20368d2ec52aSSatish Balay         else if (diff == 1) rvert[d] = (vertices[d] - vert[d] - 1) + overlap;
20378d2ec52aSSatish Balay         else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Process distance %" PetscInt_FMT " in direction %" PetscInt_FMT " should not be possible", diff, d);
20388d2ec52aSSatish Balay       }
20398d2ec52aSSatish Balay       remotes[Nl2].index = TupleToIndex_Private(dim, rvertices, rvert) + rnumCells;
20408d2ec52aSSatish Balay       if (debug) PetscCall(PetscSynchronizedPrintf(comm, "Shared Vertex %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ")\n", leaves[Nl2], remotes[Nl2].rank, remotes[Nl2].index));
20418d2ec52aSSatish Balay       ++Nl2;
20428d2ec52aSSatish Balay     }
20438d2ec52aSSatish Balay     if (debug) {
20448d2ec52aSSatish Balay       PetscCall(PetscSynchronizedPrintf(comm, "Vertex %" PetscInt_FMT ":", vertex));
20458d2ec52aSSatish Balay       for (PetscInt d = 0; d < dim; ++d) PetscCall(PetscSynchronizedPrintf(comm, " %" PetscInt_FMT, vert[d] + vstart[d]));
20468d2ec52aSSatish Balay       for (PetscInt d = 0; d < dim; ++d) PetscCall(PetscSynchronizedPrintf(comm, " %g", (double)PetscRealPart(coords[vertex * dim + d])));
20478d2ec52aSSatish Balay       PetscCall(PetscSynchronizedPrintf(comm, "\n"));
20488d2ec52aSSatish Balay     }
2049cfb853baSMatthew G. Knepley     PetscCall(DMPlexTensorPointLexicographic_Private(dim, vertices, vert));
2050cfb853baSMatthew G. Knepley   }
20518d2ec52aSSatish Balay   if (debug) PetscCall(PetscSynchronizedFlush(comm, NULL));
2052cfb853baSMatthew G. Knepley   PetscCall(VecRestoreArray(coordinates, &coords));
2053cfb853baSMatthew G. Knepley   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
2054cfb853baSMatthew G. Knepley   PetscCall(VecDestroy(&coordinates));
20558d2ec52aSSatish Balay   // Build SF
20568d2ec52aSSatish Balay   PetscCheck(Nl == Nl2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Initial number of leaves %" PetscInt_FMT " != %" PetscInt_FMT " final number", Nl, Nl2);
20578d2ec52aSSatish Balay   PetscCall(DMGetPointSF(dm, &sf));
20588d2ec52aSSatish Balay   PetscCall(PetscSFSetGraph(sf, numCells + numVertices, Nl, leaves, PETSC_OWN_POINTER, remotes, PETSC_OWN_POINTER));
20598d2ec52aSSatish Balay   if (debug) PetscCall(PetscSFView(sf, PETSC_VIEWER_STDOUT_WORLD));
2060cfb853baSMatthew G. Knepley   //PetscCall(DMSetPeriodicity(dm, NULL, lower, upper));
2061cfb853baSMatthew G. Knepley   // Attach the extent
2062cfb853baSMatthew G. Knepley   {
2063cfb853baSMatthew G. Knepley     PetscContainer c;
20648d2ec52aSSatish Balay     PetscInt      *extent, *lextent;
2065cfb853baSMatthew G. Knepley 
2066cfb853baSMatthew G. Knepley     PetscCall(PetscMalloc1(dim, &extent));
20678d2ec52aSSatish Balay     PetscCall(PetscMalloc1(dim, &lextent));
20688d2ec52aSSatish Balay     for (PetscInt d = 0; d < dim; ++d) {
20698d2ec52aSSatish Balay       extent[d]  = edges[d];
20708d2ec52aSSatish Balay       lextent[d] = ledges[d];
20718d2ec52aSSatish Balay     }
2072cfb853baSMatthew G. Knepley     PetscCall(PetscContainerCreate(PETSC_COMM_SELF, &c));
207349abdd8aSBarry Smith     PetscCall(PetscContainerSetCtxDestroy(c, PetscCtxDestroyDefault));
2074cfb853baSMatthew G. Knepley     PetscCall(PetscContainerSetPointer(c, extent));
2075cfb853baSMatthew G. Knepley     PetscCall(PetscObjectCompose((PetscObject)dm, "_extent", (PetscObject)c));
2076cfb853baSMatthew G. Knepley     PetscCall(PetscContainerDestroy(&c));
20778d2ec52aSSatish Balay     PetscCall(PetscContainerCreate(PETSC_COMM_SELF, &c));
20788d2ec52aSSatish Balay     PetscCall(PetscContainerSetCtxDestroy(c, PetscCtxDestroyDefault));
20798d2ec52aSSatish Balay     PetscCall(PetscContainerSetPointer(c, lextent));
20808d2ec52aSSatish Balay     PetscCall(PetscObjectCompose((PetscObject)dm, "_lextent", (PetscObject)c));
20818d2ec52aSSatish Balay     PetscCall(PetscContainerDestroy(&c));
2082cfb853baSMatthew G. Knepley   }
20838d2ec52aSSatish Balay   PetscCall(PetscFree4(procs, lrank, rrank, supp));
20848d2ec52aSSatish Balay   PetscCall(PetscFree7(ledges, vertices, rvertices, vert, rvert, vstart, vtmp));
20853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2086cfb853baSMatthew G. Knepley }
2087cfb853baSMatthew G. Knepley 
2088cfb853baSMatthew G. Knepley /*@C
2089aaa8cc7dSPierre Jolivet   DMPlexCreateHypercubicMesh - Creates a periodic mesh on the tensor product of unit intervals using only vertices and edges.
2090cfb853baSMatthew G. Knepley 
2091cfb853baSMatthew G. Knepley   Collective
2092cfb853baSMatthew G. Knepley 
2093cfb853baSMatthew G. Knepley   Input Parameters:
20948d2ec52aSSatish Balay + comm    - The communicator for the `DM` object
2095cfb853baSMatthew G. Knepley . dim     - The spatial dimension
209620f4b53cSBarry Smith . edges   - Number of edges per dimension, or `NULL` for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
209720f4b53cSBarry Smith . lower   - The lower left corner, or `NULL` for (0, 0, 0)
20988d2ec52aSSatish Balay . upper   - The upper right corner, or `NULL` for (1, 1, 1)
20998d2ec52aSSatish Balay - overlap - The number of vertices in each direction to include in the overlap (default is 1)
2100cfb853baSMatthew G. Knepley 
2101cfb853baSMatthew G. Knepley   Output Parameter:
2102cfb853baSMatthew G. Knepley . dm - The DM object
2103cfb853baSMatthew G. Knepley 
210420f4b53cSBarry Smith   Level: beginner
210520f4b53cSBarry Smith 
210620f4b53cSBarry Smith   Note:
210720f4b53cSBarry Smith   If you want to customize this mesh using options, you just need to
210820f4b53cSBarry Smith .vb
210920f4b53cSBarry Smith   DMCreate(comm, &dm);
211020f4b53cSBarry Smith   DMSetType(dm, DMPLEX);
211120f4b53cSBarry Smith   DMSetFromOptions(dm);
211220f4b53cSBarry Smith .ve
211320f4b53cSBarry Smith   and use the options on the `DMSetFromOptions()` page.
2114cfb853baSMatthew G. Knepley 
2115cfb853baSMatthew G. Knepley   The vertices are numbered is lexicographic order, and the dim edges exiting a vertex in the positive orthant are number consecutively,
211620f4b53cSBarry Smith .vb
211720f4b53cSBarry Smith  18--0-19--2-20--4-18
211820f4b53cSBarry Smith   |     |     |     |
211920f4b53cSBarry Smith  13    15    17    13
212020f4b53cSBarry Smith   |     |     |     |
212120f4b53cSBarry Smith  24-12-25-14-26-16-24
212220f4b53cSBarry Smith   |     |     |     |
212320f4b53cSBarry Smith   7     9    11     7
212420f4b53cSBarry Smith   |     |     |     |
212520f4b53cSBarry Smith  21--6-22--8-23-10-21
212620f4b53cSBarry Smith   |     |     |     |
212720f4b53cSBarry Smith   1     3     5     1
212820f4b53cSBarry Smith   |     |     |     |
212920f4b53cSBarry Smith  18--0-19--2-20--4-18
213020f4b53cSBarry Smith .ve
2131cfb853baSMatthew G. Knepley 
213276fbde31SPierre Jolivet .seealso: `DMSetFromOptions()`, `DMPlexCreateFromFile()`, `DMPlexCreateHexCylinderMesh()`, `DMSetType()`, `DMCreate()`
2133cfb853baSMatthew G. Knepley @*/
21348d2ec52aSSatish Balay PetscErrorCode DMPlexCreateHypercubicMesh(MPI_Comm comm, PetscInt dim, const PetscInt edges[], const PetscReal lower[], const PetscReal upper[], PetscInt overlap, DM *dm)
2135cfb853baSMatthew G. Knepley {
2136cfb853baSMatthew G. Knepley   PetscInt       *edg;
2137cfb853baSMatthew G. Knepley   PetscReal      *low, *upp;
2138cfb853baSMatthew G. Knepley   DMBoundaryType *bdt;
2139cfb853baSMatthew G. Knepley   PetscInt        d;
2140cfb853baSMatthew G. Knepley 
2141cfb853baSMatthew G. Knepley   PetscFunctionBegin;
2142cfb853baSMatthew G. Knepley   PetscCall(DMCreate(comm, dm));
2143cfb853baSMatthew G. Knepley   PetscCall(DMSetType(*dm, DMPLEX));
2144cfb853baSMatthew G. Knepley   PetscCall(PetscMalloc4(dim, &edg, dim, &low, dim, &upp, dim, &bdt));
2145cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) {
2146cfb853baSMatthew G. Knepley     edg[d] = edges ? edges[d] : 1;
2147cfb853baSMatthew G. Knepley     low[d] = lower ? lower[d] : 0.;
2148cfb853baSMatthew G. Knepley     upp[d] = upper ? upper[d] : 1.;
2149cfb853baSMatthew G. Knepley     bdt[d] = DM_BOUNDARY_PERIODIC;
2150cfb853baSMatthew G. Knepley   }
21518d2ec52aSSatish Balay   PetscCall(DMPlexCreateHypercubicMesh_Internal(*dm, dim, low, upp, edg, overlap, bdt));
2152cfb853baSMatthew G. Knepley   PetscCall(PetscFree4(edg, low, upp, bdt));
21533ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2154cfb853baSMatthew G. Knepley }
2155cfb853baSMatthew G. Knepley 
2156cc4c1da9SBarry Smith /*@
2157a1cb98faSBarry Smith   DMPlexSetOptionsPrefix - Sets the prefix used for searching for all `DM` options in the database.
2158a9074c1eSMatthew G. Knepley 
215920f4b53cSBarry Smith   Logically Collective
2160a9074c1eSMatthew G. Knepley 
2161a9074c1eSMatthew G. Knepley   Input Parameters:
216220f4b53cSBarry Smith + dm     - the `DM` context
2163a9074c1eSMatthew G. Knepley - prefix - the prefix to prepend to all option names
2164a9074c1eSMatthew G. Knepley 
2165a1cb98faSBarry Smith   Level: advanced
2166a1cb98faSBarry Smith 
2167a1cb98faSBarry Smith   Note:
2168a9074c1eSMatthew G. Knepley   A hyphen (-) must NOT be given at the beginning of the prefix name.
2169a9074c1eSMatthew G. Knepley   The first character of all runtime options is AUTOMATICALLY the hyphen.
2170a9074c1eSMatthew G. Knepley 
21711cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `SNESSetFromOptions()`
2172a9074c1eSMatthew G. Knepley @*/
2173d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetOptionsPrefix(DM dm, const char prefix[])
2174d71ae5a4SJacob Faibussowitsch {
2175a9074c1eSMatthew G. Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
2176a9074c1eSMatthew G. Knepley 
2177a9074c1eSMatthew G. Knepley   PetscFunctionBegin;
2178a9074c1eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21799566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, prefix));
21809566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)mesh->partitioner, prefix));
21813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2182a9074c1eSMatthew G. Knepley }
2183a9074c1eSMatthew G. Knepley 
21849318fe57SMatthew G. Knepley /* Remap geometry to cylinder
218561a622f3SMatthew G. Knepley    TODO: This only works for a single refinement, then it is broken
218661a622f3SMatthew G. Knepley 
21879318fe57SMatthew G. Knepley      Interior square: Linear interpolation is correct
21889318fe57SMatthew G. Knepley      The other cells all have vertices on rays from the origin. We want to uniformly expand the spacing
21899318fe57SMatthew G. Knepley      such that the last vertex is on the unit circle. So the closest and farthest vertices are at distance
21900510c589SMatthew G. Knepley 
21919318fe57SMatthew G. Knepley        phi     = arctan(y/x)
21929318fe57SMatthew G. Knepley        d_close = sqrt(1/8 + 1/4 sin^2(phi))
21939318fe57SMatthew G. Knepley        d_far   = sqrt(1/2 + sin^2(phi))
21940510c589SMatthew G. Knepley 
21959318fe57SMatthew G. Knepley      so we remap them using
21960510c589SMatthew G. Knepley 
21979318fe57SMatthew G. Knepley        x_new = x_close + (x - x_close) (1 - d_close) / (d_far - d_close)
21989318fe57SMatthew G. Knepley        y_new = y_close + (y - y_close) (1 - d_close) / (d_far - d_close)
21990510c589SMatthew G. Knepley 
22009318fe57SMatthew G. Knepley      If pi/4 < phi < 3pi/4 or -3pi/4 < phi < -pi/4, then we switch x and y.
22019318fe57SMatthew G. Knepley */
2202d71ae5a4SJacob 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[])
2203d71ae5a4SJacob Faibussowitsch {
22049318fe57SMatthew G. Knepley   const PetscReal dis = 1.0 / PetscSqrtReal(2.0);
22059318fe57SMatthew G. Knepley   const PetscReal ds2 = 0.5 * dis;
220622cc497dSMatthew G. Knepley 
22079318fe57SMatthew G. Knepley   if ((PetscAbsScalar(u[0]) <= ds2) && (PetscAbsScalar(u[1]) <= ds2)) {
22089318fe57SMatthew G. Knepley     f0[0] = u[0];
22099318fe57SMatthew G. Knepley     f0[1] = u[1];
22109318fe57SMatthew G. Knepley   } else {
22119318fe57SMatthew G. Knepley     PetscReal phi, sinp, cosp, dc, df, x, y, xc, yc;
22120510c589SMatthew G. Knepley 
22139318fe57SMatthew G. Knepley     x    = PetscRealPart(u[0]);
22149318fe57SMatthew G. Knepley     y    = PetscRealPart(u[1]);
22159318fe57SMatthew G. Knepley     phi  = PetscAtan2Real(y, x);
22169318fe57SMatthew G. Knepley     sinp = PetscSinReal(phi);
22179318fe57SMatthew G. Knepley     cosp = PetscCosReal(phi);
22189318fe57SMatthew G. Knepley     if ((PetscAbsReal(phi) > PETSC_PI / 4.0) && (PetscAbsReal(phi) < 3.0 * PETSC_PI / 4.0)) {
22199318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2 / sinp);
22209318fe57SMatthew G. Knepley       df = PetscAbsReal(dis / sinp);
22219318fe57SMatthew G. Knepley       xc = ds2 * x / PetscAbsReal(y);
22229318fe57SMatthew G. Knepley       yc = ds2 * PetscSignReal(y);
22239318fe57SMatthew G. Knepley     } else {
22249318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2 / cosp);
22259318fe57SMatthew G. Knepley       df = PetscAbsReal(dis / cosp);
22269318fe57SMatthew G. Knepley       xc = ds2 * PetscSignReal(x);
22279318fe57SMatthew G. Knepley       yc = ds2 * y / PetscAbsReal(x);
22289318fe57SMatthew G. Knepley     }
22299318fe57SMatthew G. Knepley     f0[0] = xc + (u[0] - xc) * (1.0 - dc) / (df - dc);
22309318fe57SMatthew G. Knepley     f0[1] = yc + (u[1] - yc) * (1.0 - dc) / (df - dc);
22319318fe57SMatthew G. Knepley   }
22329318fe57SMatthew G. Knepley   f0[2] = u[2];
22339318fe57SMatthew G. Knepley }
22340510c589SMatthew G. Knepley 
223549704ca5SMatthew G. Knepley static PetscErrorCode DMPlexCreateHexCylinderMesh_Internal(DM dm, DMBoundaryType periodicZ, PetscInt Nr)
2236d71ae5a4SJacob Faibussowitsch {
22370510c589SMatthew G. Knepley   const PetscInt dim = 3;
22389318fe57SMatthew G. Knepley   PetscInt       numCells, numVertices;
2239d8c47e87SMatthew G. Knepley   PetscMPIInt    rank;
22400510c589SMatthew G. Knepley 
22410510c589SMatthew G. Knepley   PetscFunctionBegin;
224246139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
22439566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
22449566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
22450510c589SMatthew G. Knepley   /* Create topology */
22460510c589SMatthew G. Knepley   {
22470510c589SMatthew G. Knepley     PetscInt cone[8], c;
22480510c589SMatthew G. Knepley 
2249dd400576SPatrick Sanan     numCells    = rank == 0 ? 5 : 0;
2250dd400576SPatrick Sanan     numVertices = rank == 0 ? 16 : 0;
2251006a8963SMatthew G. Knepley     if (periodicZ == DM_BOUNDARY_PERIODIC) {
2252ae8bcbbbSMatthew G. Knepley       numCells *= 3;
2253dd400576SPatrick Sanan       numVertices = rank == 0 ? 24 : 0;
2254006a8963SMatthew G. Knepley     }
22559566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
22569566063dSJacob Faibussowitsch     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 8));
22579566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm));
2258dd400576SPatrick Sanan     if (rank == 0) {
2259006a8963SMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
22609371c9d4SSatish Balay         cone[0] = 15;
22619371c9d4SSatish Balay         cone[1] = 18;
22629371c9d4SSatish Balay         cone[2] = 17;
22639371c9d4SSatish Balay         cone[3] = 16;
22649371c9d4SSatish Balay         cone[4] = 31;
22659371c9d4SSatish Balay         cone[5] = 32;
22669371c9d4SSatish Balay         cone[6] = 33;
22679371c9d4SSatish Balay         cone[7] = 34;
22689566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
22699371c9d4SSatish Balay         cone[0] = 16;
22709371c9d4SSatish Balay         cone[1] = 17;
22719371c9d4SSatish Balay         cone[2] = 24;
22729371c9d4SSatish Balay         cone[3] = 23;
22739371c9d4SSatish Balay         cone[4] = 32;
22749371c9d4SSatish Balay         cone[5] = 36;
22759371c9d4SSatish Balay         cone[6] = 37;
22769371c9d4SSatish Balay         cone[7] = 33; /* 22 25 26 21 */
22779566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
22789371c9d4SSatish Balay         cone[0] = 18;
22799371c9d4SSatish Balay         cone[1] = 27;
22809371c9d4SSatish Balay         cone[2] = 24;
22819371c9d4SSatish Balay         cone[3] = 17;
22829371c9d4SSatish Balay         cone[4] = 34;
22839371c9d4SSatish Balay         cone[5] = 33;
22849371c9d4SSatish Balay         cone[6] = 37;
22859371c9d4SSatish Balay         cone[7] = 38;
22869566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
22879371c9d4SSatish Balay         cone[0] = 29;
22889371c9d4SSatish Balay         cone[1] = 27;
22899371c9d4SSatish Balay         cone[2] = 18;
22909371c9d4SSatish Balay         cone[3] = 15;
22919371c9d4SSatish Balay         cone[4] = 35;
22929371c9d4SSatish Balay         cone[5] = 31;
22939371c9d4SSatish Balay         cone[6] = 34;
22949371c9d4SSatish Balay         cone[7] = 38;
22959566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
22969371c9d4SSatish Balay         cone[0] = 29;
22979371c9d4SSatish Balay         cone[1] = 15;
22989371c9d4SSatish Balay         cone[2] = 16;
22999371c9d4SSatish Balay         cone[3] = 23;
23009371c9d4SSatish Balay         cone[4] = 35;
23019371c9d4SSatish Balay         cone[5] = 36;
23029371c9d4SSatish Balay         cone[6] = 32;
23039371c9d4SSatish Balay         cone[7] = 31;
23049566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
2305006a8963SMatthew G. Knepley 
23069371c9d4SSatish Balay         cone[0] = 31;
23079371c9d4SSatish Balay         cone[1] = 34;
23089371c9d4SSatish Balay         cone[2] = 33;
23099371c9d4SSatish Balay         cone[3] = 32;
23109371c9d4SSatish Balay         cone[4] = 19;
23119371c9d4SSatish Balay         cone[5] = 22;
23129371c9d4SSatish Balay         cone[6] = 21;
23139371c9d4SSatish Balay         cone[7] = 20;
23149566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 5, cone));
23159371c9d4SSatish Balay         cone[0] = 32;
23169371c9d4SSatish Balay         cone[1] = 33;
23179371c9d4SSatish Balay         cone[2] = 37;
23189371c9d4SSatish Balay         cone[3] = 36;
23199371c9d4SSatish Balay         cone[4] = 22;
23209371c9d4SSatish Balay         cone[5] = 25;
23219371c9d4SSatish Balay         cone[6] = 26;
23229371c9d4SSatish Balay         cone[7] = 21;
23239566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 6, cone));
23249371c9d4SSatish Balay         cone[0] = 34;
23259371c9d4SSatish Balay         cone[1] = 38;
23269371c9d4SSatish Balay         cone[2] = 37;
23279371c9d4SSatish Balay         cone[3] = 33;
23289371c9d4SSatish Balay         cone[4] = 20;
23299371c9d4SSatish Balay         cone[5] = 21;
23309371c9d4SSatish Balay         cone[6] = 26;
23319371c9d4SSatish Balay         cone[7] = 28;
23329566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 7, cone));
23339371c9d4SSatish Balay         cone[0] = 35;
23349371c9d4SSatish Balay         cone[1] = 38;
23359371c9d4SSatish Balay         cone[2] = 34;
23369371c9d4SSatish Balay         cone[3] = 31;
23379371c9d4SSatish Balay         cone[4] = 30;
23389371c9d4SSatish Balay         cone[5] = 19;
23399371c9d4SSatish Balay         cone[6] = 20;
23409371c9d4SSatish Balay         cone[7] = 28;
23419566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 8, cone));
23429371c9d4SSatish Balay         cone[0] = 35;
23439371c9d4SSatish Balay         cone[1] = 31;
23449371c9d4SSatish Balay         cone[2] = 32;
23459371c9d4SSatish Balay         cone[3] = 36;
23469371c9d4SSatish Balay         cone[4] = 30;
23479371c9d4SSatish Balay         cone[5] = 25;
23489371c9d4SSatish Balay         cone[6] = 22;
23499371c9d4SSatish Balay         cone[7] = 19;
23509566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 9, cone));
2351ae8bcbbbSMatthew G. Knepley 
23529371c9d4SSatish Balay         cone[0] = 19;
23539371c9d4SSatish Balay         cone[1] = 20;
23549371c9d4SSatish Balay         cone[2] = 21;
23559371c9d4SSatish Balay         cone[3] = 22;
23569371c9d4SSatish Balay         cone[4] = 15;
23579371c9d4SSatish Balay         cone[5] = 16;
23589371c9d4SSatish Balay         cone[6] = 17;
23599371c9d4SSatish Balay         cone[7] = 18;
23609566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 10, cone));
23619371c9d4SSatish Balay         cone[0] = 22;
23629371c9d4SSatish Balay         cone[1] = 21;
23639371c9d4SSatish Balay         cone[2] = 26;
23649371c9d4SSatish Balay         cone[3] = 25;
23659371c9d4SSatish Balay         cone[4] = 16;
23669371c9d4SSatish Balay         cone[5] = 23;
23679371c9d4SSatish Balay         cone[6] = 24;
23689371c9d4SSatish Balay         cone[7] = 17;
23699566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 11, cone));
23709371c9d4SSatish Balay         cone[0] = 20;
23719371c9d4SSatish Balay         cone[1] = 28;
23729371c9d4SSatish Balay         cone[2] = 26;
23739371c9d4SSatish Balay         cone[3] = 21;
23749371c9d4SSatish Balay         cone[4] = 18;
23759371c9d4SSatish Balay         cone[5] = 17;
23769371c9d4SSatish Balay         cone[6] = 24;
23779371c9d4SSatish Balay         cone[7] = 27;
23789566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 12, cone));
23799371c9d4SSatish Balay         cone[0] = 30;
23809371c9d4SSatish Balay         cone[1] = 28;
23819371c9d4SSatish Balay         cone[2] = 20;
23829371c9d4SSatish Balay         cone[3] = 19;
23839371c9d4SSatish Balay         cone[4] = 29;
23849371c9d4SSatish Balay         cone[5] = 15;
23859371c9d4SSatish Balay         cone[6] = 18;
23869371c9d4SSatish Balay         cone[7] = 27;
23879566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 13, cone));
23889371c9d4SSatish Balay         cone[0] = 30;
23899371c9d4SSatish Balay         cone[1] = 19;
23909371c9d4SSatish Balay         cone[2] = 22;
23919371c9d4SSatish Balay         cone[3] = 25;
23929371c9d4SSatish Balay         cone[4] = 29;
23939371c9d4SSatish Balay         cone[5] = 23;
23949371c9d4SSatish Balay         cone[6] = 16;
23959371c9d4SSatish Balay         cone[7] = 15;
23969566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 14, cone));
2397006a8963SMatthew G. Knepley       } else {
23989371c9d4SSatish Balay         cone[0] = 5;
23999371c9d4SSatish Balay         cone[1] = 8;
24009371c9d4SSatish Balay         cone[2] = 7;
24019371c9d4SSatish Balay         cone[3] = 6;
24029371c9d4SSatish Balay         cone[4] = 9;
24039371c9d4SSatish Balay         cone[5] = 12;
24049371c9d4SSatish Balay         cone[6] = 11;
24059371c9d4SSatish Balay         cone[7] = 10;
24069566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
24079371c9d4SSatish Balay         cone[0] = 6;
24089371c9d4SSatish Balay         cone[1] = 7;
24099371c9d4SSatish Balay         cone[2] = 14;
24109371c9d4SSatish Balay         cone[3] = 13;
24119371c9d4SSatish Balay         cone[4] = 12;
24129371c9d4SSatish Balay         cone[5] = 15;
24139371c9d4SSatish Balay         cone[6] = 16;
24149371c9d4SSatish Balay         cone[7] = 11;
24159566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
24169371c9d4SSatish Balay         cone[0] = 8;
24179371c9d4SSatish Balay         cone[1] = 17;
24189371c9d4SSatish Balay         cone[2] = 14;
24199371c9d4SSatish Balay         cone[3] = 7;
24209371c9d4SSatish Balay         cone[4] = 10;
24219371c9d4SSatish Balay         cone[5] = 11;
24229371c9d4SSatish Balay         cone[6] = 16;
24239371c9d4SSatish Balay         cone[7] = 18;
24249566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
24259371c9d4SSatish Balay         cone[0] = 19;
24269371c9d4SSatish Balay         cone[1] = 17;
24279371c9d4SSatish Balay         cone[2] = 8;
24289371c9d4SSatish Balay         cone[3] = 5;
24299371c9d4SSatish Balay         cone[4] = 20;
24309371c9d4SSatish Balay         cone[5] = 9;
24319371c9d4SSatish Balay         cone[6] = 10;
24329371c9d4SSatish Balay         cone[7] = 18;
24339566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
24349371c9d4SSatish Balay         cone[0] = 19;
24359371c9d4SSatish Balay         cone[1] = 5;
24369371c9d4SSatish Balay         cone[2] = 6;
24379371c9d4SSatish Balay         cone[3] = 13;
24389371c9d4SSatish Balay         cone[4] = 20;
24399371c9d4SSatish Balay         cone[5] = 15;
24409371c9d4SSatish Balay         cone[6] = 12;
24419371c9d4SSatish Balay         cone[7] = 9;
24429566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
2443006a8963SMatthew G. Knepley       }
2444d8c47e87SMatthew G. Knepley     }
24459566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
24469566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
24470510c589SMatthew G. Knepley   }
2448dbc1dc17SMatthew G. Knepley   /* Create cube geometry */
24490510c589SMatthew G. Knepley   {
24500510c589SMatthew G. Knepley     Vec             coordinates;
24510510c589SMatthew G. Knepley     PetscSection    coordSection;
24520510c589SMatthew G. Knepley     PetscScalar    *coords;
24530510c589SMatthew G. Knepley     PetscInt        coordSize, v;
24540510c589SMatthew G. Knepley     const PetscReal dis = 1.0 / PetscSqrtReal(2.0);
24550510c589SMatthew G. Knepley     const PetscReal ds2 = dis / 2.0;
24560510c589SMatthew G. Knepley 
24570510c589SMatthew G. Knepley     /* Build coordinates */
24589566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
24599566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
24609566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
24619566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices));
24620510c589SMatthew G. Knepley     for (v = numCells; v < numCells + numVertices; ++v) {
24639566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
24649566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
24650510c589SMatthew G. Knepley     }
24669566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
24679566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
24689566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
24699566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
24709566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
24719566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
24729566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates, VECSTANDARD));
24739566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
2474dd400576SPatrick Sanan     if (rank == 0) {
24759371c9d4SSatish Balay       coords[0 * dim + 0]  = -ds2;
24769371c9d4SSatish Balay       coords[0 * dim + 1]  = -ds2;
24779371c9d4SSatish Balay       coords[0 * dim + 2]  = 0.0;
24789371c9d4SSatish Balay       coords[1 * dim + 0]  = ds2;
24799371c9d4SSatish Balay       coords[1 * dim + 1]  = -ds2;
24809371c9d4SSatish Balay       coords[1 * dim + 2]  = 0.0;
24819371c9d4SSatish Balay       coords[2 * dim + 0]  = ds2;
24829371c9d4SSatish Balay       coords[2 * dim + 1]  = ds2;
24839371c9d4SSatish Balay       coords[2 * dim + 2]  = 0.0;
24849371c9d4SSatish Balay       coords[3 * dim + 0]  = -ds2;
24859371c9d4SSatish Balay       coords[3 * dim + 1]  = ds2;
24869371c9d4SSatish Balay       coords[3 * dim + 2]  = 0.0;
24879371c9d4SSatish Balay       coords[4 * dim + 0]  = -ds2;
24889371c9d4SSatish Balay       coords[4 * dim + 1]  = -ds2;
24899371c9d4SSatish Balay       coords[4 * dim + 2]  = 1.0;
24909371c9d4SSatish Balay       coords[5 * dim + 0]  = -ds2;
24919371c9d4SSatish Balay       coords[5 * dim + 1]  = ds2;
24929371c9d4SSatish Balay       coords[5 * dim + 2]  = 1.0;
24939371c9d4SSatish Balay       coords[6 * dim + 0]  = ds2;
24949371c9d4SSatish Balay       coords[6 * dim + 1]  = ds2;
24959371c9d4SSatish Balay       coords[6 * dim + 2]  = 1.0;
24969371c9d4SSatish Balay       coords[7 * dim + 0]  = ds2;
24979371c9d4SSatish Balay       coords[7 * dim + 1]  = -ds2;
24989371c9d4SSatish Balay       coords[7 * dim + 2]  = 1.0;
24999371c9d4SSatish Balay       coords[8 * dim + 0]  = dis;
25009371c9d4SSatish Balay       coords[8 * dim + 1]  = -dis;
25019371c9d4SSatish Balay       coords[8 * dim + 2]  = 0.0;
25029371c9d4SSatish Balay       coords[9 * dim + 0]  = dis;
25039371c9d4SSatish Balay       coords[9 * dim + 1]  = dis;
25049371c9d4SSatish Balay       coords[9 * dim + 2]  = 0.0;
25059371c9d4SSatish Balay       coords[10 * dim + 0] = dis;
25069371c9d4SSatish Balay       coords[10 * dim + 1] = -dis;
25079371c9d4SSatish Balay       coords[10 * dim + 2] = 1.0;
25089371c9d4SSatish Balay       coords[11 * dim + 0] = dis;
25099371c9d4SSatish Balay       coords[11 * dim + 1] = dis;
25109371c9d4SSatish Balay       coords[11 * dim + 2] = 1.0;
25119371c9d4SSatish Balay       coords[12 * dim + 0] = -dis;
25129371c9d4SSatish Balay       coords[12 * dim + 1] = dis;
25139371c9d4SSatish Balay       coords[12 * dim + 2] = 0.0;
25149371c9d4SSatish Balay       coords[13 * dim + 0] = -dis;
25159371c9d4SSatish Balay       coords[13 * dim + 1] = dis;
25169371c9d4SSatish Balay       coords[13 * dim + 2] = 1.0;
25179371c9d4SSatish Balay       coords[14 * dim + 0] = -dis;
25189371c9d4SSatish Balay       coords[14 * dim + 1] = -dis;
25199371c9d4SSatish Balay       coords[14 * dim + 2] = 0.0;
25209371c9d4SSatish Balay       coords[15 * dim + 0] = -dis;
25219371c9d4SSatish Balay       coords[15 * dim + 1] = -dis;
25229371c9d4SSatish Balay       coords[15 * dim + 2] = 1.0;
2523ae8bcbbbSMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
25249371c9d4SSatish Balay         /* 15 31 19 */ coords[16 * dim + 0] = -ds2;
25259371c9d4SSatish Balay         coords[16 * dim + 1]                = -ds2;
25269371c9d4SSatish Balay         coords[16 * dim + 2]                = 0.5;
25279371c9d4SSatish Balay         /* 16 32 22 */ coords[17 * dim + 0] = ds2;
25289371c9d4SSatish Balay         coords[17 * dim + 1]                = -ds2;
25299371c9d4SSatish Balay         coords[17 * dim + 2]                = 0.5;
25309371c9d4SSatish Balay         /* 17 33 21 */ coords[18 * dim + 0] = ds2;
25319371c9d4SSatish Balay         coords[18 * dim + 1]                = ds2;
25329371c9d4SSatish Balay         coords[18 * dim + 2]                = 0.5;
25339371c9d4SSatish Balay         /* 18 34 20 */ coords[19 * dim + 0] = -ds2;
25349371c9d4SSatish Balay         coords[19 * dim + 1]                = ds2;
25359371c9d4SSatish Balay         coords[19 * dim + 2]                = 0.5;
25369371c9d4SSatish Balay         /* 29 35 30 */ coords[20 * dim + 0] = -dis;
25379371c9d4SSatish Balay         coords[20 * dim + 1]                = -dis;
25389371c9d4SSatish Balay         coords[20 * dim + 2]                = 0.5;
25399371c9d4SSatish Balay         /* 23 36 25 */ coords[21 * dim + 0] = dis;
25409371c9d4SSatish Balay         coords[21 * dim + 1]                = -dis;
25419371c9d4SSatish Balay         coords[21 * dim + 2]                = 0.5;
25429371c9d4SSatish Balay         /* 24 37 26 */ coords[22 * dim + 0] = dis;
25439371c9d4SSatish Balay         coords[22 * dim + 1]                = dis;
25449371c9d4SSatish Balay         coords[22 * dim + 2]                = 0.5;
25459371c9d4SSatish Balay         /* 27 38 28 */ coords[23 * dim + 0] = -dis;
25469371c9d4SSatish Balay         coords[23 * dim + 1]                = dis;
25479371c9d4SSatish Balay         coords[23 * dim + 2]                = 0.5;
2548ae8bcbbbSMatthew G. Knepley       }
2549d8c47e87SMatthew G. Knepley     }
25509566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
25519566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
25529566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
25530510c589SMatthew G. Knepley   }
2554006a8963SMatthew G. Knepley   /* Create periodicity */
2555006a8963SMatthew G. Knepley   if (periodicZ == DM_BOUNDARY_PERIODIC || periodicZ == DM_BOUNDARY_TWIST) {
25566858538eSMatthew G. Knepley     PetscReal L[3]       = {-1., -1., 0.};
25576858538eSMatthew G. Knepley     PetscReal maxCell[3] = {-1., -1., 0.};
2558006a8963SMatthew G. Knepley     PetscReal lower[3]   = {0.0, 0.0, 0.0};
2559ae8bcbbbSMatthew G. Knepley     PetscReal upper[3]   = {1.0, 1.0, 1.5};
25606858538eSMatthew G. Knepley     PetscInt  numZCells  = 3;
2561006a8963SMatthew G. Knepley 
25626858538eSMatthew G. Knepley     L[2]       = upper[2] - lower[2];
25636858538eSMatthew G. Knepley     maxCell[2] = 1.1 * (L[2] / numZCells);
25644fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, maxCell, lower, L));
2565006a8963SMatthew G. Knepley   }
2566dbc1dc17SMatthew G. Knepley   {
25679318fe57SMatthew G. Knepley     DM          cdm;
25689318fe57SMatthew G. Knepley     PetscDS     cds;
25699318fe57SMatthew G. Knepley     PetscScalar c[2] = {1.0, 1.0};
2570dbc1dc17SMatthew G. Knepley 
257149704ca5SMatthew G. Knepley     PetscCall(DMPlexCreateCoordinateSpace(dm, 1, PETSC_TRUE, NULL));
25729566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
25739566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
25749566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, 2, c));
2575dbc1dc17SMatthew G. Knepley   }
257646139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
257746139095SJed Brown 
25789318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
25799566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolateInPlace_Internal(dm));
258049704ca5SMatthew G. Knepley 
258149704ca5SMatthew G. Knepley   char        oldprefix[PETSC_MAX_PATH_LEN];
258249704ca5SMatthew G. Knepley   const char *prefix;
258349704ca5SMatthew G. Knepley 
258449704ca5SMatthew G. Knepley   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, &prefix));
258549704ca5SMatthew G. Knepley   PetscCall(PetscStrncpy(oldprefix, prefix, PETSC_MAX_PATH_LEN));
258649704ca5SMatthew G. Knepley   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, "petsc_cyl_ref_"));
258749704ca5SMatthew G. Knepley   for (PetscInt r = 0; r < PetscMax(0, Nr); ++r) {
258849704ca5SMatthew G. Knepley     DM rdm;
258949704ca5SMatthew G. Knepley 
259049704ca5SMatthew G. Knepley     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &rdm));
259149704ca5SMatthew G. Knepley     PetscCall(DMPlexReplace_Internal(dm, &rdm));
259249704ca5SMatthew G. Knepley   }
259349704ca5SMatthew G. Knepley   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, oldprefix));
259449704ca5SMatthew G. Knepley   PetscCall(DMPlexRemapGeometry(dm, 0.0, snapToCylinder));
259549704ca5SMatthew G. Knepley 
259649704ca5SMatthew G. Knepley   DMLabel         bdlabel, edgelabel;
259749704ca5SMatthew G. Knepley   IS              faceIS;
259849704ca5SMatthew G. Knepley   const PetscInt *faces;
259949704ca5SMatthew G. Knepley   PetscInt        Nf;
260049704ca5SMatthew G. Knepley 
260149704ca5SMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "marker"));
260249704ca5SMatthew G. Knepley   PetscCall(DMGetLabel(dm, "marker", &bdlabel));
260349704ca5SMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "generatrix"));
260449704ca5SMatthew G. Knepley   PetscCall(DMGetLabel(dm, "generatrix", &edgelabel));
260549704ca5SMatthew G. Knepley   PetscCall(DMPlexMarkBoundaryFaces(dm, PETSC_DETERMINE, bdlabel));
260649704ca5SMatthew G. Knepley   // Remove faces on top and bottom
260749704ca5SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(bdlabel, 1, &faceIS));
2608ba1b3593SJeremy L Thompson   if (faceIS) {
260949704ca5SMatthew G. Knepley     PetscCall(ISGetLocalSize(faceIS, &Nf));
261049704ca5SMatthew G. Knepley     PetscCall(ISGetIndices(faceIS, &faces));
261149704ca5SMatthew G. Knepley     for (PetscInt f = 0; f < Nf; ++f) {
261249704ca5SMatthew G. Knepley       PetscReal vol, normal[3];
261349704ca5SMatthew G. Knepley 
261449704ca5SMatthew G. Knepley       PetscCall(DMPlexComputeCellGeometryFVM(dm, faces[f], &vol, NULL, normal));
261549704ca5SMatthew G. Knepley       if (PetscAbsReal(normal[2]) < PETSC_SMALL) PetscCall(DMLabelSetValue(edgelabel, faces[f], 1));
261649704ca5SMatthew G. Knepley     }
261749704ca5SMatthew G. Knepley     PetscCall(ISRestoreIndices(faceIS, &faces));
261849704ca5SMatthew G. Knepley     PetscCall(ISDestroy(&faceIS));
2619ba1b3593SJeremy L Thompson   }
262049704ca5SMatthew G. Knepley   PetscCall(DMPlexLabelComplete(dm, bdlabel));
262149704ca5SMatthew G. Knepley   PetscCall(DMPlexLabelComplete(dm, edgelabel));
26223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
26230510c589SMatthew G. Knepley }
26240510c589SMatthew G. Knepley 
262524119c2aSMatthew G. Knepley /*@
26269318fe57SMatthew G. Knepley   DMPlexCreateHexCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using hexahedra.
262724119c2aSMatthew G. Knepley 
2628d083f849SBarry Smith   Collective
262924119c2aSMatthew G. Knepley 
263024119c2aSMatthew G. Knepley   Input Parameters:
2631a1cb98faSBarry Smith + comm      - The communicator for the `DM` object
263249704ca5SMatthew G. Knepley . periodicZ - The boundary type for the Z direction
263349704ca5SMatthew G. Knepley - Nr        - The number of refinements to carry out
263424119c2aSMatthew G. Knepley 
263524119c2aSMatthew G. Knepley   Output Parameter:
263620f4b53cSBarry Smith . dm - The `DM` object
263724119c2aSMatthew G. Knepley 
263824119c2aSMatthew G. Knepley   Level: beginner
263924119c2aSMatthew G. Knepley 
2640a1cb98faSBarry Smith   Note:
2641a4e35b19SJacob Faibussowitsch   Here is the output numbering looking from the bottom of the cylinder\:
2642a1cb98faSBarry Smith .vb
2643a1cb98faSBarry Smith        17-----14
2644a1cb98faSBarry Smith         |     |
2645a1cb98faSBarry Smith         |  2  |
2646a1cb98faSBarry Smith         |     |
2647a1cb98faSBarry Smith  17-----8-----7-----14
2648a1cb98faSBarry Smith   |     |     |     |
2649a1cb98faSBarry Smith   |  3  |  0  |  1  |
2650a1cb98faSBarry Smith   |     |     |     |
2651a1cb98faSBarry Smith  19-----5-----6-----13
2652a1cb98faSBarry Smith         |     |
2653a1cb98faSBarry Smith         |  4  |
2654a1cb98faSBarry Smith         |     |
2655a1cb98faSBarry Smith        19-----13
2656a1cb98faSBarry Smith 
2657a1cb98faSBarry Smith  and up through the top
2658a1cb98faSBarry Smith 
2659a1cb98faSBarry Smith        18-----16
2660a1cb98faSBarry Smith         |     |
2661a1cb98faSBarry Smith         |  2  |
2662a1cb98faSBarry Smith         |     |
2663a1cb98faSBarry Smith  18----10----11-----16
2664a1cb98faSBarry Smith   |     |     |     |
2665a1cb98faSBarry Smith   |  3  |  0  |  1  |
2666a1cb98faSBarry Smith   |     |     |     |
2667a1cb98faSBarry Smith  20-----9----12-----15
2668a1cb98faSBarry Smith         |     |
2669a1cb98faSBarry Smith         |  4  |
2670a1cb98faSBarry Smith         |     |
2671a1cb98faSBarry Smith        20-----15
2672a1cb98faSBarry Smith .ve
2673a1cb98faSBarry Smith 
26741cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
267524119c2aSMatthew G. Knepley @*/
267649704ca5SMatthew G. Knepley PetscErrorCode DMPlexCreateHexCylinderMesh(MPI_Comm comm, DMBoundaryType periodicZ, PetscInt Nr, DM *dm)
2677d71ae5a4SJacob Faibussowitsch {
26789318fe57SMatthew G. Knepley   PetscFunctionBegin;
267949704ca5SMatthew G. Knepley   PetscAssertPointer(dm, 4);
26809566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
26819566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
268249704ca5SMatthew G. Knepley   PetscCall(DMPlexCreateHexCylinderMesh_Internal(*dm, periodicZ, Nr));
26833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
26849318fe57SMatthew G. Knepley }
26859318fe57SMatthew G. Knepley 
2686d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateWedgeCylinderMesh_Internal(DM dm, PetscInt n, PetscBool interpolate)
2687d71ae5a4SJacob Faibussowitsch {
268824119c2aSMatthew G. Knepley   const PetscInt dim = 3;
2689412e9a14SMatthew G. Knepley   PetscInt       numCells, numVertices, v;
26909fe9f049SMatthew G. Knepley   PetscMPIInt    rank;
269124119c2aSMatthew G. Knepley 
269224119c2aSMatthew G. Knepley   PetscFunctionBegin;
269363a3b9bcSJacob Faibussowitsch   PetscCheck(n >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of wedges %" PetscInt_FMT " cannot be negative", n);
269446139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
26959566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
26969566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
2697412e9a14SMatthew G. Knepley   /* Must create the celltype label here so that we do not automatically try to compute the types */
26989566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "celltype"));
269924119c2aSMatthew G. Knepley   /* Create topology */
270024119c2aSMatthew G. Knepley   {
270124119c2aSMatthew G. Knepley     PetscInt cone[6], c;
270224119c2aSMatthew G. Knepley 
2703dd400576SPatrick Sanan     numCells    = rank == 0 ? n : 0;
2704dd400576SPatrick Sanan     numVertices = rank == 0 ? 2 * (n + 1) : 0;
27059566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
27069566063dSJacob Faibussowitsch     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 6));
27079566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm));
270824119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
27099371c9d4SSatish Balay       cone[0] = c + n * 1;
27109371c9d4SSatish Balay       cone[1] = (c + 1) % n + n * 1;
27119371c9d4SSatish Balay       cone[2] = 0 + 3 * n;
27129371c9d4SSatish Balay       cone[3] = c + n * 2;
27139371c9d4SSatish Balay       cone[4] = (c + 1) % n + n * 2;
27149371c9d4SSatish Balay       cone[5] = 1 + 3 * n;
27159566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(dm, c, cone));
27169566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(dm, c, DM_POLYTOPE_TRI_PRISM_TENSOR));
271724119c2aSMatthew G. Knepley     }
27189566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
27199566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
272024119c2aSMatthew G. Knepley   }
272148a46eb9SPierre Jolivet   for (v = numCells; v < numCells + numVertices; ++v) PetscCall(DMPlexSetCellType(dm, v, DM_POLYTOPE_POINT));
272224119c2aSMatthew G. Knepley   /* Create cylinder geometry */
272324119c2aSMatthew G. Knepley   {
272424119c2aSMatthew G. Knepley     Vec          coordinates;
272524119c2aSMatthew G. Knepley     PetscSection coordSection;
272624119c2aSMatthew G. Knepley     PetscScalar *coords;
2727412e9a14SMatthew G. Knepley     PetscInt     coordSize, c;
272824119c2aSMatthew G. Knepley 
272924119c2aSMatthew G. Knepley     /* Build coordinates */
27309566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
27319566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
27329566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
27339566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices));
273424119c2aSMatthew G. Knepley     for (v = numCells; v < numCells + numVertices; ++v) {
27359566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
27369566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
273724119c2aSMatthew G. Knepley     }
27389566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
27399566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
27409566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
27419566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
27429566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
27439566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
27449566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates, VECSTANDARD));
27459566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
274624119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
27479371c9d4SSatish Balay       coords[(c + 0 * n) * dim + 0] = PetscCosReal(2.0 * c * PETSC_PI / n);
27489371c9d4SSatish Balay       coords[(c + 0 * n) * dim + 1] = PetscSinReal(2.0 * c * PETSC_PI / n);
27499371c9d4SSatish Balay       coords[(c + 0 * n) * dim + 2] = 1.0;
27509371c9d4SSatish Balay       coords[(c + 1 * n) * dim + 0] = PetscCosReal(2.0 * c * PETSC_PI / n);
27519371c9d4SSatish Balay       coords[(c + 1 * n) * dim + 1] = PetscSinReal(2.0 * c * PETSC_PI / n);
27529371c9d4SSatish Balay       coords[(c + 1 * n) * dim + 2] = 0.0;
275324119c2aSMatthew G. Knepley     }
2754dd400576SPatrick Sanan     if (rank == 0) {
27559371c9d4SSatish Balay       coords[(2 * n + 0) * dim + 0] = 0.0;
27569371c9d4SSatish Balay       coords[(2 * n + 0) * dim + 1] = 0.0;
27579371c9d4SSatish Balay       coords[(2 * n + 0) * dim + 2] = 1.0;
27589371c9d4SSatish Balay       coords[(2 * n + 1) * dim + 0] = 0.0;
27599371c9d4SSatish Balay       coords[(2 * n + 1) * dim + 1] = 0.0;
27609371c9d4SSatish Balay       coords[(2 * n + 1) * dim + 2] = 0.0;
27619fe9f049SMatthew G. Knepley     }
27629566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
27639566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
27649566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
276524119c2aSMatthew G. Knepley   }
276646139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
27679318fe57SMatthew G. Knepley   /* Interpolate */
27689566063dSJacob Faibussowitsch   if (interpolate) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
27693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
27709318fe57SMatthew G. Knepley }
27719318fe57SMatthew G. Knepley 
27729318fe57SMatthew G. Knepley /*@
27739318fe57SMatthew G. Knepley   DMPlexCreateWedgeCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using wedges.
27749318fe57SMatthew G. Knepley 
27759318fe57SMatthew G. Knepley   Collective
27769318fe57SMatthew G. Knepley 
27779318fe57SMatthew G. Knepley   Input Parameters:
2778a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
27799318fe57SMatthew G. Knepley . n           - The number of wedges around the origin
27809318fe57SMatthew G. Knepley - interpolate - Create edges and faces
27819318fe57SMatthew G. Knepley 
27829318fe57SMatthew G. Knepley   Output Parameter:
2783a1cb98faSBarry Smith . dm - The `DM` object
27849318fe57SMatthew G. Knepley 
27859318fe57SMatthew G. Knepley   Level: beginner
27869318fe57SMatthew G. Knepley 
27871cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateHexCylinderMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
27889318fe57SMatthew G. Knepley @*/
2789d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateWedgeCylinderMesh(MPI_Comm comm, PetscInt n, PetscBool interpolate, DM *dm)
2790d71ae5a4SJacob Faibussowitsch {
27919318fe57SMatthew G. Knepley   PetscFunctionBegin;
27924f572ea9SToby Isaac   PetscAssertPointer(dm, 4);
27939566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
27949566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
27959566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateWedgeCylinderMesh_Internal(*dm, n, interpolate));
27963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
279724119c2aSMatthew G. Knepley }
279824119c2aSMatthew G. Knepley 
2799d71ae5a4SJacob Faibussowitsch static inline PetscReal DiffNormReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
2800d71ae5a4SJacob Faibussowitsch {
280165a81367SMatthew G. Knepley   PetscReal prod = 0.0;
280265a81367SMatthew G. Knepley   PetscInt  i;
280365a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += PetscSqr(x[i] - y[i]);
280465a81367SMatthew G. Knepley   return PetscSqrtReal(prod);
280565a81367SMatthew G. Knepley }
2806dd2b43ebSStefano Zampini 
2807d71ae5a4SJacob Faibussowitsch static inline PetscReal DotReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
2808d71ae5a4SJacob Faibussowitsch {
280965a81367SMatthew G. Knepley   PetscReal prod = 0.0;
281065a81367SMatthew G. Knepley   PetscInt  i;
281165a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += x[i] * y[i];
281265a81367SMatthew G. Knepley   return prod;
281365a81367SMatthew G. Knepley }
281465a81367SMatthew G. Knepley 
281551a74b61SMatthew G. Knepley /* The first constant is the sphere radius */
2816d71ae5a4SJacob 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[])
2817d71ae5a4SJacob Faibussowitsch {
281851a74b61SMatthew G. Knepley   PetscReal r     = PetscRealPart(constants[0]);
281951a74b61SMatthew G. Knepley   PetscReal norm2 = 0.0, fac;
282051a74b61SMatthew G. Knepley   PetscInt  n     = uOff[1] - uOff[0], d;
282151a74b61SMatthew G. Knepley 
282251a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) norm2 += PetscSqr(PetscRealPart(u[d]));
282351a74b61SMatthew G. Knepley   fac = r / PetscSqrtReal(norm2);
282451a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) f0[d] = u[d] * fac;
282551a74b61SMatthew G. Knepley }
282651a74b61SMatthew G. Knepley 
2827d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSphereMesh_Internal(DM dm, PetscInt dim, PetscBool simplex, PetscReal R)
2828d71ae5a4SJacob Faibussowitsch {
282965a81367SMatthew G. Knepley   const PetscInt embedDim = dim + 1;
283065a81367SMatthew G. Knepley   PetscSection   coordSection;
283165a81367SMatthew G. Knepley   Vec            coordinates;
283265a81367SMatthew G. Knepley   PetscScalar   *coords;
283365a81367SMatthew G. Knepley   PetscReal     *coordsIn;
283407c565c5SJose E. Roman   PetscInt       numCells, numEdges, numVerts = 0, firstVertex = 0, v, firstEdge, coordSize, d, e;
283565a81367SMatthew G. Knepley   PetscMPIInt    rank;
283665a81367SMatthew G. Knepley 
283765a81367SMatthew G. Knepley   PetscFunctionBegin;
28389318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveBool(dm, simplex, 3);
283946139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
28409566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
28419566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, dim + 1));
28429566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
284365a81367SMatthew G. Knepley   switch (dim) {
28445c344501SMatthew G. Knepley   case 1:
28455c344501SMatthew G. Knepley     numCells = 16;
28465c344501SMatthew G. Knepley     numVerts = numCells;
28475c344501SMatthew G. Knepley 
28485c344501SMatthew G. Knepley     // Build Topology
28495c344501SMatthew G. Knepley     PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts));
28505c344501SMatthew G. Knepley     for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim));
28515c344501SMatthew G. Knepley     PetscCall(DMSetUp(dm));
28525c344501SMatthew G. Knepley     for (PetscInt c = 0; c < numCells; ++c) {
28535c344501SMatthew G. Knepley       PetscInt cone[2];
28545c344501SMatthew G. Knepley 
28555c344501SMatthew G. Knepley       cone[0] = c + numCells;
28565c344501SMatthew G. Knepley       cone[1] = (c + 1) % numVerts + numCells;
28575c344501SMatthew G. Knepley       PetscCall(DMPlexSetCone(dm, c, cone));
28585c344501SMatthew G. Knepley     }
28595c344501SMatthew G. Knepley     PetscCall(DMPlexSymmetrize(dm));
28605c344501SMatthew G. Knepley     PetscCall(DMPlexStratify(dm));
28615c344501SMatthew G. Knepley     PetscCall(PetscMalloc1(numVerts * embedDim, &coordsIn));
28625c344501SMatthew G. Knepley     for (PetscInt v = 0; v < numVerts; ++v) {
28635c344501SMatthew G. Knepley       const PetscReal rad = 2. * PETSC_PI * v / numVerts;
28645c344501SMatthew G. Knepley 
28655c344501SMatthew G. Knepley       coordsIn[v * embedDim + 0] = PetscCosReal(rad);
28665c344501SMatthew G. Knepley       coordsIn[v * embedDim + 1] = PetscSinReal(rad);
28675c344501SMatthew G. Knepley     }
28685c344501SMatthew G. Knepley     break;
286965a81367SMatthew G. Knepley   case 2:
287065a81367SMatthew G. Knepley     if (simplex) {
287151a74b61SMatthew G. Knepley       const PetscReal radius    = PetscSqrtReal(1 + PETSC_PHI * PETSC_PHI) / (1.0 + PETSC_PHI);
287251a74b61SMatthew G. Knepley       const PetscReal edgeLen   = 2.0 / (1.0 + PETSC_PHI) * (R / radius);
287365a81367SMatthew G. Knepley       const PetscInt  degree    = 5;
287451a74b61SMatthew G. Knepley       PetscReal       vertex[3] = {0.0, 1.0 / (1.0 + PETSC_PHI), PETSC_PHI / (1.0 + PETSC_PHI)};
287565a81367SMatthew G. Knepley       PetscInt        s[3]      = {1, 1, 1};
287665a81367SMatthew G. Knepley       PetscInt        cone[3];
287707c565c5SJose E. Roman       PetscInt       *graph;
287865a81367SMatthew G. Knepley 
28799371c9d4SSatish Balay       vertex[0] *= R / radius;
28809371c9d4SSatish Balay       vertex[1] *= R / radius;
28819371c9d4SSatish Balay       vertex[2] *= R / radius;
2882dd400576SPatrick Sanan       numCells    = rank == 0 ? 20 : 0;
2883dd400576SPatrick Sanan       numVerts    = rank == 0 ? 12 : 0;
288465a81367SMatthew G. Knepley       firstVertex = numCells;
288551a74b61SMatthew G. Knepley       /* Use icosahedron, which for a R-sphere has coordinates which are all cyclic permutations of
288665a81367SMatthew G. Knepley 
288765a81367SMatthew G. Knepley            (0, \pm 1/\phi+1, \pm \phi/\phi+1)
288865a81367SMatthew G. Knepley 
288965a81367SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
289051a74b61SMatthew G. Knepley          length is then given by 2/(1+\phi) = 2 * 0.38197 = 0.76393.
289165a81367SMatthew G. Knepley       */
289265a81367SMatthew G. Knepley       /* Construct vertices */
28939566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
2894dd400576SPatrick Sanan       if (rank == 0) {
289507c565c5SJose E. Roman         for (PetscInt p = 0, i = 0; p < embedDim; ++p) {
289665a81367SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
289765a81367SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
289865a81367SMatthew G. Knepley               for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[(d + p) % embedDim] * vertex[(d + p) % embedDim];
289965a81367SMatthew G. Knepley               ++i;
290065a81367SMatthew G. Knepley             }
290165a81367SMatthew G. Knepley           }
290265a81367SMatthew G. Knepley         }
290345da822fSValeria Barra       }
290465a81367SMatthew G. Knepley       /* Construct graph */
29059566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * numVerts, &graph));
290607c565c5SJose E. Roman       for (PetscInt i = 0; i < numVerts; ++i) {
290707c565c5SJose E. Roman         PetscInt k = 0;
290807c565c5SJose E. Roman         for (PetscInt j = 0; j < numVerts; ++j) {
29099371c9d4SSatish Balay           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i * embedDim], &coordsIn[j * embedDim]) - edgeLen) < PETSC_SMALL) {
29109371c9d4SSatish Balay             graph[i * numVerts + j] = 1;
29119371c9d4SSatish Balay             ++k;
29129371c9d4SSatish Balay           }
291365a81367SMatthew G. Knepley         }
291463a3b9bcSJacob Faibussowitsch         PetscCheck(k == degree, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid icosahedron, vertex %" PetscInt_FMT " degree %" PetscInt_FMT " != %" PetscInt_FMT, i, k, degree);
291565a81367SMatthew G. Knepley       }
291665a81367SMatthew G. Knepley       /* Build Topology */
29179566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts));
291807c565c5SJose E. Roman       for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim));
29199566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
292065a81367SMatthew G. Knepley       /* Cells */
292107c565c5SJose E. Roman       for (PetscInt i = 0, c = 0; i < numVerts; ++i) {
292207c565c5SJose E. Roman         for (PetscInt j = 0; j < i; ++j) {
292307c565c5SJose E. Roman           for (PetscInt k = 0; k < j; ++k) {
292465a81367SMatthew G. Knepley             if (graph[i * numVerts + j] && graph[j * numVerts + k] && graph[k * numVerts + i]) {
29259371c9d4SSatish Balay               cone[0] = firstVertex + i;
29269371c9d4SSatish Balay               cone[1] = firstVertex + j;
29279371c9d4SSatish Balay               cone[2] = firstVertex + k;
292865a81367SMatthew G. Knepley               /* Check orientation */
292965a81367SMatthew G. Knepley               {
29309371c9d4SSatish Balay                 const PetscInt epsilon[3][3][3] = {
29319371c9d4SSatish Balay                   {{0, 0, 0},  {0, 0, 1},  {0, -1, 0}},
29329371c9d4SSatish Balay                   {{0, 0, -1}, {0, 0, 0},  {1, 0, 0} },
29339371c9d4SSatish Balay                   {{0, 1, 0},  {-1, 0, 0}, {0, 0, 0} }
29349371c9d4SSatish Balay                 };
293565a81367SMatthew G. Knepley                 PetscReal normal[3];
293665a81367SMatthew G. Knepley                 PetscInt  e, f;
293765a81367SMatthew G. Knepley 
293865a81367SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) {
293965a81367SMatthew G. Knepley                   normal[d] = 0.0;
294065a81367SMatthew G. Knepley                   for (e = 0; e < embedDim; ++e) {
2941ad540459SPierre 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]);
294265a81367SMatthew G. Knepley                   }
294365a81367SMatthew G. Knepley                 }
29449371c9d4SSatish Balay                 if (DotReal(embedDim, normal, &coordsIn[i * embedDim]) < 0) {
29459371c9d4SSatish Balay                   PetscInt tmp = cone[1];
29469371c9d4SSatish Balay                   cone[1]      = cone[2];
29479371c9d4SSatish Balay                   cone[2]      = tmp;
294865a81367SMatthew G. Knepley                 }
294965a81367SMatthew G. Knepley               }
29509566063dSJacob Faibussowitsch               PetscCall(DMPlexSetCone(dm, c++, cone));
295165a81367SMatthew G. Knepley             }
295265a81367SMatthew G. Knepley           }
295365a81367SMatthew G. Knepley         }
295465a81367SMatthew G. Knepley       }
29559566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
29569566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
29579566063dSJacob Faibussowitsch       PetscCall(PetscFree(graph));
295865a81367SMatthew G. Knepley     } else {
29592829fed8SMatthew G. Knepley       /*
29602829fed8SMatthew G. Knepley         12-21--13
29612829fed8SMatthew G. Knepley          |     |
29622829fed8SMatthew G. Knepley         25  4  24
29632829fed8SMatthew G. Knepley          |     |
29642829fed8SMatthew G. Knepley   12-25--9-16--8-24--13
29652829fed8SMatthew G. Knepley    |     |     |     |
29662829fed8SMatthew G. Knepley   23  5 17  0 15  3  22
29672829fed8SMatthew G. Knepley    |     |     |     |
29682829fed8SMatthew G. Knepley   10-20--6-14--7-19--11
29692829fed8SMatthew G. Knepley          |     |
29702829fed8SMatthew G. Knepley         20  1  19
29712829fed8SMatthew G. Knepley          |     |
29722829fed8SMatthew G. Knepley         10-18--11
29732829fed8SMatthew G. Knepley          |     |
29742829fed8SMatthew G. Knepley         23  2  22
29752829fed8SMatthew G. Knepley          |     |
29762829fed8SMatthew G. Knepley         12-21--13
29772829fed8SMatthew G. Knepley        */
29782829fed8SMatthew G. Knepley       PetscInt cone[4], ornt[4];
29792829fed8SMatthew G. Knepley 
2980dd400576SPatrick Sanan       numCells    = rank == 0 ? 6 : 0;
2981dd400576SPatrick Sanan       numEdges    = rank == 0 ? 12 : 0;
2982dd400576SPatrick Sanan       numVerts    = rank == 0 ? 8 : 0;
298365a81367SMatthew G. Knepley       firstVertex = numCells;
298465a81367SMatthew G. Knepley       firstEdge   = numCells + numVerts;
29852829fed8SMatthew G. Knepley       /* Build Topology */
29869566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells + numEdges + numVerts));
298707c565c5SJose E. Roman       for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 4));
298848a46eb9SPierre Jolivet       for (e = firstEdge; e < firstEdge + numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2));
29899566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
2990dd400576SPatrick Sanan       if (rank == 0) {
29912829fed8SMatthew G. Knepley         /* Cell 0 */
29929371c9d4SSatish Balay         cone[0] = 14;
29939371c9d4SSatish Balay         cone[1] = 15;
29949371c9d4SSatish Balay         cone[2] = 16;
29959371c9d4SSatish Balay         cone[3] = 17;
29969566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
29979371c9d4SSatish Balay         ornt[0] = 0;
29989371c9d4SSatish Balay         ornt[1] = 0;
29999371c9d4SSatish Balay         ornt[2] = 0;
30009371c9d4SSatish Balay         ornt[3] = 0;
30019566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 0, ornt));
30022829fed8SMatthew G. Knepley         /* Cell 1 */
30039371c9d4SSatish Balay         cone[0] = 18;
30049371c9d4SSatish Balay         cone[1] = 19;
30059371c9d4SSatish Balay         cone[2] = 14;
30069371c9d4SSatish Balay         cone[3] = 20;
30079566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
30089371c9d4SSatish Balay         ornt[0] = 0;
30099371c9d4SSatish Balay         ornt[1] = 0;
30109371c9d4SSatish Balay         ornt[2] = -1;
30119371c9d4SSatish Balay         ornt[3] = 0;
30129566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 1, ornt));
30132829fed8SMatthew G. Knepley         /* Cell 2 */
30149371c9d4SSatish Balay         cone[0] = 21;
30159371c9d4SSatish Balay         cone[1] = 22;
30169371c9d4SSatish Balay         cone[2] = 18;
30179371c9d4SSatish Balay         cone[3] = 23;
30189566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
30199371c9d4SSatish Balay         ornt[0] = 0;
30209371c9d4SSatish Balay         ornt[1] = 0;
30219371c9d4SSatish Balay         ornt[2] = -1;
30229371c9d4SSatish Balay         ornt[3] = 0;
30239566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 2, ornt));
30242829fed8SMatthew G. Knepley         /* Cell 3 */
30259371c9d4SSatish Balay         cone[0] = 19;
30269371c9d4SSatish Balay         cone[1] = 22;
30279371c9d4SSatish Balay         cone[2] = 24;
30289371c9d4SSatish Balay         cone[3] = 15;
30299566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
30309371c9d4SSatish Balay         ornt[0] = -1;
30319371c9d4SSatish Balay         ornt[1] = -1;
30329371c9d4SSatish Balay         ornt[2] = 0;
30339371c9d4SSatish Balay         ornt[3] = -1;
30349566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 3, ornt));
30352829fed8SMatthew G. Knepley         /* Cell 4 */
30369371c9d4SSatish Balay         cone[0] = 16;
30379371c9d4SSatish Balay         cone[1] = 24;
30389371c9d4SSatish Balay         cone[2] = 21;
30399371c9d4SSatish Balay         cone[3] = 25;
30409566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
30419371c9d4SSatish Balay         ornt[0] = -1;
30429371c9d4SSatish Balay         ornt[1] = -1;
30439371c9d4SSatish Balay         ornt[2] = -1;
30449371c9d4SSatish Balay         ornt[3] = 0;
30459566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 4, ornt));
30462829fed8SMatthew G. Knepley         /* Cell 5 */
30479371c9d4SSatish Balay         cone[0] = 20;
30489371c9d4SSatish Balay         cone[1] = 17;
30499371c9d4SSatish Balay         cone[2] = 25;
30509371c9d4SSatish Balay         cone[3] = 23;
30519566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 5, cone));
30529371c9d4SSatish Balay         ornt[0] = -1;
30539371c9d4SSatish Balay         ornt[1] = -1;
30549371c9d4SSatish Balay         ornt[2] = -1;
30559371c9d4SSatish Balay         ornt[3] = -1;
30569566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 5, ornt));
30572829fed8SMatthew G. Knepley         /* Edges */
30589371c9d4SSatish Balay         cone[0] = 6;
30599371c9d4SSatish Balay         cone[1] = 7;
30609566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 14, cone));
30619371c9d4SSatish Balay         cone[0] = 7;
30629371c9d4SSatish Balay         cone[1] = 8;
30639566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 15, cone));
30649371c9d4SSatish Balay         cone[0] = 8;
30659371c9d4SSatish Balay         cone[1] = 9;
30669566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 16, cone));
30679371c9d4SSatish Balay         cone[0] = 9;
30689371c9d4SSatish Balay         cone[1] = 6;
30699566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 17, cone));
30709371c9d4SSatish Balay         cone[0] = 10;
30719371c9d4SSatish Balay         cone[1] = 11;
30729566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 18, cone));
30739371c9d4SSatish Balay         cone[0] = 11;
30749371c9d4SSatish Balay         cone[1] = 7;
30759566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 19, cone));
30769371c9d4SSatish Balay         cone[0] = 6;
30779371c9d4SSatish Balay         cone[1] = 10;
30789566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 20, cone));
30799371c9d4SSatish Balay         cone[0] = 12;
30809371c9d4SSatish Balay         cone[1] = 13;
30819566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 21, cone));
30829371c9d4SSatish Balay         cone[0] = 13;
30839371c9d4SSatish Balay         cone[1] = 11;
30849566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 22, cone));
30859371c9d4SSatish Balay         cone[0] = 10;
30869371c9d4SSatish Balay         cone[1] = 12;
30879566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 23, cone));
30889371c9d4SSatish Balay         cone[0] = 13;
30899371c9d4SSatish Balay         cone[1] = 8;
30909566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 24, cone));
30919371c9d4SSatish Balay         cone[0] = 12;
30929371c9d4SSatish Balay         cone[1] = 9;
30939566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 25, cone));
309445da822fSValeria Barra       }
30959566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
30969566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
30972829fed8SMatthew G. Knepley       /* Build coordinates */
30989566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
3099dd400576SPatrick Sanan       if (rank == 0) {
31009371c9d4SSatish Balay         coordsIn[0 * embedDim + 0] = -R;
31019371c9d4SSatish Balay         coordsIn[0 * embedDim + 1] = R;
31029371c9d4SSatish Balay         coordsIn[0 * embedDim + 2] = -R;
31039371c9d4SSatish Balay         coordsIn[1 * embedDim + 0] = R;
31049371c9d4SSatish Balay         coordsIn[1 * embedDim + 1] = R;
31059371c9d4SSatish Balay         coordsIn[1 * embedDim + 2] = -R;
31069371c9d4SSatish Balay         coordsIn[2 * embedDim + 0] = R;
31079371c9d4SSatish Balay         coordsIn[2 * embedDim + 1] = -R;
31089371c9d4SSatish Balay         coordsIn[2 * embedDim + 2] = -R;
31099371c9d4SSatish Balay         coordsIn[3 * embedDim + 0] = -R;
31109371c9d4SSatish Balay         coordsIn[3 * embedDim + 1] = -R;
31119371c9d4SSatish Balay         coordsIn[3 * embedDim + 2] = -R;
31129371c9d4SSatish Balay         coordsIn[4 * embedDim + 0] = -R;
31139371c9d4SSatish Balay         coordsIn[4 * embedDim + 1] = R;
31149371c9d4SSatish Balay         coordsIn[4 * embedDim + 2] = R;
31159371c9d4SSatish Balay         coordsIn[5 * embedDim + 0] = R;
31169371c9d4SSatish Balay         coordsIn[5 * embedDim + 1] = R;
31179371c9d4SSatish Balay         coordsIn[5 * embedDim + 2] = R;
31189371c9d4SSatish Balay         coordsIn[6 * embedDim + 0] = -R;
31199371c9d4SSatish Balay         coordsIn[6 * embedDim + 1] = -R;
31209371c9d4SSatish Balay         coordsIn[6 * embedDim + 2] = R;
31219371c9d4SSatish Balay         coordsIn[7 * embedDim + 0] = R;
31229371c9d4SSatish Balay         coordsIn[7 * embedDim + 1] = -R;
31239371c9d4SSatish Balay         coordsIn[7 * embedDim + 2] = R;
312465a81367SMatthew G. Knepley       }
312545da822fSValeria Barra     }
312665a81367SMatthew G. Knepley     break;
312765a81367SMatthew G. Knepley   case 3:
3128116ded15SMatthew G. Knepley     if (simplex) {
3129116ded15SMatthew G. Knepley       const PetscReal edgeLen         = 1.0 / PETSC_PHI;
313051a74b61SMatthew G. Knepley       PetscReal       vertexA[4]      = {0.5, 0.5, 0.5, 0.5};
313151a74b61SMatthew G. Knepley       PetscReal       vertexB[4]      = {1.0, 0.0, 0.0, 0.0};
313251a74b61SMatthew G. Knepley       PetscReal       vertexC[4]      = {0.5, 0.5 * PETSC_PHI, 0.5 / PETSC_PHI, 0.0};
3133116ded15SMatthew G. Knepley       const PetscInt  degree          = 12;
3134116ded15SMatthew G. Knepley       PetscInt        s[4]            = {1, 1, 1};
31359371c9d4SSatish Balay       PetscInt        evenPerm[12][4] = {
31369371c9d4SSatish Balay         {0, 1, 2, 3},
31379371c9d4SSatish Balay         {0, 2, 3, 1},
31389371c9d4SSatish Balay         {0, 3, 1, 2},
31399371c9d4SSatish Balay         {1, 0, 3, 2},
31409371c9d4SSatish Balay         {1, 2, 0, 3},
31419371c9d4SSatish Balay         {1, 3, 2, 0},
31429371c9d4SSatish Balay         {2, 0, 1, 3},
31439371c9d4SSatish Balay         {2, 1, 3, 0},
31449371c9d4SSatish Balay         {2, 3, 0, 1},
31459371c9d4SSatish Balay         {3, 0, 2, 1},
31469371c9d4SSatish Balay         {3, 1, 0, 2},
31479371c9d4SSatish Balay         {3, 2, 1, 0}
31489371c9d4SSatish Balay       };
3149116ded15SMatthew G. Knepley       PetscInt  cone[4];
3150116ded15SMatthew G. Knepley       PetscInt *graph, p, i, j, k, l;
3151116ded15SMatthew G. Knepley 
31529371c9d4SSatish Balay       vertexA[0] *= R;
31539371c9d4SSatish Balay       vertexA[1] *= R;
31549371c9d4SSatish Balay       vertexA[2] *= R;
31559371c9d4SSatish Balay       vertexA[3] *= R;
31569371c9d4SSatish Balay       vertexB[0] *= R;
31579371c9d4SSatish Balay       vertexB[1] *= R;
31589371c9d4SSatish Balay       vertexB[2] *= R;
31599371c9d4SSatish Balay       vertexB[3] *= R;
31609371c9d4SSatish Balay       vertexC[0] *= R;
31619371c9d4SSatish Balay       vertexC[1] *= R;
31629371c9d4SSatish Balay       vertexC[2] *= R;
31639371c9d4SSatish Balay       vertexC[3] *= R;
3164dd400576SPatrick Sanan       numCells    = rank == 0 ? 600 : 0;
3165dd400576SPatrick Sanan       numVerts    = rank == 0 ? 120 : 0;
3166116ded15SMatthew G. Knepley       firstVertex = numCells;
3167116ded15SMatthew G. Knepley       /* Use the 600-cell, which for a unit sphere has coordinates which are
3168116ded15SMatthew G. Knepley 
3169116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm 1,    \pm 1, \pm 1)                          16
3170116ded15SMatthew G. Knepley                (\pm 1,    0,       0,      0)  all cyclic permutations   8
3171116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm phi, \pm 1/phi, 0)  all even permutations    96
3172116ded15SMatthew G. Knepley 
3173116ded15SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
31746333ae4fSvaleriabarra          length is then given by 1/\phi = 0.61803.
3175116ded15SMatthew G. Knepley 
3176116ded15SMatthew G. Knepley          http://buzzard.pugetsound.edu/sage-practice/ch03s03.html
3177116ded15SMatthew G. Knepley          http://mathworld.wolfram.com/600-Cell.html
3178116ded15SMatthew G. Knepley       */
3179116ded15SMatthew G. Knepley       /* Construct vertices */
31809566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
3181116ded15SMatthew G. Knepley       i = 0;
3182dd400576SPatrick Sanan       if (rank == 0) {
3183116ded15SMatthew G. Knepley         for (s[0] = -1; s[0] < 2; s[0] += 2) {
3184116ded15SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
3185116ded15SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
3186116ded15SMatthew G. Knepley               for (s[3] = -1; s[3] < 2; s[3] += 2) {
3187116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[d] * vertexA[d];
3188116ded15SMatthew G. Knepley                 ++i;
3189116ded15SMatthew G. Knepley               }
3190116ded15SMatthew G. Knepley             }
3191116ded15SMatthew G. Knepley           }
3192116ded15SMatthew G. Knepley         }
3193116ded15SMatthew G. Knepley         for (p = 0; p < embedDim; ++p) {
3194116ded15SMatthew G. Knepley           s[1] = s[2] = s[3] = 1;
3195116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
3196116ded15SMatthew G. Knepley             for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[(d + p) % embedDim] * vertexB[(d + p) % embedDim];
3197116ded15SMatthew G. Knepley             ++i;
3198116ded15SMatthew G. Knepley           }
3199116ded15SMatthew G. Knepley         }
3200116ded15SMatthew G. Knepley         for (p = 0; p < 12; ++p) {
3201116ded15SMatthew G. Knepley           s[3] = 1;
3202116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
3203116ded15SMatthew G. Knepley             for (s[1] = -1; s[1] < 2; s[1] += 2) {
3204116ded15SMatthew G. Knepley               for (s[2] = -1; s[2] < 2; s[2] += 2) {
3205116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[evenPerm[p][d]] * vertexC[evenPerm[p][d]];
3206116ded15SMatthew G. Knepley                 ++i;
3207116ded15SMatthew G. Knepley               }
3208116ded15SMatthew G. Knepley             }
3209116ded15SMatthew G. Knepley           }
3210116ded15SMatthew G. Knepley         }
321145da822fSValeria Barra       }
321263a3b9bcSJacob Faibussowitsch       PetscCheck(i == numVerts, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertices %" PetscInt_FMT " != %" PetscInt_FMT, i, numVerts);
3213116ded15SMatthew G. Knepley       /* Construct graph */
32149566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * numVerts, &graph));
3215116ded15SMatthew G. Knepley       for (i = 0; i < numVerts; ++i) {
3216116ded15SMatthew G. Knepley         for (j = 0, k = 0; j < numVerts; ++j) {
32179371c9d4SSatish Balay           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i * embedDim], &coordsIn[j * embedDim]) - edgeLen) < PETSC_SMALL) {
32189371c9d4SSatish Balay             graph[i * numVerts + j] = 1;
32199371c9d4SSatish Balay             ++k;
32209371c9d4SSatish Balay           }
3221116ded15SMatthew G. Knepley         }
322263a3b9bcSJacob Faibussowitsch         PetscCheck(k == degree, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertex %" PetscInt_FMT " degree %" PetscInt_FMT " != %" PetscInt_FMT, i, k, degree);
3223116ded15SMatthew G. Knepley       }
3224116ded15SMatthew G. Knepley       /* Build Topology */
32259566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts));
322607c565c5SJose E. Roman       for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim));
32279566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
3228116ded15SMatthew G. Knepley       /* Cells */
3229dd400576SPatrick Sanan       if (rank == 0) {
323007c565c5SJose E. Roman         for (PetscInt i = 0, c = 0; i < numVerts; ++i) {
3231116ded15SMatthew G. Knepley           for (j = 0; j < i; ++j) {
3232116ded15SMatthew G. Knepley             for (k = 0; k < j; ++k) {
3233116ded15SMatthew G. Knepley               for (l = 0; l < k; ++l) {
32349371c9d4SSatish 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]) {
32359371c9d4SSatish Balay                   cone[0] = firstVertex + i;
32369371c9d4SSatish Balay                   cone[1] = firstVertex + j;
32379371c9d4SSatish Balay                   cone[2] = firstVertex + k;
32389371c9d4SSatish Balay                   cone[3] = firstVertex + l;
3239116ded15SMatthew G. Knepley                   /* Check orientation: https://ef.gy/linear-algebra:normal-vectors-in-higher-dimensional-spaces */
3240116ded15SMatthew G. Knepley                   {
32419371c9d4SSatish Balay                     const PetscInt epsilon[4][4][4][4] = {
32429371c9d4SSatish 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}}},
3243116ded15SMatthew G. Knepley 
32449371c9d4SSatish 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}}},
3245116ded15SMatthew G. Knepley 
32469371c9d4SSatish 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}}},
3247116ded15SMatthew G. Knepley 
32489371c9d4SSatish 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}} }
32499371c9d4SSatish Balay                     };
3250116ded15SMatthew G. Knepley                     PetscReal normal[4];
3251116ded15SMatthew G. Knepley                     PetscInt  e, f, g;
3252116ded15SMatthew G. Knepley 
3253116ded15SMatthew G. Knepley                     for (d = 0; d < embedDim; ++d) {
3254116ded15SMatthew G. Knepley                       normal[d] = 0.0;
3255116ded15SMatthew G. Knepley                       for (e = 0; e < embedDim; ++e) {
3256116ded15SMatthew G. Knepley                         for (f = 0; f < embedDim; ++f) {
3257116ded15SMatthew G. Knepley                           for (g = 0; g < embedDim; ++g) {
3258116ded15SMatthew 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]);
3259116ded15SMatthew G. Knepley                           }
3260116ded15SMatthew G. Knepley                         }
3261116ded15SMatthew G. Knepley                       }
3262116ded15SMatthew G. Knepley                     }
32639371c9d4SSatish Balay                     if (DotReal(embedDim, normal, &coordsIn[i * embedDim]) < 0) {
32649371c9d4SSatish Balay                       PetscInt tmp = cone[1];
32659371c9d4SSatish Balay                       cone[1]      = cone[2];
32669371c9d4SSatish Balay                       cone[2]      = tmp;
32679371c9d4SSatish Balay                     }
3268116ded15SMatthew G. Knepley                   }
32699566063dSJacob Faibussowitsch                   PetscCall(DMPlexSetCone(dm, c++, cone));
3270116ded15SMatthew G. Knepley                 }
3271116ded15SMatthew G. Knepley               }
3272116ded15SMatthew G. Knepley             }
3273116ded15SMatthew G. Knepley           }
3274116ded15SMatthew G. Knepley         }
327545da822fSValeria Barra       }
32769566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
32779566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
32789566063dSJacob Faibussowitsch       PetscCall(PetscFree(graph));
3279116ded15SMatthew G. Knepley     }
3280f4d061e9SPierre Jolivet     break;
3281d71ae5a4SJacob Faibussowitsch   default:
3282d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension for sphere: %" PetscInt_FMT, dim);
328365a81367SMatthew G. Knepley   }
328465a81367SMatthew G. Knepley   /* Create coordinates */
32859566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
32869566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
32879566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, embedDim));
32889566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numVerts));
32892829fed8SMatthew G. Knepley   for (v = firstVertex; v < firstVertex + numVerts; ++v) {
32909566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, embedDim));
32919566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, embedDim));
32922829fed8SMatthew G. Knepley   }
32939566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
32949566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
32959566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
32969566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, embedDim));
32979566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
32989566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
32999566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
33009566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
33019371c9d4SSatish Balay   for (v = 0; v < numVerts; ++v)
3302ad540459SPierre Jolivet     for (d = 0; d < embedDim; ++d) coords[v * embedDim + d] = coordsIn[v * embedDim + d];
33039566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
33049566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
33059566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
33069566063dSJacob Faibussowitsch   PetscCall(PetscFree(coordsIn));
330751a74b61SMatthew G. Knepley   {
330851a74b61SMatthew G. Knepley     DM          cdm;
330951a74b61SMatthew G. Knepley     PetscDS     cds;
33109318fe57SMatthew G. Knepley     PetscScalar c = R;
331151a74b61SMatthew G. Knepley 
3312e44f6aebSMatthew G. Knepley     PetscCall(DMPlexCreateCoordinateSpace(dm, 1, PETSC_TRUE, snapToSphere));
33139566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
33149566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
33159566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, 1, &c));
331651a74b61SMatthew G. Knepley   }
331746139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
33189318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
33199566063dSJacob Faibussowitsch   if (simplex) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
33203ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
33219318fe57SMatthew G. Knepley }
33229318fe57SMatthew G. Knepley 
3323b7f5c055SJed Brown typedef void (*TPSEvaluateFunc)(const PetscReal[], PetscReal *, PetscReal[], PetscReal (*)[3]);
3324b7f5c055SJed Brown 
3325b7f5c055SJed Brown /*
3326b7f5c055SJed Brown  The Schwarz P implicit surface is
3327b7f5c055SJed Brown 
3328b7f5c055SJed Brown      f(x) = cos(x0) + cos(x1) + cos(x2) = 0
3329b7f5c055SJed Brown */
3330d71ae5a4SJacob Faibussowitsch static void TPSEvaluate_SchwarzP(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3])
3331d71ae5a4SJacob Faibussowitsch {
3332b7f5c055SJed Brown   PetscReal c[3] = {PetscCosReal(y[0] * PETSC_PI), PetscCosReal(y[1] * PETSC_PI), PetscCosReal(y[2] * PETSC_PI)};
3333b7f5c055SJed Brown   PetscReal g[3] = {-PetscSinReal(y[0] * PETSC_PI), -PetscSinReal(y[1] * PETSC_PI), -PetscSinReal(y[2] * PETSC_PI)};
3334b7f5c055SJed Brown   f[0]           = c[0] + c[1] + c[2];
3335b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) {
3336b7f5c055SJed Brown     grad[i] = PETSC_PI * g[i];
3337ad540459SPierre Jolivet     for (PetscInt j = 0; j < 3; j++) hess[i][j] = (i == j) ? -PetscSqr(PETSC_PI) * c[i] : 0.;
3338b7f5c055SJed Brown   }
3339b7f5c055SJed Brown }
3340b7f5c055SJed Brown 
33414663dae6SJed Brown // u[] is a tentative normal on input. Replace with the implicit function gradient in the same direction
3342d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSExtrudeNormalFunc_SchwarzP(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt r, PetscScalar u[], void *ctx)
3343d71ae5a4SJacob Faibussowitsch {
3344ad540459SPierre Jolivet   for (PetscInt i = 0; i < 3; i++) u[i] = -PETSC_PI * PetscSinReal(x[i] * PETSC_PI);
33453ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
33464663dae6SJed Brown }
33474663dae6SJed Brown 
3348b7f5c055SJed Brown /*
3349b7f5c055SJed Brown  The Gyroid implicit surface is
3350b7f5c055SJed Brown 
3351b7f5c055SJed 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)
3352b7f5c055SJed Brown 
3353b7f5c055SJed Brown */
3354d71ae5a4SJacob Faibussowitsch static void TPSEvaluate_Gyroid(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3])
3355d71ae5a4SJacob Faibussowitsch {
3356b7f5c055SJed Brown   PetscReal s[3] = {PetscSinReal(PETSC_PI * y[0]), PetscSinReal(PETSC_PI * (y[1] + .5)), PetscSinReal(PETSC_PI * (y[2] + .25))};
3357b7f5c055SJed Brown   PetscReal c[3] = {PetscCosReal(PETSC_PI * y[0]), PetscCosReal(PETSC_PI * (y[1] + .5)), PetscCosReal(PETSC_PI * (y[2] + .25))};
3358b7f5c055SJed Brown   f[0]           = s[0] * c[1] + s[1] * c[2] + s[2] * c[0];
3359b7f5c055SJed Brown   grad[0]        = PETSC_PI * (c[0] * c[1] - s[2] * s[0]);
3360b7f5c055SJed Brown   grad[1]        = PETSC_PI * (c[1] * c[2] - s[0] * s[1]);
3361b7f5c055SJed Brown   grad[2]        = PETSC_PI * (c[2] * c[0] - s[1] * s[2]);
3362b7f5c055SJed Brown   hess[0][0]     = -PetscSqr(PETSC_PI) * (s[0] * c[1] + s[2] * c[0]);
3363b7f5c055SJed Brown   hess[0][1]     = -PetscSqr(PETSC_PI) * (c[0] * s[1]);
3364b7f5c055SJed Brown   hess[0][2]     = -PetscSqr(PETSC_PI) * (c[2] * s[0]);
3365b7f5c055SJed Brown   hess[1][0]     = -PetscSqr(PETSC_PI) * (s[1] * c[2] + s[0] * c[1]);
3366b7f5c055SJed Brown   hess[1][1]     = -PetscSqr(PETSC_PI) * (c[1] * s[2]);
3367b7f5c055SJed Brown   hess[2][2]     = -PetscSqr(PETSC_PI) * (c[0] * s[1]);
3368b7f5c055SJed Brown   hess[2][0]     = -PetscSqr(PETSC_PI) * (s[2] * c[0] + s[1] * c[2]);
3369b7f5c055SJed Brown   hess[2][1]     = -PetscSqr(PETSC_PI) * (c[2] * s[0]);
3370b7f5c055SJed Brown   hess[2][2]     = -PetscSqr(PETSC_PI) * (c[1] * s[2]);
3371b7f5c055SJed Brown }
3372b7f5c055SJed Brown 
33734663dae6SJed Brown // u[] is a tentative normal on input. Replace with the implicit function gradient in the same direction
3374d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSExtrudeNormalFunc_Gyroid(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt r, PetscScalar u[], void *ctx)
3375d71ae5a4SJacob Faibussowitsch {
33764663dae6SJed Brown   PetscReal s[3] = {PetscSinReal(PETSC_PI * x[0]), PetscSinReal(PETSC_PI * (x[1] + .5)), PetscSinReal(PETSC_PI * (x[2] + .25))};
33774663dae6SJed Brown   PetscReal c[3] = {PetscCosReal(PETSC_PI * x[0]), PetscCosReal(PETSC_PI * (x[1] + .5)), PetscCosReal(PETSC_PI * (x[2] + .25))};
33784663dae6SJed Brown   u[0]           = PETSC_PI * (c[0] * c[1] - s[2] * s[0]);
33794663dae6SJed Brown   u[1]           = PETSC_PI * (c[1] * c[2] - s[0] * s[1]);
33804663dae6SJed Brown   u[2]           = PETSC_PI * (c[2] * c[0] - s[1] * s[2]);
33813ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
33824663dae6SJed Brown }
33834663dae6SJed Brown 
3384b7f5c055SJed Brown /*
3385b7f5c055SJed Brown    We wish to solve
3386b7f5c055SJed Brown 
3387b7f5c055SJed Brown          min_y || y - x ||^2  subject to f(y) = 0
3388b7f5c055SJed Brown 
3389b7f5c055SJed Brown    Let g(y) = grad(f).  The minimization problem is equivalent to asking to satisfy
3390b7f5c055SJed 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
3391b7f5c055SJed Brown    tangent space and ask for both components in the tangent space to be zero.
3392b7f5c055SJed Brown 
3393b7f5c055SJed Brown    Take g to be a column vector and compute the "full QR" factorization Q R = g,
3394b7f5c055SJed Brown    where Q = I - 2 n n^T is a symmetric orthogonal matrix.
3395b7f5c055SJed Brown    The first column of Q is parallel to g so the remaining two columns span the null space.
3396b7f5c055SJed Brown    Let Qn = Q[:,1:] be those remaining columns.  Then Qn Qn^T is an orthogonal projector into the tangent space.
3397da81f932SPierre Jolivet    Since Q is symmetric, this is equivalent to multiplying by Q and taking the last two entries.
3398b7f5c055SJed Brown    In total, we have a system of 3 equations in 3 unknowns:
3399b7f5c055SJed Brown 
3400b7f5c055SJed Brown      f(y) = 0                       1 equation
3401b7f5c055SJed Brown      Qn^T (y - x) = 0               2 equations
3402b7f5c055SJed Brown 
3403b7f5c055SJed Brown    Here, we compute the residual and Jacobian of this system.
3404b7f5c055SJed Brown */
3405d71ae5a4SJacob Faibussowitsch static void TPSNearestPointResJac(TPSEvaluateFunc feval, const PetscScalar x[], const PetscScalar y[], PetscScalar res[], PetscScalar J[])
3406d71ae5a4SJacob Faibussowitsch {
3407b7f5c055SJed Brown   PetscReal yreal[3] = {PetscRealPart(y[0]), PetscRealPart(y[1]), PetscRealPart(y[2])};
3408b7f5c055SJed Brown   PetscReal d[3]     = {PetscRealPart(y[0] - x[0]), PetscRealPart(y[1] - x[1]), PetscRealPart(y[2] - x[2])};
34092f0490c0SSatish Balay   PetscReal f, grad[3], n[3], norm, norm_y[3], nd, nd_y[3], sign;
34109371c9d4SSatish Balay   PetscReal n_y[3][3] = {
34119371c9d4SSatish Balay     {0, 0, 0},
34129371c9d4SSatish Balay     {0, 0, 0},
34139371c9d4SSatish Balay     {0, 0, 0}
34149371c9d4SSatish Balay   };
3415b7f5c055SJed Brown 
3416b7f5c055SJed Brown   feval(yreal, &f, grad, n_y);
3417b7f5c055SJed Brown 
3418b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) n[i] = grad[i];
3419b7f5c055SJed Brown   norm = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2]));
3420ad540459SPierre Jolivet   for (PetscInt i = 0; i < 3; i++) norm_y[i] = 1. / norm * n[i] * n_y[i][i];
3421b7f5c055SJed Brown 
3422b7f5c055SJed Brown   // Define the Householder reflector
3423b7f5c055SJed Brown   sign = n[0] >= 0 ? 1. : -1.;
3424b7f5c055SJed Brown   n[0] += norm * sign;
3425b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) n_y[0][i] += norm_y[i] * sign;
3426b7f5c055SJed Brown 
3427b7f5c055SJed Brown   norm      = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2]));
3428b7f5c055SJed Brown   norm_y[0] = 1. / norm * (n[0] * n_y[0][0]);
3429b7f5c055SJed Brown   norm_y[1] = 1. / norm * (n[0] * n_y[0][1] + n[1] * n_y[1][1]);
3430b7f5c055SJed Brown   norm_y[2] = 1. / norm * (n[0] * n_y[0][2] + n[2] * n_y[2][2]);
3431b7f5c055SJed Brown 
3432b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) {
3433b7f5c055SJed Brown     n[i] /= norm;
3434b7f5c055SJed Brown     for (PetscInt j = 0; j < 3; j++) {
3435b7f5c055SJed Brown       // note that n[i] is n_old[i]/norm when executing the code below
3436b7f5c055SJed Brown       n_y[i][j] = n_y[i][j] / norm - n[i] / norm * norm_y[j];
3437b7f5c055SJed Brown     }
3438b7f5c055SJed Brown   }
3439b7f5c055SJed Brown 
3440b7f5c055SJed Brown   nd = n[0] * d[0] + n[1] * d[1] + n[2] * d[2];
3441b7f5c055SJed 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];
3442b7f5c055SJed Brown 
3443b7f5c055SJed Brown   res[0] = f;
3444b7f5c055SJed Brown   res[1] = d[1] - 2 * n[1] * nd;
3445b7f5c055SJed Brown   res[2] = d[2] - 2 * n[2] * nd;
3446b7f5c055SJed Brown   // J[j][i] is J_{ij} (column major)
3447b7f5c055SJed Brown   for (PetscInt j = 0; j < 3; j++) {
3448b7f5c055SJed Brown     J[0 + j * 3] = grad[j];
3449b7f5c055SJed Brown     J[1 + j * 3] = (j == 1) * 1. - 2 * (n_y[1][j] * nd + n[1] * nd_y[j]);
3450b7f5c055SJed Brown     J[2 + j * 3] = (j == 2) * 1. - 2 * (n_y[2][j] * nd + n[2] * nd_y[j]);
3451b7f5c055SJed Brown   }
3452b7f5c055SJed Brown }
3453b7f5c055SJed Brown 
3454b7f5c055SJed Brown /*
3455b7f5c055SJed Brown    Project x to the nearest point on the implicit surface using Newton's method.
3456b7f5c055SJed Brown */
3457d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSNearestPoint(TPSEvaluateFunc feval, PetscScalar x[])
3458d71ae5a4SJacob Faibussowitsch {
3459b7f5c055SJed Brown   PetscScalar y[3] = {x[0], x[1], x[2]}; // Initial guess
3460b7f5c055SJed Brown 
3461b7f5c055SJed Brown   PetscFunctionBegin;
3462b7f5c055SJed Brown   for (PetscInt iter = 0; iter < 10; iter++) {
3463b7f5c055SJed Brown     PetscScalar res[3], J[9];
3464b7f5c055SJed Brown     PetscReal   resnorm;
3465b7f5c055SJed Brown     TPSNearestPointResJac(feval, x, y, res, J);
3466b7f5c055SJed Brown     resnorm = PetscSqrtReal(PetscSqr(PetscRealPart(res[0])) + PetscSqr(PetscRealPart(res[1])) + PetscSqr(PetscRealPart(res[2])));
3467b7f5c055SJed Brown     if (0) { // Turn on this monitor if you need to confirm quadratic convergence
346863a3b9bcSJacob 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])));
3469b7f5c055SJed Brown     }
3470b7f5c055SJed Brown     if (resnorm < PETSC_SMALL) break;
3471b7f5c055SJed Brown 
3472b7f5c055SJed Brown     // Take the Newton step
34739566063dSJacob Faibussowitsch     PetscCall(PetscKernel_A_gets_inverse_A_3(J, 0., PETSC_FALSE, NULL));
3474b7f5c055SJed Brown     PetscKernel_v_gets_v_minus_A_times_w_3(y, J, res);
3475b7f5c055SJed Brown   }
3476b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) x[i] = y[i];
34773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3478b7f5c055SJed Brown }
3479b7f5c055SJed Brown 
3480b7f5c055SJed Brown const char *const DMPlexTPSTypes[] = {"SCHWARZ_P", "GYROID", "DMPlexTPSType", "DMPLEX_TPS_", NULL};
3481b7f5c055SJed Brown 
3482d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateTPSMesh_Internal(DM dm, DMPlexTPSType tpstype, const PetscInt extent[], const DMBoundaryType periodic[], PetscBool tps_distribute, PetscInt refinements, PetscInt layers, PetscReal thickness)
3483d71ae5a4SJacob Faibussowitsch {
3484b7f5c055SJed Brown   PetscMPIInt rank;
3485b7f5c055SJed Brown   PetscInt    topoDim = 2, spaceDim = 3, numFaces = 0, numVertices = 0, numEdges = 0;
3486b7f5c055SJed Brown   PetscInt(*edges)[2] = NULL, *edgeSets = NULL;
3487b7f5c055SJed Brown   PetscInt           *cells_flat = NULL;
3488b7f5c055SJed Brown   PetscReal          *vtxCoords  = NULL;
3489b7f5c055SJed Brown   TPSEvaluateFunc     evalFunc   = NULL;
34908434afd1SBarry Smith   PetscSimplePointFn *normalFunc = NULL;
3491b7f5c055SJed Brown   DMLabel             label;
3492b7f5c055SJed Brown 
3493b7f5c055SJed Brown   PetscFunctionBegin;
349446139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
34959566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
349663a3b9bcSJacob 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);
3497b7f5c055SJed Brown   switch (tpstype) {
3498b7f5c055SJed Brown   case DMPLEX_TPS_SCHWARZ_P:
3499b7f5c055SJed 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");
3500c5853193SPierre Jolivet     if (rank == 0) {
3501b7f5c055SJed Brown       PetscInt(*cells)[6][4][4] = NULL; // [junction, junction-face, cell, conn]
3502b7f5c055SJed Brown       PetscInt  Njunctions = 0, Ncuts = 0, Npipes[3], vcount;
3503b7f5c055SJed Brown       PetscReal L = 1;
3504b7f5c055SJed Brown 
3505b7f5c055SJed Brown       Npipes[0]   = (extent[0] + 1) * extent[1] * extent[2];
3506b7f5c055SJed Brown       Npipes[1]   = extent[0] * (extent[1] + 1) * extent[2];
3507b7f5c055SJed Brown       Npipes[2]   = extent[0] * extent[1] * (extent[2] + 1);
3508b7f5c055SJed Brown       Njunctions  = extent[0] * extent[1] * extent[2];
3509b7f5c055SJed Brown       Ncuts       = 2 * (extent[0] * extent[1] + extent[1] * extent[2] + extent[2] * extent[0]);
3510b7f5c055SJed Brown       numVertices = 4 * (Npipes[0] + Npipes[1] + Npipes[2]) + 8 * Njunctions;
35119566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(3 * numVertices, &vtxCoords));
35129566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Njunctions, &cells));
35139566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Ncuts * 4, &edges));
35149566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Ncuts * 4, &edgeSets));
3515b7f5c055SJed Brown       // x-normal pipes
3516b7f5c055SJed Brown       vcount = 0;
3517b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0] + 1; i++) {
3518b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3519b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2]; k++) {
3520b7f5c055SJed Brown             for (PetscInt l = 0; l < 4; l++) {
3521b7f5c055SJed Brown               vtxCoords[vcount++] = (2 * i - 1) * L;
3522b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * j * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3523b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * k * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3524b7f5c055SJed Brown             }
3525b7f5c055SJed Brown           }
3526b7f5c055SJed Brown         }
3527b7f5c055SJed Brown       }
3528b7f5c055SJed Brown       // y-normal pipes
3529b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0]; i++) {
3530b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1] + 1; j++) {
3531b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2]; k++) {
3532b7f5c055SJed Brown             for (PetscInt l = 0; l < 4; l++) {
3533b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * i * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3534b7f5c055SJed Brown               vtxCoords[vcount++] = (2 * j - 1) * L;
3535b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * k * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3536b7f5c055SJed Brown             }
3537b7f5c055SJed Brown           }
3538b7f5c055SJed Brown         }
3539b7f5c055SJed Brown       }
3540b7f5c055SJed Brown       // z-normal pipes
3541b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0]; i++) {
3542b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3543b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2] + 1; k++) {
3544b7f5c055SJed Brown             for (PetscInt l = 0; l < 4; l++) {
3545b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * i * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3546b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * j * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3547b7f5c055SJed Brown               vtxCoords[vcount++] = (2 * k - 1) * L;
3548b7f5c055SJed Brown             }
3549b7f5c055SJed Brown           }
3550b7f5c055SJed Brown         }
3551b7f5c055SJed Brown       }
3552b7f5c055SJed Brown       // junctions
3553b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0]; i++) {
3554b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3555b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2]; k++) {
3556b7f5c055SJed Brown             const PetscInt J = (i * extent[1] + j) * extent[2] + k, Jvoff = (Npipes[0] + Npipes[1] + Npipes[2]) * 4 + J * 8;
3557b7f5c055SJed Brown             PetscCheck(vcount / 3 == Jvoff, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unexpected vertex count");
3558b7f5c055SJed Brown             for (PetscInt ii = 0; ii < 2; ii++) {
3559b7f5c055SJed Brown               for (PetscInt jj = 0; jj < 2; jj++) {
3560b7f5c055SJed Brown                 for (PetscInt kk = 0; kk < 2; kk++) {
3561b7f5c055SJed Brown                   double Ls           = (1 - sqrt(2) / 4) * L;
3562b7f5c055SJed Brown                   vtxCoords[vcount++] = 2 * i * L + (2 * ii - 1) * Ls;
3563b7f5c055SJed Brown                   vtxCoords[vcount++] = 2 * j * L + (2 * jj - 1) * Ls;
3564b7f5c055SJed Brown                   vtxCoords[vcount++] = 2 * k * L + (2 * kk - 1) * Ls;
3565b7f5c055SJed Brown                 }
3566b7f5c055SJed Brown               }
3567b7f5c055SJed Brown             }
3568b7f5c055SJed Brown             const PetscInt jfaces[3][2][4] = {
3569b7f5c055SJed Brown               {{3, 1, 0, 2}, {7, 5, 4, 6}}, // x-aligned
3570b7f5c055SJed Brown               {{5, 4, 0, 1}, {7, 6, 2, 3}}, // y-aligned
3571b7f5c055SJed Brown               {{6, 2, 0, 4}, {7, 3, 1, 5}}  // z-aligned
3572b7f5c055SJed Brown             };
3573b7f5c055SJed Brown             const PetscInt pipe_lo[3] = {// vertex numbers of pipes
35749371c9d4SSatish 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};
3575b7f5c055SJed Brown             const PetscInt pipe_hi[3] = {// vertex numbers of pipes
35769371c9d4SSatish 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};
3577b7f5c055SJed Brown             for (PetscInt dir = 0; dir < 3; dir++) { // x,y,z
3578b7f5c055SJed Brown               const PetscInt ijk[3] = {i, j, k};
3579b7f5c055SJed Brown               for (PetscInt l = 0; l < 4; l++) { // rotations
3580b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][0] = pipe_lo[dir] + l;
3581b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][1] = Jvoff + jfaces[dir][0][l];
3582b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][2] = Jvoff + jfaces[dir][0][(l - 1 + 4) % 4];
3583b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][3] = pipe_lo[dir] + (l - 1 + 4) % 4;
3584b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][0] = Jvoff + jfaces[dir][1][l];
3585b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][1] = pipe_hi[dir] + l;
3586b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][2] = pipe_hi[dir] + (l - 1 + 4) % 4;
3587b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][3] = Jvoff + jfaces[dir][1][(l - 1 + 4) % 4];
3588b7f5c055SJed Brown                 if (ijk[dir] == 0) {
3589b7f5c055SJed Brown                   edges[numEdges][0] = pipe_lo[dir] + l;
3590b7f5c055SJed Brown                   edges[numEdges][1] = pipe_lo[dir] + (l + 1) % 4;
3591b7f5c055SJed Brown                   edgeSets[numEdges] = dir * 2 + 1;
3592b7f5c055SJed Brown                   numEdges++;
3593b7f5c055SJed Brown                 }
3594b7f5c055SJed Brown                 if (ijk[dir] + 1 == extent[dir]) {
3595b7f5c055SJed Brown                   edges[numEdges][0] = pipe_hi[dir] + l;
3596b7f5c055SJed Brown                   edges[numEdges][1] = pipe_hi[dir] + (l + 1) % 4;
3597b7f5c055SJed Brown                   edgeSets[numEdges] = dir * 2 + 2;
3598b7f5c055SJed Brown                   numEdges++;
3599b7f5c055SJed Brown                 }
3600b7f5c055SJed Brown               }
3601b7f5c055SJed Brown             }
3602b7f5c055SJed Brown           }
3603b7f5c055SJed Brown         }
3604b7f5c055SJed Brown       }
360563a3b9bcSJacob Faibussowitsch       PetscCheck(numEdges == Ncuts * 4, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Edge count %" PetscInt_FMT " incompatible with number of cuts %" PetscInt_FMT, numEdges, Ncuts);
3606b7f5c055SJed Brown       numFaces   = 24 * Njunctions;
3607b7f5c055SJed Brown       cells_flat = cells[0][0][0];
3608b7f5c055SJed Brown     }
3609b7f5c055SJed Brown     evalFunc   = TPSEvaluate_SchwarzP;
36104663dae6SJed Brown     normalFunc = TPSExtrudeNormalFunc_SchwarzP;
3611b7f5c055SJed Brown     break;
3612b7f5c055SJed Brown   case DMPLEX_TPS_GYROID:
3613c5853193SPierre Jolivet     if (rank == 0) {
3614b7f5c055SJed Brown       // This is a coarse mesh approximation of the gyroid shifted to being the zero of the level set
3615b7f5c055SJed Brown       //
3616b7f5c055SJed 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)
3617b7f5c055SJed Brown       //
3618b7f5c055SJed Brown       // on the cell [0,2]^3.
3619b7f5c055SJed Brown       //
3620b7f5c055SJed Brown       // Think about dividing that cell into four columns, and focus on the column [0,1]x[0,1]x[0,2].
3621b7f5c055SJed Brown       // If you looked at the gyroid in that column at different slices of z you would see that it kind of spins
3622b7f5c055SJed Brown       // like a boomerang:
3623b7f5c055SJed Brown       //
3624b7f5c055SJed Brown       //     z = 0          z = 1/4        z = 1/2        z = 3/4     //
3625b7f5c055SJed Brown       //     -----          -------        -------        -------     //
3626b7f5c055SJed Brown       //                                                              //
3627b7f5c055SJed Brown       //     +       +      +       +      +       +      +   \   +   //
3628b7f5c055SJed Brown       //      \                                   /            \      //
3629b7f5c055SJed Brown       //       \            `-_   _-'            /              }     //
3630b7f5c055SJed Brown       //        *-_            `-'            _-'              /      //
3631b7f5c055SJed Brown       //     +     `-+      +       +      +-'     +      +   /   +   //
3632b7f5c055SJed Brown       //                                                              //
3633b7f5c055SJed Brown       //                                                              //
3634b7f5c055SJed Brown       //     z = 1          z = 5/4        z = 3/2        z = 7/4     //
3635b7f5c055SJed Brown       //     -----          -------        -------        -------     //
3636b7f5c055SJed Brown       //                                                              //
3637b7f5c055SJed Brown       //     +-_     +      +       +      +     _-+      +   /   +   //
3638b7f5c055SJed Brown       //        `-_            _-_            _-`            /        //
3639b7f5c055SJed Brown       //           \        _-'   `-_        /              {         //
3640b7f5c055SJed Brown       //            \                       /                \        //
3641b7f5c055SJed Brown       //     +       +      +       +      +       +      +   \   +   //
3642b7f5c055SJed Brown       //
3643b7f5c055SJed Brown       //
3644b7f5c055SJed Brown       // This course mesh approximates each of these slices by two line segments,
3645b7f5c055SJed Brown       // and then connects the segments in consecutive layers with quadrilateral faces.
3646b7f5c055SJed Brown       // All of the end points of the segments are multiples of 1/4 except for the
3647b7f5c055SJed Brown       // point * in the picture for z = 0 above and the similar points in other layers.
3648b7f5c055SJed Brown       // That point is at (gamma, gamma, 0), where gamma is calculated below.
3649b7f5c055SJed Brown       //
3650b7f5c055SJed Brown       // The column  [1,2]x[1,2]x[0,2] looks the same as this column;
3651b7f5c055SJed Brown       // The columns [1,2]x[0,1]x[0,2] and [0,1]x[1,2]x[0,2] are mirror images.
3652b7f5c055SJed Brown       //
3653b7f5c055SJed Brown       // As for how this method turned into the names given to the vertices:
3654b7f5c055SJed Brown       // that was not systematic, it was just the way it worked out in my handwritten notes.
3655b7f5c055SJed Brown 
3656b7f5c055SJed Brown       PetscInt facesPerBlock = 64;
3657b7f5c055SJed Brown       PetscInt vertsPerBlock = 56;
3658b7f5c055SJed Brown       PetscInt extentPlus[3];
3659b7f5c055SJed Brown       PetscInt numBlocks, numBlocksPlus;
36609371c9d4SSatish 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;
36619371c9d4SSatish Balay       const PetscInt pattern[64][4] = {
36629371c9d4SSatish Balay         /* face to vertex within the coarse discretization of a single gyroid block */
3663b7f5c055SJed Brown         /* layer 0 */
36649371c9d4SSatish Balay         {A,           C,           K,           G          },
36659371c9d4SSatish Balay         {C,           B,           II,          K          },
36669371c9d4SSatish Balay         {D,           A,           H,           L          },
36679371c9d4SSatish Balay         {B + 56 * 1,  D,           L,           J          },
36689371c9d4SSatish Balay         {E,           B + 56 * 1,  J,           N          },
36699371c9d4SSatish Balay         {A + 56 * 2,  E,           N,           H + 56 * 2 },
36709371c9d4SSatish Balay         {F,           A + 56 * 2,  G + 56 * 2,  M          },
36719371c9d4SSatish Balay         {B,           F,           M,           II         },
3672b7f5c055SJed Brown         /* layer 1 */
36739371c9d4SSatish Balay         {G,           K,           Q,           O          },
36749371c9d4SSatish Balay         {K,           II,          P,           Q          },
36759371c9d4SSatish Balay         {L,           H,           O + 56 * 1,  R          },
36769371c9d4SSatish Balay         {J,           L,           R,           P          },
36779371c9d4SSatish Balay         {N,           J,           P,           S          },
36789371c9d4SSatish Balay         {H + 56 * 2,  N,           S,           O + 56 * 3 },
36799371c9d4SSatish Balay         {M,           G + 56 * 2,  O + 56 * 2,  T          },
36809371c9d4SSatish Balay         {II,          M,           T,           P          },
3681b7f5c055SJed Brown         /* layer 2 */
36829371c9d4SSatish Balay         {O,           Q,           Y,           U          },
36839371c9d4SSatish Balay         {Q,           P,           W,           Y          },
36849371c9d4SSatish Balay         {R,           O + 56 * 1,  U + 56 * 1,  Ap         },
36859371c9d4SSatish Balay         {P,           R,           Ap,          W          },
36869371c9d4SSatish Balay         {S,           P,           X,           Bp         },
36879371c9d4SSatish Balay         {O + 56 * 3,  S,           Bp,          V + 56 * 1 },
36889371c9d4SSatish Balay         {T,           O + 56 * 2,  V,           Z          },
36899371c9d4SSatish Balay         {P,           T,           Z,           X          },
3690b7f5c055SJed Brown         /* layer 3 */
36919371c9d4SSatish Balay         {U,           Y,           Ep,          Dp         },
36929371c9d4SSatish Balay         {Y,           W,           Cp,          Ep         },
36939371c9d4SSatish Balay         {Ap,          U + 56 * 1,  Dp + 56 * 1, Gp         },
36949371c9d4SSatish Balay         {W,           Ap,          Gp,          Cp         },
36959371c9d4SSatish Balay         {Bp,          X,           Cp + 56 * 2, Fp         },
36969371c9d4SSatish Balay         {V + 56 * 1,  Bp,          Fp,          Dp + 56 * 1},
36979371c9d4SSatish Balay         {Z,           V,           Dp,          Hp         },
36989371c9d4SSatish Balay         {X,           Z,           Hp,          Cp + 56 * 2},
3699b7f5c055SJed Brown         /* layer 4 */
37009371c9d4SSatish Balay         {Dp,          Ep,          Mp,          Kp         },
37019371c9d4SSatish Balay         {Ep,          Cp,          Ip,          Mp         },
37029371c9d4SSatish Balay         {Gp,          Dp + 56 * 1, Lp,          Np         },
37039371c9d4SSatish Balay         {Cp,          Gp,          Np,          Jp         },
37049371c9d4SSatish Balay         {Fp,          Cp + 56 * 2, Jp + 56 * 2, Pp         },
37059371c9d4SSatish Balay         {Dp + 56 * 1, Fp,          Pp,          Lp         },
37069371c9d4SSatish Balay         {Hp,          Dp,          Kp,          Op         },
37079371c9d4SSatish Balay         {Cp + 56 * 2, Hp,          Op,          Ip + 56 * 2},
3708b7f5c055SJed Brown         /* layer 5 */
37099371c9d4SSatish Balay         {Kp,          Mp,          Sp,          Rp         },
37109371c9d4SSatish Balay         {Mp,          Ip,          Qp,          Sp         },
37119371c9d4SSatish Balay         {Np,          Lp,          Rp,          Tp         },
37129371c9d4SSatish Balay         {Jp,          Np,          Tp,          Qp + 56 * 1},
37139371c9d4SSatish Balay         {Pp,          Jp + 56 * 2, Qp + 56 * 3, Up         },
37149371c9d4SSatish Balay         {Lp,          Pp,          Up,          Rp         },
37159371c9d4SSatish Balay         {Op,          Kp,          Rp,          Vp         },
37169371c9d4SSatish Balay         {Ip + 56 * 2, Op,          Vp,          Qp + 56 * 2},
3717b7f5c055SJed Brown         /* layer 6 */
37189371c9d4SSatish Balay         {Rp,          Sp,          Aq,          Yp         },
37199371c9d4SSatish Balay         {Sp,          Qp,          Wp,          Aq         },
37209371c9d4SSatish Balay         {Tp,          Rp,          Yp,          Cq         },
37219371c9d4SSatish Balay         {Qp + 56 * 1, Tp,          Cq,          Wp + 56 * 1},
37229371c9d4SSatish Balay         {Up,          Qp + 56 * 3, Xp + 56 * 1, Dq         },
37239371c9d4SSatish Balay         {Rp,          Up,          Dq,          Zp         },
37249371c9d4SSatish Balay         {Vp,          Rp,          Zp,          Bq         },
37259371c9d4SSatish Balay         {Qp + 56 * 2, Vp,          Bq,          Xp         },
3726b7f5c055SJed Brown         /* layer 7 (the top is the periodic image of the bottom of layer 0) */
37279371c9d4SSatish Balay         {Yp,          Aq,          C + 56 * 4,  A + 56 * 4 },
37289371c9d4SSatish Balay         {Aq,          Wp,          B + 56 * 4,  C + 56 * 4 },
37299371c9d4SSatish Balay         {Cq,          Yp,          A + 56 * 4,  D + 56 * 4 },
37309371c9d4SSatish Balay         {Wp + 56 * 1, Cq,          D + 56 * 4,  B + 56 * 5 },
37319371c9d4SSatish Balay         {Dq,          Xp + 56 * 1, B + 56 * 5,  E + 56 * 4 },
37329371c9d4SSatish Balay         {Zp,          Dq,          E + 56 * 4,  A + 56 * 6 },
37339371c9d4SSatish Balay         {Bq,          Zp,          A + 56 * 6,  F + 56 * 4 },
37349371c9d4SSatish Balay         {Xp,          Bq,          F + 56 * 4,  B + 56 * 4 }
3735b7f5c055SJed Brown       };
3736b7f5c055SJed Brown       const PetscReal gamma                = PetscAcosReal((PetscSqrtReal(3.) - 1.) / PetscSqrtReal(2.)) / PETSC_PI;
37379371c9d4SSatish Balay       const PetscReal patternCoords[56][3] = {
3738bee3fc89SBarry Smith         {1.,        0.,        0.  }, /* A  */
3739bee3fc89SBarry Smith         {0.,        1.,        0.  }, /* B  */
3740bee3fc89SBarry Smith         {gamma,     gamma,     0.  }, /* C  */
3741bee3fc89SBarry Smith         {1 + gamma, 1 - gamma, 0.  }, /* D  */
3742bee3fc89SBarry Smith         {2 - gamma, 2 - gamma, 0.  }, /* E  */
3743bee3fc89SBarry Smith         {1 - gamma, 1 + gamma, 0.  }, /* F  */
3744b7f5c055SJed Brown 
3745bee3fc89SBarry Smith         {.5,        0,         .25 }, /* G  */
3746bee3fc89SBarry Smith         {1.5,       0.,        .25 }, /* H  */
3747bee3fc89SBarry Smith         {.5,        1.,        .25 }, /* II */
3748bee3fc89SBarry Smith         {1.5,       1.,        .25 }, /* J  */
3749bee3fc89SBarry Smith         {.25,       .5,        .25 }, /* K  */
3750bee3fc89SBarry Smith         {1.25,      .5,        .25 }, /* L  */
3751bee3fc89SBarry Smith         {.75,       1.5,       .25 }, /* M  */
3752bee3fc89SBarry Smith         {1.75,      1.5,       .25 }, /* N  */
3753b7f5c055SJed Brown 
3754bee3fc89SBarry Smith         {0.,        0.,        .5  }, /* O  */
3755bee3fc89SBarry Smith         {1.,        1.,        .5  }, /* P  */
3756bee3fc89SBarry Smith         {gamma,     1 - gamma, .5  }, /* Q  */
3757bee3fc89SBarry Smith         {1 + gamma, gamma,     .5  }, /* R  */
3758bee3fc89SBarry Smith         {2 - gamma, 1 + gamma, .5  }, /* S  */
3759bee3fc89SBarry Smith         {1 - gamma, 2 - gamma, .5  }, /* T  */
3760b7f5c055SJed Brown 
3761bee3fc89SBarry Smith         {0.,        .5,        .75 }, /* U  */
3762bee3fc89SBarry Smith         {0.,        1.5,       .75 }, /* V  */
3763bee3fc89SBarry Smith         {1.,        .5,        .75 }, /* W  */
3764bee3fc89SBarry Smith         {1.,        1.5,       .75 }, /* X  */
3765bee3fc89SBarry Smith         {.5,        .75,       .75 }, /* Y  */
3766bee3fc89SBarry Smith         {.5,        1.75,      .75 }, /* Z  */
3767bee3fc89SBarry Smith         {1.5,       .25,       .75 }, /* Ap */
3768bee3fc89SBarry Smith         {1.5,       1.25,      .75 }, /* Bp */
3769b7f5c055SJed Brown 
3770bee3fc89SBarry Smith         {1.,        0.,        1.  }, /* Cp */
3771bee3fc89SBarry Smith         {0.,        1.,        1.  }, /* Dp */
3772bee3fc89SBarry Smith         {1 - gamma, 1 - gamma, 1.  }, /* Ep */
3773bee3fc89SBarry Smith         {1 + gamma, 1 + gamma, 1.  }, /* Fp */
3774bee3fc89SBarry Smith         {2 - gamma, gamma,     1.  }, /* Gp */
3775bee3fc89SBarry Smith         {gamma,     2 - gamma, 1.  }, /* Hp */
3776b7f5c055SJed Brown 
3777bee3fc89SBarry Smith         {.5,        0.,        1.25}, /* Ip */
3778bee3fc89SBarry Smith         {1.5,       0.,        1.25}, /* Jp */
3779bee3fc89SBarry Smith         {.5,        1.,        1.25}, /* Kp */
3780bee3fc89SBarry Smith         {1.5,       1.,        1.25}, /* Lp */
3781bee3fc89SBarry Smith         {.75,       .5,        1.25}, /* Mp */
3782bee3fc89SBarry Smith         {1.75,      .5,        1.25}, /* Np */
3783bee3fc89SBarry Smith         {.25,       1.5,       1.25}, /* Op */
3784bee3fc89SBarry Smith         {1.25,      1.5,       1.25}, /* Pp */
3785b7f5c055SJed Brown 
3786bee3fc89SBarry Smith         {0.,        0.,        1.5 }, /* Qp */
3787bee3fc89SBarry Smith         {1.,        1.,        1.5 }, /* Rp */
3788bee3fc89SBarry Smith         {1 - gamma, gamma,     1.5 }, /* Sp */
3789bee3fc89SBarry Smith         {2 - gamma, 1 - gamma, 1.5 }, /* Tp */
3790bee3fc89SBarry Smith         {1 + gamma, 2 - gamma, 1.5 }, /* Up */
3791bee3fc89SBarry Smith         {gamma,     1 + gamma, 1.5 }, /* Vp */
3792b7f5c055SJed Brown 
3793bee3fc89SBarry Smith         {0.,        .5,        1.75}, /* Wp */
3794bee3fc89SBarry Smith         {0.,        1.5,       1.75}, /* Xp */
3795bee3fc89SBarry Smith         {1.,        .5,        1.75}, /* Yp */
3796bee3fc89SBarry Smith         {1.,        1.5,       1.75}, /* Zp */
3797bee3fc89SBarry Smith         {.5,        .25,       1.75}, /* Aq */
3798bee3fc89SBarry Smith         {.5,        1.25,      1.75}, /* Bq */
3799bee3fc89SBarry Smith         {1.5,       .75,       1.75}, /* Cq */
3800bee3fc89SBarry Smith         {1.5,       1.75,      1.75}, /* Dq */
3801b7f5c055SJed Brown       };
3802b7f5c055SJed Brown       PetscInt(*cells)[64][4] = NULL;
3803b7f5c055SJed Brown       PetscBool *seen;
3804b7f5c055SJed Brown       PetscInt  *vertToTrueVert;
3805b7f5c055SJed Brown       PetscInt   count;
3806b7f5c055SJed Brown 
3807b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) extentPlus[i] = extent[i] + 1;
3808b7f5c055SJed Brown       numBlocks = 1;
3809b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) numBlocks *= extent[i];
3810b7f5c055SJed Brown       numBlocksPlus = 1;
3811b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) numBlocksPlus *= extentPlus[i];
3812b7f5c055SJed Brown       numFaces = numBlocks * facesPerBlock;
38139566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numBlocks, &cells));
38149566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numBlocksPlus * vertsPerBlock, &seen));
3815b7f5c055SJed Brown       for (PetscInt k = 0; k < extent[2]; k++) {
3816b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3817b7f5c055SJed Brown           for (PetscInt i = 0; i < extent[0]; i++) {
3818b7f5c055SJed Brown             for (PetscInt f = 0; f < facesPerBlock; f++) {
3819b7f5c055SJed Brown               for (PetscInt v = 0; v < 4; v++) {
3820b7f5c055SJed Brown                 PetscInt vertRaw     = pattern[f][v];
3821b7f5c055SJed Brown                 PetscInt blockidx    = vertRaw / 56;
3822b7f5c055SJed Brown                 PetscInt patternvert = vertRaw % 56;
3823b7f5c055SJed Brown                 PetscInt xplus       = (blockidx & 1);
3824b7f5c055SJed Brown                 PetscInt yplus       = (blockidx & 2) >> 1;
3825b7f5c055SJed Brown                 PetscInt zplus       = (blockidx & 4) >> 2;
3826b7f5c055SJed Brown                 PetscInt zcoord      = (periodic && periodic[2] == DM_BOUNDARY_PERIODIC) ? ((k + zplus) % extent[2]) : (k + zplus);
3827b7f5c055SJed Brown                 PetscInt ycoord      = (periodic && periodic[1] == DM_BOUNDARY_PERIODIC) ? ((j + yplus) % extent[1]) : (j + yplus);
3828b7f5c055SJed Brown                 PetscInt xcoord      = (periodic && periodic[0] == DM_BOUNDARY_PERIODIC) ? ((i + xplus) % extent[0]) : (i + xplus);
3829b7f5c055SJed Brown                 PetscInt vert        = ((zcoord * extentPlus[1] + ycoord) * extentPlus[0] + xcoord) * 56 + patternvert;
3830b7f5c055SJed Brown 
3831b7f5c055SJed Brown                 cells[(k * extent[1] + j) * extent[0] + i][f][v] = vert;
3832b7f5c055SJed Brown                 seen[vert]                                       = PETSC_TRUE;
3833b7f5c055SJed Brown               }
3834b7f5c055SJed Brown             }
3835b7f5c055SJed Brown           }
3836b7f5c055SJed Brown         }
3837b7f5c055SJed Brown       }
38389371c9d4SSatish Balay       for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++)
38399371c9d4SSatish Balay         if (seen[i]) numVertices++;
3840b7f5c055SJed Brown       count = 0;
38419566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numBlocksPlus * vertsPerBlock, &vertToTrueVert));
38429566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numVertices * 3, &vtxCoords));
3843b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++) vertToTrueVert[i] = -1;
3844b7f5c055SJed Brown       for (PetscInt k = 0; k < extentPlus[2]; k++) {
3845b7f5c055SJed Brown         for (PetscInt j = 0; j < extentPlus[1]; j++) {
3846b7f5c055SJed Brown           for (PetscInt i = 0; i < extentPlus[0]; i++) {
3847b7f5c055SJed Brown             for (PetscInt v = 0; v < vertsPerBlock; v++) {
3848b7f5c055SJed Brown               PetscInt vIdx = ((k * extentPlus[1] + j) * extentPlus[0] + i) * vertsPerBlock + v;
3849b7f5c055SJed Brown 
3850b7f5c055SJed Brown               if (seen[vIdx]) {
3851b7f5c055SJed Brown                 PetscInt thisVert;
3852b7f5c055SJed Brown 
3853b7f5c055SJed Brown                 vertToTrueVert[vIdx] = thisVert = count++;
3854b7f5c055SJed Brown 
3855b7f5c055SJed Brown                 for (PetscInt d = 0; d < 3; d++) vtxCoords[3 * thisVert + d] = patternCoords[v][d];
3856b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 0] += i * 2;
3857b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 1] += j * 2;
3858b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 2] += k * 2;
3859b7f5c055SJed Brown               }
3860b7f5c055SJed Brown             }
3861b7f5c055SJed Brown           }
3862b7f5c055SJed Brown         }
3863b7f5c055SJed Brown       }
3864b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocks; i++) {
3865b7f5c055SJed Brown         for (PetscInt f = 0; f < facesPerBlock; f++) {
3866ad540459SPierre Jolivet           for (PetscInt v = 0; v < 4; v++) cells[i][f][v] = vertToTrueVert[cells[i][f][v]];
3867b7f5c055SJed Brown         }
3868b7f5c055SJed Brown       }
38699566063dSJacob Faibussowitsch       PetscCall(PetscFree(vertToTrueVert));
38709566063dSJacob Faibussowitsch       PetscCall(PetscFree(seen));
3871b7f5c055SJed Brown       cells_flat = cells[0][0];
3872b7f5c055SJed Brown       numEdges   = 0;
3873b7f5c055SJed Brown       for (PetscInt i = 0; i < numFaces; i++) {
3874b7f5c055SJed Brown         for (PetscInt e = 0; e < 4; e++) {
3875b7f5c055SJed Brown           PetscInt         ev[]       = {cells_flat[i * 4 + e], cells_flat[i * 4 + ((e + 1) % 4)]};
3876b7f5c055SJed Brown           const PetscReal *evCoords[] = {&vtxCoords[3 * ev[0]], &vtxCoords[3 * ev[1]]};
3877b7f5c055SJed Brown 
3878b7f5c055SJed Brown           for (PetscInt d = 0; d < 3; d++) {
3879b7f5c055SJed Brown             if (!periodic || periodic[0] != DM_BOUNDARY_PERIODIC) {
3880b7f5c055SJed Brown               if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) numEdges++;
3881b7f5c055SJed Brown               if (evCoords[0][d] == 2. * extent[d] && evCoords[1][d] == 2. * extent[d]) numEdges++;
3882b7f5c055SJed Brown             }
3883b7f5c055SJed Brown           }
3884b7f5c055SJed Brown         }
3885b7f5c055SJed Brown       }
38869566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numEdges, &edges));
38879566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numEdges, &edgeSets));
3888b7f5c055SJed Brown       for (PetscInt edge = 0, i = 0; i < numFaces; i++) {
3889b7f5c055SJed Brown         for (PetscInt e = 0; e < 4; e++) {
3890b7f5c055SJed Brown           PetscInt         ev[]       = {cells_flat[i * 4 + e], cells_flat[i * 4 + ((e + 1) % 4)]};
3891b7f5c055SJed Brown           const PetscReal *evCoords[] = {&vtxCoords[3 * ev[0]], &vtxCoords[3 * ev[1]]};
3892b7f5c055SJed Brown 
3893b7f5c055SJed Brown           for (PetscInt d = 0; d < 3; d++) {
3894b7f5c055SJed Brown             if (!periodic || periodic[d] != DM_BOUNDARY_PERIODIC) {
3895b7f5c055SJed Brown               if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) {
3896b7f5c055SJed Brown                 edges[edge][0]   = ev[0];
3897b7f5c055SJed Brown                 edges[edge][1]   = ev[1];
3898b7f5c055SJed Brown                 edgeSets[edge++] = 2 * d;
3899b7f5c055SJed Brown               }
3900b7f5c055SJed Brown               if (evCoords[0][d] == 2. * extent[d] && evCoords[1][d] == 2. * extent[d]) {
3901b7f5c055SJed Brown                 edges[edge][0]   = ev[0];
3902b7f5c055SJed Brown                 edges[edge][1]   = ev[1];
3903b7f5c055SJed Brown                 edgeSets[edge++] = 2 * d + 1;
3904b7f5c055SJed Brown               }
3905b7f5c055SJed Brown             }
3906b7f5c055SJed Brown           }
3907b7f5c055SJed Brown         }
3908b7f5c055SJed Brown       }
3909b7f5c055SJed Brown     }
3910b7f5c055SJed Brown     evalFunc   = TPSEvaluate_Gyroid;
39114663dae6SJed Brown     normalFunc = TPSExtrudeNormalFunc_Gyroid;
3912b7f5c055SJed Brown     break;
3913b7f5c055SJed Brown   }
3914b7f5c055SJed Brown 
39159566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, topoDim));
3916c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildFromCellList(dm, numFaces, numVertices, 4, cells_flat));
39179566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildFromCellList(dm, 0, 0, 0, NULL));
39189566063dSJacob Faibussowitsch   PetscCall(PetscFree(cells_flat));
3919b7f5c055SJed Brown   {
3920b7f5c055SJed Brown     DM idm;
39219566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(dm, &idm));
392269d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &idm));
3923b7f5c055SJed Brown   }
3924c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildCoordinatesFromCellList(dm, spaceDim, vtxCoords));
39259566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildCoordinatesFromCellList(dm, spaceDim, NULL));
39269566063dSJacob Faibussowitsch   PetscCall(PetscFree(vtxCoords));
3927b7f5c055SJed Brown 
39289566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
39299566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "Face Sets", &label));
3930b7f5c055SJed Brown   for (PetscInt e = 0; e < numEdges; e++) {
3931b7f5c055SJed Brown     PetscInt        njoin;
3932b7f5c055SJed Brown     const PetscInt *join, verts[] = {numFaces + edges[e][0], numFaces + edges[e][1]};
39339566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, 2, verts, &njoin, &join));
393463a3b9bcSJacob 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]);
39359566063dSJacob Faibussowitsch     PetscCall(DMLabelSetValue(label, join[0], edgeSets[e]));
39369566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, 2, verts, &njoin, &join));
3937b7f5c055SJed Brown   }
39389566063dSJacob Faibussowitsch   PetscCall(PetscFree(edges));
39399566063dSJacob Faibussowitsch   PetscCall(PetscFree(edgeSets));
39401436d7faSJed Brown   if (tps_distribute) {
39411436d7faSJed Brown     DM               pdm = NULL;
39421436d7faSJed Brown     PetscPartitioner part;
39431436d7faSJed Brown 
39449566063dSJacob Faibussowitsch     PetscCall(DMPlexGetPartitioner(dm, &part));
39459566063dSJacob Faibussowitsch     PetscCall(PetscPartitionerSetFromOptions(part));
39469566063dSJacob Faibussowitsch     PetscCall(DMPlexDistribute(dm, 0, NULL, &pdm));
394748a46eb9SPierre Jolivet     if (pdm) PetscCall(DMPlexReplace_Internal(dm, &pdm));
39481436d7faSJed Brown     // Do not auto-distribute again
39499566063dSJacob Faibussowitsch     PetscCall(DMPlexDistributeSetDefault(dm, PETSC_FALSE));
39501436d7faSJed Brown   }
3951b7f5c055SJed Brown 
39529566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
3953b7f5c055SJed Brown   for (PetscInt refine = 0; refine < refinements; refine++) {
3954b7f5c055SJed Brown     PetscInt     m;
3955b7f5c055SJed Brown     DM           dmf;
3956b7f5c055SJed Brown     Vec          X;
3957b7f5c055SJed Brown     PetscScalar *x;
39589566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, MPI_COMM_NULL, &dmf));
395969d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &dmf));
3960b7f5c055SJed Brown 
39619566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &X));
39629566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(X, &m));
39639566063dSJacob Faibussowitsch     PetscCall(VecGetArray(X, &x));
396448a46eb9SPierre Jolivet     for (PetscInt i = 0; i < m; i += 3) PetscCall(TPSNearestPoint(evalFunc, &x[i]));
39659566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(X, &x));
3966b7f5c055SJed Brown   }
3967b7f5c055SJed Brown 
3968b7f5c055SJed Brown   // Face Sets has already been propagated to new vertices during refinement; this propagates to the initial vertices.
39699566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "Face Sets", &label));
39709566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete(dm, label));
3971b7f5c055SJed Brown 
397246139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
397346139095SJed Brown 
3974b7f5c055SJed Brown   if (thickness > 0) {
39754663dae6SJed Brown     DM              edm, cdm, ecdm;
39764663dae6SJed Brown     DMPlexTransform tr;
39774663dae6SJed Brown     const char     *prefix;
39784663dae6SJed Brown     PetscOptions    options;
39794663dae6SJed Brown     // Code from DMPlexExtrude
39804663dae6SJed Brown     PetscCall(DMPlexTransformCreate(PetscObjectComm((PetscObject)dm), &tr));
39814663dae6SJed Brown     PetscCall(DMPlexTransformSetDM(tr, dm));
39824663dae6SJed Brown     PetscCall(DMPlexTransformSetType(tr, DMPLEXEXTRUDE));
39834663dae6SJed Brown     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, &prefix));
39844663dae6SJed Brown     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)tr, prefix));
39854663dae6SJed Brown     PetscCall(PetscObjectGetOptions((PetscObject)dm, &options));
39864663dae6SJed Brown     PetscCall(PetscObjectSetOptions((PetscObject)tr, options));
39874663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetLayers(tr, layers));
39884663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetThickness(tr, thickness));
39894663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetTensor(tr, PETSC_FALSE));
39904663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetSymmetric(tr, PETSC_TRUE));
39914663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetNormalFunction(tr, normalFunc));
39924663dae6SJed Brown     PetscCall(DMPlexTransformSetFromOptions(tr));
39934663dae6SJed Brown     PetscCall(PetscObjectSetOptions((PetscObject)tr, NULL));
39944663dae6SJed Brown     PetscCall(DMPlexTransformSetUp(tr));
39954663dae6SJed Brown     PetscCall(PetscObjectViewFromOptions((PetscObject)tr, NULL, "-dm_plex_tps_transform_view"));
39964663dae6SJed Brown     PetscCall(DMPlexTransformApply(tr, dm, &edm));
39974663dae6SJed Brown     PetscCall(DMCopyDisc(dm, edm));
39984663dae6SJed Brown     PetscCall(DMGetCoordinateDM(dm, &cdm));
39994663dae6SJed Brown     PetscCall(DMGetCoordinateDM(edm, &ecdm));
40004663dae6SJed Brown     PetscCall(DMCopyDisc(cdm, ecdm));
40014663dae6SJed Brown     PetscCall(DMPlexTransformCreateDiscLabels(tr, edm));
40024663dae6SJed Brown     PetscCall(DMPlexTransformDestroy(&tr));
4003a77a5016SMatthew G. Knepley     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, edm));
400469d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &edm));
4005b7f5c055SJed Brown   }
40063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4007b7f5c055SJed Brown }
4008b7f5c055SJed Brown 
4009b7f5c055SJed Brown /*@
4010b7f5c055SJed Brown   DMPlexCreateTPSMesh - Create a distributed, interpolated mesh of a triply-periodic surface
4011b7f5c055SJed Brown 
4012b7f5c055SJed Brown   Collective
4013b7f5c055SJed Brown 
4014b7f5c055SJed Brown   Input Parameters:
4015a1cb98faSBarry Smith + comm           - The communicator for the `DM` object
4016b7f5c055SJed Brown . tpstype        - Type of triply-periodic surface
4017b7f5c055SJed Brown . extent         - Array of length 3 containing number of periods in each direction
401820f4b53cSBarry Smith . periodic       - array of length 3 with periodicity, or `NULL` for non-periodic
40191436d7faSJed Brown . tps_distribute - Distribute 2D manifold mesh prior to refinement and extrusion (more scalable)
4020817da375SSatish Balay . refinements    - Number of factor-of-2 refinements of 2D manifold mesh
40211436d7faSJed Brown . layers         - Number of cell layers extruded in normal direction
4022817da375SSatish Balay - thickness      - Thickness in normal direction
4023b7f5c055SJed Brown 
4024b7f5c055SJed Brown   Output Parameter:
4025a1cb98faSBarry Smith . dm - The `DM` object
4026a1cb98faSBarry Smith 
4027a1cb98faSBarry Smith   Level: beginner
4028b7f5c055SJed Brown 
4029b7f5c055SJed Brown   Notes:
403015229ffcSPierre Jolivet   This meshes the surface of the Schwarz P or Gyroid surfaces.  Schwarz P is the simplest member of the triply-periodic minimal surfaces.
40311d27aa22SBarry Smith   <https://en.wikipedia.org/wiki/Schwarz_minimal_surface#Schwarz_P_(%22Primitive%22)> and can be cut with "clean" boundaries.
40321d27aa22SBarry 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.
4033b7f5c055SJed Brown   Our implementation creates a very coarse mesh of the surface and refines (by 4-way splitting) as many times as requested.
4034b7f5c055SJed Brown   On each refinement, all vertices are projected to their nearest point on the surface.
4035b7f5c055SJed Brown   This projection could readily be extended to related surfaces.
4036b7f5c055SJed Brown 
40371d27aa22SBarry Smith   See {cite}`maskery2018insights`
40381d27aa22SBarry Smith 
40391d27aa22SBarry Smith   The face (edge) sets for the Schwarz P surface are numbered $1(-x), 2(+x), 3(-y), 4(+y), 5(-z), 6(+z)$.
40401d27aa22SBarry Smith   When the mesh is refined, "Face Sets" contain the new vertices (created during refinement).
40411d27aa22SBarry Smith   Use `DMPlexLabelComplete()` to propagate to coarse-level vertices.
4042b7f5c055SJed Brown 
404360225df5SJacob Faibussowitsch   Developer Notes:
4044b7f5c055SJed Brown   The Gyroid mesh does not currently mark boundary sets.
4045b7f5c055SJed Brown 
40461cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSphereMesh()`, `DMSetType()`, `DMCreate()`
4047b7f5c055SJed Brown @*/
4048d71ae5a4SJacob 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)
4049d71ae5a4SJacob Faibussowitsch {
4050b7f5c055SJed Brown   PetscFunctionBegin;
40519566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
40529566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
40539566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateTPSMesh_Internal(*dm, tpstype, extent, periodic, tps_distribute, refinements, layers, thickness));
40543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4055b7f5c055SJed Brown }
4056b7f5c055SJed Brown 
40579318fe57SMatthew G. Knepley /*@
40589318fe57SMatthew G. Knepley   DMPlexCreateSphereMesh - Creates a mesh on the d-dimensional sphere, S^d.
40599318fe57SMatthew G. Knepley 
40609318fe57SMatthew G. Knepley   Collective
40619318fe57SMatthew G. Knepley 
40629318fe57SMatthew G. Knepley   Input Parameters:
4063a1cb98faSBarry Smith + comm    - The communicator for the `DM` object
40649318fe57SMatthew G. Knepley . dim     - The dimension
40659318fe57SMatthew G. Knepley . simplex - Use simplices, or tensor product cells
40669318fe57SMatthew G. Knepley - R       - The radius
40679318fe57SMatthew G. Knepley 
40689318fe57SMatthew G. Knepley   Output Parameter:
4069a1cb98faSBarry Smith . dm - The `DM` object
40709318fe57SMatthew G. Knepley 
40719318fe57SMatthew G. Knepley   Level: beginner
40729318fe57SMatthew G. Knepley 
40731cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBallMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
40749318fe57SMatthew G. Knepley @*/
4075d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateSphereMesh(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscReal R, DM *dm)
4076d71ae5a4SJacob Faibussowitsch {
40779318fe57SMatthew G. Knepley   PetscFunctionBegin;
40784f572ea9SToby Isaac   PetscAssertPointer(dm, 5);
40799566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
40809566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
40819566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSphereMesh_Internal(*dm, dim, simplex, R));
40823ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
40839318fe57SMatthew G. Knepley }
40849318fe57SMatthew G. Knepley 
4085d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBallMesh_Internal(DM dm, PetscInt dim, PetscReal R)
4086d71ae5a4SJacob Faibussowitsch {
40879318fe57SMatthew G. Knepley   DM          sdm, vol;
40889318fe57SMatthew G. Knepley   DMLabel     bdlabel;
4089dd2b43ebSStefano Zampini   const char *prefix;
40909318fe57SMatthew G. Knepley 
40919318fe57SMatthew G. Knepley   PetscFunctionBegin;
40929566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &sdm));
40939566063dSJacob Faibussowitsch   PetscCall(DMSetType(sdm, DMPLEX));
4094dd2b43ebSStefano Zampini   PetscCall(DMGetOptionsPrefix(dm, &prefix));
4095dd2b43ebSStefano Zampini   PetscCall(DMSetOptionsPrefix(sdm, prefix));
4096dd2b43ebSStefano Zampini   PetscCall(DMAppendOptionsPrefix(sdm, "bd_"));
4097dd2b43ebSStefano Zampini   PetscCall(DMPlexDistributeSetDefault(sdm, PETSC_FALSE));
40989566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSphereMesh_Internal(sdm, dim - 1, PETSC_TRUE, R));
40999566063dSJacob Faibussowitsch   PetscCall(DMSetFromOptions(sdm));
41009566063dSJacob Faibussowitsch   PetscCall(DMViewFromOptions(sdm, NULL, "-dm_view"));
41019566063dSJacob Faibussowitsch   PetscCall(DMPlexGenerate(sdm, NULL, PETSC_TRUE, &vol));
41029566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&sdm));
410369d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &vol));
41049566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
41059566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "marker", &bdlabel));
41069566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkBoundaryFaces(dm, PETSC_DETERMINE, bdlabel));
41079566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete(dm, bdlabel));
41083ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
410951a74b61SMatthew G. Knepley }
411051a74b61SMatthew G. Knepley 
411151a74b61SMatthew G. Knepley /*@
411251a74b61SMatthew G. Knepley   DMPlexCreateBallMesh - Creates a simplex mesh on the d-dimensional ball, B^d.
411351a74b61SMatthew G. Knepley 
411451a74b61SMatthew G. Knepley   Collective
411551a74b61SMatthew G. Knepley 
411651a74b61SMatthew G. Knepley   Input Parameters:
4117a1cb98faSBarry Smith + comm - The communicator for the `DM` object
411851a74b61SMatthew G. Knepley . dim  - The dimension
411951a74b61SMatthew G. Knepley - R    - The radius
412051a74b61SMatthew G. Knepley 
412151a74b61SMatthew G. Knepley   Output Parameter:
4122a1cb98faSBarry Smith . dm - The `DM` object
412351a74b61SMatthew G. Knepley 
4124a1cb98faSBarry Smith   Options Database Key:
412560225df5SJacob Faibussowitsch . bd_dm_refine - This will refine the surface mesh preserving the sphere geometry
412651a74b61SMatthew G. Knepley 
412751a74b61SMatthew G. Knepley   Level: beginner
412851a74b61SMatthew G. Knepley 
41291cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSphereMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
413051a74b61SMatthew G. Knepley @*/
4131d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateBallMesh(MPI_Comm comm, PetscInt dim, PetscReal R, DM *dm)
4132d71ae5a4SJacob Faibussowitsch {
413351a74b61SMatthew G. Knepley   PetscFunctionBegin;
41349566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
41359566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
41369566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBallMesh_Internal(*dm, dim, R));
41373ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
41382829fed8SMatthew G. Knepley }
41392829fed8SMatthew G. Knepley 
4140d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateReferenceCell_Internal(DM rdm, DMPolytopeType ct)
4141d71ae5a4SJacob Faibussowitsch {
41420a6ba040SMatthew G. Knepley   PetscFunctionBegin;
41439318fe57SMatthew G. Knepley   switch (ct) {
41449371c9d4SSatish Balay   case DM_POLYTOPE_POINT: {
41459318fe57SMatthew G. Knepley     PetscInt    numPoints[1]        = {1};
41469318fe57SMatthew G. Knepley     PetscInt    coneSize[1]         = {0};
41479318fe57SMatthew G. Knepley     PetscInt    cones[1]            = {0};
41489318fe57SMatthew G. Knepley     PetscInt    coneOrientations[1] = {0};
41499318fe57SMatthew G. Knepley     PetscScalar vertexCoords[1]     = {0.0};
41509318fe57SMatthew G. Knepley 
41519566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 0));
41529566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 0, numPoints, coneSize, cones, coneOrientations, vertexCoords));
41539371c9d4SSatish Balay   } break;
41549371c9d4SSatish Balay   case DM_POLYTOPE_SEGMENT: {
41559318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {2, 1};
41569318fe57SMatthew G. Knepley     PetscInt    coneSize[3]         = {2, 0, 0};
41579318fe57SMatthew G. Knepley     PetscInt    cones[2]            = {1, 2};
41589318fe57SMatthew G. Knepley     PetscInt    coneOrientations[2] = {0, 0};
41599318fe57SMatthew G. Knepley     PetscScalar vertexCoords[2]     = {-1.0, 1.0};
41609318fe57SMatthew G. Knepley 
41619566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 1));
41629566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
41639371c9d4SSatish Balay   } break;
41649371c9d4SSatish Balay   case DM_POLYTOPE_POINT_PRISM_TENSOR: {
4165b5a892a1SMatthew G. Knepley     PetscInt    numPoints[2]        = {2, 1};
4166b5a892a1SMatthew G. Knepley     PetscInt    coneSize[3]         = {2, 0, 0};
4167b5a892a1SMatthew G. Knepley     PetscInt    cones[2]            = {1, 2};
4168b5a892a1SMatthew G. Knepley     PetscInt    coneOrientations[2] = {0, 0};
4169b5a892a1SMatthew G. Knepley     PetscScalar vertexCoords[2]     = {-1.0, 1.0};
4170b5a892a1SMatthew G. Knepley 
41719566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 1));
41729566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
41739371c9d4SSatish Balay   } break;
41749371c9d4SSatish Balay   case DM_POLYTOPE_TRIANGLE: {
41759318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {3, 1};
41769318fe57SMatthew G. Knepley     PetscInt    coneSize[4]         = {3, 0, 0, 0};
41779318fe57SMatthew G. Knepley     PetscInt    cones[3]            = {1, 2, 3};
41789318fe57SMatthew G. Knepley     PetscInt    coneOrientations[3] = {0, 0, 0};
41799318fe57SMatthew G. Knepley     PetscScalar vertexCoords[6]     = {-1.0, -1.0, 1.0, -1.0, -1.0, 1.0};
41809318fe57SMatthew G. Knepley 
41819566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 2));
41829566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
41839371c9d4SSatish Balay   } break;
41849371c9d4SSatish Balay   case DM_POLYTOPE_QUADRILATERAL: {
41859318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {4, 1};
41869318fe57SMatthew G. Knepley     PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
41879318fe57SMatthew G. Knepley     PetscInt    cones[4]            = {1, 2, 3, 4};
41889318fe57SMatthew G. Knepley     PetscInt    coneOrientations[4] = {0, 0, 0, 0};
41899318fe57SMatthew G. Knepley     PetscScalar vertexCoords[8]     = {-1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0};
41909318fe57SMatthew G. Knepley 
41919566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 2));
41929566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
41939371c9d4SSatish Balay   } break;
41949371c9d4SSatish Balay   case DM_POLYTOPE_SEG_PRISM_TENSOR: {
41959318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {4, 1};
41969318fe57SMatthew G. Knepley     PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
41979318fe57SMatthew G. Knepley     PetscInt    cones[4]            = {1, 2, 3, 4};
41989318fe57SMatthew G. Knepley     PetscInt    coneOrientations[4] = {0, 0, 0, 0};
41999318fe57SMatthew G. Knepley     PetscScalar vertexCoords[8]     = {-1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0};
42009318fe57SMatthew G. Knepley 
42019566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 2));
42029566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
42039371c9d4SSatish Balay   } break;
42049371c9d4SSatish Balay   case DM_POLYTOPE_TETRAHEDRON: {
42059318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {4, 1};
42069318fe57SMatthew G. Knepley     PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
4207f0edb160SMatthew G. Knepley     PetscInt    cones[4]            = {1, 2, 3, 4};
42089318fe57SMatthew G. Knepley     PetscInt    coneOrientations[4] = {0, 0, 0, 0};
4209f0edb160SMatthew 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};
42109318fe57SMatthew G. Knepley 
42119566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
42129566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
42139371c9d4SSatish Balay   } break;
42149371c9d4SSatish Balay   case DM_POLYTOPE_HEXAHEDRON: {
42159318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {8, 1};
42169318fe57SMatthew G. Knepley     PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
4217f0edb160SMatthew G. Knepley     PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
42189318fe57SMatthew G. Knepley     PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
42199371c9d4SSatish 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};
42209318fe57SMatthew G. Knepley 
42219566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
42229566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
42239371c9d4SSatish Balay   } break;
42249371c9d4SSatish Balay   case DM_POLYTOPE_TRI_PRISM: {
42259318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {6, 1};
42269318fe57SMatthew G. Knepley     PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
4227f0edb160SMatthew G. Knepley     PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
42289318fe57SMatthew G. Knepley     PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
42299371c9d4SSatish 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};
42309318fe57SMatthew G. Knepley 
42319566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
42329566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
42339371c9d4SSatish Balay   } break;
42349371c9d4SSatish Balay   case DM_POLYTOPE_TRI_PRISM_TENSOR: {
42359318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {6, 1};
42369318fe57SMatthew G. Knepley     PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
42379318fe57SMatthew G. Knepley     PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
42389318fe57SMatthew G. Knepley     PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
42399371c9d4SSatish 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};
42409318fe57SMatthew G. Knepley 
42419566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
42429566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
42439371c9d4SSatish Balay   } break;
42449371c9d4SSatish Balay   case DM_POLYTOPE_QUAD_PRISM_TENSOR: {
42459318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {8, 1};
42469318fe57SMatthew G. Knepley     PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
42479318fe57SMatthew G. Knepley     PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
42489318fe57SMatthew G. Knepley     PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
42499371c9d4SSatish 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};
42509318fe57SMatthew G. Knepley 
42519566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
42529566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
42539371c9d4SSatish Balay   } break;
42549371c9d4SSatish Balay   case DM_POLYTOPE_PYRAMID: {
42559318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {5, 1};
42569318fe57SMatthew G. Knepley     PetscInt    coneSize[6]         = {5, 0, 0, 0, 0, 0};
4257f0edb160SMatthew G. Knepley     PetscInt    cones[5]            = {1, 2, 3, 4, 5};
42589318fe57SMatthew G. Knepley     PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
42599371c9d4SSatish 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};
42609318fe57SMatthew G. Knepley 
42619566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
42629566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
42639371c9d4SSatish Balay   } break;
4264d71ae5a4SJacob Faibussowitsch   default:
4265d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)rdm), PETSC_ERR_ARG_WRONG, "Cannot create reference cell for cell type %s", DMPolytopeTypes[ct]);
42669318fe57SMatthew G. Knepley   }
42679318fe57SMatthew G. Knepley   {
42689318fe57SMatthew G. Knepley     PetscInt Nv, v;
42699318fe57SMatthew G. Knepley 
42709318fe57SMatthew G. Knepley     /* Must create the celltype label here so that we do not automatically try to compute the types */
42719566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(rdm, "celltype"));
42729566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCellType(rdm, 0, ct));
42739566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(rdm, NULL, &Nv));
42749566063dSJacob Faibussowitsch     for (v = 1; v < Nv; ++v) PetscCall(DMPlexSetCellType(rdm, v, DM_POLYTOPE_POINT));
42759318fe57SMatthew G. Knepley   }
42769566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolateInPlace_Internal(rdm));
42779566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)rdm, DMPolytopeTypes[ct]));
42783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
42790a6ba040SMatthew G. Knepley }
42800a6ba040SMatthew G. Knepley 
42819318fe57SMatthew G. Knepley /*@
4282a1cb98faSBarry Smith   DMPlexCreateReferenceCell - Create a `DMPLEX` with the appropriate FEM reference cell
42839318fe57SMatthew G. Knepley 
42849318fe57SMatthew G. Knepley   Collective
42859318fe57SMatthew G. Knepley 
42869318fe57SMatthew G. Knepley   Input Parameters:
42879318fe57SMatthew G. Knepley + comm - The communicator
42889318fe57SMatthew G. Knepley - ct   - The cell type of the reference cell
42899318fe57SMatthew G. Knepley 
42909318fe57SMatthew G. Knepley   Output Parameter:
42919318fe57SMatthew G. Knepley . refdm - The reference cell
42929318fe57SMatthew G. Knepley 
42939318fe57SMatthew G. Knepley   Level: intermediate
42949318fe57SMatthew G. Knepley 
429542747ad1SJacob Faibussowitsch .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBoxMesh()`
42969318fe57SMatthew G. Knepley @*/
4297d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateReferenceCell(MPI_Comm comm, DMPolytopeType ct, DM *refdm)
4298d71ae5a4SJacob Faibussowitsch {
42990a6ba040SMatthew G. Knepley   PetscFunctionBegin;
43009566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, refdm));
43019566063dSJacob Faibussowitsch   PetscCall(DMSetType(*refdm, DMPLEX));
43029566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateReferenceCell_Internal(*refdm, ct));
43033ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
43049318fe57SMatthew G. Knepley }
430579a015ccSMatthew G. Knepley 
4306d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoundaryLabel_Private(DM dm, const char name[])
4307d71ae5a4SJacob Faibussowitsch {
43089318fe57SMatthew G. Knepley   DM        plex;
43099318fe57SMatthew G. Knepley   DMLabel   label;
43109318fe57SMatthew G. Knepley   PetscBool hasLabel;
43110a6ba040SMatthew G. Knepley 
4312c22d3578SMatthew G. Knepley   PetscFunctionBegin;
43139566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &hasLabel));
43143ba16761SJacob Faibussowitsch   if (hasLabel) PetscFunctionReturn(PETSC_SUCCESS);
43159566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, name));
43169566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
43179566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
43189566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkBoundaryFaces(plex, 1, label));
43191c8afea9SMatthew G. Knepley   PetscCall(DMPlexLabelComplete(plex, label));
43209566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plex));
43213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
43229318fe57SMatthew G. Knepley }
4323acdc6f61SToby Isaac 
4324669647acSMatthew G. Knepley /*
4325669647acSMatthew 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.
4326669647acSMatthew G. Knepley 
4327669647acSMatthew G. Knepley     (x, y) -> (r, theta) = (x[1], (x[0] - lower[0]) * 2\pi/(upper[0] - lower[0]))
4328669647acSMatthew G. Knepley */
4329d71ae5a4SJacob 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[])
4330d71ae5a4SJacob Faibussowitsch {
4331669647acSMatthew G. Knepley   const PetscReal low = PetscRealPart(constants[0]);
4332669647acSMatthew G. Knepley   const PetscReal upp = PetscRealPart(constants[1]);
4333669647acSMatthew G. Knepley   const PetscReal r   = PetscRealPart(u[1]);
4334669647acSMatthew G. Knepley   const PetscReal th  = 2. * PETSC_PI * (PetscRealPart(u[0]) - low) / (upp - low);
4335669647acSMatthew G. Knepley 
4336669647acSMatthew G. Knepley   f0[0] = r * PetscCosReal(th);
4337669647acSMatthew G. Knepley   f0[1] = r * PetscSinReal(th);
4338669647acSMatthew G. Knepley }
4339669647acSMatthew G. Knepley 
43405390be7dSMatthew G. Knepley // Insert vertices and their joins, marked by depth
43415390be7dSMatthew G. Knepley static PetscErrorCode ProcessCohesiveLabel_Vertices(DM dm, DMLabel label, DMLabel vlabel, PetscInt val, PetscInt n, const PetscInt vertices[])
43425390be7dSMatthew G. Knepley {
43435390be7dSMatthew G. Knepley   PetscFunctionBegin;
43445390be7dSMatthew G. Knepley   PetscCall(DMPlexMarkSubmesh_Interpolated(dm, vlabel, val, PETSC_FALSE, PETSC_FALSE, label, NULL));
43455390be7dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
43465390be7dSMatthew G. Knepley }
43475390be7dSMatthew G. Knepley 
43485390be7dSMatthew G. Knepley // Insert faces and their closures, marked by depth
43495390be7dSMatthew G. Knepley static PetscErrorCode ProcessCohesiveLabel_Faces(DM dm, DMLabel label, PetscInt n, const PetscInt faces[])
43505390be7dSMatthew G. Knepley {
43515390be7dSMatthew G. Knepley   PetscFunctionBegin;
43525390be7dSMatthew G. Knepley   for (PetscInt p = 0; p < n; ++p) {
43535390be7dSMatthew G. Knepley     const PetscInt point   = faces[p];
43545390be7dSMatthew G. Knepley     PetscInt      *closure = NULL;
43555390be7dSMatthew G. Knepley     PetscInt       clSize, pdepth;
43565390be7dSMatthew G. Knepley 
43575390be7dSMatthew G. Knepley     PetscCall(DMPlexGetPointDepth(dm, point, &pdepth));
43585390be7dSMatthew G. Knepley     PetscCall(DMLabelSetValue(label, point, pdepth));
43595390be7dSMatthew G. Knepley     PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &clSize, &closure));
43605390be7dSMatthew G. Knepley     for (PetscInt cl = 0; cl < clSize * 2; cl += 2) {
43615390be7dSMatthew G. Knepley       PetscCall(DMPlexGetPointDepth(dm, closure[cl], &pdepth));
43625390be7dSMatthew G. Knepley       PetscCall(DMLabelSetValue(label, closure[cl], pdepth));
43635390be7dSMatthew G. Knepley     }
43645390be7dSMatthew G. Knepley     PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &clSize, &closure));
43655390be7dSMatthew G. Knepley   }
43665390be7dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
43675390be7dSMatthew G. Knepley }
43685390be7dSMatthew G. Knepley 
43694e22dd4cSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscOptionsFindPairPrefix_Private(PetscOptions, const char pre[], const char name[], const char *option[], const char *value[], PetscBool *flg);
43704e22dd4cSMatthew G. Knepley 
43715dca41c3SJed Brown const char *const DMPlexShapes[] = {"box", "box_surface", "ball", "sphere", "cylinder", "schwarz_p", "gyroid", "doublet", "annulus", "hypercubic", "zbox", "unknown", "DMPlexShape", "DM_SHAPE_", NULL};
43729318fe57SMatthew G. Knepley 
4373d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateFromOptions_Internal(PetscOptionItems *PetscOptionsObject, PetscBool *useCoordSpace, DM dm)
4374d71ae5a4SJacob Faibussowitsch {
43759318fe57SMatthew G. Knepley   DMPlexShape    shape   = DM_SHAPE_BOX;
43769318fe57SMatthew G. Knepley   DMPolytopeType cell    = DM_POLYTOPE_TRIANGLE;
43779318fe57SMatthew G. Knepley   PetscInt       dim     = 2;
4378b9da1bb3SMatthew G. Knepley   PetscBool      simplex = PETSC_TRUE, interpolate = PETSC_TRUE, orient = PETSC_FALSE, adjCone = PETSC_FALSE, adjClosure = PETSC_TRUE, refDomain = PETSC_FALSE;
4379d0812dedSMatthew G. Knepley   PetscBool      flg, flg2, fflg, strflg, bdfflg, nameflg;
43809318fe57SMatthew G. Knepley   MPI_Comm       comm;
4381ed5e4e85SVaclav Hapla   char           filename[PETSC_MAX_PATH_LEN]   = "<unspecified>";
4382ed5e4e85SVaclav Hapla   char           bdFilename[PETSC_MAX_PATH_LEN] = "<unspecified>";
4383ed5e4e85SVaclav Hapla   char           plexname[PETSC_MAX_PATH_LEN]   = "";
43844e22dd4cSMatthew G. Knepley   const char    *option;
43859318fe57SMatthew G. Knepley 
43869318fe57SMatthew G. Knepley   PetscFunctionBegin;
4387708be2fdSJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_CreateFromOptions, dm, 0, 0, 0));
43889566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
43899318fe57SMatthew G. Knepley   /* TODO Turn this into a registration interface */
43909566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_filename", "File containing a mesh", "DMPlexCreateFromFile", filename, filename, sizeof(filename), &fflg));
4391d0812dedSMatthew G. Knepley   PetscCall(PetscOptionsString("-dm_plex_file_contents", "Contents of a file format in a string", "DMPlexCreateFromFile", filename, filename, sizeof(filename), &strflg));
43929566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_boundary_filename", "File containing a mesh boundary", "DMPlexCreateFromFile", bdFilename, bdFilename, sizeof(bdFilename), &bdfflg));
43939566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_name", "Name of the mesh in the file", "DMPlexCreateFromFile", plexname, plexname, sizeof(plexname), &nameflg));
43949566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_plex_cell", "Cell shape", "", DMPolytopeTypes, (PetscEnum)cell, (PetscEnum *)&cell, NULL));
43959566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_reference_cell_domain", "Use a reference cell domain", "", refDomain, &refDomain, NULL));
43969566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_plex_shape", "Shape for built-in mesh", "", DMPlexShapes, (PetscEnum)shape, (PetscEnum *)&shape, &flg));
43979566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_dim", "Topological dimension of the mesh", "DMGetDimension", dim, &dim, &flg, 0));
43989566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_simplex", "Mesh cell shape", "", simplex, &simplex, &flg));
43999566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_interpolate", "Flag to create edges and faces automatically", "", interpolate, &interpolate, &flg));
4400b9da1bb3SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_plex_orient", "Orient the constructed mesh", "DMPlexOrient", orient, &orient, &flg));
44019566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_adj_cone", "Set adjacency direction", "DMSetBasicAdjacency", adjCone, &adjCone, &flg));
44029566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_adj_closure", "Set adjacency size", "DMSetBasicAdjacency", adjClosure, &adjClosure, &flg2));
44039566063dSJacob Faibussowitsch   if (flg || flg2) PetscCall(DMSetBasicAdjacency(dm, adjCone, adjClosure));
44043f3e541fSMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_adj", "Debug output level all adjacency computations", "", 0, &((DM_Plex *)dm->data)->printAdj, NULL, 0));
44059318fe57SMatthew G. Knepley 
440661a622f3SMatthew G. Knepley   switch (cell) {
440761a622f3SMatthew G. Knepley   case DM_POLYTOPE_POINT:
440861a622f3SMatthew G. Knepley   case DM_POLYTOPE_SEGMENT:
440961a622f3SMatthew G. Knepley   case DM_POLYTOPE_POINT_PRISM_TENSOR:
441061a622f3SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
441161a622f3SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
441261a622f3SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
4413d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_HEXAHEDRON:
4414d71ae5a4SJacob Faibussowitsch     *useCoordSpace = PETSC_TRUE;
4415d71ae5a4SJacob Faibussowitsch     break;
4416d71ae5a4SJacob Faibussowitsch   default:
4417d71ae5a4SJacob Faibussowitsch     *useCoordSpace = PETSC_FALSE;
4418d71ae5a4SJacob Faibussowitsch     break;
441961a622f3SMatthew G. Knepley   }
442061a622f3SMatthew G. Knepley 
44219318fe57SMatthew G. Knepley   if (fflg) {
44229318fe57SMatthew G. Knepley     DM          dmnew;
44231e4a82c4SMatthew G. Knepley     const char *name;
44249318fe57SMatthew G. Knepley 
44251e4a82c4SMatthew G. Knepley     PetscCall(PetscObjectGetName((PetscObject)dm, &name));
44261e4a82c4SMatthew G. Knepley     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), filename, nameflg ? plexname : name, interpolate, &dmnew));
44275de52c6dSVaclav Hapla     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
442869d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &dmnew));
44299318fe57SMatthew G. Knepley   } else if (refDomain) {
44309566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateReferenceCell_Internal(dm, cell));
44319318fe57SMatthew G. Knepley   } else if (bdfflg) {
44329318fe57SMatthew G. Knepley     DM          bdm, dmnew;
44331e4a82c4SMatthew G. Knepley     const char *name;
44349318fe57SMatthew G. Knepley 
44351e4a82c4SMatthew G. Knepley     PetscCall(PetscObjectGetName((PetscObject)dm, &name));
44361e4a82c4SMatthew G. Knepley     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), bdFilename, nameflg ? plexname : name, interpolate, &bdm));
44379566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)bdm, "bd_"));
44389566063dSJacob Faibussowitsch     PetscCall(DMSetFromOptions(bdm));
44399566063dSJacob Faibussowitsch     PetscCall(DMPlexGenerate(bdm, NULL, interpolate, &dmnew));
44409566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&bdm));
44415de52c6dSVaclav Hapla     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
444269d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &dmnew));
4443d0812dedSMatthew G. Knepley   } else if (strflg) {
4444d0812dedSMatthew G. Knepley     DM          dmnew;
4445d0812dedSMatthew G. Knepley     PetscViewer viewer;
4446d0812dedSMatthew G. Knepley     const char *contents;
4447d0812dedSMatthew G. Knepley     char       *strname;
4448d0812dedSMatthew G. Knepley     char        tmpdir[PETSC_MAX_PATH_LEN];
4449d0812dedSMatthew G. Knepley     char        tmpfilename[PETSC_MAX_PATH_LEN];
4450d0812dedSMatthew G. Knepley     char        name[PETSC_MAX_PATH_LEN];
4451d0812dedSMatthew G. Knepley     MPI_Comm    comm;
4452d0812dedSMatthew G. Knepley     PetscMPIInt rank;
4453d0812dedSMatthew G. Knepley 
4454d0812dedSMatthew G. Knepley     PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4455d0812dedSMatthew G. Knepley     PetscCallMPI(MPI_Comm_rank(comm, &rank));
4456d0812dedSMatthew G. Knepley     PetscCall(PetscStrchr(filename, ':', &strname));
4457d0812dedSMatthew G. Knepley     PetscCheck(strname, comm, PETSC_ERR_ARG_WRONG, "File contents must have the form \"ext:string_name\", not %s", filename);
4458d0812dedSMatthew G. Knepley     strname[0] = '\0';
4459d0812dedSMatthew G. Knepley     ++strname;
4460d0812dedSMatthew G. Knepley     PetscCall(PetscDLSym(NULL, strname, (void **)&contents));
4461d0812dedSMatthew G. Knepley     PetscCheck(contents, comm, PETSC_ERR_ARG_WRONG, "Could not locate mesh string %s", strname);
4462d0812dedSMatthew G. Knepley     PetscCall(PetscGetTmp(comm, tmpdir, PETSC_MAX_PATH_LEN));
4463ed32af8cSMatthew G. Knepley     PetscCall(PetscStrlcat(tmpdir, "/meshXXXXXX", PETSC_MAX_PATH_LEN));
4464ed32af8cSMatthew G. Knepley     PetscCall(PetscMkdtemp(tmpdir));
4465ed32af8cSMatthew G. Knepley     PetscCall(PetscSNPrintf(tmpfilename, PETSC_MAX_PATH_LEN, "%s/mesh.%s", tmpdir, filename));
4466d0812dedSMatthew G. Knepley     PetscCall(PetscViewerASCIIOpen(comm, tmpfilename, &viewer));
4467d0812dedSMatthew G. Knepley     PetscCall(PetscViewerASCIIPrintf(viewer, "%s\n", contents));
4468d0812dedSMatthew G. Knepley     PetscCall(PetscViewerDestroy(&viewer));
4469d0812dedSMatthew G. Knepley     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), tmpfilename, plexname, interpolate, &dmnew));
4470ed32af8cSMatthew G. Knepley     PetscCall(PetscRMTree(tmpdir));
4471d0812dedSMatthew G. Knepley     PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN, "%s Mesh", strname));
4472d0812dedSMatthew G. Knepley     PetscCall(PetscObjectSetName((PetscObject)dm, name));
4473d0812dedSMatthew G. Knepley     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
4474d0812dedSMatthew G. Knepley     PetscCall(DMPlexReplace_Internal(dm, &dmnew));
44759318fe57SMatthew G. Knepley   } else {
44769566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)dm, DMPlexShapes[shape]));
44779318fe57SMatthew G. Knepley     switch (shape) {
4478669647acSMatthew G. Knepley     case DM_SHAPE_BOX:
44795dca41c3SJed Brown     case DM_SHAPE_ZBOX:
4480669647acSMatthew G. Knepley     case DM_SHAPE_ANNULUS: {
44819318fe57SMatthew G. Knepley       PetscInt       faces[3]  = {0, 0, 0};
44829318fe57SMatthew G. Knepley       PetscReal      lower[3]  = {0, 0, 0};
44839318fe57SMatthew G. Knepley       PetscReal      upper[3]  = {1, 1, 1};
44849318fe57SMatthew G. Knepley       DMBoundaryType bdt[3]    = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
4485669647acSMatthew G. Knepley       PetscBool      isAnnular = shape == DM_SHAPE_ANNULUS ? PETSC_TRUE : PETSC_FALSE;
44869318fe57SMatthew G. Knepley       PetscInt       i, n;
44879318fe57SMatthew G. Knepley 
44889318fe57SMatthew G. Knepley       n = dim;
44899318fe57SMatthew G. Knepley       for (i = 0; i < dim; ++i) faces[i] = (dim == 1 ? 1 : 4 - dim);
44909566063dSJacob Faibussowitsch       PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg));
44919318fe57SMatthew G. Knepley       n = 3;
44929566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
449363a3b9bcSJacob Faibussowitsch       PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Lower box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
44949318fe57SMatthew G. Knepley       n = 3;
44959566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
449663a3b9bcSJacob Faibussowitsch       PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Upper box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
44979318fe57SMatthew G. Knepley       n = 3;
44989566063dSJacob Faibussowitsch       PetscCall(PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *)bdt, &n, &flg));
449963a3b9bcSJacob Faibussowitsch       PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Box boundary types had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
4500669647acSMatthew G. Knepley 
4501669647acSMatthew G. Knepley       PetscCheck(!isAnnular || dim == 2, comm, PETSC_ERR_ARG_OUTOFRANGE, "Only two dimensional annuli have been implemented");
4502669647acSMatthew G. Knepley       if (isAnnular)
4503669647acSMatthew G. Knepley         for (i = 0; i < dim - 1; ++i) bdt[i] = DM_BOUNDARY_PERIODIC;
4504669647acSMatthew G. Knepley 
45059318fe57SMatthew G. Knepley       switch (cell) {
450661a622f3SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
45079566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateWedgeBoxMesh_Internal(dm, faces, lower, upper, bdt));
4508d410b0cfSMatthew G. Knepley         if (!interpolate) {
4509d410b0cfSMatthew G. Knepley           DM udm;
4510d410b0cfSMatthew G. Knepley 
45119566063dSJacob Faibussowitsch           PetscCall(DMPlexUninterpolate(dm, &udm));
451269d8a87bSksagiyam           PetscCall(DMPlexReplace_Internal(dm, &udm));
4513d410b0cfSMatthew G. Knepley         }
45149318fe57SMatthew G. Knepley         break;
4515d71ae5a4SJacob Faibussowitsch       default:
45165dca41c3SJed Brown         PetscCall(DMPlexCreateBoxMesh_Internal(dm, shape, dim, simplex, faces, lower, upper, bdt, interpolate));
4517d71ae5a4SJacob Faibussowitsch         break;
45189318fe57SMatthew G. Knepley       }
4519669647acSMatthew G. Knepley       if (isAnnular) {
4520669647acSMatthew G. Knepley         DM          cdm;
4521669647acSMatthew G. Knepley         PetscDS     cds;
4522669647acSMatthew G. Knepley         PetscScalar bounds[2] = {lower[0], upper[0]};
4523669647acSMatthew G. Knepley 
4524669647acSMatthew G. Knepley         // Fix coordinates for annular region
4525669647acSMatthew G. Knepley         PetscCall(DMSetPeriodicity(dm, NULL, NULL, NULL));
4526669647acSMatthew G. Knepley         PetscCall(DMSetCellCoordinatesLocal(dm, NULL));
4527669647acSMatthew G. Knepley         PetscCall(DMSetCellCoordinates(dm, NULL));
4528e44f6aebSMatthew G. Knepley         PetscCall(DMPlexCreateCoordinateSpace(dm, 1, PETSC_TRUE, NULL));
4529669647acSMatthew G. Knepley         PetscCall(DMGetCoordinateDM(dm, &cdm));
4530669647acSMatthew G. Knepley         PetscCall(DMGetDS(cdm, &cds));
4531669647acSMatthew G. Knepley         PetscCall(PetscDSSetConstants(cds, 2, bounds));
4532669647acSMatthew G. Knepley         PetscCall(DMPlexRemapGeometry(dm, 0.0, boxToAnnulus));
4533669647acSMatthew G. Knepley       }
45349371c9d4SSatish Balay     } break;
45359371c9d4SSatish Balay     case DM_SHAPE_BOX_SURFACE: {
45369318fe57SMatthew G. Knepley       PetscInt  faces[3] = {0, 0, 0};
45379318fe57SMatthew G. Knepley       PetscReal lower[3] = {0, 0, 0};
45389318fe57SMatthew G. Knepley       PetscReal upper[3] = {1, 1, 1};
45399318fe57SMatthew G. Knepley       PetscInt  i, n;
45409318fe57SMatthew G. Knepley 
45419318fe57SMatthew G. Knepley       n = dim + 1;
45429318fe57SMatthew G. Knepley       for (i = 0; i < dim + 1; ++i) faces[i] = (dim + 1 == 1 ? 1 : 4 - (dim + 1));
45439566063dSJacob Faibussowitsch       PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg));
45449318fe57SMatthew G. Knepley       n = 3;
45459566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
454663a3b9bcSJacob 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);
45479318fe57SMatthew G. Knepley       n = 3;
45489566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
454963a3b9bcSJacob 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);
45509566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(dm, dim + 1, faces, lower, upper, interpolate));
45519371c9d4SSatish Balay     } break;
45529371c9d4SSatish Balay     case DM_SHAPE_SPHERE: {
45539318fe57SMatthew G. Knepley       PetscReal R = 1.0;
45549318fe57SMatthew G. Knepley 
45559566063dSJacob Faibussowitsch       PetscCall(PetscOptionsReal("-dm_plex_sphere_radius", "Radius of the sphere", "", R, &R, &flg));
45569566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateSphereMesh_Internal(dm, dim, simplex, R));
45579371c9d4SSatish Balay     } break;
45589371c9d4SSatish Balay     case DM_SHAPE_BALL: {
45599318fe57SMatthew G. Knepley       PetscReal R = 1.0;
45609318fe57SMatthew G. Knepley 
45619566063dSJacob Faibussowitsch       PetscCall(PetscOptionsReal("-dm_plex_ball_radius", "Radius of the ball", "", R, &R, &flg));
45629566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateBallMesh_Internal(dm, dim, R));
45639371c9d4SSatish Balay     } break;
45649371c9d4SSatish Balay     case DM_SHAPE_CYLINDER: {
45659318fe57SMatthew G. Knepley       DMBoundaryType bdt = DM_BOUNDARY_NONE;
45669318fe57SMatthew G. Knepley       PetscInt       Nw  = 6;
456749704ca5SMatthew G. Knepley       PetscInt       Nr  = 0;
45689318fe57SMatthew G. Knepley 
45699566063dSJacob Faibussowitsch       PetscCall(PetscOptionsEnum("-dm_plex_cylinder_bd", "Boundary type in the z direction", "", DMBoundaryTypes, (PetscEnum)bdt, (PetscEnum *)&bdt, NULL));
45709566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-dm_plex_cylinder_num_wedges", "Number of wedges around the cylinder", "", Nw, &Nw, NULL));
457149704ca5SMatthew G. Knepley       PetscCall(PetscOptionsInt("-dm_plex_cylinder_num_refine", "Number of refinements before projection", "", Nr, &Nr, NULL));
45729318fe57SMatthew G. Knepley       switch (cell) {
4573d71ae5a4SJacob Faibussowitsch       case DM_POLYTOPE_TRI_PRISM_TENSOR:
4574d71ae5a4SJacob Faibussowitsch         PetscCall(DMPlexCreateWedgeCylinderMesh_Internal(dm, Nw, interpolate));
4575d71ae5a4SJacob Faibussowitsch         break;
4576d71ae5a4SJacob Faibussowitsch       default:
457749704ca5SMatthew G. Knepley         PetscCall(DMPlexCreateHexCylinderMesh_Internal(dm, bdt, Nr));
4578d71ae5a4SJacob Faibussowitsch         break;
45799318fe57SMatthew G. Knepley       }
45809371c9d4SSatish Balay     } break;
4581b7f5c055SJed Brown     case DM_SHAPE_SCHWARZ_P: // fallthrough
45829371c9d4SSatish Balay     case DM_SHAPE_GYROID: {
4583b7f5c055SJed Brown       PetscInt       extent[3] = {1, 1, 1}, refine = 0, layers = 0, three;
4584b7f5c055SJed Brown       PetscReal      thickness   = 0.;
4585b7f5c055SJed Brown       DMBoundaryType periodic[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
4586b7f5c055SJed Brown       DMPlexTPSType  tps_type    = shape == DM_SHAPE_SCHWARZ_P ? DMPLEX_TPS_SCHWARZ_P : DMPLEX_TPS_GYROID;
45871436d7faSJed Brown       PetscBool      tps_distribute;
45889566063dSJacob Faibussowitsch       PetscCall(PetscOptionsIntArray("-dm_plex_tps_extent", "Number of replicas for each of three dimensions", NULL, extent, (three = 3, &three), NULL));
45899566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-dm_plex_tps_refine", "Number of refinements", NULL, refine, &refine, NULL));
45909566063dSJacob Faibussowitsch       PetscCall(PetscOptionsEnumArray("-dm_plex_tps_periodic", "Periodicity in each of three dimensions", NULL, DMBoundaryTypes, (PetscEnum *)periodic, (three = 3, &three), NULL));
45919566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-dm_plex_tps_layers", "Number of layers in volumetric extrusion (or zero to not extrude)", NULL, layers, &layers, NULL));
45929566063dSJacob Faibussowitsch       PetscCall(PetscOptionsReal("-dm_plex_tps_thickness", "Thickness of volumetric extrusion", NULL, thickness, &thickness, NULL));
45939566063dSJacob Faibussowitsch       PetscCall(DMPlexDistributeGetDefault(dm, &tps_distribute));
45949566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_tps_distribute", "Distribute the 2D mesh prior to refinement and extrusion", NULL, tps_distribute, &tps_distribute, NULL));
45959566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateTPSMesh_Internal(dm, tps_type, extent, periodic, tps_distribute, refine, layers, thickness));
45969371c9d4SSatish Balay     } break;
45979371c9d4SSatish Balay     case DM_SHAPE_DOUBLET: {
459805bd46c0SStefano Zampini       DM        dmnew;
459905bd46c0SStefano Zampini       PetscReal rl = 0.0;
460005bd46c0SStefano Zampini 
460105bd46c0SStefano Zampini       PetscCall(PetscOptionsReal("-dm_plex_doublet_refinementlimit", "Refinement limit", NULL, rl, &rl, NULL));
460205bd46c0SStefano Zampini       PetscCall(DMPlexCreateDoublet(PetscObjectComm((PetscObject)dm), dim, simplex, interpolate, rl, &dmnew));
46035de52c6dSVaclav Hapla       PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
460469d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &dmnew));
46059371c9d4SSatish Balay     } break;
4606cfb853baSMatthew G. Knepley     case DM_SHAPE_HYPERCUBIC: {
46078d2ec52aSSatish Balay       PetscInt       *edges, overlap = 1;
4608cfb853baSMatthew G. Knepley       PetscReal      *lower, *upper;
4609cfb853baSMatthew G. Knepley       DMBoundaryType *bdt;
4610cfb853baSMatthew G. Knepley       PetscInt        n, d;
4611cfb853baSMatthew G. Knepley 
4612cfb853baSMatthew G. Knepley       *useCoordSpace = PETSC_FALSE;
4613cfb853baSMatthew G. Knepley       PetscCall(PetscMalloc4(dim, &edges, dim, &lower, dim, &upper, dim, &bdt));
4614cfb853baSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
4615cfb853baSMatthew G. Knepley         edges[d] = 1;
4616cfb853baSMatthew G. Knepley         lower[d] = 0.;
4617cfb853baSMatthew G. Knepley         upper[d] = 1.;
4618cfb853baSMatthew G. Knepley         bdt[d]   = DM_BOUNDARY_PERIODIC;
4619cfb853baSMatthew G. Knepley       }
4620cfb853baSMatthew G. Knepley       n = dim;
4621cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", edges, &n, &flg));
4622cfb853baSMatthew G. Knepley       n = dim;
4623cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
4624cfb853baSMatthew 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);
4625cfb853baSMatthew G. Knepley       n = dim;
4626cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
4627cfb853baSMatthew 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);
4628cfb853baSMatthew G. Knepley       n = dim;
4629cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *)bdt, &n, &flg));
4630cfb853baSMatthew 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);
46318d2ec52aSSatish Balay       PetscCall(PetscOptionsBoundedInt("-dm_distribute_overlap", "The size of the overlap halo", "DMPlexDistribute", overlap, &overlap, NULL, 0));
46328d2ec52aSSatish Balay       PetscCall(DMPlexCreateHypercubicMesh_Internal(dm, dim, lower, upper, edges, overlap, bdt));
4633cfb853baSMatthew G. Knepley       PetscCall(PetscFree4(edges, lower, upper, bdt));
4634cfb853baSMatthew G. Knepley     } break;
4635d71ae5a4SJacob Faibussowitsch     default:
4636d71ae5a4SJacob Faibussowitsch       SETERRQ(comm, PETSC_ERR_SUP, "Domain shape %s is unsupported", DMPlexShapes[shape]);
46379318fe57SMatthew G. Knepley     }
46389318fe57SMatthew G. Knepley   }
46399566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
464048a46eb9SPierre Jolivet   if (!((PetscObject)dm)->name && nameflg) PetscCall(PetscObjectSetName((PetscObject)dm, plexname));
4641b9da1bb3SMatthew G. Knepley   if (orient) PetscCall(DMPlexOrient(dm));
46424e22dd4cSMatthew G. Knepley   // Allow label creation
46434e22dd4cSMatthew G. Knepley   PetscCall(PetscOptionsFindPairPrefix_Private(NULL, ((PetscObject)dm)->prefix, "-dm_plex_label_", &option, NULL, &flg));
46444e22dd4cSMatthew G. Knepley   if (flg) {
46454e22dd4cSMatthew G. Knepley     DMLabel     label;
46464e22dd4cSMatthew G. Knepley     PetscInt    points[1024], n = 1024;
46474e22dd4cSMatthew G. Knepley     char        fulloption[PETSC_MAX_PATH_LEN];
46484e22dd4cSMatthew G. Knepley     const char *name = &option[14];
46494e22dd4cSMatthew G. Knepley 
46504e22dd4cSMatthew G. Knepley     PetscCall(DMCreateLabel(dm, name));
46514e22dd4cSMatthew G. Knepley     PetscCall(DMGetLabel(dm, name, &label));
46524e22dd4cSMatthew G. Knepley     fulloption[0] = '-';
46534e22dd4cSMatthew G. Knepley     fulloption[1] = 0;
46544e22dd4cSMatthew G. Knepley     PetscCall(PetscStrlcat(fulloption, option, PETSC_MAX_PATH_LEN));
46554e22dd4cSMatthew G. Knepley     PetscCall(PetscOptionsGetIntArray(NULL, ((PetscObject)dm)->prefix, fulloption, points, &n, NULL));
46564e22dd4cSMatthew G. Knepley     for (PetscInt p = 0; p < n; ++p) PetscCall(DMLabelSetValue(label, points[p], 1));
46574e22dd4cSMatthew G. Knepley   }
4658dd0eeac9SMatthew G. Knepley   // Allow cohesive label creation
4659dd0eeac9SMatthew G. Knepley   //   Faces are input, completed, and all points are marked with their depth
4660dd0eeac9SMatthew G. Knepley   PetscCall(PetscOptionsFindPairPrefix_Private(NULL, ((PetscObject)dm)->prefix, "-dm_plex_cohesive_label_", &option, NULL, &flg));
4661dd0eeac9SMatthew G. Knepley   if (flg) {
4662dd0eeac9SMatthew G. Knepley     DMLabel   label;
4663dd0eeac9SMatthew G. Knepley     PetscInt  points[1024], n, pStart, pEnd, Nl = 1;
46645390be7dSMatthew G. Knepley     PetscBool noCreate = PETSC_FALSE;
4665dd0eeac9SMatthew G. Knepley     char      fulloption[PETSC_MAX_PATH_LEN];
4666dd0eeac9SMatthew G. Knepley     char      name[PETSC_MAX_PATH_LEN];
4667dd0eeac9SMatthew G. Knepley     size_t    len;
4668dd0eeac9SMatthew G. Knepley 
4669dd0eeac9SMatthew G. Knepley     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
4670dd0eeac9SMatthew G. Knepley     PetscCall(PetscStrncpy(name, &option[23], PETSC_MAX_PATH_LEN));
4671dd0eeac9SMatthew G. Knepley     PetscCall(PetscStrlen(name, &len));
4672dd0eeac9SMatthew G. Knepley     if (name[len - 1] == '0') Nl = 10;
4673dd0eeac9SMatthew G. Knepley     for (PetscInt l = 0; l < Nl; ++l) {
46746497c311SBarry Smith       if (l > 0) name[len - 1] = (char)('0' + l);
4675dd0eeac9SMatthew G. Knepley       fulloption[0] = 0;
4676dd0eeac9SMatthew G. Knepley       PetscCall(PetscStrlcat(fulloption, "-dm_plex_cohesive_label_", 32));
4677dd0eeac9SMatthew G. Knepley       PetscCall(PetscStrlcat(fulloption, name, PETSC_MAX_PATH_LEN - 32));
4678dd0eeac9SMatthew G. Knepley       n = 1024;
4679dd0eeac9SMatthew G. Knepley       PetscCall(PetscOptionsGetIntArray(NULL, ((PetscObject)dm)->prefix, fulloption, points, &n, &flg));
4680dd0eeac9SMatthew G. Knepley       if (!flg) break;
46815390be7dSMatthew G. Knepley       PetscCall(DMHasLabel(dm, name, &noCreate));
46825390be7dSMatthew G. Knepley       if (noCreate) {
46835390be7dSMatthew G. Knepley         DMLabel         inlabel;
46845390be7dSMatthew G. Knepley         IS              pointIS;
46855390be7dSMatthew G. Knepley         const PetscInt *lpoints;
46865390be7dSMatthew G. Knepley         PetscInt        pdep, ln, inval = points[0];
46875390be7dSMatthew G. Knepley         char            newname[PETSC_MAX_PATH_LEN];
46885390be7dSMatthew G. Knepley 
46895390be7dSMatthew G. Knepley         PetscCheck(n == 1, comm, PETSC_ERR_ARG_WRONG, "Must specify a label value with this option");
46905390be7dSMatthew G. Knepley         PetscCall(DMGetLabel(dm, name, &inlabel));
46915390be7dSMatthew G. Knepley         PetscCall(DMLabelGetStratumIS(inlabel, inval, &pointIS));
46925390be7dSMatthew G. Knepley         PetscCall(ISGetLocalSize(pointIS, &ln));
46935390be7dSMatthew G. Knepley         PetscCall(ISGetIndices(pointIS, &lpoints));
46945390be7dSMatthew G. Knepley         PetscCall(DMPlexGetPointDepth(dm, lpoints[0], &pdep));
46955390be7dSMatthew G. Knepley         PetscCall(PetscSNPrintf(newname, PETSC_MAX_PATH_LEN, "%s%" PetscInt_FMT, name, points[0]));
46965390be7dSMatthew G. Knepley         PetscCall(DMCreateLabel(dm, newname));
46975390be7dSMatthew G. Knepley         PetscCall(DMGetLabel(dm, newname, &label));
46985390be7dSMatthew G. Knepley         if (!pdep) PetscCall(ProcessCohesiveLabel_Vertices(dm, label, inlabel, inval, ln, lpoints));
46995390be7dSMatthew G. Knepley         else PetscCall(ProcessCohesiveLabel_Faces(dm, label, ln, lpoints));
47005390be7dSMatthew G. Knepley         PetscCall(ISRestoreIndices(pointIS, &lpoints));
47015390be7dSMatthew G. Knepley         PetscCall(ISDestroy(&pointIS));
47025390be7dSMatthew G. Knepley       } else {
4703dd0eeac9SMatthew G. Knepley         PetscCall(DMCreateLabel(dm, name));
4704dd0eeac9SMatthew G. Knepley         PetscCall(DMGetLabel(dm, name, &label));
4705dd0eeac9SMatthew G. Knepley         if (pStart >= pEnd) n = 0;
47065390be7dSMatthew G. Knepley         PetscCall(ProcessCohesiveLabel_Faces(dm, label, n, points));
4707dd0eeac9SMatthew G. Knepley       }
4708dd0eeac9SMatthew G. Knepley       PetscCall(DMPlexOrientLabel(dm, label));
47090542aa8cSMatthew G. Knepley       PetscCall(DMPlexLabelCohesiveComplete(dm, label, NULL, 1, PETSC_FALSE, PETSC_FALSE, NULL));
4710dd0eeac9SMatthew G. Knepley     }
4711dd0eeac9SMatthew G. Knepley   }
47125390be7dSMatthew G. Knepley   PetscCall(DMViewFromOptions(dm, NULL, "-created_dm_view"));
4713708be2fdSJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_CreateFromOptions, dm, 0, 0, 0));
47143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
47150a6ba040SMatthew G. Knepley }
47160a6ba040SMatthew G. Knepley 
4717d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions_NonRefinement_Plex(DM dm, PetscOptionItems *PetscOptionsObject)
4718d71ae5a4SJacob Faibussowitsch {
47190a6ba040SMatthew G. Knepley   DM_Plex  *mesh = (DM_Plex *)dm->data;
47207f9d8d6cSVaclav Hapla   PetscBool flg, flg2;
47219318fe57SMatthew G. Knepley   char      bdLabel[PETSC_MAX_PATH_LEN];
4722adc21957SMatthew G. Knepley   char      method[PETSC_MAX_PATH_LEN];
47230a6ba040SMatthew G. Knepley 
47240a6ba040SMatthew G. Knepley   PetscFunctionBegin;
47250a6ba040SMatthew G. Knepley   /* Handle viewing */
47269566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_print_set_values", "Output all set values info", "DMPlexMatSetClosure", PETSC_FALSE, &mesh->printSetValues, NULL));
47275962854dSMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_fem", "Debug output level for all fem computations", "DMPlexSNESComputeResidualFEM", 0, &mesh->printFEM, NULL, 0));
47285962854dSMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_fvm", "Debug output level for all fvm computations", "DMPlexSNESComputeResidualFVM", 0, &mesh->printFVM, NULL, 0));
47293f3e541fSMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_adj", "Debug output level all adjacency computations", "DMPlexSNESComputeResidualFEM", 0, &mesh->printAdj, NULL, 0));
47309566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-dm_plex_print_tol", "Tolerance for FEM output", "DMPlexSNESComputeResidualFEM", mesh->printTol, &mesh->printTol, NULL));
47319566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_l2", "Debug output level all L2 diff computations", "DMComputeL2Diff", 0, &mesh->printL2, NULL, 0));
4732f5867de0SMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_locate", "Debug output level all point location computations", "DMLocatePoints", 0, &mesh->printLocate, NULL, 0));
4733a77a5016SMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_project", "Debug output level all projection computations", "DMPlexProject", 0, &mesh->printProject, NULL, 0));
47349566063dSJacob Faibussowitsch   PetscCall(DMMonitorSetFromOptions(dm, "-dm_plex_monitor_throughput", "Monitor the simulation throughput", "DMPlexMonitorThroughput", DMPlexMonitorThroughput, NULL, &flg));
47359566063dSJacob Faibussowitsch   if (flg) PetscCall(PetscLogDefaultBegin());
4736*5e2c5519SMatthew G. Knepley   // Interpolation
4737*5e2c5519SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_plex_interpolate_prefer_tensor", "When different orderings exist, prefer the tensor order", "DMPlexSetInterpolationPreferTensor", mesh->interpolatePreferTensor, &mesh->interpolatePreferTensor, NULL));
47389318fe57SMatthew G. Knepley   /* Labeling */
47399566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_boundary_label", "Label to mark the mesh boundary", "", bdLabel, bdLabel, sizeof(bdLabel), &flg));
47409566063dSJacob Faibussowitsch   if (flg) PetscCall(DMPlexCreateBoundaryLabel_Private(dm, bdLabel));
4741953fc75cSMatthew G. Knepley   /* Point Location */
47429566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_hash_location", "Use grid hashing for point location", "DMInterpolate", PETSC_FALSE, &mesh->useHashLocation, NULL));
47430848f4b5SMatthew G. Knepley   /* Partitioning and distribution */
47449566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_partition_balance", "Attempt to evenly divide points on partition boundary between processes", "DMPlexSetPartitionBalance", PETSC_FALSE, &mesh->partitionBalance, NULL));
4745d02c7345SMatthew G. Knepley   /* Reordering */
4746adc21957SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_reorder_section", "Compute point permutation for local section", "DMReorderSectionSetDefault", PETSC_FALSE, &flg2, &flg));
4747adc21957SMatthew G. Knepley   if (flg) PetscCall(DMReorderSectionSetDefault(dm, flg2 ? DM_REORDER_DEFAULT_TRUE : DM_REORDER_DEFAULT_FALSE));
4748adc21957SMatthew G. Knepley   PetscCall(PetscOptionsString("-dm_reorder_section_type", "Reordering method for local section", "DMReorderSectionSetType", method, method, PETSC_MAX_PATH_LEN, &flg));
4749adc21957SMatthew G. Knepley   if (flg) PetscCall(DMReorderSectionSetType(dm, method));
47502e62ab5aSMatthew G. Knepley   /* Generation and remeshing */
47519566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_remesh_bd", "Allow changes to the boundary on remeshing", "DMAdapt", PETSC_FALSE, &mesh->remeshBd, NULL));
4752b29cfa1cSToby Isaac   /* Projection behavior */
4753d5b43468SJose E. Roman   PetscCall(PetscOptionsBoundedInt("-dm_plex_max_projection_height", "Maximum mesh point height used to project locally", "DMPlexSetMaxProjectionHeight", 0, &mesh->maxProjectionHeight, NULL, 0));
47549566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_regular_refinement", "Use special nested projection algorithm for regular refinement", "DMPlexSetRegularRefinement", mesh->regularRefinement, &mesh->regularRefinement, NULL));
4755f12cf164SMatthew G. Knepley   /* Checking structure */
4756f12cf164SMatthew G. Knepley   {
47577f9d8d6cSVaclav Hapla     PetscBool all = PETSC_FALSE;
4758f12cf164SMatthew G. Knepley 
47597f9d8d6cSVaclav Hapla     PetscCall(PetscOptionsBool("-dm_plex_check_all", "Perform all basic checks", "DMPlexCheck", PETSC_FALSE, &all, NULL));
47607f9d8d6cSVaclav Hapla     if (all) {
47617f9d8d6cSVaclav Hapla       PetscCall(DMPlexCheck(dm));
47627f9d8d6cSVaclav Hapla     } else {
47639566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_symmetry", "Check that the adjacency information in the mesh is symmetric", "DMPlexCheckSymmetry", PETSC_FALSE, &flg, &flg2));
47647f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckSymmetry(dm));
47659566063dSJacob 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));
47667f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckSkeleton(dm, 0));
47679566063dSJacob 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));
47687f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckFaces(dm, 0));
47699566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_geometry", "Check that cells have positive volume", "DMPlexCheckGeometry", PETSC_FALSE, &flg, &flg2));
47707f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckGeometry(dm));
47719566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_pointsf", "Check some necessary conditions for PointSF", "DMPlexCheckPointSF", PETSC_FALSE, &flg, &flg2));
4772d7d32a9aSMatthew G. Knepley       if (flg && flg2) PetscCall(DMPlexCheckPointSF(dm, NULL, PETSC_FALSE));
47739566063dSJacob 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));
47747f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckInterfaceCones(dm));
47757f9d8d6cSVaclav Hapla     }
47769566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-dm_plex_check_cell_shape", "Check cell shape", "DMPlexCheckCellShape", PETSC_FALSE, &flg, &flg2));
47779566063dSJacob Faibussowitsch     if (flg && flg2) PetscCall(DMPlexCheckCellShape(dm, PETSC_TRUE, PETSC_DETERMINE));
4778f12cf164SMatthew G. Knepley   }
47799318fe57SMatthew G. Knepley   {
47809318fe57SMatthew G. Knepley     PetscReal scale = 1.0;
47814f3833eaSMatthew G. Knepley 
47829566063dSJacob Faibussowitsch     PetscCall(PetscOptionsReal("-dm_plex_scale", "Scale factor for mesh coordinates", "DMPlexScale", scale, &scale, &flg));
47839318fe57SMatthew G. Knepley     if (flg) {
47849318fe57SMatthew G. Knepley       Vec coordinates, coordinatesLocal;
47859318fe57SMatthew G. Knepley 
47869566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinates(dm, &coordinates));
47879566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinatesLocal(dm, &coordinatesLocal));
47889566063dSJacob Faibussowitsch       PetscCall(VecScale(coordinates, scale));
47899566063dSJacob Faibussowitsch       PetscCall(VecScale(coordinatesLocal, scale));
47909318fe57SMatthew G. Knepley     }
47919318fe57SMatthew G. Knepley   }
47929566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerSetFromOptions(mesh->partitioner));
47933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
479468d4fef7SMatthew G. Knepley }
479568d4fef7SMatthew G. Knepley 
4796d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions_Overlap_Plex(DM dm, PetscOptionItems *PetscOptionsObject, PetscInt *overlap)
4797d71ae5a4SJacob Faibussowitsch {
4798c506a872SMatthew G. Knepley   PetscInt  numOvLabels = 16, numOvExLabels = 16;
4799c506a872SMatthew G. Knepley   char     *ovLabelNames[16], *ovExLabelNames[16];
4800c506a872SMatthew G. Knepley   PetscInt  numOvValues = 16, numOvExValues = 16, l;
4801c506a872SMatthew G. Knepley   PetscBool flg;
4802c506a872SMatthew G. Knepley 
4803c506a872SMatthew G. Knepley   PetscFunctionBegin;
4804c506a872SMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_distribute_overlap", "The size of the overlap halo", "DMPlexDistribute", *overlap, overlap, NULL, 0));
4805c506a872SMatthew G. Knepley   PetscCall(PetscOptionsStringArray("-dm_distribute_overlap_labels", "List of overlap label names", "DMPlexDistribute", ovLabelNames, &numOvLabels, &flg));
4806c506a872SMatthew G. Knepley   if (!flg) numOvLabels = 0;
4807c506a872SMatthew G. Knepley   if (numOvLabels) {
4808c506a872SMatthew G. Knepley     ((DM_Plex *)dm->data)->numOvLabels = numOvLabels;
4809c506a872SMatthew G. Knepley     for (l = 0; l < numOvLabels; ++l) {
4810c506a872SMatthew G. Knepley       PetscCall(DMGetLabel(dm, ovLabelNames[l], &((DM_Plex *)dm->data)->ovLabels[l]));
4811c506a872SMatthew G. Knepley       PetscCheck(((DM_Plex *)dm->data)->ovLabels[l], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid label name %s", ovLabelNames[l]);
4812c506a872SMatthew G. Knepley       PetscCall(PetscFree(ovLabelNames[l]));
4813c506a872SMatthew G. Knepley     }
4814c506a872SMatthew G. Knepley     PetscCall(PetscOptionsIntArray("-dm_distribute_overlap_values", "List of overlap label values", "DMPlexDistribute", ((DM_Plex *)dm->data)->ovValues, &numOvValues, &flg));
4815c506a872SMatthew G. Knepley     if (!flg) numOvValues = 0;
4816c506a872SMatthew 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);
4817c506a872SMatthew G. Knepley 
4818c506a872SMatthew G. Knepley     PetscCall(PetscOptionsStringArray("-dm_distribute_overlap_exclude_labels", "List of overlap exclude label names", "DMPlexDistribute", ovExLabelNames, &numOvExLabels, &flg));
4819c506a872SMatthew G. Knepley     if (!flg) numOvExLabels = 0;
4820c506a872SMatthew G. Knepley     ((DM_Plex *)dm->data)->numOvExLabels = numOvExLabels;
4821c506a872SMatthew G. Knepley     for (l = 0; l < numOvExLabels; ++l) {
4822c506a872SMatthew G. Knepley       PetscCall(DMGetLabel(dm, ovExLabelNames[l], &((DM_Plex *)dm->data)->ovExLabels[l]));
4823c506a872SMatthew G. Knepley       PetscCheck(((DM_Plex *)dm->data)->ovExLabels[l], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid label name %s", ovExLabelNames[l]);
4824c506a872SMatthew G. Knepley       PetscCall(PetscFree(ovExLabelNames[l]));
4825c506a872SMatthew G. Knepley     }
4826c506a872SMatthew G. Knepley     PetscCall(PetscOptionsIntArray("-dm_distribute_overlap_exclude_values", "List of overlap exclude label values", "DMPlexDistribute", ((DM_Plex *)dm->data)->ovExValues, &numOvExValues, &flg));
4827c506a872SMatthew G. Knepley     if (!flg) numOvExValues = 0;
4828c506a872SMatthew 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);
4829c506a872SMatthew G. Knepley   }
48303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4831c506a872SMatthew G. Knepley }
4832c506a872SMatthew G. Knepley 
4833d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetFromOptions_Plex(DM dm, PetscOptionItems *PetscOptionsObject)
4834d71ae5a4SJacob Faibussowitsch {
4835bdf63967SMatthew G. Knepley   PetscFunctionList    ordlist;
4836bdf63967SMatthew G. Knepley   char                 oname[256];
48374e22dd4cSMatthew G. Knepley   char                 sublabelname[PETSC_MAX_PATH_LEN] = "";
4838adc21957SMatthew G. Knepley   DMReorderDefaultFlag reorder;
4839d410b0cfSMatthew G. Knepley   PetscReal            volume    = -1.0;
48409318fe57SMatthew G. Knepley   PetscInt             prerefine = 0, refine = 0, r, coarsen = 0, overlap = 0, extLayers = 0, dim;
48411b742f01SMatthew 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;
484268d4fef7SMatthew G. Knepley 
484368d4fef7SMatthew G. Knepley   PetscFunctionBegin;
4844d0609cedSBarry Smith   PetscOptionsHeadBegin(PetscOptionsObject, "DMPlex Options");
4845dd4c3f67SMatthew G. Knepley   if (dm->cloneOpts) goto non_refine;
48469318fe57SMatthew G. Knepley   /* Handle automatic creation */
48479566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
48486bc1bd01Sksagiyam   if (dim < 0) {
48496bc1bd01Sksagiyam     PetscCall(DMPlexCreateFromOptions_Internal(PetscOptionsObject, &coordSpace, dm));
48506bc1bd01Sksagiyam     created = PETSC_TRUE;
48516bc1bd01Sksagiyam   }
48526bc1bd01Sksagiyam   PetscCall(DMGetDimension(dm, &dim));
4853d89e6e46SMatthew G. Knepley   /* Handle interpolation before distribution */
48549566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_interpolate_pre", "Flag to interpolate mesh before distribution", "", interpolate, &interpolate, &flg));
4855d89e6e46SMatthew G. Knepley   if (flg) {
4856d89e6e46SMatthew G. Knepley     DMPlexInterpolatedFlag interpolated;
4857d89e6e46SMatthew G. Knepley 
48589566063dSJacob Faibussowitsch     PetscCall(DMPlexIsInterpolated(dm, &interpolated));
4859d89e6e46SMatthew G. Knepley     if (interpolated == DMPLEX_INTERPOLATED_FULL && !interpolate) {
4860d89e6e46SMatthew G. Knepley       DM udm;
4861d89e6e46SMatthew G. Knepley 
48629566063dSJacob Faibussowitsch       PetscCall(DMPlexUninterpolate(dm, &udm));
486369d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &udm));
4864d89e6e46SMatthew G. Knepley     } else if (interpolated != DMPLEX_INTERPOLATED_FULL && interpolate) {
4865d89e6e46SMatthew G. Knepley       DM idm;
4866d89e6e46SMatthew G. Knepley 
48679566063dSJacob Faibussowitsch       PetscCall(DMPlexInterpolate(dm, &idm));
486869d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &idm));
4869d89e6e46SMatthew G. Knepley     }
4870d89e6e46SMatthew G. Knepley   }
48714e22dd4cSMatthew G. Knepley   // Handle submesh selection before distribution
48724e22dd4cSMatthew G. Knepley   PetscCall(PetscOptionsString("-dm_plex_submesh", "Label to use for submesh selection", "", sublabelname, sublabelname, PETSC_MAX_PATH_LEN, &flg));
48734e22dd4cSMatthew G. Knepley   if (flg) {
48744e22dd4cSMatthew G. Knepley     DM              subdm;
48754e22dd4cSMatthew G. Knepley     DMLabel         label;
48764e22dd4cSMatthew G. Knepley     IS              valueIS, pointIS;
48774e22dd4cSMatthew G. Knepley     const PetscInt *values, *points;
48784e22dd4cSMatthew G. Knepley     PetscBool       markedFaces = PETSC_FALSE;
48794e22dd4cSMatthew G. Knepley     PetscInt        Nv, value, Np;
48804e22dd4cSMatthew G. Knepley 
48814e22dd4cSMatthew G. Knepley     PetscCall(DMGetLabel(dm, sublabelname, &label));
48824e22dd4cSMatthew G. Knepley     PetscCall(DMLabelGetNumValues(label, &Nv));
48834e22dd4cSMatthew 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);
48844e22dd4cSMatthew G. Knepley     PetscCall(DMLabelGetValueIS(label, &valueIS));
48854e22dd4cSMatthew G. Knepley     PetscCall(ISGetIndices(valueIS, &values));
48864e22dd4cSMatthew G. Knepley     value = values[0];
48874e22dd4cSMatthew G. Knepley     PetscCall(ISRestoreIndices(valueIS, &values));
48884e22dd4cSMatthew G. Knepley     PetscCall(ISDestroy(&valueIS));
48894e22dd4cSMatthew G. Knepley     PetscCall(DMLabelGetStratumSize(label, value, &Np));
48904e22dd4cSMatthew G. Knepley     PetscCall(DMLabelGetStratumIS(label, value, &pointIS));
48914e22dd4cSMatthew G. Knepley     PetscCall(ISGetIndices(pointIS, &points));
48924e22dd4cSMatthew G. Knepley     for (PetscInt p = 0; p < Np; ++p) {
48934e22dd4cSMatthew G. Knepley       PetscInt pdepth;
48944e22dd4cSMatthew G. Knepley 
48954e22dd4cSMatthew G. Knepley       PetscCall(DMPlexGetPointDepth(dm, points[p], &pdepth));
48964e22dd4cSMatthew G. Knepley       if (pdepth) {
48974e22dd4cSMatthew G. Knepley         markedFaces = PETSC_TRUE;
48984e22dd4cSMatthew G. Knepley         break;
48994e22dd4cSMatthew G. Knepley       }
49004e22dd4cSMatthew G. Knepley     }
49014e22dd4cSMatthew G. Knepley     PetscCall(ISRestoreIndices(pointIS, &points));
49024e22dd4cSMatthew G. Knepley     PetscCall(ISDestroy(&pointIS));
49034e22dd4cSMatthew G. Knepley     PetscCall(DMPlexCreateSubmesh(dm, label, value, markedFaces, &subdm));
49044e22dd4cSMatthew G. Knepley     PetscCall(DMPlexReplace_Internal(dm, &subdm));
49054e22dd4cSMatthew G. Knepley     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
49064e22dd4cSMatthew G. Knepley   }
49079b44eab4SMatthew G. Knepley   /* Handle DMPlex refinement before distribution */
49089566063dSJacob Faibussowitsch   PetscCall(DMPlexGetRefinementUniform(dm, &uniformOrig));
49099566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine_pre", "The number of refinements before distribution", "DMCreate", prerefine, &prerefine, NULL, 0));
49109566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_remap_pre", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL));
49119566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_uniform_pre", "Flag for uniform refinement before distribution", "DMCreate", uniform, &uniform, &flg));
49129566063dSJacob Faibussowitsch   if (flg) PetscCall(DMPlexSetRefinementUniform(dm, uniform));
49139566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-dm_refine_volume_limit_pre", "The maximum cell volume after refinement before distribution", "DMCreate", volume, &volume, &flg));
49149318fe57SMatthew G. Knepley   if (flg) {
49159566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementUniform(dm, PETSC_FALSE));
49169566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementLimit(dm, volume));
49179318fe57SMatthew G. Knepley     prerefine = PetscMax(prerefine, 1);
49189318fe57SMatthew G. Knepley   }
4919b23db253SStefano Zampini   if (prerefine) PetscCall(DMLocalizeCoordinates(dm));
49209b44eab4SMatthew G. Knepley   for (r = 0; r < prerefine; ++r) {
49219b44eab4SMatthew G. Knepley     DM             rdm;
49229b44eab4SMatthew G. Knepley     PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc;
49239b44eab4SMatthew G. Knepley 
4924dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
49259566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &rdm));
492669d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &rdm));
4927dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
492861a622f3SMatthew G. Knepley     if (coordFunc && remap) {
49299566063dSJacob Faibussowitsch       PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
49309b44eab4SMatthew G. Knepley       ((DM_Plex *)dm->data)->coordFunc = coordFunc;
49319b44eab4SMatthew G. Knepley     }
49329b44eab4SMatthew G. Knepley   }
49339566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, uniformOrig));
49349318fe57SMatthew G. Knepley   /* Handle DMPlex extrusion before distribution */
49359566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_extrude", "The number of layers to extrude", "", extLayers, &extLayers, NULL, 0));
49369318fe57SMatthew G. Knepley   if (extLayers) {
49379318fe57SMatthew G. Knepley     DM edm;
49389318fe57SMatthew G. Knepley 
49399566063dSJacob Faibussowitsch     PetscCall(DMExtrude(dm, extLayers, &edm));
494069d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &edm));
494148d16a33SMatthew G. Knepley     ((DM_Plex *)dm->data)->coordFunc = NULL;
4942dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
4943d410b0cfSMatthew G. Knepley     extLayers = 0;
49445e17fc22SAidan Hamilton     PetscCall(DMGetDimension(dm, &dim));
49459318fe57SMatthew G. Knepley   }
4946bdf63967SMatthew G. Knepley   /* Handle DMPlex reordering before distribution */
49476bc1bd01Sksagiyam   PetscCall(DMPlexReorderGetDefault(dm, &reorder));
49489566063dSJacob Faibussowitsch   PetscCall(MatGetOrderingList(&ordlist));
49496bc1bd01Sksagiyam   PetscCall(PetscStrncpy(oname, MATORDERINGNATURAL, sizeof(oname)));
49509566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_plex_reorder", "Set mesh reordering type", "DMPlexGetOrdering", ordlist, MATORDERINGNATURAL, oname, sizeof(oname), &flg));
4951adc21957SMatthew G. Knepley   if (reorder == DM_REORDER_DEFAULT_TRUE || flg) {
4952bdf63967SMatthew G. Knepley     DM pdm;
4953bdf63967SMatthew G. Knepley     IS perm;
4954bdf63967SMatthew G. Knepley 
49559566063dSJacob Faibussowitsch     PetscCall(DMPlexGetOrdering(dm, oname, NULL, &perm));
49569566063dSJacob Faibussowitsch     PetscCall(DMPlexPermute(dm, perm, &pdm));
49579566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&perm));
495869d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &pdm));
4959dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
4960bdf63967SMatthew G. Knepley   }
49619b44eab4SMatthew G. Knepley   /* Handle DMPlex distribution */
49629566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeGetDefault(dm, &distribute));
4963c506a872SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_distribute", "Flag to redistribute a mesh among processes", "DMPlexDistribute", distribute, &distribute, NULL));
4964a286e215SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_distribute_save_sf", "Flag to save the migration SF", "DMPlexSetMigrationSF", saveSF, &saveSF, NULL));
4965dbbe0bcdSBarry Smith   PetscCall(DMSetFromOptions_Overlap_Plex(dm, PetscOptionsObject, &overlap));
49669b44eab4SMatthew G. Knepley   if (distribute) {
49679b44eab4SMatthew G. Knepley     DM               pdm = NULL;
49689b44eab4SMatthew G. Knepley     PetscPartitioner part;
4969a286e215SMatthew G. Knepley     PetscSF          sfMigration;
49709b44eab4SMatthew G. Knepley 
49719566063dSJacob Faibussowitsch     PetscCall(DMPlexGetPartitioner(dm, &part));
49729566063dSJacob Faibussowitsch     PetscCall(PetscPartitionerSetFromOptions(part));
4973a286e215SMatthew G. Knepley     PetscCall(DMPlexDistribute(dm, overlap, &sfMigration, &pdm));
49745d2873a6SJames Wright     if (pdm) {
49755d2873a6SJames Wright       // Delete the local section to force the existing one to be rebuilt with the distributed DM
49765d2873a6SJames Wright       PetscCall(DMSetLocalSection(dm, pdm->localSection));
49775d2873a6SJames Wright       PetscCall(DMPlexReplace_Internal(dm, &pdm));
49785d2873a6SJames Wright     }
4979a286e215SMatthew G. Knepley     if (saveSF) PetscCall(DMPlexSetMigrationSF(dm, sfMigration));
4980a286e215SMatthew G. Knepley     PetscCall(PetscSFDestroy(&sfMigration));
49819b44eab4SMatthew G. Knepley   }
49824054ae39SJames Wright 
49834054ae39SJames Wright   {
49844054ae39SJames Wright     PetscBool useBoxLabel = PETSC_FALSE;
49854054ae39SJames Wright     PetscCall(PetscOptionsBool("-dm_plex_box_label", "Create 'Face Sets' assuming boundary faces align with cartesian directions", "DMCreate", useBoxLabel, &useBoxLabel, NULL));
4986d7d2d1d2SJames Wright     if (useBoxLabel) {
4987d7d2d1d2SJames Wright       PetscInt       n      = 3;
4988d7d2d1d2SJames Wright       DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
4989d7d2d1d2SJames Wright 
4990d7d2d1d2SJames Wright       PetscCall(PetscOptionsEnumArray("-dm_plex_box_label_bd", "Boundary type for each dimension when using -dm_plex_box_label", "", DMBoundaryTypes, (PetscEnum *)bdt, &n, &flg));
4991d7d2d1d2SJames 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);
4992d7d2d1d2SJames Wright       PetscCall(DMPlexSetBoxLabel_Internal(dm, bdt));
4993d7d2d1d2SJames Wright     }
49944054ae39SJames Wright   }
4995d2b2dc1eSMatthew G. Knepley   /* Must check CEED options before creating function space for coordinates */
4996d2b2dc1eSMatthew G. Knepley   {
4997d2b2dc1eSMatthew G. Knepley     PetscBool useCeed = PETSC_FALSE, flg;
4998d2b2dc1eSMatthew G. Knepley 
4999d2b2dc1eSMatthew G. Knepley     PetscCall(PetscOptionsBool("-dm_plex_use_ceed", "Use LibCEED as the FEM backend", "DMPlexSetUseCeed", useCeed, &useCeed, &flg));
5000d2b2dc1eSMatthew G. Knepley     if (flg) PetscCall(DMPlexSetUseCeed(dm, useCeed));
5001d2b2dc1eSMatthew G. Knepley   }
50029318fe57SMatthew G. Knepley   /* Create coordinate space */
50039318fe57SMatthew G. Knepley   if (created) {
500461a622f3SMatthew G. Knepley     DM_Plex  *mesh   = (DM_Plex *)dm->data;
5005e44f6aebSMatthew G. Knepley     PetscInt  degree = 1, deg;
50065515ebd3SMatthew G. Knepley     PetscInt  height = 0;
50075515ebd3SMatthew G. Knepley     DM        cdm;
5008c3db174cSMatthew G. Knepley     PetscBool flg, localize = PETSC_TRUE, sparseLocalize = PETSC_TRUE;
50099318fe57SMatthew G. Knepley 
50109566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-dm_coord_space", "Use an FEM space for coordinates", "", coordSpace, &coordSpace, &flg));
50119566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-dm_coord_petscspace_degree", "FEM degree for coordinate space", "", degree, &degree, NULL));
5012e44f6aebSMatthew G. Knepley     PetscCall(DMGetCoordinateDegree_Internal(dm, &deg));
5013e44f6aebSMatthew G. Knepley     if (coordSpace && deg <= 1) PetscCall(DMPlexCreateCoordinateSpace(dm, degree, PETSC_TRUE, mesh->coordFunc));
50145515ebd3SMatthew G. Knepley     PetscCall(DMGetCoordinateDM(dm, &cdm));
501561a622f3SMatthew G. Knepley     if (flg && !coordSpace) {
501661a622f3SMatthew G. Knepley       PetscDS      cds;
501761a622f3SMatthew G. Knepley       PetscObject  obj;
501861a622f3SMatthew G. Knepley       PetscClassId id;
501961a622f3SMatthew G. Knepley 
50209566063dSJacob Faibussowitsch       PetscCall(DMGetDS(cdm, &cds));
50219566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
50229566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
502361a622f3SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
502461a622f3SMatthew G. Knepley         PetscContainer dummy;
502561a622f3SMatthew G. Knepley 
50269566063dSJacob Faibussowitsch         PetscCall(PetscContainerCreate(PETSC_COMM_SELF, &dummy));
50279566063dSJacob Faibussowitsch         PetscCall(PetscObjectSetName((PetscObject)dummy, "coordinates"));
50289566063dSJacob Faibussowitsch         PetscCall(DMSetField(cdm, 0, NULL, (PetscObject)dummy));
50299566063dSJacob Faibussowitsch         PetscCall(PetscContainerDestroy(&dummy));
50309566063dSJacob Faibussowitsch         PetscCall(DMClearDS(cdm));
503161a622f3SMatthew G. Knepley       }
503261a622f3SMatthew G. Knepley       mesh->coordFunc = NULL;
503361a622f3SMatthew G. Knepley     }
5034c3db174cSMatthew G. Knepley     PetscCall(PetscOptionsBool("-dm_localize", "Localize mesh coordinates", "", localize, &localize, NULL));
5035c3db174cSMatthew G. Knepley     PetscCall(PetscOptionsBool("-dm_sparse_localize", "Localize only necessary cells", "DMSetSparseLocalize", sparseLocalize, &sparseLocalize, &flg));
5036c3db174cSMatthew G. Knepley     if (flg) PetscCall(DMSetSparseLocalize(dm, sparseLocalize));
50375515ebd3SMatthew G. Knepley     PetscCall(PetscOptionsInt("-dm_localize_height", "Localize edges and faces in addition to cells", "", height, &height, &flg));
50385515ebd3SMatthew G. Knepley     if (flg) PetscCall(DMPlexSetMaxProjectionHeight(cdm, height));
5039c3db174cSMatthew G. Knepley     if (localize) PetscCall(DMLocalizeCoordinates(dm));
50409318fe57SMatthew G. Knepley   }
504168d4fef7SMatthew G. Knepley   /* Handle DMPlex refinement */
504261a622f3SMatthew G. Knepley   remap = PETSC_TRUE;
50439566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine", "The number of uniform refinements", "DMCreate", refine, &refine, NULL, 0));
50449566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_remap", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL));
50459566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine_hierarchy", "The number of uniform refinements", "DMCreate", refine, &refine, &isHierarchy, 0));
50469566063dSJacob Faibussowitsch   if (refine) PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
504768d4fef7SMatthew G. Knepley   if (refine && isHierarchy) {
5048acdc6f61SToby Isaac     DM *dms, coarseDM;
504968d4fef7SMatthew G. Knepley 
50509566063dSJacob Faibussowitsch     PetscCall(DMGetCoarseDM(dm, &coarseDM));
50519566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)coarseDM));
50529566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(refine, &dms));
50539566063dSJacob Faibussowitsch     PetscCall(DMRefineHierarchy(dm, refine, dms));
505468d4fef7SMatthew G. Knepley     /* Total hack since we do not pass in a pointer */
50559566063dSJacob Faibussowitsch     PetscCall(DMPlexSwap_Static(dm, dms[refine - 1]));
505668d4fef7SMatthew G. Knepley     if (refine == 1) {
50579566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dm, dms[0]));
50589566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dm, PETSC_TRUE));
505968d4fef7SMatthew G. Knepley     } else {
50609566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dm, dms[refine - 2]));
50619566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dm, PETSC_TRUE));
50629566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dms[0], dms[refine - 1]));
50639566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dms[0], PETSC_TRUE));
506468d4fef7SMatthew G. Knepley     }
50659566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dms[refine - 1], coarseDM));
50669566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)coarseDM));
506768d4fef7SMatthew G. Knepley     /* Free DMs */
506868d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
5069dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dms[r], PetscOptionsObject));
50709566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dms[r]));
507168d4fef7SMatthew G. Knepley     }
50729566063dSJacob Faibussowitsch     PetscCall(PetscFree(dms));
507368d4fef7SMatthew G. Knepley   } else {
507468d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
50759318fe57SMatthew G. Knepley       DM             rdm;
507651a74b61SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc;
507768d4fef7SMatthew G. Knepley 
5078dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
50799566063dSJacob Faibussowitsch       PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &rdm));
508068d4fef7SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
508169d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &rdm));
5082dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
508361a622f3SMatthew G. Knepley       if (coordFunc && remap) {
50849566063dSJacob Faibussowitsch         PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
508551a74b61SMatthew G. Knepley         ((DM_Plex *)dm->data)->coordFunc = coordFunc;
508651a74b61SMatthew G. Knepley       }
508768d4fef7SMatthew G. Knepley     }
508868d4fef7SMatthew G. Knepley   }
50893cf6fe12SMatthew G. Knepley   /* Handle DMPlex coarsening */
50909566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_coarsen", "Coarsen the mesh", "DMCreate", coarsen, &coarsen, NULL, 0));
50919566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_coarsen_hierarchy", "The number of coarsenings", "DMCreate", coarsen, &coarsen, &isHierarchy, 0));
5092b653a561SMatthew G. Knepley   if (coarsen && isHierarchy) {
5093b653a561SMatthew G. Knepley     DM *dms;
5094b653a561SMatthew G. Knepley 
50959566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(coarsen, &dms));
50969566063dSJacob Faibussowitsch     PetscCall(DMCoarsenHierarchy(dm, coarsen, dms));
5097b653a561SMatthew G. Knepley     /* Free DMs */
5098b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
5099dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dms[r], PetscOptionsObject));
51009566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dms[r]));
5101b653a561SMatthew G. Knepley     }
51029566063dSJacob Faibussowitsch     PetscCall(PetscFree(dms));
5103b653a561SMatthew G. Knepley   } else {
5104b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
51059318fe57SMatthew G. Knepley       DM             cdm;
51069318fe57SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc;
51073cf6fe12SMatthew G. Knepley 
5108dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
51099566063dSJacob Faibussowitsch       PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &cdm));
51103cf6fe12SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
511169d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &cdm));
5112dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
51139318fe57SMatthew G. Knepley       if (coordFunc) {
51149566063dSJacob Faibussowitsch         PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
51159318fe57SMatthew G. Knepley         ((DM_Plex *)dm->data)->coordFunc = coordFunc;
51169318fe57SMatthew G. Knepley       }
51173cf6fe12SMatthew G. Knepley     }
5118b653a561SMatthew G. Knepley   }
5119be664eb1SMatthew G. Knepley   // Handle coordinate remapping
5120be664eb1SMatthew G. Knepley   remap = PETSC_FALSE;
5121be664eb1SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_coord_remap", "Flag to control coordinate remapping", "", remap, &remap, NULL));
5122be664eb1SMatthew G. Knepley   if (remap) {
5123be664eb1SMatthew G. Knepley     DMPlexCoordMap map     = DM_COORD_MAP_NONE;
5124be664eb1SMatthew G. Knepley     PetscPointFunc mapFunc = NULL;
5125be664eb1SMatthew G. Knepley     PetscScalar    params[16];
5126f45b553cSPierre Jolivet     PetscInt       Np = PETSC_STATIC_ARRAY_LENGTH(params), cdim;
5127be664eb1SMatthew G. Knepley     MPI_Comm       comm;
5128be664eb1SMatthew G. Knepley 
5129be664eb1SMatthew G. Knepley     PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
5130be664eb1SMatthew G. Knepley     PetscCall(DMGetCoordinateDim(dm, &cdim));
5131be664eb1SMatthew G. Knepley     PetscCall(PetscOptionsScalarArray("-dm_coord_map_params", "Parameters for the coordinate remapping", "", params, &Np, &flg));
5132be664eb1SMatthew G. Knepley     if (!flg) Np = 0;
5133be664eb1SMatthew G. Knepley     // TODO Allow user to pass a map function by name
5134be664eb1SMatthew G. Knepley     PetscCall(PetscOptionsEnum("-dm_coord_map", "Coordinate mapping for built-in mesh", "", DMPlexCoordMaps, (PetscEnum)map, (PetscEnum *)&map, &flg));
5135be664eb1SMatthew G. Knepley     if (flg) {
5136be664eb1SMatthew G. Knepley       switch (map) {
5137be664eb1SMatthew G. Knepley       case DM_COORD_MAP_NONE:
5138be664eb1SMatthew G. Knepley         mapFunc = coordMap_identity;
5139be664eb1SMatthew G. Knepley         break;
5140be664eb1SMatthew G. Knepley       case DM_COORD_MAP_SHEAR:
5141be664eb1SMatthew G. Knepley         mapFunc = coordMap_shear;
5142be664eb1SMatthew G. Knepley         if (!Np) {
5143be664eb1SMatthew G. Knepley           Np        = cdim + 1;
5144be664eb1SMatthew G. Knepley           params[0] = 0;
5145be664eb1SMatthew G. Knepley           for (PetscInt d = 1; d <= cdim; ++d) params[d] = 1.0;
5146be664eb1SMatthew G. Knepley         }
5147be664eb1SMatthew 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);
5148be664eb1SMatthew G. Knepley         break;
5149be664eb1SMatthew G. Knepley       case DM_COORD_MAP_FLARE:
5150be664eb1SMatthew G. Knepley         mapFunc = coordMap_flare;
5151be664eb1SMatthew G. Knepley         if (!Np) {
5152be664eb1SMatthew G. Knepley           Np        = cdim + 1;
5153be664eb1SMatthew G. Knepley           params[0] = 0;
5154be664eb1SMatthew G. Knepley           for (PetscInt d = 1; d <= cdim; ++d) params[d] = 1.0;
5155be664eb1SMatthew G. Knepley         }
5156be664eb1SMatthew 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);
5157be664eb1SMatthew G. Knepley         break;
5158be664eb1SMatthew G. Knepley       case DM_COORD_MAP_ANNULUS:
5159be664eb1SMatthew G. Knepley         mapFunc = coordMap_annulus;
5160be664eb1SMatthew G. Knepley         if (!Np) {
5161be664eb1SMatthew G. Knepley           Np        = 2;
5162be664eb1SMatthew G. Knepley           params[0] = 1.;
5163be664eb1SMatthew G. Knepley           params[1] = 2.;
5164be664eb1SMatthew G. Knepley         }
5165be664eb1SMatthew G. Knepley         PetscCheck(Np == 2, comm, PETSC_ERR_ARG_WRONG, "The annulus coordinate map must have 2 parameters, not %" PetscInt_FMT, Np);
5166be664eb1SMatthew G. Knepley         break;
5167be664eb1SMatthew G. Knepley       case DM_COORD_MAP_SHELL:
5168be664eb1SMatthew G. Knepley         mapFunc = coordMap_shell;
5169be664eb1SMatthew G. Knepley         if (!Np) {
5170be664eb1SMatthew G. Knepley           Np        = 2;
5171be664eb1SMatthew G. Knepley           params[0] = 1.;
5172be664eb1SMatthew G. Knepley           params[1] = 2.;
5173be664eb1SMatthew G. Knepley         }
5174be664eb1SMatthew G. Knepley         PetscCheck(Np == 2, comm, PETSC_ERR_ARG_WRONG, "The spherical shell coordinate map must have 2 parameters, not %" PetscInt_FMT, Np);
5175be664eb1SMatthew G. Knepley         break;
5176be664eb1SMatthew G. Knepley       default:
5177be664eb1SMatthew G. Knepley         mapFunc = coordMap_identity;
5178be664eb1SMatthew G. Knepley       }
5179be664eb1SMatthew G. Knepley     }
5180be664eb1SMatthew G. Knepley     if (Np) {
5181be664eb1SMatthew G. Knepley       DM      cdm;
5182be664eb1SMatthew G. Knepley       PetscDS cds;
5183be664eb1SMatthew G. Knepley 
5184be664eb1SMatthew G. Knepley       PetscCall(DMGetCoordinateDM(dm, &cdm));
5185be664eb1SMatthew G. Knepley       PetscCall(DMGetDS(cdm, &cds));
5186be664eb1SMatthew G. Knepley       PetscCall(PetscDSSetConstants(cds, Np, params));
5187be664eb1SMatthew G. Knepley     }
5188be664eb1SMatthew G. Knepley     PetscCall(DMPlexRemapGeometry(dm, 0.0, mapFunc));
5189be664eb1SMatthew G. Knepley   }
5190909dfd52SMatthew G. Knepley   /* Handle ghost cells */
51919566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_create_fv_ghost_cells", "Flag to create finite volume ghost cells on the boundary", "DMCreate", ghostCells, &ghostCells, NULL));
5192909dfd52SMatthew G. Knepley   if (ghostCells) {
5193909dfd52SMatthew G. Knepley     DM   gdm;
5194909dfd52SMatthew G. Knepley     char lname[PETSC_MAX_PATH_LEN];
5195909dfd52SMatthew G. Knepley 
5196909dfd52SMatthew G. Knepley     lname[0] = '\0';
51979566063dSJacob Faibussowitsch     PetscCall(PetscOptionsString("-dm_plex_fv_ghost_cells_label", "Label name for ghost cells boundary", "DMCreate", lname, lname, sizeof(lname), &flg));
51989566063dSJacob Faibussowitsch     PetscCall(DMPlexConstructGhostCells(dm, flg ? lname : NULL, NULL, &gdm));
519969d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &gdm));
5200909dfd52SMatthew G. Knepley   }
52016913077dSMatthew G. Knepley   /* Handle 1D order */
5202adc21957SMatthew G. Knepley   if (reorder != DM_REORDER_DEFAULT_FALSE && dim == 1) {
52036913077dSMatthew G. Knepley     DM           cdm, rdm;
52046913077dSMatthew G. Knepley     PetscDS      cds;
52056913077dSMatthew G. Knepley     PetscObject  obj;
52066913077dSMatthew G. Knepley     PetscClassId id = PETSC_OBJECT_CLASSID;
52076913077dSMatthew G. Knepley     IS           perm;
52086bc1bd01Sksagiyam     PetscInt     Nf;
52096913077dSMatthew G. Knepley     PetscBool    distributed;
52106913077dSMatthew G. Knepley 
52119566063dSJacob Faibussowitsch     PetscCall(DMPlexIsDistributed(dm, &distributed));
52129566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
52139566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
52149566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(cds, &Nf));
52156913077dSMatthew G. Knepley     if (Nf) {
52169566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
52179566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
52186913077dSMatthew G. Knepley     }
52196bc1bd01Sksagiyam     if (!distributed && id != PETSCFE_CLASSID) {
52209566063dSJacob Faibussowitsch       PetscCall(DMPlexGetOrdering1D(dm, &perm));
52219566063dSJacob Faibussowitsch       PetscCall(DMPlexPermute(dm, perm, &rdm));
522269d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &rdm));
52239566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&perm));
52246913077dSMatthew G. Knepley     }
52256913077dSMatthew G. Knepley   }
52263cf6fe12SMatthew G. Knepley /* Handle */
5227dd4c3f67SMatthew G. Knepley non_refine:
5228dbbe0bcdSBarry Smith   PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
522922d6dc08SStefano Zampini   char    *phases[16];
523022d6dc08SStefano Zampini   PetscInt Nphases = 16;
523122d6dc08SStefano Zampini   PetscCall(PetscOptionsStringArray("-dm_plex_option_phases", "Option phase prefixes", "DMSetFromOptions", phases, &Nphases, &flg));
5232d0609cedSBarry Smith   PetscOptionsHeadEnd();
523322d6dc08SStefano Zampini 
523422d6dc08SStefano Zampini   // Phases
523522d6dc08SStefano Zampini   if (flg) {
523622d6dc08SStefano Zampini     const char *oldPrefix;
523722d6dc08SStefano Zampini 
523822d6dc08SStefano Zampini     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, &oldPrefix));
523922d6dc08SStefano Zampini     for (PetscInt ph = 0; ph < Nphases; ++ph) {
524022d6dc08SStefano Zampini       PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)dm, phases[ph]));
524122d6dc08SStefano Zampini       PetscCall(PetscInfo(dm, "Options phase %s for DM %s\n", phases[ph], dm->hdr.name));
524222d6dc08SStefano Zampini       PetscCall(DMSetFromOptions(dm));
524322d6dc08SStefano Zampini       PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, oldPrefix));
524422d6dc08SStefano Zampini       PetscCall(PetscFree(phases[ph]));
524522d6dc08SStefano Zampini     }
524622d6dc08SStefano Zampini   }
52473ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
52480a6ba040SMatthew G. Knepley }
52490a6ba040SMatthew G. Knepley 
5250d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMCreateGlobalVector_Plex(DM dm, Vec *vec)
5251d71ae5a4SJacob Faibussowitsch {
5252552f7358SJed Brown   PetscFunctionBegin;
52539566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector_Section_Private(dm, vec));
52549566063dSJacob Faibussowitsch   /* PetscCall(VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM)); */
52559566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEW, (void (*)(void))VecView_Plex));
52569566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEWNATIVE, (void (*)(void))VecView_Plex_Native));
52579566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOAD, (void (*)(void))VecLoad_Plex));
52589566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOADNATIVE, (void (*)(void))VecLoad_Plex_Native));
52593ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5260552f7358SJed Brown }
5261552f7358SJed Brown 
5262d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMCreateLocalVector_Plex(DM dm, Vec *vec)
5263d71ae5a4SJacob Faibussowitsch {
5264552f7358SJed Brown   PetscFunctionBegin;
52659566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector_Section_Private(dm, vec));
52669566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEW, (void (*)(void))VecView_Plex_Local));
52679566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOAD, (void (*)(void))VecLoad_Plex_Local));
52683ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5269552f7358SJed Brown }
5270552f7358SJed Brown 
5271d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGetDimPoints_Plex(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
5272d71ae5a4SJacob Faibussowitsch {
5273793f3fe5SMatthew G. Knepley   PetscInt depth, d;
5274793f3fe5SMatthew G. Knepley 
5275793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
52769566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
5277793f3fe5SMatthew G. Knepley   if (depth == 1) {
52789566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(dm, &d));
52799566063dSJacob Faibussowitsch     if (dim == 0) PetscCall(DMPlexGetDepthStratum(dm, dim, pStart, pEnd));
52809566063dSJacob Faibussowitsch     else if (dim == d) PetscCall(DMPlexGetDepthStratum(dm, 1, pStart, pEnd));
52819371c9d4SSatish Balay     else {
52829371c9d4SSatish Balay       *pStart = 0;
52839371c9d4SSatish Balay       *pEnd   = 0;
52849371c9d4SSatish Balay     }
5285793f3fe5SMatthew G. Knepley   } else {
52869566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, dim, pStart, pEnd));
5287793f3fe5SMatthew G. Knepley   }
52883ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5289793f3fe5SMatthew G. Knepley }
5290793f3fe5SMatthew G. Knepley 
5291d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGetNeighbors_Plex(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
5292d71ae5a4SJacob Faibussowitsch {
5293502a2867SDave May   PetscSF            sf;
52946497c311SBarry Smith   PetscMPIInt        niranks, njranks;
52956497c311SBarry Smith   PetscInt           n;
52960a19bb7dSprj-   const PetscMPIInt *iranks, *jranks;
52970a19bb7dSprj-   DM_Plex           *data = (DM_Plex *)dm->data;
5298502a2867SDave May 
52992f356facSMatthew G. Knepley   PetscFunctionBegin;
53009566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
53010a19bb7dSprj-   if (!data->neighbors) {
53029566063dSJacob Faibussowitsch     PetscCall(PetscSFSetUp(sf));
53039566063dSJacob Faibussowitsch     PetscCall(PetscSFGetRootRanks(sf, &njranks, &jranks, NULL, NULL, NULL));
53049566063dSJacob Faibussowitsch     PetscCall(PetscSFGetLeafRanks(sf, &niranks, &iranks, NULL, NULL));
53059566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(njranks + niranks + 1, &data->neighbors));
53069566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(data->neighbors + 1, jranks, njranks));
53079566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(data->neighbors + njranks + 1, iranks, niranks));
53080a19bb7dSprj-     n = njranks + niranks;
53099566063dSJacob Faibussowitsch     PetscCall(PetscSortRemoveDupsMPIInt(&n, data->neighbors + 1));
53100a19bb7dSprj-     /* The following cast should never fail: can't have more neighbors than PETSC_MPI_INT_MAX */
53119566063dSJacob Faibussowitsch     PetscCall(PetscMPIIntCast(n, data->neighbors));
53120a19bb7dSprj-   }
53130a19bb7dSprj-   if (nranks) *nranks = data->neighbors[0];
53140a19bb7dSprj-   if (ranks) {
53150a19bb7dSprj-     if (data->neighbors[0]) *ranks = data->neighbors + 1;
53160a19bb7dSprj-     else *ranks = NULL;
53170a19bb7dSprj-   }
53183ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5319502a2867SDave May }
5320502a2867SDave May 
53211eb70e55SToby Isaac PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM, DM, Mat, Vec, Vec);
53221eb70e55SToby Isaac 
5323d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMInitialize_Plex(DM dm)
5324d71ae5a4SJacob Faibussowitsch {
5325552f7358SJed Brown   PetscFunctionBegin;
5326552f7358SJed Brown   dm->ops->view                      = DMView_Plex;
53272c40f234SMatthew G. Knepley   dm->ops->load                      = DMLoad_Plex;
5328552f7358SJed Brown   dm->ops->setfromoptions            = DMSetFromOptions_Plex;
532938221697SMatthew G. Knepley   dm->ops->clone                     = DMClone_Plex;
5330552f7358SJed Brown   dm->ops->setup                     = DMSetUp_Plex;
53311bb6d2a8SBarry Smith   dm->ops->createlocalsection        = DMCreateLocalSection_Plex;
5332adc21957SMatthew G. Knepley   dm->ops->createsectionpermutation  = DMCreateSectionPermutation_Plex;
533366ad2231SToby Isaac   dm->ops->createdefaultconstraints  = DMCreateDefaultConstraints_Plex;
5334552f7358SJed Brown   dm->ops->createglobalvector        = DMCreateGlobalVector_Plex;
5335552f7358SJed Brown   dm->ops->createlocalvector         = DMCreateLocalVector_Plex;
5336184d77edSJed Brown   dm->ops->getlocaltoglobalmapping   = NULL;
53370298fd71SBarry Smith   dm->ops->createfieldis             = NULL;
5338552f7358SJed Brown   dm->ops->createcoordinatedm        = DMCreateCoordinateDM_Plex;
5339f19dbd58SToby Isaac   dm->ops->createcoordinatefield     = DMCreateCoordinateField_Plex;
53400a6ba040SMatthew G. Knepley   dm->ops->getcoloring               = NULL;
5341552f7358SJed Brown   dm->ops->creatematrix              = DMCreateMatrix_Plex;
5342bceba477SMatthew G. Knepley   dm->ops->createinterpolation       = DMCreateInterpolation_Plex;
5343bd041c0cSMatthew G. Knepley   dm->ops->createmassmatrix          = DMCreateMassMatrix_Plex;
5344b4937a87SMatthew G. Knepley   dm->ops->createmassmatrixlumped    = DMCreateMassMatrixLumped_Plex;
53455a84ad33SLisandro Dalcin   dm->ops->createinjection           = DMCreateInjection_Plex;
5346552f7358SJed Brown   dm->ops->refine                    = DMRefine_Plex;
53470a6ba040SMatthew G. Knepley   dm->ops->coarsen                   = DMCoarsen_Plex;
53480a6ba040SMatthew G. Knepley   dm->ops->refinehierarchy           = DMRefineHierarchy_Plex;
5349b653a561SMatthew G. Knepley   dm->ops->coarsenhierarchy          = DMCoarsenHierarchy_Plex;
5350d410b0cfSMatthew G. Knepley   dm->ops->extrude                   = DMExtrude_Plex;
53510298fd71SBarry Smith   dm->ops->globaltolocalbegin        = NULL;
53520298fd71SBarry Smith   dm->ops->globaltolocalend          = NULL;
53530298fd71SBarry Smith   dm->ops->localtoglobalbegin        = NULL;
53540298fd71SBarry Smith   dm->ops->localtoglobalend          = NULL;
5355552f7358SJed Brown   dm->ops->destroy                   = DMDestroy_Plex;
5356552f7358SJed Brown   dm->ops->createsubdm               = DMCreateSubDM_Plex;
53572adcc780SMatthew G. Knepley   dm->ops->createsuperdm             = DMCreateSuperDM_Plex;
5358793f3fe5SMatthew G. Knepley   dm->ops->getdimpoints              = DMGetDimPoints_Plex;
5359552f7358SJed Brown   dm->ops->locatepoints              = DMLocatePoints_Plex;
53600709b2feSToby Isaac   dm->ops->projectfunctionlocal      = DMProjectFunctionLocal_Plex;
53610709b2feSToby Isaac   dm->ops->projectfunctionlabellocal = DMProjectFunctionLabelLocal_Plex;
5362bfc4295aSToby Isaac   dm->ops->projectfieldlocal         = DMProjectFieldLocal_Plex;
53638c6c5593SMatthew G. Knepley   dm->ops->projectfieldlabellocal    = DMProjectFieldLabelLocal_Plex;
5364ece3a9fcSMatthew G. Knepley   dm->ops->projectbdfieldlabellocal  = DMProjectBdFieldLabelLocal_Plex;
53650709b2feSToby Isaac   dm->ops->computel2diff             = DMComputeL2Diff_Plex;
5366b698f381SToby Isaac   dm->ops->computel2gradientdiff     = DMComputeL2GradientDiff_Plex;
53672a16baeaSToby Isaac   dm->ops->computel2fielddiff        = DMComputeL2FieldDiff_Plex;
536828d58a37SPierre Jolivet   dm->ops->getneighbors              = DMGetNeighbors_Plex;
53696c6a6b79SMatthew G. Knepley   dm->ops->getlocalboundingbox       = DMGetLocalBoundingBox_Coordinates;
5370907a3e9cSStefano Zampini   dm->ops->createdomaindecomposition = DMCreateDomainDecomposition_Plex;
5371907a3e9cSStefano Zampini   dm->ops->createddscatters          = DMCreateDomainDecompositionScatters_Plex;
53729566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertBoundaryValues_C", DMPlexInsertBoundaryValues_Plex));
53736c51210dSStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertTimeDerivativeBoundaryValues_C", DMPlexInsertTimeDerivativeBoundaryValues_Plex));
53749566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMSetUpGLVisViewer_C", DMSetUpGLVisViewer_Plex));
53759566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMCreateNeumannOverlap_C", DMCreateNeumannOverlap_Plex));
53769566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeGetDefault_C", DMPlexDistributeGetDefault_Plex));
53779566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeSetDefault_C", DMPlexDistributeSetDefault_Plex));
53786bc1bd01Sksagiyam   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderGetDefault_C", DMPlexReorderGetDefault_Plex));
53796bc1bd01Sksagiyam   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderSetDefault_C", DMPlexReorderSetDefault_Plex));
5380adc21957SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionGetDefault_C", DMReorderSectionGetDefault_Plex));
5381adc21957SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionSetDefault_C", DMReorderSectionSetDefault_Plex));
5382adc21957SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionGetType_C", DMReorderSectionGetType_Plex));
5383adc21957SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionSetType_C", DMReorderSectionSetType_Plex));
53849566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMInterpolateSolution_C", DMInterpolateSolution_Plex));
5385c506a872SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetOverlap_C", DMPlexGetOverlap_Plex));
5386c506a872SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexSetOverlap_C", DMPlexSetOverlap_Plex));
5387d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetUseCeed_C", DMPlexGetUseCeed_Plex));
5388d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexSetUseCeed_C", DMPlexSetUseCeed_Plex));
53893ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5390552f7358SJed Brown }
5391552f7358SJed Brown 
5392d71ae5a4SJacob Faibussowitsch PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm)
5393d71ae5a4SJacob Faibussowitsch {
539463a16f15SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *)dm->data;
53951fca310dSJames Wright   const PetscSF *face_sfs;
53961fca310dSJames Wright   PetscInt       num_face_sfs;
539763a16f15SMatthew G. Knepley 
539863a16f15SMatthew G. Knepley   PetscFunctionBegin;
539963a16f15SMatthew G. Knepley   mesh->refct++;
540063a16f15SMatthew G. Knepley   (*newdm)->data = mesh;
54011fca310dSJames Wright   PetscCall(DMPlexGetIsoperiodicFaceSF(dm, &num_face_sfs, &face_sfs));
54021fca310dSJames Wright   PetscCall(DMPlexSetIsoperiodicFaceSF(*newdm, num_face_sfs, (PetscSF *)face_sfs));
54039566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)*newdm, DMPLEX));
54049566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(*newdm));
54053ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
540663a16f15SMatthew G. Knepley }
540763a16f15SMatthew G. Knepley 
54088818961aSMatthew G Knepley /*MC
5409a1cb98faSBarry Smith   DMPLEX = "plex" - A `DM` object that encapsulates an unstructured mesh, or CW Complex, which can be expressed using a Hasse Diagram.
541020f4b53cSBarry Smith                     In the local representation, `Vec`s contain all unknowns in the interior and shared boundary. This is
54118818961aSMatthew G Knepley                     specified by a PetscSection object. Ownership in the global representation is determined by
5412a1cb98faSBarry Smith                     ownership of the underlying `DMPLEX` points. This is specified by another `PetscSection` object.
54138818961aSMatthew G Knepley 
5414e5893cccSMatthew G. Knepley   Options Database Keys:
5415250712c9SMatthew G. Knepley + -dm_refine_pre                     - Refine mesh before distribution
5416250712c9SMatthew G. Knepley + -dm_refine_uniform_pre             - Choose uniform or generator-based refinement
5417250712c9SMatthew G. Knepley + -dm_refine_volume_limit_pre        - Cell volume limit after pre-refinement using generator
5418250712c9SMatthew G. Knepley . -dm_distribute                     - Distribute mesh across processes
5419250712c9SMatthew G. Knepley . -dm_distribute_overlap             - Number of cells to overlap for distribution
5420250712c9SMatthew G. Knepley . -dm_refine                         - Refine mesh after distribution
5421c3db174cSMatthew G. Knepley . -dm_localize <bool>                - Whether to localize coordinates for periodic meshes
5422c3db174cSMatthew G. Knepley . -dm_sparse_localize <bool>         - Whether to only localize cells on the periodic boundary
5423250712c9SMatthew G. Knepley . -dm_plex_hash_location             - Use grid hashing for point location
5424ddce0771SMatthew G. Knepley . -dm_plex_hash_box_faces <n,m,p>    - The number of divisions in each direction of the grid hash
5425f12cf164SMatthew G. Knepley . -dm_plex_partition_balance         - Attempt to evenly divide points on partition boundary between processes
5426f12cf164SMatthew G. Knepley . -dm_plex_remesh_bd                 - Allow changes to the boundary on remeshing
5427d5b43468SJose E. Roman . -dm_plex_max_projection_height     - Maximum mesh point height used to project locally
5428f12cf164SMatthew G. Knepley . -dm_plex_regular_refinement        - Use special nested projection algorithm for regular refinement
5429d02c7345SMatthew G. Knepley . -dm_plex_reorder_section           - Use specialized blocking if available
5430aaa8cc7dSPierre Jolivet . -dm_plex_check_all                 - Perform all checks below
5431f12cf164SMatthew G. Knepley . -dm_plex_check_symmetry            - Check that the adjacency information in the mesh is symmetric
5432f12cf164SMatthew G. Knepley . -dm_plex_check_skeleton <celltype> - Check that each cell has the correct number of vertices
5433f12cf164SMatthew 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
5434f12cf164SMatthew G. Knepley . -dm_plex_check_geometry            - Check that cells have positive volume
5435f12cf164SMatthew G. Knepley . -dm_view :mesh.tex:ascii_latex     - View the mesh in LaTeX/TikZ
5436e5893cccSMatthew G. Knepley . -dm_plex_view_scale <num>          - Scale the TikZ
54375962854dSMatthew G. Knepley . -dm_plex_print_fem <num>           - View FEM assembly information, such as element vectors and matrices
54385962854dSMatthew G. Knepley - -dm_plex_print_fvm <num>           - View FVM assembly information, such as flux updates
5439e5893cccSMatthew G. Knepley 
54408818961aSMatthew G Knepley   Level: intermediate
54418818961aSMatthew G Knepley 
54421cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMType`, `DMPlexCreate()`, `DMCreate()`, `DMSetType()`, `PetscSection`
54438818961aSMatthew G Knepley M*/
54448818961aSMatthew G Knepley 
5445d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode DMCreate_Plex(DM dm)
5446d71ae5a4SJacob Faibussowitsch {
5447552f7358SJed Brown   DM_Plex *mesh;
5448412e9a14SMatthew G. Knepley   PetscInt unit;
5449552f7358SJed Brown 
5450552f7358SJed Brown   PetscFunctionBegin;
5451f39ec787SMatthew G. Knepley   PetscCall(PetscCitationsRegister(PlexCitation, &Plexcite));
5452552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54534dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&mesh));
5454adc21957SMatthew G. Knepley   dm->reorderSection = DM_REORDER_DEFAULT_NOTSET;
5455552f7358SJed Brown   dm->data           = mesh;
5456552f7358SJed Brown 
5457552f7358SJed Brown   mesh->refct = 1;
54589566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->coneSection));
54599566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->supportSection));
5460552f7358SJed Brown   mesh->refinementUniform       = PETSC_TRUE;
5461552f7358SJed Brown   mesh->refinementLimit         = -1.0;
5462e600fa54SMatthew G. Knepley   mesh->distDefault             = PETSC_TRUE;
5463adc21957SMatthew G. Knepley   mesh->reorderDefault          = DM_REORDER_DEFAULT_NOTSET;
54641d1f2f2aSksagiyam   mesh->distributionName        = NULL;
54657d0f5628SVaclav Hapla   mesh->interpolated            = DMPLEX_INTERPOLATED_INVALID;
54667d0f5628SVaclav Hapla   mesh->interpolatedCollective  = DMPLEX_INTERPOLATED_INVALID;
5467*5e2c5519SMatthew G. Knepley   mesh->interpolatePreferTensor = PETSC_TRUE;
5468552f7358SJed Brown 
54699566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerCreate(PetscObjectComm((PetscObject)dm), &mesh->partitioner));
54702e62ab5aSMatthew G. Knepley   mesh->remeshBd = PETSC_FALSE;
5471d9deefdfSMatthew G. Knepley 
54728865f1eaSKarl Rupp   for (unit = 0; unit < NUM_PETSC_UNITS; ++unit) mesh->scale[unit] = 1.0;
5473552f7358SJed Brown 
5474df0420ecSMatthew G. Knepley   mesh->depthState    = -1;
5475ba2698f1SMatthew G. Knepley   mesh->celltypeState = -1;
54766113b454SMatthew G. Knepley   mesh->printTol      = 1.0e-10;
5477c29ce622SStefano Zampini   mesh->nonempty_comm = MPI_COMM_SELF;
5478552f7358SJed Brown 
54799566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(dm));
54803ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5481552f7358SJed Brown }
5482552f7358SJed Brown 
5483552f7358SJed Brown /*@
5484a1cb98faSBarry Smith   DMPlexCreate - Creates a `DMPLEX` object, which encapsulates an unstructured mesh, or CW complex, which can be expressed using a Hasse Diagram.
5485552f7358SJed Brown 
5486d083f849SBarry Smith   Collective
5487552f7358SJed Brown 
5488552f7358SJed Brown   Input Parameter:
5489a1cb98faSBarry Smith . comm - The communicator for the `DMPLEX` object
5490552f7358SJed Brown 
5491552f7358SJed Brown   Output Parameter:
5492a1cb98faSBarry Smith . mesh - The `DMPLEX` object
5493552f7358SJed Brown 
5494552f7358SJed Brown   Level: beginner
5495552f7358SJed Brown 
549642747ad1SJacob Faibussowitsch .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMType`, `DMCreate()`, `DMSetType()`
5497552f7358SJed Brown @*/
5498d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreate(MPI_Comm comm, DM *mesh)
5499d71ae5a4SJacob Faibussowitsch {
5500552f7358SJed Brown   PetscFunctionBegin;
55014f572ea9SToby Isaac   PetscAssertPointer(mesh, 2);
55029566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, mesh));
55039566063dSJacob Faibussowitsch   PetscCall(DMSetType(*mesh, DMPLEX));
55043ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5505552f7358SJed Brown }
5506552f7358SJed Brown 
5507b09969d6SVaclav Hapla /*@C
5508b0fe842aSMatthew 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
5509a1cb98faSBarry Smith 
551020f4b53cSBarry Smith   Collective; No Fortran Support
5511b09969d6SVaclav Hapla 
5512b09969d6SVaclav Hapla   Input Parameters:
5513a1cb98faSBarry Smith + dm          - The `DM`
5514b09969d6SVaclav Hapla . numCells    - The number of cells owned by this process
5515a1cb98faSBarry Smith . numVertices - The number of vertices to be owned by this process, or `PETSC_DECIDE`
5516a1cb98faSBarry Smith . NVertices   - The global number of vertices, or `PETSC_DETERMINE`
5517b09969d6SVaclav Hapla . numCorners  - The number of vertices for each cell
55185e488331SVaclav Hapla - cells       - An array of numCells*numCorners numbers, the global vertex numbers for each cell
5519b09969d6SVaclav Hapla 
5520be8c289dSNicolas Barral   Output Parameters:
5521a1cb98faSBarry Smith + vertexSF         - (Optional) `PetscSF` describing complete vertex ownership
5522be8c289dSNicolas Barral - verticesAdjSaved - (Optional) vertex adjacency array
5523b09969d6SVaclav Hapla 
5524b09969d6SVaclav Hapla   Level: advanced
5525b09969d6SVaclav Hapla 
5526a1cb98faSBarry Smith   Notes:
5527a1cb98faSBarry Smith   Two triangles sharing a face
5528a1cb98faSBarry Smith .vb
5529a1cb98faSBarry Smith 
5530a1cb98faSBarry Smith         2
5531a1cb98faSBarry Smith       / | \
5532a1cb98faSBarry Smith      /  |  \
5533a1cb98faSBarry Smith     /   |   \
5534a1cb98faSBarry Smith    0  0 | 1  3
5535a1cb98faSBarry Smith     \   |   /
5536a1cb98faSBarry Smith      \  |  /
5537a1cb98faSBarry Smith       \ | /
5538a1cb98faSBarry Smith         1
5539a1cb98faSBarry Smith .ve
5540a1cb98faSBarry Smith   would have input
5541a1cb98faSBarry Smith .vb
5542a1cb98faSBarry Smith   numCells = 2, numVertices = 4
5543a1cb98faSBarry Smith   cells = [0 1 2  1 3 2]
5544a1cb98faSBarry Smith .ve
5545a1cb98faSBarry Smith   which would result in the `DMPLEX`
5546a1cb98faSBarry Smith .vb
5547a1cb98faSBarry Smith 
5548a1cb98faSBarry Smith         4
5549a1cb98faSBarry Smith       / | \
5550a1cb98faSBarry Smith      /  |  \
5551a1cb98faSBarry Smith     /   |   \
5552a1cb98faSBarry Smith    2  0 | 1  5
5553a1cb98faSBarry Smith     \   |   /
5554a1cb98faSBarry Smith      \  |  /
5555a1cb98faSBarry Smith       \ | /
5556a1cb98faSBarry Smith         3
5557a1cb98faSBarry Smith .ve
5558a1cb98faSBarry Smith 
5559a1cb98faSBarry Smith   Vertices are implicitly numbered consecutively 0,...,NVertices.
5560a1cb98faSBarry Smith   Each rank owns a chunk of numVertices consecutive vertices.
5561a1cb98faSBarry Smith   If numVertices is `PETSC_DECIDE`, PETSc will distribute them as evenly as possible using PetscLayout.
5562a1cb98faSBarry Smith   If NVertices is `PETSC_DETERMINE` and numVertices is PETSC_DECIDE, NVertices is computed by PETSc as the maximum vertex index in cells + 1.
5563a1cb98faSBarry Smith   If only NVertices is `PETSC_DETERMINE`, it is computed as the sum of numVertices over all ranks.
5564a1cb98faSBarry Smith 
5565a1cb98faSBarry Smith   The cell distribution is arbitrary non-overlapping, independent of the vertex distribution.
5566a1cb98faSBarry Smith 
55671cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildFromCellList()`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildCoordinatesFromCellListParallel()`,
5568a1cb98faSBarry Smith           `PetscSF`
5569b09969d6SVaclav Hapla @*/
5570d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildFromCellListParallel(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt NVertices, PetscInt numCorners, const PetscInt cells[], PetscSF *vertexSF, PetscInt **verticesAdjSaved)
5571d71ae5a4SJacob Faibussowitsch {
55722464107aSksagiyam   PetscSF     sfPoint;
55732464107aSksagiyam   PetscLayout layout;
557482fb893eSVaclav Hapla   PetscInt    numVerticesAdj, *verticesAdj, *cones, c, p;
5575a47d0d45SMatthew G. Knepley 
5576a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
557725b6865aSVaclav Hapla   PetscValidLogicalCollectiveInt(dm, NVertices, 4);
55789566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
557925b6865aSVaclav Hapla   /* Get/check global number of vertices */
558025b6865aSVaclav Hapla   {
558125b6865aSVaclav Hapla     PetscInt       NVerticesInCells, i;
558225b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
558325b6865aSVaclav Hapla 
558425b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
55851690c2aeSBarry Smith     NVerticesInCells = PETSC_INT_MIN;
55869371c9d4SSatish Balay     for (i = 0; i < len; i++)
55879371c9d4SSatish Balay       if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
558825b6865aSVaclav Hapla     ++NVerticesInCells;
5589462c564dSBarry Smith     PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &NVerticesInCells, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
559025b6865aSVaclav Hapla 
559125b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE && NVertices == PETSC_DECIDE) NVertices = NVerticesInCells;
55929371c9d4SSatish Balay     else
55939371c9d4SSatish 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);
559425b6865aSVaclav Hapla   }
55959079aca8SVaclav Hapla   /* Count locally unique vertices */
55969079aca8SVaclav Hapla   {
55979079aca8SVaclav Hapla     PetscHSetI vhash;
55989079aca8SVaclav Hapla     PetscInt   off = 0;
55999079aca8SVaclav Hapla 
56009566063dSJacob Faibussowitsch     PetscCall(PetscHSetICreate(&vhash));
5601a47d0d45SMatthew G. Knepley     for (c = 0; c < numCells; ++c) {
560248a46eb9SPierre Jolivet       for (p = 0; p < numCorners; ++p) PetscCall(PetscHSetIAdd(vhash, cells[c * numCorners + p]));
5603a47d0d45SMatthew G. Knepley     }
56049566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetSize(vhash, &numVerticesAdj));
56059566063dSJacob Faibussowitsch     if (!verticesAdjSaved) PetscCall(PetscMalloc1(numVerticesAdj, &verticesAdj));
5606ad540459SPierre Jolivet     else verticesAdj = *verticesAdjSaved;
56079566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetElems(vhash, &off, verticesAdj));
56089566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&vhash));
560963a3b9bcSJacob Faibussowitsch     PetscCheck(off == numVerticesAdj, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid number of local vertices %" PetscInt_FMT " should be %" PetscInt_FMT, off, numVerticesAdj);
5610a47d0d45SMatthew G. Knepley   }
56119566063dSJacob Faibussowitsch   PetscCall(PetscSortInt(numVerticesAdj, verticesAdj));
5612a47d0d45SMatthew G. Knepley   /* Create cones */
56139566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, 0, numCells + numVerticesAdj));
56149566063dSJacob Faibussowitsch   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, numCorners));
56159566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm));
56169566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCones(dm, &cones));
5617a47d0d45SMatthew G. Knepley   for (c = 0; c < numCells; ++c) {
5618a47d0d45SMatthew G. Knepley     for (p = 0; p < numCorners; ++p) {
5619a47d0d45SMatthew G. Knepley       const PetscInt gv = cells[c * numCorners + p];
5620a47d0d45SMatthew G. Knepley       PetscInt       lv;
5621a47d0d45SMatthew G. Knepley 
56229079aca8SVaclav Hapla       /* Positions within verticesAdj form 0-based local vertex numbering;
56239079aca8SVaclav Hapla          we need to shift it by numCells to get correct DAG points (cells go first) */
56249566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv));
562563a3b9bcSJacob Faibussowitsch       PetscCheck(lv >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not find global vertex %" PetscInt_FMT " in local connectivity", gv);
5626961cfab0SVaclav Hapla       cones[c * numCorners + p] = lv + numCells;
5627a47d0d45SMatthew G. Knepley     }
5628a47d0d45SMatthew G. Knepley   }
56292464107aSksagiyam   /* Build point sf */
56309566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(PetscObjectComm((PetscObject)dm), &layout));
56319566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetSize(layout, NVertices));
56329566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, numVertices));
56339566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
56349566063dSJacob Faibussowitsch   PetscCall(PetscSFCreateByMatchingIndices(layout, numVerticesAdj, verticesAdj, NULL, numCells, numVerticesAdj, verticesAdj, NULL, numCells, vertexSF, &sfPoint));
56359566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
56369566063dSJacob Faibussowitsch   if (!verticesAdjSaved) PetscCall(PetscFree(verticesAdj));
56379566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)sfPoint, "point SF"));
56382464107aSksagiyam   if (dm->sf) {
56392464107aSksagiyam     const char *prefix;
56402464107aSksagiyam 
56419566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm->sf, &prefix));
56429566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)sfPoint, prefix));
56432464107aSksagiyam   }
56449566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dm, sfPoint));
56459566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&sfPoint));
5646f4f49eeaSPierre Jolivet   if (vertexSF) PetscCall(PetscObjectSetName((PetscObject)*vertexSF, "Vertex Ownership SF"));
5647a47d0d45SMatthew G. Knepley   /* Fill in the rest of the topology structure */
56489566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
56499566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
56509566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
56513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5652a47d0d45SMatthew G. Knepley }
5653a47d0d45SMatthew G. Knepley 
5654b0fe842aSMatthew G. Knepley /*@C
5655b0fe842aSMatthew G. Knepley   DMPlexBuildFromCellSectionParallel - Build distributed `DMPLEX` topology from a list of vertices for each cell (common mesh generator output) allowing multiple celltypes
5656b0fe842aSMatthew G. Knepley 
5657b0fe842aSMatthew G. Knepley   Collective; No Fortran Support
5658b0fe842aSMatthew G. Knepley 
5659b0fe842aSMatthew G. Knepley   Input Parameters:
5660b0fe842aSMatthew G. Knepley + dm          - The `DM`
5661b0fe842aSMatthew G. Knepley . numCells    - The number of cells owned by this process
5662b0fe842aSMatthew G. Knepley . numVertices - The number of vertices to be owned by this process, or `PETSC_DECIDE`
5663b0fe842aSMatthew G. Knepley . NVertices   - The global number of vertices, or `PETSC_DETERMINE`
5664b0fe842aSMatthew G. Knepley . cellSection - The `PetscSection` giving the number of vertices for each cell (layout of cells)
5665b0fe842aSMatthew G. Knepley - cells       - An array of the global vertex numbers for each cell
5666b0fe842aSMatthew G. Knepley 
5667b0fe842aSMatthew G. Knepley   Output Parameters:
5668b0fe842aSMatthew G. Knepley + vertexSF         - (Optional) `PetscSF` describing complete vertex ownership
5669b0fe842aSMatthew G. Knepley - verticesAdjSaved - (Optional) vertex adjacency array
5670b0fe842aSMatthew G. Knepley 
5671b0fe842aSMatthew G. Knepley   Level: advanced
5672b0fe842aSMatthew G. Knepley 
5673b0fe842aSMatthew G. Knepley   Notes:
5674b0fe842aSMatthew G. Knepley   A triangle and quadrilateral sharing a face
5675b0fe842aSMatthew G. Knepley .vb
5676b0fe842aSMatthew G. Knepley         2----------3
5677b0fe842aSMatthew G. Knepley       / |          |
5678b0fe842aSMatthew G. Knepley      /  |          |
5679b0fe842aSMatthew G. Knepley     /   |          |
5680b0fe842aSMatthew G. Knepley    0  0 |     1    |
5681b0fe842aSMatthew G. Knepley     \   |          |
5682b0fe842aSMatthew G. Knepley      \  |          |
5683b0fe842aSMatthew G. Knepley       \ |          |
5684b0fe842aSMatthew G. Knepley         1----------4
5685b0fe842aSMatthew G. Knepley .ve
5686b0fe842aSMatthew G. Knepley   would have input
5687b0fe842aSMatthew G. Knepley .vb
5688b0fe842aSMatthew G. Knepley   numCells = 2, numVertices = 5
5689b0fe842aSMatthew G. Knepley   cells = [0 1 2  1 4 3 2]
5690b0fe842aSMatthew G. Knepley .ve
5691b0fe842aSMatthew G. Knepley   which would result in the `DMPLEX`
5692b0fe842aSMatthew G. Knepley .vb
5693b0fe842aSMatthew G. Knepley         4----------5
5694b0fe842aSMatthew G. Knepley       / |          |
5695b0fe842aSMatthew G. Knepley      /  |          |
5696b0fe842aSMatthew G. Knepley     /   |          |
5697b0fe842aSMatthew G. Knepley    2  0 |     1    |
5698b0fe842aSMatthew G. Knepley     \   |          |
5699b0fe842aSMatthew G. Knepley      \  |          |
5700b0fe842aSMatthew G. Knepley       \ |          |
5701b0fe842aSMatthew G. Knepley         3----------6
5702b0fe842aSMatthew G. Knepley .ve
5703b0fe842aSMatthew G. Knepley 
5704b0fe842aSMatthew G. Knepley   Vertices are implicitly numbered consecutively 0,...,NVertices.
5705b0fe842aSMatthew G. Knepley   Each rank owns a chunk of numVertices consecutive vertices.
5706b0fe842aSMatthew G. Knepley   If numVertices is `PETSC_DECIDE`, PETSc will distribute them as evenly as possible using PetscLayout.
5707b0fe842aSMatthew 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.
5708b0fe842aSMatthew G. Knepley   If only NVertices is `PETSC_DETERMINE`, it is computed as the sum of numVertices over all ranks.
5709b0fe842aSMatthew G. Knepley 
5710b0fe842aSMatthew G. Knepley   The cell distribution is arbitrary non-overlapping, independent of the vertex distribution.
5711b0fe842aSMatthew G. Knepley 
5712b0fe842aSMatthew G. Knepley .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildFromCellListParallel()`, `DMPlexCreateFromCellSectionParallel()`, `DMPlexBuildCoordinatesFromCellListParallel()`,
5713b0fe842aSMatthew G. Knepley           `PetscSF`
5714b0fe842aSMatthew G. Knepley @*/
5715b0fe842aSMatthew G. Knepley PetscErrorCode DMPlexBuildFromCellSectionParallel(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt NVertices, PetscSection cellSection, const PetscInt cells[], PetscSF *vertexSF, PetscInt **verticesAdjSaved)
5716b0fe842aSMatthew G. Knepley {
5717b0fe842aSMatthew G. Knepley   PetscSF     sfPoint;
5718b0fe842aSMatthew G. Knepley   PetscLayout layout;
5719b0fe842aSMatthew G. Knepley   PetscInt    numVerticesAdj, *verticesAdj, *cones, cStart, cEnd, len;
5720b0fe842aSMatthew G. Knepley 
5721b0fe842aSMatthew G. Knepley   PetscFunctionBegin;
5722b0fe842aSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, NVertices, 4);
5723b0fe842aSMatthew G. Knepley   PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
5724b0fe842aSMatthew G. Knepley   PetscCall(PetscSectionGetChart(cellSection, &cStart, &cEnd));
5725b0fe842aSMatthew G. Knepley   PetscCall(PetscSectionGetStorageSize(cellSection, &len));
5726b0fe842aSMatthew 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);
5727b0fe842aSMatthew G. Knepley   /* Get/check global number of vertices */
5728b0fe842aSMatthew G. Knepley   {
5729b0fe842aSMatthew G. Knepley     PetscInt NVerticesInCells;
5730b0fe842aSMatthew G. Knepley 
5731b0fe842aSMatthew G. Knepley     /* NVerticesInCells = max(cells) + 1 */
5732b0fe842aSMatthew G. Knepley     NVerticesInCells = PETSC_MIN_INT;
5733b0fe842aSMatthew G. Knepley     for (PetscInt i = 0; i < len; i++)
5734b0fe842aSMatthew G. Knepley       if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
5735b0fe842aSMatthew G. Knepley     ++NVerticesInCells;
5736b0fe842aSMatthew G. Knepley     PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &NVerticesInCells, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
5737b0fe842aSMatthew G. Knepley 
5738b0fe842aSMatthew G. Knepley     if (numVertices == PETSC_DECIDE && NVertices == PETSC_DECIDE) NVertices = NVerticesInCells;
5739b0fe842aSMatthew G. Knepley     else
5740b0fe842aSMatthew 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);
5741b0fe842aSMatthew G. Knepley   }
5742b0fe842aSMatthew G. Knepley   /* Count locally unique vertices */
5743b0fe842aSMatthew G. Knepley   {
5744b0fe842aSMatthew G. Knepley     PetscHSetI vhash;
5745b0fe842aSMatthew G. Knepley     PetscInt   off = 0;
5746b0fe842aSMatthew G. Knepley 
5747b0fe842aSMatthew G. Knepley     PetscCall(PetscHSetICreate(&vhash));
5748b0fe842aSMatthew G. Knepley     for (PetscInt i = 0; i < len; i++) PetscCall(PetscHSetIAdd(vhash, cells[i]));
5749b0fe842aSMatthew G. Knepley     PetscCall(PetscHSetIGetSize(vhash, &numVerticesAdj));
5750b0fe842aSMatthew G. Knepley     if (!verticesAdjSaved) PetscCall(PetscMalloc1(numVerticesAdj, &verticesAdj));
5751b0fe842aSMatthew G. Knepley     else verticesAdj = *verticesAdjSaved;
5752b0fe842aSMatthew G. Knepley     PetscCall(PetscHSetIGetElems(vhash, &off, verticesAdj));
5753b0fe842aSMatthew G. Knepley     PetscCall(PetscHSetIDestroy(&vhash));
5754b0fe842aSMatthew 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);
5755b0fe842aSMatthew G. Knepley   }
5756b0fe842aSMatthew G. Knepley   PetscCall(PetscSortInt(numVerticesAdj, verticesAdj));
5757b0fe842aSMatthew G. Knepley   /* Create cones */
5758b0fe842aSMatthew G. Knepley   PetscCall(DMPlexSetChart(dm, 0, numCells + numVerticesAdj));
5759b0fe842aSMatthew G. Knepley   for (PetscInt c = 0; c < numCells; ++c) {
5760b0fe842aSMatthew G. Knepley     PetscInt dof;
5761b0fe842aSMatthew G. Knepley 
5762b0fe842aSMatthew G. Knepley     PetscCall(PetscSectionGetDof(cellSection, c, &dof));
5763b0fe842aSMatthew G. Knepley     PetscCall(DMPlexSetConeSize(dm, c, dof));
5764b0fe842aSMatthew G. Knepley   }
5765b0fe842aSMatthew G. Knepley   PetscCall(DMSetUp(dm));
5766b0fe842aSMatthew G. Knepley   PetscCall(DMPlexGetCones(dm, &cones));
5767b0fe842aSMatthew G. Knepley   for (PetscInt c = 0; c < numCells; ++c) {
5768b0fe842aSMatthew G. Knepley     PetscInt dof, off;
5769b0fe842aSMatthew G. Knepley 
5770b0fe842aSMatthew G. Knepley     PetscCall(PetscSectionGetDof(cellSection, c, &dof));
5771b0fe842aSMatthew G. Knepley     PetscCall(PetscSectionGetOffset(cellSection, c, &off));
5772b0fe842aSMatthew G. Knepley     for (PetscInt p = off; p < off + dof; ++p) {
5773b0fe842aSMatthew G. Knepley       const PetscInt gv = cells[p];
5774b0fe842aSMatthew G. Knepley       PetscInt       lv;
5775b0fe842aSMatthew G. Knepley 
5776b0fe842aSMatthew G. Knepley       /* Positions within verticesAdj form 0-based local vertex numbering;
5777b0fe842aSMatthew G. Knepley          we need to shift it by numCells to get correct DAG points (cells go first) */
5778b0fe842aSMatthew G. Knepley       PetscCall(PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv));
5779b0fe842aSMatthew G. Knepley       PetscCheck(lv >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not find global vertex %" PetscInt_FMT " in local connectivity", gv);
5780b0fe842aSMatthew G. Knepley       cones[p] = lv + numCells;
5781b0fe842aSMatthew G. Knepley     }
5782b0fe842aSMatthew G. Knepley   }
5783b0fe842aSMatthew G. Knepley   /* Build point sf */
5784b0fe842aSMatthew G. Knepley   PetscCall(PetscLayoutCreate(PetscObjectComm((PetscObject)dm), &layout));
5785b0fe842aSMatthew G. Knepley   PetscCall(PetscLayoutSetSize(layout, NVertices));
5786b0fe842aSMatthew G. Knepley   PetscCall(PetscLayoutSetLocalSize(layout, numVertices));
5787b0fe842aSMatthew G. Knepley   PetscCall(PetscLayoutSetBlockSize(layout, 1));
5788b0fe842aSMatthew G. Knepley   PetscCall(PetscSFCreateByMatchingIndices(layout, numVerticesAdj, verticesAdj, NULL, numCells, numVerticesAdj, verticesAdj, NULL, numCells, vertexSF, &sfPoint));
5789b0fe842aSMatthew G. Knepley   PetscCall(PetscLayoutDestroy(&layout));
5790b0fe842aSMatthew G. Knepley   if (!verticesAdjSaved) PetscCall(PetscFree(verticesAdj));
5791b0fe842aSMatthew G. Knepley   PetscCall(PetscObjectSetName((PetscObject)sfPoint, "point SF"));
5792b0fe842aSMatthew G. Knepley   if (dm->sf) {
5793b0fe842aSMatthew G. Knepley     const char *prefix;
5794b0fe842aSMatthew G. Knepley 
5795b0fe842aSMatthew G. Knepley     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm->sf, &prefix));
5796b0fe842aSMatthew G. Knepley     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)sfPoint, prefix));
5797b0fe842aSMatthew G. Knepley   }
5798b0fe842aSMatthew G. Knepley   PetscCall(DMSetPointSF(dm, sfPoint));
5799b0fe842aSMatthew G. Knepley   PetscCall(PetscSFDestroy(&sfPoint));
5800b0fe842aSMatthew G. Knepley   if (vertexSF) PetscCall(PetscObjectSetName((PetscObject)*vertexSF, "Vertex Ownership SF"));
5801b0fe842aSMatthew G. Knepley   /* Fill in the rest of the topology structure */
5802b0fe842aSMatthew G. Knepley   PetscCall(DMPlexSymmetrize(dm));
5803b0fe842aSMatthew G. Knepley   PetscCall(DMPlexStratify(dm));
5804b0fe842aSMatthew G. Knepley   PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
5805b0fe842aSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
5806b0fe842aSMatthew G. Knepley }
5807b0fe842aSMatthew G. Knepley 
5808cc4c1da9SBarry Smith /*@
5809a1cb98faSBarry Smith   DMPlexBuildCoordinatesFromCellListParallel - Build `DM` coordinates from a list of coordinates for each owned vertex (common mesh generator output)
5810a1cb98faSBarry Smith 
581120f4b53cSBarry Smith   Collective; No Fortran Support
5812b09969d6SVaclav Hapla 
5813b09969d6SVaclav Hapla   Input Parameters:
5814a1cb98faSBarry Smith + dm           - The `DM`
5815b09969d6SVaclav Hapla . spaceDim     - The spatial dimension used for coordinates
5816a1cb98faSBarry Smith . sfVert       - `PetscSF` describing complete vertex ownership
5817b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
5818b09969d6SVaclav Hapla 
5819b09969d6SVaclav Hapla   Level: advanced
5820b09969d6SVaclav Hapla 
58211cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildFromCellListParallel()`
5822b09969d6SVaclav Hapla @*/
5823d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildCoordinatesFromCellListParallel(DM dm, PetscInt spaceDim, PetscSF sfVert, const PetscReal vertexCoords[])
5824d71ae5a4SJacob Faibussowitsch {
5825a47d0d45SMatthew G. Knepley   PetscSection coordSection;
5826a47d0d45SMatthew G. Knepley   Vec          coordinates;
5827a47d0d45SMatthew G. Knepley   PetscScalar *coords;
58281edcf0b2SVaclav Hapla   PetscInt     numVertices, numVerticesAdj, coordSize, v, vStart, vEnd;
5829835f2295SStefano Zampini   PetscMPIInt  spaceDimi;
5830a47d0d45SMatthew G. Knepley 
5831a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
58329566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
58339566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
58341dca8a05SBarry Smith   PetscCheck(vStart >= 0 && vEnd >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
58359566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, spaceDim));
58369566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfVert, &numVertices, &numVerticesAdj, NULL, NULL));
58371dca8a05SBarry 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);
58389566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
58399566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
58409566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, spaceDim));
58419566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, vStart, vEnd));
58421edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
58439566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, spaceDim));
58449566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, spaceDim));
5845a47d0d45SMatthew G. Knepley   }
58469566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
58479566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
58489566063dSJacob Faibussowitsch   PetscCall(VecCreate(PetscObjectComm((PetscObject)dm), &coordinates));
58499566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, spaceDim));
58509566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
58519566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
58529566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
58539566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
5854a47d0d45SMatthew G. Knepley   {
5855a47d0d45SMatthew G. Knepley     MPI_Datatype coordtype;
5856a47d0d45SMatthew G. Knepley 
5857a47d0d45SMatthew G. Knepley     /* Need a temp buffer for coords if we have complex/single */
5858835f2295SStefano Zampini     PetscCall(PetscMPIIntCast(spaceDim, &spaceDimi));
5859835f2295SStefano Zampini     PetscCallMPI(MPI_Type_contiguous(spaceDimi, MPIU_SCALAR, &coordtype));
58609566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_commit(&coordtype));
586121016a8bSBarry Smith #if defined(PETSC_USE_COMPLEX)
586221016a8bSBarry Smith     {
586321016a8bSBarry Smith       PetscScalar *svertexCoords;
586421016a8bSBarry Smith       PetscInt     i;
58659566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numVertices * spaceDim, &svertexCoords));
58663612f820SVaclav Hapla       for (i = 0; i < numVertices * spaceDim; i++) svertexCoords[i] = vertexCoords[i];
58679566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(sfVert, coordtype, svertexCoords, coords, MPI_REPLACE));
58689566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(sfVert, coordtype, svertexCoords, coords, MPI_REPLACE));
58699566063dSJacob Faibussowitsch       PetscCall(PetscFree(svertexCoords));
587021016a8bSBarry Smith     }
587121016a8bSBarry Smith #else
58729566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sfVert, coordtype, vertexCoords, coords, MPI_REPLACE));
58739566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sfVert, coordtype, vertexCoords, coords, MPI_REPLACE));
587421016a8bSBarry Smith #endif
58759566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_free(&coordtype));
5876a47d0d45SMatthew G. Knepley   }
58779566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
58789566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
58799566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
58809566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
58813ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5882a47d0d45SMatthew G. Knepley }
5883a47d0d45SMatthew G. Knepley 
5884c3edce3dSSatish Balay /*@
5885b0fe842aSMatthew 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
5886a1cb98faSBarry Smith 
5887a1cb98faSBarry Smith   Collective
5888a47d0d45SMatthew G. Knepley 
5889a47d0d45SMatthew G. Knepley   Input Parameters:
5890a47d0d45SMatthew G. Knepley + comm         - The communicator
5891a47d0d45SMatthew G. Knepley . dim          - The topological dimension of the mesh
5892a47d0d45SMatthew G. Knepley . numCells     - The number of cells owned by this process
5893a1cb98faSBarry Smith . numVertices  - The number of vertices owned by this process, or `PETSC_DECIDE`
5894a1cb98faSBarry Smith . NVertices    - The global number of vertices, or `PETSC_DECIDE`
5895a47d0d45SMatthew G. Knepley . numCorners   - The number of vertices for each cell
5896a47d0d45SMatthew G. Knepley . interpolate  - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
5897a47d0d45SMatthew G. Knepley . cells        - An array of numCells*numCorners numbers, the global vertex numbers for each cell
5898a47d0d45SMatthew G. Knepley . spaceDim     - The spatial dimension used for coordinates
5899a47d0d45SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
5900a47d0d45SMatthew G. Knepley 
5901d8d19677SJose E. Roman   Output Parameters:
5902a1cb98faSBarry Smith + dm          - The `DM`
5903a1cb98faSBarry Smith . vertexSF    - (Optional) `PetscSF` describing complete vertex ownership
590460225df5SJacob Faibussowitsch - verticesAdj - (Optional) vertex adjacency array
5905a47d0d45SMatthew G. Knepley 
5906b09969d6SVaclav Hapla   Level: intermediate
5907a47d0d45SMatthew G. Knepley 
5908a1cb98faSBarry Smith   Notes:
5909a1cb98faSBarry Smith   This function is just a convenient sequence of `DMCreate()`, `DMSetType()`, `DMSetDimension()`,
5910a1cb98faSBarry Smith   `DMPlexBuildFromCellListParallel()`, `DMPlexInterpolate()`, `DMPlexBuildCoordinatesFromCellListParallel()`
5911a1cb98faSBarry Smith 
5912a1cb98faSBarry Smith   See `DMPlexBuildFromCellListParallel()` for an example and details about the topology-related parameters.
5913a1cb98faSBarry Smith 
5914a1cb98faSBarry Smith   See `DMPlexBuildCoordinatesFromCellListParallel()` for details about the geometry-related parameters.
5915a1cb98faSBarry Smith 
59161cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()`
5917a47d0d45SMatthew G. Knepley @*/
5918d71ae5a4SJacob 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)
5919d71ae5a4SJacob Faibussowitsch {
5920a47d0d45SMatthew G. Knepley   PetscSF sfVert;
5921a47d0d45SMatthew G. Knepley 
5922a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
59239566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
59249566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
5925a47d0d45SMatthew G. Knepley   PetscValidLogicalCollectiveInt(*dm, dim, 2);
5926064a246eSJacob Faibussowitsch   PetscValidLogicalCollectiveInt(*dm, spaceDim, 9);
59279566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
59289566063dSJacob Faibussowitsch   PetscCall(DMPlexBuildFromCellListParallel(*dm, numCells, numVertices, NVertices, numCorners, cells, &sfVert, verticesAdj));
5929a47d0d45SMatthew G. Knepley   if (interpolate) {
59305fd9971aSMatthew G. Knepley     DM idm;
5931a47d0d45SMatthew G. Knepley 
59329566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
59339566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
5934a47d0d45SMatthew G. Knepley     *dm = idm;
5935a47d0d45SMatthew G. Knepley   }
59369566063dSJacob Faibussowitsch   PetscCall(DMPlexBuildCoordinatesFromCellListParallel(*dm, spaceDim, sfVert, vertexCoords));
593718d54ad4SMichael Lange   if (vertexSF) *vertexSF = sfVert;
59389566063dSJacob Faibussowitsch   else PetscCall(PetscSFDestroy(&sfVert));
59393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5940a47d0d45SMatthew G. Knepley }
5941a47d0d45SMatthew G. Knepley 
5942cc4c1da9SBarry Smith /*@
5943b0fe842aSMatthew G. Knepley   DMPlexCreateFromCellSectionParallel - Create distributed `DMPLEX` from a list of vertices for each cell (common mesh generator output) and supports multiple celltypes
5944b0fe842aSMatthew G. Knepley 
5945b0fe842aSMatthew G. Knepley   Collective
5946b0fe842aSMatthew G. Knepley 
5947b0fe842aSMatthew G. Knepley   Input Parameters:
5948b0fe842aSMatthew G. Knepley + comm         - The communicator
5949b0fe842aSMatthew G. Knepley . dim          - The topological dimension of the mesh
5950b0fe842aSMatthew G. Knepley . numCells     - The number of cells owned by this process
5951b0fe842aSMatthew G. Knepley . numVertices  - The number of vertices owned by this process, or `PETSC_DECIDE`
5952b0fe842aSMatthew G. Knepley . NVertices    - The global number of vertices, or `PETSC_DECIDE`
5953b0fe842aSMatthew G. Knepley . cellSection  - The `PetscSection` giving the number of vertices for each cell (layout of cells)
5954b0fe842aSMatthew G. Knepley . interpolate  - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
5955b0fe842aSMatthew G. Knepley . cells        - An array of the global vertex numbers for each cell
5956b0fe842aSMatthew G. Knepley . spaceDim     - The spatial dimension used for coordinates
5957b0fe842aSMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
5958b0fe842aSMatthew G. Knepley 
5959b0fe842aSMatthew G. Knepley   Output Parameters:
5960b0fe842aSMatthew G. Knepley + dm          - The `DM`
5961b0fe842aSMatthew G. Knepley . vertexSF    - (Optional) `PetscSF` describing complete vertex ownership
5962b0fe842aSMatthew G. Knepley - verticesAdj - (Optional) vertex adjacency array
5963b0fe842aSMatthew G. Knepley 
5964b0fe842aSMatthew G. Knepley   Level: intermediate
5965b0fe842aSMatthew G. Knepley 
5966b0fe842aSMatthew G. Knepley   Notes:
5967b0fe842aSMatthew G. Knepley   This function is just a convenient sequence of `DMCreate()`, `DMSetType()`, `DMSetDimension()`,
5968b0fe842aSMatthew G. Knepley   `DMPlexBuildFromCellSectionParallel()`, `DMPlexInterpolate()`, `DMPlexBuildCoordinatesFromCellListParallel()`
5969b0fe842aSMatthew G. Knepley 
5970b0fe842aSMatthew G. Knepley   See `DMPlexBuildFromCellSectionParallel()` for an example and details about the topology-related parameters.
5971b0fe842aSMatthew G. Knepley 
5972b0fe842aSMatthew G. Knepley   See `DMPlexBuildCoordinatesFromCellListParallel()` for details about the geometry-related parameters.
5973b0fe842aSMatthew G. Knepley 
5974b0fe842aSMatthew G. Knepley .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()`
5975b0fe842aSMatthew G. Knepley @*/
5976b0fe842aSMatthew 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)
5977b0fe842aSMatthew G. Knepley {
5978b0fe842aSMatthew G. Knepley   PetscSF sfVert;
5979b0fe842aSMatthew G. Knepley 
5980b0fe842aSMatthew G. Knepley   PetscFunctionBegin;
5981b0fe842aSMatthew G. Knepley   PetscCall(DMCreate(comm, dm));
5982b0fe842aSMatthew G. Knepley   PetscCall(DMSetType(*dm, DMPLEX));
5983b0fe842aSMatthew G. Knepley   PetscValidLogicalCollectiveInt(*dm, dim, 2);
5984b0fe842aSMatthew G. Knepley   PetscValidLogicalCollectiveInt(*dm, spaceDim, 9);
5985b0fe842aSMatthew G. Knepley   PetscCall(DMSetDimension(*dm, dim));
5986b0fe842aSMatthew G. Knepley   PetscCall(DMPlexBuildFromCellSectionParallel(*dm, numCells, numVertices, NVertices, cellSection, cells, &sfVert, verticesAdj));
5987b0fe842aSMatthew G. Knepley   if (interpolate) {
5988b0fe842aSMatthew G. Knepley     DM idm;
5989b0fe842aSMatthew G. Knepley 
5990b0fe842aSMatthew G. Knepley     PetscCall(DMPlexInterpolate(*dm, &idm));
5991b0fe842aSMatthew G. Knepley     PetscCall(DMDestroy(dm));
5992b0fe842aSMatthew G. Knepley     *dm = idm;
5993b0fe842aSMatthew G. Knepley   }
5994b0fe842aSMatthew G. Knepley   PetscCall(DMPlexBuildCoordinatesFromCellListParallel(*dm, spaceDim, sfVert, vertexCoords));
5995b0fe842aSMatthew G. Knepley   if (vertexSF) *vertexSF = sfVert;
5996b0fe842aSMatthew G. Knepley   else PetscCall(PetscSFDestroy(&sfVert));
5997b0fe842aSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
5998b0fe842aSMatthew G. Knepley }
5999b0fe842aSMatthew G. Knepley 
6000b0fe842aSMatthew G. Knepley /*@
6001a1cb98faSBarry Smith   DMPlexBuildFromCellList - Build `DMPLEX` topology from a list of vertices for each cell (common mesh generator output)
6002a1cb98faSBarry Smith 
600320f4b53cSBarry Smith   Collective; No Fortran Support
60049298eaa6SMatthew G Knepley 
60059298eaa6SMatthew G Knepley   Input Parameters:
6006a1cb98faSBarry Smith + dm          - The `DM`
6007b09969d6SVaclav Hapla . numCells    - The number of cells owned by this process
6008a1cb98faSBarry Smith . numVertices - The number of vertices owned by this process, or `PETSC_DETERMINE`
60099298eaa6SMatthew G Knepley . numCorners  - The number of vertices for each cell
6010a3b724e8SBarry Smith - cells       - An array of `numCells` x `numCorners` numbers, the global vertex numbers for each cell
60119298eaa6SMatthew G Knepley 
6012b09969d6SVaclav Hapla   Level: advanced
60139298eaa6SMatthew G Knepley 
6014b09969d6SVaclav Hapla   Notes:
6015b09969d6SVaclav Hapla   Two triangles sharing a face
6016a1cb98faSBarry Smith .vb
60179298eaa6SMatthew G Knepley 
6018a1cb98faSBarry Smith         2
6019a1cb98faSBarry Smith       / | \
6020a1cb98faSBarry Smith      /  |  \
6021a1cb98faSBarry Smith     /   |   \
6022a1cb98faSBarry Smith    0  0 | 1  3
6023a1cb98faSBarry Smith     \   |   /
6024a1cb98faSBarry Smith      \  |  /
6025a1cb98faSBarry Smith       \ | /
6026a1cb98faSBarry Smith         1
6027a1cb98faSBarry Smith .ve
6028a1cb98faSBarry Smith   would have input
6029a1cb98faSBarry Smith .vb
6030a1cb98faSBarry Smith   numCells = 2, numVertices = 4
6031a1cb98faSBarry Smith   cells = [0 1 2  1 3 2]
6032a1cb98faSBarry Smith .ve
6033a1cb98faSBarry Smith   which would result in the `DMPLEX`
6034a1cb98faSBarry Smith .vb
6035a1cb98faSBarry Smith 
6036a1cb98faSBarry Smith         4
6037a1cb98faSBarry Smith       / | \
6038a1cb98faSBarry Smith      /  |  \
6039a1cb98faSBarry Smith     /   |   \
6040a1cb98faSBarry Smith    2  0 | 1  5
6041a1cb98faSBarry Smith     \   |   /
6042a1cb98faSBarry Smith      \  |  /
6043a1cb98faSBarry Smith       \ | /
6044a1cb98faSBarry Smith         3
6045a1cb98faSBarry Smith .ve
6046a1cb98faSBarry Smith 
6047a1cb98faSBarry Smith   If numVertices is `PETSC_DETERMINE`, it is computed by PETSc as the maximum vertex index in cells + 1.
604825b6865aSVaclav Hapla 
60491cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromCellListPetsc()`
6050b09969d6SVaclav Hapla @*/
6051d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildFromCellList(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const PetscInt cells[])
6052d71ae5a4SJacob Faibussowitsch {
6053961cfab0SVaclav Hapla   PetscInt *cones, c, p, dim;
6054b09969d6SVaclav Hapla 
6055b09969d6SVaclav Hapla   PetscFunctionBegin;
60569566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
60579566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
605825b6865aSVaclav Hapla   /* Get/check global number of vertices */
605925b6865aSVaclav Hapla   {
606025b6865aSVaclav Hapla     PetscInt       NVerticesInCells, i;
606125b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
606225b6865aSVaclav Hapla 
606325b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
60641690c2aeSBarry Smith     NVerticesInCells = PETSC_INT_MIN;
60659371c9d4SSatish Balay     for (i = 0; i < len; i++)
60669371c9d4SSatish Balay       if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
606725b6865aSVaclav Hapla     ++NVerticesInCells;
606825b6865aSVaclav Hapla 
606925b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE) numVertices = NVerticesInCells;
60709371c9d4SSatish Balay     else
60719371c9d4SSatish 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);
607225b6865aSVaclav Hapla   }
60739566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
607448a46eb9SPierre Jolivet   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, numCorners));
60759566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm));
60769566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCones(dm, &cones));
6077b09969d6SVaclav Hapla   for (c = 0; c < numCells; ++c) {
6078ad540459SPierre Jolivet     for (p = 0; p < numCorners; ++p) cones[c * numCorners + p] = cells[c * numCorners + p] + numCells;
6079b09969d6SVaclav Hapla   }
60809566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
60819566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
60829566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
60833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6084b09969d6SVaclav Hapla }
6085b09969d6SVaclav Hapla 
6086cc4c1da9SBarry Smith /*@
6087a1cb98faSBarry Smith   DMPlexBuildCoordinatesFromCellList - Build `DM` coordinates from a list of coordinates for each owned vertex (common mesh generator output)
6088a1cb98faSBarry Smith 
6089cc4c1da9SBarry Smith   Collective
6090b09969d6SVaclav Hapla 
6091b09969d6SVaclav Hapla   Input Parameters:
6092a1cb98faSBarry Smith + dm           - The `DM`
6093b09969d6SVaclav Hapla . spaceDim     - The spatial dimension used for coordinates
6094b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
6095b09969d6SVaclav Hapla 
6096b09969d6SVaclav Hapla   Level: advanced
6097b09969d6SVaclav Hapla 
60981cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellList()`
6099b09969d6SVaclav Hapla @*/
6100d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildCoordinatesFromCellList(DM dm, PetscInt spaceDim, const PetscReal vertexCoords[])
6101d71ae5a4SJacob Faibussowitsch {
6102b09969d6SVaclav Hapla   PetscSection coordSection;
6103b09969d6SVaclav Hapla   Vec          coordinates;
6104b09969d6SVaclav Hapla   DM           cdm;
6105b09969d6SVaclav Hapla   PetscScalar *coords;
61061edcf0b2SVaclav Hapla   PetscInt     v, vStart, vEnd, d;
6107b09969d6SVaclav Hapla 
6108b09969d6SVaclav Hapla   PetscFunctionBegin;
61099566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
61109566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
61111dca8a05SBarry Smith   PetscCheck(vStart >= 0 && vEnd >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
61129566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, spaceDim));
61139566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
61149566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
61159566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, spaceDim));
61169566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, vStart, vEnd));
61171edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
61189566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, spaceDim));
61199566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, spaceDim));
6120b09969d6SVaclav Hapla   }
61219566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
6122b09969d6SVaclav Hapla 
61239566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
61249566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(cdm, &coordinates));
61259566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, spaceDim));
61269566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
61279566063dSJacob Faibussowitsch   PetscCall(VecGetArrayWrite(coordinates, &coords));
61281edcf0b2SVaclav Hapla   for (v = 0; v < vEnd - vStart; ++v) {
6129ad540459SPierre Jolivet     for (d = 0; d < spaceDim; ++d) coords[v * spaceDim + d] = vertexCoords[v * spaceDim + d];
6130b09969d6SVaclav Hapla   }
61319566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayWrite(coordinates, &coords));
61329566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
61339566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
61349566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
61353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6136b09969d6SVaclav Hapla }
6137b09969d6SVaclav Hapla 
6138b09969d6SVaclav Hapla /*@
6139a1cb98faSBarry Smith   DMPlexCreateFromCellListPetsc - Create `DMPLEX` from a list of vertices for each cell (common mesh generator output), but only process 0 takes in the input
61403df08285SMatthew G. Knepley 
6141a1cb98faSBarry Smith   Collective
6142b09969d6SVaclav Hapla 
6143b09969d6SVaclav Hapla   Input Parameters:
6144b09969d6SVaclav Hapla + comm         - The communicator
6145b09969d6SVaclav Hapla . dim          - The topological dimension of the mesh
61463df08285SMatthew G. Knepley . numCells     - The number of cells, only on process 0
6147a1cb98faSBarry Smith . numVertices  - The number of vertices owned by this process, or `PETSC_DECIDE`, only on process 0
61483df08285SMatthew G. Knepley . numCorners   - The number of vertices for each cell, only on process 0
6149b09969d6SVaclav Hapla . interpolate  - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
61503df08285SMatthew G. Knepley . cells        - An array of numCells*numCorners numbers, the vertices for each cell, only on process 0
6151b09969d6SVaclav Hapla . spaceDim     - The spatial dimension used for coordinates
61523df08285SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex, only on process 0
6153b09969d6SVaclav Hapla 
6154b09969d6SVaclav Hapla   Output Parameter:
6155a1cb98faSBarry Smith . dm - The `DM`, which only has points on process 0
615625b6865aSVaclav Hapla 
6157b09969d6SVaclav Hapla   Level: intermediate
6158b09969d6SVaclav Hapla 
6159a1cb98faSBarry Smith   Notes:
6160a1cb98faSBarry Smith   This function is just a convenient sequence of `DMCreate()`, `DMSetType()`, `DMSetDimension()`, `DMPlexBuildFromCellList()`,
6161a1cb98faSBarry Smith   `DMPlexInterpolate()`, `DMPlexBuildCoordinatesFromCellList()`
6162a1cb98faSBarry Smith 
6163a1cb98faSBarry Smith   See `DMPlexBuildFromCellList()` for an example and details about the topology-related parameters.
6164a1cb98faSBarry Smith   See `DMPlexBuildCoordinatesFromCellList()` for details about the geometry-related parameters.
6165a1cb98faSBarry Smith   See `DMPlexCreateFromCellListParallelPetsc()` for parallel input
6166a1cb98faSBarry Smith 
61671cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildFromCellList()`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()`
61689298eaa6SMatthew G Knepley @*/
6169d71ae5a4SJacob 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)
6170d71ae5a4SJacob Faibussowitsch {
61713df08285SMatthew G. Knepley   PetscMPIInt rank;
61729298eaa6SMatthew G Knepley 
61739298eaa6SMatthew G Knepley   PetscFunctionBegin;
617428b400f6SJacob 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.");
61759566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
61769566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
61779566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
61789566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
6179c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildFromCellList(*dm, numCells, numVertices, numCorners, cells));
61809566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildFromCellList(*dm, 0, 0, 0, NULL));
61819298eaa6SMatthew G Knepley   if (interpolate) {
61825fd9971aSMatthew G. Knepley     DM idm;
61839298eaa6SMatthew G Knepley 
61849566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
61859566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
61869298eaa6SMatthew G Knepley     *dm = idm;
61879298eaa6SMatthew G Knepley   }
6188c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, vertexCoords));
61899566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, NULL));
61903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
61919298eaa6SMatthew G Knepley }
61929298eaa6SMatthew G Knepley 
6193939f6067SMatthew G. Knepley /*@
619420f4b53cSBarry Smith   DMPlexCreateFromDAG - This takes as input the adjacency-list representation of the Directed Acyclic Graph (Hasse Diagram) encoding a mesh, and produces a `DM`
6195939f6067SMatthew G. Knepley 
6196939f6067SMatthew G. Knepley   Input Parameters:
619720f4b53cSBarry Smith + dm               - The empty `DM` object, usually from `DMCreate()` and `DMSetDimension()`
6198939f6067SMatthew G. Knepley . depth            - The depth of the DAG
619920f4b53cSBarry Smith . numPoints        - Array of size depth + 1 containing the number of points at each `depth`
6200939f6067SMatthew G. Knepley . coneSize         - The cone size of each point
6201939f6067SMatthew G. Knepley . cones            - The concatenation of the cone points for each point, the cone list must be oriented correctly for each point
6202939f6067SMatthew G. Knepley . coneOrientations - The orientation of each cone point
620320f4b53cSBarry Smith - vertexCoords     - An array of `numPoints`[0]*spacedim numbers representing the coordinates of each vertex, with spacedim the value set via `DMSetCoordinateDim()`
6204939f6067SMatthew G. Knepley 
6205939f6067SMatthew G. Knepley   Output Parameter:
620620f4b53cSBarry Smith . dm - The `DM`
620720f4b53cSBarry Smith 
620820f4b53cSBarry Smith   Level: advanced
6209939f6067SMatthew G. Knepley 
6210a1cb98faSBarry Smith   Note:
6211a1cb98faSBarry Smith   Two triangles sharing a face would have input
6212a1cb98faSBarry Smith .vb
6213a1cb98faSBarry Smith   depth = 1, numPoints = [4 2], coneSize = [3 3 0 0 0 0]
6214a1cb98faSBarry Smith   cones = [2 3 4  3 5 4], coneOrientations = [0 0 0  0 0 0]
6215a1cb98faSBarry Smith  vertexCoords = [-1.0 0.0  0.0 -1.0  0.0 1.0  1.0 0.0]
6216a1cb98faSBarry Smith .ve
6217939f6067SMatthew G. Knepley   which would result in the DMPlex
6218a1cb98faSBarry Smith .vb
6219a1cb98faSBarry Smith         4
6220a1cb98faSBarry Smith       / | \
6221a1cb98faSBarry Smith      /  |  \
6222a1cb98faSBarry Smith     /   |   \
6223a1cb98faSBarry Smith    2  0 | 1  5
6224a1cb98faSBarry Smith     \   |   /
6225a1cb98faSBarry Smith      \  |  /
6226a1cb98faSBarry Smith       \ | /
6227a1cb98faSBarry Smith         3
6228a1cb98faSBarry Smith .ve
6229a1cb98faSBarry Smith   Notice that all points are numbered consecutively, unlike `DMPlexCreateFromCellListPetsc()`
6230939f6067SMatthew G. Knepley 
62311cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`
6232939f6067SMatthew G. Knepley @*/
6233d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateFromDAG(DM dm, PetscInt depth, const PetscInt numPoints[], const PetscInt coneSize[], const PetscInt cones[], const PetscInt coneOrientations[], const PetscScalar vertexCoords[])
6234d71ae5a4SJacob Faibussowitsch {
62359298eaa6SMatthew G Knepley   Vec          coordinates;
62369298eaa6SMatthew G Knepley   PetscSection coordSection;
62379298eaa6SMatthew G Knepley   PetscScalar *coords;
6238811e8653SToby Isaac   PetscInt     coordSize, firstVertex = -1, pStart = 0, pEnd = 0, p, v, dim, dimEmbed, d, off;
62399298eaa6SMatthew G Knepley 
62409298eaa6SMatthew G Knepley   PetscFunctionBegin;
62419566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
62429566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dimEmbed));
624363a3b9bcSJacob Faibussowitsch   PetscCheck(dimEmbed >= dim, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Embedding dimension %" PetscInt_FMT " cannot be less than intrinsic dimension %" PetscInt_FMT, dimEmbed, dim);
62449298eaa6SMatthew G Knepley   for (d = 0; d <= depth; ++d) pEnd += numPoints[d];
62459566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, pStart, pEnd));
62469298eaa6SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
62479566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeSize(dm, p, coneSize[p - pStart]));
6248ad540459SPierre Jolivet     if (firstVertex < 0 && !coneSize[p - pStart]) firstVertex = p - pStart;
624997e052ccSToby Isaac   }
62501dca8a05SBarry Smith   PetscCheck(firstVertex >= 0 || !numPoints[0], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Expected %" PetscInt_FMT " vertices but could not find any", numPoints[0]);
62519566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm)); /* Allocate space for cones */
62529298eaa6SMatthew G Knepley   for (p = pStart, off = 0; p < pEnd; off += coneSize[p - pStart], ++p) {
62539566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(dm, p, &cones[off]));
62549566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeOrientation(dm, p, &coneOrientations[off]));
62559298eaa6SMatthew G Knepley   }
62569566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
62579566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
62589298eaa6SMatthew G Knepley   /* Build coordinates */
62599566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
62609566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
62619566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dimEmbed));
62629566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numPoints[0]));
62639298eaa6SMatthew G Knepley   for (v = firstVertex; v < firstVertex + numPoints[0]; ++v) {
62649566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, dimEmbed));
62659566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dimEmbed));
62669298eaa6SMatthew G Knepley   }
62679566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
62689566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
62699566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
62709566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
62719566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
62729566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, dimEmbed));
62739566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
62749318fe57SMatthew G. Knepley   if (vertexCoords) {
62759566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
62769298eaa6SMatthew G Knepley     for (v = 0; v < numPoints[0]; ++v) {
62779298eaa6SMatthew G Knepley       PetscInt off;
62789298eaa6SMatthew G Knepley 
62799566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, v + firstVertex, &off));
6280ad540459SPierre Jolivet       for (d = 0; d < dimEmbed; ++d) coords[off + d] = vertexCoords[v * dimEmbed + d];
62819298eaa6SMatthew G Knepley     }
62829318fe57SMatthew G. Knepley   }
62839566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
62849566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
62859566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
62863ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
62879298eaa6SMatthew G Knepley }
62888415267dSToby Isaac 
6289a4e35b19SJacob Faibussowitsch /*
6290a1cb98faSBarry Smith   DMPlexCreateCellVertexFromFile - Create a `DMPLEX` mesh from a simple cell-vertex file.
6291a1cb98faSBarry Smith 
6292a1cb98faSBarry Smith   Collective
62938ca92349SMatthew G. Knepley 
62948ca92349SMatthew G. Knepley + comm        - The MPI communicator
62958ca92349SMatthew G. Knepley . filename    - Name of the .dat file
62968ca92349SMatthew G. Knepley - interpolate - Create faces and edges in the mesh
62978ca92349SMatthew G. Knepley 
62988ca92349SMatthew G. Knepley   Output Parameter:
6299a1cb98faSBarry Smith . dm  - The `DM` object representing the mesh
63008ca92349SMatthew G. Knepley 
63018ca92349SMatthew G. Knepley   Level: beginner
63028ca92349SMatthew G. Knepley 
6303a1cb98faSBarry Smith   Note:
6304a1cb98faSBarry Smith   The format is the simplest possible:
6305a1cb98faSBarry Smith .vb
6306d0812dedSMatthew G. Knepley   dim Ne Nv Nc Nl
6307d0812dedSMatthew G. Knepley   v_1 v_2 ... v_Nc
6308d0812dedSMatthew G. Knepley   ...
6309d0812dedSMatthew G. Knepley   x y z marker_1 ... marker_Nl
6310a1cb98faSBarry Smith .ve
6311a1cb98faSBarry Smith 
6312a1cb98faSBarry Smith   Developer Note:
6313a1cb98faSBarry Smith   Should use a `PetscViewer` not a filename
6314a1cb98faSBarry Smith 
63156afe31f6SMartin Diehl .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromFile()`, `DMPlexCreateGmsh()`, `DMPlexCreate()`
6316a4e35b19SJacob Faibussowitsch */
6317ff6a9541SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCellVertexFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
6318d71ae5a4SJacob Faibussowitsch {
63198ca92349SMatthew G. Knepley   DMLabel      marker;
63208ca92349SMatthew G. Knepley   PetscViewer  viewer;
63218ca92349SMatthew G. Knepley   Vec          coordinates;
63228ca92349SMatthew G. Knepley   PetscSection coordSection;
63238ca92349SMatthew G. Knepley   PetscScalar *coords;
63248ca92349SMatthew G. Knepley   char         line[PETSC_MAX_PATH_LEN];
6325d0812dedSMatthew G. Knepley   PetscInt     cdim, coordSize, v, c, d;
63268ca92349SMatthew G. Knepley   PetscMPIInt  rank;
6327d0812dedSMatthew G. Knepley   int          snum, dim, Nv, Nc, Ncn, Nl;
63288ca92349SMatthew G. Knepley 
63298ca92349SMatthew G. Knepley   PetscFunctionBegin;
63309566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
63319566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, &viewer));
63329566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(viewer, PETSCVIEWERASCII));
63339566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetMode(viewer, FILE_MODE_READ));
63349566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetName(viewer, filename));
6335dd400576SPatrick Sanan   if (rank == 0) {
6336d0812dedSMatthew G. Knepley     PetscCall(PetscViewerRead(viewer, line, 5, NULL, PETSC_STRING));
6337d0812dedSMatthew G. Knepley     snum = sscanf(line, "%d %d %d %d %d", &dim, &Nc, &Nv, &Ncn, &Nl);
6338d0812dedSMatthew G. Knepley     PetscCheck(snum == 5, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
633925ce1634SJed Brown   } else {
6340f8d5e320SMatthew G. Knepley     Nc = Nv = Ncn = Nl = 0;
63418ca92349SMatthew G. Knepley   }
6342d0812dedSMatthew G. Knepley   PetscCallMPI(MPI_Bcast(&dim, 1, MPI_INT, 0, comm));
6343835f2295SStefano Zampini   cdim = dim;
63449566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
63459566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
63469566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(*dm, 0, Nc + Nv));
6347835f2295SStefano Zampini   PetscCall(DMSetDimension(*dm, dim));
63489566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*dm, cdim));
63498ca92349SMatthew G. Knepley   /* Read topology */
6350dd400576SPatrick Sanan   if (rank == 0) {
6351f8d5e320SMatthew G. Knepley     char     format[PETSC_MAX_PATH_LEN];
6352f8d5e320SMatthew G. Knepley     PetscInt cone[8];
63538ca92349SMatthew G. Knepley     int      vbuf[8], v;
63548ca92349SMatthew G. Knepley 
63559371c9d4SSatish Balay     for (c = 0; c < Ncn; ++c) {
63569371c9d4SSatish Balay       format[c * 3 + 0] = '%';
63579371c9d4SSatish Balay       format[c * 3 + 1] = 'd';
63589371c9d4SSatish Balay       format[c * 3 + 2] = ' ';
63599371c9d4SSatish Balay     }
6360f8d5e320SMatthew G. Knepley     format[Ncn * 3 - 1] = '\0';
63619566063dSJacob Faibussowitsch     for (c = 0; c < Nc; ++c) PetscCall(DMPlexSetConeSize(*dm, c, Ncn));
63629566063dSJacob Faibussowitsch     PetscCall(DMSetUp(*dm));
63638ca92349SMatthew G. Knepley     for (c = 0; c < Nc; ++c) {
63649566063dSJacob Faibussowitsch       PetscCall(PetscViewerRead(viewer, line, Ncn, NULL, PETSC_STRING));
6365f8d5e320SMatthew G. Knepley       switch (Ncn) {
6366d71ae5a4SJacob Faibussowitsch       case 2:
6367d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1]);
6368d71ae5a4SJacob Faibussowitsch         break;
6369d71ae5a4SJacob Faibussowitsch       case 3:
6370d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2]);
6371d71ae5a4SJacob Faibussowitsch         break;
6372d71ae5a4SJacob Faibussowitsch       case 4:
6373d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3]);
6374d71ae5a4SJacob Faibussowitsch         break;
6375d71ae5a4SJacob Faibussowitsch       case 6:
6376d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5]);
6377d71ae5a4SJacob Faibussowitsch         break;
6378d71ae5a4SJacob Faibussowitsch       case 8:
6379d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5], &vbuf[6], &vbuf[7]);
6380d71ae5a4SJacob Faibussowitsch         break;
6381d71ae5a4SJacob Faibussowitsch       default:
6382d71ae5a4SJacob Faibussowitsch         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No cell shape with %d vertices", Ncn);
6383f8d5e320SMatthew G. Knepley       }
638408401ef6SPierre Jolivet       PetscCheck(snum == Ncn, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
6385f8d5e320SMatthew G. Knepley       for (v = 0; v < Ncn; ++v) cone[v] = vbuf[v] + Nc;
63868ca92349SMatthew G. Knepley       /* Hexahedra are inverted */
6387f8d5e320SMatthew G. Knepley       if (Ncn == 8) {
63888ca92349SMatthew G. Knepley         PetscInt tmp = cone[1];
63898ca92349SMatthew G. Knepley         cone[1]      = cone[3];
63908ca92349SMatthew G. Knepley         cone[3]      = tmp;
63918ca92349SMatthew G. Knepley       }
63929566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(*dm, c, cone));
63938ca92349SMatthew G. Knepley     }
63948ca92349SMatthew G. Knepley   }
63959566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(*dm));
63969566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(*dm));
63978ca92349SMatthew G. Knepley   /* Read coordinates */
63989566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(*dm, &coordSection));
63999566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
64009566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, cdim));
64019566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, Nc, Nc + Nv));
64028ca92349SMatthew G. Knepley   for (v = Nc; v < Nc + Nv; ++v) {
64039566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, cdim));
64049566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, cdim));
64058ca92349SMatthew G. Knepley   }
64069566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
64079566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
64089566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
64099566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
64109566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
64119566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, cdim));
64129566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
64139566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
6414dd400576SPatrick Sanan   if (rank == 0) {
6415f8d5e320SMatthew G. Knepley     char   format[PETSC_MAX_PATH_LEN];
64168ca92349SMatthew G. Knepley     double x[3];
6417f8d5e320SMatthew G. Knepley     int    l, val[3];
64188ca92349SMatthew G. Knepley 
6419f8d5e320SMatthew G. Knepley     if (Nl) {
64209371c9d4SSatish Balay       for (l = 0; l < Nl; ++l) {
64219371c9d4SSatish Balay         format[l * 3 + 0] = '%';
64229371c9d4SSatish Balay         format[l * 3 + 1] = 'd';
64239371c9d4SSatish Balay         format[l * 3 + 2] = ' ';
64249371c9d4SSatish Balay       }
6425f8d5e320SMatthew G. Knepley       format[Nl * 3 - 1] = '\0';
64269566063dSJacob Faibussowitsch       PetscCall(DMCreateLabel(*dm, "marker"));
64279566063dSJacob Faibussowitsch       PetscCall(DMGetLabel(*dm, "marker", &marker));
6428f8d5e320SMatthew G. Knepley     }
64298ca92349SMatthew G. Knepley     for (v = 0; v < Nv; ++v) {
64309566063dSJacob Faibussowitsch       PetscCall(PetscViewerRead(viewer, line, 3 + Nl, NULL, PETSC_STRING));
6431f8d5e320SMatthew G. Knepley       snum = sscanf(line, "%lg %lg %lg", &x[0], &x[1], &x[2]);
643208401ef6SPierre Jolivet       PetscCheck(snum == 3, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
6433f8d5e320SMatthew G. Knepley       switch (Nl) {
6434d71ae5a4SJacob Faibussowitsch       case 0:
6435d71ae5a4SJacob Faibussowitsch         snum = 0;
6436d71ae5a4SJacob Faibussowitsch         break;
6437d71ae5a4SJacob Faibussowitsch       case 1:
6438d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &val[0]);
6439d71ae5a4SJacob Faibussowitsch         break;
6440d71ae5a4SJacob Faibussowitsch       case 2:
6441d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &val[0], &val[1]);
6442d71ae5a4SJacob Faibussowitsch         break;
6443d71ae5a4SJacob Faibussowitsch       case 3:
6444d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &val[0], &val[1], &val[2]);
6445d71ae5a4SJacob Faibussowitsch         break;
6446d71ae5a4SJacob Faibussowitsch       default:
6447d71ae5a4SJacob Faibussowitsch         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Request support for %d labels", Nl);
6448f8d5e320SMatthew G. Knepley       }
644908401ef6SPierre Jolivet       PetscCheck(snum == Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
64508ca92349SMatthew G. Knepley       for (d = 0; d < cdim; ++d) coords[v * cdim + d] = x[d];
64519566063dSJacob Faibussowitsch       for (l = 0; l < Nl; ++l) PetscCall(DMLabelSetValue(marker, v + Nc, val[l]));
64528ca92349SMatthew G. Knepley     }
64538ca92349SMatthew G. Knepley   }
64549566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
64559566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(*dm, coordinates));
64569566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
64579566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(&viewer));
64588ca92349SMatthew G. Knepley   if (interpolate) {
64598ca92349SMatthew G. Knepley     DM      idm;
64608ca92349SMatthew G. Knepley     DMLabel bdlabel;
64618ca92349SMatthew G. Knepley 
64629566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
64639566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
64648ca92349SMatthew G. Knepley     *dm = idm;
64658ca92349SMatthew G. Knepley 
6466f8d5e320SMatthew G. Knepley     if (!Nl) {
64679566063dSJacob Faibussowitsch       PetscCall(DMCreateLabel(*dm, "marker"));
64689566063dSJacob Faibussowitsch       PetscCall(DMGetLabel(*dm, "marker", &bdlabel));
64699566063dSJacob Faibussowitsch       PetscCall(DMPlexMarkBoundaryFaces(*dm, PETSC_DETERMINE, bdlabel));
64709566063dSJacob Faibussowitsch       PetscCall(DMPlexLabelComplete(*dm, bdlabel));
64718ca92349SMatthew G. Knepley     }
6472f8d5e320SMatthew G. Knepley   }
64733ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
64748ca92349SMatthew G. Knepley }
64758ca92349SMatthew G. Knepley 
6476cc4c1da9SBarry Smith /*@
6477a1cb98faSBarry Smith   DMPlexCreateFromFile - This takes a filename and produces a `DM`
6478a1cb98faSBarry Smith 
6479a1cb98faSBarry Smith   Collective
6480ca522641SMatthew G. Knepley 
6481ca522641SMatthew G. Knepley   Input Parameters:
6482ca522641SMatthew G. Knepley + comm        - The communicator
6483ca522641SMatthew G. Knepley . filename    - A file name
6484a1cb98faSBarry Smith . plexname    - The object name of the resulting `DM`, also used for intra-datafile lookup by some formats
6485ca522641SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
6486ca522641SMatthew G. Knepley 
6487ca522641SMatthew G. Knepley   Output Parameter:
6488a1cb98faSBarry Smith . dm - The `DM`
6489ca522641SMatthew G. Knepley 
6490a1cb98faSBarry Smith   Options Database Key:
6491a1cb98faSBarry Smith . -dm_plex_create_from_hdf5_xdmf - use the `PETSC_VIEWER_HDF5_XDMF` format for reading HDF5
649202ef0d99SVaclav Hapla 
649337fdd005SBarry Smith   Use `-dm_plex_create_ prefix` to pass options to the internal `PetscViewer`, e.g.
6494bca97951SVaclav Hapla $ -dm_plex_create_viewer_hdf5_collective
6495bca97951SVaclav Hapla 
6496ca522641SMatthew G. Knepley   Level: beginner
6497ca522641SMatthew G. Knepley 
6498a1cb98faSBarry Smith   Notes:
6499a1cb98faSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
6500a1cb98faSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
6501a1cb98faSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
6502a1cb98faSBarry Smith   The input parameter name is thus used to name the `DMPLEX` object when `DMPlexCreateFromFile()` internally
6503a1cb98faSBarry Smith   calls `DMLoad()`. Currently, name is ignored for other viewer types and/or formats.
6504a1cb98faSBarry Smith 
65051cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromDAG()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`, `PetscObjectSetName()`, `DMView()`, `DMLoad()`
6506ca522641SMatthew G. Knepley @*/
6507d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateFromFile(MPI_Comm comm, const char filename[], const char plexname[], PetscBool interpolate, DM *dm)
6508d71ae5a4SJacob Faibussowitsch {
6509ef3a5affSJacob Faibussowitsch   const char  extGmsh[]      = ".msh";
6510ef3a5affSJacob Faibussowitsch   const char  extGmsh2[]     = ".msh2";
6511ef3a5affSJacob Faibussowitsch   const char  extGmsh4[]     = ".msh4";
6512ef3a5affSJacob Faibussowitsch   const char  extCGNS[]      = ".cgns";
6513ef3a5affSJacob Faibussowitsch   const char  extExodus[]    = ".exo";
6514ef3a5affSJacob Faibussowitsch   const char  extExodus_e[]  = ".e";
6515ef3a5affSJacob Faibussowitsch   const char  extGenesis[]   = ".gen";
6516ef3a5affSJacob Faibussowitsch   const char  extFluent[]    = ".cas";
6517ef3a5affSJacob Faibussowitsch   const char  extHDF5[]      = ".h5";
65186f2c871aSStefano Zampini   const char  extXDMFHDF5[]  = ".xdmf.h5";
6519ef3a5affSJacob Faibussowitsch   const char  extPLY[]       = ".ply";
6520ef3a5affSJacob Faibussowitsch   const char  extEGADSLite[] = ".egadslite";
6521ef3a5affSJacob Faibussowitsch   const char  extEGADS[]     = ".egads";
6522ef3a5affSJacob Faibussowitsch   const char  extIGES[]      = ".igs";
6523ef3a5affSJacob Faibussowitsch   const char  extSTEP[]      = ".stp";
6524ef3a5affSJacob Faibussowitsch   const char  extCV[]        = ".dat";
6525ca522641SMatthew G. Knepley   size_t      len;
65266afe31f6SMartin Diehl   PetscBool   isGmsh, isGmsh2, isGmsh4, isCGNS, isExodus, isGenesis, isFluent, isHDF5, isPLY, isEGADSLite, isEGADS, isIGES, isSTEP, isCV, isXDMFHDF5;
6527ca522641SMatthew G. Knepley   PetscMPIInt rank;
6528ca522641SMatthew G. Knepley 
6529ca522641SMatthew G. Knepley   PetscFunctionBegin;
65304f572ea9SToby Isaac   PetscAssertPointer(filename, 2);
65314f572ea9SToby Isaac   if (plexname) PetscAssertPointer(plexname, 3);
65324f572ea9SToby Isaac   PetscAssertPointer(dm, 5);
65339566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
65349566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_CreateFromFile, 0, 0, 0, 0));
65359566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
65369566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(filename, &len));
653728b400f6SJacob Faibussowitsch   PetscCheck(len, comm, PETSC_ERR_ARG_WRONG, "Filename must be a valid path");
6538ef3a5affSJacob Faibussowitsch 
65399371c9d4SSatish Balay #define CheckExtension(extension__, is_extension__) \
65409371c9d4SSatish Balay   do { \
6541274aaeaaSJacob Faibussowitsch     PetscAssert(sizeof(extension__), comm, PETSC_ERR_PLIB, "Zero-size extension: %s", extension__); \
6542274aaeaaSJacob Faibussowitsch     /* don't count the null-terminator at the end */ \
6543274aaeaaSJacob Faibussowitsch     const size_t ext_len = sizeof(extension__) - 1; \
6544274aaeaaSJacob Faibussowitsch     if (len < ext_len) { \
6545ef3a5affSJacob Faibussowitsch       is_extension__ = PETSC_FALSE; \
6546ef3a5affSJacob Faibussowitsch     } else { \
6547274aaeaaSJacob Faibussowitsch       PetscCall(PetscStrncmp(filename + len - ext_len, extension__, ext_len, &is_extension__)); \
6548ef3a5affSJacob Faibussowitsch     } \
6549ef3a5affSJacob Faibussowitsch   } while (0)
6550ef3a5affSJacob Faibussowitsch 
6551ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh, isGmsh);
6552ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh2, isGmsh2);
6553ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh4, isGmsh4);
6554ef3a5affSJacob Faibussowitsch   CheckExtension(extCGNS, isCGNS);
6555ef3a5affSJacob Faibussowitsch   CheckExtension(extExodus, isExodus);
6556ef3a5affSJacob Faibussowitsch   if (!isExodus) CheckExtension(extExodus_e, isExodus);
6557ef3a5affSJacob Faibussowitsch   CheckExtension(extGenesis, isGenesis);
6558ef3a5affSJacob Faibussowitsch   CheckExtension(extFluent, isFluent);
6559ef3a5affSJacob Faibussowitsch   CheckExtension(extHDF5, isHDF5);
6560ef3a5affSJacob Faibussowitsch   CheckExtension(extPLY, isPLY);
6561ef3a5affSJacob Faibussowitsch   CheckExtension(extEGADSLite, isEGADSLite);
6562ef3a5affSJacob Faibussowitsch   CheckExtension(extEGADS, isEGADS);
6563ef3a5affSJacob Faibussowitsch   CheckExtension(extIGES, isIGES);
6564ef3a5affSJacob Faibussowitsch   CheckExtension(extSTEP, isSTEP);
6565ef3a5affSJacob Faibussowitsch   CheckExtension(extCV, isCV);
65666f2c871aSStefano Zampini   CheckExtension(extXDMFHDF5, isXDMFHDF5);
6567ef3a5affSJacob Faibussowitsch 
6568ef3a5affSJacob Faibussowitsch #undef CheckExtension
6569ef3a5affSJacob Faibussowitsch 
6570de78e4feSLisandro Dalcin   if (isGmsh || isGmsh2 || isGmsh4) {
65719566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateGmshFromFile(comm, filename, interpolate, dm));
6572ca522641SMatthew G. Knepley   } else if (isCGNS) {
65739566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCGNSFromFile(comm, filename, interpolate, dm));
657490c68965SMatthew G. Knepley   } else if (isExodus || isGenesis) {
65759566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateExodusFromFile(comm, filename, interpolate, dm));
65762f0bd6dcSMichael Lange   } else if (isFluent) {
65779566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFluentFromFile(comm, filename, interpolate, dm));
6578cc2f8f65SMatthew G. Knepley   } else if (isHDF5) {
6579cc2f8f65SMatthew G. Knepley     PetscViewer viewer;
6580cc2f8f65SMatthew G. Knepley 
658143b242b4SVaclav 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 */
65826f2c871aSStefano Zampini     PetscCall(PetscOptionsGetBool(NULL, NULL, "-dm_plex_create_from_hdf5_xdmf", &isXDMFHDF5, NULL));
65839566063dSJacob Faibussowitsch     PetscCall(PetscViewerCreate(comm, &viewer));
65849566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetType(viewer, PETSCVIEWERHDF5));
65859566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetOptionsPrefix(viewer, "dm_plex_create_"));
65869566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetFromOptions(viewer));
65879566063dSJacob Faibussowitsch     PetscCall(PetscViewerFileSetMode(viewer, FILE_MODE_READ));
65889566063dSJacob Faibussowitsch     PetscCall(PetscViewerFileSetName(viewer, filename));
6589cd7e8a5eSksagiyam 
65909566063dSJacob Faibussowitsch     PetscCall(DMCreate(comm, dm));
6591f4f49eeaSPierre Jolivet     PetscCall(PetscObjectSetName((PetscObject)*dm, plexname));
65929566063dSJacob Faibussowitsch     PetscCall(DMSetType(*dm, DMPLEX));
65936f2c871aSStefano Zampini     if (isXDMFHDF5) PetscCall(PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_XDMF));
65949566063dSJacob Faibussowitsch     PetscCall(DMLoad(*dm, viewer));
65956f2c871aSStefano Zampini     if (isXDMFHDF5) PetscCall(PetscViewerPopFormat(viewer));
65969566063dSJacob Faibussowitsch     PetscCall(PetscViewerDestroy(&viewer));
65975fd9971aSMatthew G. Knepley 
65985fd9971aSMatthew G. Knepley     if (interpolate) {
65995fd9971aSMatthew G. Knepley       DM idm;
66005fd9971aSMatthew G. Knepley 
66019566063dSJacob Faibussowitsch       PetscCall(DMPlexInterpolate(*dm, &idm));
66029566063dSJacob Faibussowitsch       PetscCall(DMDestroy(dm));
66035fd9971aSMatthew G. Knepley       *dm = idm;
66045fd9971aSMatthew G. Knepley     }
6605f2801cd6SMatthew G. Knepley   } else if (isPLY) {
66069566063dSJacob Faibussowitsch     PetscCall(DMPlexCreatePLYFromFile(comm, filename, interpolate, dm));
6607c1cad2e7SMatthew G. Knepley   } else if (isEGADSLite || isEGADS || isIGES || isSTEP) {
66089566063dSJacob Faibussowitsch     if (isEGADSLite) PetscCall(DMPlexCreateEGADSLiteFromFile(comm, filename, dm));
66099566063dSJacob Faibussowitsch     else PetscCall(DMPlexCreateEGADSFromFile(comm, filename, dm));
66107bee2925SMatthew Knepley     if (!interpolate) {
66117bee2925SMatthew Knepley       DM udm;
66127bee2925SMatthew Knepley 
66139566063dSJacob Faibussowitsch       PetscCall(DMPlexUninterpolate(*dm, &udm));
66149566063dSJacob Faibussowitsch       PetscCall(DMDestroy(dm));
66157bee2925SMatthew Knepley       *dm = udm;
66167bee2925SMatthew Knepley     }
66178ca92349SMatthew G. Knepley   } else if (isCV) {
66189566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCellVertexFromFile(comm, filename, interpolate, dm));
661998921bdaSJacob Faibussowitsch   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot load file %s: unrecognized extension", filename);
66209566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(plexname, &len));
6621f4f49eeaSPierre Jolivet   if (len) PetscCall(PetscObjectSetName((PetscObject)*dm, plexname));
66229566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_CreateFromFile, 0, 0, 0, 0));
66233ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6624ca522641SMatthew G. Knepley }
662512b8a6daSStefano Zampini 
6626cc4c1da9SBarry Smith /*@
66279f6c5813SMatthew 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.
66289f6c5813SMatthew G. Knepley 
66290528010dSStefano Zampini   Input Parameters:
66300528010dSStefano Zampini + tr     - The `DMPlexTransform`
66310528010dSStefano Zampini - prefix - An options prefix, or NULL
66329f6c5813SMatthew G. Knepley 
66339f6c5813SMatthew G. Knepley   Output Parameter:
66349f6c5813SMatthew G. Knepley . dm - The `DM`
66359f6c5813SMatthew G. Knepley 
66369f6c5813SMatthew G. Knepley   Level: beginner
66379f6c5813SMatthew G. Knepley 
663820f4b53cSBarry Smith   Notes:
663920f4b53cSBarry 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.
664020f4b53cSBarry Smith 
66419f6c5813SMatthew G. Knepley .seealso: `DMPlexCreateFromFile`, `DMPlexCreateFromDAG()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`
66429f6c5813SMatthew G. Knepley @*/
66430528010dSStefano Zampini PetscErrorCode DMPlexCreateEphemeral(DMPlexTransform tr, const char prefix[], DM *dm)
66449f6c5813SMatthew G. Knepley {
66450528010dSStefano Zampini   DM           bdm, bcdm, cdm;
66460528010dSStefano Zampini   Vec          coordinates, coordinatesNew;
66470528010dSStefano Zampini   PetscSection cs;
6648817b2c36SMatthew G. Knepley   PetscInt     cdim, Nl;
66499f6c5813SMatthew G. Knepley 
66509f6c5813SMatthew G. Knepley   PetscFunctionBegin;
66519f6c5813SMatthew G. Knepley   PetscCall(DMCreate(PetscObjectComm((PetscObject)tr), dm));
66529f6c5813SMatthew G. Knepley   PetscCall(DMSetType(*dm, DMPLEX));
66530528010dSStefano Zampini   ((DM_Plex *)(*dm)->data)->interpolated = DMPLEX_INTERPOLATED_FULL;
66540528010dSStefano Zampini   // Handle coordinates
66550528010dSStefano Zampini   PetscCall(DMPlexTransformGetDM(tr, &bdm));
6656817b2c36SMatthew G. Knepley   PetscCall(DMPlexTransformSetDimensions(tr, bdm, *dm));
6657817b2c36SMatthew G. Knepley   PetscCall(DMGetCoordinateDim(*dm, &cdim));
66580528010dSStefano Zampini   PetscCall(DMGetCoordinateDM(bdm, &bcdm));
66590528010dSStefano Zampini   PetscCall(DMGetCoordinateDM(*dm, &cdm));
66600528010dSStefano Zampini   PetscCall(DMCopyDisc(bcdm, cdm));
66610528010dSStefano Zampini   PetscCall(DMGetLocalSection(cdm, &cs));
66620528010dSStefano Zampini   PetscCall(PetscSectionSetNumFields(cs, 1));
66630528010dSStefano Zampini   PetscCall(PetscSectionSetFieldComponents(cs, 0, cdim));
66640528010dSStefano Zampini   PetscCall(DMGetCoordinatesLocal(bdm, &coordinates));
66650528010dSStefano Zampini   PetscCall(VecDuplicate(coordinates, &coordinatesNew));
66660528010dSStefano Zampini   PetscCall(VecCopy(coordinates, coordinatesNew));
66670528010dSStefano Zampini   PetscCall(DMSetCoordinatesLocal(*dm, coordinatesNew));
66680528010dSStefano Zampini   PetscCall(VecDestroy(&coordinatesNew));
66699f6c5813SMatthew G. Knepley 
66709f6c5813SMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)tr));
66719f6c5813SMatthew G. Knepley   PetscCall(DMPlexTransformDestroy(&((DM_Plex *)(*dm)->data)->tr));
66729f6c5813SMatthew G. Knepley   ((DM_Plex *)(*dm)->data)->tr = tr;
66730528010dSStefano Zampini   PetscCall(DMPlexDistributeSetDefault(*dm, PETSC_FALSE));
66740528010dSStefano Zampini   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)*dm, prefix));
66750528010dSStefano Zampini   PetscCall(DMSetFromOptions(*dm));
66769f6c5813SMatthew G. Knepley 
66779f6c5813SMatthew G. Knepley   PetscCall(DMGetNumLabels(bdm, &Nl));
66789f6c5813SMatthew G. Knepley   for (PetscInt l = 0; l < Nl; ++l) {
66799f6c5813SMatthew G. Knepley     DMLabel     label, labelNew;
66809f6c5813SMatthew G. Knepley     const char *lname;
66819f6c5813SMatthew G. Knepley     PetscBool   isDepth, isCellType;
66829f6c5813SMatthew G. Knepley 
66839f6c5813SMatthew G. Knepley     PetscCall(DMGetLabelName(bdm, l, &lname));
66849f6c5813SMatthew G. Knepley     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
66859f6c5813SMatthew G. Knepley     if (isDepth) continue;
66869f6c5813SMatthew G. Knepley     PetscCall(PetscStrcmp(lname, "celltype", &isCellType));
66879f6c5813SMatthew G. Knepley     if (isCellType) continue;
66889f6c5813SMatthew G. Knepley     PetscCall(DMCreateLabel(*dm, lname));
66899f6c5813SMatthew G. Knepley     PetscCall(DMGetLabel(bdm, lname, &label));
66909f6c5813SMatthew G. Knepley     PetscCall(DMGetLabel(*dm, lname, &labelNew));
66919f6c5813SMatthew G. Knepley     PetscCall(DMLabelSetType(labelNew, DMLABELEPHEMERAL));
66929f6c5813SMatthew G. Knepley     PetscCall(DMLabelEphemeralSetLabel(labelNew, label));
66939f6c5813SMatthew G. Knepley     PetscCall(DMLabelEphemeralSetTransform(labelNew, tr));
66949f6c5813SMatthew G. Knepley     PetscCall(DMLabelSetUp(labelNew));
66959f6c5813SMatthew G. Knepley   }
66963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
66979f6c5813SMatthew G. Knepley }
6698