xref: /petsc/src/dm/impls/plex/plexcreate.c (revision 99acd26ccad0c0164dc0ff85e6875509e1bda4a5)
1552f7358SJed Brown #define PETSCDM_DLL
2af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h> /*I   "petscdmplex.h"   I*/
3cc4c1da9SBarry Smith #include <petsc/private/hashseti.h>
40c312b8eSJed Brown #include <petscsf.h>
5cc4c1da9SBarry Smith #include <petscdmplextransform.h> /*I   "petscdmplextransform.h"   I*/
69f6c5813SMatthew G. Knepley #include <petscdmlabelephemeral.h>
7b7f5c055SJed Brown #include <petsc/private/kernels/blockmatmult.h>
8b7f5c055SJed Brown #include <petsc/private/kernels/blockinvert.h>
9552f7358SJed Brown 
10d0812dedSMatthew G. Knepley #ifdef PETSC_HAVE_UNISTD_H
11d0812dedSMatthew G. Knepley   #include <unistd.h>
12d0812dedSMatthew G. Knepley #endif
13d0812dedSMatthew G. Knepley #include <errno.h>
14d0812dedSMatthew G. Knepley 
15708be2fdSJed Brown PetscLogEvent DMPLEX_CreateFromFile, DMPLEX_CreateFromOptions, DMPLEX_BuildFromCellList, DMPLEX_BuildCoordinatesFromCellList;
1658cd63d5SVaclav Hapla 
179318fe57SMatthew G. Knepley /* External function declarations here */
189318fe57SMatthew G. Knepley static PetscErrorCode DMInitialize_Plex(DM dm);
199318fe57SMatthew G. Knepley 
205552b385SBrandon PETSC_EXTERN PetscErrorCode DMPlexCheckEGADS_Private(DM dm)
215552b385SBrandon {
225552b385SBrandon   PetscObject modelObj;
235552b385SBrandon 
245552b385SBrandon   PetscFunctionBegin;
255552b385SBrandon   PetscCall(PetscObjectQuery((PetscObject)dm, "EGADS Model", &modelObj));
265552b385SBrandon   PetscCheck(modelObj, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Input DM must have attached EGADS Geometry Model");
275552b385SBrandon   PetscFunctionReturn(PETSC_SUCCESS);
285552b385SBrandon }
295552b385SBrandon 
305552b385SBrandon static PetscErrorCode DMPlexCopyContext_Private(DM dmin, const char name[], DM dmout)
315552b385SBrandon {
325552b385SBrandon   PetscObject obj;
335552b385SBrandon 
345552b385SBrandon   PetscFunctionBegin;
355552b385SBrandon   PetscCall(PetscObjectQuery((PetscObject)dmin, name, &obj));
365552b385SBrandon   if (obj) PetscCall(PetscObjectCompose((PetscObject)dmout, name, obj));
375552b385SBrandon   PetscFunctionReturn(PETSC_SUCCESS);
385552b385SBrandon }
395552b385SBrandon 
405552b385SBrandon static PetscErrorCode DMPlexSwapContext_Private(DM dmA, const char name[], DM dmB)
415552b385SBrandon {
425552b385SBrandon   PetscObject objA, objB;
435552b385SBrandon 
445552b385SBrandon   PetscFunctionBegin;
455552b385SBrandon   PetscCall(PetscObjectQuery((PetscObject)dmA, name, &objA));
465552b385SBrandon   PetscCall(PetscObjectQuery((PetscObject)dmB, name, &objB));
475552b385SBrandon   PetscCall(PetscObjectReference(objA));
485552b385SBrandon   PetscCall(PetscObjectReference(objB));
495552b385SBrandon   PetscCall(PetscObjectCompose((PetscObject)dmA, name, objB));
505552b385SBrandon   PetscCall(PetscObjectCompose((PetscObject)dmB, name, objA));
515552b385SBrandon   PetscCall(PetscObjectDereference(objA));
525552b385SBrandon   PetscCall(PetscObjectDereference(objB));
535552b385SBrandon   PetscFunctionReturn(PETSC_SUCCESS);
545552b385SBrandon }
555552b385SBrandon 
565552b385SBrandon PetscErrorCode DMPlexCopyEGADSInfo_Internal(DM dmin, DM dmout)
575552b385SBrandon {
585552b385SBrandon   PetscFunctionBegin;
595552b385SBrandon   PetscCall(DMPlexCopyContext_Private(dmin, "EGADS Model", dmout));
605552b385SBrandon   PetscCall(DMPlexCopyContext_Private(dmin, "EGADS Context", dmout));
615552b385SBrandon   PetscCall(DMPlexCopyContext_Private(dmin, "EGADSlite Model", dmout));
625552b385SBrandon   PetscCall(DMPlexCopyContext_Private(dmin, "EGADSlite Context", dmout));
635552b385SBrandon   PetscFunctionReturn(PETSC_SUCCESS);
645552b385SBrandon }
655552b385SBrandon 
665552b385SBrandon static PetscErrorCode DMPlexSwapEGADSInfo_Private(DM dmA, DM dmB)
675552b385SBrandon {
685552b385SBrandon   PetscFunctionBegin;
695552b385SBrandon   PetscCall(DMPlexSwapContext_Private(dmA, "EGADS Model", dmB));
705552b385SBrandon   PetscCall(DMPlexSwapContext_Private(dmA, "EGADS Context", dmB));
715552b385SBrandon   PetscCall(DMPlexSwapContext_Private(dmA, "EGADSlite Model", dmB));
725552b385SBrandon   PetscCall(DMPlexSwapContext_Private(dmA, "EGADSlite Context", dmB));
735552b385SBrandon   PetscFunctionReturn(PETSC_SUCCESS);
745552b385SBrandon }
755552b385SBrandon 
76e600fa54SMatthew G. Knepley /* This copies internal things in the Plex structure that we generally want when making a new, related Plex */
77d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCopy_Internal(DM dmin, PetscBool copyPeriodicity, PetscBool copyOverlap, DM dmout)
78d71ae5a4SJacob Faibussowitsch {
794fb89dddSMatthew G. Knepley   const PetscReal     *maxCell, *Lstart, *L;
8012a88998SMatthew G. Knepley   VecType              vecType;
8112a88998SMatthew G. Knepley   MatType              matType;
82129f447cSJames Wright   PetscBool            dist, useCeed, balance_partition;
83adc21957SMatthew G. Knepley   DMReorderDefaultFlag reorder;
84e600fa54SMatthew G. Knepley 
85e600fa54SMatthew G. Knepley   PetscFunctionBegin;
86835f2295SStefano Zampini   if (dmin == dmout) PetscFunctionReturn(PETSC_SUCCESS);
8712a88998SMatthew G. Knepley   PetscCall(DMGetVecType(dmin, &vecType));
8812a88998SMatthew G. Knepley   PetscCall(DMSetVecType(dmout, vecType));
8912a88998SMatthew G. Knepley   PetscCall(DMGetMatType(dmin, &matType));
9012a88998SMatthew G. Knepley   PetscCall(DMSetMatType(dmout, matType));
91e600fa54SMatthew G. Knepley   if (copyPeriodicity) {
924fb89dddSMatthew G. Knepley     PetscCall(DMGetPeriodicity(dmin, &maxCell, &Lstart, &L));
934fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dmout, maxCell, Lstart, L));
943d0e8ed9SDavid Salac     PetscCall(DMLocalizeCoordinates(dmout));
95e600fa54SMatthew G. Knepley   }
969566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeGetDefault(dmin, &dist));
979566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeSetDefault(dmout, dist));
986bc1bd01Sksagiyam   PetscCall(DMPlexReorderGetDefault(dmin, &reorder));
996bc1bd01Sksagiyam   PetscCall(DMPlexReorderSetDefault(dmout, reorder));
1005962854dSMatthew G. Knepley   PetscCall(DMPlexGetUseCeed(dmin, &useCeed));
1015962854dSMatthew G. Knepley   PetscCall(DMPlexSetUseCeed(dmout, useCeed));
102129f447cSJames Wright   PetscCall(DMPlexGetPartitionBalance(dmin, &balance_partition));
103129f447cSJames Wright   PetscCall(DMPlexSetPartitionBalance(dmout, balance_partition));
104e600fa54SMatthew G. Knepley   ((DM_Plex *)dmout->data)->useHashLocation = ((DM_Plex *)dmin->data)->useHashLocation;
1055962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printSetValues  = ((DM_Plex *)dmin->data)->printSetValues;
1065962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printFEM        = ((DM_Plex *)dmin->data)->printFEM;
1075962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printFVM        = ((DM_Plex *)dmin->data)->printFVM;
1085962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printL2         = ((DM_Plex *)dmin->data)->printL2;
1095962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printLocate     = ((DM_Plex *)dmin->data)->printLocate;
110a77a5016SMatthew G. Knepley   ((DM_Plex *)dmout->data)->printProject    = ((DM_Plex *)dmin->data)->printProject;
1115962854dSMatthew G. Knepley   ((DM_Plex *)dmout->data)->printTol        = ((DM_Plex *)dmin->data)->printTol;
1121baa6e33SBarry Smith   if (copyOverlap) PetscCall(DMPlexSetOverlap_Plex(dmout, dmin, 0));
1135552b385SBrandon   PetscCall(DMPlexCopyEGADSInfo_Internal(dmin, dmout));
1143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
115e600fa54SMatthew G. Knepley }
116e600fa54SMatthew G. Knepley 
1179318fe57SMatthew G. Knepley /* Replace dm with the contents of ndm, and then destroy ndm
1189318fe57SMatthew G. Knepley    - Share the DM_Plex structure
1199318fe57SMatthew G. Knepley    - Share the coordinates
1209318fe57SMatthew G. Knepley    - Share the SF
1219318fe57SMatthew G. Knepley */
122d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexReplace_Internal(DM dm, DM *ndm)
123d71ae5a4SJacob Faibussowitsch {
1249318fe57SMatthew G. Knepley   PetscSF          sf;
1259318fe57SMatthew G. Knepley   DM               dmNew = *ndm, coordDM, coarseDM;
1269318fe57SMatthew G. Knepley   Vec              coords;
1272192575eSBarry Smith   PetscPointFn    *coordFunc;
1284fb89dddSMatthew G. Knepley   const PetscReal *maxCell, *Lstart, *L;
1299318fe57SMatthew G. Knepley   PetscInt         dim, cdim;
130e535cce4SJames Wright   PetscBool        use_natural;
1319318fe57SMatthew G. Knepley 
1329318fe57SMatthew G. Knepley   PetscFunctionBegin;
1339318fe57SMatthew G. Knepley   if (dm == dmNew) {
1349566063dSJacob Faibussowitsch     PetscCall(DMDestroy(ndm));
1353ba16761SJacob Faibussowitsch     PetscFunctionReturn(PETSC_SUCCESS);
1369318fe57SMatthew G. Knepley   }
1379318fe57SMatthew G. Knepley   dm->setupcalled = dmNew->setupcalled;
138d0812dedSMatthew G. Knepley   if (!dm->hdr.name) {
139d0812dedSMatthew G. Knepley     const char *name;
140d0812dedSMatthew G. Knepley 
141d0812dedSMatthew G. Knepley     PetscCall(PetscObjectGetName((PetscObject)*ndm, &name));
142d0812dedSMatthew G. Knepley     PetscCall(PetscObjectSetName((PetscObject)dm, name));
143d0812dedSMatthew G. Knepley   }
1443674be70SMatthew G. Knepley   {
1453674be70SMatthew G. Knepley     PetscInt ndim;
1463674be70SMatthew G. Knepley 
1473674be70SMatthew G. Knepley     // If topological dimensions are the same, we retain the old coordinate map,
1483674be70SMatthew G. Knepley     //   otherwise we overwrite with the new one
1493674be70SMatthew G. Knepley     PetscCall(DMGetDimension(dm, &dim));
1503674be70SMatthew G. Knepley     PetscCall(DMGetDimension(dmNew, &ndim));
1513674be70SMatthew G. Knepley     PetscCall(DMPlexGetCoordinateMap(dm, &coordFunc));
1523674be70SMatthew G. Knepley     if (dim == ndim) PetscCall(DMPlexSetCoordinateMap(dmNew, coordFunc));
1533674be70SMatthew G. Knepley   }
1549566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dmNew, &dim));
1559566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
1569566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dmNew, &cdim));
1579566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, cdim));
1589566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmNew, &sf));
1599566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dm, sf));
1609566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmNew, &coordDM));
1619566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmNew, &coords));
1629566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dm, coordDM));
1639566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coords));
1646858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dmNew, &coordDM));
1656858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dmNew, &coords));
1666858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinateDM(dm, coordDM));
1676858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinatesLocal(dm, coords));
1689318fe57SMatthew G. Knepley   /* Do not want to create the coordinate field if it does not already exist, so do not call DMGetCoordinateField() */
1696858538eSMatthew G. Knepley   PetscCall(DMFieldDestroy(&dm->coordinates[0].field));
1706858538eSMatthew G. Knepley   dm->coordinates[0].field = dmNew->coordinates[0].field;
1714fb89dddSMatthew G. Knepley   PetscCall(DMGetPeriodicity(dmNew, &maxCell, &Lstart, &L));
1724fb89dddSMatthew G. Knepley   PetscCall(DMSetPeriodicity(dm, maxCell, Lstart, L));
173e535cce4SJames Wright   PetscCall(DMGetNaturalSF(dmNew, &sf));
174e535cce4SJames Wright   PetscCall(DMSetNaturalSF(dm, sf));
175e535cce4SJames Wright   PetscCall(DMGetUseNatural(dmNew, &use_natural));
176e535cce4SJames Wright   PetscCall(DMSetUseNatural(dm, use_natural));
1779566063dSJacob Faibussowitsch   PetscCall(DMDestroy_Plex(dm));
1789566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(dm));
1799318fe57SMatthew G. Knepley   dm->data = dmNew->data;
1809318fe57SMatthew G. Knepley   ((DM_Plex *)dmNew->data)->refct++;
1811fca310dSJames Wright   {
1821fca310dSJames Wright     PetscInt       num_face_sfs;
1831fca310dSJames Wright     const PetscSF *sfs;
1841fca310dSJames Wright     PetscCall(DMPlexGetIsoperiodicFaceSF(dm, &num_face_sfs, &sfs));
1851fca310dSJames Wright     PetscCall(DMPlexSetIsoperiodicFaceSF(dm, num_face_sfs, (PetscSF *)sfs)); // for the compose function effect on dm
1861fca310dSJames Wright   }
1879566063dSJacob Faibussowitsch   PetscCall(DMDestroyLabelLinkList_Internal(dm));
1889566063dSJacob Faibussowitsch   PetscCall(DMCopyLabels(dmNew, dm, PETSC_OWN_POINTER, PETSC_TRUE, DM_COPY_LABELS_FAIL));
1899566063dSJacob Faibussowitsch   PetscCall(DMGetCoarseDM(dmNew, &coarseDM));
1909566063dSJacob Faibussowitsch   PetscCall(DMSetCoarseDM(dm, coarseDM));
1915552b385SBrandon   PetscCall(DMPlexCopyEGADSInfo_Internal(dmNew, dm));
1929566063dSJacob Faibussowitsch   PetscCall(DMDestroy(ndm));
1933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1949318fe57SMatthew G. Knepley }
1959318fe57SMatthew G. Knepley 
1969318fe57SMatthew G. Knepley /* Swap dm with the contents of dmNew
1979318fe57SMatthew G. Knepley    - Swap the DM_Plex structure
1989318fe57SMatthew G. Knepley    - Swap the coordinates
1999318fe57SMatthew G. Knepley    - Swap the point PetscSF
2009318fe57SMatthew G. Knepley */
201d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexSwap_Static(DM dmA, DM dmB)
202d71ae5a4SJacob Faibussowitsch {
2039318fe57SMatthew G. Knepley   DM          coordDMA, coordDMB;
2049318fe57SMatthew G. Knepley   Vec         coordsA, coordsB;
2059318fe57SMatthew G. Knepley   PetscSF     sfA, sfB;
2069318fe57SMatthew G. Knepley   DMField     fieldTmp;
2079318fe57SMatthew G. Knepley   void       *tmp;
2089318fe57SMatthew G. Knepley   DMLabelLink listTmp;
2099318fe57SMatthew G. Knepley   DMLabel     depthTmp;
2109318fe57SMatthew G. Knepley   PetscInt    tmpI;
2119318fe57SMatthew G. Knepley 
2129318fe57SMatthew G. Knepley   PetscFunctionBegin;
2133ba16761SJacob Faibussowitsch   if (dmA == dmB) PetscFunctionReturn(PETSC_SUCCESS);
2149566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmA, &sfA));
2159566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmB, &sfB));
2169566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sfA));
2179566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dmA, sfB));
2189566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dmB, sfA));
2199566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)sfA));
2209318fe57SMatthew G. Knepley 
2219566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmA, &coordDMA));
2229566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmB, &coordDMB));
2239566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)coordDMA));
2249566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dmA, coordDMB));
2259566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dmB, coordDMA));
2269566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)coordDMA));
2279318fe57SMatthew G. Knepley 
2289566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmA, &coordsA));
2299566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmB, &coordsB));
2309566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)coordsA));
2319566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmA, coordsB));
2329566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmB, coordsA));
2339566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)coordsA));
2349318fe57SMatthew G. Knepley 
2356858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dmA, &coordDMA));
2366858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dmB, &coordDMB));
2376858538eSMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)coordDMA));
2386858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinateDM(dmA, coordDMB));
2396858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinateDM(dmB, coordDMA));
2406858538eSMatthew G. Knepley   PetscCall(PetscObjectDereference((PetscObject)coordDMA));
2416858538eSMatthew G. Knepley 
2426858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dmA, &coordsA));
2436858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dmB, &coordsB));
2446858538eSMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)coordsA));
2456858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinatesLocal(dmA, coordsB));
2466858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinatesLocal(dmB, coordsA));
2476858538eSMatthew G. Knepley   PetscCall(PetscObjectDereference((PetscObject)coordsA));
2486858538eSMatthew G. Knepley 
2495552b385SBrandon   PetscCall(DMPlexSwapEGADSInfo_Private(dmA, dmB));
2505552b385SBrandon 
2516858538eSMatthew G. Knepley   fieldTmp                  = dmA->coordinates[0].field;
2526858538eSMatthew G. Knepley   dmA->coordinates[0].field = dmB->coordinates[0].field;
2536858538eSMatthew G. Knepley   dmB->coordinates[0].field = fieldTmp;
2546858538eSMatthew G. Knepley   fieldTmp                  = dmA->coordinates[1].field;
2556858538eSMatthew G. Knepley   dmA->coordinates[1].field = dmB->coordinates[1].field;
2566858538eSMatthew G. Knepley   dmB->coordinates[1].field = fieldTmp;
2579318fe57SMatthew G. Knepley   tmp                       = dmA->data;
2589318fe57SMatthew G. Knepley   dmA->data                 = dmB->data;
2599318fe57SMatthew G. Knepley   dmB->data                 = tmp;
2609318fe57SMatthew G. Knepley   listTmp                   = dmA->labels;
2619318fe57SMatthew G. Knepley   dmA->labels               = dmB->labels;
2629318fe57SMatthew G. Knepley   dmB->labels               = listTmp;
2639318fe57SMatthew G. Knepley   depthTmp                  = dmA->depthLabel;
2649318fe57SMatthew G. Knepley   dmA->depthLabel           = dmB->depthLabel;
2659318fe57SMatthew G. Knepley   dmB->depthLabel           = depthTmp;
2669318fe57SMatthew G. Knepley   depthTmp                  = dmA->celltypeLabel;
2679318fe57SMatthew G. Knepley   dmA->celltypeLabel        = dmB->celltypeLabel;
2689318fe57SMatthew G. Knepley   dmB->celltypeLabel        = depthTmp;
2699318fe57SMatthew G. Knepley   tmpI                      = dmA->levelup;
2709318fe57SMatthew G. Knepley   dmA->levelup              = dmB->levelup;
2719318fe57SMatthew G. Knepley   dmB->levelup              = tmpI;
2723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2739318fe57SMatthew G. Knepley }
2749318fe57SMatthew G. Knepley 
2753431e603SJed Brown PetscErrorCode DMPlexInterpolateInPlace_Internal(DM dm)
276d71ae5a4SJacob Faibussowitsch {
2779318fe57SMatthew G. Knepley   DM idm;
2789318fe57SMatthew G. Knepley 
2799318fe57SMatthew G. Knepley   PetscFunctionBegin;
2809566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolate(dm, &idm));
2819566063dSJacob Faibussowitsch   PetscCall(DMPlexCopyCoordinates(dm, idm));
28269d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &idm));
2833ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2849318fe57SMatthew G. Knepley }
2859318fe57SMatthew G. Knepley 
2869318fe57SMatthew G. Knepley /*@C
2879318fe57SMatthew G. Knepley   DMPlexCreateCoordinateSpace - Creates a finite element space for the coordinates
2889318fe57SMatthew G. Knepley 
28920f4b53cSBarry Smith   Collective
2909318fe57SMatthew G. Knepley 
2919318fe57SMatthew G. Knepley   Input Parameters:
29260225df5SJacob Faibussowitsch + dm        - The `DMPLEX`
29320f4b53cSBarry Smith . degree    - The degree of the finite element or `PETSC_DECIDE`
294e44f6aebSMatthew G. Knepley . project   - Flag to project current coordinates into the space
2959318fe57SMatthew G. Knepley - coordFunc - An optional function to map new points from refinement to the surface
2969318fe57SMatthew G. Knepley 
2979318fe57SMatthew G. Knepley   Level: advanced
2989318fe57SMatthew G. Knepley 
2992192575eSBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `PetscPointFn`, `PetscFECreateLagrange()`, `DMGetCoordinateDM()`
3009318fe57SMatthew G. Knepley @*/
3012192575eSBarry Smith PetscErrorCode DMPlexCreateCoordinateSpace(DM dm, PetscInt degree, PetscBool project, PetscPointFn *coordFunc)
302d71ae5a4SJacob Faibussowitsch {
303e44f6aebSMatthew G. Knepley   PetscFE  fe = NULL;
3049318fe57SMatthew G. Knepley   DM       cdm;
305ac9d17c7SMatthew G. Knepley   PetscInt dim, cdim, dE, qorder, height;
3069318fe57SMatthew G. Knepley 
307e44f6aebSMatthew G. Knepley   PetscFunctionBegin;
3089566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
309ac9d17c7SMatthew G. Knepley   cdim = dim;
3109566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dE));
3119318fe57SMatthew G. Knepley   qorder = degree;
312e44f6aebSMatthew G. Knepley   PetscCall(DMGetCoordinateDM(dm, &cdm));
313509b31aaSMatthew G. Knepley   if (!coordFunc) PetscCall(DMPlexGetCoordinateMap(dm, &coordFunc));
314d0609cedSBarry Smith   PetscObjectOptionsBegin((PetscObject)cdm);
315dc431b0cSMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-default_quadrature_order", "Quadrature order is one less than quadrature points per edge", "DMPlexCreateCoordinateSpace", qorder, &qorder, NULL, 0));
316ac9d17c7SMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_coordinate_dim", "Set the coordinate dimension", "DMPlexCreateCoordinateSpace", cdim, &cdim, NULL, dim));
317d0609cedSBarry Smith   PetscOptionsEnd();
3181df12153SMatthew G. Knepley   PetscCall(DMPlexGetVTKCellHeight(dm, &height));
319ac9d17c7SMatthew G. Knepley   if (cdim > dim) {
320ac9d17c7SMatthew G. Knepley     DM           cdm;
321ac9d17c7SMatthew G. Knepley     PetscSection cs, csNew;
322ac9d17c7SMatthew G. Knepley     Vec          coordinates, coordinatesNew;
323ac9d17c7SMatthew G. Knepley     VecType      vectype;
324ac9d17c7SMatthew G. Knepley     IS           idx;
325ac9d17c7SMatthew G. Knepley     PetscInt    *indices;
326ac9d17c7SMatthew G. Knepley     PetscInt     bs, n;
327ac9d17c7SMatthew G. Knepley 
328ac9d17c7SMatthew G. Knepley     // Recreate coordinate section
329ac9d17c7SMatthew G. Knepley     {
330ac9d17c7SMatthew G. Knepley       const char *fieldName = NULL, *compName = NULL;
331ac9d17c7SMatthew G. Knepley       PetscInt    Nc, pStart, pEnd;
332ac9d17c7SMatthew G. Knepley 
333ac9d17c7SMatthew G. Knepley       PetscCall(DMGetCoordinateDM(dm, &cdm));
334ac9d17c7SMatthew G. Knepley       PetscCall(DMGetLocalSection(cdm, &cs));
335ac9d17c7SMatthew G. Knepley       PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)cs), &csNew));
336ac9d17c7SMatthew G. Knepley       PetscCall(PetscSectionSetNumFields(csNew, 1));
337ac9d17c7SMatthew G. Knepley       PetscCall(PetscSectionGetFieldName(cs, 0, &fieldName));
338ac9d17c7SMatthew G. Knepley       PetscCall(PetscSectionSetFieldName(csNew, 0, fieldName));
339ac9d17c7SMatthew G. Knepley       PetscCall(PetscSectionGetFieldComponents(cs, 0, &Nc));
340ac9d17c7SMatthew G. Knepley       PetscCall(PetscSectionSetFieldComponents(csNew, 0, cdim));
341ac9d17c7SMatthew G. Knepley       for (PetscInt c = 0; c < Nc; ++c) {
342ac9d17c7SMatthew G. Knepley         PetscCall(PetscSectionGetComponentName(cs, 0, c, &compName));
343ac9d17c7SMatthew G. Knepley         PetscCall(PetscSectionSetComponentName(csNew, 0, c, compName));
344ac9d17c7SMatthew G. Knepley       }
345ac9d17c7SMatthew G. Knepley       PetscCall(PetscSectionGetChart(cs, &pStart, &pEnd));
346ac9d17c7SMatthew G. Knepley       PetscCall(PetscSectionSetChart(csNew, pStart, pEnd));
347ac9d17c7SMatthew G. Knepley       for (PetscInt p = pStart; p < pEnd; ++p) {
348530e699aSMatthew G. Knepley         PetscInt dof;
349530e699aSMatthew G. Knepley 
350530e699aSMatthew G. Knepley         PetscCall(PetscSectionGetDof(cs, p, &dof));
351530e699aSMatthew G. Knepley         if (dof) {
352ac9d17c7SMatthew G. Knepley           PetscCall(PetscSectionSetDof(csNew, p, cdim));
353ac9d17c7SMatthew G. Knepley           PetscCall(PetscSectionSetFieldDof(csNew, p, 0, cdim));
354ac9d17c7SMatthew G. Knepley         }
355530e699aSMatthew G. Knepley       }
356ac9d17c7SMatthew G. Knepley       PetscCall(PetscSectionSetUp(csNew));
357ac9d17c7SMatthew G. Knepley     }
358ac9d17c7SMatthew G. Knepley     PetscCall(DMSetLocalSection(cdm, csNew));
359ac9d17c7SMatthew G. Knepley     PetscCall(PetscSectionDestroy(&csNew));
360530e699aSMatthew G. Knepley     // Reset coordinate dimension for coordinate DM
361530e699aSMatthew G. Knepley     PetscCall(DMSetCoordinateDim(cdm, cdim));
362530e699aSMatthew G. Knepley     PetscCall(DMSetCoordinateField(cdm, NULL));
363ac9d17c7SMatthew G. Knepley     // Inject coordinates into higher dimension
364ac9d17c7SMatthew G. Knepley     PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
365ac9d17c7SMatthew G. Knepley     PetscCall(VecGetBlockSize(coordinates, &bs));
366ac9d17c7SMatthew G. Knepley     PetscCheck(bs == dim, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "We can only inject simple coordinates into a higher dimension");
367ac9d17c7SMatthew G. Knepley     PetscCall(VecCreate(PetscObjectComm((PetscObject)coordinates), &coordinatesNew));
368ac9d17c7SMatthew G. Knepley     PetscCall(VecGetType(coordinates, &vectype));
369ac9d17c7SMatthew G. Knepley     PetscCall(VecSetType(coordinatesNew, vectype));
370ac9d17c7SMatthew G. Knepley     PetscCall(VecGetLocalSize(coordinates, &n));
371ac9d17c7SMatthew G. Knepley     PetscCheck(!(n % bs), PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "We can only inject simple coordinates into a higher dimension");
372ac9d17c7SMatthew G. Knepley     n /= bs;
373ac9d17c7SMatthew G. Knepley     PetscCall(VecSetSizes(coordinatesNew, n * cdim, PETSC_DETERMINE));
374ac9d17c7SMatthew G. Knepley     PetscCall(VecSetUp(coordinatesNew));
375ac9d17c7SMatthew G. Knepley     PetscCall(PetscMalloc1(n * bs, &indices));
376ac9d17c7SMatthew G. Knepley     for (PetscInt i = 0; i < n; ++i)
377ac9d17c7SMatthew G. Knepley       for (PetscInt b = 0; b < bs; ++b) indices[i * bs + b] = i * cdim + b;
378ac9d17c7SMatthew G. Knepley     PetscCall(ISCreateGeneral(PETSC_COMM_SELF, n * bs, indices, PETSC_OWN_POINTER, &idx));
379ac9d17c7SMatthew G. Knepley     PetscCall(VecISCopy(coordinatesNew, idx, SCATTER_FORWARD, coordinates));
380ac9d17c7SMatthew G. Knepley     PetscCall(ISDestroy(&idx));
381ac9d17c7SMatthew G. Knepley     PetscCall(DMSetCoordinatesLocal(dm, coordinatesNew));
382ac9d17c7SMatthew G. Knepley     PetscCall(VecDestroy(&coordinatesNew));
383ac9d17c7SMatthew G. Knepley     PetscCall(DMSetCoordinateDim(dm, cdim));
384ac9d17c7SMatthew G. Knepley     {
385ac9d17c7SMatthew G. Knepley       PetscInt gn;
386ac9d17c7SMatthew G. Knepley 
387ac9d17c7SMatthew G. Knepley       PetscCall(DMGetCoordinates(dm, &coordinatesNew));
388ac9d17c7SMatthew G. Knepley       PetscCall(VecGetLocalSize(coordinatesNew, &gn));
389ac9d17c7SMatthew G. Knepley       PetscCheck(gn == n * cdim, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Global coordinate size %" PetscInt_FMT " != %" PetscInt_FMT "local coordinate size", gn, n * cdim);
390ac9d17c7SMatthew G. Knepley     }
391ac9d17c7SMatthew G. Knepley     dE      = cdim;
392530e699aSMatthew G. Knepley     project = PETSC_FALSE;
393ac9d17c7SMatthew G. Knepley   }
394e44f6aebSMatthew G. Knepley   if (degree >= 0) {
395e44f6aebSMatthew G. Knepley     DMPolytopeType ct = DM_POLYTOPE_UNKNOWN;
396e44f6aebSMatthew G. Knepley     PetscInt       cStart, cEnd, gct;
397dc431b0cSMatthew G. Knepley 
3981df12153SMatthew G. Knepley     PetscCall(DMPlexGetHeightStratum(dm, height, &cStart, &cEnd));
399dc431b0cSMatthew G. Knepley     if (cEnd > cStart) PetscCall(DMPlexGetCellType(dm, cStart, &ct));
400e44f6aebSMatthew G. Knepley     gct = (PetscInt)ct;
401462c564dSBarry Smith     PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &gct, 1, MPIU_INT, MPI_MIN, PetscObjectComm((PetscObject)dm)));
402e44f6aebSMatthew G. Knepley     ct = (DMPolytopeType)gct;
403e44f6aebSMatthew G. Knepley     // Work around current bug in PetscDualSpaceSetUp_Lagrange()
404e44f6aebSMatthew G. Knepley     //   Can be seen in plex_tutorials-ex10_1
405e44f6aebSMatthew 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));
4064f9ab2b4SJed Brown   }
407e44f6aebSMatthew G. Knepley   PetscCall(DMSetCoordinateDisc(dm, fe, project));
4089566063dSJacob Faibussowitsch   PetscCall(PetscFEDestroy(&fe));
4093ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4109318fe57SMatthew G. Knepley }
4119318fe57SMatthew G. Knepley 
4121df5d5c5SMatthew G. Knepley /*@
4131df5d5c5SMatthew G. Knepley   DMPlexCreateDoublet - Creates a mesh of two cells of the specified type, optionally with later refinement.
4141df5d5c5SMatthew G. Knepley 
415d083f849SBarry Smith   Collective
4161df5d5c5SMatthew G. Knepley 
4171df5d5c5SMatthew G. Knepley   Input Parameters:
418a1cb98faSBarry Smith + comm            - The communicator for the `DM` object
4191df5d5c5SMatthew G. Knepley . dim             - The spatial dimension
4201df5d5c5SMatthew G. Knepley . simplex         - Flag for simplicial cells, otherwise they are tensor product cells
4211df5d5c5SMatthew G. Knepley . interpolate     - Flag to create intermediate mesh pieces (edges, faces)
4221df5d5c5SMatthew G. Knepley - refinementLimit - A nonzero number indicates the largest admissible volume for a refined cell
4231df5d5c5SMatthew G. Knepley 
4241df5d5c5SMatthew G. Knepley   Output Parameter:
42560225df5SJacob Faibussowitsch . newdm - The `DM` object
4261df5d5c5SMatthew G. Knepley 
4271df5d5c5SMatthew G. Knepley   Level: beginner
4281df5d5c5SMatthew G. Knepley 
4291cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMSetType()`, `DMCreate()`
4301df5d5c5SMatthew G. Knepley @*/
431d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateDoublet(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscBool interpolate, PetscReal refinementLimit, DM *newdm)
432d71ae5a4SJacob Faibussowitsch {
4331df5d5c5SMatthew G. Knepley   DM          dm;
4341df5d5c5SMatthew G. Knepley   PetscMPIInt rank;
4351df5d5c5SMatthew G. Knepley 
4361df5d5c5SMatthew G. Knepley   PetscFunctionBegin;
4379566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, &dm));
4389566063dSJacob Faibussowitsch   PetscCall(DMSetType(dm, DMPLEX));
4399566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
44046139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
4419566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
442ce78fa2fSMatthew G. Knepley   switch (dim) {
443ce78fa2fSMatthew G. Knepley   case 2:
4449566063dSJacob Faibussowitsch     if (simplex) PetscCall(PetscObjectSetName((PetscObject)dm, "triangular"));
4459566063dSJacob Faibussowitsch     else PetscCall(PetscObjectSetName((PetscObject)dm, "quadrilateral"));
446ce78fa2fSMatthew G. Knepley     break;
447ce78fa2fSMatthew G. Knepley   case 3:
4489566063dSJacob Faibussowitsch     if (simplex) PetscCall(PetscObjectSetName((PetscObject)dm, "tetrahedral"));
4499566063dSJacob Faibussowitsch     else PetscCall(PetscObjectSetName((PetscObject)dm, "hexahedral"));
450ce78fa2fSMatthew G. Knepley     break;
451d71ae5a4SJacob Faibussowitsch   default:
452d71ae5a4SJacob Faibussowitsch     SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %" PetscInt_FMT, dim);
453ce78fa2fSMatthew G. Knepley   }
4541df5d5c5SMatthew G. Knepley   if (rank) {
4551df5d5c5SMatthew G. Knepley     PetscInt numPoints[2] = {0, 0};
4569566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, NULL, NULL, NULL, NULL));
4571df5d5c5SMatthew G. Knepley   } else {
4581df5d5c5SMatthew G. Knepley     switch (dim) {
4591df5d5c5SMatthew G. Knepley     case 2:
4601df5d5c5SMatthew G. Knepley       if (simplex) {
4611df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {4, 2};
4621df5d5c5SMatthew G. Knepley         PetscInt    coneSize[6]         = {3, 3, 0, 0, 0, 0};
4631df5d5c5SMatthew G. Knepley         PetscInt    cones[6]            = {2, 3, 4, 5, 4, 3};
4641df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
4651df5d5c5SMatthew G. Knepley         PetscScalar vertexCoords[8]     = {-0.5, 0.5, 0.0, 0.0, 0.0, 1.0, 0.5, 0.5};
4661df5d5c5SMatthew G. Knepley 
4679566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
4681df5d5c5SMatthew G. Knepley       } else {
4691df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {6, 2};
4701df5d5c5SMatthew G. Knepley         PetscInt    coneSize[8]         = {4, 4, 0, 0, 0, 0, 0, 0};
4711df5d5c5SMatthew G. Knepley         PetscInt    cones[8]            = {2, 3, 4, 5, 3, 6, 7, 4};
4721df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
4731df5d5c5SMatthew 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};
4741df5d5c5SMatthew G. Knepley 
4759566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
4761df5d5c5SMatthew G. Knepley       }
4771df5d5c5SMatthew G. Knepley       break;
4781df5d5c5SMatthew G. Knepley     case 3:
4791df5d5c5SMatthew G. Knepley       if (simplex) {
4801df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {5, 2};
4811df5d5c5SMatthew G. Knepley         PetscInt    coneSize[7]         = {4, 4, 0, 0, 0, 0, 0};
4821df5d5c5SMatthew G. Knepley         PetscInt    cones[8]            = {4, 3, 5, 2, 5, 3, 4, 6};
4831df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
4841df5d5c5SMatthew 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};
4851df5d5c5SMatthew G. Knepley 
4869566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
4871df5d5c5SMatthew G. Knepley       } else {
4881df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]         = {12, 2};
4891df5d5c5SMatthew G. Knepley         PetscInt    coneSize[14]         = {8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
4901df5d5c5SMatthew G. Knepley         PetscInt    cones[16]            = {2, 3, 4, 5, 6, 7, 8, 9, 5, 4, 10, 11, 7, 12, 13, 8};
4911df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
4929371c9d4SSatish 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};
4931df5d5c5SMatthew G. Knepley 
4949566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
4951df5d5c5SMatthew G. Knepley       }
4961df5d5c5SMatthew G. Knepley       break;
497d71ae5a4SJacob Faibussowitsch     default:
498d71ae5a4SJacob Faibussowitsch       SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %" PetscInt_FMT, dim);
4991df5d5c5SMatthew G. Knepley     }
5001df5d5c5SMatthew G. Knepley   }
50146139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
5021df5d5c5SMatthew G. Knepley   *newdm = dm;
5031df5d5c5SMatthew G. Knepley   if (refinementLimit > 0.0) {
5041df5d5c5SMatthew G. Knepley     DM          rdm;
5051df5d5c5SMatthew G. Knepley     const char *name;
5061df5d5c5SMatthew G. Knepley 
5079566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementUniform(*newdm, PETSC_FALSE));
5089566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementLimit(*newdm, refinementLimit));
5099566063dSJacob Faibussowitsch     PetscCall(DMRefine(*newdm, comm, &rdm));
5109566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)*newdm, &name));
5119566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)rdm, name));
5129566063dSJacob Faibussowitsch     PetscCall(DMDestroy(newdm));
5131df5d5c5SMatthew G. Knepley     *newdm = rdm;
5141df5d5c5SMatthew G. Knepley   }
5151df5d5c5SMatthew G. Knepley   if (interpolate) {
5165fd9971aSMatthew G. Knepley     DM idm;
5171df5d5c5SMatthew G. Knepley 
5189566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*newdm, &idm));
5199566063dSJacob Faibussowitsch     PetscCall(DMDestroy(newdm));
5201df5d5c5SMatthew G. Knepley     *newdm = idm;
5211df5d5c5SMatthew G. Knepley   }
5223ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5231df5d5c5SMatthew G. Knepley }
5241df5d5c5SMatthew G. Knepley 
525d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
526d71ae5a4SJacob Faibussowitsch {
5279318fe57SMatthew G. Knepley   const PetscInt numVertices    = 2;
5289318fe57SMatthew G. Knepley   PetscInt       markerRight    = 1;
5299318fe57SMatthew G. Knepley   PetscInt       markerLeft     = 1;
5309318fe57SMatthew G. Knepley   PetscBool      markerSeparate = PETSC_FALSE;
5319318fe57SMatthew G. Knepley   Vec            coordinates;
5329318fe57SMatthew G. Knepley   PetscSection   coordSection;
5339318fe57SMatthew G. Knepley   PetscScalar   *coords;
5349318fe57SMatthew G. Knepley   PetscInt       coordSize;
5359318fe57SMatthew G. Knepley   PetscMPIInt    rank;
5369318fe57SMatthew G. Knepley   PetscInt       cdim = 1, v;
537552f7358SJed Brown 
5389318fe57SMatthew G. Knepley   PetscFunctionBegin;
5399566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
5409318fe57SMatthew G. Knepley   if (markerSeparate) {
5419318fe57SMatthew G. Knepley     markerRight = 2;
5429318fe57SMatthew G. Knepley     markerLeft  = 1;
5439318fe57SMatthew G. Knepley   }
5449566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
545c5853193SPierre Jolivet   if (rank == 0) {
5469566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numVertices));
5479566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
5489566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", 0, markerLeft));
5499566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", 1, markerRight));
5509318fe57SMatthew G. Knepley   }
5519566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
5529566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
5539318fe57SMatthew G. Knepley   /* Build coordinates */
5549566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, cdim));
5559566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
5569566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
5579566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, 0, numVertices));
5589566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, cdim));
5599318fe57SMatthew G. Knepley   for (v = 0; v < numVertices; ++v) {
5609566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, cdim));
5619566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, cdim));
5629318fe57SMatthew G. Knepley   }
5639566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
5649566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
5659566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
5669566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
5679566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
5689566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, cdim));
5699566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
5709566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
5719318fe57SMatthew G. Knepley   coords[0] = lower[0];
5729318fe57SMatthew G. Knepley   coords[1] = upper[0];
5739566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
5749566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
5759566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
5763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5779318fe57SMatthew G. Knepley }
57826492d91SMatthew G. Knepley 
579d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
580d71ae5a4SJacob Faibussowitsch {
5811df21d24SMatthew G. Knepley   const PetscInt numVertices    = (edges[0] + 1) * (edges[1] + 1);
5821df21d24SMatthew G. Knepley   const PetscInt numEdges       = edges[0] * (edges[1] + 1) + (edges[0] + 1) * edges[1];
583552f7358SJed Brown   PetscInt       markerTop      = 1;
584552f7358SJed Brown   PetscInt       markerBottom   = 1;
585552f7358SJed Brown   PetscInt       markerRight    = 1;
586552f7358SJed Brown   PetscInt       markerLeft     = 1;
587552f7358SJed Brown   PetscBool      markerSeparate = PETSC_FALSE;
588552f7358SJed Brown   Vec            coordinates;
589552f7358SJed Brown   PetscSection   coordSection;
590552f7358SJed Brown   PetscScalar   *coords;
591552f7358SJed Brown   PetscInt       coordSize;
592552f7358SJed Brown   PetscMPIInt    rank;
593552f7358SJed Brown   PetscInt       v, vx, vy;
594552f7358SJed Brown 
595552f7358SJed Brown   PetscFunctionBegin;
5969566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
597552f7358SJed Brown   if (markerSeparate) {
5981df21d24SMatthew G. Knepley     markerTop    = 3;
5991df21d24SMatthew G. Knepley     markerBottom = 1;
6001df21d24SMatthew G. Knepley     markerRight  = 2;
6011df21d24SMatthew G. Knepley     markerLeft   = 4;
602552f7358SJed Brown   }
6039566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
604dd400576SPatrick Sanan   if (rank == 0) {
605552f7358SJed Brown     PetscInt e, ex, ey;
606552f7358SJed Brown 
6079566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numEdges + numVertices));
60848a46eb9SPierre Jolivet     for (e = 0; e < numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2));
6099566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
610552f7358SJed Brown     for (vx = 0; vx <= edges[0]; vx++) {
611552f7358SJed Brown       for (ey = 0; ey < edges[1]; ey++) {
612552f7358SJed Brown         PetscInt edge   = vx * edges[1] + ey + edges[0] * (edges[1] + 1);
613552f7358SJed Brown         PetscInt vertex = ey * (edges[0] + 1) + vx + numEdges;
614da80777bSKarl Rupp         PetscInt cone[2];
615552f7358SJed Brown 
6169371c9d4SSatish Balay         cone[0] = vertex;
6179371c9d4SSatish Balay         cone[1] = vertex + edges[0] + 1;
6189566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, edge, cone));
619552f7358SJed Brown         if (vx == edges[0]) {
6209566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
6219566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
622552f7358SJed Brown           if (ey == edges[1] - 1) {
6239566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
6249566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerRight));
625552f7358SJed Brown           }
626552f7358SJed Brown         } else if (vx == 0) {
6279566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
6289566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
629552f7358SJed Brown           if (ey == edges[1] - 1) {
6309566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
6319566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerLeft));
632552f7358SJed Brown           }
633552f7358SJed Brown         }
634552f7358SJed Brown       }
635552f7358SJed Brown     }
636552f7358SJed Brown     for (vy = 0; vy <= edges[1]; vy++) {
637552f7358SJed Brown       for (ex = 0; ex < edges[0]; ex++) {
638552f7358SJed Brown         PetscInt edge   = vy * edges[0] + ex;
639552f7358SJed Brown         PetscInt vertex = vy * (edges[0] + 1) + ex + numEdges;
640da80777bSKarl Rupp         PetscInt cone[2];
641552f7358SJed Brown 
6429371c9d4SSatish Balay         cone[0] = vertex;
6439371c9d4SSatish Balay         cone[1] = vertex + 1;
6449566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, edge, cone));
645552f7358SJed Brown         if (vy == edges[1]) {
6469566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
6479566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
648552f7358SJed Brown           if (ex == edges[0] - 1) {
6499566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
6509566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerTop));
651552f7358SJed Brown           }
652552f7358SJed Brown         } else if (vy == 0) {
6539566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
6549566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
655552f7358SJed Brown           if (ex == edges[0] - 1) {
6569566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
6579566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerBottom));
658552f7358SJed Brown           }
659552f7358SJed Brown         }
660552f7358SJed Brown       }
661552f7358SJed Brown     }
662552f7358SJed Brown   }
6639566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
6649566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
665552f7358SJed Brown   /* Build coordinates */
6669566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, 2));
6679566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
6689566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
6699566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, numEdges, numEdges + numVertices));
6709566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, 2));
671552f7358SJed Brown   for (v = numEdges; v < numEdges + numVertices; ++v) {
6729566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, 2));
6739566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, 2));
674552f7358SJed Brown   }
6759566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
6769566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
6779566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
6789566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
6799566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
6809566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, 2));
6819566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
6829566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
683552f7358SJed Brown   for (vy = 0; vy <= edges[1]; ++vy) {
684552f7358SJed Brown     for (vx = 0; vx <= edges[0]; ++vx) {
685552f7358SJed Brown       coords[(vy * (edges[0] + 1) + vx) * 2 + 0] = lower[0] + ((upper[0] - lower[0]) / edges[0]) * vx;
686552f7358SJed Brown       coords[(vy * (edges[0] + 1) + vx) * 2 + 1] = lower[1] + ((upper[1] - lower[1]) / edges[1]) * vy;
687552f7358SJed Brown     }
688552f7358SJed Brown   }
6899566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
6909566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
6919566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
6923ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
693552f7358SJed Brown }
694552f7358SJed Brown 
695d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt faces[])
696d71ae5a4SJacob Faibussowitsch {
6979e8abbc3SMichael Lange   PetscInt     vertices[3], numVertices;
6987b59f5a9SMichael Lange   PetscInt     numFaces       = 2 * faces[0] * faces[1] + 2 * faces[1] * faces[2] + 2 * faces[0] * faces[2];
699c2df9bbfSMatthew G. Knepley   PetscInt     markerTop      = 1;
700c2df9bbfSMatthew G. Knepley   PetscInt     markerBottom   = 1;
701c2df9bbfSMatthew G. Knepley   PetscInt     markerFront    = 1;
702c2df9bbfSMatthew G. Knepley   PetscInt     markerBack     = 1;
703c2df9bbfSMatthew G. Knepley   PetscInt     markerRight    = 1;
704c2df9bbfSMatthew G. Knepley   PetscInt     markerLeft     = 1;
705c2df9bbfSMatthew G. Knepley   PetscBool    markerSeparate = PETSC_FALSE;
706552f7358SJed Brown   Vec          coordinates;
707552f7358SJed Brown   PetscSection coordSection;
708552f7358SJed Brown   PetscScalar *coords;
709552f7358SJed Brown   PetscInt     coordSize;
710552f7358SJed Brown   PetscMPIInt  rank;
711552f7358SJed Brown   PetscInt     v, vx, vy, vz;
7127b59f5a9SMichael Lange   PetscInt     voffset, iface = 0, cone[4];
713552f7358SJed Brown 
714552f7358SJed Brown   PetscFunctionBegin;
7151dca8a05SBarry 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");
7169566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
717c2df9bbfSMatthew G. Knepley   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
718c2df9bbfSMatthew G. Knepley   if (markerSeparate) {
719c2df9bbfSMatthew G. Knepley     markerBottom = 1;
720c2df9bbfSMatthew G. Knepley     markerTop    = 2;
721c2df9bbfSMatthew G. Knepley     markerFront  = 3;
722c2df9bbfSMatthew G. Knepley     markerBack   = 4;
723c2df9bbfSMatthew G. Knepley     markerRight  = 5;
724c2df9bbfSMatthew G. Knepley     markerLeft   = 6;
725c2df9bbfSMatthew G. Knepley   }
7269371c9d4SSatish Balay   vertices[0] = faces[0] + 1;
7279371c9d4SSatish Balay   vertices[1] = faces[1] + 1;
7289371c9d4SSatish Balay   vertices[2] = faces[2] + 1;
7299e8abbc3SMichael Lange   numVertices = vertices[0] * vertices[1] * vertices[2];
730dd400576SPatrick Sanan   if (rank == 0) {
731552f7358SJed Brown     PetscInt f;
732552f7358SJed Brown 
7339566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numFaces + numVertices));
73448a46eb9SPierre Jolivet     for (f = 0; f < numFaces; ++f) PetscCall(DMPlexSetConeSize(dm, f, 4));
7359566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
7367b59f5a9SMichael Lange 
7377b59f5a9SMichael Lange     /* Side 0 (Top) */
7387b59f5a9SMichael Lange     for (vy = 0; vy < faces[1]; vy++) {
7397b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
7407b59f5a9SMichael Lange         voffset = numFaces + vertices[0] * vertices[1] * (vertices[2] - 1) + vy * vertices[0] + vx;
7419371c9d4SSatish Balay         cone[0] = voffset;
7429371c9d4SSatish Balay         cone[1] = voffset + 1;
7439371c9d4SSatish Balay         cone[2] = voffset + vertices[0] + 1;
7449371c9d4SSatish Balay         cone[3] = voffset + vertices[0];
7459566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
746c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerTop));
747c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerTop));
748c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerTop));
749c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerTop));
750c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 1, markerTop));
7517b59f5a9SMichael Lange         iface++;
752552f7358SJed Brown       }
753552f7358SJed Brown     }
7547b59f5a9SMichael Lange 
7557b59f5a9SMichael Lange     /* Side 1 (Bottom) */
7567b59f5a9SMichael Lange     for (vy = 0; vy < faces[1]; vy++) {
7577b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
7587b59f5a9SMichael Lange         voffset = numFaces + vy * (faces[0] + 1) + vx;
7599371c9d4SSatish Balay         cone[0] = voffset + 1;
7609371c9d4SSatish Balay         cone[1] = voffset;
7619371c9d4SSatish Balay         cone[2] = voffset + vertices[0];
7629371c9d4SSatish Balay         cone[3] = voffset + vertices[0] + 1;
7639566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
764c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerBottom));
765c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerBottom));
766c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerBottom));
767c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerBottom));
768c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 1, markerBottom));
7697b59f5a9SMichael Lange         iface++;
770552f7358SJed Brown       }
771552f7358SJed Brown     }
7727b59f5a9SMichael Lange 
7737b59f5a9SMichael Lange     /* Side 2 (Front) */
7747b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
7757b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
7767b59f5a9SMichael Lange         voffset = numFaces + vz * vertices[0] * vertices[1] + vx;
7779371c9d4SSatish Balay         cone[0] = voffset;
7789371c9d4SSatish Balay         cone[1] = voffset + 1;
7799371c9d4SSatish Balay         cone[2] = voffset + vertices[0] * vertices[1] + 1;
7809371c9d4SSatish Balay         cone[3] = voffset + vertices[0] * vertices[1];
7819566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
782c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerFront));
783c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerFront));
784c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerFront));
785c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 0, markerFront));
786c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 1, markerFront));
7877b59f5a9SMichael Lange         iface++;
788552f7358SJed Brown       }
7897b59f5a9SMichael Lange     }
7907b59f5a9SMichael Lange 
7917b59f5a9SMichael Lange     /* Side 3 (Back) */
7927b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
7937b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
7947b59f5a9SMichael Lange         voffset = numFaces + vz * vertices[0] * vertices[1] + vertices[0] * (vertices[1] - 1) + vx;
7959371c9d4SSatish Balay         cone[0] = voffset + vertices[0] * vertices[1];
7969371c9d4SSatish Balay         cone[1] = voffset + vertices[0] * vertices[1] + 1;
7979371c9d4SSatish Balay         cone[2] = voffset + 1;
7989371c9d4SSatish Balay         cone[3] = voffset;
7999566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
800c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerBack));
801c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerBack));
802c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerBack));
803c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 0, markerBack));
804c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 1, markerBack));
8057b59f5a9SMichael Lange         iface++;
8067b59f5a9SMichael Lange       }
8077b59f5a9SMichael Lange     }
8087b59f5a9SMichael Lange 
8097b59f5a9SMichael Lange     /* Side 4 (Left) */
8107b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
8117b59f5a9SMichael Lange       for (vy = 0; vy < faces[1]; vy++) {
8127b59f5a9SMichael Lange         voffset = numFaces + vz * vertices[0] * vertices[1] + vy * vertices[0];
8139371c9d4SSatish Balay         cone[0] = voffset;
8149371c9d4SSatish Balay         cone[1] = voffset + vertices[0] * vertices[1];
8159371c9d4SSatish Balay         cone[2] = voffset + vertices[0] * vertices[1] + vertices[0];
8169371c9d4SSatish Balay         cone[3] = voffset + vertices[0];
8179566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
818c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerLeft));
819c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerLeft));
820c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerLeft));
821c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[1] + 0, markerLeft));
822c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + vertices[0], markerLeft));
8237b59f5a9SMichael Lange         iface++;
8247b59f5a9SMichael Lange       }
8257b59f5a9SMichael Lange     }
8267b59f5a9SMichael Lange 
8277b59f5a9SMichael Lange     /* Side 5 (Right) */
8287b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
8297b59f5a9SMichael Lange       for (vy = 0; vy < faces[1]; vy++) {
830aab5bcd8SJed Brown         voffset = numFaces + vz * vertices[0] * vertices[1] + vy * vertices[0] + faces[0];
8319371c9d4SSatish Balay         cone[0] = voffset + vertices[0] * vertices[1];
8329371c9d4SSatish Balay         cone[1] = voffset;
8339371c9d4SSatish Balay         cone[2] = voffset + vertices[0];
8349371c9d4SSatish Balay         cone[3] = voffset + vertices[0] * vertices[1] + vertices[0];
8359566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
836c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerRight));
837c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerRight));
838c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerRight));
839c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 0, markerRight));
840c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + vertices[0], markerRight));
8417b59f5a9SMichael Lange         iface++;
8427b59f5a9SMichael Lange       }
843552f7358SJed Brown     }
844552f7358SJed Brown   }
8459566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
8469566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
847552f7358SJed Brown   /* Build coordinates */
8489566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, 3));
8499566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
8509566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
8519566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, numFaces, numFaces + numVertices));
8529566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, 3));
853552f7358SJed Brown   for (v = numFaces; v < numFaces + numVertices; ++v) {
8549566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, 3));
8559566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, 3));
856552f7358SJed Brown   }
8579566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
8589566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
8599566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
8609566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
8619566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
8629566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, 3));
8639566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
8649566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
865552f7358SJed Brown   for (vz = 0; vz <= faces[2]; ++vz) {
866552f7358SJed Brown     for (vy = 0; vy <= faces[1]; ++vy) {
867552f7358SJed Brown       for (vx = 0; vx <= faces[0]; ++vx) {
868552f7358SJed Brown         coords[((vz * (faces[1] + 1) + vy) * (faces[0] + 1) + vx) * 3 + 0] = lower[0] + ((upper[0] - lower[0]) / faces[0]) * vx;
869552f7358SJed Brown         coords[((vz * (faces[1] + 1) + vy) * (faces[0] + 1) + vx) * 3 + 1] = lower[1] + ((upper[1] - lower[1]) / faces[1]) * vy;
870552f7358SJed Brown         coords[((vz * (faces[1] + 1) + vy) * (faces[0] + 1) + vx) * 3 + 2] = lower[2] + ((upper[2] - lower[2]) / faces[2]) * vz;
871552f7358SJed Brown       }
872552f7358SJed Brown     }
873552f7358SJed Brown   }
8749566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
8759566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
8769566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
8773ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
878552f7358SJed Brown }
879552f7358SJed Brown 
880d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate)
881d71ae5a4SJacob Faibussowitsch {
8829318fe57SMatthew G. Knepley   PetscFunctionBegin;
8839318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
88446139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
8859566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim - 1));
8869566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, dim));
8879318fe57SMatthew G. Knepley   switch (dim) {
888d71ae5a4SJacob Faibussowitsch   case 1:
889d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(dm, lower, upper, faces));
890d71ae5a4SJacob Faibussowitsch     break;
891d71ae5a4SJacob Faibussowitsch   case 2:
892d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(dm, lower, upper, faces));
893d71ae5a4SJacob Faibussowitsch     break;
894d71ae5a4SJacob Faibussowitsch   case 3:
895d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(dm, lower, upper, faces));
896d71ae5a4SJacob Faibussowitsch     break;
897d71ae5a4SJacob Faibussowitsch   default:
898d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Dimension not supported: %" PetscInt_FMT, dim);
8999318fe57SMatthew G. Knepley   }
90046139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
9019566063dSJacob Faibussowitsch   if (interpolate) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
9023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9039318fe57SMatthew G. Knepley }
9049318fe57SMatthew G. Knepley 
9059318fe57SMatthew G. Knepley /*@C
9069318fe57SMatthew G. Knepley   DMPlexCreateBoxSurfaceMesh - Creates a mesh on the surface of the tensor product of unit intervals (box) using tensor cells (hexahedra).
9079318fe57SMatthew G. Knepley 
9089318fe57SMatthew G. Knepley   Collective
9099318fe57SMatthew G. Knepley 
9109318fe57SMatthew G. Knepley   Input Parameters:
911a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
91220f4b53cSBarry Smith . dim         - The spatial dimension of the box, so the resulting mesh is has dimension `dim`-1
91320f4b53cSBarry Smith . faces       - Number of faces per dimension, or `NULL` for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
91420f4b53cSBarry Smith . lower       - The lower left corner, or `NULL` for (0, 0, 0)
91520f4b53cSBarry Smith . upper       - The upper right corner, or `NULL` for (1, 1, 1)
9169318fe57SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
9179318fe57SMatthew G. Knepley 
9189318fe57SMatthew G. Knepley   Output Parameter:
919a1cb98faSBarry Smith . dm - The `DM` object
9209318fe57SMatthew G. Knepley 
9219318fe57SMatthew G. Knepley   Level: beginner
9229318fe57SMatthew G. Knepley 
9231cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMSetFromOptions()`, `DMPlexCreateBoxMesh()`, `DMPlexCreateFromFile()`, `DMSetType()`, `DMCreate()`
9249318fe57SMatthew G. Knepley @*/
925d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateBoxSurfaceMesh(MPI_Comm comm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate, DM *dm)
926d71ae5a4SJacob Faibussowitsch {
9279318fe57SMatthew G. Knepley   PetscInt  fac[3] = {1, 1, 1};
9289318fe57SMatthew G. Knepley   PetscReal low[3] = {0, 0, 0};
9299318fe57SMatthew G. Knepley   PetscReal upp[3] = {1, 1, 1};
9309318fe57SMatthew G. Knepley 
9319318fe57SMatthew G. Knepley   PetscFunctionBegin;
9329566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
9339566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
9349566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(*dm, dim, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, interpolate));
9353ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
9369318fe57SMatthew G. Knepley }
9379318fe57SMatthew G. Knepley 
938d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateLineMesh_Internal(DM dm, PetscInt segments, PetscReal lower, PetscReal upper, DMBoundaryType bd)
939d71ae5a4SJacob Faibussowitsch {
940fdbf62faSLisandro Dalcin   PetscInt     i, fStart, fEnd, numCells = 0, numVerts = 0;
941fdbf62faSLisandro Dalcin   PetscInt     numPoints[2], *coneSize, *cones, *coneOrientations;
942fdbf62faSLisandro Dalcin   PetscScalar *vertexCoords;
943fdbf62faSLisandro Dalcin   PetscReal    L, maxCell;
944fdbf62faSLisandro Dalcin   PetscBool    markerSeparate = PETSC_FALSE;
945fdbf62faSLisandro Dalcin   PetscInt     markerLeft = 1, faceMarkerLeft = 1;
946fdbf62faSLisandro Dalcin   PetscInt     markerRight = 1, faceMarkerRight = 2;
947fdbf62faSLisandro Dalcin   PetscBool    wrap = (bd == DM_BOUNDARY_PERIODIC || bd == DM_BOUNDARY_TWIST) ? PETSC_TRUE : PETSC_FALSE;
948fdbf62faSLisandro Dalcin   PetscMPIInt  rank;
949fdbf62faSLisandro Dalcin 
950fdbf62faSLisandro Dalcin   PetscFunctionBegin;
9514f572ea9SToby Isaac   PetscAssertPointer(dm, 1);
952fdbf62faSLisandro Dalcin 
9539566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, 1));
9549566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
9559566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
956fdbf62faSLisandro Dalcin 
9579566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
958dd400576SPatrick Sanan   if (rank == 0) numCells = segments;
959dd400576SPatrick Sanan   if (rank == 0) numVerts = segments + (wrap ? 0 : 1);
960fdbf62faSLisandro Dalcin 
9619371c9d4SSatish Balay   numPoints[0] = numVerts;
9629371c9d4SSatish Balay   numPoints[1] = numCells;
9639566063dSJacob Faibussowitsch   PetscCall(PetscMalloc4(numCells + numVerts, &coneSize, numCells * 2, &cones, numCells + numVerts, &coneOrientations, numVerts, &vertexCoords));
9649566063dSJacob Faibussowitsch   PetscCall(PetscArrayzero(coneOrientations, numCells + numVerts));
965ad540459SPierre Jolivet   for (i = 0; i < numCells; ++i) coneSize[i] = 2;
966ad540459SPierre Jolivet   for (i = 0; i < numVerts; ++i) coneSize[numCells + i] = 0;
9679371c9d4SSatish Balay   for (i = 0; i < numCells; ++i) {
9689371c9d4SSatish Balay     cones[2 * i]     = numCells + i % numVerts;
9699371c9d4SSatish Balay     cones[2 * i + 1] = numCells + (i + 1) % numVerts;
9709371c9d4SSatish Balay   }
971ad540459SPierre Jolivet   for (i = 0; i < numVerts; ++i) vertexCoords[i] = lower + (upper - lower) * ((PetscReal)i / (PetscReal)numCells);
9729566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
9739566063dSJacob Faibussowitsch   PetscCall(PetscFree4(coneSize, cones, coneOrientations, vertexCoords));
974fdbf62faSLisandro Dalcin 
9759566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
9769371c9d4SSatish Balay   if (markerSeparate) {
9779371c9d4SSatish Balay     markerLeft  = faceMarkerLeft;
9789371c9d4SSatish Balay     markerRight = faceMarkerRight;
9799371c9d4SSatish Balay   }
980dd400576SPatrick Sanan   if (!wrap && rank == 0) {
9819566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
9829566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", fStart, markerLeft));
9839566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", fEnd - 1, markerRight));
9849566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "Face Sets", fStart, faceMarkerLeft));
9859566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "Face Sets", fEnd - 1, faceMarkerRight));
986fdbf62faSLisandro Dalcin   }
987fdbf62faSLisandro Dalcin   if (wrap) {
988fdbf62faSLisandro Dalcin     L       = upper - lower;
989fdbf62faSLisandro Dalcin     maxCell = (PetscReal)1.1 * (L / (PetscReal)PetscMax(1, segments));
9904fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, &maxCell, &lower, &L));
991fdbf62faSLisandro Dalcin   }
9929566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
9933ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
994fdbf62faSLisandro Dalcin }
995fdbf62faSLisandro Dalcin 
9964054ae39SJames Wright // Creates "Face Sets" label based on the standard box labeling conventions
997d7d2d1d2SJames Wright static PetscErrorCode DMPlexSetBoxLabel_Internal(DM dm, const DMBoundaryType periodicity[])
9984054ae39SJames Wright {
9996ff49feeSJames Wright   DM              cdm;
10006ff49feeSJames Wright   PetscSection    csection;
10016ff49feeSJames Wright   Vec             coordinates;
10024054ae39SJames Wright   DMLabel         label;
10036ff49feeSJames Wright   IS              faces_is;
10042b4f33d9SJames Wright   PetscInt        dim, num_face = 0;
10054054ae39SJames Wright   const PetscInt *faces;
10064054ae39SJames Wright   PetscInt        faceMarkerBottom, faceMarkerTop, faceMarkerFront, faceMarkerBack, faceMarkerRight, faceMarkerLeft;
10074054ae39SJames Wright 
10084054ae39SJames Wright   PetscFunctionBeginUser;
10094054ae39SJames Wright   PetscCall(DMGetDimension(dm, &dim));
1010d7c1f440SPierre 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);
10114054ae39SJames Wright   // Get Face Sets label
10124054ae39SJames Wright   PetscCall(DMGetLabel(dm, "Face Sets", &label));
10134054ae39SJames Wright   if (label) {
10144054ae39SJames Wright     PetscCall(DMLabelReset(label));
10154054ae39SJames Wright   } else {
10164054ae39SJames Wright     PetscCall(DMCreateLabel(dm, "Face Sets"));
10174054ae39SJames Wright     PetscCall(DMGetLabel(dm, "Face Sets", &label));
10184054ae39SJames Wright   }
10194054ae39SJames Wright   PetscCall(DMPlexMarkBoundaryFaces(dm, 1, label));
10206ff49feeSJames Wright   PetscCall(DMGetStratumIS(dm, "Face Sets", 1, &faces_is));
10214054ae39SJames Wright 
10224054ae39SJames Wright   switch (dim) {
10234054ae39SJames Wright   case 2:
10244054ae39SJames Wright     faceMarkerTop    = 3;
10254054ae39SJames Wright     faceMarkerBottom = 1;
10264054ae39SJames Wright     faceMarkerRight  = 2;
10274054ae39SJames Wright     faceMarkerLeft   = 4;
10284054ae39SJames Wright     break;
10294054ae39SJames Wright   case 3:
10304054ae39SJames Wright     faceMarkerBottom = 1;
10314054ae39SJames Wright     faceMarkerTop    = 2;
10324054ae39SJames Wright     faceMarkerFront  = 3;
10334054ae39SJames Wright     faceMarkerBack   = 4;
10344054ae39SJames Wright     faceMarkerRight  = 5;
10354054ae39SJames Wright     faceMarkerLeft   = 6;
10364054ae39SJames Wright     break;
10374054ae39SJames Wright   default:
10384054ae39SJames Wright     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Dimension %" PetscInt_FMT " not supported", dim);
10394054ae39SJames Wright   }
10404054ae39SJames Wright 
10412b4f33d9SJames Wright   if (faces_is) PetscCall(ISGetLocalSize(faces_is, &num_face));
10422b4f33d9SJames Wright   if (faces_is) PetscCall(ISGetIndices(faces_is, &faces));
10436ff49feeSJames Wright   PetscCall(DMGetCoordinatesLocal(dm, &coordinates));
10446ff49feeSJames Wright   PetscCall(DMGetCoordinateDM(dm, &cdm));
10456ff49feeSJames Wright   PetscCall(DMGetLocalSection(cdm, &csection));
10464054ae39SJames Wright   for (PetscInt f = 0; f < num_face; ++f) {
10476ff49feeSJames Wright     PetscScalar *coords = NULL;
10486ff49feeSJames Wright     PetscInt     face = faces[f], flip = 1, label_value = -1, coords_size;
10494054ae39SJames Wright 
10504054ae39SJames Wright     { // Determine if orientation of face is flipped
10514054ae39SJames Wright       PetscInt        num_cells_support, num_faces, start = -1;
10524054ae39SJames Wright       const PetscInt *orients, *cell_faces, *cells;
10534054ae39SJames Wright 
10544054ae39SJames Wright       PetscCall(DMPlexGetSupport(dm, face, &cells));
10554054ae39SJames Wright       PetscCall(DMPlexGetSupportSize(dm, face, &num_cells_support));
10564054ae39SJames 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);
10574054ae39SJames Wright       PetscCall(DMPlexGetCone(dm, cells[0], &cell_faces));
10584054ae39SJames Wright       PetscCall(DMPlexGetConeSize(dm, cells[0], &num_faces));
10594054ae39SJames Wright       for (PetscInt i = 0; i < num_faces; i++) {
10604054ae39SJames Wright         if (cell_faces[i] == face) start = i;
10614054ae39SJames Wright       }
10624054ae39SJames Wright       PetscCheck(start >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "Could not find face %" PetscInt_FMT " in cone of its support", face);
10634054ae39SJames Wright       PetscCall(DMPlexGetConeOrientation(dm, cells[0], &orients));
10644054ae39SJames Wright       if (orients[start] < 0) flip = -1;
10654054ae39SJames Wright     }
10664054ae39SJames Wright 
10676ff49feeSJames Wright     // Cannot use DMPlexComputeCellGeometryFVM() for high-order geometry, so must calculate normal vectors manually
10686ff49feeSJames Wright     // Use the vertices (depth 0) of coordinate DM to calculate normal vector
10696ff49feeSJames Wright     PetscCall(DMPlexVecGetClosureAtDepth_Internal(cdm, csection, coordinates, face, 0, &coords_size, &coords));
10704054ae39SJames Wright     switch (dim) {
10714054ae39SJames Wright     case 2: {
10726ff49feeSJames Wright       PetscScalar vec[2];
10736ff49feeSJames Wright 
10746ff49feeSJames Wright       for (PetscInt d = 0; d < dim; ++d) vec[d] = flip * (PetscRealPart(coords[1 * dim + d]) - PetscRealPart(coords[0 * dim + d]));
10756ff49feeSJames Wright       PetscScalar normal[] = {vec[1], -vec[0]};
10766ff49feeSJames Wright       if (PetscAbsScalar(normal[0]) > PetscAbsScalar(normal[1])) {
10776ff49feeSJames Wright         label_value = PetscRealPart(normal[0]) > 0 ? faceMarkerRight : faceMarkerLeft;
10784054ae39SJames Wright       } else {
10796ff49feeSJames Wright         label_value = PetscRealPart(normal[1]) > 0 ? faceMarkerTop : faceMarkerBottom;
10804054ae39SJames Wright       }
10814054ae39SJames Wright     } break;
10824054ae39SJames Wright     case 3: {
10836ff49feeSJames Wright       PetscScalar vec1[3], vec2[3], normal[3];
10846ff49feeSJames Wright 
10856ff49feeSJames Wright       for (PetscInt d = 0; d < dim; ++d) {
10866ff49feeSJames Wright         vec1[d] = PetscRealPart(coords[1 * dim + d]) - PetscRealPart(coords[0 * dim + d]);
10876ff49feeSJames Wright         vec2[d] = PetscRealPart(coords[2 * dim + d]) - PetscRealPart(coords[1 * dim + d]);
10886ff49feeSJames Wright       }
10896ff49feeSJames Wright 
10906ff49feeSJames Wright       // Calculate normal vector via cross-product
10916ff49feeSJames Wright       normal[0] = flip * ((vec1[1] * vec2[2]) - (vec1[2] * vec2[1]));
10926ff49feeSJames Wright       normal[1] = flip * ((vec1[2] * vec2[0]) - (vec1[0] * vec2[2]));
10936ff49feeSJames Wright       normal[2] = flip * ((vec1[0] * vec2[1]) - (vec1[1] * vec2[0]));
10946ff49feeSJames Wright 
10956ff49feeSJames Wright       if (PetscAbsScalar(normal[0]) > PetscAbsScalar(normal[1])) {
10966ff49feeSJames Wright         if (PetscAbsScalar(normal[0]) > PetscAbsScalar(normal[2])) {
10976ff49feeSJames Wright           label_value = PetscRealPart(normal[0]) > 0 ? faceMarkerRight : faceMarkerLeft;
10984054ae39SJames Wright         } else {
10996ff49feeSJames Wright           label_value = PetscRealPart(normal[2]) > 0 ? faceMarkerTop : faceMarkerBottom;
11004054ae39SJames Wright         }
11014054ae39SJames Wright       } else {
11026ff49feeSJames Wright         if (PetscAbsScalar(normal[1]) > PetscAbsScalar(normal[2])) {
11036ff49feeSJames Wright           label_value = PetscRealPart(normal[1]) > 0 ? faceMarkerBack : faceMarkerFront;
11044054ae39SJames Wright         } else {
11056ff49feeSJames Wright           label_value = PetscRealPart(normal[2]) > 0 ? faceMarkerTop : faceMarkerBottom;
11064054ae39SJames Wright         }
11074054ae39SJames Wright       }
11084054ae39SJames Wright     } break;
11094054ae39SJames Wright     }
11104054ae39SJames Wright 
11114054ae39SJames Wright     PetscInt previous_label_value; // always 1 due to DMPlexMarkBoundaryFaces call above
11124054ae39SJames Wright     PetscCall(DMGetLabelValue(dm, "Face Sets", face, &previous_label_value));
11134054ae39SJames Wright     PetscCall(DMClearLabelValue(dm, "Face Sets", face, previous_label_value));
11144054ae39SJames Wright     PetscCall(DMSetLabelValue(dm, "Face Sets", face, label_value));
11156ff49feeSJames Wright     PetscCall(DMPlexVecRestoreClosure(cdm, csection, coordinates, face, &coords_size, &coords));
11164054ae39SJames Wright   }
11172b4f33d9SJames Wright   if (faces_is) PetscCall(ISRestoreIndices(faces_is, &faces));
11186ff49feeSJames Wright   PetscCall(ISDestroy(&faces_is));
1119d7d2d1d2SJames Wright 
1120d7d2d1d2SJames Wright   // Create Isoperiodic SF from newly-created face labels
1121d7d2d1d2SJames Wright   PetscSF     periodicsfs[3];
1122d7d2d1d2SJames Wright   PetscInt    periodic_sf_index  = 0;
1123d7d2d1d2SJames Wright   PetscScalar transform[3][4][4] = {{{0.}}};
1124d7d2d1d2SJames Wright   for (PetscInt d = 0; d < dim; d++) {
1125d7d2d1d2SJames Wright     IS              donor_is, periodic_is;
1126d7d2d1d2SJames Wright     const PetscInt *donor_faces = NULL, *periodic_faces = NULL;
1127d7d2d1d2SJames Wright     PetscInt        num_donor = 0, num_periodic = 0;
1128d7d2d1d2SJames Wright     PetscSF         centroidsf;
1129d7d2d1d2SJames Wright     PetscReal       donor_to_periodic_distance;
1130d7d2d1d2SJames Wright     const PetscInt  face_pairings[2][3][2] = {
1131d7d2d1d2SJames Wright       // 2D face pairings, {donor, periodic}
1132d7d2d1d2SJames Wright       {{4, 2}, {1, 3}},
1133d7d2d1d2SJames Wright       // 3D face pairings
1134d7d2d1d2SJames Wright       {{5, 6}, {3, 4}, {1, 2}}
1135d7d2d1d2SJames Wright     };
1136d7d2d1d2SJames Wright 
1137d7d2d1d2SJames Wright     if (periodicity[d] != DM_BOUNDARY_PERIODIC) continue;
1138d7d2d1d2SJames Wright     {
1139d7d2d1d2SJames Wright       // Compute centroidsf, which is the mapping from donor faces to periodic faces
1140d7d2d1d2SJames Wright       // Matches the centroid of the faces together, ignoring the periodic direction component (which should not match between donor and periodic face)
1141d7d2d1d2SJames Wright       PetscInt     coords_size, centroid_comps = dim - 1;
1142d7d2d1d2SJames Wright       PetscScalar *coords = NULL;
1143d7d2d1d2SJames Wright       PetscReal   *donor_centroids, *periodic_centroids;
1144d7d2d1d2SJames 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
1145d7d2d1d2SJames Wright 
1146d7d2d1d2SJames Wright       PetscCall(DMGetStratumIS(dm, "Face Sets", face_pairings[dim - 2][d][0], &donor_is));
1147d7d2d1d2SJames Wright       PetscCall(DMGetStratumIS(dm, "Face Sets", face_pairings[dim - 2][d][1], &periodic_is));
1148d7d2d1d2SJames Wright       if (donor_is) {
1149d7d2d1d2SJames Wright         PetscCall(ISGetLocalSize(donor_is, &num_donor));
1150d7d2d1d2SJames Wright         PetscCall(ISGetIndices(donor_is, &donor_faces));
1151d7d2d1d2SJames Wright       }
1152d7d2d1d2SJames Wright       if (periodic_is) {
1153d7d2d1d2SJames Wright         PetscCall(ISGetLocalSize(periodic_is, &num_periodic));
1154d7d2d1d2SJames Wright         PetscCall(ISGetIndices(periodic_is, &periodic_faces));
1155d7d2d1d2SJames Wright       }
1156d7d2d1d2SJames Wright       PetscCall(PetscCalloc2(num_donor * centroid_comps, &donor_centroids, num_periodic * centroid_comps, &periodic_centroids));
1157d7d2d1d2SJames Wright       for (PetscInt f = 0; f < num_donor; f++) {
1158d7d2d1d2SJames Wright         PetscInt face = donor_faces[f], num_coords;
1159d7d2d1d2SJames Wright         PetscCall(DMPlexVecGetClosureAtDepth_Internal(cdm, csection, coordinates, face, 0, &coords_size, &coords));
1160d7d2d1d2SJames Wright         num_coords = coords_size / dim;
1161d7d2d1d2SJames Wright         for (PetscInt c = 0; c < num_coords; c++) {
1162d7d2d1d2SJames Wright           PetscInt comp_index = 0;
1163d7d2d1d2SJames Wright           loc_periodic[0]     = PetscRealPart(coords[c * dim + d]);
1164d7d2d1d2SJames Wright           for (PetscInt i = 0; i < dim; i++) {
1165d7d2d1d2SJames Wright             if (i == d) continue; // Periodic direction not used for centroid calculation
1166d7d2d1d2SJames Wright             donor_centroids[f * centroid_comps + comp_index] += PetscRealPart(coords[c * dim + i]) / num_coords;
1167d7d2d1d2SJames Wright             comp_index++;
1168d7d2d1d2SJames Wright           }
1169d7d2d1d2SJames Wright         }
1170d7d2d1d2SJames Wright         PetscCall(DMPlexVecRestoreClosure(cdm, csection, coordinates, face, &coords_size, &coords));
1171d7d2d1d2SJames Wright       }
1172d7d2d1d2SJames Wright 
1173d7d2d1d2SJames Wright       for (PetscInt f = 0; f < num_periodic; f++) {
1174d7d2d1d2SJames Wright         PetscInt face = periodic_faces[f], num_coords;
1175d7d2d1d2SJames Wright         PetscCall(DMPlexVecGetClosureAtDepth_Internal(cdm, csection, coordinates, face, 0, &coords_size, &coords));
1176d7d2d1d2SJames Wright         num_coords = coords_size / dim;
1177d7d2d1d2SJames Wright         for (PetscInt c = 0; c < num_coords; c++) {
1178d7d2d1d2SJames Wright           PetscInt comp_index = 0;
1179d7d2d1d2SJames Wright           loc_periodic[1]     = PetscRealPart(coords[c * dim + d]);
1180d7d2d1d2SJames Wright           for (PetscInt i = 0; i < dim; i++) {
1181d7d2d1d2SJames Wright             if (i == d) continue; // Periodic direction not used for centroid calculation
1182d7d2d1d2SJames Wright             periodic_centroids[f * centroid_comps + comp_index] += PetscRealPart(coords[c * dim + i]) / num_coords;
1183d7d2d1d2SJames Wright             comp_index++;
1184d7d2d1d2SJames Wright           }
1185d7d2d1d2SJames Wright         }
1186d7d2d1d2SJames Wright         PetscCall(DMPlexVecRestoreClosure(cdm, csection, coordinates, face, &coords_size, &coords));
1187d7d2d1d2SJames Wright       }
1188d7d2d1d2SJames Wright       PetscCallMPI(MPIU_Allreduce(loc_periodic, loc_periodic_global, 2, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject)dm)));
1189d7d2d1d2SJames Wright       donor_to_periodic_distance = loc_periodic_global[1] - loc_periodic_global[0];
1190d7d2d1d2SJames Wright 
1191d7d2d1d2SJames Wright       PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &centroidsf));
1192d7d2d1d2SJames Wright       PetscCall(PetscSFSetGraphFromCoordinates(centroidsf, num_donor, num_periodic, centroid_comps, 1e-10, donor_centroids, periodic_centroids));
1193d7d2d1d2SJames Wright       PetscCall(PetscSFViewFromOptions(centroidsf, NULL, "-dm_plex_box_label_centroid_sf_view"));
1194d7d2d1d2SJames Wright       PetscCall(PetscFree2(donor_centroids, periodic_centroids));
1195d7d2d1d2SJames Wright     }
1196d7d2d1d2SJames Wright 
1197d7d2d1d2SJames Wright     { // Create Isoperiodic SF using centroidsSF
1198d7d2d1d2SJames Wright       PetscInt           pStart, pEnd;
1199d7d2d1d2SJames Wright       PetscInt          *leaf_faces;
1200d7d2d1d2SJames Wright       const PetscSFNode *firemote;
1201d7d2d1d2SJames Wright       PetscSFNode       *isoperiodic_leaves;
1202d7d2d1d2SJames Wright 
1203d7d2d1d2SJames Wright       PetscCall(PetscMalloc1(num_periodic, &leaf_faces));
1204d7d2d1d2SJames Wright       PetscCall(PetscSFBcastBegin(centroidsf, MPIU_INT, donor_faces, leaf_faces, MPI_REPLACE));
1205d7d2d1d2SJames Wright       PetscCall(PetscSFBcastEnd(centroidsf, MPIU_INT, donor_faces, leaf_faces, MPI_REPLACE));
1206d7d2d1d2SJames Wright 
1207d7d2d1d2SJames Wright       PetscCall(PetscMalloc1(num_periodic, &isoperiodic_leaves));
1208d7d2d1d2SJames Wright       PetscCall(PetscSFGetGraph(centroidsf, NULL, NULL, NULL, &firemote));
1209d7d2d1d2SJames Wright       for (PetscInt l = 0; l < num_periodic; ++l) {
1210d7d2d1d2SJames Wright         isoperiodic_leaves[l].index = leaf_faces[l];
1211d7d2d1d2SJames Wright         isoperiodic_leaves[l].rank  = firemote[l].rank;
1212d7d2d1d2SJames Wright       }
1213d7d2d1d2SJames Wright 
1214d7d2d1d2SJames Wright       PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
1215d7d2d1d2SJames Wright       PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)dm), &periodicsfs[periodic_sf_index]));
1216d7d2d1d2SJames Wright       PetscCall(PetscSFSetGraph(periodicsfs[periodic_sf_index], pEnd - pStart, num_periodic, (PetscInt *)periodic_faces, PETSC_COPY_VALUES, isoperiodic_leaves, PETSC_OWN_POINTER));
1217d7d2d1d2SJames Wright       PetscCall(PetscSFViewFromOptions(periodicsfs[periodic_sf_index], NULL, "-dm_plex_box_label_periodic_sf_view"));
1218d7d2d1d2SJames Wright       PetscCall(PetscFree(leaf_faces));
1219d7d2d1d2SJames Wright     }
1220d7d2d1d2SJames Wright 
1221d7d2d1d2SJames Wright     transform[periodic_sf_index][0][0] = 1;
1222d7d2d1d2SJames Wright     transform[periodic_sf_index][1][1] = 1;
1223d7d2d1d2SJames Wright     transform[periodic_sf_index][2][2] = 1;
1224d7d2d1d2SJames Wright     transform[periodic_sf_index][3][3] = 1;
1225d7d2d1d2SJames Wright     transform[periodic_sf_index][d][3] = donor_to_periodic_distance;
1226d7d2d1d2SJames Wright 
1227d7d2d1d2SJames Wright     periodic_sf_index++;
1228d7d2d1d2SJames Wright     PetscCall(PetscSFDestroy(&centroidsf));
1229d7d2d1d2SJames Wright     if (donor_is) {
1230d7d2d1d2SJames Wright       PetscCall(ISRestoreIndices(donor_is, &donor_faces));
1231d7d2d1d2SJames Wright       PetscCall(ISDestroy(&donor_is));
1232d7d2d1d2SJames Wright     }
1233d7d2d1d2SJames Wright     if (periodic_is) {
1234d7d2d1d2SJames Wright       PetscCall(ISRestoreIndices(periodic_is, &periodic_faces));
1235d7d2d1d2SJames Wright       PetscCall(ISDestroy(&periodic_is));
1236d7d2d1d2SJames Wright     }
1237d7d2d1d2SJames Wright     PetscCall(DMClearLabelStratum(dm, "Face Sets", face_pairings[dim - 2][d][0]));
1238d7d2d1d2SJames Wright     PetscCall(DMClearLabelStratum(dm, "Face Sets", face_pairings[dim - 2][d][1]));
1239d7d2d1d2SJames Wright   }
1240d7d2d1d2SJames Wright   PetscCall(DMPlexSetIsoperiodicFaceSF(dm, periodic_sf_index, periodicsfs));
1241d7d2d1d2SJames Wright   PetscCall(DMPlexSetIsoperiodicFaceTransform(dm, periodic_sf_index, (const PetscScalar *)transform));
1242d7d2d1d2SJames Wright   for (PetscInt p = 0; p < periodic_sf_index; p++) PetscCall(PetscSFDestroy(&periodicsfs[p]));
1243d7d2d1d2SJames Wright 
1244d7d2d1d2SJames Wright   { // Update coordinate DM with new Face Sets label
1245d7d2d1d2SJames Wright     DM      cdm;
1246d7d2d1d2SJames Wright     DMLabel oldFaceSets, newFaceSets;
1247d7d2d1d2SJames Wright     PetscCall(DMGetCoordinateDM(dm, &cdm));
1248d7d2d1d2SJames Wright     PetscCall(DMGetLabel(cdm, "Face Sets", &oldFaceSets));
1249d7d2d1d2SJames Wright     if (oldFaceSets) PetscCall(DMRemoveLabelBySelf(cdm, &oldFaceSets, PETSC_FALSE));
1250d7d2d1d2SJames Wright     PetscCall(DMLabelDuplicate(label, &newFaceSets));
1251d7d2d1d2SJames Wright     PetscCall(DMAddLabel(cdm, newFaceSets));
1252d7d2d1d2SJames Wright     PetscCall(DMLabelDestroy(&newFaceSets));
1253d7d2d1d2SJames Wright   }
12544054ae39SJames Wright   PetscFunctionReturn(PETSC_SUCCESS);
12554054ae39SJames Wright }
12564054ae39SJames Wright 
1257d698cf03SStefano Zampini static PetscErrorCode DMPlexCreateSquareMesh_Simplex_CrissCross(DM dm, const PetscInt edges[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType bd[])
1258d698cf03SStefano Zampini {
1259d698cf03SStefano Zampini   PetscInt       markerTop = 1, faceMarkerTop = 3;
1260d698cf03SStefano Zampini   PetscInt       markerBottom = 1, faceMarkerBottom = 1;
1261d698cf03SStefano Zampini   PetscInt       markerRight = 1, faceMarkerRight = 2;
1262d698cf03SStefano Zampini   PetscInt       markerLeft = 1, faceMarkerLeft = 4;
1263d698cf03SStefano Zampini   PetscBool      markerSeparate = PETSC_FALSE;
1264d698cf03SStefano Zampini   DMBoundaryType bdX = bd[0], bdY = bd[1];
1265d698cf03SStefano Zampini   PetscMPIInt    rank;
1266d698cf03SStefano Zampini 
1267d698cf03SStefano Zampini   PetscFunctionBegin;
1268d698cf03SStefano Zampini   PetscCheck(bdX == DM_BOUNDARY_NONE || bdX == DM_BOUNDARY_PERIODIC, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Not implemented for boundary type %s", DMBoundaryTypes[bdX]);
1269d698cf03SStefano Zampini   PetscCheck(bdY == DM_BOUNDARY_NONE || bdY == DM_BOUNDARY_PERIODIC, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Not implemented for boundary type %s", DMBoundaryTypes[bdY]);
1270d698cf03SStefano Zampini   PetscCall(DMSetDimension(dm, 2));
1271d698cf03SStefano Zampini   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
1272d698cf03SStefano Zampini   PetscCall(DMCreateLabel(dm, "marker"));
1273d698cf03SStefano Zampini   PetscCall(DMCreateLabel(dm, "Face Sets"));
1274d698cf03SStefano Zampini   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
1275d698cf03SStefano Zampini   if (markerSeparate) {
1276d698cf03SStefano Zampini     markerBottom = faceMarkerBottom;
1277d698cf03SStefano Zampini     markerTop    = faceMarkerTop;
1278d698cf03SStefano Zampini     markerRight  = faceMarkerRight;
1279d698cf03SStefano Zampini     markerLeft   = faceMarkerLeft;
1280d698cf03SStefano Zampini   }
1281d698cf03SStefano Zampini   {
1282d698cf03SStefano Zampini     const PetscInt numXEdges    = rank == 0 ? edges[0] : 0;
1283d698cf03SStefano Zampini     const PetscInt numYEdges    = rank == 0 ? edges[1] : 0;
1284d698cf03SStefano Zampini     const PetscInt numZEdges    = rank == 0 ? 4 * edges[0] * edges[1] : 0; /* Z-edges are the 4 internal edges per cell */
1285d698cf03SStefano Zampini     const PetscInt numXVertices = rank == 0 ? (bdX == DM_BOUNDARY_PERIODIC ? edges[0] : edges[0] + 1) : 0;
1286d698cf03SStefano Zampini     const PetscInt numYVertices = rank == 0 ? (bdY == DM_BOUNDARY_PERIODIC ? edges[1] : edges[1] + 1) : 0;
1287d698cf03SStefano Zampini     const PetscInt numZVertices = rank == 0 ? edges[0] * edges[1] : 0;
1288d698cf03SStefano Zampini     const PetscInt numCells     = 4 * numXEdges * numYEdges;
1289d698cf03SStefano Zampini     const PetscInt numTotXEdges = numXEdges * numYVertices;
1290d698cf03SStefano Zampini     const PetscInt numTotYEdges = numYEdges * numXVertices;
1291d698cf03SStefano Zampini     const PetscInt numVertices  = numXVertices * numYVertices + numZVertices;
1292d698cf03SStefano Zampini     const PetscInt numEdges     = numTotXEdges + numTotYEdges + numZEdges;
1293d698cf03SStefano Zampini     const PetscInt firstVertex  = numCells;
1294d698cf03SStefano Zampini     const PetscInt firstXEdge   = numCells + numVertices;
1295d698cf03SStefano Zampini     const PetscInt firstYEdge   = firstXEdge + numTotXEdges;
1296d698cf03SStefano Zampini     const PetscInt firstZEdge   = firstYEdge + numTotYEdges;
1297d698cf03SStefano Zampini     Vec            coordinates;
1298d698cf03SStefano Zampini     PetscSection   coordSection;
1299d698cf03SStefano Zampini     PetscScalar   *coords;
1300d698cf03SStefano Zampini     PetscInt       coordSize;
1301d698cf03SStefano Zampini     PetscInt       v, vx, vy;
1302d698cf03SStefano Zampini     PetscInt       c, e, ex, ey;
1303d698cf03SStefano Zampini 
1304d698cf03SStefano Zampini     PetscCall(DMPlexSetChart(dm, 0, numCells + numEdges + numVertices));
1305d698cf03SStefano Zampini     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 3));
1306d698cf03SStefano Zampini     for (e = firstXEdge; e < firstXEdge + numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2));
1307d698cf03SStefano Zampini     PetscCall(DMSetUp(dm));
1308d698cf03SStefano Zampini 
1309d698cf03SStefano Zampini     /* Build cells and Z-edges */
1310d698cf03SStefano Zampini     for (ey = 0; ey < numYEdges; ++ey) {
1311d698cf03SStefano Zampini       for (ex = 0; ex < numXEdges; ++ex) {
1312d698cf03SStefano Zampini         const PetscInt exp1 = (ex + 1) % numXVertices;
1313d698cf03SStefano Zampini         const PetscInt eyp1 = (ey + 1) % numYVertices;
1314d698cf03SStefano Zampini         const PetscInt ez   = firstZEdge + 4 * (ey * numXEdges + ex);
1315d698cf03SStefano Zampini         const PetscInt vc   = firstVertex + numXVertices * numYVertices + ey * numXEdges + ex;
1316d698cf03SStefano Zampini         const PetscInt v0   = firstVertex + ey * numXVertices + ex;
1317d698cf03SStefano Zampini         const PetscInt v1   = firstVertex + ey * numXVertices + exp1;
1318d698cf03SStefano Zampini         const PetscInt v2   = firstVertex + eyp1 * numXVertices + exp1;
1319d698cf03SStefano Zampini         const PetscInt v3   = firstVertex + eyp1 * numXVertices + ex;
1320d698cf03SStefano Zampini         const PetscInt e0   = firstXEdge + ey * numXEdges + ex;
1321d698cf03SStefano Zampini         const PetscInt e1   = firstYEdge + exp1 * numYEdges + ey;
1322d698cf03SStefano Zampini         const PetscInt e2   = firstXEdge + eyp1 * numXEdges + ex;
1323d698cf03SStefano Zampini         const PetscInt e3   = firstYEdge + ex * numYEdges + ey;
1324d698cf03SStefano Zampini 
1325d698cf03SStefano Zampini         const PetscInt cones[] = {ez, e0, ez + 1, ez + 1, e1, ez + 2, ez + 2, e2, ez + 3, ez + 3, e3, ez};
1326d698cf03SStefano Zampini         const PetscInt ornts[] = {-1, 0, 0, -1, 0, 0, -1, -1, 0, -1, -1, 0};
1327d698cf03SStefano Zampini         const PetscInt verts[] = {v0, vc, v1, vc, v2, vc, v3, vc};
1328d698cf03SStefano Zampini 
1329d698cf03SStefano Zampini         for (c = 0; c < 4; c++) {
1330d698cf03SStefano Zampini           PetscInt cell = 4 * (ey * numXEdges + ex) + c;
1331d698cf03SStefano Zampini           PetscInt edge = ez + c;
1332d698cf03SStefano Zampini 
1333d698cf03SStefano Zampini           PetscCall(DMPlexSetCone(dm, cell, cones + 3 * c));
1334d698cf03SStefano Zampini           PetscCall(DMPlexSetConeOrientation(dm, cell, ornts + 3 * c));
1335d698cf03SStefano Zampini           PetscCall(DMPlexSetCone(dm, edge, verts + 2 * c));
1336d698cf03SStefano Zampini         }
1337d698cf03SStefano Zampini       }
1338d698cf03SStefano Zampini     }
1339d698cf03SStefano Zampini 
1340d698cf03SStefano Zampini     /* Build Y edges*/
1341d698cf03SStefano Zampini     for (vx = 0; vx < numXVertices; vx++) {
1342d698cf03SStefano Zampini       for (ey = 0; ey < numYEdges; ey++) {
1343d698cf03SStefano Zampini         const PetscInt edge   = firstYEdge + vx * numYEdges + ey;
1344d698cf03SStefano Zampini         const PetscInt v0     = firstVertex + ey * numXVertices + vx;
1345d698cf03SStefano Zampini         const PetscInt v1     = firstVertex + ((ey + 1) % numYVertices) * numXVertices + vx;
1346d698cf03SStefano Zampini         const PetscInt cone[] = {v0, v1};
1347d698cf03SStefano Zampini 
1348d698cf03SStefano Zampini         PetscCall(DMPlexSetCone(dm, edge, cone));
1349d698cf03SStefano Zampini         if ((bdX != DM_BOUNDARY_PERIODIC) && (bdX != DM_BOUNDARY_TWIST)) {
1350d698cf03SStefano Zampini           if (vx == numXVertices - 1) {
1351d698cf03SStefano Zampini             PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerRight));
1352d698cf03SStefano Zampini             PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
1353d698cf03SStefano Zampini             PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1354d698cf03SStefano Zampini             if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1355d698cf03SStefano Zampini           } else if (vx == 0) {
1356d698cf03SStefano Zampini             PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerLeft));
1357d698cf03SStefano Zampini             PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
1358d698cf03SStefano Zampini             PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1359d698cf03SStefano Zampini             if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
1360d698cf03SStefano Zampini           }
1361d698cf03SStefano Zampini         }
1362d698cf03SStefano Zampini       }
1363d698cf03SStefano Zampini     }
1364d698cf03SStefano Zampini 
1365d698cf03SStefano Zampini     /* Build X edges*/
1366d698cf03SStefano Zampini     for (vy = 0; vy < numYVertices; vy++) {
1367d698cf03SStefano Zampini       for (ex = 0; ex < numXEdges; ex++) {
1368d698cf03SStefano Zampini         const PetscInt edge   = firstXEdge + vy * numXEdges + ex;
1369d698cf03SStefano Zampini         const PetscInt v0     = firstVertex + vy * numXVertices + ex;
1370d698cf03SStefano Zampini         const PetscInt v1     = firstVertex + vy * numXVertices + (ex + 1) % numXVertices;
1371d698cf03SStefano Zampini         const PetscInt cone[] = {v0, v1};
1372d698cf03SStefano Zampini 
1373d698cf03SStefano Zampini         PetscCall(DMPlexSetCone(dm, edge, cone));
1374d698cf03SStefano Zampini         if ((bdY != DM_BOUNDARY_PERIODIC) && (bdY != DM_BOUNDARY_TWIST)) {
1375d698cf03SStefano Zampini           if (vy == numYVertices - 1) {
1376d698cf03SStefano Zampini             PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerTop));
1377d698cf03SStefano Zampini             PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
1378d698cf03SStefano Zampini             PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1379d698cf03SStefano Zampini             if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1380d698cf03SStefano Zampini           } else if (vy == 0) {
1381d698cf03SStefano Zampini             PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerBottom));
1382d698cf03SStefano Zampini             PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
1383d698cf03SStefano Zampini             PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1384d698cf03SStefano Zampini             if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
1385d698cf03SStefano Zampini           }
1386d698cf03SStefano Zampini         }
1387d698cf03SStefano Zampini       }
1388d698cf03SStefano Zampini     }
1389d698cf03SStefano Zampini 
1390d698cf03SStefano Zampini     /* Compute support, stratify, and celltype label */
1391d698cf03SStefano Zampini     PetscCall(DMPlexSymmetrize(dm));
1392d698cf03SStefano Zampini     PetscCall(DMPlexStratify(dm));
1393d698cf03SStefano Zampini     PetscCall(DMPlexComputeCellTypes(dm));
1394d698cf03SStefano Zampini 
1395d698cf03SStefano Zampini     /* Build coordinates */
1396d698cf03SStefano Zampini     PetscCall(DMGetCoordinateSection(dm, &coordSection));
1397d698cf03SStefano Zampini     PetscCall(PetscSectionSetNumFields(coordSection, 1));
1398d698cf03SStefano Zampini     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, 2));
1399d698cf03SStefano Zampini     PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numVertices));
1400d698cf03SStefano Zampini     for (v = firstVertex; v < firstVertex + numVertices; ++v) {
1401d698cf03SStefano Zampini       PetscCall(PetscSectionSetDof(coordSection, v, 2));
1402d698cf03SStefano Zampini       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, 2));
1403d698cf03SStefano Zampini     }
1404d698cf03SStefano Zampini     PetscCall(PetscSectionSetUp(coordSection));
1405d698cf03SStefano Zampini     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
1406d698cf03SStefano Zampini     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
1407d698cf03SStefano Zampini     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
1408d698cf03SStefano Zampini     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
1409d698cf03SStefano Zampini     PetscCall(VecSetBlockSize(coordinates, 2));
1410d698cf03SStefano Zampini     PetscCall(VecSetType(coordinates, VECSTANDARD));
1411d698cf03SStefano Zampini     PetscCall(VecGetArray(coordinates, &coords));
1412d698cf03SStefano Zampini     for (vy = 0; vy < numYVertices; ++vy) {
1413d698cf03SStefano Zampini       for (vx = 0; vx < numXVertices; ++vx) {
1414d698cf03SStefano Zampini         coords[2 * (vy * numXVertices + vx) + 0] = lower[0] + ((upper[0] - lower[0]) / numXEdges) * vx;
1415d698cf03SStefano Zampini         coords[2 * (vy * numXVertices + vx) + 1] = lower[1] + ((upper[1] - lower[1]) / numYEdges) * vy;
1416d698cf03SStefano Zampini       }
1417d698cf03SStefano Zampini     }
1418d698cf03SStefano Zampini     for (ey = 0; ey < numYEdges; ++ey) {
1419d698cf03SStefano Zampini       for (ex = 0; ex < numXEdges; ++ex) {
1420d698cf03SStefano Zampini         const PetscInt c = ey * numXEdges + ex + numYVertices * numXVertices;
1421d698cf03SStefano Zampini 
1422d698cf03SStefano Zampini         coords[2 * c + 0] = lower[0] + ((upper[0] - lower[0]) / numXEdges) * (ex + 0.5);
1423d698cf03SStefano Zampini         coords[2 * c + 1] = lower[1] + ((upper[1] - lower[1]) / numYEdges) * (ey + 0.5);
1424d698cf03SStefano Zampini       }
1425d698cf03SStefano Zampini     }
1426d698cf03SStefano Zampini     PetscCall(VecRestoreArray(coordinates, &coords));
1427d698cf03SStefano Zampini     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
1428d698cf03SStefano Zampini     PetscCall(VecDestroy(&coordinates));
1429d698cf03SStefano Zampini 
1430d698cf03SStefano Zampini     /* handle periodic BC */
1431d698cf03SStefano Zampini     if (bdX == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_PERIODIC) {
1432d698cf03SStefano Zampini       PetscReal L[2]       = {-1., -1.};
1433d698cf03SStefano Zampini       PetscReal maxCell[2] = {-1., -1.};
1434d698cf03SStefano Zampini 
1435d698cf03SStefano Zampini       for (PetscInt d = 0; d < 2; ++d) {
1436d698cf03SStefano Zampini         if (bd[d] != DM_BOUNDARY_NONE) {
1437d698cf03SStefano Zampini           L[d]       = upper[d] - lower[d];
1438d698cf03SStefano Zampini           maxCell[d] = 1.1 * (L[d] / PetscMax(1, edges[d]));
1439d698cf03SStefano Zampini         }
1440d698cf03SStefano Zampini       }
1441d698cf03SStefano Zampini       PetscCall(DMSetPeriodicity(dm, maxCell, lower, L));
1442d698cf03SStefano Zampini     }
1443d698cf03SStefano Zampini   }
1444d698cf03SStefano Zampini   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
1445d698cf03SStefano Zampini   PetscFunctionReturn(PETSC_SUCCESS);
1446d698cf03SStefano Zampini }
1447d698cf03SStefano Zampini 
1448d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxMesh_Simplex_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate)
1449d71ae5a4SJacob Faibussowitsch {
14509318fe57SMatthew G. Knepley   DM        boundary, vol;
1451c22d3578SMatthew G. Knepley   DMLabel   bdlabel;
1452d698cf03SStefano Zampini   PetscBool crisscross = PETSC_FALSE;
1453d6218766SMatthew G. Knepley 
1454d6218766SMatthew G. Knepley   PetscFunctionBegin;
14554f572ea9SToby Isaac   PetscAssertPointer(dm, 1);
1456d698cf03SStefano Zampini   if (dim == 2) PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_box_crisscross", &crisscross, NULL));
1457d698cf03SStefano Zampini   if (crisscross) {
1458d698cf03SStefano Zampini     PetscCall(DMPlexCreateSquareMesh_Simplex_CrissCross(dm, faces, lower, upper, periodicity));
1459d698cf03SStefano Zampini   } else {
1460c22d3578SMatthew 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");
14619566063dSJacob Faibussowitsch     PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &boundary));
14629566063dSJacob Faibussowitsch     PetscCall(DMSetType(boundary, DMPLEX));
1463d698cf03SStefano Zampini     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)boundary, ((PetscObject)dm)->prefix));
14649566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(boundary, dim, faces, lower, upper, PETSC_FALSE));
14659566063dSJacob Faibussowitsch     PetscCall(DMPlexGenerate(boundary, NULL, interpolate, &vol));
1466c22d3578SMatthew G. Knepley     PetscCall(DMGetLabel(vol, "marker", &bdlabel));
1467c22d3578SMatthew G. Knepley     if (bdlabel) PetscCall(DMPlexLabelComplete(vol, bdlabel));
14685de52c6dSVaclav Hapla     PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_FALSE, vol));
146969d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &vol));
1470d698cf03SStefano Zampini     PetscCall(DMDestroy(&boundary));
1471d698cf03SStefano Zampini   }
14724054ae39SJames Wright   if (interpolate) {
14734054ae39SJames Wright     PetscCall(DMPlexInterpolateInPlace_Internal(dm));
1474d7d2d1d2SJames Wright     PetscCall(DMPlexSetBoxLabel_Internal(dm, periodicity));
14754054ae39SJames Wright   }
14763ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1477d6218766SMatthew G. Knepley }
1478d6218766SMatthew G. Knepley 
1479d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCubeMesh_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], DMBoundaryType bdX, DMBoundaryType bdY, DMBoundaryType bdZ)
1480d71ae5a4SJacob Faibussowitsch {
1481ed0e4b50SMatthew G. Knepley   DMLabel     cutLabel  = NULL;
1482f4eb4c5dSMatthew G. Knepley   PetscInt    markerTop = 1, faceMarkerTop = 1;
1483f4eb4c5dSMatthew G. Knepley   PetscInt    markerBottom = 1, faceMarkerBottom = 1;
1484f4eb4c5dSMatthew G. Knepley   PetscInt    markerFront = 1, faceMarkerFront = 1;
1485f4eb4c5dSMatthew G. Knepley   PetscInt    markerBack = 1, faceMarkerBack = 1;
1486f4eb4c5dSMatthew G. Knepley   PetscInt    markerRight = 1, faceMarkerRight = 1;
1487f4eb4c5dSMatthew G. Knepley   PetscInt    markerLeft = 1, faceMarkerLeft = 1;
14883dfda0b1SToby Isaac   PetscInt    dim;
1489d8211ee3SMatthew G. Knepley   PetscBool   markerSeparate = PETSC_FALSE, cutMarker = PETSC_FALSE;
14903dfda0b1SToby Isaac   PetscMPIInt rank;
14913dfda0b1SToby Isaac 
14923dfda0b1SToby Isaac   PetscFunctionBegin;
14939566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
14949566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
14959566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
14969566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
14979566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL));
14989371c9d4SSatish 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) {
14999371c9d4SSatish Balay     if (cutMarker) {
15009371c9d4SSatish Balay       PetscCall(DMCreateLabel(dm, "periodic_cut"));
15019371c9d4SSatish Balay       PetscCall(DMGetLabel(dm, "periodic_cut", &cutLabel));
15029371c9d4SSatish Balay     }
1503d8211ee3SMatthew G. Knepley   }
15043dfda0b1SToby Isaac   switch (dim) {
15053dfda0b1SToby Isaac   case 2:
1506f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 3;
1507f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
1508f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 2;
1509f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 4;
15103dfda0b1SToby Isaac     break;
15113dfda0b1SToby Isaac   case 3:
1512f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
1513f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 2;
1514f4eb4c5dSMatthew G. Knepley     faceMarkerFront  = 3;
1515f4eb4c5dSMatthew G. Knepley     faceMarkerBack   = 4;
1516f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 5;
1517f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 6;
15183dfda0b1SToby Isaac     break;
1519d71ae5a4SJacob Faibussowitsch   default:
1520d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Dimension %" PetscInt_FMT " not supported", dim);
15213dfda0b1SToby Isaac   }
15229566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
1523f4eb4c5dSMatthew G. Knepley   if (markerSeparate) {
1524f4eb4c5dSMatthew G. Knepley     markerBottom = faceMarkerBottom;
1525f4eb4c5dSMatthew G. Knepley     markerTop    = faceMarkerTop;
1526f4eb4c5dSMatthew G. Knepley     markerFront  = faceMarkerFront;
1527f4eb4c5dSMatthew G. Knepley     markerBack   = faceMarkerBack;
1528f4eb4c5dSMatthew G. Knepley     markerRight  = faceMarkerRight;
1529f4eb4c5dSMatthew G. Knepley     markerLeft   = faceMarkerLeft;
15303dfda0b1SToby Isaac   }
15313dfda0b1SToby Isaac   {
1532dd400576SPatrick Sanan     const PetscInt numXEdges    = rank == 0 ? edges[0] : 0;
1533dd400576SPatrick Sanan     const PetscInt numYEdges    = rank == 0 ? edges[1] : 0;
1534dd400576SPatrick Sanan     const PetscInt numZEdges    = rank == 0 ? edges[2] : 0;
1535dd400576SPatrick Sanan     const PetscInt numXVertices = rank == 0 ? (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ? edges[0] : edges[0] + 1) : 0;
1536dd400576SPatrick Sanan     const PetscInt numYVertices = rank == 0 ? (bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ? edges[1] : edges[1] + 1) : 0;
1537dd400576SPatrick Sanan     const PetscInt numZVertices = rank == 0 ? (bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST ? edges[2] : edges[2] + 1) : 0;
15383dfda0b1SToby Isaac     const PetscInt numCells     = numXEdges * numYEdges * numZEdges;
15393dfda0b1SToby Isaac     const PetscInt numXFaces    = numYEdges * numZEdges;
15403dfda0b1SToby Isaac     const PetscInt numYFaces    = numXEdges * numZEdges;
15413dfda0b1SToby Isaac     const PetscInt numZFaces    = numXEdges * numYEdges;
15423dfda0b1SToby Isaac     const PetscInt numTotXFaces = numXVertices * numXFaces;
15433dfda0b1SToby Isaac     const PetscInt numTotYFaces = numYVertices * numYFaces;
15443dfda0b1SToby Isaac     const PetscInt numTotZFaces = numZVertices * numZFaces;
15453dfda0b1SToby Isaac     const PetscInt numFaces     = numTotXFaces + numTotYFaces + numTotZFaces;
15463dfda0b1SToby Isaac     const PetscInt numTotXEdges = numXEdges * numYVertices * numZVertices;
15473dfda0b1SToby Isaac     const PetscInt numTotYEdges = numYEdges * numXVertices * numZVertices;
15483dfda0b1SToby Isaac     const PetscInt numTotZEdges = numZEdges * numXVertices * numYVertices;
15493dfda0b1SToby Isaac     const PetscInt numVertices  = numXVertices * numYVertices * numZVertices;
15503dfda0b1SToby Isaac     const PetscInt numEdges     = numTotXEdges + numTotYEdges + numTotZEdges;
15513dfda0b1SToby Isaac     const PetscInt firstVertex  = (dim == 2) ? numFaces : numCells;
15523dfda0b1SToby Isaac     const PetscInt firstXFace   = (dim == 2) ? 0 : numCells + numVertices;
15533dfda0b1SToby Isaac     const PetscInt firstYFace   = firstXFace + numTotXFaces;
15543dfda0b1SToby Isaac     const PetscInt firstZFace   = firstYFace + numTotYFaces;
15553dfda0b1SToby Isaac     const PetscInt firstXEdge   = numCells + numFaces + numVertices;
15563dfda0b1SToby Isaac     const PetscInt firstYEdge   = firstXEdge + numTotXEdges;
15573dfda0b1SToby Isaac     const PetscInt firstZEdge   = firstYEdge + numTotYEdges;
15583dfda0b1SToby Isaac     Vec            coordinates;
15593dfda0b1SToby Isaac     PetscSection   coordSection;
15603dfda0b1SToby Isaac     PetscScalar   *coords;
15613dfda0b1SToby Isaac     PetscInt       coordSize;
15623dfda0b1SToby Isaac     PetscInt       v, vx, vy, vz;
15633dfda0b1SToby Isaac     PetscInt       c, f, fx, fy, fz, e, ex, ey, ez;
15643dfda0b1SToby Isaac 
15659566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells + numFaces + numEdges + numVertices));
156648a46eb9SPierre Jolivet     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 6));
156748a46eb9SPierre Jolivet     for (f = firstXFace; f < firstXFace + numFaces; ++f) PetscCall(DMPlexSetConeSize(dm, f, 4));
156848a46eb9SPierre Jolivet     for (e = firstXEdge; e < firstXEdge + numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2));
15699566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
15703dfda0b1SToby Isaac     /* Build cells */
15713dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
15723dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
15733dfda0b1SToby Isaac         for (fx = 0; fx < numXEdges; ++fx) {
15743dfda0b1SToby Isaac           PetscInt cell  = (fz * numYEdges + fy) * numXEdges + fx;
15753dfda0b1SToby Isaac           PetscInt faceB = firstZFace + (fy * numXEdges + fx) * numZVertices + fz;
15763dfda0b1SToby Isaac           PetscInt faceT = firstZFace + (fy * numXEdges + fx) * numZVertices + ((fz + 1) % numZVertices);
15773dfda0b1SToby Isaac           PetscInt faceF = firstYFace + (fz * numXEdges + fx) * numYVertices + fy;
15783dfda0b1SToby Isaac           PetscInt faceK = firstYFace + (fz * numXEdges + fx) * numYVertices + ((fy + 1) % numYVertices);
15793dfda0b1SToby Isaac           PetscInt faceL = firstXFace + (fz * numYEdges + fy) * numXVertices + fx;
15803dfda0b1SToby Isaac           PetscInt faceR = firstXFace + (fz * numYEdges + fy) * numXVertices + ((fx + 1) % numXVertices);
15813dfda0b1SToby Isaac           /* B,  T,  F,  K,  R,  L */
1582b5a892a1SMatthew G. Knepley           PetscInt ornt[6] = {-2, 0, 0, -3, 0, -2}; /* ??? */
158342206facSLisandro Dalcin           PetscInt cone[6];
15843dfda0b1SToby Isaac 
15853dfda0b1SToby Isaac           /* no boundary twisting in 3D */
15869371c9d4SSatish Balay           cone[0] = faceB;
15879371c9d4SSatish Balay           cone[1] = faceT;
15889371c9d4SSatish Balay           cone[2] = faceF;
15899371c9d4SSatish Balay           cone[3] = faceK;
15909371c9d4SSatish Balay           cone[4] = faceR;
15919371c9d4SSatish Balay           cone[5] = faceL;
15929566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, cell, cone));
15939566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, cell, ornt));
15949566063dSJacob Faibussowitsch           if (bdX != DM_BOUNDARY_NONE && fx == numXEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
15959566063dSJacob Faibussowitsch           if (bdY != DM_BOUNDARY_NONE && fy == numYEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
15969566063dSJacob Faibussowitsch           if (bdZ != DM_BOUNDARY_NONE && fz == numZEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
15973dfda0b1SToby Isaac         }
15983dfda0b1SToby Isaac       }
15993dfda0b1SToby Isaac     }
16003dfda0b1SToby Isaac     /* Build x faces */
16013dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
16023dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
16033dfda0b1SToby Isaac         for (fx = 0; fx < numXVertices; ++fx) {
16043dfda0b1SToby Isaac           PetscInt face    = firstXFace + (fz * numYEdges + fy) * numXVertices + fx;
16053dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy * numXVertices + fx) * numZEdges + fz;
16063dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (((fy + 1) % numYVertices) * numXVertices + fx) * numZEdges + fz;
16073dfda0b1SToby Isaac           PetscInt edgeB   = firstYEdge + (fz * numXVertices + fx) * numYEdges + fy;
16083dfda0b1SToby Isaac           PetscInt edgeT   = firstYEdge + (((fz + 1) % numZVertices) * numXVertices + fx) * numYEdges + fy;
1609b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
16103dfda0b1SToby Isaac           PetscInt cone[4];
16113dfda0b1SToby Isaac 
16123dfda0b1SToby Isaac           if (dim == 3) {
16133dfda0b1SToby Isaac             /* markers */
16143dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
16153dfda0b1SToby Isaac               if (fx == numXVertices - 1) {
16169566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerRight));
16179566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerRight));
16189371c9d4SSatish Balay               } else if (fx == 0) {
16199566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerLeft));
16209566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerLeft));
16213dfda0b1SToby Isaac               }
16223dfda0b1SToby Isaac             }
16233dfda0b1SToby Isaac           }
16249371c9d4SSatish Balay           cone[0] = edgeB;
16259371c9d4SSatish Balay           cone[1] = edgeR;
16269371c9d4SSatish Balay           cone[2] = edgeT;
16279371c9d4SSatish Balay           cone[3] = edgeL;
16289566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
16299566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
16303dfda0b1SToby Isaac         }
16313dfda0b1SToby Isaac       }
16323dfda0b1SToby Isaac     }
16333dfda0b1SToby Isaac     /* Build y faces */
16343dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
163542206facSLisandro Dalcin       for (fx = 0; fx < numXEdges; ++fx) {
16363dfda0b1SToby Isaac         for (fy = 0; fy < numYVertices; ++fy) {
16373dfda0b1SToby Isaac           PetscInt face    = firstYFace + (fz * numXEdges + fx) * numYVertices + fy;
16383dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy * numXVertices + fx) * numZEdges + fz;
16393dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (fy * numXVertices + ((fx + 1) % numXVertices)) * numZEdges + fz;
16403dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz * numYVertices + fy) * numXEdges + fx;
16413dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (((fz + 1) % numZVertices) * numYVertices + fy) * numXEdges + fx;
1642b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
16433dfda0b1SToby Isaac           PetscInt cone[4];
16443dfda0b1SToby Isaac 
16453dfda0b1SToby Isaac           if (dim == 3) {
16463dfda0b1SToby Isaac             /* markers */
16473dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
16483dfda0b1SToby Isaac               if (fy == numYVertices - 1) {
16499566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerBack));
16509566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerBack));
16519371c9d4SSatish Balay               } else if (fy == 0) {
16529566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerFront));
16539566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerFront));
16543dfda0b1SToby Isaac               }
16553dfda0b1SToby Isaac             }
16563dfda0b1SToby Isaac           }
16579371c9d4SSatish Balay           cone[0] = edgeB;
16589371c9d4SSatish Balay           cone[1] = edgeR;
16599371c9d4SSatish Balay           cone[2] = edgeT;
16609371c9d4SSatish Balay           cone[3] = edgeL;
16619566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
16629566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
16633dfda0b1SToby Isaac         }
16643dfda0b1SToby Isaac       }
16653dfda0b1SToby Isaac     }
16663dfda0b1SToby Isaac     /* Build z faces */
16673dfda0b1SToby Isaac     for (fy = 0; fy < numYEdges; ++fy) {
16683dfda0b1SToby Isaac       for (fx = 0; fx < numXEdges; ++fx) {
16693dfda0b1SToby Isaac         for (fz = 0; fz < numZVertices; fz++) {
16703dfda0b1SToby Isaac           PetscInt face    = firstZFace + (fy * numXEdges + fx) * numZVertices + fz;
16713dfda0b1SToby Isaac           PetscInt edgeL   = firstYEdge + (fz * numXVertices + fx) * numYEdges + fy;
16723dfda0b1SToby Isaac           PetscInt edgeR   = firstYEdge + (fz * numXVertices + ((fx + 1) % numXVertices)) * numYEdges + fy;
16733dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz * numYVertices + fy) * numXEdges + fx;
16743dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (fz * numYVertices + ((fy + 1) % numYVertices)) * numXEdges + fx;
1675b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
16763dfda0b1SToby Isaac           PetscInt cone[4];
16773dfda0b1SToby Isaac 
16783dfda0b1SToby Isaac           if (dim == 2) {
16799371c9d4SSatish Balay             if (bdX == DM_BOUNDARY_TWIST && fx == numXEdges - 1) {
16809371c9d4SSatish Balay               edgeR += numYEdges - 1 - 2 * fy;
16819371c9d4SSatish Balay               ornt[1] = -1;
16829371c9d4SSatish Balay             }
16839371c9d4SSatish Balay             if (bdY == DM_BOUNDARY_TWIST && fy == numYEdges - 1) {
16849371c9d4SSatish Balay               edgeT += numXEdges - 1 - 2 * fx;
16859371c9d4SSatish Balay               ornt[2] = 0;
16869371c9d4SSatish Balay             }
16879566063dSJacob Faibussowitsch             if (bdX != DM_BOUNDARY_NONE && fx == numXEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, face, 2));
16889566063dSJacob Faibussowitsch             if (bdY != DM_BOUNDARY_NONE && fy == numYEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, face, 2));
1689d1c88043SMatthew G. Knepley           } else {
16903dfda0b1SToby Isaac             /* markers */
16913dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
16923dfda0b1SToby Isaac               if (fz == numZVertices - 1) {
16939566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerTop));
16949566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerTop));
16959371c9d4SSatish Balay               } else if (fz == 0) {
16969566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerBottom));
16979566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerBottom));
16983dfda0b1SToby Isaac               }
16993dfda0b1SToby Isaac             }
17003dfda0b1SToby Isaac           }
17019371c9d4SSatish Balay           cone[0] = edgeB;
17029371c9d4SSatish Balay           cone[1] = edgeR;
17039371c9d4SSatish Balay           cone[2] = edgeT;
17049371c9d4SSatish Balay           cone[3] = edgeL;
17059566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
17069566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
17073dfda0b1SToby Isaac         }
17083dfda0b1SToby Isaac       }
17093dfda0b1SToby Isaac     }
17103dfda0b1SToby Isaac     /* Build Z edges*/
17113dfda0b1SToby Isaac     for (vy = 0; vy < numYVertices; vy++) {
17123dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
17133dfda0b1SToby Isaac         for (ez = 0; ez < numZEdges; ez++) {
17143dfda0b1SToby Isaac           const PetscInt edge    = firstZEdge + (vy * numXVertices + vx) * numZEdges + ez;
17153dfda0b1SToby Isaac           const PetscInt vertexB = firstVertex + (ez * numYVertices + vy) * numXVertices + vx;
17163dfda0b1SToby Isaac           const PetscInt vertexT = firstVertex + (((ez + 1) % numZVertices) * numYVertices + vy) * numXVertices + vx;
17173dfda0b1SToby Isaac           PetscInt       cone[2];
17183dfda0b1SToby Isaac 
17199371c9d4SSatish Balay           cone[0] = vertexB;
17209371c9d4SSatish Balay           cone[1] = vertexT;
1721c2df9bbfSMatthew G. Knepley           PetscCall(DMPlexSetCone(dm, edge, cone));
17223dfda0b1SToby Isaac           if (dim == 3) {
17233dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
17243dfda0b1SToby Isaac               if (vx == numXVertices - 1) {
17259566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
1726c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1727c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1728c2df9bbfSMatthew G. Knepley               } else if (vx == 0) {
17299566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
1730c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1731c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
17323dfda0b1SToby Isaac               }
17333dfda0b1SToby Isaac             }
17343dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
17353dfda0b1SToby Isaac               if (vy == numYVertices - 1) {
17369566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBack));
1737c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBack));
1738c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBack));
1739c2df9bbfSMatthew G. Knepley               } else if (vy == 0) {
17409566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerFront));
1741c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerFront));
1742c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerFront));
17433dfda0b1SToby Isaac               }
17443dfda0b1SToby Isaac             }
17453dfda0b1SToby Isaac           }
17463dfda0b1SToby Isaac         }
17473dfda0b1SToby Isaac       }
17483dfda0b1SToby Isaac     }
17493dfda0b1SToby Isaac     /* Build Y edges*/
17503dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
17513dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
17523dfda0b1SToby Isaac         for (ey = 0; ey < numYEdges; ey++) {
17533dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdY == DM_BOUNDARY_TWIST && ey == numYEdges - 1) ? (numXVertices - vx - 1) : (vz * numYVertices + ((ey + 1) % numYVertices)) * numXVertices + vx;
17543dfda0b1SToby Isaac           const PetscInt edge    = firstYEdge + (vz * numXVertices + vx) * numYEdges + ey;
17553dfda0b1SToby Isaac           const PetscInt vertexF = firstVertex + (vz * numYVertices + ey) * numXVertices + vx;
17563dfda0b1SToby Isaac           const PetscInt vertexK = firstVertex + nextv;
17573dfda0b1SToby Isaac           PetscInt       cone[2];
17583dfda0b1SToby Isaac 
17599371c9d4SSatish Balay           cone[0] = vertexF;
17609371c9d4SSatish Balay           cone[1] = vertexK;
17619566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, edge, cone));
17623dfda0b1SToby Isaac           if (dim == 2) {
17633dfda0b1SToby Isaac             if ((bdX != DM_BOUNDARY_PERIODIC) && (bdX != DM_BOUNDARY_TWIST)) {
17643dfda0b1SToby Isaac               if (vx == numXVertices - 1) {
17659566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerRight));
17669566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
17679566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1768c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1769d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
17709566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerLeft));
17719566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
17729566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1773c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
17743dfda0b1SToby Isaac               }
1775d8211ee3SMatthew G. Knepley             } else {
17764c67ea77SStefano Zampini               if (vx == 0 && cutLabel) {
17779566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, edge, 1));
17789566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, cone[0], 1));
1779c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMLabelSetValue(cutLabel, cone[1], 1));
17803dfda0b1SToby Isaac               }
1781d8211ee3SMatthew G. Knepley             }
1782d8211ee3SMatthew G. Knepley           } else {
17833dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
17843dfda0b1SToby Isaac               if (vx == numXVertices - 1) {
17859566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
1786c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1787c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1788d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
17899566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
1790c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1791c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
17923dfda0b1SToby Isaac               }
17933dfda0b1SToby Isaac             }
17943dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
17953dfda0b1SToby Isaac               if (vz == numZVertices - 1) {
17969566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
1797c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1798c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1799d8211ee3SMatthew G. Knepley               } else if (vz == 0) {
18009566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
1801c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1802c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
18033dfda0b1SToby Isaac               }
18043dfda0b1SToby Isaac             }
18053dfda0b1SToby Isaac           }
18063dfda0b1SToby Isaac         }
18073dfda0b1SToby Isaac       }
18083dfda0b1SToby Isaac     }
18093dfda0b1SToby Isaac     /* Build X edges*/
18103dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
18113dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; vy++) {
18123dfda0b1SToby Isaac         for (ex = 0; ex < numXEdges; ex++) {
18133dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdX == DM_BOUNDARY_TWIST && ex == numXEdges - 1) ? (numYVertices - vy - 1) * numXVertices : (vz * numYVertices + vy) * numXVertices + (ex + 1) % numXVertices;
18143dfda0b1SToby Isaac           const PetscInt edge    = firstXEdge + (vz * numYVertices + vy) * numXEdges + ex;
18153dfda0b1SToby Isaac           const PetscInt vertexL = firstVertex + (vz * numYVertices + vy) * numXVertices + ex;
18163dfda0b1SToby Isaac           const PetscInt vertexR = firstVertex + nextv;
18173dfda0b1SToby Isaac           PetscInt       cone[2];
18183dfda0b1SToby Isaac 
18199371c9d4SSatish Balay           cone[0] = vertexL;
18209371c9d4SSatish Balay           cone[1] = vertexR;
18219566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, edge, cone));
18223dfda0b1SToby Isaac           if (dim == 2) {
18233dfda0b1SToby Isaac             if ((bdY != DM_BOUNDARY_PERIODIC) && (bdY != DM_BOUNDARY_TWIST)) {
18243dfda0b1SToby Isaac               if (vy == numYVertices - 1) {
18259566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerTop));
18269566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
18279566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1828c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1829d8211ee3SMatthew G. Knepley               } else if (vy == 0) {
18309566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerBottom));
18319566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
18329566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1833c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
18343dfda0b1SToby Isaac               }
1835d8211ee3SMatthew G. Knepley             } else {
18364c67ea77SStefano Zampini               if (vy == 0 && cutLabel) {
18379566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, edge, 1));
18389566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, cone[0], 1));
1839c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMLabelSetValue(cutLabel, cone[1], 1));
18403dfda0b1SToby Isaac               }
1841d8211ee3SMatthew G. Knepley             }
1842d8211ee3SMatthew G. Knepley           } else {
18433dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
18443dfda0b1SToby Isaac               if (vy == numYVertices - 1) {
18459566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBack));
1846c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBack));
1847c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBack));
1848c2df9bbfSMatthew G. Knepley               } else if (vy == 0) {
18499566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerFront));
1850c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerFront));
1851c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerFront));
18523dfda0b1SToby Isaac               }
18533dfda0b1SToby Isaac             }
18543dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
18553dfda0b1SToby Isaac               if (vz == numZVertices - 1) {
18569566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
1857c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1858c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1859c2df9bbfSMatthew G. Knepley               } else if (vz == 0) {
18609566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
1861c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1862c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
18633dfda0b1SToby Isaac               }
18643dfda0b1SToby Isaac             }
18653dfda0b1SToby Isaac           }
18663dfda0b1SToby Isaac         }
18673dfda0b1SToby Isaac       }
18683dfda0b1SToby Isaac     }
18699566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
18709566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
18713dfda0b1SToby Isaac     /* Build coordinates */
18729566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
18739566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
18749566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
18759566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numVertices));
18763dfda0b1SToby Isaac     for (v = firstVertex; v < firstVertex + numVertices; ++v) {
18779566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
18789566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
18793dfda0b1SToby Isaac     }
18809566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
18819566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
18829566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
18839566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
18849566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
18859566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
18869566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates, VECSTANDARD));
18879566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
18883dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; ++vz) {
18893dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; ++vy) {
18903dfda0b1SToby Isaac         for (vx = 0; vx < numXVertices; ++vx) {
18913dfda0b1SToby Isaac           coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 0] = lower[0] + ((upper[0] - lower[0]) / numXEdges) * vx;
18923dfda0b1SToby Isaac           coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 1] = lower[1] + ((upper[1] - lower[1]) / numYEdges) * vy;
1893ad540459SPierre Jolivet           if (dim == 3) coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 2] = lower[2] + ((upper[2] - lower[2]) / numZEdges) * vz;
18943dfda0b1SToby Isaac         }
18953dfda0b1SToby Isaac       }
18963dfda0b1SToby Isaac     }
18979566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
18989566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
18999566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
19003dfda0b1SToby Isaac   }
19013ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
19023dfda0b1SToby Isaac }
19033dfda0b1SToby Isaac 
1904d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxMesh_Tensor_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
1905d71ae5a4SJacob Faibussowitsch {
19069318fe57SMatthew G. Knepley   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
19079318fe57SMatthew G. Knepley   PetscInt       fac[3] = {0, 0, 0}, d;
1908552f7358SJed Brown 
1909552f7358SJed Brown   PetscFunctionBegin;
19104f572ea9SToby Isaac   PetscAssertPointer(dm, 1);
19119318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
19129566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
19139371c9d4SSatish Balay   for (d = 0; d < dim; ++d) {
19149371c9d4SSatish Balay     fac[d] = faces[d];
19159371c9d4SSatish Balay     bdt[d] = periodicity[d];
19169371c9d4SSatish Balay   }
19179566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateCubeMesh_Internal(dm, lower, upper, fac, bdt[0], bdt[1], bdt[2]));
19189371c9d4SSatish 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))) {
19196858538eSMatthew G. Knepley     PetscReal L[3]       = {-1., -1., 0.};
19206858538eSMatthew G. Knepley     PetscReal maxCell[3] = {-1., -1., 0.};
1921552f7358SJed Brown 
19229318fe57SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
19236858538eSMatthew G. Knepley       if (periodicity[d] != DM_BOUNDARY_NONE) {
19249318fe57SMatthew G. Knepley         L[d]       = upper[d] - lower[d];
19259318fe57SMatthew G. Knepley         maxCell[d] = 1.1 * (L[d] / PetscMax(1, faces[d]));
1926768d5fceSMatthew G. Knepley       }
19276858538eSMatthew G. Knepley     }
19284fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, maxCell, lower, L));
1929768d5fceSMatthew G. Knepley   }
19309566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
19313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
19329318fe57SMatthew G. Knepley }
19339318fe57SMatthew G. Knepley 
19345dca41c3SJed 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)
1935d71ae5a4SJacob Faibussowitsch {
19369318fe57SMatthew G. Knepley   PetscFunctionBegin;
193746139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
19385dca41c3SJed Brown   if (shape == DM_SHAPE_ZBOX) PetscCall(DMPlexCreateBoxMesh_Tensor_SFC_Internal(dm, dim, faces, lower, upper, periodicity, interpolate));
19396725e60dSJed Brown   else if (dim == 1) PetscCall(DMPlexCreateLineMesh_Internal(dm, faces[0], lower[0], upper[0], periodicity[0]));
19409566063dSJacob Faibussowitsch   else if (simplex) PetscCall(DMPlexCreateBoxMesh_Simplex_Internal(dm, dim, faces, lower, upper, periodicity, interpolate));
19419566063dSJacob Faibussowitsch   else PetscCall(DMPlexCreateBoxMesh_Tensor_Internal(dm, dim, faces, lower, upper, periodicity));
19429318fe57SMatthew G. Knepley   if (!interpolate && dim > 1 && !simplex) {
1943768d5fceSMatthew G. Knepley     DM udm;
1944768d5fceSMatthew G. Knepley 
19459566063dSJacob Faibussowitsch     PetscCall(DMPlexUninterpolate(dm, &udm));
19469566063dSJacob Faibussowitsch     PetscCall(DMPlexCopyCoordinates(dm, udm));
194769d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &udm));
1948768d5fceSMatthew G. Knepley   }
194946139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
19503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
1951c8c68bd8SToby Isaac }
1952c8c68bd8SToby Isaac 
19535d83a8b1SBarry Smith /*@
1954768d5fceSMatthew G. Knepley   DMPlexCreateBoxMesh - Creates a mesh on the tensor product of unit intervals (box) using simplices or tensor cells (hexahedra).
1955768d5fceSMatthew G. Knepley 
1956d083f849SBarry Smith   Collective
1957768d5fceSMatthew G. Knepley 
1958768d5fceSMatthew G. Knepley   Input Parameters:
1959a1cb98faSBarry Smith + comm               - The communicator for the `DM` object
1960768d5fceSMatthew G. Knepley . dim                - The spatial dimension
1961a1cb98faSBarry Smith . simplex            - `PETSC_TRUE` for simplices, `PETSC_FALSE` for tensor cells
196220f4b53cSBarry Smith . faces              - Number of faces per dimension, or `NULL` for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
196320f4b53cSBarry Smith . lower              - The lower left corner, or `NULL` for (0, 0, 0)
196420f4b53cSBarry Smith . upper              - The upper right corner, or `NULL` for (1, 1, 1)
196520f4b53cSBarry Smith . periodicity        - The boundary type for the X,Y,Z direction, or `NULL` for `DM_BOUNDARY_NONE`
196642108689Sksagiyam . interpolate        - Flag to create intermediate mesh pieces (edges, faces)
196742108689Sksagiyam . localizationHeight - Flag to localize edges and faces in addition to cells; only significant for periodic meshes
196842108689Sksagiyam - sparseLocalize     - Flag to localize coordinates only for cells near the periodic boundary; only significant for periodic meshes
1969768d5fceSMatthew G. Knepley 
1970768d5fceSMatthew G. Knepley   Output Parameter:
1971a1cb98faSBarry Smith . dm - The `DM` object
1972768d5fceSMatthew G. Knepley 
1973768d5fceSMatthew G. Knepley   Level: beginner
1974768d5fceSMatthew G. Knepley 
1975a1cb98faSBarry Smith   Note:
1976a1cb98faSBarry Smith   To customize this mesh using options, use
1977a1cb98faSBarry Smith .vb
1978a1cb98faSBarry Smith   DMCreate(comm, &dm);
1979a1cb98faSBarry Smith   DMSetType(dm, DMPLEX);
1980a1cb98faSBarry Smith   DMSetFromOptions(dm);
1981a1cb98faSBarry Smith .ve
1982a1cb98faSBarry Smith   and use the options in `DMSetFromOptions()`.
1983a1cb98faSBarry Smith 
1984a4e35b19SJacob Faibussowitsch   Here is the numbering returned for 2 faces in each direction for tensor cells\:
1985a1cb98faSBarry Smith .vb
1986a1cb98faSBarry Smith  10---17---11---18----12
1987a1cb98faSBarry Smith   |         |         |
1988a1cb98faSBarry Smith   |         |         |
1989a1cb98faSBarry Smith  20    2   22    3    24
1990a1cb98faSBarry Smith   |         |         |
1991a1cb98faSBarry Smith   |         |         |
1992a1cb98faSBarry Smith   7---15----8---16----9
1993a1cb98faSBarry Smith   |         |         |
1994a1cb98faSBarry Smith   |         |         |
1995a1cb98faSBarry Smith  19    0   21    1   23
1996a1cb98faSBarry Smith   |         |         |
1997a1cb98faSBarry Smith   |         |         |
1998a1cb98faSBarry Smith   4---13----5---14----6
1999a1cb98faSBarry Smith .ve
2000a1cb98faSBarry Smith   and for simplicial cells
2001a1cb98faSBarry Smith .vb
2002a1cb98faSBarry Smith  14----8---15----9----16
2003a1cb98faSBarry Smith   |\     5  |\      7 |
2004a1cb98faSBarry Smith   | \       | \       |
2005a1cb98faSBarry Smith  13   2    14    3    15
2006a1cb98faSBarry Smith   | 4   \   | 6   \   |
2007a1cb98faSBarry Smith   |       \ |       \ |
2008a1cb98faSBarry Smith  11----6---12----7----13
2009a1cb98faSBarry Smith   |\        |\        |
2010a1cb98faSBarry Smith   | \    1  | \     3 |
2011a1cb98faSBarry Smith  10   0    11    1    12
2012a1cb98faSBarry Smith   | 0   \   | 2   \   |
2013a1cb98faSBarry Smith   |       \ |       \ |
2014a1cb98faSBarry Smith   8----4----9----5----10
2015a1cb98faSBarry Smith .ve
2016a1cb98faSBarry Smith 
20171cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMSetFromOptions()`, `DMPlexCreateFromFile()`, `DMPlexCreateHexCylinderMesh()`, `DMSetType()`, `DMCreate()`
2018768d5fceSMatthew G. Knepley @*/
201942108689Sksagiyam 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)
2020d71ae5a4SJacob Faibussowitsch {
20219318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
2022fdbf62faSLisandro Dalcin   PetscReal      low[3] = {0, 0, 0};
2023fdbf62faSLisandro Dalcin   PetscReal      upp[3] = {1, 1, 1};
2024fdbf62faSLisandro Dalcin   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
2025552f7358SJed Brown 
2026768d5fceSMatthew G. Knepley   PetscFunctionBegin;
20279566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
20289566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
20295dca41c3SJed Brown   PetscCall(DMPlexCreateBoxMesh_Internal(*dm, DM_SHAPE_BOX, dim, simplex, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt, interpolate));
203042108689Sksagiyam   if (periodicity) {
203142108689Sksagiyam     DM cdm;
203242108689Sksagiyam 
203342108689Sksagiyam     PetscCall(DMGetCoordinateDM(*dm, &cdm));
203442108689Sksagiyam     PetscCall(DMPlexSetMaxProjectionHeight(cdm, localizationHeight));
203542108689Sksagiyam     PetscCall(DMSetSparseLocalize(*dm, sparseLocalize));
203642108689Sksagiyam     PetscCall(DMLocalizeCoordinates(*dm));
203742108689Sksagiyam   }
20383ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
20399318fe57SMatthew G. Knepley }
2040fdbf62faSLisandro Dalcin 
2041d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateWedgeBoxMesh_Internal(DM dm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
2042d71ae5a4SJacob Faibussowitsch {
20439318fe57SMatthew G. Knepley   DM       bdm, vol;
20449318fe57SMatthew G. Knepley   PetscInt i;
20459318fe57SMatthew G. Knepley 
20469318fe57SMatthew G. Knepley   PetscFunctionBegin;
20471fcf445aSMatthew G. Knepley   // TODO Now we can support periodicity
204808401ef6SPierre Jolivet   for (i = 0; i < 3; ++i) PetscCheck(periodicity[i] == DM_BOUNDARY_NONE, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Periodicity not yet supported");
20499566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &bdm));
20509566063dSJacob Faibussowitsch   PetscCall(DMSetType(bdm, DMPLEX));
20519566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(bdm, 2));
205246139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, bdm, 0, 0, 0));
20539566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxMesh_Simplex_Internal(bdm, 2, faces, lower, upper, periodicity, PETSC_TRUE));
20541fcf445aSMatthew G. Knepley   PetscCall(DMPlexExtrude(bdm, faces[2], upper[2] - lower[2], PETSC_TRUE, PETSC_FALSE, PETSC_FALSE, NULL, NULL, &vol));
205546139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, bdm, 0, 0, 0));
20569566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&bdm));
205769d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &vol));
20589318fe57SMatthew G. Knepley   if (lower[2] != 0.0) {
20599318fe57SMatthew G. Knepley     Vec          v;
20609318fe57SMatthew G. Knepley     PetscScalar *x;
20619318fe57SMatthew G. Knepley     PetscInt     cDim, n;
20629318fe57SMatthew G. Knepley 
20639566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &v));
20649566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(v, &cDim));
20659566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(v, &n));
20669566063dSJacob Faibussowitsch     PetscCall(VecGetArray(v, &x));
20679318fe57SMatthew G. Knepley     x += cDim;
20689318fe57SMatthew G. Knepley     for (i = 0; i < n; i += cDim) x[i] += lower[2];
20699566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(v, &x));
20709566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, v));
20719318fe57SMatthew G. Knepley   }
20723ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2073552f7358SJed Brown }
2074552f7358SJed Brown 
207500dabe28SStefano Zampini /*@
207639f4f5dbSPierre Jolivet   DMPlexCreateWedgeBoxMesh - Creates a 3-D mesh tessellating the (x,y) plane and extruding in the third direction using wedge cells.
207700dabe28SStefano Zampini 
2078d083f849SBarry Smith   Collective
207900dabe28SStefano Zampini 
208000dabe28SStefano Zampini   Input Parameters:
2081a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
208220f4b53cSBarry Smith . faces       - Number of faces per dimension, or `NULL` for (1, 1, 1)
208320f4b53cSBarry Smith . lower       - The lower left corner, or `NULL` for (0, 0, 0)
208420f4b53cSBarry Smith . upper       - The upper right corner, or `NULL` for (1, 1, 1)
208520f4b53cSBarry Smith . periodicity - The boundary type for the X,Y,Z direction, or `NULL` for `DM_BOUNDARY_NONE`
2086a1cb98faSBarry Smith . orderHeight - If `PETSC_TRUE`, orders the extruded cells in the height first. Otherwise, orders the cell on the layers first
208700dabe28SStefano Zampini - interpolate - Flag to create intermediate mesh pieces (edges, faces)
208800dabe28SStefano Zampini 
208900dabe28SStefano Zampini   Output Parameter:
2090a1cb98faSBarry Smith . dm - The `DM` object
209100dabe28SStefano Zampini 
209200dabe28SStefano Zampini   Level: beginner
209300dabe28SStefano Zampini 
20941cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateHexCylinderMesh()`, `DMPlexCreateWedgeCylinderMesh()`, `DMExtrude()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
209500dabe28SStefano Zampini @*/
2096d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateWedgeBoxMesh(MPI_Comm comm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool orderHeight, PetscBool interpolate, DM *dm)
2097d71ae5a4SJacob Faibussowitsch {
20989318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
209900dabe28SStefano Zampini   PetscReal      low[3] = {0, 0, 0};
210000dabe28SStefano Zampini   PetscReal      upp[3] = {1, 1, 1};
210100dabe28SStefano Zampini   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
210200dabe28SStefano Zampini 
210300dabe28SStefano Zampini   PetscFunctionBegin;
21049566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
21059566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
21069566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateWedgeBoxMesh_Internal(*dm, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt));
2107d410b0cfSMatthew G. Knepley   if (!interpolate) {
2108d410b0cfSMatthew G. Knepley     DM udm;
210900dabe28SStefano Zampini 
21109566063dSJacob Faibussowitsch     PetscCall(DMPlexUninterpolate(*dm, &udm));
211169d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(*dm, &udm));
211200dabe28SStefano Zampini   }
21137ff04441SMatthew G. Knepley   if (periodicity) PetscCall(DMLocalizeCoordinates(*dm));
21143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
211500dabe28SStefano Zampini }
211600dabe28SStefano Zampini 
2117cfb853baSMatthew G. Knepley /*
2118cfb853baSMatthew 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.
2119cfb853baSMatthew G. Knepley 
2120cfb853baSMatthew G. Knepley   Input Parameters:
2121cfb853baSMatthew G. Knepley + len - The length of the tuple
2122cfb853baSMatthew G. Knepley . max - The maximum for each dimension, so values are in [0, max)
2123cfb853baSMatthew G. Knepley - tup - A tuple of length len+1: tup[len] > 0 indicates a stopping condition
2124cfb853baSMatthew G. Knepley 
2125cfb853baSMatthew G. Knepley   Output Parameter:
212620f4b53cSBarry Smith . tup - A tuple of `len` integers whose entries are at most `max`
2127cfb853baSMatthew G. Knepley 
2128cfb853baSMatthew G. Knepley   Level: developer
2129cfb853baSMatthew G. Knepley 
213020f4b53cSBarry Smith   Note:
213120f4b53cSBarry Smith   Ordering is lexicographic with lowest index as least significant in ordering.
213220f4b53cSBarry 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}.
213320f4b53cSBarry Smith 
2134cfb853baSMatthew G. Knepley .seealso: PetscDualSpaceTensorPointLexicographic_Internal(), PetscDualSpaceLatticePointLexicographic_Internal()
2135cfb853baSMatthew G. Knepley */
2136cfb853baSMatthew G. Knepley static PetscErrorCode DMPlexTensorPointLexicographic_Private(PetscInt len, const PetscInt max[], PetscInt tup[])
2137cfb853baSMatthew G. Knepley {
2138cfb853baSMatthew G. Knepley   PetscInt i;
2139cfb853baSMatthew G. Knepley 
2140cfb853baSMatthew G. Knepley   PetscFunctionBegin;
2141cfb853baSMatthew G. Knepley   for (i = 0; i < len; ++i) {
2142cfb853baSMatthew G. Knepley     if (tup[i] < max[i] - 1) {
2143cfb853baSMatthew G. Knepley       break;
2144cfb853baSMatthew G. Knepley     } else {
2145cfb853baSMatthew G. Knepley       tup[i] = 0;
2146cfb853baSMatthew G. Knepley     }
2147cfb853baSMatthew G. Knepley   }
2148cfb853baSMatthew G. Knepley   if (i == len) tup[i - 1] = max[i - 1];
2149cfb853baSMatthew G. Knepley   else ++tup[i];
21503ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2151cfb853baSMatthew G. Knepley }
2152cfb853baSMatthew G. Knepley 
2153cfb853baSMatthew G. Knepley static PetscInt TupleToIndex_Private(PetscInt len, const PetscInt max[], const PetscInt tup[])
2154cfb853baSMatthew G. Knepley {
21558d2ec52aSSatish Balay   PetscInt idx = tup[len - 1];
2156cfb853baSMatthew G. Knepley 
21578d2ec52aSSatish Balay   for (PetscInt i = len - 2; i >= 0; --i) {
2158cfb853baSMatthew G. Knepley     idx *= max[i];
2159cfb853baSMatthew G. Knepley     idx += tup[i];
2160cfb853baSMatthew G. Knepley   }
2161cfb853baSMatthew G. Knepley   return idx;
2162cfb853baSMatthew G. Knepley }
2163cfb853baSMatthew G. Knepley 
21648d2ec52aSSatish Balay static void IndexToTuple_Private(PetscInt len, const PetscInt max[], PetscInt idx, PetscInt tup[])
2165cfb853baSMatthew G. Knepley {
21668d2ec52aSSatish Balay   for (PetscInt i = 0; i < len; ++i) {
21678d2ec52aSSatish Balay     tup[i] = idx % max[i];
21688d2ec52aSSatish Balay     idx    = (idx - tup[i]) / max[i];
21698d2ec52aSSatish Balay   }
21708d2ec52aSSatish Balay }
21718d2ec52aSSatish Balay 
21728d2ec52aSSatish Balay static void TupleToRanks_Private(PetscInt len, const PetscInt max[], const PetscInt procs[], const PetscInt tup[], PetscInt ranks[])
21738d2ec52aSSatish Balay {
21748d2ec52aSSatish Balay   for (PetscInt i = 0; i < len; ++i) {
21758d2ec52aSSatish Balay     const PetscInt div = max[i] / procs[i];
21768d2ec52aSSatish Balay     const PetscInt rem = max[i] % procs[i];
21778d2ec52aSSatish Balay     const PetscInt idx = (tup[i] < 0 ? max[i] + tup[i] : tup[i]) % max[i];
21788d2ec52aSSatish Balay 
21798d2ec52aSSatish Balay     if (idx < rem * (div + 1)) ranks[i] = idx / (div + 1);
21808d2ec52aSSatish Balay     else ranks[i] = rem + (idx - rem * (div + 1)) / div;
21818d2ec52aSSatish Balay   }
21828d2ec52aSSatish Balay }
21838d2ec52aSSatish Balay 
21848d2ec52aSSatish Balay static void RanksToSizes_Private(PetscInt len, const PetscInt max[], const PetscInt procs[], const PetscInt ranks[], PetscInt sizes[])
21858d2ec52aSSatish Balay {
21868d2ec52aSSatish Balay   for (PetscInt i = 0; i < len; ++i) {
21878d2ec52aSSatish Balay     const PetscInt div = max[i] / procs[i];
21888d2ec52aSSatish Balay     const PetscInt rem = max[i] % procs[i];
21898d2ec52aSSatish Balay 
21908d2ec52aSSatish Balay     sizes[i] = ranks[i] < rem ? div + 1 : div;
21918d2ec52aSSatish Balay   }
21928d2ec52aSSatish Balay }
21938d2ec52aSSatish Balay 
21948d2ec52aSSatish Balay /*
21958d2ec52aSSatish 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.
21968d2ec52aSSatish Balay 
21978d2ec52aSSatish 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.
21988d2ec52aSSatish Balay 
21998d2ec52aSSatish 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.
2200dfe9cfe5SMatthew Knepley 
2201dfe9cfe5SMatthew Knepley   Parallel Layout:
2202dfe9cfe5SMatthew Knepley 
2203dfe9cfe5SMatthew 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.
22048d2ec52aSSatish Balay */
22058d2ec52aSSatish Balay static PetscErrorCode DMPlexCreateHypercubicMesh_Internal(DM dm, PetscInt dim, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], PetscInt overlap, const DMBoundaryType bd[])
22068d2ec52aSSatish Balay {
22078d2ec52aSSatish Balay   const PetscInt debug = ((DM_Plex *)dm->data)->printAdj;
22088d2ec52aSSatish Balay   PetscSF        sf;
2209cfb853baSMatthew G. Knepley   Vec            coordinates;
2210cfb853baSMatthew G. Knepley   PetscSection   coordSection;
2211cfb853baSMatthew G. Knepley   DMLabel        cutLabel    = NULL;
2212cfb853baSMatthew G. Knepley   PetscBool      cutMarker   = PETSC_FALSE;
2213cfb853baSMatthew G. Knepley   PetscBool      periodic    = PETSC_FALSE;
22148d2ec52aSSatish Balay   PetscInt       numCells    = 1;
22158d2ec52aSSatish Balay   PetscInt       numVertices = 1;
22168d2ec52aSSatish Balay   PetscSFNode   *remotes;
2217cfb853baSMatthew G. Knepley   PetscScalar   *coords;
22188d2ec52aSSatish Balay   PetscInt      *procs;     // The number of processes along each dimension
22198d2ec52aSSatish Balay   PetscInt      *lrank;     // Rank in each dimension, lrank[d] \in [0, procs[d])
22208d2ec52aSSatish Balay   PetscInt      *ledges;    // The number of edges along each dimension for this process
22218d2ec52aSSatish Balay   PetscInt      *vstart;    // The first vertex along each dimension on this processes
22228d2ec52aSSatish Balay   PetscInt      *vertices;  // The number of vertices along each dimension on this process
22238d2ec52aSSatish Balay   PetscInt      *rvert;     // The global (not local) vertex number along each dimension
22248d2ec52aSSatish Balay   PetscInt      *rrank;     // The rank along each dimension for the process owning rvert[]
22258d2ec52aSSatish Balay   PetscInt      *rvertices; // The number of vertices along each dimension for the process rrank[]
22268d2ec52aSSatish Balay   PetscInt      *vert, *vtmp, *supp, cone[2], *leaves;
22278d2ec52aSSatish Balay   PetscInt       cell = 0, coordSize, Nl = 0, Nl2 = 0;
22288d2ec52aSSatish Balay   PetscMPIInt    rank, size;
22298d2ec52aSSatish Balay   MPI_Comm       comm;
2230cfb853baSMatthew G. Knepley 
2231cfb853baSMatthew G. Knepley   PetscFunctionBegin;
22328d2ec52aSSatish Balay   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
22338d2ec52aSSatish Balay   PetscCallMPI(MPI_Comm_rank(comm, &rank));
22348d2ec52aSSatish Balay   PetscCallMPI(MPI_Comm_size(comm, &size));
2235cfb853baSMatthew G. Knepley   PetscCall(DMSetDimension(dm, dim));
22368d2ec52aSSatish Balay   PetscCall(DMPlexDistributeSetDefault(dm, PETSC_FALSE));
22378d2ec52aSSatish Balay   PetscCall(PetscCalloc4(dim, &procs, dim, &lrank, dim, &rrank, 2 * dim, &supp));
22388d2ec52aSSatish Balay   PetscCall(PetscCalloc7(dim, &ledges, dim, &vertices, dim, &rvertices, dim, &vert, dim, &rvert, dim, &vstart, dim, &vtmp));
2239cfb853baSMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "marker"));
2240cfb853baSMatthew G. Knepley   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL));
22418d2ec52aSSatish Balay   for (PetscInt d = 0; d < dim; ++d) periodic = (periodic || bd[d] == DM_BOUNDARY_PERIODIC) ? PETSC_TRUE : PETSC_FALSE;
2242cfb853baSMatthew G. Knepley   if (periodic && cutMarker) {
2243cfb853baSMatthew G. Knepley     PetscCall(DMCreateLabel(dm, "periodic_cut"));
2244cfb853baSMatthew G. Knepley     PetscCall(DMGetLabel(dm, "periodic_cut", &cutLabel));
2245cfb853baSMatthew G. Knepley   }
22468d2ec52aSSatish Balay   for (PetscInt d = 0; d < dim; ++d) PetscCheck(bd[d] == DM_BOUNDARY_PERIODIC, comm, PETSC_ERR_SUP, "Hypercubic mesh must be periodic now");
22478d2ec52aSSatish Balay   overlap = overlap == PETSC_DETERMINE ? 1 : overlap;
22488d2ec52aSSatish Balay   PetscCheck(overlap >= 1, comm, PETSC_ERR_SUP, "Overlap %" PetscInt_FMT " must be greater than 0", overlap);
22498d2ec52aSSatish Balay   if (size > 1) {
22508d2ec52aSSatish Balay     PetscInt Npr = 1;
22518d2ec52aSSatish Balay 
22528d2ec52aSSatish Balay     // Make process grid
22538d2ec52aSSatish Balay     if (debug) PetscCall(PetscPrintf(comm, "Process grid:"));
22548d2ec52aSSatish Balay     for (PetscInt d = 0; d < dim; ++d) {
22558d2ec52aSSatish Balay       procs[d] = PetscRintReal(PetscPowReal(size, 1. / dim));
22568d2ec52aSSatish Balay       Npr *= procs[d];
22578d2ec52aSSatish Balay       if (debug) PetscCall(PetscPrintf(comm, " %" PetscInt_FMT, procs[d]));
22588d2ec52aSSatish Balay     }
22598d2ec52aSSatish Balay     if (debug) PetscCall(PetscPrintf(comm, "\n"));
22608d2ec52aSSatish Balay     PetscCheck(Npr == size, comm, PETSC_ERR_PLIB, "Process grid size %" PetscInt_FMT " != %d comm size", Npr, size);
22618d2ec52aSSatish Balay     IndexToTuple_Private(dim, procs, rank, lrank);
22628d2ec52aSSatish Balay     for (PetscInt d = 0; d < dim; ++d) {
22638d2ec52aSSatish Balay       ledges[d] = edges[d] / procs[d] + (edges[d] % procs[d] > lrank[d] ? 1 : 0);
22648d2ec52aSSatish Balay       vstart[d] = 0;
22658d2ec52aSSatish Balay       for (PetscInt r = 0; r < lrank[d]; ++r) vstart[d] += edges[d] / procs[d] + (edges[d] % procs[d] > r ? 1 : 0);
22668d2ec52aSSatish Balay       vstart[d] -= overlap; // For halo
22678d2ec52aSSatish Balay     }
22688d2ec52aSSatish Balay   } else {
22698d2ec52aSSatish Balay     for (PetscInt d = 0; d < dim; ++d) {
22708d2ec52aSSatish Balay       procs[d]  = 1;
22718d2ec52aSSatish Balay       ledges[d] = edges[d];
22728d2ec52aSSatish Balay     }
22738d2ec52aSSatish Balay   }
22748d2ec52aSSatish Balay   // Calculate local patch size
22758d2ec52aSSatish Balay   for (PetscInt d = 0; d < dim; ++d) {
22768d2ec52aSSatish Balay     vertices[d] = ledges[d] + (procs[d] > 1 ? 2 * overlap : 0);
2277cfb853baSMatthew G. Knepley     numVertices *= vertices[d];
2278cfb853baSMatthew G. Knepley   }
2279cfb853baSMatthew G. Knepley   numCells = numVertices * dim;
2280cfb853baSMatthew G. Knepley   PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
22818d2ec52aSSatish Balay   for (PetscInt c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, 2));
22828d2ec52aSSatish Balay   for (PetscInt v = numCells; v < numCells + numVertices; ++v) PetscCall(DMPlexSetSupportSize(dm, v, 2 * dim));
2283cfb853baSMatthew G. Knepley   PetscCall(DMSetUp(dm)); /* Allocate space for cones and supports */
2284cfb853baSMatthew G. Knepley   /* Build cell cones and vertex supports */
2285cfb853baSMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "celltype"));
22868d2ec52aSSatish Balay   if (debug) PetscCall(PetscSynchronizedPrintf(comm, "Topology for rank %d:\n", rank));
2287cfb853baSMatthew G. Knepley   while (vert[dim - 1] < vertices[dim - 1]) {
2288cfb853baSMatthew G. Knepley     const PetscInt vertex = TupleToIndex_Private(dim, vertices, vert) + numCells;
2289cfb853baSMatthew G. Knepley     PetscInt       s      = 0;
22908d2ec52aSSatish Balay     PetscBool      leaf   = PETSC_FALSE;
2291cfb853baSMatthew G. Knepley 
22928d2ec52aSSatish Balay     if (debug) {
22938d2ec52aSSatish Balay       PetscCall(PetscSynchronizedPrintf(comm, "Vertex %" PetscInt_FMT ":", vertex));
22948d2ec52aSSatish Balay       for (PetscInt d = 0; d < dim; ++d) PetscCall(PetscSynchronizedPrintf(comm, " %" PetscInt_FMT, vert[d]));
22958d2ec52aSSatish Balay       PetscCall(PetscSynchronizedPrintf(comm, "\n"));
22968d2ec52aSSatish Balay     }
2297cfb853baSMatthew G. Knepley     PetscCall(DMPlexSetCellType(dm, vertex, DM_POLYTOPE_POINT));
22988d2ec52aSSatish Balay     // Define edge cones
22998d2ec52aSSatish Balay     for (PetscInt d = 0; d < dim; ++d) {
23008d2ec52aSSatish Balay       for (PetscInt e = 0; e < dim; ++e) vtmp[e] = vert[e];
2301cfb853baSMatthew G. Knepley       vtmp[d] = (vert[d] + 1) % vertices[d];
2302cfb853baSMatthew G. Knepley       cone[0] = vertex;
2303cfb853baSMatthew G. Knepley       cone[1] = TupleToIndex_Private(dim, vertices, vtmp) + numCells;
23048d2ec52aSSatish Balay       if (debug) {
23058d2ec52aSSatish Balay         PetscCall(PetscSynchronizedPrintf(comm, "  Vertex %" PetscInt_FMT ":", cone[1]));
23068d2ec52aSSatish Balay         for (PetscInt e = 0; e < dim; ++e) PetscCall(PetscSynchronizedPrintf(comm, " %" PetscInt_FMT, vtmp[e]));
23078d2ec52aSSatish Balay         PetscCall(PetscSynchronizedPrintf(comm, "\n"));
23088d2ec52aSSatish Balay       }
2309cfb853baSMatthew G. Knepley       PetscCall(DMPlexSetCone(dm, cell, cone));
2310cfb853baSMatthew G. Knepley       PetscCall(DMPlexSetCellType(dm, cell, DM_POLYTOPE_SEGMENT));
23118d2ec52aSSatish Balay       if (debug) PetscCall(PetscSynchronizedPrintf(comm, "  Edge %" PetscInt_FMT " (%" PetscInt_FMT " %" PetscInt_FMT ")\n", cell, cone[0], cone[1]));
2312cfb853baSMatthew G. Knepley       ++cell;
23138d2ec52aSSatish Balay       // Shared vertices are any in the first or last overlap layers
23148d2ec52aSSatish Balay       if (vert[d] < overlap || vert[d] >= vertices[d] - overlap) leaf = PETSC_TRUE;
2315cfb853baSMatthew G. Knepley     }
23168d2ec52aSSatish Balay     if (size > 1 && leaf) ++Nl;
23178d2ec52aSSatish Balay     // Define vertex supports
23188d2ec52aSSatish Balay     for (PetscInt d = 0; d < dim; ++d) {
23198d2ec52aSSatish Balay       for (PetscInt e = 0; e < dim; ++e) vtmp[e] = vert[e];
2320cfb853baSMatthew G. Knepley       vtmp[d]   = (vert[d] + vertices[d] - 1) % vertices[d];
2321cfb853baSMatthew G. Knepley       supp[s++] = TupleToIndex_Private(dim, vertices, vtmp) * dim + d;
2322cfb853baSMatthew G. Knepley       supp[s++] = (vertex - numCells) * dim + d;
2323cfb853baSMatthew G. Knepley       PetscCall(DMPlexSetSupport(dm, vertex, supp));
2324cfb853baSMatthew G. Knepley     }
2325cfb853baSMatthew G. Knepley     PetscCall(DMPlexTensorPointLexicographic_Private(dim, vertices, vert));
2326cfb853baSMatthew G. Knepley   }
23278d2ec52aSSatish Balay   if (debug) PetscCall(PetscSynchronizedFlush(comm, NULL));
2328cfb853baSMatthew G. Knepley   PetscCall(DMPlexStratify(dm));
23298d2ec52aSSatish Balay   // Allocate for SF
23308d2ec52aSSatish Balay   PetscCall(PetscMalloc1(Nl, &leaves));
23318d2ec52aSSatish Balay   PetscCall(PetscMalloc1(Nl, &remotes));
23328d2ec52aSSatish Balay   // Build coordinates
2333cfb853baSMatthew G. Knepley   PetscCall(DMGetCoordinateSection(dm, &coordSection));
2334cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetNumFields(coordSection, 1));
2335cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
2336cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices));
23378d2ec52aSSatish Balay   for (PetscInt v = numCells; v < numCells + numVertices; ++v) {
2338cfb853baSMatthew G. Knepley     PetscCall(PetscSectionSetDof(coordSection, v, dim));
2339cfb853baSMatthew G. Knepley     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
2340cfb853baSMatthew G. Knepley   }
2341cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetUp(coordSection));
2342cfb853baSMatthew G. Knepley   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
23438d2ec52aSSatish Balay   PetscCall(VecCreate(comm, &coordinates));
2344cfb853baSMatthew G. Knepley   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
2345cfb853baSMatthew G. Knepley   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
2346cfb853baSMatthew G. Knepley   PetscCall(VecSetBlockSize(coordinates, dim));
2347cfb853baSMatthew G. Knepley   PetscCall(VecSetType(coordinates, VECSTANDARD));
2348cfb853baSMatthew G. Knepley   PetscCall(VecGetArray(coordinates, &coords));
23498d2ec52aSSatish Balay   if (debug) PetscCall(PetscSynchronizedPrintf(comm, "Geometry for rank %d:\n", rank));
23508d2ec52aSSatish Balay   for (PetscInt d = 0; d < dim; ++d) vert[d] = 0;
2351cfb853baSMatthew G. Knepley   while (vert[dim - 1] < vertices[dim - 1]) {
2352cfb853baSMatthew G. Knepley     const PetscInt vertex = TupleToIndex_Private(dim, vertices, vert);
23538d2ec52aSSatish Balay     PetscBool      leaf   = PETSC_FALSE;
2354cfb853baSMatthew G. Knepley 
23558d2ec52aSSatish Balay     for (PetscInt d = 0; d < dim; ++d) {
23568d2ec52aSSatish Balay       coords[vertex * dim + d] = lower[d] + ((upper[d] - lower[d]) / edges[d]) * (vert[d] + vstart[d]);
23578d2ec52aSSatish Balay       if (vert[d] < overlap || vert[d] >= vertices[d] - overlap) leaf = PETSC_TRUE;
23588d2ec52aSSatish Balay     }
23598d2ec52aSSatish Balay     if (size > 1 && leaf) {
23608d2ec52aSSatish Balay       PetscInt rnumCells = 1;
23618d2ec52aSSatish Balay 
23628d2ec52aSSatish Balay       for (PetscInt d = 0; d < dim; ++d) rvert[d] = vert[d] + vstart[d];
23638d2ec52aSSatish Balay       TupleToRanks_Private(dim, edges, procs, rvert, rrank);
23648d2ec52aSSatish Balay       leaves[Nl2]       = vertex + numCells;
23658d2ec52aSSatish Balay       remotes[Nl2].rank = TupleToIndex_Private(dim, procs, rrank);
23668d2ec52aSSatish Balay       RanksToSizes_Private(dim, edges, procs, rrank, rvertices);
23678d2ec52aSSatish Balay       for (PetscInt d = 0; d < dim; ++d) {
23688d2ec52aSSatish Balay         rvertices[d] += 2 * overlap; // Add halo
23698d2ec52aSSatish Balay         rnumCells *= rvertices[d];
23708d2ec52aSSatish Balay       }
23718d2ec52aSSatish Balay       rnumCells *= dim;
23728d2ec52aSSatish Balay       for (PetscInt d = 0; d < dim; ++d) {
23738d2ec52aSSatish Balay         const PetscInt diff = rrank[d] - lrank[d];
23748d2ec52aSSatish Balay 
23758d2ec52aSSatish Balay         if (!diff) rvert[d] = vert[d];                                     // Vertex is local
23768d2ec52aSSatish Balay         else if (rvert[d] < 0) rvert[d] = rvertices[d] - 1 + rvert[d];     // Wrap around at the bottom
23778d2ec52aSSatish Balay         else if (rvert[d] >= edges[d]) rvert[d] = rvert[d] - edges[d] + 1; // Wrap around at the top
23788d2ec52aSSatish Balay         else if (diff == -1) rvert[d] = rvertices[d] - 1 + (vert[d] - overlap);
23798d2ec52aSSatish Balay         else if (diff == 1) rvert[d] = (vertices[d] - vert[d] - 1) + overlap;
23808d2ec52aSSatish Balay         else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Process distance %" PetscInt_FMT " in direction %" PetscInt_FMT " should not be possible", diff, d);
23818d2ec52aSSatish Balay       }
23828d2ec52aSSatish Balay       remotes[Nl2].index = TupleToIndex_Private(dim, rvertices, rvert) + rnumCells;
23838d2ec52aSSatish Balay       if (debug) PetscCall(PetscSynchronizedPrintf(comm, "Shared Vertex %" PetscInt_FMT " (%" PetscInt_FMT ", %" PetscInt_FMT ")\n", leaves[Nl2], remotes[Nl2].rank, remotes[Nl2].index));
23848d2ec52aSSatish Balay       ++Nl2;
23858d2ec52aSSatish Balay     }
23868d2ec52aSSatish Balay     if (debug) {
23878d2ec52aSSatish Balay       PetscCall(PetscSynchronizedPrintf(comm, "Vertex %" PetscInt_FMT ":", vertex));
23888d2ec52aSSatish Balay       for (PetscInt d = 0; d < dim; ++d) PetscCall(PetscSynchronizedPrintf(comm, " %" PetscInt_FMT, vert[d] + vstart[d]));
23898d2ec52aSSatish Balay       for (PetscInt d = 0; d < dim; ++d) PetscCall(PetscSynchronizedPrintf(comm, " %g", (double)PetscRealPart(coords[vertex * dim + d])));
23908d2ec52aSSatish Balay       PetscCall(PetscSynchronizedPrintf(comm, "\n"));
23918d2ec52aSSatish Balay     }
2392cfb853baSMatthew G. Knepley     PetscCall(DMPlexTensorPointLexicographic_Private(dim, vertices, vert));
2393cfb853baSMatthew G. Knepley   }
23948d2ec52aSSatish Balay   if (debug) PetscCall(PetscSynchronizedFlush(comm, NULL));
2395cfb853baSMatthew G. Knepley   PetscCall(VecRestoreArray(coordinates, &coords));
2396cfb853baSMatthew G. Knepley   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
2397cfb853baSMatthew G. Knepley   PetscCall(VecDestroy(&coordinates));
23988d2ec52aSSatish Balay   // Build SF
23998d2ec52aSSatish Balay   PetscCheck(Nl == Nl2, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Initial number of leaves %" PetscInt_FMT " != %" PetscInt_FMT " final number", Nl, Nl2);
24008d2ec52aSSatish Balay   PetscCall(DMGetPointSF(dm, &sf));
24018d2ec52aSSatish Balay   PetscCall(PetscSFSetGraph(sf, numCells + numVertices, Nl, leaves, PETSC_OWN_POINTER, remotes, PETSC_OWN_POINTER));
24028d2ec52aSSatish Balay   if (debug) PetscCall(PetscSFView(sf, PETSC_VIEWER_STDOUT_WORLD));
2403cfb853baSMatthew G. Knepley   //PetscCall(DMSetPeriodicity(dm, NULL, lower, upper));
2404cfb853baSMatthew G. Knepley   // Attach the extent
2405cfb853baSMatthew G. Knepley   {
2406cfb853baSMatthew G. Knepley     PetscContainer c;
24078d2ec52aSSatish Balay     PetscInt      *extent, *lextent;
2408cfb853baSMatthew G. Knepley 
2409cfb853baSMatthew G. Knepley     PetscCall(PetscMalloc1(dim, &extent));
24108d2ec52aSSatish Balay     PetscCall(PetscMalloc1(dim, &lextent));
24118d2ec52aSSatish Balay     for (PetscInt d = 0; d < dim; ++d) {
24128d2ec52aSSatish Balay       extent[d]  = edges[d];
24138d2ec52aSSatish Balay       lextent[d] = ledges[d];
24148d2ec52aSSatish Balay     }
2415cfb853baSMatthew G. Knepley     PetscCall(PetscContainerCreate(PETSC_COMM_SELF, &c));
241649abdd8aSBarry Smith     PetscCall(PetscContainerSetCtxDestroy(c, PetscCtxDestroyDefault));
2417cfb853baSMatthew G. Knepley     PetscCall(PetscContainerSetPointer(c, extent));
2418cfb853baSMatthew G. Knepley     PetscCall(PetscObjectCompose((PetscObject)dm, "_extent", (PetscObject)c));
2419cfb853baSMatthew G. Knepley     PetscCall(PetscContainerDestroy(&c));
24208d2ec52aSSatish Balay     PetscCall(PetscContainerCreate(PETSC_COMM_SELF, &c));
24218d2ec52aSSatish Balay     PetscCall(PetscContainerSetCtxDestroy(c, PetscCtxDestroyDefault));
24228d2ec52aSSatish Balay     PetscCall(PetscContainerSetPointer(c, lextent));
24238d2ec52aSSatish Balay     PetscCall(PetscObjectCompose((PetscObject)dm, "_lextent", (PetscObject)c));
24248d2ec52aSSatish Balay     PetscCall(PetscContainerDestroy(&c));
2425cfb853baSMatthew G. Knepley   }
24268d2ec52aSSatish Balay   PetscCall(PetscFree4(procs, lrank, rrank, supp));
24278d2ec52aSSatish Balay   PetscCall(PetscFree7(ledges, vertices, rvertices, vert, rvert, vstart, vtmp));
24283ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2429cfb853baSMatthew G. Knepley }
2430cfb853baSMatthew G. Knepley 
2431cfb853baSMatthew G. Knepley /*@C
2432aaa8cc7dSPierre Jolivet   DMPlexCreateHypercubicMesh - Creates a periodic mesh on the tensor product of unit intervals using only vertices and edges.
2433cfb853baSMatthew G. Knepley 
2434cfb853baSMatthew G. Knepley   Collective
2435cfb853baSMatthew G. Knepley 
2436cfb853baSMatthew G. Knepley   Input Parameters:
24378d2ec52aSSatish Balay + comm    - The communicator for the `DM` object
2438cfb853baSMatthew G. Knepley . dim     - The spatial dimension
243920f4b53cSBarry Smith . edges   - Number of edges per dimension, or `NULL` for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
244020f4b53cSBarry Smith . lower   - The lower left corner, or `NULL` for (0, 0, 0)
24418d2ec52aSSatish Balay . upper   - The upper right corner, or `NULL` for (1, 1, 1)
24428d2ec52aSSatish Balay - overlap - The number of vertices in each direction to include in the overlap (default is 1)
2443cfb853baSMatthew G. Knepley 
2444cfb853baSMatthew G. Knepley   Output Parameter:
2445cfb853baSMatthew G. Knepley . dm - The DM object
2446cfb853baSMatthew G. Knepley 
244720f4b53cSBarry Smith   Level: beginner
244820f4b53cSBarry Smith 
244920f4b53cSBarry Smith   Note:
245020f4b53cSBarry Smith   If you want to customize this mesh using options, you just need to
245120f4b53cSBarry Smith .vb
245220f4b53cSBarry Smith   DMCreate(comm, &dm);
245320f4b53cSBarry Smith   DMSetType(dm, DMPLEX);
245420f4b53cSBarry Smith   DMSetFromOptions(dm);
245520f4b53cSBarry Smith .ve
245620f4b53cSBarry Smith   and use the options on the `DMSetFromOptions()` page.
2457cfb853baSMatthew G. Knepley 
2458cfb853baSMatthew G. Knepley   The vertices are numbered is lexicographic order, and the dim edges exiting a vertex in the positive orthant are number consecutively,
245920f4b53cSBarry Smith .vb
246020f4b53cSBarry Smith  18--0-19--2-20--4-18
246120f4b53cSBarry Smith   |     |     |     |
246220f4b53cSBarry Smith  13    15    17    13
246320f4b53cSBarry Smith   |     |     |     |
246420f4b53cSBarry Smith  24-12-25-14-26-16-24
246520f4b53cSBarry Smith   |     |     |     |
246620f4b53cSBarry Smith   7     9    11     7
246720f4b53cSBarry Smith   |     |     |     |
246820f4b53cSBarry Smith  21--6-22--8-23-10-21
246920f4b53cSBarry Smith   |     |     |     |
247020f4b53cSBarry Smith   1     3     5     1
247120f4b53cSBarry Smith   |     |     |     |
247220f4b53cSBarry Smith  18--0-19--2-20--4-18
247320f4b53cSBarry Smith .ve
2474cfb853baSMatthew G. Knepley 
247576fbde31SPierre Jolivet .seealso: `DMSetFromOptions()`, `DMPlexCreateFromFile()`, `DMPlexCreateHexCylinderMesh()`, `DMSetType()`, `DMCreate()`
2476cfb853baSMatthew G. Knepley @*/
24778d2ec52aSSatish Balay PetscErrorCode DMPlexCreateHypercubicMesh(MPI_Comm comm, PetscInt dim, const PetscInt edges[], const PetscReal lower[], const PetscReal upper[], PetscInt overlap, DM *dm)
2478cfb853baSMatthew G. Knepley {
2479cfb853baSMatthew G. Knepley   PetscInt       *edg;
2480cfb853baSMatthew G. Knepley   PetscReal      *low, *upp;
2481cfb853baSMatthew G. Knepley   DMBoundaryType *bdt;
2482cfb853baSMatthew G. Knepley   PetscInt        d;
2483cfb853baSMatthew G. Knepley 
2484cfb853baSMatthew G. Knepley   PetscFunctionBegin;
2485cfb853baSMatthew G. Knepley   PetscCall(DMCreate(comm, dm));
2486cfb853baSMatthew G. Knepley   PetscCall(DMSetType(*dm, DMPLEX));
2487cfb853baSMatthew G. Knepley   PetscCall(PetscMalloc4(dim, &edg, dim, &low, dim, &upp, dim, &bdt));
2488cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) {
2489cfb853baSMatthew G. Knepley     edg[d] = edges ? edges[d] : 1;
2490cfb853baSMatthew G. Knepley     low[d] = lower ? lower[d] : 0.;
2491cfb853baSMatthew G. Knepley     upp[d] = upper ? upper[d] : 1.;
2492cfb853baSMatthew G. Knepley     bdt[d] = DM_BOUNDARY_PERIODIC;
2493cfb853baSMatthew G. Knepley   }
24948d2ec52aSSatish Balay   PetscCall(DMPlexCreateHypercubicMesh_Internal(*dm, dim, low, upp, edg, overlap, bdt));
2495cfb853baSMatthew G. Knepley   PetscCall(PetscFree4(edg, low, upp, bdt));
24963ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2497cfb853baSMatthew G. Knepley }
2498cfb853baSMatthew G. Knepley 
2499cc4c1da9SBarry Smith /*@
2500a1cb98faSBarry Smith   DMPlexSetOptionsPrefix - Sets the prefix used for searching for all `DM` options in the database.
2501a9074c1eSMatthew G. Knepley 
250220f4b53cSBarry Smith   Logically Collective
2503a9074c1eSMatthew G. Knepley 
2504a9074c1eSMatthew G. Knepley   Input Parameters:
250520f4b53cSBarry Smith + dm     - the `DM` context
2506a9074c1eSMatthew G. Knepley - prefix - the prefix to prepend to all option names
2507a9074c1eSMatthew G. Knepley 
2508a1cb98faSBarry Smith   Level: advanced
2509a1cb98faSBarry Smith 
2510a1cb98faSBarry Smith   Note:
2511a9074c1eSMatthew G. Knepley   A hyphen (-) must NOT be given at the beginning of the prefix name.
2512a9074c1eSMatthew G. Knepley   The first character of all runtime options is AUTOMATICALLY the hyphen.
2513a9074c1eSMatthew G. Knepley 
25141cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `SNESSetFromOptions()`
2515a9074c1eSMatthew G. Knepley @*/
2516d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetOptionsPrefix(DM dm, const char prefix[])
2517d71ae5a4SJacob Faibussowitsch {
2518a9074c1eSMatthew G. Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
2519a9074c1eSMatthew G. Knepley 
2520a9074c1eSMatthew G. Knepley   PetscFunctionBegin;
2521a9074c1eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
25229566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, prefix));
25239566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)mesh->partitioner, prefix));
25243ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
2525a9074c1eSMatthew G. Knepley }
2526a9074c1eSMatthew G. Knepley 
25279318fe57SMatthew G. Knepley /* Remap geometry to cylinder
252861a622f3SMatthew G. Knepley    TODO: This only works for a single refinement, then it is broken
252961a622f3SMatthew G. Knepley 
25309318fe57SMatthew G. Knepley      Interior square: Linear interpolation is correct
25319318fe57SMatthew G. Knepley      The other cells all have vertices on rays from the origin. We want to uniformly expand the spacing
25329318fe57SMatthew G. Knepley      such that the last vertex is on the unit circle. So the closest and farthest vertices are at distance
25330510c589SMatthew G. Knepley 
25349318fe57SMatthew G. Knepley        phi     = arctan(y/x)
25359318fe57SMatthew G. Knepley        d_close = sqrt(1/8 + 1/4 sin^2(phi))
25369318fe57SMatthew G. Knepley        d_far   = sqrt(1/2 + sin^2(phi))
25370510c589SMatthew G. Knepley 
25389318fe57SMatthew G. Knepley      so we remap them using
25390510c589SMatthew G. Knepley 
25409318fe57SMatthew G. Knepley        x_new = x_close + (x - x_close) (1 - d_close) / (d_far - d_close)
25419318fe57SMatthew G. Knepley        y_new = y_close + (y - y_close) (1 - d_close) / (d_far - d_close)
25420510c589SMatthew G. Knepley 
25439318fe57SMatthew G. Knepley      If pi/4 < phi < 3pi/4 or -3pi/4 < phi < -pi/4, then we switch x and y.
25449318fe57SMatthew G. Knepley */
2545d71ae5a4SJacob 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[])
2546d71ae5a4SJacob Faibussowitsch {
25479318fe57SMatthew G. Knepley   const PetscReal dis = 1.0 / PetscSqrtReal(2.0);
25489318fe57SMatthew G. Knepley   const PetscReal ds2 = 0.5 * dis;
254922cc497dSMatthew G. Knepley 
25509318fe57SMatthew G. Knepley   if ((PetscAbsScalar(u[0]) <= ds2) && (PetscAbsScalar(u[1]) <= ds2)) {
25519318fe57SMatthew G. Knepley     f0[0] = u[0];
25529318fe57SMatthew G. Knepley     f0[1] = u[1];
25539318fe57SMatthew G. Knepley   } else {
25549318fe57SMatthew G. Knepley     PetscReal phi, sinp, cosp, dc, df, x, y, xc, yc;
25550510c589SMatthew G. Knepley 
25569318fe57SMatthew G. Knepley     x    = PetscRealPart(u[0]);
25579318fe57SMatthew G. Knepley     y    = PetscRealPart(u[1]);
25589318fe57SMatthew G. Knepley     phi  = PetscAtan2Real(y, x);
25599318fe57SMatthew G. Knepley     sinp = PetscSinReal(phi);
25609318fe57SMatthew G. Knepley     cosp = PetscCosReal(phi);
25619318fe57SMatthew G. Knepley     if ((PetscAbsReal(phi) > PETSC_PI / 4.0) && (PetscAbsReal(phi) < 3.0 * PETSC_PI / 4.0)) {
25629318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2 / sinp);
25639318fe57SMatthew G. Knepley       df = PetscAbsReal(dis / sinp);
25649318fe57SMatthew G. Knepley       xc = ds2 * x / PetscAbsReal(y);
25659318fe57SMatthew G. Knepley       yc = ds2 * PetscSignReal(y);
25669318fe57SMatthew G. Knepley     } else {
25679318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2 / cosp);
25689318fe57SMatthew G. Knepley       df = PetscAbsReal(dis / cosp);
25699318fe57SMatthew G. Knepley       xc = ds2 * PetscSignReal(x);
25709318fe57SMatthew G. Knepley       yc = ds2 * y / PetscAbsReal(x);
25719318fe57SMatthew G. Knepley     }
25729318fe57SMatthew G. Knepley     f0[0] = xc + (u[0] - xc) * (1.0 - dc) / (df - dc);
25739318fe57SMatthew G. Knepley     f0[1] = yc + (u[1] - yc) * (1.0 - dc) / (df - dc);
25749318fe57SMatthew G. Knepley   }
25759318fe57SMatthew G. Knepley   f0[2] = u[2];
25769318fe57SMatthew G. Knepley }
25770510c589SMatthew G. Knepley 
257849704ca5SMatthew G. Knepley static PetscErrorCode DMPlexCreateHexCylinderMesh_Internal(DM dm, DMBoundaryType periodicZ, PetscInt Nr)
2579d71ae5a4SJacob Faibussowitsch {
25800510c589SMatthew G. Knepley   const PetscInt dim = 3;
25819318fe57SMatthew G. Knepley   PetscInt       numCells, numVertices;
2582d8c47e87SMatthew G. Knepley   PetscMPIInt    rank;
25830510c589SMatthew G. Knepley 
25840510c589SMatthew G. Knepley   PetscFunctionBegin;
258546139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
25869566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
25879566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
25880510c589SMatthew G. Knepley   /* Create topology */
25890510c589SMatthew G. Knepley   {
25900510c589SMatthew G. Knepley     PetscInt cone[8], c;
25910510c589SMatthew G. Knepley 
2592dd400576SPatrick Sanan     numCells    = rank == 0 ? 5 : 0;
2593dd400576SPatrick Sanan     numVertices = rank == 0 ? 16 : 0;
2594006a8963SMatthew G. Knepley     if (periodicZ == DM_BOUNDARY_PERIODIC) {
2595ae8bcbbbSMatthew G. Knepley       numCells *= 3;
2596dd400576SPatrick Sanan       numVertices = rank == 0 ? 24 : 0;
2597006a8963SMatthew G. Knepley     }
25989566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
25999566063dSJacob Faibussowitsch     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 8));
26009566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm));
2601dd400576SPatrick Sanan     if (rank == 0) {
2602006a8963SMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
26039371c9d4SSatish Balay         cone[0] = 15;
26049371c9d4SSatish Balay         cone[1] = 18;
26059371c9d4SSatish Balay         cone[2] = 17;
26069371c9d4SSatish Balay         cone[3] = 16;
26079371c9d4SSatish Balay         cone[4] = 31;
26089371c9d4SSatish Balay         cone[5] = 32;
26099371c9d4SSatish Balay         cone[6] = 33;
26109371c9d4SSatish Balay         cone[7] = 34;
26119566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
26129371c9d4SSatish Balay         cone[0] = 16;
26139371c9d4SSatish Balay         cone[1] = 17;
26149371c9d4SSatish Balay         cone[2] = 24;
26159371c9d4SSatish Balay         cone[3] = 23;
26169371c9d4SSatish Balay         cone[4] = 32;
26179371c9d4SSatish Balay         cone[5] = 36;
26189371c9d4SSatish Balay         cone[6] = 37;
26199371c9d4SSatish Balay         cone[7] = 33; /* 22 25 26 21 */
26209566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
26219371c9d4SSatish Balay         cone[0] = 18;
26229371c9d4SSatish Balay         cone[1] = 27;
26239371c9d4SSatish Balay         cone[2] = 24;
26249371c9d4SSatish Balay         cone[3] = 17;
26259371c9d4SSatish Balay         cone[4] = 34;
26269371c9d4SSatish Balay         cone[5] = 33;
26279371c9d4SSatish Balay         cone[6] = 37;
26289371c9d4SSatish Balay         cone[7] = 38;
26299566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
26309371c9d4SSatish Balay         cone[0] = 29;
26319371c9d4SSatish Balay         cone[1] = 27;
26329371c9d4SSatish Balay         cone[2] = 18;
26339371c9d4SSatish Balay         cone[3] = 15;
26349371c9d4SSatish Balay         cone[4] = 35;
26359371c9d4SSatish Balay         cone[5] = 31;
26369371c9d4SSatish Balay         cone[6] = 34;
26379371c9d4SSatish Balay         cone[7] = 38;
26389566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
26399371c9d4SSatish Balay         cone[0] = 29;
26409371c9d4SSatish Balay         cone[1] = 15;
26419371c9d4SSatish Balay         cone[2] = 16;
26429371c9d4SSatish Balay         cone[3] = 23;
26439371c9d4SSatish Balay         cone[4] = 35;
26449371c9d4SSatish Balay         cone[5] = 36;
26459371c9d4SSatish Balay         cone[6] = 32;
26469371c9d4SSatish Balay         cone[7] = 31;
26479566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
2648006a8963SMatthew G. Knepley 
26499371c9d4SSatish Balay         cone[0] = 31;
26509371c9d4SSatish Balay         cone[1] = 34;
26519371c9d4SSatish Balay         cone[2] = 33;
26529371c9d4SSatish Balay         cone[3] = 32;
26539371c9d4SSatish Balay         cone[4] = 19;
26549371c9d4SSatish Balay         cone[5] = 22;
26559371c9d4SSatish Balay         cone[6] = 21;
26569371c9d4SSatish Balay         cone[7] = 20;
26579566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 5, cone));
26589371c9d4SSatish Balay         cone[0] = 32;
26599371c9d4SSatish Balay         cone[1] = 33;
26609371c9d4SSatish Balay         cone[2] = 37;
26619371c9d4SSatish Balay         cone[3] = 36;
26629371c9d4SSatish Balay         cone[4] = 22;
26639371c9d4SSatish Balay         cone[5] = 25;
26649371c9d4SSatish Balay         cone[6] = 26;
26659371c9d4SSatish Balay         cone[7] = 21;
26669566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 6, cone));
26679371c9d4SSatish Balay         cone[0] = 34;
26689371c9d4SSatish Balay         cone[1] = 38;
26699371c9d4SSatish Balay         cone[2] = 37;
26709371c9d4SSatish Balay         cone[3] = 33;
26719371c9d4SSatish Balay         cone[4] = 20;
26729371c9d4SSatish Balay         cone[5] = 21;
26739371c9d4SSatish Balay         cone[6] = 26;
26749371c9d4SSatish Balay         cone[7] = 28;
26759566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 7, cone));
26769371c9d4SSatish Balay         cone[0] = 35;
26779371c9d4SSatish Balay         cone[1] = 38;
26789371c9d4SSatish Balay         cone[2] = 34;
26799371c9d4SSatish Balay         cone[3] = 31;
26809371c9d4SSatish Balay         cone[4] = 30;
26819371c9d4SSatish Balay         cone[5] = 19;
26829371c9d4SSatish Balay         cone[6] = 20;
26839371c9d4SSatish Balay         cone[7] = 28;
26849566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 8, cone));
26859371c9d4SSatish Balay         cone[0] = 35;
26869371c9d4SSatish Balay         cone[1] = 31;
26879371c9d4SSatish Balay         cone[2] = 32;
26889371c9d4SSatish Balay         cone[3] = 36;
26899371c9d4SSatish Balay         cone[4] = 30;
26909371c9d4SSatish Balay         cone[5] = 25;
26919371c9d4SSatish Balay         cone[6] = 22;
26929371c9d4SSatish Balay         cone[7] = 19;
26939566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 9, cone));
2694ae8bcbbbSMatthew G. Knepley 
26959371c9d4SSatish Balay         cone[0] = 19;
26969371c9d4SSatish Balay         cone[1] = 20;
26979371c9d4SSatish Balay         cone[2] = 21;
26989371c9d4SSatish Balay         cone[3] = 22;
26999371c9d4SSatish Balay         cone[4] = 15;
27009371c9d4SSatish Balay         cone[5] = 16;
27019371c9d4SSatish Balay         cone[6] = 17;
27029371c9d4SSatish Balay         cone[7] = 18;
27039566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 10, cone));
27049371c9d4SSatish Balay         cone[0] = 22;
27059371c9d4SSatish Balay         cone[1] = 21;
27069371c9d4SSatish Balay         cone[2] = 26;
27079371c9d4SSatish Balay         cone[3] = 25;
27089371c9d4SSatish Balay         cone[4] = 16;
27099371c9d4SSatish Balay         cone[5] = 23;
27109371c9d4SSatish Balay         cone[6] = 24;
27119371c9d4SSatish Balay         cone[7] = 17;
27129566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 11, cone));
27139371c9d4SSatish Balay         cone[0] = 20;
27149371c9d4SSatish Balay         cone[1] = 28;
27159371c9d4SSatish Balay         cone[2] = 26;
27169371c9d4SSatish Balay         cone[3] = 21;
27179371c9d4SSatish Balay         cone[4] = 18;
27189371c9d4SSatish Balay         cone[5] = 17;
27199371c9d4SSatish Balay         cone[6] = 24;
27209371c9d4SSatish Balay         cone[7] = 27;
27219566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 12, cone));
27229371c9d4SSatish Balay         cone[0] = 30;
27239371c9d4SSatish Balay         cone[1] = 28;
27249371c9d4SSatish Balay         cone[2] = 20;
27259371c9d4SSatish Balay         cone[3] = 19;
27269371c9d4SSatish Balay         cone[4] = 29;
27279371c9d4SSatish Balay         cone[5] = 15;
27289371c9d4SSatish Balay         cone[6] = 18;
27299371c9d4SSatish Balay         cone[7] = 27;
27309566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 13, cone));
27319371c9d4SSatish Balay         cone[0] = 30;
27329371c9d4SSatish Balay         cone[1] = 19;
27339371c9d4SSatish Balay         cone[2] = 22;
27349371c9d4SSatish Balay         cone[3] = 25;
27359371c9d4SSatish Balay         cone[4] = 29;
27369371c9d4SSatish Balay         cone[5] = 23;
27379371c9d4SSatish Balay         cone[6] = 16;
27389371c9d4SSatish Balay         cone[7] = 15;
27399566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 14, cone));
2740006a8963SMatthew G. Knepley       } else {
27419371c9d4SSatish Balay         cone[0] = 5;
27429371c9d4SSatish Balay         cone[1] = 8;
27439371c9d4SSatish Balay         cone[2] = 7;
27449371c9d4SSatish Balay         cone[3] = 6;
27459371c9d4SSatish Balay         cone[4] = 9;
27469371c9d4SSatish Balay         cone[5] = 12;
27479371c9d4SSatish Balay         cone[6] = 11;
27489371c9d4SSatish Balay         cone[7] = 10;
27499566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
27509371c9d4SSatish Balay         cone[0] = 6;
27519371c9d4SSatish Balay         cone[1] = 7;
27529371c9d4SSatish Balay         cone[2] = 14;
27539371c9d4SSatish Balay         cone[3] = 13;
27549371c9d4SSatish Balay         cone[4] = 12;
27559371c9d4SSatish Balay         cone[5] = 15;
27569371c9d4SSatish Balay         cone[6] = 16;
27579371c9d4SSatish Balay         cone[7] = 11;
27589566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
27599371c9d4SSatish Balay         cone[0] = 8;
27609371c9d4SSatish Balay         cone[1] = 17;
27619371c9d4SSatish Balay         cone[2] = 14;
27629371c9d4SSatish Balay         cone[3] = 7;
27639371c9d4SSatish Balay         cone[4] = 10;
27649371c9d4SSatish Balay         cone[5] = 11;
27659371c9d4SSatish Balay         cone[6] = 16;
27669371c9d4SSatish Balay         cone[7] = 18;
27679566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
27689371c9d4SSatish Balay         cone[0] = 19;
27699371c9d4SSatish Balay         cone[1] = 17;
27709371c9d4SSatish Balay         cone[2] = 8;
27719371c9d4SSatish Balay         cone[3] = 5;
27729371c9d4SSatish Balay         cone[4] = 20;
27739371c9d4SSatish Balay         cone[5] = 9;
27749371c9d4SSatish Balay         cone[6] = 10;
27759371c9d4SSatish Balay         cone[7] = 18;
27769566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
27779371c9d4SSatish Balay         cone[0] = 19;
27789371c9d4SSatish Balay         cone[1] = 5;
27799371c9d4SSatish Balay         cone[2] = 6;
27809371c9d4SSatish Balay         cone[3] = 13;
27819371c9d4SSatish Balay         cone[4] = 20;
27829371c9d4SSatish Balay         cone[5] = 15;
27839371c9d4SSatish Balay         cone[6] = 12;
27849371c9d4SSatish Balay         cone[7] = 9;
27859566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
2786006a8963SMatthew G. Knepley       }
2787d8c47e87SMatthew G. Knepley     }
27889566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
27899566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
27900510c589SMatthew G. Knepley   }
2791dbc1dc17SMatthew G. Knepley   /* Create cube geometry */
27920510c589SMatthew G. Knepley   {
27930510c589SMatthew G. Knepley     Vec             coordinates;
27940510c589SMatthew G. Knepley     PetscSection    coordSection;
27950510c589SMatthew G. Knepley     PetscScalar    *coords;
27960510c589SMatthew G. Knepley     PetscInt        coordSize, v;
27970510c589SMatthew G. Knepley     const PetscReal dis = 1.0 / PetscSqrtReal(2.0);
27980510c589SMatthew G. Knepley     const PetscReal ds2 = dis / 2.0;
27990510c589SMatthew G. Knepley 
28000510c589SMatthew G. Knepley     /* Build coordinates */
28019566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
28029566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
28039566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
28049566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices));
28050510c589SMatthew G. Knepley     for (v = numCells; v < numCells + numVertices; ++v) {
28069566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
28079566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
28080510c589SMatthew G. Knepley     }
28099566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
28109566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
28119566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
28129566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
28139566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
28149566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
28159566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates, VECSTANDARD));
28169566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
2817dd400576SPatrick Sanan     if (rank == 0) {
28189371c9d4SSatish Balay       coords[0 * dim + 0]  = -ds2;
28199371c9d4SSatish Balay       coords[0 * dim + 1]  = -ds2;
28209371c9d4SSatish Balay       coords[0 * dim + 2]  = 0.0;
28219371c9d4SSatish Balay       coords[1 * dim + 0]  = ds2;
28229371c9d4SSatish Balay       coords[1 * dim + 1]  = -ds2;
28239371c9d4SSatish Balay       coords[1 * dim + 2]  = 0.0;
28249371c9d4SSatish Balay       coords[2 * dim + 0]  = ds2;
28259371c9d4SSatish Balay       coords[2 * dim + 1]  = ds2;
28269371c9d4SSatish Balay       coords[2 * dim + 2]  = 0.0;
28279371c9d4SSatish Balay       coords[3 * dim + 0]  = -ds2;
28289371c9d4SSatish Balay       coords[3 * dim + 1]  = ds2;
28299371c9d4SSatish Balay       coords[3 * dim + 2]  = 0.0;
28309371c9d4SSatish Balay       coords[4 * dim + 0]  = -ds2;
28319371c9d4SSatish Balay       coords[4 * dim + 1]  = -ds2;
28329371c9d4SSatish Balay       coords[4 * dim + 2]  = 1.0;
28339371c9d4SSatish Balay       coords[5 * dim + 0]  = -ds2;
28349371c9d4SSatish Balay       coords[5 * dim + 1]  = ds2;
28359371c9d4SSatish Balay       coords[5 * dim + 2]  = 1.0;
28369371c9d4SSatish Balay       coords[6 * dim + 0]  = ds2;
28379371c9d4SSatish Balay       coords[6 * dim + 1]  = ds2;
28389371c9d4SSatish Balay       coords[6 * dim + 2]  = 1.0;
28399371c9d4SSatish Balay       coords[7 * dim + 0]  = ds2;
28409371c9d4SSatish Balay       coords[7 * dim + 1]  = -ds2;
28419371c9d4SSatish Balay       coords[7 * dim + 2]  = 1.0;
28429371c9d4SSatish Balay       coords[8 * dim + 0]  = dis;
28439371c9d4SSatish Balay       coords[8 * dim + 1]  = -dis;
28449371c9d4SSatish Balay       coords[8 * dim + 2]  = 0.0;
28459371c9d4SSatish Balay       coords[9 * dim + 0]  = dis;
28469371c9d4SSatish Balay       coords[9 * dim + 1]  = dis;
28479371c9d4SSatish Balay       coords[9 * dim + 2]  = 0.0;
28489371c9d4SSatish Balay       coords[10 * dim + 0] = dis;
28499371c9d4SSatish Balay       coords[10 * dim + 1] = -dis;
28509371c9d4SSatish Balay       coords[10 * dim + 2] = 1.0;
28519371c9d4SSatish Balay       coords[11 * dim + 0] = dis;
28529371c9d4SSatish Balay       coords[11 * dim + 1] = dis;
28539371c9d4SSatish Balay       coords[11 * dim + 2] = 1.0;
28549371c9d4SSatish Balay       coords[12 * dim + 0] = -dis;
28559371c9d4SSatish Balay       coords[12 * dim + 1] = dis;
28569371c9d4SSatish Balay       coords[12 * dim + 2] = 0.0;
28579371c9d4SSatish Balay       coords[13 * dim + 0] = -dis;
28589371c9d4SSatish Balay       coords[13 * dim + 1] = dis;
28599371c9d4SSatish Balay       coords[13 * dim + 2] = 1.0;
28609371c9d4SSatish Balay       coords[14 * dim + 0] = -dis;
28619371c9d4SSatish Balay       coords[14 * dim + 1] = -dis;
28629371c9d4SSatish Balay       coords[14 * dim + 2] = 0.0;
28639371c9d4SSatish Balay       coords[15 * dim + 0] = -dis;
28649371c9d4SSatish Balay       coords[15 * dim + 1] = -dis;
28659371c9d4SSatish Balay       coords[15 * dim + 2] = 1.0;
2866ae8bcbbbSMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
28679371c9d4SSatish Balay         /* 15 31 19 */ coords[16 * dim + 0] = -ds2;
28689371c9d4SSatish Balay         coords[16 * dim + 1]                = -ds2;
28699371c9d4SSatish Balay         coords[16 * dim + 2]                = 0.5;
28709371c9d4SSatish Balay         /* 16 32 22 */ coords[17 * dim + 0] = ds2;
28719371c9d4SSatish Balay         coords[17 * dim + 1]                = -ds2;
28729371c9d4SSatish Balay         coords[17 * dim + 2]                = 0.5;
28739371c9d4SSatish Balay         /* 17 33 21 */ coords[18 * dim + 0] = ds2;
28749371c9d4SSatish Balay         coords[18 * dim + 1]                = ds2;
28759371c9d4SSatish Balay         coords[18 * dim + 2]                = 0.5;
28769371c9d4SSatish Balay         /* 18 34 20 */ coords[19 * dim + 0] = -ds2;
28779371c9d4SSatish Balay         coords[19 * dim + 1]                = ds2;
28789371c9d4SSatish Balay         coords[19 * dim + 2]                = 0.5;
28799371c9d4SSatish Balay         /* 29 35 30 */ coords[20 * dim + 0] = -dis;
28809371c9d4SSatish Balay         coords[20 * dim + 1]                = -dis;
28819371c9d4SSatish Balay         coords[20 * dim + 2]                = 0.5;
28829371c9d4SSatish Balay         /* 23 36 25 */ coords[21 * dim + 0] = dis;
28839371c9d4SSatish Balay         coords[21 * dim + 1]                = -dis;
28849371c9d4SSatish Balay         coords[21 * dim + 2]                = 0.5;
28859371c9d4SSatish Balay         /* 24 37 26 */ coords[22 * dim + 0] = dis;
28869371c9d4SSatish Balay         coords[22 * dim + 1]                = dis;
28879371c9d4SSatish Balay         coords[22 * dim + 2]                = 0.5;
28889371c9d4SSatish Balay         /* 27 38 28 */ coords[23 * dim + 0] = -dis;
28899371c9d4SSatish Balay         coords[23 * dim + 1]                = dis;
28909371c9d4SSatish Balay         coords[23 * dim + 2]                = 0.5;
2891ae8bcbbbSMatthew G. Knepley       }
2892d8c47e87SMatthew G. Knepley     }
28939566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
28949566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
28959566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
28960510c589SMatthew G. Knepley   }
2897006a8963SMatthew G. Knepley   /* Create periodicity */
2898006a8963SMatthew G. Knepley   if (periodicZ == DM_BOUNDARY_PERIODIC || periodicZ == DM_BOUNDARY_TWIST) {
28996858538eSMatthew G. Knepley     PetscReal L[3]       = {-1., -1., 0.};
29006858538eSMatthew G. Knepley     PetscReal maxCell[3] = {-1., -1., 0.};
2901006a8963SMatthew G. Knepley     PetscReal lower[3]   = {0.0, 0.0, 0.0};
2902ae8bcbbbSMatthew G. Knepley     PetscReal upper[3]   = {1.0, 1.0, 1.5};
29036858538eSMatthew G. Knepley     PetscInt  numZCells  = 3;
2904006a8963SMatthew G. Knepley 
29056858538eSMatthew G. Knepley     L[2]       = upper[2] - lower[2];
29066858538eSMatthew G. Knepley     maxCell[2] = 1.1 * (L[2] / numZCells);
29074fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, maxCell, lower, L));
2908006a8963SMatthew G. Knepley   }
2909dbc1dc17SMatthew G. Knepley   {
29109318fe57SMatthew G. Knepley     DM          cdm;
29119318fe57SMatthew G. Knepley     PetscDS     cds;
29129318fe57SMatthew G. Knepley     PetscScalar c[2] = {1.0, 1.0};
2913dbc1dc17SMatthew G. Knepley 
291449704ca5SMatthew G. Knepley     PetscCall(DMPlexCreateCoordinateSpace(dm, 1, PETSC_TRUE, NULL));
29159566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
29169566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
29179566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, 2, c));
2918dbc1dc17SMatthew G. Knepley   }
291946139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
292046139095SJed Brown 
29219318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
29229566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolateInPlace_Internal(dm));
292349704ca5SMatthew G. Knepley 
292449704ca5SMatthew G. Knepley   char        oldprefix[PETSC_MAX_PATH_LEN];
292549704ca5SMatthew G. Knepley   const char *prefix;
292649704ca5SMatthew G. Knepley 
292749704ca5SMatthew G. Knepley   PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, &prefix));
292849704ca5SMatthew G. Knepley   PetscCall(PetscStrncpy(oldprefix, prefix, PETSC_MAX_PATH_LEN));
292949704ca5SMatthew G. Knepley   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, "petsc_cyl_ref_"));
293049704ca5SMatthew G. Knepley   for (PetscInt r = 0; r < PetscMax(0, Nr); ++r) {
293149704ca5SMatthew G. Knepley     DM rdm;
293249704ca5SMatthew G. Knepley 
293349704ca5SMatthew G. Knepley     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &rdm));
293449704ca5SMatthew G. Knepley     PetscCall(DMPlexReplace_Internal(dm, &rdm));
293549704ca5SMatthew G. Knepley   }
293649704ca5SMatthew G. Knepley   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, oldprefix));
293749704ca5SMatthew G. Knepley   PetscCall(DMPlexRemapGeometry(dm, 0.0, snapToCylinder));
293849704ca5SMatthew G. Knepley 
293949704ca5SMatthew G. Knepley   DMLabel         bdlabel, edgelabel;
294049704ca5SMatthew G. Knepley   IS              faceIS;
294149704ca5SMatthew G. Knepley   const PetscInt *faces;
294249704ca5SMatthew G. Knepley   PetscInt        Nf;
294349704ca5SMatthew G. Knepley 
294449704ca5SMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "marker"));
294549704ca5SMatthew G. Knepley   PetscCall(DMGetLabel(dm, "marker", &bdlabel));
294649704ca5SMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "generatrix"));
294749704ca5SMatthew G. Knepley   PetscCall(DMGetLabel(dm, "generatrix", &edgelabel));
294849704ca5SMatthew G. Knepley   PetscCall(DMPlexMarkBoundaryFaces(dm, PETSC_DETERMINE, bdlabel));
294949704ca5SMatthew G. Knepley   // Remove faces on top and bottom
295049704ca5SMatthew G. Knepley   PetscCall(DMLabelGetStratumIS(bdlabel, 1, &faceIS));
2951ba1b3593SJeremy L Thompson   if (faceIS) {
295249704ca5SMatthew G. Knepley     PetscCall(ISGetLocalSize(faceIS, &Nf));
295349704ca5SMatthew G. Knepley     PetscCall(ISGetIndices(faceIS, &faces));
295449704ca5SMatthew G. Knepley     for (PetscInt f = 0; f < Nf; ++f) {
295549704ca5SMatthew G. Knepley       PetscReal vol, normal[3];
295649704ca5SMatthew G. Knepley 
295749704ca5SMatthew G. Knepley       PetscCall(DMPlexComputeCellGeometryFVM(dm, faces[f], &vol, NULL, normal));
295849704ca5SMatthew G. Knepley       if (PetscAbsReal(normal[2]) < PETSC_SMALL) PetscCall(DMLabelSetValue(edgelabel, faces[f], 1));
295949704ca5SMatthew G. Knepley     }
296049704ca5SMatthew G. Knepley     PetscCall(ISRestoreIndices(faceIS, &faces));
296149704ca5SMatthew G. Knepley     PetscCall(ISDestroy(&faceIS));
2962ba1b3593SJeremy L Thompson   }
296349704ca5SMatthew G. Knepley   PetscCall(DMPlexLabelComplete(dm, bdlabel));
296449704ca5SMatthew G. Knepley   PetscCall(DMPlexLabelComplete(dm, edgelabel));
29653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
29660510c589SMatthew G. Knepley }
29670510c589SMatthew G. Knepley 
296824119c2aSMatthew G. Knepley /*@
29699318fe57SMatthew G. Knepley   DMPlexCreateHexCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using hexahedra.
297024119c2aSMatthew G. Knepley 
2971d083f849SBarry Smith   Collective
297224119c2aSMatthew G. Knepley 
297324119c2aSMatthew G. Knepley   Input Parameters:
2974a1cb98faSBarry Smith + comm      - The communicator for the `DM` object
297549704ca5SMatthew G. Knepley . periodicZ - The boundary type for the Z direction
297649704ca5SMatthew G. Knepley - Nr        - The number of refinements to carry out
297724119c2aSMatthew G. Knepley 
297824119c2aSMatthew G. Knepley   Output Parameter:
297920f4b53cSBarry Smith . dm - The `DM` object
298024119c2aSMatthew G. Knepley 
298124119c2aSMatthew G. Knepley   Level: beginner
298224119c2aSMatthew G. Knepley 
2983a1cb98faSBarry Smith   Note:
2984a4e35b19SJacob Faibussowitsch   Here is the output numbering looking from the bottom of the cylinder\:
2985a1cb98faSBarry Smith .vb
2986a1cb98faSBarry Smith        17-----14
2987a1cb98faSBarry Smith         |     |
2988a1cb98faSBarry Smith         |  2  |
2989a1cb98faSBarry Smith         |     |
2990a1cb98faSBarry Smith  17-----8-----7-----14
2991a1cb98faSBarry Smith   |     |     |     |
2992a1cb98faSBarry Smith   |  3  |  0  |  1  |
2993a1cb98faSBarry Smith   |     |     |     |
2994a1cb98faSBarry Smith  19-----5-----6-----13
2995a1cb98faSBarry Smith         |     |
2996a1cb98faSBarry Smith         |  4  |
2997a1cb98faSBarry Smith         |     |
2998a1cb98faSBarry Smith        19-----13
2999a1cb98faSBarry Smith 
3000a1cb98faSBarry Smith  and up through the top
3001a1cb98faSBarry Smith 
3002a1cb98faSBarry Smith        18-----16
3003a1cb98faSBarry Smith         |     |
3004a1cb98faSBarry Smith         |  2  |
3005a1cb98faSBarry Smith         |     |
3006a1cb98faSBarry Smith  18----10----11-----16
3007a1cb98faSBarry Smith   |     |     |     |
3008a1cb98faSBarry Smith   |  3  |  0  |  1  |
3009a1cb98faSBarry Smith   |     |     |     |
3010a1cb98faSBarry Smith  20-----9----12-----15
3011a1cb98faSBarry Smith         |     |
3012a1cb98faSBarry Smith         |  4  |
3013a1cb98faSBarry Smith         |     |
3014a1cb98faSBarry Smith        20-----15
3015a1cb98faSBarry Smith .ve
3016a1cb98faSBarry Smith 
30171cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
301824119c2aSMatthew G. Knepley @*/
301949704ca5SMatthew G. Knepley PetscErrorCode DMPlexCreateHexCylinderMesh(MPI_Comm comm, DMBoundaryType periodicZ, PetscInt Nr, DM *dm)
3020d71ae5a4SJacob Faibussowitsch {
30219318fe57SMatthew G. Knepley   PetscFunctionBegin;
302249704ca5SMatthew G. Knepley   PetscAssertPointer(dm, 4);
30239566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
30249566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
302549704ca5SMatthew G. Knepley   PetscCall(DMPlexCreateHexCylinderMesh_Internal(*dm, periodicZ, Nr));
30263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
30279318fe57SMatthew G. Knepley }
30289318fe57SMatthew G. Knepley 
3029d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateWedgeCylinderMesh_Internal(DM dm, PetscInt n, PetscBool interpolate)
3030d71ae5a4SJacob Faibussowitsch {
303124119c2aSMatthew G. Knepley   const PetscInt dim = 3;
3032412e9a14SMatthew G. Knepley   PetscInt       numCells, numVertices, v;
30339fe9f049SMatthew G. Knepley   PetscMPIInt    rank;
303424119c2aSMatthew G. Knepley 
303524119c2aSMatthew G. Knepley   PetscFunctionBegin;
303663a3b9bcSJacob Faibussowitsch   PetscCheck(n >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of wedges %" PetscInt_FMT " cannot be negative", n);
303746139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
30389566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
30399566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
3040412e9a14SMatthew G. Knepley   /* Must create the celltype label here so that we do not automatically try to compute the types */
30419566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "celltype"));
304224119c2aSMatthew G. Knepley   /* Create topology */
304324119c2aSMatthew G. Knepley   {
304424119c2aSMatthew G. Knepley     PetscInt cone[6], c;
304524119c2aSMatthew G. Knepley 
3046dd400576SPatrick Sanan     numCells    = rank == 0 ? n : 0;
3047dd400576SPatrick Sanan     numVertices = rank == 0 ? 2 * (n + 1) : 0;
30489566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
30499566063dSJacob Faibussowitsch     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 6));
30509566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm));
305124119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
30529371c9d4SSatish Balay       cone[0] = c + n * 1;
30539371c9d4SSatish Balay       cone[1] = (c + 1) % n + n * 1;
30549371c9d4SSatish Balay       cone[2] = 0 + 3 * n;
30559371c9d4SSatish Balay       cone[3] = c + n * 2;
30569371c9d4SSatish Balay       cone[4] = (c + 1) % n + n * 2;
30579371c9d4SSatish Balay       cone[5] = 1 + 3 * n;
30589566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(dm, c, cone));
30599566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(dm, c, DM_POLYTOPE_TRI_PRISM_TENSOR));
306024119c2aSMatthew G. Knepley     }
30619566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
30629566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
306324119c2aSMatthew G. Knepley   }
306448a46eb9SPierre Jolivet   for (v = numCells; v < numCells + numVertices; ++v) PetscCall(DMPlexSetCellType(dm, v, DM_POLYTOPE_POINT));
306524119c2aSMatthew G. Knepley   /* Create cylinder geometry */
306624119c2aSMatthew G. Knepley   {
306724119c2aSMatthew G. Knepley     Vec          coordinates;
306824119c2aSMatthew G. Knepley     PetscSection coordSection;
306924119c2aSMatthew G. Knepley     PetscScalar *coords;
3070412e9a14SMatthew G. Knepley     PetscInt     coordSize, c;
307124119c2aSMatthew G. Knepley 
307224119c2aSMatthew G. Knepley     /* Build coordinates */
30739566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
30749566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
30759566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
30769566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices));
307724119c2aSMatthew G. Knepley     for (v = numCells; v < numCells + numVertices; ++v) {
30789566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
30799566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
308024119c2aSMatthew G. Knepley     }
30819566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
30829566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
30839566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
30849566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
30859566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
30869566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
30879566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates, VECSTANDARD));
30889566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
308924119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
30909371c9d4SSatish Balay       coords[(c + 0 * n) * dim + 0] = PetscCosReal(2.0 * c * PETSC_PI / n);
30919371c9d4SSatish Balay       coords[(c + 0 * n) * dim + 1] = PetscSinReal(2.0 * c * PETSC_PI / n);
30929371c9d4SSatish Balay       coords[(c + 0 * n) * dim + 2] = 1.0;
30939371c9d4SSatish Balay       coords[(c + 1 * n) * dim + 0] = PetscCosReal(2.0 * c * PETSC_PI / n);
30949371c9d4SSatish Balay       coords[(c + 1 * n) * dim + 1] = PetscSinReal(2.0 * c * PETSC_PI / n);
30959371c9d4SSatish Balay       coords[(c + 1 * n) * dim + 2] = 0.0;
309624119c2aSMatthew G. Knepley     }
3097dd400576SPatrick Sanan     if (rank == 0) {
30989371c9d4SSatish Balay       coords[(2 * n + 0) * dim + 0] = 0.0;
30999371c9d4SSatish Balay       coords[(2 * n + 0) * dim + 1] = 0.0;
31009371c9d4SSatish Balay       coords[(2 * n + 0) * dim + 2] = 1.0;
31019371c9d4SSatish Balay       coords[(2 * n + 1) * dim + 0] = 0.0;
31029371c9d4SSatish Balay       coords[(2 * n + 1) * dim + 1] = 0.0;
31039371c9d4SSatish Balay       coords[(2 * n + 1) * dim + 2] = 0.0;
31049fe9f049SMatthew G. Knepley     }
31059566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
31069566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
31079566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
310824119c2aSMatthew G. Knepley   }
310946139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
31109318fe57SMatthew G. Knepley   /* Interpolate */
31119566063dSJacob Faibussowitsch   if (interpolate) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
31123ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
31139318fe57SMatthew G. Knepley }
31149318fe57SMatthew G. Knepley 
31159318fe57SMatthew G. Knepley /*@
31169318fe57SMatthew G. Knepley   DMPlexCreateWedgeCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using wedges.
31179318fe57SMatthew G. Knepley 
31189318fe57SMatthew G. Knepley   Collective
31199318fe57SMatthew G. Knepley 
31209318fe57SMatthew G. Knepley   Input Parameters:
3121a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
31229318fe57SMatthew G. Knepley . n           - The number of wedges around the origin
31239318fe57SMatthew G. Knepley - interpolate - Create edges and faces
31249318fe57SMatthew G. Knepley 
31259318fe57SMatthew G. Knepley   Output Parameter:
3126a1cb98faSBarry Smith . dm - The `DM` object
31279318fe57SMatthew G. Knepley 
31289318fe57SMatthew G. Knepley   Level: beginner
31299318fe57SMatthew G. Knepley 
31301cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateHexCylinderMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
31319318fe57SMatthew G. Knepley @*/
3132d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateWedgeCylinderMesh(MPI_Comm comm, PetscInt n, PetscBool interpolate, DM *dm)
3133d71ae5a4SJacob Faibussowitsch {
31349318fe57SMatthew G. Knepley   PetscFunctionBegin;
31354f572ea9SToby Isaac   PetscAssertPointer(dm, 4);
31369566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
31379566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
31389566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateWedgeCylinderMesh_Internal(*dm, n, interpolate));
31393ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
314024119c2aSMatthew G. Knepley }
314124119c2aSMatthew G. Knepley 
3142d71ae5a4SJacob Faibussowitsch static inline PetscReal DiffNormReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
3143d71ae5a4SJacob Faibussowitsch {
314465a81367SMatthew G. Knepley   PetscReal prod = 0.0;
314565a81367SMatthew G. Knepley   PetscInt  i;
314665a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += PetscSqr(x[i] - y[i]);
314765a81367SMatthew G. Knepley   return PetscSqrtReal(prod);
314865a81367SMatthew G. Knepley }
3149dd2b43ebSStefano Zampini 
3150d71ae5a4SJacob Faibussowitsch static inline PetscReal DotReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
3151d71ae5a4SJacob Faibussowitsch {
315265a81367SMatthew G. Knepley   PetscReal prod = 0.0;
315365a81367SMatthew G. Knepley   PetscInt  i;
315465a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += x[i] * y[i];
315565a81367SMatthew G. Knepley   return prod;
315665a81367SMatthew G. Knepley }
315765a81367SMatthew G. Knepley 
315851a74b61SMatthew G. Knepley /* The first constant is the sphere radius */
3159d71ae5a4SJacob 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[])
3160d71ae5a4SJacob Faibussowitsch {
316151a74b61SMatthew G. Knepley   PetscReal r     = PetscRealPart(constants[0]);
316251a74b61SMatthew G. Knepley   PetscReal norm2 = 0.0, fac;
316351a74b61SMatthew G. Knepley   PetscInt  n     = uOff[1] - uOff[0], d;
316451a74b61SMatthew G. Knepley 
316551a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) norm2 += PetscSqr(PetscRealPart(u[d]));
316651a74b61SMatthew G. Knepley   fac = r / PetscSqrtReal(norm2);
316751a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) f0[d] = u[d] * fac;
316851a74b61SMatthew G. Knepley }
316951a74b61SMatthew G. Knepley 
3170d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSphereMesh_Internal(DM dm, PetscInt dim, PetscBool simplex, PetscReal R)
3171d71ae5a4SJacob Faibussowitsch {
317265a81367SMatthew G. Knepley   const PetscInt embedDim = dim + 1;
317365a81367SMatthew G. Knepley   PetscSection   coordSection;
317465a81367SMatthew G. Knepley   Vec            coordinates;
317565a81367SMatthew G. Knepley   PetscScalar   *coords;
317665a81367SMatthew G. Knepley   PetscReal     *coordsIn;
317707c565c5SJose E. Roman   PetscInt       numCells, numEdges, numVerts = 0, firstVertex = 0, v, firstEdge, coordSize, d, e;
317865a81367SMatthew G. Knepley   PetscMPIInt    rank;
317965a81367SMatthew G. Knepley 
318065a81367SMatthew G. Knepley   PetscFunctionBegin;
31819318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveBool(dm, simplex, 3);
318246139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
31839566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
31849566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, dim + 1));
31859566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
318665a81367SMatthew G. Knepley   switch (dim) {
31875c344501SMatthew G. Knepley   case 1:
31885c344501SMatthew G. Knepley     numCells = 16;
31895c344501SMatthew G. Knepley     numVerts = numCells;
31905c344501SMatthew G. Knepley 
31915c344501SMatthew G. Knepley     // Build Topology
31925c344501SMatthew G. Knepley     PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts));
31935c344501SMatthew G. Knepley     for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim));
31945c344501SMatthew G. Knepley     PetscCall(DMSetUp(dm));
31955c344501SMatthew G. Knepley     for (PetscInt c = 0; c < numCells; ++c) {
31965c344501SMatthew G. Knepley       PetscInt cone[2];
31975c344501SMatthew G. Knepley 
31985c344501SMatthew G. Knepley       cone[0] = c + numCells;
31995c344501SMatthew G. Knepley       cone[1] = (c + 1) % numVerts + numCells;
32005c344501SMatthew G. Knepley       PetscCall(DMPlexSetCone(dm, c, cone));
32015c344501SMatthew G. Knepley     }
32025c344501SMatthew G. Knepley     PetscCall(DMPlexSymmetrize(dm));
32035c344501SMatthew G. Knepley     PetscCall(DMPlexStratify(dm));
32045c344501SMatthew G. Knepley     PetscCall(PetscMalloc1(numVerts * embedDim, &coordsIn));
32055c344501SMatthew G. Knepley     for (PetscInt v = 0; v < numVerts; ++v) {
32065c344501SMatthew G. Knepley       const PetscReal rad = 2. * PETSC_PI * v / numVerts;
32075c344501SMatthew G. Knepley 
32085c344501SMatthew G. Knepley       coordsIn[v * embedDim + 0] = PetscCosReal(rad);
32095c344501SMatthew G. Knepley       coordsIn[v * embedDim + 1] = PetscSinReal(rad);
32105c344501SMatthew G. Knepley     }
32115c344501SMatthew G. Knepley     break;
321265a81367SMatthew G. Knepley   case 2:
321365a81367SMatthew G. Knepley     if (simplex) {
321451a74b61SMatthew G. Knepley       const PetscReal radius    = PetscSqrtReal(1 + PETSC_PHI * PETSC_PHI) / (1.0 + PETSC_PHI);
321551a74b61SMatthew G. Knepley       const PetscReal edgeLen   = 2.0 / (1.0 + PETSC_PHI) * (R / radius);
321665a81367SMatthew G. Knepley       const PetscInt  degree    = 5;
321751a74b61SMatthew G. Knepley       PetscReal       vertex[3] = {0.0, 1.0 / (1.0 + PETSC_PHI), PETSC_PHI / (1.0 + PETSC_PHI)};
321865a81367SMatthew G. Knepley       PetscInt        s[3]      = {1, 1, 1};
321965a81367SMatthew G. Knepley       PetscInt        cone[3];
322007c565c5SJose E. Roman       PetscInt       *graph;
322165a81367SMatthew G. Knepley 
32229371c9d4SSatish Balay       vertex[0] *= R / radius;
32239371c9d4SSatish Balay       vertex[1] *= R / radius;
32249371c9d4SSatish Balay       vertex[2] *= R / radius;
3225dd400576SPatrick Sanan       numCells    = rank == 0 ? 20 : 0;
3226dd400576SPatrick Sanan       numVerts    = rank == 0 ? 12 : 0;
322765a81367SMatthew G. Knepley       firstVertex = numCells;
322851a74b61SMatthew G. Knepley       /* Use icosahedron, which for a R-sphere has coordinates which are all cyclic permutations of
322965a81367SMatthew G. Knepley 
323065a81367SMatthew G. Knepley            (0, \pm 1/\phi+1, \pm \phi/\phi+1)
323165a81367SMatthew G. Knepley 
323265a81367SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
323351a74b61SMatthew G. Knepley          length is then given by 2/(1+\phi) = 2 * 0.38197 = 0.76393.
323465a81367SMatthew G. Knepley       */
323565a81367SMatthew G. Knepley       /* Construct vertices */
32369566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
3237dd400576SPatrick Sanan       if (rank == 0) {
323807c565c5SJose E. Roman         for (PetscInt p = 0, i = 0; p < embedDim; ++p) {
323965a81367SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
324065a81367SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
324165a81367SMatthew G. Knepley               for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[(d + p) % embedDim] * vertex[(d + p) % embedDim];
324265a81367SMatthew G. Knepley               ++i;
324365a81367SMatthew G. Knepley             }
324465a81367SMatthew G. Knepley           }
324565a81367SMatthew G. Knepley         }
324645da822fSValeria Barra       }
324765a81367SMatthew G. Knepley       /* Construct graph */
32489566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * numVerts, &graph));
324907c565c5SJose E. Roman       for (PetscInt i = 0; i < numVerts; ++i) {
325007c565c5SJose E. Roman         PetscInt k = 0;
325107c565c5SJose E. Roman         for (PetscInt j = 0; j < numVerts; ++j) {
32529371c9d4SSatish Balay           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i * embedDim], &coordsIn[j * embedDim]) - edgeLen) < PETSC_SMALL) {
32539371c9d4SSatish Balay             graph[i * numVerts + j] = 1;
32549371c9d4SSatish Balay             ++k;
32559371c9d4SSatish Balay           }
325665a81367SMatthew G. Knepley         }
325763a3b9bcSJacob Faibussowitsch         PetscCheck(k == degree, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid icosahedron, vertex %" PetscInt_FMT " degree %" PetscInt_FMT " != %" PetscInt_FMT, i, k, degree);
325865a81367SMatthew G. Knepley       }
325965a81367SMatthew G. Knepley       /* Build Topology */
32609566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts));
326107c565c5SJose E. Roman       for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim));
32629566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
326365a81367SMatthew G. Knepley       /* Cells */
326407c565c5SJose E. Roman       for (PetscInt i = 0, c = 0; i < numVerts; ++i) {
326507c565c5SJose E. Roman         for (PetscInt j = 0; j < i; ++j) {
326607c565c5SJose E. Roman           for (PetscInt k = 0; k < j; ++k) {
326765a81367SMatthew G. Knepley             if (graph[i * numVerts + j] && graph[j * numVerts + k] && graph[k * numVerts + i]) {
32689371c9d4SSatish Balay               cone[0] = firstVertex + i;
32699371c9d4SSatish Balay               cone[1] = firstVertex + j;
32709371c9d4SSatish Balay               cone[2] = firstVertex + k;
327165a81367SMatthew G. Knepley               /* Check orientation */
327265a81367SMatthew G. Knepley               {
32739371c9d4SSatish Balay                 const PetscInt epsilon[3][3][3] = {
32749371c9d4SSatish Balay                   {{0, 0, 0},  {0, 0, 1},  {0, -1, 0}},
32759371c9d4SSatish Balay                   {{0, 0, -1}, {0, 0, 0},  {1, 0, 0} },
32769371c9d4SSatish Balay                   {{0, 1, 0},  {-1, 0, 0}, {0, 0, 0} }
32779371c9d4SSatish Balay                 };
327865a81367SMatthew G. Knepley                 PetscReal normal[3];
327965a81367SMatthew G. Knepley                 PetscInt  e, f;
328065a81367SMatthew G. Knepley 
328165a81367SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) {
328265a81367SMatthew G. Knepley                   normal[d] = 0.0;
328365a81367SMatthew G. Knepley                   for (e = 0; e < embedDim; ++e) {
3284ad540459SPierre 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]);
328565a81367SMatthew G. Knepley                   }
328665a81367SMatthew G. Knepley                 }
32879371c9d4SSatish Balay                 if (DotReal(embedDim, normal, &coordsIn[i * embedDim]) < 0) {
32889371c9d4SSatish Balay                   PetscInt tmp = cone[1];
32899371c9d4SSatish Balay                   cone[1]      = cone[2];
32909371c9d4SSatish Balay                   cone[2]      = tmp;
329165a81367SMatthew G. Knepley                 }
329265a81367SMatthew G. Knepley               }
32939566063dSJacob Faibussowitsch               PetscCall(DMPlexSetCone(dm, c++, cone));
329465a81367SMatthew G. Knepley             }
329565a81367SMatthew G. Knepley           }
329665a81367SMatthew G. Knepley         }
329765a81367SMatthew G. Knepley       }
32989566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
32999566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
33009566063dSJacob Faibussowitsch       PetscCall(PetscFree(graph));
330165a81367SMatthew G. Knepley     } else {
33022829fed8SMatthew G. Knepley       /*
33032829fed8SMatthew G. Knepley         12-21--13
33042829fed8SMatthew G. Knepley          |     |
33052829fed8SMatthew G. Knepley         25  4  24
33062829fed8SMatthew G. Knepley          |     |
33072829fed8SMatthew G. Knepley   12-25--9-16--8-24--13
33082829fed8SMatthew G. Knepley    |     |     |     |
33092829fed8SMatthew G. Knepley   23  5 17  0 15  3  22
33102829fed8SMatthew G. Knepley    |     |     |     |
33112829fed8SMatthew G. Knepley   10-20--6-14--7-19--11
33122829fed8SMatthew G. Knepley          |     |
33132829fed8SMatthew G. Knepley         20  1  19
33142829fed8SMatthew G. Knepley          |     |
33152829fed8SMatthew G. Knepley         10-18--11
33162829fed8SMatthew G. Knepley          |     |
33172829fed8SMatthew G. Knepley         23  2  22
33182829fed8SMatthew G. Knepley          |     |
33192829fed8SMatthew G. Knepley         12-21--13
33202829fed8SMatthew G. Knepley        */
33212829fed8SMatthew G. Knepley       PetscInt cone[4], ornt[4];
33222829fed8SMatthew G. Knepley 
3323dd400576SPatrick Sanan       numCells    = rank == 0 ? 6 : 0;
3324dd400576SPatrick Sanan       numEdges    = rank == 0 ? 12 : 0;
3325dd400576SPatrick Sanan       numVerts    = rank == 0 ? 8 : 0;
332665a81367SMatthew G. Knepley       firstVertex = numCells;
332765a81367SMatthew G. Knepley       firstEdge   = numCells + numVerts;
33282829fed8SMatthew G. Knepley       /* Build Topology */
33299566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells + numEdges + numVerts));
333007c565c5SJose E. Roman       for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 4));
333148a46eb9SPierre Jolivet       for (e = firstEdge; e < firstEdge + numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2));
33329566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
3333dd400576SPatrick Sanan       if (rank == 0) {
33342829fed8SMatthew G. Knepley         /* Cell 0 */
33359371c9d4SSatish Balay         cone[0] = 14;
33369371c9d4SSatish Balay         cone[1] = 15;
33379371c9d4SSatish Balay         cone[2] = 16;
33389371c9d4SSatish Balay         cone[3] = 17;
33399566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
33409371c9d4SSatish Balay         ornt[0] = 0;
33419371c9d4SSatish Balay         ornt[1] = 0;
33429371c9d4SSatish Balay         ornt[2] = 0;
33439371c9d4SSatish Balay         ornt[3] = 0;
33449566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 0, ornt));
33452829fed8SMatthew G. Knepley         /* Cell 1 */
33469371c9d4SSatish Balay         cone[0] = 18;
33479371c9d4SSatish Balay         cone[1] = 19;
33489371c9d4SSatish Balay         cone[2] = 14;
33499371c9d4SSatish Balay         cone[3] = 20;
33509566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
33519371c9d4SSatish Balay         ornt[0] = 0;
33529371c9d4SSatish Balay         ornt[1] = 0;
33539371c9d4SSatish Balay         ornt[2] = -1;
33549371c9d4SSatish Balay         ornt[3] = 0;
33559566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 1, ornt));
33562829fed8SMatthew G. Knepley         /* Cell 2 */
33579371c9d4SSatish Balay         cone[0] = 21;
33589371c9d4SSatish Balay         cone[1] = 22;
33599371c9d4SSatish Balay         cone[2] = 18;
33609371c9d4SSatish Balay         cone[3] = 23;
33619566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
33629371c9d4SSatish Balay         ornt[0] = 0;
33639371c9d4SSatish Balay         ornt[1] = 0;
33649371c9d4SSatish Balay         ornt[2] = -1;
33659371c9d4SSatish Balay         ornt[3] = 0;
33669566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 2, ornt));
33672829fed8SMatthew G. Knepley         /* Cell 3 */
33689371c9d4SSatish Balay         cone[0] = 19;
33699371c9d4SSatish Balay         cone[1] = 22;
33709371c9d4SSatish Balay         cone[2] = 24;
33719371c9d4SSatish Balay         cone[3] = 15;
33729566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
33739371c9d4SSatish Balay         ornt[0] = -1;
33749371c9d4SSatish Balay         ornt[1] = -1;
33759371c9d4SSatish Balay         ornt[2] = 0;
33769371c9d4SSatish Balay         ornt[3] = -1;
33779566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 3, ornt));
33782829fed8SMatthew G. Knepley         /* Cell 4 */
33799371c9d4SSatish Balay         cone[0] = 16;
33809371c9d4SSatish Balay         cone[1] = 24;
33819371c9d4SSatish Balay         cone[2] = 21;
33829371c9d4SSatish Balay         cone[3] = 25;
33839566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
33849371c9d4SSatish Balay         ornt[0] = -1;
33859371c9d4SSatish Balay         ornt[1] = -1;
33869371c9d4SSatish Balay         ornt[2] = -1;
33879371c9d4SSatish Balay         ornt[3] = 0;
33889566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 4, ornt));
33892829fed8SMatthew G. Knepley         /* Cell 5 */
33909371c9d4SSatish Balay         cone[0] = 20;
33919371c9d4SSatish Balay         cone[1] = 17;
33929371c9d4SSatish Balay         cone[2] = 25;
33939371c9d4SSatish Balay         cone[3] = 23;
33949566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 5, cone));
33959371c9d4SSatish Balay         ornt[0] = -1;
33969371c9d4SSatish Balay         ornt[1] = -1;
33979371c9d4SSatish Balay         ornt[2] = -1;
33989371c9d4SSatish Balay         ornt[3] = -1;
33999566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 5, ornt));
34002829fed8SMatthew G. Knepley         /* Edges */
34019371c9d4SSatish Balay         cone[0] = 6;
34029371c9d4SSatish Balay         cone[1] = 7;
34039566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 14, cone));
34049371c9d4SSatish Balay         cone[0] = 7;
34059371c9d4SSatish Balay         cone[1] = 8;
34069566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 15, cone));
34079371c9d4SSatish Balay         cone[0] = 8;
34089371c9d4SSatish Balay         cone[1] = 9;
34099566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 16, cone));
34109371c9d4SSatish Balay         cone[0] = 9;
34119371c9d4SSatish Balay         cone[1] = 6;
34129566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 17, cone));
34139371c9d4SSatish Balay         cone[0] = 10;
34149371c9d4SSatish Balay         cone[1] = 11;
34159566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 18, cone));
34169371c9d4SSatish Balay         cone[0] = 11;
34179371c9d4SSatish Balay         cone[1] = 7;
34189566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 19, cone));
34199371c9d4SSatish Balay         cone[0] = 6;
34209371c9d4SSatish Balay         cone[1] = 10;
34219566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 20, cone));
34229371c9d4SSatish Balay         cone[0] = 12;
34239371c9d4SSatish Balay         cone[1] = 13;
34249566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 21, cone));
34259371c9d4SSatish Balay         cone[0] = 13;
34269371c9d4SSatish Balay         cone[1] = 11;
34279566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 22, cone));
34289371c9d4SSatish Balay         cone[0] = 10;
34299371c9d4SSatish Balay         cone[1] = 12;
34309566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 23, cone));
34319371c9d4SSatish Balay         cone[0] = 13;
34329371c9d4SSatish Balay         cone[1] = 8;
34339566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 24, cone));
34349371c9d4SSatish Balay         cone[0] = 12;
34359371c9d4SSatish Balay         cone[1] = 9;
34369566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 25, cone));
343745da822fSValeria Barra       }
34389566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
34399566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
34402829fed8SMatthew G. Knepley       /* Build coordinates */
34419566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
3442dd400576SPatrick Sanan       if (rank == 0) {
34439371c9d4SSatish Balay         coordsIn[0 * embedDim + 0] = -R;
34449371c9d4SSatish Balay         coordsIn[0 * embedDim + 1] = R;
34459371c9d4SSatish Balay         coordsIn[0 * embedDim + 2] = -R;
34469371c9d4SSatish Balay         coordsIn[1 * embedDim + 0] = R;
34479371c9d4SSatish Balay         coordsIn[1 * embedDim + 1] = R;
34489371c9d4SSatish Balay         coordsIn[1 * embedDim + 2] = -R;
34499371c9d4SSatish Balay         coordsIn[2 * embedDim + 0] = R;
34509371c9d4SSatish Balay         coordsIn[2 * embedDim + 1] = -R;
34519371c9d4SSatish Balay         coordsIn[2 * embedDim + 2] = -R;
34529371c9d4SSatish Balay         coordsIn[3 * embedDim + 0] = -R;
34539371c9d4SSatish Balay         coordsIn[3 * embedDim + 1] = -R;
34549371c9d4SSatish Balay         coordsIn[3 * embedDim + 2] = -R;
34559371c9d4SSatish Balay         coordsIn[4 * embedDim + 0] = -R;
34569371c9d4SSatish Balay         coordsIn[4 * embedDim + 1] = R;
34579371c9d4SSatish Balay         coordsIn[4 * embedDim + 2] = R;
34589371c9d4SSatish Balay         coordsIn[5 * embedDim + 0] = R;
34599371c9d4SSatish Balay         coordsIn[5 * embedDim + 1] = R;
34609371c9d4SSatish Balay         coordsIn[5 * embedDim + 2] = R;
34619371c9d4SSatish Balay         coordsIn[6 * embedDim + 0] = -R;
34629371c9d4SSatish Balay         coordsIn[6 * embedDim + 1] = -R;
34639371c9d4SSatish Balay         coordsIn[6 * embedDim + 2] = R;
34649371c9d4SSatish Balay         coordsIn[7 * embedDim + 0] = R;
34659371c9d4SSatish Balay         coordsIn[7 * embedDim + 1] = -R;
34669371c9d4SSatish Balay         coordsIn[7 * embedDim + 2] = R;
346765a81367SMatthew G. Knepley       }
346845da822fSValeria Barra     }
346965a81367SMatthew G. Knepley     break;
347065a81367SMatthew G. Knepley   case 3:
3471116ded15SMatthew G. Knepley     if (simplex) {
3472116ded15SMatthew G. Knepley       const PetscReal edgeLen         = 1.0 / PETSC_PHI;
347351a74b61SMatthew G. Knepley       PetscReal       vertexA[4]      = {0.5, 0.5, 0.5, 0.5};
347451a74b61SMatthew G. Knepley       PetscReal       vertexB[4]      = {1.0, 0.0, 0.0, 0.0};
347551a74b61SMatthew G. Knepley       PetscReal       vertexC[4]      = {0.5, 0.5 * PETSC_PHI, 0.5 / PETSC_PHI, 0.0};
3476116ded15SMatthew G. Knepley       const PetscInt  degree          = 12;
3477116ded15SMatthew G. Knepley       PetscInt        s[4]            = {1, 1, 1};
34789371c9d4SSatish Balay       PetscInt        evenPerm[12][4] = {
34799371c9d4SSatish Balay         {0, 1, 2, 3},
34809371c9d4SSatish Balay         {0, 2, 3, 1},
34819371c9d4SSatish Balay         {0, 3, 1, 2},
34829371c9d4SSatish Balay         {1, 0, 3, 2},
34839371c9d4SSatish Balay         {1, 2, 0, 3},
34849371c9d4SSatish Balay         {1, 3, 2, 0},
34859371c9d4SSatish Balay         {2, 0, 1, 3},
34869371c9d4SSatish Balay         {2, 1, 3, 0},
34879371c9d4SSatish Balay         {2, 3, 0, 1},
34889371c9d4SSatish Balay         {3, 0, 2, 1},
34899371c9d4SSatish Balay         {3, 1, 0, 2},
34909371c9d4SSatish Balay         {3, 2, 1, 0}
34919371c9d4SSatish Balay       };
3492116ded15SMatthew G. Knepley       PetscInt  cone[4];
3493116ded15SMatthew G. Knepley       PetscInt *graph, p, i, j, k, l;
3494116ded15SMatthew G. Knepley 
34959371c9d4SSatish Balay       vertexA[0] *= R;
34969371c9d4SSatish Balay       vertexA[1] *= R;
34979371c9d4SSatish Balay       vertexA[2] *= R;
34989371c9d4SSatish Balay       vertexA[3] *= R;
34999371c9d4SSatish Balay       vertexB[0] *= R;
35009371c9d4SSatish Balay       vertexB[1] *= R;
35019371c9d4SSatish Balay       vertexB[2] *= R;
35029371c9d4SSatish Balay       vertexB[3] *= R;
35039371c9d4SSatish Balay       vertexC[0] *= R;
35049371c9d4SSatish Balay       vertexC[1] *= R;
35059371c9d4SSatish Balay       vertexC[2] *= R;
35069371c9d4SSatish Balay       vertexC[3] *= R;
3507dd400576SPatrick Sanan       numCells    = rank == 0 ? 600 : 0;
3508dd400576SPatrick Sanan       numVerts    = rank == 0 ? 120 : 0;
3509116ded15SMatthew G. Knepley       firstVertex = numCells;
3510116ded15SMatthew G. Knepley       /* Use the 600-cell, which for a unit sphere has coordinates which are
3511116ded15SMatthew G. Knepley 
3512116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm 1,    \pm 1, \pm 1)                          16
3513116ded15SMatthew G. Knepley                (\pm 1,    0,       0,      0)  all cyclic permutations   8
3514116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm phi, \pm 1/phi, 0)  all even permutations    96
3515116ded15SMatthew G. Knepley 
3516116ded15SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
35176333ae4fSvaleriabarra          length is then given by 1/\phi = 0.61803.
3518116ded15SMatthew G. Knepley 
3519116ded15SMatthew G. Knepley          http://buzzard.pugetsound.edu/sage-practice/ch03s03.html
3520116ded15SMatthew G. Knepley          http://mathworld.wolfram.com/600-Cell.html
3521116ded15SMatthew G. Knepley       */
3522116ded15SMatthew G. Knepley       /* Construct vertices */
35239566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
3524116ded15SMatthew G. Knepley       i = 0;
3525dd400576SPatrick Sanan       if (rank == 0) {
3526116ded15SMatthew G. Knepley         for (s[0] = -1; s[0] < 2; s[0] += 2) {
3527116ded15SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
3528116ded15SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
3529116ded15SMatthew G. Knepley               for (s[3] = -1; s[3] < 2; s[3] += 2) {
3530116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[d] * vertexA[d];
3531116ded15SMatthew G. Knepley                 ++i;
3532116ded15SMatthew G. Knepley               }
3533116ded15SMatthew G. Knepley             }
3534116ded15SMatthew G. Knepley           }
3535116ded15SMatthew G. Knepley         }
3536116ded15SMatthew G. Knepley         for (p = 0; p < embedDim; ++p) {
3537116ded15SMatthew G. Knepley           s[1] = s[2] = s[3] = 1;
3538116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
3539116ded15SMatthew G. Knepley             for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[(d + p) % embedDim] * vertexB[(d + p) % embedDim];
3540116ded15SMatthew G. Knepley             ++i;
3541116ded15SMatthew G. Knepley           }
3542116ded15SMatthew G. Knepley         }
3543116ded15SMatthew G. Knepley         for (p = 0; p < 12; ++p) {
3544116ded15SMatthew G. Knepley           s[3] = 1;
3545116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
3546116ded15SMatthew G. Knepley             for (s[1] = -1; s[1] < 2; s[1] += 2) {
3547116ded15SMatthew G. Knepley               for (s[2] = -1; s[2] < 2; s[2] += 2) {
3548116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[evenPerm[p][d]] * vertexC[evenPerm[p][d]];
3549116ded15SMatthew G. Knepley                 ++i;
3550116ded15SMatthew G. Knepley               }
3551116ded15SMatthew G. Knepley             }
3552116ded15SMatthew G. Knepley           }
3553116ded15SMatthew G. Knepley         }
355445da822fSValeria Barra       }
355563a3b9bcSJacob Faibussowitsch       PetscCheck(i == numVerts, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertices %" PetscInt_FMT " != %" PetscInt_FMT, i, numVerts);
3556116ded15SMatthew G. Knepley       /* Construct graph */
35579566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * numVerts, &graph));
3558116ded15SMatthew G. Knepley       for (i = 0; i < numVerts; ++i) {
3559116ded15SMatthew G. Knepley         for (j = 0, k = 0; j < numVerts; ++j) {
35609371c9d4SSatish Balay           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i * embedDim], &coordsIn[j * embedDim]) - edgeLen) < PETSC_SMALL) {
35619371c9d4SSatish Balay             graph[i * numVerts + j] = 1;
35629371c9d4SSatish Balay             ++k;
35639371c9d4SSatish Balay           }
3564116ded15SMatthew G. Knepley         }
356563a3b9bcSJacob Faibussowitsch         PetscCheck(k == degree, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertex %" PetscInt_FMT " degree %" PetscInt_FMT " != %" PetscInt_FMT, i, k, degree);
3566116ded15SMatthew G. Knepley       }
3567116ded15SMatthew G. Knepley       /* Build Topology */
35689566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts));
356907c565c5SJose E. Roman       for (PetscInt c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim));
35709566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
3571116ded15SMatthew G. Knepley       /* Cells */
3572dd400576SPatrick Sanan       if (rank == 0) {
357307c565c5SJose E. Roman         for (PetscInt i = 0, c = 0; i < numVerts; ++i) {
3574116ded15SMatthew G. Knepley           for (j = 0; j < i; ++j) {
3575116ded15SMatthew G. Knepley             for (k = 0; k < j; ++k) {
3576116ded15SMatthew G. Knepley               for (l = 0; l < k; ++l) {
35779371c9d4SSatish 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]) {
35789371c9d4SSatish Balay                   cone[0] = firstVertex + i;
35799371c9d4SSatish Balay                   cone[1] = firstVertex + j;
35809371c9d4SSatish Balay                   cone[2] = firstVertex + k;
35819371c9d4SSatish Balay                   cone[3] = firstVertex + l;
3582116ded15SMatthew G. Knepley                   /* Check orientation: https://ef.gy/linear-algebra:normal-vectors-in-higher-dimensional-spaces */
3583116ded15SMatthew G. Knepley                   {
35849371c9d4SSatish Balay                     const PetscInt epsilon[4][4][4][4] = {
35859371c9d4SSatish 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}}},
3586116ded15SMatthew G. Knepley 
35879371c9d4SSatish 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}}},
3588116ded15SMatthew G. Knepley 
35899371c9d4SSatish 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}}},
3590116ded15SMatthew G. Knepley 
35919371c9d4SSatish 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}} }
35929371c9d4SSatish Balay                     };
3593116ded15SMatthew G. Knepley                     PetscReal normal[4];
3594116ded15SMatthew G. Knepley                     PetscInt  e, f, g;
3595116ded15SMatthew G. Knepley 
3596116ded15SMatthew G. Knepley                     for (d = 0; d < embedDim; ++d) {
3597116ded15SMatthew G. Knepley                       normal[d] = 0.0;
3598116ded15SMatthew G. Knepley                       for (e = 0; e < embedDim; ++e) {
3599116ded15SMatthew G. Knepley                         for (f = 0; f < embedDim; ++f) {
3600116ded15SMatthew G. Knepley                           for (g = 0; g < embedDim; ++g) {
3601116ded15SMatthew 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]);
3602116ded15SMatthew G. Knepley                           }
3603116ded15SMatthew G. Knepley                         }
3604116ded15SMatthew G. Knepley                       }
3605116ded15SMatthew G. Knepley                     }
36069371c9d4SSatish Balay                     if (DotReal(embedDim, normal, &coordsIn[i * embedDim]) < 0) {
36079371c9d4SSatish Balay                       PetscInt tmp = cone[1];
36089371c9d4SSatish Balay                       cone[1]      = cone[2];
36099371c9d4SSatish Balay                       cone[2]      = tmp;
36109371c9d4SSatish Balay                     }
3611116ded15SMatthew G. Knepley                   }
36129566063dSJacob Faibussowitsch                   PetscCall(DMPlexSetCone(dm, c++, cone));
3613116ded15SMatthew G. Knepley                 }
3614116ded15SMatthew G. Knepley               }
3615116ded15SMatthew G. Knepley             }
3616116ded15SMatthew G. Knepley           }
3617116ded15SMatthew G. Knepley         }
361845da822fSValeria Barra       }
36199566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
36209566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
36219566063dSJacob Faibussowitsch       PetscCall(PetscFree(graph));
3622116ded15SMatthew G. Knepley     }
3623f4d061e9SPierre Jolivet     break;
3624d71ae5a4SJacob Faibussowitsch   default:
3625d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension for sphere: %" PetscInt_FMT, dim);
362665a81367SMatthew G. Knepley   }
362765a81367SMatthew G. Knepley   /* Create coordinates */
36289566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
36299566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
36309566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, embedDim));
36319566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numVerts));
36322829fed8SMatthew G. Knepley   for (v = firstVertex; v < firstVertex + numVerts; ++v) {
36339566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, embedDim));
36349566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, embedDim));
36352829fed8SMatthew G. Knepley   }
36369566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
36379566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
36389566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
36399566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, embedDim));
36409566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
36419566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
36429566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
36439566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
36449371c9d4SSatish Balay   for (v = 0; v < numVerts; ++v)
3645ad540459SPierre Jolivet     for (d = 0; d < embedDim; ++d) coords[v * embedDim + d] = coordsIn[v * embedDim + d];
36469566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
36479566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
36489566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
36499566063dSJacob Faibussowitsch   PetscCall(PetscFree(coordsIn));
365051a74b61SMatthew G. Knepley   {
365151a74b61SMatthew G. Knepley     DM          cdm;
365251a74b61SMatthew G. Knepley     PetscDS     cds;
36539318fe57SMatthew G. Knepley     PetscScalar c = R;
365451a74b61SMatthew G. Knepley 
3655509b31aaSMatthew G. Knepley     PetscCall(DMPlexSetCoordinateMap(dm, snapToSphere));
3656509b31aaSMatthew G. Knepley     PetscCall(DMPlexCreateCoordinateSpace(dm, 1, PETSC_TRUE, NULL));
36579566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
36589566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
36599566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, 1, &c));
366051a74b61SMatthew G. Knepley   }
366146139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
36629318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
36639566063dSJacob Faibussowitsch   if (simplex) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
36643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
36659318fe57SMatthew G. Knepley }
36669318fe57SMatthew G. Knepley 
3667b7f5c055SJed Brown typedef void (*TPSEvaluateFunc)(const PetscReal[], PetscReal *, PetscReal[], PetscReal (*)[3]);
3668b7f5c055SJed Brown 
3669b7f5c055SJed Brown /*
3670b7f5c055SJed Brown  The Schwarz P implicit surface is
3671b7f5c055SJed Brown 
3672b7f5c055SJed Brown      f(x) = cos(x0) + cos(x1) + cos(x2) = 0
3673b7f5c055SJed Brown */
3674d71ae5a4SJacob Faibussowitsch static void TPSEvaluate_SchwarzP(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3])
3675d71ae5a4SJacob Faibussowitsch {
3676b7f5c055SJed Brown   PetscReal c[3] = {PetscCosReal(y[0] * PETSC_PI), PetscCosReal(y[1] * PETSC_PI), PetscCosReal(y[2] * PETSC_PI)};
3677b7f5c055SJed Brown   PetscReal g[3] = {-PetscSinReal(y[0] * PETSC_PI), -PetscSinReal(y[1] * PETSC_PI), -PetscSinReal(y[2] * PETSC_PI)};
3678b7f5c055SJed Brown   f[0]           = c[0] + c[1] + c[2];
3679b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) {
3680b7f5c055SJed Brown     grad[i] = PETSC_PI * g[i];
3681ad540459SPierre Jolivet     for (PetscInt j = 0; j < 3; j++) hess[i][j] = (i == j) ? -PetscSqr(PETSC_PI) * c[i] : 0.;
3682b7f5c055SJed Brown   }
3683b7f5c055SJed Brown }
3684b7f5c055SJed Brown 
36854663dae6SJed Brown // u[] is a tentative normal on input. Replace with the implicit function gradient in the same direction
3686d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSExtrudeNormalFunc_SchwarzP(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt r, PetscScalar u[], void *ctx)
3687d71ae5a4SJacob Faibussowitsch {
3688ad540459SPierre Jolivet   for (PetscInt i = 0; i < 3; i++) u[i] = -PETSC_PI * PetscSinReal(x[i] * PETSC_PI);
36893ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
36904663dae6SJed Brown }
36914663dae6SJed Brown 
3692b7f5c055SJed Brown /*
3693b7f5c055SJed Brown  The Gyroid implicit surface is
3694b7f5c055SJed Brown 
3695b7f5c055SJed 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)
3696b7f5c055SJed Brown 
3697b7f5c055SJed Brown */
3698d71ae5a4SJacob Faibussowitsch static void TPSEvaluate_Gyroid(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3])
3699d71ae5a4SJacob Faibussowitsch {
3700b7f5c055SJed Brown   PetscReal s[3] = {PetscSinReal(PETSC_PI * y[0]), PetscSinReal(PETSC_PI * (y[1] + .5)), PetscSinReal(PETSC_PI * (y[2] + .25))};
3701b7f5c055SJed Brown   PetscReal c[3] = {PetscCosReal(PETSC_PI * y[0]), PetscCosReal(PETSC_PI * (y[1] + .5)), PetscCosReal(PETSC_PI * (y[2] + .25))};
3702b7f5c055SJed Brown   f[0]           = s[0] * c[1] + s[1] * c[2] + s[2] * c[0];
3703b7f5c055SJed Brown   grad[0]        = PETSC_PI * (c[0] * c[1] - s[2] * s[0]);
3704b7f5c055SJed Brown   grad[1]        = PETSC_PI * (c[1] * c[2] - s[0] * s[1]);
3705b7f5c055SJed Brown   grad[2]        = PETSC_PI * (c[2] * c[0] - s[1] * s[2]);
3706b7f5c055SJed Brown   hess[0][0]     = -PetscSqr(PETSC_PI) * (s[0] * c[1] + s[2] * c[0]);
3707b7f5c055SJed Brown   hess[0][1]     = -PetscSqr(PETSC_PI) * (c[0] * s[1]);
3708b7f5c055SJed Brown   hess[0][2]     = -PetscSqr(PETSC_PI) * (c[2] * s[0]);
3709b7f5c055SJed Brown   hess[1][0]     = -PetscSqr(PETSC_PI) * (s[1] * c[2] + s[0] * c[1]);
3710b7f5c055SJed Brown   hess[1][1]     = -PetscSqr(PETSC_PI) * (c[1] * s[2]);
3711b7f5c055SJed Brown   hess[2][2]     = -PetscSqr(PETSC_PI) * (c[0] * s[1]);
3712b7f5c055SJed Brown   hess[2][0]     = -PetscSqr(PETSC_PI) * (s[2] * c[0] + s[1] * c[2]);
3713b7f5c055SJed Brown   hess[2][1]     = -PetscSqr(PETSC_PI) * (c[2] * s[0]);
3714b7f5c055SJed Brown   hess[2][2]     = -PetscSqr(PETSC_PI) * (c[1] * s[2]);
3715b7f5c055SJed Brown }
3716b7f5c055SJed Brown 
37174663dae6SJed Brown // u[] is a tentative normal on input. Replace with the implicit function gradient in the same direction
3718d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSExtrudeNormalFunc_Gyroid(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt r, PetscScalar u[], void *ctx)
3719d71ae5a4SJacob Faibussowitsch {
37204663dae6SJed Brown   PetscReal s[3] = {PetscSinReal(PETSC_PI * x[0]), PetscSinReal(PETSC_PI * (x[1] + .5)), PetscSinReal(PETSC_PI * (x[2] + .25))};
37214663dae6SJed Brown   PetscReal c[3] = {PetscCosReal(PETSC_PI * x[0]), PetscCosReal(PETSC_PI * (x[1] + .5)), PetscCosReal(PETSC_PI * (x[2] + .25))};
37224663dae6SJed Brown   u[0]           = PETSC_PI * (c[0] * c[1] - s[2] * s[0]);
37234663dae6SJed Brown   u[1]           = PETSC_PI * (c[1] * c[2] - s[0] * s[1]);
37244663dae6SJed Brown   u[2]           = PETSC_PI * (c[2] * c[0] - s[1] * s[2]);
37253ba16761SJacob Faibussowitsch   return PETSC_SUCCESS;
37264663dae6SJed Brown }
37274663dae6SJed Brown 
3728b7f5c055SJed Brown /*
3729b7f5c055SJed Brown    We wish to solve
3730b7f5c055SJed Brown 
3731b7f5c055SJed Brown          min_y || y - x ||^2  subject to f(y) = 0
3732b7f5c055SJed Brown 
3733b7f5c055SJed Brown    Let g(y) = grad(f).  The minimization problem is equivalent to asking to satisfy
3734b7f5c055SJed 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
3735b7f5c055SJed Brown    tangent space and ask for both components in the tangent space to be zero.
3736b7f5c055SJed Brown 
3737b7f5c055SJed Brown    Take g to be a column vector and compute the "full QR" factorization Q R = g,
3738b7f5c055SJed Brown    where Q = I - 2 n n^T is a symmetric orthogonal matrix.
3739b7f5c055SJed Brown    The first column of Q is parallel to g so the remaining two columns span the null space.
3740b7f5c055SJed Brown    Let Qn = Q[:,1:] be those remaining columns.  Then Qn Qn^T is an orthogonal projector into the tangent space.
3741da81f932SPierre Jolivet    Since Q is symmetric, this is equivalent to multiplying by Q and taking the last two entries.
3742b7f5c055SJed Brown    In total, we have a system of 3 equations in 3 unknowns:
3743b7f5c055SJed Brown 
3744b7f5c055SJed Brown      f(y) = 0                       1 equation
3745b7f5c055SJed Brown      Qn^T (y - x) = 0               2 equations
3746b7f5c055SJed Brown 
3747b7f5c055SJed Brown    Here, we compute the residual and Jacobian of this system.
3748b7f5c055SJed Brown */
3749d71ae5a4SJacob Faibussowitsch static void TPSNearestPointResJac(TPSEvaluateFunc feval, const PetscScalar x[], const PetscScalar y[], PetscScalar res[], PetscScalar J[])
3750d71ae5a4SJacob Faibussowitsch {
3751b7f5c055SJed Brown   PetscReal yreal[3] = {PetscRealPart(y[0]), PetscRealPart(y[1]), PetscRealPart(y[2])};
3752b7f5c055SJed Brown   PetscReal d[3]     = {PetscRealPart(y[0] - x[0]), PetscRealPart(y[1] - x[1]), PetscRealPart(y[2] - x[2])};
37532f0490c0SSatish Balay   PetscReal f, grad[3], n[3], norm, norm_y[3], nd, nd_y[3], sign;
37549371c9d4SSatish Balay   PetscReal n_y[3][3] = {
37559371c9d4SSatish Balay     {0, 0, 0},
37569371c9d4SSatish Balay     {0, 0, 0},
37579371c9d4SSatish Balay     {0, 0, 0}
37589371c9d4SSatish Balay   };
3759b7f5c055SJed Brown 
3760b7f5c055SJed Brown   feval(yreal, &f, grad, n_y);
3761b7f5c055SJed Brown 
3762b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) n[i] = grad[i];
3763b7f5c055SJed Brown   norm = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2]));
3764ad540459SPierre Jolivet   for (PetscInt i = 0; i < 3; i++) norm_y[i] = 1. / norm * n[i] * n_y[i][i];
3765b7f5c055SJed Brown 
3766b7f5c055SJed Brown   // Define the Householder reflector
3767b7f5c055SJed Brown   sign = n[0] >= 0 ? 1. : -1.;
3768b7f5c055SJed Brown   n[0] += norm * sign;
3769b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) n_y[0][i] += norm_y[i] * sign;
3770b7f5c055SJed Brown 
3771b7f5c055SJed Brown   norm      = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2]));
3772b7f5c055SJed Brown   norm_y[0] = 1. / norm * (n[0] * n_y[0][0]);
3773b7f5c055SJed Brown   norm_y[1] = 1. / norm * (n[0] * n_y[0][1] + n[1] * n_y[1][1]);
3774b7f5c055SJed Brown   norm_y[2] = 1. / norm * (n[0] * n_y[0][2] + n[2] * n_y[2][2]);
3775b7f5c055SJed Brown 
3776b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) {
3777b7f5c055SJed Brown     n[i] /= norm;
3778b7f5c055SJed Brown     for (PetscInt j = 0; j < 3; j++) {
3779b7f5c055SJed Brown       // note that n[i] is n_old[i]/norm when executing the code below
3780b7f5c055SJed Brown       n_y[i][j] = n_y[i][j] / norm - n[i] / norm * norm_y[j];
3781b7f5c055SJed Brown     }
3782b7f5c055SJed Brown   }
3783b7f5c055SJed Brown 
3784b7f5c055SJed Brown   nd = n[0] * d[0] + n[1] * d[1] + n[2] * d[2];
3785b7f5c055SJed 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];
3786b7f5c055SJed Brown 
3787b7f5c055SJed Brown   res[0] = f;
3788b7f5c055SJed Brown   res[1] = d[1] - 2 * n[1] * nd;
3789b7f5c055SJed Brown   res[2] = d[2] - 2 * n[2] * nd;
3790b7f5c055SJed Brown   // J[j][i] is J_{ij} (column major)
3791b7f5c055SJed Brown   for (PetscInt j = 0; j < 3; j++) {
3792b7f5c055SJed Brown     J[0 + j * 3] = grad[j];
3793b7f5c055SJed Brown     J[1 + j * 3] = (j == 1) * 1. - 2 * (n_y[1][j] * nd + n[1] * nd_y[j]);
3794b7f5c055SJed Brown     J[2 + j * 3] = (j == 2) * 1. - 2 * (n_y[2][j] * nd + n[2] * nd_y[j]);
3795b7f5c055SJed Brown   }
3796b7f5c055SJed Brown }
3797b7f5c055SJed Brown 
3798b7f5c055SJed Brown /*
3799b7f5c055SJed Brown    Project x to the nearest point on the implicit surface using Newton's method.
3800b7f5c055SJed Brown */
3801d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSNearestPoint(TPSEvaluateFunc feval, PetscScalar x[])
3802d71ae5a4SJacob Faibussowitsch {
3803b7f5c055SJed Brown   PetscScalar y[3] = {x[0], x[1], x[2]}; // Initial guess
3804b7f5c055SJed Brown 
3805b7f5c055SJed Brown   PetscFunctionBegin;
3806b7f5c055SJed Brown   for (PetscInt iter = 0; iter < 10; iter++) {
3807b7f5c055SJed Brown     PetscScalar res[3], J[9];
3808b7f5c055SJed Brown     PetscReal   resnorm;
3809b7f5c055SJed Brown     TPSNearestPointResJac(feval, x, y, res, J);
3810b7f5c055SJed Brown     resnorm = PetscSqrtReal(PetscSqr(PetscRealPart(res[0])) + PetscSqr(PetscRealPart(res[1])) + PetscSqr(PetscRealPart(res[2])));
3811b7f5c055SJed Brown     if (0) { // Turn on this monitor if you need to confirm quadratic convergence
381263a3b9bcSJacob 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])));
3813b7f5c055SJed Brown     }
3814b7f5c055SJed Brown     if (resnorm < PETSC_SMALL) break;
3815b7f5c055SJed Brown 
3816b7f5c055SJed Brown     // Take the Newton step
38179566063dSJacob Faibussowitsch     PetscCall(PetscKernel_A_gets_inverse_A_3(J, 0., PETSC_FALSE, NULL));
3818b7f5c055SJed Brown     PetscKernel_v_gets_v_minus_A_times_w_3(y, J, res);
3819b7f5c055SJed Brown   }
3820b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) x[i] = y[i];
38213ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
3822b7f5c055SJed Brown }
3823b7f5c055SJed Brown 
3824b7f5c055SJed Brown const char *const DMPlexTPSTypes[] = {"SCHWARZ_P", "GYROID", "DMPlexTPSType", "DMPLEX_TPS_", NULL};
3825b7f5c055SJed Brown 
3826d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateTPSMesh_Internal(DM dm, DMPlexTPSType tpstype, const PetscInt extent[], const DMBoundaryType periodic[], PetscBool tps_distribute, PetscInt refinements, PetscInt layers, PetscReal thickness)
3827d71ae5a4SJacob Faibussowitsch {
3828b7f5c055SJed Brown   PetscMPIInt rank;
3829b7f5c055SJed Brown   PetscInt    topoDim = 2, spaceDim = 3, numFaces = 0, numVertices = 0, numEdges = 0;
3830b7f5c055SJed Brown   PetscInt (*edges)[2] = NULL, *edgeSets = NULL;
3831b7f5c055SJed Brown   PetscInt           *cells_flat = NULL;
3832b7f5c055SJed Brown   PetscReal          *vtxCoords  = NULL;
3833b7f5c055SJed Brown   TPSEvaluateFunc     evalFunc   = NULL;
38348434afd1SBarry Smith   PetscSimplePointFn *normalFunc = NULL;
3835b7f5c055SJed Brown   DMLabel             label;
3836b7f5c055SJed Brown 
3837b7f5c055SJed Brown   PetscFunctionBegin;
383846139095SJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_Generate, dm, 0, 0, 0));
38399566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
384063a3b9bcSJacob 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);
3841b7f5c055SJed Brown   switch (tpstype) {
3842b7f5c055SJed Brown   case DMPLEX_TPS_SCHWARZ_P:
3843b7f5c055SJed 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");
3844c5853193SPierre Jolivet     if (rank == 0) {
3845b7f5c055SJed Brown       PetscInt (*cells)[6][4][4] = NULL; // [junction, junction-face, cell, conn]
3846b7f5c055SJed Brown       PetscInt  Njunctions = 0, Ncuts = 0, Npipes[3], vcount;
3847b7f5c055SJed Brown       PetscReal L = 1;
3848b7f5c055SJed Brown 
3849b7f5c055SJed Brown       Npipes[0]   = (extent[0] + 1) * extent[1] * extent[2];
3850b7f5c055SJed Brown       Npipes[1]   = extent[0] * (extent[1] + 1) * extent[2];
3851b7f5c055SJed Brown       Npipes[2]   = extent[0] * extent[1] * (extent[2] + 1);
3852b7f5c055SJed Brown       Njunctions  = extent[0] * extent[1] * extent[2];
3853b7f5c055SJed Brown       Ncuts       = 2 * (extent[0] * extent[1] + extent[1] * extent[2] + extent[2] * extent[0]);
3854b7f5c055SJed Brown       numVertices = 4 * (Npipes[0] + Npipes[1] + Npipes[2]) + 8 * Njunctions;
38559566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(3 * numVertices, &vtxCoords));
38569566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Njunctions, &cells));
38579566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Ncuts * 4, &edges));
38589566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Ncuts * 4, &edgeSets));
3859b7f5c055SJed Brown       // x-normal pipes
3860b7f5c055SJed Brown       vcount = 0;
3861b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0] + 1; i++) {
3862b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3863b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2]; k++) {
3864b7f5c055SJed Brown             for (PetscInt l = 0; l < 4; l++) {
3865b7f5c055SJed Brown               vtxCoords[vcount++] = (2 * i - 1) * L;
3866b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * j * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3867b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * k * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3868b7f5c055SJed Brown             }
3869b7f5c055SJed Brown           }
3870b7f5c055SJed Brown         }
3871b7f5c055SJed Brown       }
3872b7f5c055SJed Brown       // y-normal pipes
3873b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0]; i++) {
3874b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1] + 1; j++) {
3875b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2]; k++) {
3876b7f5c055SJed Brown             for (PetscInt l = 0; l < 4; l++) {
3877b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * i * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3878b7f5c055SJed Brown               vtxCoords[vcount++] = (2 * j - 1) * L;
3879b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * k * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3880b7f5c055SJed Brown             }
3881b7f5c055SJed Brown           }
3882b7f5c055SJed Brown         }
3883b7f5c055SJed Brown       }
3884b7f5c055SJed Brown       // z-normal pipes
3885b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0]; i++) {
3886b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3887b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2] + 1; k++) {
3888b7f5c055SJed Brown             for (PetscInt l = 0; l < 4; l++) {
3889b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * i * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3890b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * j * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3891b7f5c055SJed Brown               vtxCoords[vcount++] = (2 * k - 1) * L;
3892b7f5c055SJed Brown             }
3893b7f5c055SJed Brown           }
3894b7f5c055SJed Brown         }
3895b7f5c055SJed Brown       }
3896b7f5c055SJed Brown       // junctions
3897b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0]; i++) {
3898b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3899b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2]; k++) {
3900b7f5c055SJed Brown             const PetscInt J = (i * extent[1] + j) * extent[2] + k, Jvoff = (Npipes[0] + Npipes[1] + Npipes[2]) * 4 + J * 8;
3901b7f5c055SJed Brown             PetscCheck(vcount / 3 == Jvoff, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unexpected vertex count");
3902b7f5c055SJed Brown             for (PetscInt ii = 0; ii < 2; ii++) {
3903b7f5c055SJed Brown               for (PetscInt jj = 0; jj < 2; jj++) {
3904b7f5c055SJed Brown                 for (PetscInt kk = 0; kk < 2; kk++) {
3905b7f5c055SJed Brown                   double Ls           = (1 - sqrt(2) / 4) * L;
3906b7f5c055SJed Brown                   vtxCoords[vcount++] = 2 * i * L + (2 * ii - 1) * Ls;
3907b7f5c055SJed Brown                   vtxCoords[vcount++] = 2 * j * L + (2 * jj - 1) * Ls;
3908b7f5c055SJed Brown                   vtxCoords[vcount++] = 2 * k * L + (2 * kk - 1) * Ls;
3909b7f5c055SJed Brown                 }
3910b7f5c055SJed Brown               }
3911b7f5c055SJed Brown             }
3912b7f5c055SJed Brown             const PetscInt jfaces[3][2][4] = {
3913b7f5c055SJed Brown               {{3, 1, 0, 2}, {7, 5, 4, 6}}, // x-aligned
3914b7f5c055SJed Brown               {{5, 4, 0, 1}, {7, 6, 2, 3}}, // y-aligned
3915b7f5c055SJed Brown               {{6, 2, 0, 4}, {7, 3, 1, 5}}  // z-aligned
3916b7f5c055SJed Brown             };
3917b7f5c055SJed Brown             const PetscInt pipe_lo[3] = {// vertex numbers of pipes
39189371c9d4SSatish 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};
3919b7f5c055SJed Brown             const PetscInt pipe_hi[3] = {// vertex numbers of pipes
39209371c9d4SSatish 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};
3921b7f5c055SJed Brown             for (PetscInt dir = 0; dir < 3; dir++) { // x,y,z
3922b7f5c055SJed Brown               const PetscInt ijk[3] = {i, j, k};
3923b7f5c055SJed Brown               for (PetscInt l = 0; l < 4; l++) { // rotations
3924b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][0] = pipe_lo[dir] + l;
3925b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][1] = Jvoff + jfaces[dir][0][l];
3926b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][2] = Jvoff + jfaces[dir][0][(l - 1 + 4) % 4];
3927b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][3] = pipe_lo[dir] + (l - 1 + 4) % 4;
3928b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][0] = Jvoff + jfaces[dir][1][l];
3929b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][1] = pipe_hi[dir] + l;
3930b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][2] = pipe_hi[dir] + (l - 1 + 4) % 4;
3931b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][3] = Jvoff + jfaces[dir][1][(l - 1 + 4) % 4];
3932b7f5c055SJed Brown                 if (ijk[dir] == 0) {
3933b7f5c055SJed Brown                   edges[numEdges][0] = pipe_lo[dir] + l;
3934b7f5c055SJed Brown                   edges[numEdges][1] = pipe_lo[dir] + (l + 1) % 4;
3935b7f5c055SJed Brown                   edgeSets[numEdges] = dir * 2 + 1;
3936b7f5c055SJed Brown                   numEdges++;
3937b7f5c055SJed Brown                 }
3938b7f5c055SJed Brown                 if (ijk[dir] + 1 == extent[dir]) {
3939b7f5c055SJed Brown                   edges[numEdges][0] = pipe_hi[dir] + l;
3940b7f5c055SJed Brown                   edges[numEdges][1] = pipe_hi[dir] + (l + 1) % 4;
3941b7f5c055SJed Brown                   edgeSets[numEdges] = dir * 2 + 2;
3942b7f5c055SJed Brown                   numEdges++;
3943b7f5c055SJed Brown                 }
3944b7f5c055SJed Brown               }
3945b7f5c055SJed Brown             }
3946b7f5c055SJed Brown           }
3947b7f5c055SJed Brown         }
3948b7f5c055SJed Brown       }
394963a3b9bcSJacob Faibussowitsch       PetscCheck(numEdges == Ncuts * 4, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Edge count %" PetscInt_FMT " incompatible with number of cuts %" PetscInt_FMT, numEdges, Ncuts);
3950b7f5c055SJed Brown       numFaces   = 24 * Njunctions;
3951b7f5c055SJed Brown       cells_flat = cells[0][0][0];
3952b7f5c055SJed Brown     }
3953b7f5c055SJed Brown     evalFunc   = TPSEvaluate_SchwarzP;
39544663dae6SJed Brown     normalFunc = TPSExtrudeNormalFunc_SchwarzP;
3955b7f5c055SJed Brown     break;
3956b7f5c055SJed Brown   case DMPLEX_TPS_GYROID:
3957c5853193SPierre Jolivet     if (rank == 0) {
3958b7f5c055SJed Brown       // This is a coarse mesh approximation of the gyroid shifted to being the zero of the level set
3959b7f5c055SJed Brown       //
3960b7f5c055SJed 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)
3961b7f5c055SJed Brown       //
3962b7f5c055SJed Brown       // on the cell [0,2]^3.
3963b7f5c055SJed Brown       //
3964b7f5c055SJed Brown       // Think about dividing that cell into four columns, and focus on the column [0,1]x[0,1]x[0,2].
3965b7f5c055SJed Brown       // If you looked at the gyroid in that column at different slices of z you would see that it kind of spins
3966b7f5c055SJed Brown       // like a boomerang:
3967b7f5c055SJed Brown       //
3968b7f5c055SJed Brown       //     z = 0          z = 1/4        z = 1/2        z = 3/4     //
3969b7f5c055SJed Brown       //     -----          -------        -------        -------     //
3970b7f5c055SJed Brown       //                                                              //
3971b7f5c055SJed Brown       //     +       +      +       +      +       +      +   \   +   //
3972b7f5c055SJed Brown       //      \                                   /            \      //
3973b7f5c055SJed Brown       //       \            `-_   _-'            /              }     //
3974b7f5c055SJed Brown       //        *-_            `-'            _-'              /      //
3975b7f5c055SJed Brown       //     +     `-+      +       +      +-'     +      +   /   +   //
3976b7f5c055SJed Brown       //                                                              //
3977b7f5c055SJed Brown       //                                                              //
3978b7f5c055SJed Brown       //     z = 1          z = 5/4        z = 3/2        z = 7/4     //
3979b7f5c055SJed Brown       //     -----          -------        -------        -------     //
3980b7f5c055SJed Brown       //                                                              //
3981b7f5c055SJed Brown       //     +-_     +      +       +      +     _-+      +   /   +   //
3982b7f5c055SJed Brown       //        `-_            _-_            _-`            /        //
3983b7f5c055SJed Brown       //           \        _-'   `-_        /              {         //
3984b7f5c055SJed Brown       //            \                       /                \        //
3985b7f5c055SJed Brown       //     +       +      +       +      +       +      +   \   +   //
3986b7f5c055SJed Brown       //
3987b7f5c055SJed Brown       //
3988b7f5c055SJed Brown       // This course mesh approximates each of these slices by two line segments,
3989b7f5c055SJed Brown       // and then connects the segments in consecutive layers with quadrilateral faces.
3990b7f5c055SJed Brown       // All of the end points of the segments are multiples of 1/4 except for the
3991b7f5c055SJed Brown       // point * in the picture for z = 0 above and the similar points in other layers.
3992b7f5c055SJed Brown       // That point is at (gamma, gamma, 0), where gamma is calculated below.
3993b7f5c055SJed Brown       //
3994b7f5c055SJed Brown       // The column  [1,2]x[1,2]x[0,2] looks the same as this column;
3995b7f5c055SJed Brown       // The columns [1,2]x[0,1]x[0,2] and [0,1]x[1,2]x[0,2] are mirror images.
3996b7f5c055SJed Brown       //
3997b7f5c055SJed Brown       // As for how this method turned into the names given to the vertices:
3998b7f5c055SJed Brown       // that was not systematic, it was just the way it worked out in my handwritten notes.
3999b7f5c055SJed Brown 
4000b7f5c055SJed Brown       PetscInt facesPerBlock = 64;
4001b7f5c055SJed Brown       PetscInt vertsPerBlock = 56;
4002b7f5c055SJed Brown       PetscInt extentPlus[3];
4003b7f5c055SJed Brown       PetscInt numBlocks, numBlocksPlus;
40049371c9d4SSatish 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;
40059371c9d4SSatish Balay       const PetscInt pattern[64][4] = {
40069371c9d4SSatish Balay         /* face to vertex within the coarse discretization of a single gyroid block */
4007b7f5c055SJed Brown         /* layer 0 */
40089371c9d4SSatish Balay         {A,           C,           K,           G          },
40099371c9d4SSatish Balay         {C,           B,           II,          K          },
40109371c9d4SSatish Balay         {D,           A,           H,           L          },
40119371c9d4SSatish Balay         {B + 56 * 1,  D,           L,           J          },
40129371c9d4SSatish Balay         {E,           B + 56 * 1,  J,           N          },
40139371c9d4SSatish Balay         {A + 56 * 2,  E,           N,           H + 56 * 2 },
40149371c9d4SSatish Balay         {F,           A + 56 * 2,  G + 56 * 2,  M          },
40159371c9d4SSatish Balay         {B,           F,           M,           II         },
4016b7f5c055SJed Brown         /* layer 1 */
40179371c9d4SSatish Balay         {G,           K,           Q,           O          },
40189371c9d4SSatish Balay         {K,           II,          P,           Q          },
40199371c9d4SSatish Balay         {L,           H,           O + 56 * 1,  R          },
40209371c9d4SSatish Balay         {J,           L,           R,           P          },
40219371c9d4SSatish Balay         {N,           J,           P,           S          },
40229371c9d4SSatish Balay         {H + 56 * 2,  N,           S,           O + 56 * 3 },
40239371c9d4SSatish Balay         {M,           G + 56 * 2,  O + 56 * 2,  T          },
40249371c9d4SSatish Balay         {II,          M,           T,           P          },
4025b7f5c055SJed Brown         /* layer 2 */
40269371c9d4SSatish Balay         {O,           Q,           Y,           U          },
40279371c9d4SSatish Balay         {Q,           P,           W,           Y          },
40289371c9d4SSatish Balay         {R,           O + 56 * 1,  U + 56 * 1,  Ap         },
40299371c9d4SSatish Balay         {P,           R,           Ap,          W          },
40309371c9d4SSatish Balay         {S,           P,           X,           Bp         },
40319371c9d4SSatish Balay         {O + 56 * 3,  S,           Bp,          V + 56 * 1 },
40329371c9d4SSatish Balay         {T,           O + 56 * 2,  V,           Z          },
40339371c9d4SSatish Balay         {P,           T,           Z,           X          },
4034b7f5c055SJed Brown         /* layer 3 */
40359371c9d4SSatish Balay         {U,           Y,           Ep,          Dp         },
40369371c9d4SSatish Balay         {Y,           W,           Cp,          Ep         },
40379371c9d4SSatish Balay         {Ap,          U + 56 * 1,  Dp + 56 * 1, Gp         },
40389371c9d4SSatish Balay         {W,           Ap,          Gp,          Cp         },
40399371c9d4SSatish Balay         {Bp,          X,           Cp + 56 * 2, Fp         },
40409371c9d4SSatish Balay         {V + 56 * 1,  Bp,          Fp,          Dp + 56 * 1},
40419371c9d4SSatish Balay         {Z,           V,           Dp,          Hp         },
40429371c9d4SSatish Balay         {X,           Z,           Hp,          Cp + 56 * 2},
4043b7f5c055SJed Brown         /* layer 4 */
40449371c9d4SSatish Balay         {Dp,          Ep,          Mp,          Kp         },
40459371c9d4SSatish Balay         {Ep,          Cp,          Ip,          Mp         },
40469371c9d4SSatish Balay         {Gp,          Dp + 56 * 1, Lp,          Np         },
40479371c9d4SSatish Balay         {Cp,          Gp,          Np,          Jp         },
40489371c9d4SSatish Balay         {Fp,          Cp + 56 * 2, Jp + 56 * 2, Pp         },
40499371c9d4SSatish Balay         {Dp + 56 * 1, Fp,          Pp,          Lp         },
40509371c9d4SSatish Balay         {Hp,          Dp,          Kp,          Op         },
40519371c9d4SSatish Balay         {Cp + 56 * 2, Hp,          Op,          Ip + 56 * 2},
4052b7f5c055SJed Brown         /* layer 5 */
40539371c9d4SSatish Balay         {Kp,          Mp,          Sp,          Rp         },
40549371c9d4SSatish Balay         {Mp,          Ip,          Qp,          Sp         },
40559371c9d4SSatish Balay         {Np,          Lp,          Rp,          Tp         },
40569371c9d4SSatish Balay         {Jp,          Np,          Tp,          Qp + 56 * 1},
40579371c9d4SSatish Balay         {Pp,          Jp + 56 * 2, Qp + 56 * 3, Up         },
40589371c9d4SSatish Balay         {Lp,          Pp,          Up,          Rp         },
40599371c9d4SSatish Balay         {Op,          Kp,          Rp,          Vp         },
40609371c9d4SSatish Balay         {Ip + 56 * 2, Op,          Vp,          Qp + 56 * 2},
4061b7f5c055SJed Brown         /* layer 6 */
40629371c9d4SSatish Balay         {Rp,          Sp,          Aq,          Yp         },
40639371c9d4SSatish Balay         {Sp,          Qp,          Wp,          Aq         },
40649371c9d4SSatish Balay         {Tp,          Rp,          Yp,          Cq         },
40659371c9d4SSatish Balay         {Qp + 56 * 1, Tp,          Cq,          Wp + 56 * 1},
40669371c9d4SSatish Balay         {Up,          Qp + 56 * 3, Xp + 56 * 1, Dq         },
40679371c9d4SSatish Balay         {Rp,          Up,          Dq,          Zp         },
40689371c9d4SSatish Balay         {Vp,          Rp,          Zp,          Bq         },
40699371c9d4SSatish Balay         {Qp + 56 * 2, Vp,          Bq,          Xp         },
4070b7f5c055SJed Brown         /* layer 7 (the top is the periodic image of the bottom of layer 0) */
40719371c9d4SSatish Balay         {Yp,          Aq,          C + 56 * 4,  A + 56 * 4 },
40729371c9d4SSatish Balay         {Aq,          Wp,          B + 56 * 4,  C + 56 * 4 },
40739371c9d4SSatish Balay         {Cq,          Yp,          A + 56 * 4,  D + 56 * 4 },
40749371c9d4SSatish Balay         {Wp + 56 * 1, Cq,          D + 56 * 4,  B + 56 * 5 },
40759371c9d4SSatish Balay         {Dq,          Xp + 56 * 1, B + 56 * 5,  E + 56 * 4 },
40769371c9d4SSatish Balay         {Zp,          Dq,          E + 56 * 4,  A + 56 * 6 },
40779371c9d4SSatish Balay         {Bq,          Zp,          A + 56 * 6,  F + 56 * 4 },
40789371c9d4SSatish Balay         {Xp,          Bq,          F + 56 * 4,  B + 56 * 4 }
4079b7f5c055SJed Brown       };
4080b7f5c055SJed Brown       const PetscReal gamma                = PetscAcosReal((PetscSqrtReal(3.) - 1.) / PetscSqrtReal(2.)) / PETSC_PI;
40819371c9d4SSatish Balay       const PetscReal patternCoords[56][3] = {
4082bee3fc89SBarry Smith         {1.,        0.,        0.  }, /* A  */
4083bee3fc89SBarry Smith         {0.,        1.,        0.  }, /* B  */
4084bee3fc89SBarry Smith         {gamma,     gamma,     0.  }, /* C  */
4085bee3fc89SBarry Smith         {1 + gamma, 1 - gamma, 0.  }, /* D  */
4086bee3fc89SBarry Smith         {2 - gamma, 2 - gamma, 0.  }, /* E  */
4087bee3fc89SBarry Smith         {1 - gamma, 1 + gamma, 0.  }, /* F  */
4088b7f5c055SJed Brown 
4089bee3fc89SBarry Smith         {.5,        0,         .25 }, /* G  */
4090bee3fc89SBarry Smith         {1.5,       0.,        .25 }, /* H  */
4091bee3fc89SBarry Smith         {.5,        1.,        .25 }, /* II */
4092bee3fc89SBarry Smith         {1.5,       1.,        .25 }, /* J  */
4093bee3fc89SBarry Smith         {.25,       .5,        .25 }, /* K  */
4094bee3fc89SBarry Smith         {1.25,      .5,        .25 }, /* L  */
4095bee3fc89SBarry Smith         {.75,       1.5,       .25 }, /* M  */
4096bee3fc89SBarry Smith         {1.75,      1.5,       .25 }, /* N  */
4097b7f5c055SJed Brown 
4098bee3fc89SBarry Smith         {0.,        0.,        .5  }, /* O  */
4099bee3fc89SBarry Smith         {1.,        1.,        .5  }, /* P  */
4100bee3fc89SBarry Smith         {gamma,     1 - gamma, .5  }, /* Q  */
4101bee3fc89SBarry Smith         {1 + gamma, gamma,     .5  }, /* R  */
4102bee3fc89SBarry Smith         {2 - gamma, 1 + gamma, .5  }, /* S  */
4103bee3fc89SBarry Smith         {1 - gamma, 2 - gamma, .5  }, /* T  */
4104b7f5c055SJed Brown 
4105bee3fc89SBarry Smith         {0.,        .5,        .75 }, /* U  */
4106bee3fc89SBarry Smith         {0.,        1.5,       .75 }, /* V  */
4107bee3fc89SBarry Smith         {1.,        .5,        .75 }, /* W  */
4108bee3fc89SBarry Smith         {1.,        1.5,       .75 }, /* X  */
4109bee3fc89SBarry Smith         {.5,        .75,       .75 }, /* Y  */
4110bee3fc89SBarry Smith         {.5,        1.75,      .75 }, /* Z  */
4111bee3fc89SBarry Smith         {1.5,       .25,       .75 }, /* Ap */
4112bee3fc89SBarry Smith         {1.5,       1.25,      .75 }, /* Bp */
4113b7f5c055SJed Brown 
4114bee3fc89SBarry Smith         {1.,        0.,        1.  }, /* Cp */
4115bee3fc89SBarry Smith         {0.,        1.,        1.  }, /* Dp */
4116bee3fc89SBarry Smith         {1 - gamma, 1 - gamma, 1.  }, /* Ep */
4117bee3fc89SBarry Smith         {1 + gamma, 1 + gamma, 1.  }, /* Fp */
4118bee3fc89SBarry Smith         {2 - gamma, gamma,     1.  }, /* Gp */
4119bee3fc89SBarry Smith         {gamma,     2 - gamma, 1.  }, /* Hp */
4120b7f5c055SJed Brown 
4121bee3fc89SBarry Smith         {.5,        0.,        1.25}, /* Ip */
4122bee3fc89SBarry Smith         {1.5,       0.,        1.25}, /* Jp */
4123bee3fc89SBarry Smith         {.5,        1.,        1.25}, /* Kp */
4124bee3fc89SBarry Smith         {1.5,       1.,        1.25}, /* Lp */
4125bee3fc89SBarry Smith         {.75,       .5,        1.25}, /* Mp */
4126bee3fc89SBarry Smith         {1.75,      .5,        1.25}, /* Np */
4127bee3fc89SBarry Smith         {.25,       1.5,       1.25}, /* Op */
4128bee3fc89SBarry Smith         {1.25,      1.5,       1.25}, /* Pp */
4129b7f5c055SJed Brown 
4130bee3fc89SBarry Smith         {0.,        0.,        1.5 }, /* Qp */
4131bee3fc89SBarry Smith         {1.,        1.,        1.5 }, /* Rp */
4132bee3fc89SBarry Smith         {1 - gamma, gamma,     1.5 }, /* Sp */
4133bee3fc89SBarry Smith         {2 - gamma, 1 - gamma, 1.5 }, /* Tp */
4134bee3fc89SBarry Smith         {1 + gamma, 2 - gamma, 1.5 }, /* Up */
4135bee3fc89SBarry Smith         {gamma,     1 + gamma, 1.5 }, /* Vp */
4136b7f5c055SJed Brown 
4137bee3fc89SBarry Smith         {0.,        .5,        1.75}, /* Wp */
4138bee3fc89SBarry Smith         {0.,        1.5,       1.75}, /* Xp */
4139bee3fc89SBarry Smith         {1.,        .5,        1.75}, /* Yp */
4140bee3fc89SBarry Smith         {1.,        1.5,       1.75}, /* Zp */
4141bee3fc89SBarry Smith         {.5,        .25,       1.75}, /* Aq */
4142bee3fc89SBarry Smith         {.5,        1.25,      1.75}, /* Bq */
4143bee3fc89SBarry Smith         {1.5,       .75,       1.75}, /* Cq */
4144bee3fc89SBarry Smith         {1.5,       1.75,      1.75}, /* Dq */
4145b7f5c055SJed Brown       };
4146b7f5c055SJed Brown       PetscInt (*cells)[64][4] = NULL;
4147b7f5c055SJed Brown       PetscBool *seen;
4148b7f5c055SJed Brown       PetscInt  *vertToTrueVert;
4149b7f5c055SJed Brown       PetscInt   count;
4150b7f5c055SJed Brown 
4151b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) extentPlus[i] = extent[i] + 1;
4152b7f5c055SJed Brown       numBlocks = 1;
4153b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) numBlocks *= extent[i];
4154b7f5c055SJed Brown       numBlocksPlus = 1;
4155b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) numBlocksPlus *= extentPlus[i];
4156b7f5c055SJed Brown       numFaces = numBlocks * facesPerBlock;
41579566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numBlocks, &cells));
41589566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numBlocksPlus * vertsPerBlock, &seen));
4159b7f5c055SJed Brown       for (PetscInt k = 0; k < extent[2]; k++) {
4160b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
4161b7f5c055SJed Brown           for (PetscInt i = 0; i < extent[0]; i++) {
4162b7f5c055SJed Brown             for (PetscInt f = 0; f < facesPerBlock; f++) {
4163b7f5c055SJed Brown               for (PetscInt v = 0; v < 4; v++) {
4164b7f5c055SJed Brown                 PetscInt vertRaw     = pattern[f][v];
4165b7f5c055SJed Brown                 PetscInt blockidx    = vertRaw / 56;
4166b7f5c055SJed Brown                 PetscInt patternvert = vertRaw % 56;
4167b7f5c055SJed Brown                 PetscInt xplus       = (blockidx & 1);
4168b7f5c055SJed Brown                 PetscInt yplus       = (blockidx & 2) >> 1;
4169b7f5c055SJed Brown                 PetscInt zplus       = (blockidx & 4) >> 2;
4170b7f5c055SJed Brown                 PetscInt zcoord      = (periodic && periodic[2] == DM_BOUNDARY_PERIODIC) ? ((k + zplus) % extent[2]) : (k + zplus);
4171b7f5c055SJed Brown                 PetscInt ycoord      = (periodic && periodic[1] == DM_BOUNDARY_PERIODIC) ? ((j + yplus) % extent[1]) : (j + yplus);
4172b7f5c055SJed Brown                 PetscInt xcoord      = (periodic && periodic[0] == DM_BOUNDARY_PERIODIC) ? ((i + xplus) % extent[0]) : (i + xplus);
4173b7f5c055SJed Brown                 PetscInt vert        = ((zcoord * extentPlus[1] + ycoord) * extentPlus[0] + xcoord) * 56 + patternvert;
4174b7f5c055SJed Brown 
4175b7f5c055SJed Brown                 cells[(k * extent[1] + j) * extent[0] + i][f][v] = vert;
4176b7f5c055SJed Brown                 seen[vert]                                       = PETSC_TRUE;
4177b7f5c055SJed Brown               }
4178b7f5c055SJed Brown             }
4179b7f5c055SJed Brown           }
4180b7f5c055SJed Brown         }
4181b7f5c055SJed Brown       }
41829371c9d4SSatish Balay       for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++)
41839371c9d4SSatish Balay         if (seen[i]) numVertices++;
4184b7f5c055SJed Brown       count = 0;
41859566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numBlocksPlus * vertsPerBlock, &vertToTrueVert));
41869566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numVertices * 3, &vtxCoords));
4187b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++) vertToTrueVert[i] = -1;
4188b7f5c055SJed Brown       for (PetscInt k = 0; k < extentPlus[2]; k++) {
4189b7f5c055SJed Brown         for (PetscInt j = 0; j < extentPlus[1]; j++) {
4190b7f5c055SJed Brown           for (PetscInt i = 0; i < extentPlus[0]; i++) {
4191b7f5c055SJed Brown             for (PetscInt v = 0; v < vertsPerBlock; v++) {
4192b7f5c055SJed Brown               PetscInt vIdx = ((k * extentPlus[1] + j) * extentPlus[0] + i) * vertsPerBlock + v;
4193b7f5c055SJed Brown 
4194b7f5c055SJed Brown               if (seen[vIdx]) {
4195b7f5c055SJed Brown                 PetscInt thisVert;
4196b7f5c055SJed Brown 
4197b7f5c055SJed Brown                 vertToTrueVert[vIdx] = thisVert = count++;
4198b7f5c055SJed Brown 
4199b7f5c055SJed Brown                 for (PetscInt d = 0; d < 3; d++) vtxCoords[3 * thisVert + d] = patternCoords[v][d];
4200b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 0] += i * 2;
4201b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 1] += j * 2;
4202b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 2] += k * 2;
4203b7f5c055SJed Brown               }
4204b7f5c055SJed Brown             }
4205b7f5c055SJed Brown           }
4206b7f5c055SJed Brown         }
4207b7f5c055SJed Brown       }
4208b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocks; i++) {
4209b7f5c055SJed Brown         for (PetscInt f = 0; f < facesPerBlock; f++) {
4210ad540459SPierre Jolivet           for (PetscInt v = 0; v < 4; v++) cells[i][f][v] = vertToTrueVert[cells[i][f][v]];
4211b7f5c055SJed Brown         }
4212b7f5c055SJed Brown       }
42139566063dSJacob Faibussowitsch       PetscCall(PetscFree(vertToTrueVert));
42149566063dSJacob Faibussowitsch       PetscCall(PetscFree(seen));
4215b7f5c055SJed Brown       cells_flat = cells[0][0];
4216b7f5c055SJed Brown       numEdges   = 0;
4217b7f5c055SJed Brown       for (PetscInt i = 0; i < numFaces; i++) {
4218b7f5c055SJed Brown         for (PetscInt e = 0; e < 4; e++) {
4219b7f5c055SJed Brown           PetscInt         ev[]       = {cells_flat[i * 4 + e], cells_flat[i * 4 + ((e + 1) % 4)]};
4220b7f5c055SJed Brown           const PetscReal *evCoords[] = {&vtxCoords[3 * ev[0]], &vtxCoords[3 * ev[1]]};
4221b7f5c055SJed Brown 
4222b7f5c055SJed Brown           for (PetscInt d = 0; d < 3; d++) {
4223b7f5c055SJed Brown             if (!periodic || periodic[0] != DM_BOUNDARY_PERIODIC) {
4224b7f5c055SJed Brown               if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) numEdges++;
4225b7f5c055SJed Brown               if (evCoords[0][d] == 2. * extent[d] && evCoords[1][d] == 2. * extent[d]) numEdges++;
4226b7f5c055SJed Brown             }
4227b7f5c055SJed Brown           }
4228b7f5c055SJed Brown         }
4229b7f5c055SJed Brown       }
42309566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numEdges, &edges));
42319566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numEdges, &edgeSets));
4232b7f5c055SJed Brown       for (PetscInt edge = 0, i = 0; i < numFaces; i++) {
4233b7f5c055SJed Brown         for (PetscInt e = 0; e < 4; e++) {
4234b7f5c055SJed Brown           PetscInt         ev[]       = {cells_flat[i * 4 + e], cells_flat[i * 4 + ((e + 1) % 4)]};
4235b7f5c055SJed Brown           const PetscReal *evCoords[] = {&vtxCoords[3 * ev[0]], &vtxCoords[3 * ev[1]]};
4236b7f5c055SJed Brown 
4237b7f5c055SJed Brown           for (PetscInt d = 0; d < 3; d++) {
4238b7f5c055SJed Brown             if (!periodic || periodic[d] != DM_BOUNDARY_PERIODIC) {
4239b7f5c055SJed Brown               if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) {
4240b7f5c055SJed Brown                 edges[edge][0]   = ev[0];
4241b7f5c055SJed Brown                 edges[edge][1]   = ev[1];
4242b7f5c055SJed Brown                 edgeSets[edge++] = 2 * d;
4243b7f5c055SJed Brown               }
4244b7f5c055SJed Brown               if (evCoords[0][d] == 2. * extent[d] && evCoords[1][d] == 2. * extent[d]) {
4245b7f5c055SJed Brown                 edges[edge][0]   = ev[0];
4246b7f5c055SJed Brown                 edges[edge][1]   = ev[1];
4247b7f5c055SJed Brown                 edgeSets[edge++] = 2 * d + 1;
4248b7f5c055SJed Brown               }
4249b7f5c055SJed Brown             }
4250b7f5c055SJed Brown           }
4251b7f5c055SJed Brown         }
4252b7f5c055SJed Brown       }
4253b7f5c055SJed Brown     }
4254b7f5c055SJed Brown     evalFunc   = TPSEvaluate_Gyroid;
42554663dae6SJed Brown     normalFunc = TPSExtrudeNormalFunc_Gyroid;
4256b7f5c055SJed Brown     break;
4257b7f5c055SJed Brown   }
4258b7f5c055SJed Brown 
42599566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, topoDim));
4260c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildFromCellList(dm, numFaces, numVertices, 4, cells_flat));
42619566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildFromCellList(dm, 0, 0, 0, NULL));
42629566063dSJacob Faibussowitsch   PetscCall(PetscFree(cells_flat));
4263b7f5c055SJed Brown   {
4264b7f5c055SJed Brown     DM idm;
42659566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(dm, &idm));
426669d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &idm));
4267b7f5c055SJed Brown   }
4268c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildCoordinatesFromCellList(dm, spaceDim, vtxCoords));
42699566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildCoordinatesFromCellList(dm, spaceDim, NULL));
42709566063dSJacob Faibussowitsch   PetscCall(PetscFree(vtxCoords));
4271b7f5c055SJed Brown 
42729566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
42739566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "Face Sets", &label));
4274b7f5c055SJed Brown   for (PetscInt e = 0; e < numEdges; e++) {
4275b7f5c055SJed Brown     PetscInt        njoin;
4276b7f5c055SJed Brown     const PetscInt *join, verts[] = {numFaces + edges[e][0], numFaces + edges[e][1]};
42779566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, 2, verts, &njoin, &join));
427863a3b9bcSJacob 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]);
42799566063dSJacob Faibussowitsch     PetscCall(DMLabelSetValue(label, join[0], edgeSets[e]));
42809566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, 2, verts, &njoin, &join));
4281b7f5c055SJed Brown   }
42829566063dSJacob Faibussowitsch   PetscCall(PetscFree(edges));
42839566063dSJacob Faibussowitsch   PetscCall(PetscFree(edgeSets));
42841436d7faSJed Brown   if (tps_distribute) {
42851436d7faSJed Brown     DM               pdm = NULL;
42861436d7faSJed Brown     PetscPartitioner part;
42871436d7faSJed Brown 
42889566063dSJacob Faibussowitsch     PetscCall(DMPlexGetPartitioner(dm, &part));
42899566063dSJacob Faibussowitsch     PetscCall(PetscPartitionerSetFromOptions(part));
42909566063dSJacob Faibussowitsch     PetscCall(DMPlexDistribute(dm, 0, NULL, &pdm));
429148a46eb9SPierre Jolivet     if (pdm) PetscCall(DMPlexReplace_Internal(dm, &pdm));
42921436d7faSJed Brown     // Do not auto-distribute again
42939566063dSJacob Faibussowitsch     PetscCall(DMPlexDistributeSetDefault(dm, PETSC_FALSE));
42941436d7faSJed Brown   }
4295b7f5c055SJed Brown 
42969566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
4297b7f5c055SJed Brown   for (PetscInt refine = 0; refine < refinements; refine++) {
4298b7f5c055SJed Brown     PetscInt     m;
4299f1d4225fSZach Atkins     DM           dmf, cdm, cdmf;
4300b7f5c055SJed Brown     Vec          X;
4301b7f5c055SJed Brown     PetscScalar *x;
4302f1d4225fSZach Atkins 
43039566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, MPI_COMM_NULL, &dmf));
4304f1d4225fSZach Atkins     PetscCall(DMGetCoordinateDM(dm, &cdm));
4305f1d4225fSZach Atkins     PetscCall(DMGetCoordinateDM(dmf, &cdmf));
4306f1d4225fSZach Atkins     PetscCall(DMCopyDisc(cdm, cdmf));
430769d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &dmf));
4308b7f5c055SJed Brown 
43099566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &X));
43109566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(X, &m));
43119566063dSJacob Faibussowitsch     PetscCall(VecGetArray(X, &x));
431248a46eb9SPierre Jolivet     for (PetscInt i = 0; i < m; i += 3) PetscCall(TPSNearestPoint(evalFunc, &x[i]));
43139566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(X, &x));
4314b7f5c055SJed Brown   }
4315b7f5c055SJed Brown 
4316b7f5c055SJed Brown   // Face Sets has already been propagated to new vertices during refinement; this propagates to the initial vertices.
43179566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "Face Sets", &label));
43189566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete(dm, label));
4319b7f5c055SJed Brown 
432046139095SJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_Generate, dm, 0, 0, 0));
432146139095SJed Brown 
4322b7f5c055SJed Brown   if (thickness > 0) {
43234663dae6SJed Brown     DM              edm, cdm, ecdm;
43244663dae6SJed Brown     DMPlexTransform tr;
43254663dae6SJed Brown     const char     *prefix;
43264663dae6SJed Brown     PetscOptions    options;
43274663dae6SJed Brown     // Code from DMPlexExtrude
43284663dae6SJed Brown     PetscCall(DMPlexTransformCreate(PetscObjectComm((PetscObject)dm), &tr));
43294663dae6SJed Brown     PetscCall(DMPlexTransformSetDM(tr, dm));
4330ce78bad3SBarry Smith     PetscCall(DMPlexTransformSetType(tr, DMPLEXEXTRUDETYPE));
43314663dae6SJed Brown     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, &prefix));
43324663dae6SJed Brown     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)tr, prefix));
43334663dae6SJed Brown     PetscCall(PetscObjectGetOptions((PetscObject)dm, &options));
43344663dae6SJed Brown     PetscCall(PetscObjectSetOptions((PetscObject)tr, options));
43354663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetLayers(tr, layers));
43364663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetThickness(tr, thickness));
43374663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetTensor(tr, PETSC_FALSE));
43384663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetSymmetric(tr, PETSC_TRUE));
43394663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetNormalFunction(tr, normalFunc));
43404663dae6SJed Brown     PetscCall(DMPlexTransformSetFromOptions(tr));
43414663dae6SJed Brown     PetscCall(PetscObjectSetOptions((PetscObject)tr, NULL));
43424663dae6SJed Brown     PetscCall(DMPlexTransformSetUp(tr));
43434663dae6SJed Brown     PetscCall(PetscObjectViewFromOptions((PetscObject)tr, NULL, "-dm_plex_tps_transform_view"));
43444663dae6SJed Brown     PetscCall(DMPlexTransformApply(tr, dm, &edm));
43454663dae6SJed Brown     PetscCall(DMCopyDisc(dm, edm));
43464663dae6SJed Brown     PetscCall(DMGetCoordinateDM(dm, &cdm));
43474663dae6SJed Brown     PetscCall(DMGetCoordinateDM(edm, &ecdm));
43484663dae6SJed Brown     PetscCall(DMCopyDisc(cdm, ecdm));
43494663dae6SJed Brown     PetscCall(DMPlexTransformCreateDiscLabels(tr, edm));
43504663dae6SJed Brown     PetscCall(DMPlexTransformDestroy(&tr));
4351a77a5016SMatthew G. Knepley     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, edm));
435269d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &edm));
4353b7f5c055SJed Brown   }
43543ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4355b7f5c055SJed Brown }
4356b7f5c055SJed Brown 
4357b7f5c055SJed Brown /*@
4358b7f5c055SJed Brown   DMPlexCreateTPSMesh - Create a distributed, interpolated mesh of a triply-periodic surface
4359b7f5c055SJed Brown 
4360b7f5c055SJed Brown   Collective
4361b7f5c055SJed Brown 
4362b7f5c055SJed Brown   Input Parameters:
4363a1cb98faSBarry Smith + comm           - The communicator for the `DM` object
4364b7f5c055SJed Brown . tpstype        - Type of triply-periodic surface
4365b7f5c055SJed Brown . extent         - Array of length 3 containing number of periods in each direction
436620f4b53cSBarry Smith . periodic       - array of length 3 with periodicity, or `NULL` for non-periodic
43671436d7faSJed Brown . tps_distribute - Distribute 2D manifold mesh prior to refinement and extrusion (more scalable)
4368817da375SSatish Balay . refinements    - Number of factor-of-2 refinements of 2D manifold mesh
43691436d7faSJed Brown . layers         - Number of cell layers extruded in normal direction
4370817da375SSatish Balay - thickness      - Thickness in normal direction
4371b7f5c055SJed Brown 
4372b7f5c055SJed Brown   Output Parameter:
4373a1cb98faSBarry Smith . dm - The `DM` object
4374a1cb98faSBarry Smith 
4375a1cb98faSBarry Smith   Level: beginner
4376b7f5c055SJed Brown 
4377b7f5c055SJed Brown   Notes:
437815229ffcSPierre Jolivet   This meshes the surface of the Schwarz P or Gyroid surfaces.  Schwarz P is the simplest member of the triply-periodic minimal surfaces.
43791d27aa22SBarry Smith   <https://en.wikipedia.org/wiki/Schwarz_minimal_surface#Schwarz_P_(%22Primitive%22)> and can be cut with "clean" boundaries.
43801d27aa22SBarry 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.
4381b7f5c055SJed Brown   Our implementation creates a very coarse mesh of the surface and refines (by 4-way splitting) as many times as requested.
4382b7f5c055SJed Brown   On each refinement, all vertices are projected to their nearest point on the surface.
4383b7f5c055SJed Brown   This projection could readily be extended to related surfaces.
4384b7f5c055SJed Brown 
43851d27aa22SBarry Smith   See {cite}`maskery2018insights`
43861d27aa22SBarry Smith 
43871d27aa22SBarry Smith   The face (edge) sets for the Schwarz P surface are numbered $1(-x), 2(+x), 3(-y), 4(+y), 5(-z), 6(+z)$.
43881d27aa22SBarry Smith   When the mesh is refined, "Face Sets" contain the new vertices (created during refinement).
43891d27aa22SBarry Smith   Use `DMPlexLabelComplete()` to propagate to coarse-level vertices.
4390b7f5c055SJed Brown 
439160225df5SJacob Faibussowitsch   Developer Notes:
4392b7f5c055SJed Brown   The Gyroid mesh does not currently mark boundary sets.
4393b7f5c055SJed Brown 
43941cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSphereMesh()`, `DMSetType()`, `DMCreate()`
4395b7f5c055SJed Brown @*/
4396d71ae5a4SJacob 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)
4397d71ae5a4SJacob Faibussowitsch {
4398b7f5c055SJed Brown   PetscFunctionBegin;
43999566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
44009566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
44019566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateTPSMesh_Internal(*dm, tpstype, extent, periodic, tps_distribute, refinements, layers, thickness));
44023ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
4403b7f5c055SJed Brown }
4404b7f5c055SJed Brown 
44059318fe57SMatthew G. Knepley /*@
44069318fe57SMatthew G. Knepley   DMPlexCreateSphereMesh - Creates a mesh on the d-dimensional sphere, S^d.
44079318fe57SMatthew G. Knepley 
44089318fe57SMatthew G. Knepley   Collective
44099318fe57SMatthew G. Knepley 
44109318fe57SMatthew G. Knepley   Input Parameters:
4411a1cb98faSBarry Smith + comm    - The communicator for the `DM` object
44129318fe57SMatthew G. Knepley . dim     - The dimension
44139318fe57SMatthew G. Knepley . simplex - Use simplices, or tensor product cells
44149318fe57SMatthew G. Knepley - R       - The radius
44159318fe57SMatthew G. Knepley 
44169318fe57SMatthew G. Knepley   Output Parameter:
4417a1cb98faSBarry Smith . dm - The `DM` object
44189318fe57SMatthew G. Knepley 
44199318fe57SMatthew G. Knepley   Level: beginner
44209318fe57SMatthew G. Knepley 
44211cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBallMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
44229318fe57SMatthew G. Knepley @*/
4423d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateSphereMesh(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscReal R, DM *dm)
4424d71ae5a4SJacob Faibussowitsch {
44259318fe57SMatthew G. Knepley   PetscFunctionBegin;
44264f572ea9SToby Isaac   PetscAssertPointer(dm, 5);
44279566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
44289566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
44299566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSphereMesh_Internal(*dm, dim, simplex, R));
44303ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44319318fe57SMatthew G. Knepley }
44329318fe57SMatthew G. Knepley 
4433d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBallMesh_Internal(DM dm, PetscInt dim, PetscReal R)
4434d71ae5a4SJacob Faibussowitsch {
44359318fe57SMatthew G. Knepley   DM          sdm, vol;
44369318fe57SMatthew G. Knepley   DMLabel     bdlabel;
4437dd2b43ebSStefano Zampini   const char *prefix;
44389318fe57SMatthew G. Knepley 
44399318fe57SMatthew G. Knepley   PetscFunctionBegin;
44409566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &sdm));
44419566063dSJacob Faibussowitsch   PetscCall(DMSetType(sdm, DMPLEX));
4442dd2b43ebSStefano Zampini   PetscCall(DMGetOptionsPrefix(dm, &prefix));
4443dd2b43ebSStefano Zampini   PetscCall(DMSetOptionsPrefix(sdm, prefix));
4444dd2b43ebSStefano Zampini   PetscCall(DMAppendOptionsPrefix(sdm, "bd_"));
4445dd2b43ebSStefano Zampini   PetscCall(DMPlexDistributeSetDefault(sdm, PETSC_FALSE));
44469566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSphereMesh_Internal(sdm, dim - 1, PETSC_TRUE, R));
44479566063dSJacob Faibussowitsch   PetscCall(DMSetFromOptions(sdm));
44489566063dSJacob Faibussowitsch   PetscCall(DMViewFromOptions(sdm, NULL, "-dm_view"));
44499566063dSJacob Faibussowitsch   PetscCall(DMPlexGenerate(sdm, NULL, PETSC_TRUE, &vol));
44509566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&sdm));
445169d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &vol));
44529566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
44539566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "marker", &bdlabel));
44549566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkBoundaryFaces(dm, PETSC_DETERMINE, bdlabel));
44559566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete(dm, bdlabel));
44563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
445751a74b61SMatthew G. Knepley }
445851a74b61SMatthew G. Knepley 
445951a74b61SMatthew G. Knepley /*@
446051a74b61SMatthew G. Knepley   DMPlexCreateBallMesh - Creates a simplex mesh on the d-dimensional ball, B^d.
446151a74b61SMatthew G. Knepley 
446251a74b61SMatthew G. Knepley   Collective
446351a74b61SMatthew G. Knepley 
446451a74b61SMatthew G. Knepley   Input Parameters:
4465a1cb98faSBarry Smith + comm - The communicator for the `DM` object
446651a74b61SMatthew G. Knepley . dim  - The dimension
446751a74b61SMatthew G. Knepley - R    - The radius
446851a74b61SMatthew G. Knepley 
446951a74b61SMatthew G. Knepley   Output Parameter:
4470a1cb98faSBarry Smith . dm - The `DM` object
447151a74b61SMatthew G. Knepley 
4472a1cb98faSBarry Smith   Options Database Key:
447360225df5SJacob Faibussowitsch . bd_dm_refine - This will refine the surface mesh preserving the sphere geometry
447451a74b61SMatthew G. Knepley 
447551a74b61SMatthew G. Knepley   Level: beginner
447651a74b61SMatthew G. Knepley 
44771cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSphereMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
447851a74b61SMatthew G. Knepley @*/
4479d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateBallMesh(MPI_Comm comm, PetscInt dim, PetscReal R, DM *dm)
4480d71ae5a4SJacob Faibussowitsch {
448151a74b61SMatthew G. Knepley   PetscFunctionBegin;
44829566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
44839566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
44849566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBallMesh_Internal(*dm, dim, R));
44853ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
44862829fed8SMatthew G. Knepley }
44872829fed8SMatthew G. Knepley 
4488d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateReferenceCell_Internal(DM rdm, DMPolytopeType ct)
4489d71ae5a4SJacob Faibussowitsch {
44900a6ba040SMatthew G. Knepley   PetscFunctionBegin;
44919318fe57SMatthew G. Knepley   switch (ct) {
44929371c9d4SSatish Balay   case DM_POLYTOPE_POINT: {
44939318fe57SMatthew G. Knepley     PetscInt    numPoints[1]        = {1};
44949318fe57SMatthew G. Knepley     PetscInt    coneSize[1]         = {0};
44959318fe57SMatthew G. Knepley     PetscInt    cones[1]            = {0};
44969318fe57SMatthew G. Knepley     PetscInt    coneOrientations[1] = {0};
44979318fe57SMatthew G. Knepley     PetscScalar vertexCoords[1]     = {0.0};
44989318fe57SMatthew G. Knepley 
44999566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 0));
45009566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 0, numPoints, coneSize, cones, coneOrientations, vertexCoords));
45019371c9d4SSatish Balay   } break;
45029371c9d4SSatish Balay   case DM_POLYTOPE_SEGMENT: {
45039318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {2, 1};
45049318fe57SMatthew G. Knepley     PetscInt    coneSize[3]         = {2, 0, 0};
45059318fe57SMatthew G. Knepley     PetscInt    cones[2]            = {1, 2};
45069318fe57SMatthew G. Knepley     PetscInt    coneOrientations[2] = {0, 0};
45079318fe57SMatthew G. Knepley     PetscScalar vertexCoords[2]     = {-1.0, 1.0};
45089318fe57SMatthew G. Knepley 
45099566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 1));
45109566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
45119371c9d4SSatish Balay   } break;
45129371c9d4SSatish Balay   case DM_POLYTOPE_POINT_PRISM_TENSOR: {
4513b5a892a1SMatthew G. Knepley     PetscInt    numPoints[2]        = {2, 1};
4514b5a892a1SMatthew G. Knepley     PetscInt    coneSize[3]         = {2, 0, 0};
4515b5a892a1SMatthew G. Knepley     PetscInt    cones[2]            = {1, 2};
4516b5a892a1SMatthew G. Knepley     PetscInt    coneOrientations[2] = {0, 0};
4517b5a892a1SMatthew G. Knepley     PetscScalar vertexCoords[2]     = {-1.0, 1.0};
4518b5a892a1SMatthew G. Knepley 
45199566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 1));
45209566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
45219371c9d4SSatish Balay   } break;
45229371c9d4SSatish Balay   case DM_POLYTOPE_TRIANGLE: {
45239318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {3, 1};
45249318fe57SMatthew G. Knepley     PetscInt    coneSize[4]         = {3, 0, 0, 0};
45259318fe57SMatthew G. Knepley     PetscInt    cones[3]            = {1, 2, 3};
45269318fe57SMatthew G. Knepley     PetscInt    coneOrientations[3] = {0, 0, 0};
45279318fe57SMatthew G. Knepley     PetscScalar vertexCoords[6]     = {-1.0, -1.0, 1.0, -1.0, -1.0, 1.0};
45289318fe57SMatthew G. Knepley 
45299566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 2));
45309566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
45319371c9d4SSatish Balay   } break;
45329371c9d4SSatish Balay   case DM_POLYTOPE_QUADRILATERAL: {
45339318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {4, 1};
45349318fe57SMatthew G. Knepley     PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
45359318fe57SMatthew G. Knepley     PetscInt    cones[4]            = {1, 2, 3, 4};
45369318fe57SMatthew G. Knepley     PetscInt    coneOrientations[4] = {0, 0, 0, 0};
45379318fe57SMatthew G. Knepley     PetscScalar vertexCoords[8]     = {-1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0};
45389318fe57SMatthew G. Knepley 
45399566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 2));
45409566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
45419371c9d4SSatish Balay   } break;
45429371c9d4SSatish Balay   case DM_POLYTOPE_SEG_PRISM_TENSOR: {
45439318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {4, 1};
45449318fe57SMatthew G. Knepley     PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
45459318fe57SMatthew G. Knepley     PetscInt    cones[4]            = {1, 2, 3, 4};
45469318fe57SMatthew G. Knepley     PetscInt    coneOrientations[4] = {0, 0, 0, 0};
45479318fe57SMatthew G. Knepley     PetscScalar vertexCoords[8]     = {-1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0};
45489318fe57SMatthew G. Knepley 
45499566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 2));
45509566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
45519371c9d4SSatish Balay   } break;
45529371c9d4SSatish Balay   case DM_POLYTOPE_TETRAHEDRON: {
45539318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {4, 1};
45549318fe57SMatthew G. Knepley     PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
4555f0edb160SMatthew G. Knepley     PetscInt    cones[4]            = {1, 2, 3, 4};
45569318fe57SMatthew G. Knepley     PetscInt    coneOrientations[4] = {0, 0, 0, 0};
4557f0edb160SMatthew 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};
45589318fe57SMatthew G. Knepley 
45599566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
45609566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
45619371c9d4SSatish Balay   } break;
45629371c9d4SSatish Balay   case DM_POLYTOPE_HEXAHEDRON: {
45639318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {8, 1};
45649318fe57SMatthew G. Knepley     PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
4565f0edb160SMatthew G. Knepley     PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
45669318fe57SMatthew G. Knepley     PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
45679371c9d4SSatish 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};
45689318fe57SMatthew G. Knepley 
45699566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
45709566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
45719371c9d4SSatish Balay   } break;
45729371c9d4SSatish Balay   case DM_POLYTOPE_TRI_PRISM: {
45739318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {6, 1};
45749318fe57SMatthew G. Knepley     PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
4575f0edb160SMatthew G. Knepley     PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
45769318fe57SMatthew G. Knepley     PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
45779371c9d4SSatish 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};
45789318fe57SMatthew G. Knepley 
45799566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
45809566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
45819371c9d4SSatish Balay   } break;
45829371c9d4SSatish Balay   case DM_POLYTOPE_TRI_PRISM_TENSOR: {
45839318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {6, 1};
45849318fe57SMatthew G. Knepley     PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
45859318fe57SMatthew G. Knepley     PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
45869318fe57SMatthew G. Knepley     PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
45879371c9d4SSatish 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};
45889318fe57SMatthew G. Knepley 
45899566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
45909566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
45919371c9d4SSatish Balay   } break;
45929371c9d4SSatish Balay   case DM_POLYTOPE_QUAD_PRISM_TENSOR: {
45939318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {8, 1};
45949318fe57SMatthew G. Knepley     PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
45959318fe57SMatthew G. Knepley     PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
45969318fe57SMatthew G. Knepley     PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
45979371c9d4SSatish 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};
45989318fe57SMatthew G. Knepley 
45999566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
46009566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
46019371c9d4SSatish Balay   } break;
46029371c9d4SSatish Balay   case DM_POLYTOPE_PYRAMID: {
46039318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {5, 1};
46049318fe57SMatthew G. Knepley     PetscInt    coneSize[6]         = {5, 0, 0, 0, 0, 0};
4605f0edb160SMatthew G. Knepley     PetscInt    cones[5]            = {1, 2, 3, 4, 5};
46069318fe57SMatthew G. Knepley     PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
46079371c9d4SSatish 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};
46089318fe57SMatthew G. Knepley 
46099566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
46109566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
46119371c9d4SSatish Balay   } break;
4612d71ae5a4SJacob Faibussowitsch   default:
4613d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)rdm), PETSC_ERR_ARG_WRONG, "Cannot create reference cell for cell type %s", DMPolytopeTypes[ct]);
46149318fe57SMatthew G. Knepley   }
46159318fe57SMatthew G. Knepley   {
46169318fe57SMatthew G. Knepley     PetscInt Nv, v;
46179318fe57SMatthew G. Knepley 
46189318fe57SMatthew G. Knepley     /* Must create the celltype label here so that we do not automatically try to compute the types */
46199566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(rdm, "celltype"));
46209566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCellType(rdm, 0, ct));
46219566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(rdm, NULL, &Nv));
46229566063dSJacob Faibussowitsch     for (v = 1; v < Nv; ++v) PetscCall(DMPlexSetCellType(rdm, v, DM_POLYTOPE_POINT));
46239318fe57SMatthew G. Knepley   }
46249566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolateInPlace_Internal(rdm));
46259566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)rdm, DMPolytopeTypes[ct]));
46263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
46270a6ba040SMatthew G. Knepley }
46280a6ba040SMatthew G. Knepley 
46299318fe57SMatthew G. Knepley /*@
4630a1cb98faSBarry Smith   DMPlexCreateReferenceCell - Create a `DMPLEX` with the appropriate FEM reference cell
46319318fe57SMatthew G. Knepley 
46329318fe57SMatthew G. Knepley   Collective
46339318fe57SMatthew G. Knepley 
46349318fe57SMatthew G. Knepley   Input Parameters:
46359318fe57SMatthew G. Knepley + comm - The communicator
46369318fe57SMatthew G. Knepley - ct   - The cell type of the reference cell
46379318fe57SMatthew G. Knepley 
46389318fe57SMatthew G. Knepley   Output Parameter:
46399318fe57SMatthew G. Knepley . refdm - The reference cell
46409318fe57SMatthew G. Knepley 
46419318fe57SMatthew G. Knepley   Level: intermediate
46429318fe57SMatthew G. Knepley 
464342747ad1SJacob Faibussowitsch .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBoxMesh()`
46449318fe57SMatthew G. Knepley @*/
4645d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateReferenceCell(MPI_Comm comm, DMPolytopeType ct, DM *refdm)
4646d71ae5a4SJacob Faibussowitsch {
46470a6ba040SMatthew G. Knepley   PetscFunctionBegin;
46489566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, refdm));
46499566063dSJacob Faibussowitsch   PetscCall(DMSetType(*refdm, DMPLEX));
46509566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateReferenceCell_Internal(*refdm, ct));
46513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
46529318fe57SMatthew G. Knepley }
465379a015ccSMatthew G. Knepley 
4654d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoundaryLabel_Private(DM dm, const char name[])
4655d71ae5a4SJacob Faibussowitsch {
46569318fe57SMatthew G. Knepley   DM        plex;
46579318fe57SMatthew G. Knepley   DMLabel   label;
46589318fe57SMatthew G. Knepley   PetscBool hasLabel;
46590a6ba040SMatthew G. Knepley 
4660c22d3578SMatthew G. Knepley   PetscFunctionBegin;
46619566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &hasLabel));
46623ba16761SJacob Faibussowitsch   if (hasLabel) PetscFunctionReturn(PETSC_SUCCESS);
46639566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, name));
46649566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
46659566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
46669566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkBoundaryFaces(plex, 1, label));
46671c8afea9SMatthew G. Knepley   PetscCall(DMPlexLabelComplete(plex, label));
46689566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plex));
46693ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
46709318fe57SMatthew G. Knepley }
4671acdc6f61SToby Isaac 
4672669647acSMatthew G. Knepley /*
4673669647acSMatthew 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.
4674669647acSMatthew G. Knepley 
4675669647acSMatthew G. Knepley     (x, y) -> (r, theta) = (x[1], (x[0] - lower[0]) * 2\pi/(upper[0] - lower[0]))
4676669647acSMatthew G. Knepley */
4677d71ae5a4SJacob 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[])
4678d71ae5a4SJacob Faibussowitsch {
4679669647acSMatthew G. Knepley   const PetscReal low = PetscRealPart(constants[0]);
4680669647acSMatthew G. Knepley   const PetscReal upp = PetscRealPart(constants[1]);
4681669647acSMatthew G. Knepley   const PetscReal r   = PetscRealPart(u[1]);
4682669647acSMatthew G. Knepley   const PetscReal th  = 2. * PETSC_PI * (PetscRealPart(u[0]) - low) / (upp - low);
4683669647acSMatthew G. Knepley 
4684669647acSMatthew G. Knepley   f0[0] = r * PetscCosReal(th);
4685669647acSMatthew G. Knepley   f0[1] = r * PetscSinReal(th);
4686669647acSMatthew G. Knepley }
4687669647acSMatthew G. Knepley 
46885390be7dSMatthew G. Knepley // Insert vertices and their joins, marked by depth
46895390be7dSMatthew G. Knepley static PetscErrorCode ProcessCohesiveLabel_Vertices(DM dm, DMLabel label, DMLabel vlabel, PetscInt val, PetscInt n, const PetscInt vertices[])
46905390be7dSMatthew G. Knepley {
46915390be7dSMatthew G. Knepley   PetscFunctionBegin;
46925390be7dSMatthew G. Knepley   PetscCall(DMPlexMarkSubmesh_Interpolated(dm, vlabel, val, PETSC_FALSE, PETSC_FALSE, label, NULL));
46935390be7dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
46945390be7dSMatthew G. Knepley }
46955390be7dSMatthew G. Knepley 
46965390be7dSMatthew G. Knepley // Insert faces and their closures, marked by depth
46975390be7dSMatthew G. Knepley static PetscErrorCode ProcessCohesiveLabel_Faces(DM dm, DMLabel label, PetscInt n, const PetscInt faces[])
46985390be7dSMatthew G. Knepley {
46995390be7dSMatthew G. Knepley   PetscFunctionBegin;
47005390be7dSMatthew G. Knepley   for (PetscInt p = 0; p < n; ++p) {
47015390be7dSMatthew G. Knepley     const PetscInt point   = faces[p];
47025390be7dSMatthew G. Knepley     PetscInt      *closure = NULL;
47035390be7dSMatthew G. Knepley     PetscInt       clSize, pdepth;
47045390be7dSMatthew G. Knepley 
47055390be7dSMatthew G. Knepley     PetscCall(DMPlexGetPointDepth(dm, point, &pdepth));
47065390be7dSMatthew G. Knepley     PetscCall(DMLabelSetValue(label, point, pdepth));
47075390be7dSMatthew G. Knepley     PetscCall(DMPlexGetTransitiveClosure(dm, point, PETSC_TRUE, &clSize, &closure));
47085390be7dSMatthew G. Knepley     for (PetscInt cl = 0; cl < clSize * 2; cl += 2) {
47095390be7dSMatthew G. Knepley       PetscCall(DMPlexGetPointDepth(dm, closure[cl], &pdepth));
47105390be7dSMatthew G. Knepley       PetscCall(DMLabelSetValue(label, closure[cl], pdepth));
47115390be7dSMatthew G. Knepley     }
47125390be7dSMatthew G. Knepley     PetscCall(DMPlexRestoreTransitiveClosure(dm, point, PETSC_TRUE, &clSize, &closure));
47135390be7dSMatthew G. Knepley   }
47145390be7dSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
47155390be7dSMatthew G. Knepley }
47165390be7dSMatthew G. Knepley 
47174e22dd4cSMatthew G. Knepley PETSC_EXTERN PetscErrorCode PetscOptionsFindPairPrefix_Private(PetscOptions, const char pre[], const char name[], const char *option[], const char *value[], PetscBool *flg);
47184e22dd4cSMatthew G. Knepley 
47195dca41c3SJed Brown const char *const DMPlexShapes[] = {"box", "box_surface", "ball", "sphere", "cylinder", "schwarz_p", "gyroid", "doublet", "annulus", "hypercubic", "zbox", "unknown", "DMPlexShape", "DM_SHAPE_", NULL};
47209318fe57SMatthew G. Knepley 
4721ce78bad3SBarry Smith static PetscErrorCode DMPlexCreateFromOptions_Internal(PetscOptionItems PetscOptionsObject, PetscBool *useCoordSpace, DM dm)
4722d71ae5a4SJacob Faibussowitsch {
47239318fe57SMatthew G. Knepley   DMPlexShape    shape   = DM_SHAPE_BOX;
47249318fe57SMatthew G. Knepley   DMPolytopeType cell    = DM_POLYTOPE_TRIANGLE;
47259318fe57SMatthew G. Knepley   PetscInt       dim     = 2;
4726b9da1bb3SMatthew G. Knepley   PetscBool      simplex = PETSC_TRUE, interpolate = PETSC_TRUE, orient = PETSC_FALSE, adjCone = PETSC_FALSE, adjClosure = PETSC_TRUE, refDomain = PETSC_FALSE;
4727d0812dedSMatthew G. Knepley   PetscBool      flg, flg2, fflg, strflg, bdfflg, nameflg;
47289318fe57SMatthew G. Knepley   MPI_Comm       comm;
4729ed5e4e85SVaclav Hapla   char           filename[PETSC_MAX_PATH_LEN]   = "<unspecified>";
4730ed5e4e85SVaclav Hapla   char           bdFilename[PETSC_MAX_PATH_LEN] = "<unspecified>";
4731ed5e4e85SVaclav Hapla   char           plexname[PETSC_MAX_PATH_LEN]   = "";
47324e22dd4cSMatthew G. Knepley   const char    *option;
47339318fe57SMatthew G. Knepley 
47349318fe57SMatthew G. Knepley   PetscFunctionBegin;
4735708be2fdSJed Brown   PetscCall(PetscLogEventBegin(DMPLEX_CreateFromOptions, dm, 0, 0, 0));
47369566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
47379318fe57SMatthew G. Knepley   /* TODO Turn this into a registration interface */
47389566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_filename", "File containing a mesh", "DMPlexCreateFromFile", filename, filename, sizeof(filename), &fflg));
4739d0812dedSMatthew G. Knepley   PetscCall(PetscOptionsString("-dm_plex_file_contents", "Contents of a file format in a string", "DMPlexCreateFromFile", filename, filename, sizeof(filename), &strflg));
47409566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_boundary_filename", "File containing a mesh boundary", "DMPlexCreateFromFile", bdFilename, bdFilename, sizeof(bdFilename), &bdfflg));
47419566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_name", "Name of the mesh in the file", "DMPlexCreateFromFile", plexname, plexname, sizeof(plexname), &nameflg));
47429566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_plex_cell", "Cell shape", "", DMPolytopeTypes, (PetscEnum)cell, (PetscEnum *)&cell, NULL));
47439566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_reference_cell_domain", "Use a reference cell domain", "", refDomain, &refDomain, NULL));
47449566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_plex_shape", "Shape for built-in mesh", "", DMPlexShapes, (PetscEnum)shape, (PetscEnum *)&shape, &flg));
47459566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_dim", "Topological dimension of the mesh", "DMGetDimension", dim, &dim, &flg, 0));
47469566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_simplex", "Mesh cell shape", "", simplex, &simplex, &flg));
47479566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_interpolate", "Flag to create edges and faces automatically", "", interpolate, &interpolate, &flg));
4748b9da1bb3SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_plex_orient", "Orient the constructed mesh", "DMPlexOrient", orient, &orient, &flg));
47499566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_adj_cone", "Set adjacency direction", "DMSetBasicAdjacency", adjCone, &adjCone, &flg));
47509566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_adj_closure", "Set adjacency size", "DMSetBasicAdjacency", adjClosure, &adjClosure, &flg2));
47519566063dSJacob Faibussowitsch   if (flg || flg2) PetscCall(DMSetBasicAdjacency(dm, adjCone, adjClosure));
47523f3e541fSMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_adj", "Debug output level all adjacency computations", "", 0, &((DM_Plex *)dm->data)->printAdj, NULL, 0));
47539318fe57SMatthew G. Knepley 
475461a622f3SMatthew G. Knepley   switch (cell) {
475561a622f3SMatthew G. Knepley   case DM_POLYTOPE_POINT:
475661a622f3SMatthew G. Knepley   case DM_POLYTOPE_SEGMENT:
475761a622f3SMatthew G. Knepley   case DM_POLYTOPE_POINT_PRISM_TENSOR:
475861a622f3SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
475961a622f3SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
476061a622f3SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
4761d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_HEXAHEDRON:
4762d71ae5a4SJacob Faibussowitsch     *useCoordSpace = PETSC_TRUE;
4763d71ae5a4SJacob Faibussowitsch     break;
4764d71ae5a4SJacob Faibussowitsch   default:
4765d71ae5a4SJacob Faibussowitsch     *useCoordSpace = PETSC_FALSE;
4766d71ae5a4SJacob Faibussowitsch     break;
476761a622f3SMatthew G. Knepley   }
476861a622f3SMatthew G. Knepley 
47699318fe57SMatthew G. Knepley   if (fflg) {
47709318fe57SMatthew G. Knepley     DM          dmnew;
47711e4a82c4SMatthew G. Knepley     const char *name;
47729318fe57SMatthew G. Knepley 
47731e4a82c4SMatthew G. Knepley     PetscCall(PetscObjectGetName((PetscObject)dm, &name));
47741e4a82c4SMatthew G. Knepley     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), filename, nameflg ? plexname : name, interpolate, &dmnew));
47755de52c6dSVaclav Hapla     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
477669d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &dmnew));
47779318fe57SMatthew G. Knepley   } else if (refDomain) {
47789566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateReferenceCell_Internal(dm, cell));
47799318fe57SMatthew G. Knepley   } else if (bdfflg) {
47809318fe57SMatthew G. Knepley     DM          bdm, dmnew;
47811e4a82c4SMatthew G. Knepley     const char *name;
47829318fe57SMatthew G. Knepley 
47831e4a82c4SMatthew G. Knepley     PetscCall(PetscObjectGetName((PetscObject)dm, &name));
47841e4a82c4SMatthew G. Knepley     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), bdFilename, nameflg ? plexname : name, interpolate, &bdm));
47859566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)bdm, "bd_"));
47869566063dSJacob Faibussowitsch     PetscCall(DMSetFromOptions(bdm));
47879566063dSJacob Faibussowitsch     PetscCall(DMPlexGenerate(bdm, NULL, interpolate, &dmnew));
47889566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&bdm));
47895de52c6dSVaclav Hapla     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
479069d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &dmnew));
4791d0812dedSMatthew G. Knepley   } else if (strflg) {
4792d0812dedSMatthew G. Knepley     DM          dmnew;
4793d0812dedSMatthew G. Knepley     PetscViewer viewer;
4794d0812dedSMatthew G. Knepley     const char *contents;
4795d0812dedSMatthew G. Knepley     char       *strname;
4796d0812dedSMatthew G. Knepley     char        tmpdir[PETSC_MAX_PATH_LEN];
4797d0812dedSMatthew G. Knepley     char        tmpfilename[PETSC_MAX_PATH_LEN];
4798d0812dedSMatthew G. Knepley     char        name[PETSC_MAX_PATH_LEN];
4799d0812dedSMatthew G. Knepley     MPI_Comm    comm;
4800d0812dedSMatthew G. Knepley     PetscMPIInt rank;
4801d0812dedSMatthew G. Knepley 
4802d0812dedSMatthew G. Knepley     PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
4803d0812dedSMatthew G. Knepley     PetscCallMPI(MPI_Comm_rank(comm, &rank));
4804d0812dedSMatthew G. Knepley     PetscCall(PetscStrchr(filename, ':', &strname));
4805d0812dedSMatthew G. Knepley     PetscCheck(strname, comm, PETSC_ERR_ARG_WRONG, "File contents must have the form \"ext:string_name\", not %s", filename);
4806d0812dedSMatthew G. Knepley     strname[0] = '\0';
4807d0812dedSMatthew G. Knepley     ++strname;
4808d0812dedSMatthew G. Knepley     PetscCall(PetscDLSym(NULL, strname, (void **)&contents));
4809d0812dedSMatthew G. Knepley     PetscCheck(contents, comm, PETSC_ERR_ARG_WRONG, "Could not locate mesh string %s", strname);
4810d0812dedSMatthew G. Knepley     PetscCall(PetscGetTmp(comm, tmpdir, PETSC_MAX_PATH_LEN));
4811ed32af8cSMatthew G. Knepley     PetscCall(PetscStrlcat(tmpdir, "/meshXXXXXX", PETSC_MAX_PATH_LEN));
4812ed32af8cSMatthew G. Knepley     PetscCall(PetscMkdtemp(tmpdir));
4813ed32af8cSMatthew G. Knepley     PetscCall(PetscSNPrintf(tmpfilename, PETSC_MAX_PATH_LEN, "%s/mesh.%s", tmpdir, filename));
4814d0812dedSMatthew G. Knepley     PetscCall(PetscViewerASCIIOpen(comm, tmpfilename, &viewer));
4815d0812dedSMatthew G. Knepley     PetscCall(PetscViewerASCIIPrintf(viewer, "%s\n", contents));
4816d0812dedSMatthew G. Knepley     PetscCall(PetscViewerDestroy(&viewer));
4817d0812dedSMatthew G. Knepley     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), tmpfilename, plexname, interpolate, &dmnew));
4818ed32af8cSMatthew G. Knepley     PetscCall(PetscRMTree(tmpdir));
4819d0812dedSMatthew G. Knepley     PetscCall(PetscSNPrintf(name, PETSC_MAX_PATH_LEN, "%s Mesh", strname));
4820d0812dedSMatthew G. Knepley     PetscCall(PetscObjectSetName((PetscObject)dm, name));
4821d0812dedSMatthew G. Knepley     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
4822d0812dedSMatthew G. Knepley     PetscCall(DMPlexReplace_Internal(dm, &dmnew));
48239318fe57SMatthew G. Knepley   } else {
48249566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)dm, DMPlexShapes[shape]));
48259318fe57SMatthew G. Knepley     switch (shape) {
4826669647acSMatthew G. Knepley     case DM_SHAPE_BOX:
48275dca41c3SJed Brown     case DM_SHAPE_ZBOX:
4828669647acSMatthew G. Knepley     case DM_SHAPE_ANNULUS: {
48299318fe57SMatthew G. Knepley       PetscInt       faces[3]  = {0, 0, 0};
48309318fe57SMatthew G. Knepley       PetscReal      lower[3]  = {0, 0, 0};
48319318fe57SMatthew G. Knepley       PetscReal      upper[3]  = {1, 1, 1};
48329318fe57SMatthew G. Knepley       DMBoundaryType bdt[3]    = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
4833669647acSMatthew G. Knepley       PetscBool      isAnnular = shape == DM_SHAPE_ANNULUS ? PETSC_TRUE : PETSC_FALSE;
48349318fe57SMatthew G. Knepley       PetscInt       i, n;
48359318fe57SMatthew G. Knepley 
48369318fe57SMatthew G. Knepley       n = dim;
48379318fe57SMatthew G. Knepley       for (i = 0; i < dim; ++i) faces[i] = (dim == 1 ? 1 : 4 - dim);
48389566063dSJacob Faibussowitsch       PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg));
48399318fe57SMatthew G. Knepley       n = 3;
48409566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
484163a3b9bcSJacob Faibussowitsch       PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Lower box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
48429318fe57SMatthew G. Knepley       n = 3;
48439566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
484463a3b9bcSJacob Faibussowitsch       PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Upper box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
48459318fe57SMatthew G. Knepley       n = 3;
48469566063dSJacob Faibussowitsch       PetscCall(PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *)bdt, &n, &flg));
484763a3b9bcSJacob Faibussowitsch       PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Box boundary types had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
4848669647acSMatthew G. Knepley 
4849669647acSMatthew G. Knepley       PetscCheck(!isAnnular || dim == 2, comm, PETSC_ERR_ARG_OUTOFRANGE, "Only two dimensional annuli have been implemented");
4850669647acSMatthew G. Knepley       if (isAnnular)
4851669647acSMatthew G. Knepley         for (i = 0; i < dim - 1; ++i) bdt[i] = DM_BOUNDARY_PERIODIC;
4852669647acSMatthew G. Knepley 
48539318fe57SMatthew G. Knepley       switch (cell) {
485461a622f3SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
48559566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateWedgeBoxMesh_Internal(dm, faces, lower, upper, bdt));
4856d410b0cfSMatthew G. Knepley         if (!interpolate) {
4857d410b0cfSMatthew G. Knepley           DM udm;
4858d410b0cfSMatthew G. Knepley 
48599566063dSJacob Faibussowitsch           PetscCall(DMPlexUninterpolate(dm, &udm));
486069d8a87bSksagiyam           PetscCall(DMPlexReplace_Internal(dm, &udm));
4861d410b0cfSMatthew G. Knepley         }
48629318fe57SMatthew G. Knepley         break;
4863d71ae5a4SJacob Faibussowitsch       default:
48645dca41c3SJed Brown         PetscCall(DMPlexCreateBoxMesh_Internal(dm, shape, dim, simplex, faces, lower, upper, bdt, interpolate));
4865d71ae5a4SJacob Faibussowitsch         break;
48669318fe57SMatthew G. Knepley       }
4867669647acSMatthew G. Knepley       if (isAnnular) {
4868669647acSMatthew G. Knepley         DM          cdm;
4869669647acSMatthew G. Knepley         PetscDS     cds;
4870669647acSMatthew G. Knepley         PetscScalar bounds[2] = {lower[0], upper[0]};
4871669647acSMatthew G. Knepley 
4872669647acSMatthew G. Knepley         // Fix coordinates for annular region
4873669647acSMatthew G. Knepley         PetscCall(DMSetPeriodicity(dm, NULL, NULL, NULL));
4874669647acSMatthew G. Knepley         PetscCall(DMSetCellCoordinatesLocal(dm, NULL));
4875669647acSMatthew G. Knepley         PetscCall(DMSetCellCoordinates(dm, NULL));
4876e44f6aebSMatthew G. Knepley         PetscCall(DMPlexCreateCoordinateSpace(dm, 1, PETSC_TRUE, NULL));
4877669647acSMatthew G. Knepley         PetscCall(DMGetCoordinateDM(dm, &cdm));
4878669647acSMatthew G. Knepley         PetscCall(DMGetDS(cdm, &cds));
4879669647acSMatthew G. Knepley         PetscCall(PetscDSSetConstants(cds, 2, bounds));
4880669647acSMatthew G. Knepley         PetscCall(DMPlexRemapGeometry(dm, 0.0, boxToAnnulus));
4881669647acSMatthew G. Knepley       }
48829371c9d4SSatish Balay     } break;
48839371c9d4SSatish Balay     case DM_SHAPE_BOX_SURFACE: {
48849318fe57SMatthew G. Knepley       PetscInt  faces[3] = {0, 0, 0};
48859318fe57SMatthew G. Knepley       PetscReal lower[3] = {0, 0, 0};
48869318fe57SMatthew G. Knepley       PetscReal upper[3] = {1, 1, 1};
48879318fe57SMatthew G. Knepley       PetscInt  i, n;
48889318fe57SMatthew G. Knepley 
48899318fe57SMatthew G. Knepley       n = dim + 1;
48909318fe57SMatthew G. Knepley       for (i = 0; i < dim + 1; ++i) faces[i] = (dim + 1 == 1 ? 1 : 4 - (dim + 1));
48919566063dSJacob Faibussowitsch       PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg));
48929318fe57SMatthew G. Knepley       n = 3;
48939566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
489463a3b9bcSJacob 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);
48959318fe57SMatthew G. Knepley       n = 3;
48969566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
489763a3b9bcSJacob 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);
48989566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(dm, dim + 1, faces, lower, upper, interpolate));
48999371c9d4SSatish Balay     } break;
49009371c9d4SSatish Balay     case DM_SHAPE_SPHERE: {
49019318fe57SMatthew G. Knepley       PetscReal R = 1.0;
49029318fe57SMatthew G. Knepley 
49039566063dSJacob Faibussowitsch       PetscCall(PetscOptionsReal("-dm_plex_sphere_radius", "Radius of the sphere", "", R, &R, &flg));
49049566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateSphereMesh_Internal(dm, dim, simplex, R));
49059371c9d4SSatish Balay     } break;
49069371c9d4SSatish Balay     case DM_SHAPE_BALL: {
49079318fe57SMatthew G. Knepley       PetscReal R = 1.0;
49089318fe57SMatthew G. Knepley 
49099566063dSJacob Faibussowitsch       PetscCall(PetscOptionsReal("-dm_plex_ball_radius", "Radius of the ball", "", R, &R, &flg));
49109566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateBallMesh_Internal(dm, dim, R));
49119371c9d4SSatish Balay     } break;
49129371c9d4SSatish Balay     case DM_SHAPE_CYLINDER: {
49139318fe57SMatthew G. Knepley       DMBoundaryType bdt = DM_BOUNDARY_NONE;
49149318fe57SMatthew G. Knepley       PetscInt       Nw  = 6;
491549704ca5SMatthew G. Knepley       PetscInt       Nr  = 0;
49169318fe57SMatthew G. Knepley 
49179566063dSJacob Faibussowitsch       PetscCall(PetscOptionsEnum("-dm_plex_cylinder_bd", "Boundary type in the z direction", "", DMBoundaryTypes, (PetscEnum)bdt, (PetscEnum *)&bdt, NULL));
49189566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-dm_plex_cylinder_num_wedges", "Number of wedges around the cylinder", "", Nw, &Nw, NULL));
491949704ca5SMatthew G. Knepley       PetscCall(PetscOptionsInt("-dm_plex_cylinder_num_refine", "Number of refinements before projection", "", Nr, &Nr, NULL));
49209318fe57SMatthew G. Knepley       switch (cell) {
4921d71ae5a4SJacob Faibussowitsch       case DM_POLYTOPE_TRI_PRISM_TENSOR:
4922d71ae5a4SJacob Faibussowitsch         PetscCall(DMPlexCreateWedgeCylinderMesh_Internal(dm, Nw, interpolate));
4923d71ae5a4SJacob Faibussowitsch         break;
4924d71ae5a4SJacob Faibussowitsch       default:
492549704ca5SMatthew G. Knepley         PetscCall(DMPlexCreateHexCylinderMesh_Internal(dm, bdt, Nr));
4926d71ae5a4SJacob Faibussowitsch         break;
49279318fe57SMatthew G. Knepley       }
49289371c9d4SSatish Balay     } break;
4929b7f5c055SJed Brown     case DM_SHAPE_SCHWARZ_P: // fallthrough
49309371c9d4SSatish Balay     case DM_SHAPE_GYROID: {
4931b7f5c055SJed Brown       PetscInt       extent[3] = {1, 1, 1}, refine = 0, layers = 0, three;
4932b7f5c055SJed Brown       PetscReal      thickness   = 0.;
4933b7f5c055SJed Brown       DMBoundaryType periodic[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
4934b7f5c055SJed Brown       DMPlexTPSType  tps_type    = shape == DM_SHAPE_SCHWARZ_P ? DMPLEX_TPS_SCHWARZ_P : DMPLEX_TPS_GYROID;
49351436d7faSJed Brown       PetscBool      tps_distribute;
49369566063dSJacob Faibussowitsch       PetscCall(PetscOptionsIntArray("-dm_plex_tps_extent", "Number of replicas for each of three dimensions", NULL, extent, (three = 3, &three), NULL));
49379566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-dm_plex_tps_refine", "Number of refinements", NULL, refine, &refine, NULL));
49389566063dSJacob Faibussowitsch       PetscCall(PetscOptionsEnumArray("-dm_plex_tps_periodic", "Periodicity in each of three dimensions", NULL, DMBoundaryTypes, (PetscEnum *)periodic, (three = 3, &three), NULL));
49399566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-dm_plex_tps_layers", "Number of layers in volumetric extrusion (or zero to not extrude)", NULL, layers, &layers, NULL));
49409566063dSJacob Faibussowitsch       PetscCall(PetscOptionsReal("-dm_plex_tps_thickness", "Thickness of volumetric extrusion", NULL, thickness, &thickness, NULL));
49419566063dSJacob Faibussowitsch       PetscCall(DMPlexDistributeGetDefault(dm, &tps_distribute));
49429566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_tps_distribute", "Distribute the 2D mesh prior to refinement and extrusion", NULL, tps_distribute, &tps_distribute, NULL));
49439566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateTPSMesh_Internal(dm, tps_type, extent, periodic, tps_distribute, refine, layers, thickness));
49449371c9d4SSatish Balay     } break;
49459371c9d4SSatish Balay     case DM_SHAPE_DOUBLET: {
494605bd46c0SStefano Zampini       DM        dmnew;
494705bd46c0SStefano Zampini       PetscReal rl = 0.0;
494805bd46c0SStefano Zampini 
494905bd46c0SStefano Zampini       PetscCall(PetscOptionsReal("-dm_plex_doublet_refinementlimit", "Refinement limit", NULL, rl, &rl, NULL));
495005bd46c0SStefano Zampini       PetscCall(DMPlexCreateDoublet(PetscObjectComm((PetscObject)dm), dim, simplex, interpolate, rl, &dmnew));
49515de52c6dSVaclav Hapla       PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
495269d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &dmnew));
49539371c9d4SSatish Balay     } break;
4954cfb853baSMatthew G. Knepley     case DM_SHAPE_HYPERCUBIC: {
49558d2ec52aSSatish Balay       PetscInt       *edges, overlap = 1;
4956cfb853baSMatthew G. Knepley       PetscReal      *lower, *upper;
4957cfb853baSMatthew G. Knepley       DMBoundaryType *bdt;
4958cfb853baSMatthew G. Knepley       PetscInt        n, d;
4959cfb853baSMatthew G. Knepley 
4960cfb853baSMatthew G. Knepley       *useCoordSpace = PETSC_FALSE;
4961cfb853baSMatthew G. Knepley       PetscCall(PetscMalloc4(dim, &edges, dim, &lower, dim, &upper, dim, &bdt));
4962cfb853baSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
4963cfb853baSMatthew G. Knepley         edges[d] = 1;
4964cfb853baSMatthew G. Knepley         lower[d] = 0.;
4965cfb853baSMatthew G. Knepley         upper[d] = 1.;
4966cfb853baSMatthew G. Knepley         bdt[d]   = DM_BOUNDARY_PERIODIC;
4967cfb853baSMatthew G. Knepley       }
4968cfb853baSMatthew G. Knepley       n = dim;
4969cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", edges, &n, &flg));
4970cfb853baSMatthew G. Knepley       n = dim;
4971cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
4972cfb853baSMatthew 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);
4973cfb853baSMatthew G. Knepley       n = dim;
4974cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
4975cfb853baSMatthew 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);
4976cfb853baSMatthew G. Knepley       n = dim;
4977cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *)bdt, &n, &flg));
4978cfb853baSMatthew 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);
49798d2ec52aSSatish Balay       PetscCall(PetscOptionsBoundedInt("-dm_distribute_overlap", "The size of the overlap halo", "DMPlexDistribute", overlap, &overlap, NULL, 0));
49808d2ec52aSSatish Balay       PetscCall(DMPlexCreateHypercubicMesh_Internal(dm, dim, lower, upper, edges, overlap, bdt));
4981cfb853baSMatthew G. Knepley       PetscCall(PetscFree4(edges, lower, upper, bdt));
4982cfb853baSMatthew G. Knepley     } break;
4983d71ae5a4SJacob Faibussowitsch     default:
4984d71ae5a4SJacob Faibussowitsch       SETERRQ(comm, PETSC_ERR_SUP, "Domain shape %s is unsupported", DMPlexShapes[shape]);
49859318fe57SMatthew G. Knepley     }
49869318fe57SMatthew G. Knepley   }
49879566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
498848a46eb9SPierre Jolivet   if (!((PetscObject)dm)->name && nameflg) PetscCall(PetscObjectSetName((PetscObject)dm, plexname));
4989b9da1bb3SMatthew G. Knepley   if (orient) PetscCall(DMPlexOrient(dm));
49904e22dd4cSMatthew G. Knepley   // Allow label creation
49914e22dd4cSMatthew G. Knepley   PetscCall(PetscOptionsFindPairPrefix_Private(NULL, ((PetscObject)dm)->prefix, "-dm_plex_label_", &option, NULL, &flg));
49924e22dd4cSMatthew G. Knepley   if (flg) {
49934e22dd4cSMatthew G. Knepley     DMLabel     label;
49944e22dd4cSMatthew G. Knepley     PetscInt    points[1024], n = 1024;
49954e22dd4cSMatthew G. Knepley     char        fulloption[PETSC_MAX_PATH_LEN];
49964e22dd4cSMatthew G. Knepley     const char *name = &option[14];
49974e22dd4cSMatthew G. Knepley 
49984e22dd4cSMatthew G. Knepley     PetscCall(DMCreateLabel(dm, name));
49994e22dd4cSMatthew G. Knepley     PetscCall(DMGetLabel(dm, name, &label));
50004e22dd4cSMatthew G. Knepley     fulloption[0] = '-';
50014e22dd4cSMatthew G. Knepley     fulloption[1] = 0;
50024e22dd4cSMatthew G. Knepley     PetscCall(PetscStrlcat(fulloption, option, PETSC_MAX_PATH_LEN));
50034e22dd4cSMatthew G. Knepley     PetscCall(PetscOptionsGetIntArray(NULL, ((PetscObject)dm)->prefix, fulloption, points, &n, NULL));
50044e22dd4cSMatthew G. Knepley     for (PetscInt p = 0; p < n; ++p) PetscCall(DMLabelSetValue(label, points[p], 1));
50054e22dd4cSMatthew G. Knepley   }
5006dd0eeac9SMatthew G. Knepley   // Allow cohesive label creation
5007dd0eeac9SMatthew G. Knepley   //   Faces are input, completed, and all points are marked with their depth
5008dd0eeac9SMatthew G. Knepley   PetscCall(PetscOptionsFindPairPrefix_Private(NULL, ((PetscObject)dm)->prefix, "-dm_plex_cohesive_label_", &option, NULL, &flg));
5009dd0eeac9SMatthew G. Knepley   if (flg) {
5010dd0eeac9SMatthew G. Knepley     DMLabel   label;
5011dd0eeac9SMatthew G. Knepley     PetscInt  points[1024], n, pStart, pEnd, Nl = 1;
50125390be7dSMatthew G. Knepley     PetscBool noCreate = PETSC_FALSE;
5013dd0eeac9SMatthew G. Knepley     char      fulloption[PETSC_MAX_PATH_LEN];
5014dd0eeac9SMatthew G. Knepley     char      name[PETSC_MAX_PATH_LEN];
5015dd0eeac9SMatthew G. Knepley     size_t    len;
5016dd0eeac9SMatthew G. Knepley 
5017dd0eeac9SMatthew G. Knepley     PetscCall(DMPlexGetChart(dm, &pStart, &pEnd));
5018dd0eeac9SMatthew G. Knepley     PetscCall(PetscStrncpy(name, &option[23], PETSC_MAX_PATH_LEN));
5019dd0eeac9SMatthew G. Knepley     PetscCall(PetscStrlen(name, &len));
5020dd0eeac9SMatthew G. Knepley     if (name[len - 1] == '0') Nl = 10;
5021dd0eeac9SMatthew G. Knepley     for (PetscInt l = 0; l < Nl; ++l) {
50226497c311SBarry Smith       if (l > 0) name[len - 1] = (char)('0' + l);
5023dd0eeac9SMatthew G. Knepley       fulloption[0] = 0;
5024dd0eeac9SMatthew G. Knepley       PetscCall(PetscStrlcat(fulloption, "-dm_plex_cohesive_label_", 32));
5025dd0eeac9SMatthew G. Knepley       PetscCall(PetscStrlcat(fulloption, name, PETSC_MAX_PATH_LEN - 32));
5026dd0eeac9SMatthew G. Knepley       n = 1024;
5027dd0eeac9SMatthew G. Knepley       PetscCall(PetscOptionsGetIntArray(NULL, ((PetscObject)dm)->prefix, fulloption, points, &n, &flg));
5028dd0eeac9SMatthew G. Knepley       if (!flg) break;
50295390be7dSMatthew G. Knepley       PetscCall(DMHasLabel(dm, name, &noCreate));
50305390be7dSMatthew G. Knepley       if (noCreate) {
50315390be7dSMatthew G. Knepley         DMLabel         inlabel;
50325390be7dSMatthew G. Knepley         IS              pointIS;
50335390be7dSMatthew G. Knepley         const PetscInt *lpoints;
50345390be7dSMatthew G. Knepley         PetscInt        pdep, ln, inval = points[0];
50355390be7dSMatthew G. Knepley         char            newname[PETSC_MAX_PATH_LEN];
50365390be7dSMatthew G. Knepley 
50375390be7dSMatthew G. Knepley         PetscCheck(n == 1, comm, PETSC_ERR_ARG_WRONG, "Must specify a label value with this option");
50385390be7dSMatthew G. Knepley         PetscCall(DMGetLabel(dm, name, &inlabel));
50395390be7dSMatthew G. Knepley         PetscCall(DMLabelGetStratumIS(inlabel, inval, &pointIS));
50405390be7dSMatthew G. Knepley         PetscCall(ISGetLocalSize(pointIS, &ln));
50415390be7dSMatthew G. Knepley         PetscCall(ISGetIndices(pointIS, &lpoints));
50425390be7dSMatthew G. Knepley         PetscCall(DMPlexGetPointDepth(dm, lpoints[0], &pdep));
50435390be7dSMatthew G. Knepley         PetscCall(PetscSNPrintf(newname, PETSC_MAX_PATH_LEN, "%s%" PetscInt_FMT, name, points[0]));
50445390be7dSMatthew G. Knepley         PetscCall(DMCreateLabel(dm, newname));
50455390be7dSMatthew G. Knepley         PetscCall(DMGetLabel(dm, newname, &label));
50465390be7dSMatthew G. Knepley         if (!pdep) PetscCall(ProcessCohesiveLabel_Vertices(dm, label, inlabel, inval, ln, lpoints));
50475390be7dSMatthew G. Knepley         else PetscCall(ProcessCohesiveLabel_Faces(dm, label, ln, lpoints));
50485390be7dSMatthew G. Knepley         PetscCall(ISRestoreIndices(pointIS, &lpoints));
50495390be7dSMatthew G. Knepley         PetscCall(ISDestroy(&pointIS));
50505390be7dSMatthew G. Knepley       } else {
5051dd0eeac9SMatthew G. Knepley         PetscCall(DMCreateLabel(dm, name));
5052dd0eeac9SMatthew G. Knepley         PetscCall(DMGetLabel(dm, name, &label));
5053dd0eeac9SMatthew G. Knepley         if (pStart >= pEnd) n = 0;
50545390be7dSMatthew G. Knepley         PetscCall(ProcessCohesiveLabel_Faces(dm, label, n, points));
5055dd0eeac9SMatthew G. Knepley       }
5056dd0eeac9SMatthew G. Knepley       PetscCall(DMPlexOrientLabel(dm, label));
50570542aa8cSMatthew G. Knepley       PetscCall(DMPlexLabelCohesiveComplete(dm, label, NULL, 1, PETSC_FALSE, PETSC_FALSE, NULL));
5058dd0eeac9SMatthew G. Knepley     }
5059dd0eeac9SMatthew G. Knepley   }
50605390be7dSMatthew G. Knepley   PetscCall(DMViewFromOptions(dm, NULL, "-created_dm_view"));
5061708be2fdSJed Brown   PetscCall(PetscLogEventEnd(DMPLEX_CreateFromOptions, dm, 0, 0, 0));
50623ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
50630a6ba040SMatthew G. Knepley }
50640a6ba040SMatthew G. Knepley 
5065ce78bad3SBarry Smith PetscErrorCode DMSetFromOptions_NonRefinement_Plex(DM dm, PetscOptionItems PetscOptionsObject)
5066d71ae5a4SJacob Faibussowitsch {
50670a6ba040SMatthew G. Knepley   DM_Plex  *mesh = (DM_Plex *)dm->data;
50687f9d8d6cSVaclav Hapla   PetscBool flg, flg2;
50699318fe57SMatthew G. Knepley   char      bdLabel[PETSC_MAX_PATH_LEN];
5070adc21957SMatthew G. Knepley   char      method[PETSC_MAX_PATH_LEN];
50710a6ba040SMatthew G. Knepley 
50720a6ba040SMatthew G. Knepley   PetscFunctionBegin;
50730a6ba040SMatthew G. Knepley   /* Handle viewing */
50749566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_print_set_values", "Output all set values info", "DMPlexMatSetClosure", PETSC_FALSE, &mesh->printSetValues, NULL));
50755962854dSMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_fem", "Debug output level for all fem computations", "DMPlexSNESComputeResidualFEM", 0, &mesh->printFEM, NULL, 0));
50765962854dSMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_fvm", "Debug output level for all fvm computations", "DMPlexSNESComputeResidualFVM", 0, &mesh->printFVM, NULL, 0));
50779566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-dm_plex_print_tol", "Tolerance for FEM output", "DMPlexSNESComputeResidualFEM", mesh->printTol, &mesh->printTol, NULL));
50789566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_l2", "Debug output level all L2 diff computations", "DMComputeL2Diff", 0, &mesh->printL2, NULL, 0));
5079f5867de0SMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_locate", "Debug output level all point location computations", "DMLocatePoints", 0, &mesh->printLocate, NULL, 0));
5080a77a5016SMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_project", "Debug output level all projection computations", "DMPlexProject", 0, &mesh->printProject, NULL, 0));
50819566063dSJacob Faibussowitsch   PetscCall(DMMonitorSetFromOptions(dm, "-dm_plex_monitor_throughput", "Monitor the simulation throughput", "DMPlexMonitorThroughput", DMPlexMonitorThroughput, NULL, &flg));
50829566063dSJacob Faibussowitsch   if (flg) PetscCall(PetscLogDefaultBegin());
50835e2c5519SMatthew G. Knepley   // Interpolation
50845e2c5519SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_plex_interpolate_prefer_tensor", "When different orderings exist, prefer the tensor order", "DMPlexSetInterpolationPreferTensor", mesh->interpolatePreferTensor, &mesh->interpolatePreferTensor, NULL));
50859318fe57SMatthew G. Knepley   /* Labeling */
50869566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_boundary_label", "Label to mark the mesh boundary", "", bdLabel, bdLabel, sizeof(bdLabel), &flg));
50879566063dSJacob Faibussowitsch   if (flg) PetscCall(DMPlexCreateBoundaryLabel_Private(dm, bdLabel));
5088953fc75cSMatthew G. Knepley   /* Point Location */
50899566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_hash_location", "Use grid hashing for point location", "DMInterpolate", PETSC_FALSE, &mesh->useHashLocation, NULL));
50900848f4b5SMatthew G. Knepley   /* Partitioning and distribution */
50919566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_partition_balance", "Attempt to evenly divide points on partition boundary between processes", "DMPlexSetPartitionBalance", PETSC_FALSE, &mesh->partitionBalance, NULL));
5092d02c7345SMatthew G. Knepley   /* Reordering */
5093adc21957SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_reorder_section", "Compute point permutation for local section", "DMReorderSectionSetDefault", PETSC_FALSE, &flg2, &flg));
5094adc21957SMatthew G. Knepley   if (flg) PetscCall(DMReorderSectionSetDefault(dm, flg2 ? DM_REORDER_DEFAULT_TRUE : DM_REORDER_DEFAULT_FALSE));
5095adc21957SMatthew G. Knepley   PetscCall(PetscOptionsString("-dm_reorder_section_type", "Reordering method for local section", "DMReorderSectionSetType", method, method, PETSC_MAX_PATH_LEN, &flg));
5096adc21957SMatthew G. Knepley   if (flg) PetscCall(DMReorderSectionSetType(dm, method));
50972e62ab5aSMatthew G. Knepley   /* Generation and remeshing */
50989566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_remesh_bd", "Allow changes to the boundary on remeshing", "DMAdapt", PETSC_FALSE, &mesh->remeshBd, NULL));
509961f058f9SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_plex_save_transform", "Save the transform producing this mesh", "DMAdapt", PETSC_FALSE, &mesh->saveTransform, NULL));
5100b29cfa1cSToby Isaac   /* Projection behavior */
5101d5b43468SJose E. Roman   PetscCall(PetscOptionsBoundedInt("-dm_plex_max_projection_height", "Maximum mesh point height used to project locally", "DMPlexSetMaxProjectionHeight", 0, &mesh->maxProjectionHeight, NULL, 0));
51029566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_regular_refinement", "Use special nested projection algorithm for regular refinement", "DMPlexSetRegularRefinement", mesh->regularRefinement, &mesh->regularRefinement, NULL));
5103f12cf164SMatthew G. Knepley   /* Checking structure */
5104f12cf164SMatthew G. Knepley   {
51057f9d8d6cSVaclav Hapla     PetscBool all = PETSC_FALSE;
5106f12cf164SMatthew G. Knepley 
51077f9d8d6cSVaclav Hapla     PetscCall(PetscOptionsBool("-dm_plex_check_all", "Perform all basic checks", "DMPlexCheck", PETSC_FALSE, &all, NULL));
51087f9d8d6cSVaclav Hapla     if (all) {
51097f9d8d6cSVaclav Hapla       PetscCall(DMPlexCheck(dm));
51107f9d8d6cSVaclav Hapla     } else {
51119566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_symmetry", "Check that the adjacency information in the mesh is symmetric", "DMPlexCheckSymmetry", PETSC_FALSE, &flg, &flg2));
51127f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckSymmetry(dm));
51139566063dSJacob 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));
51147f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckSkeleton(dm, 0));
51159566063dSJacob 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));
51167f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckFaces(dm, 0));
51179566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_geometry", "Check that cells have positive volume", "DMPlexCheckGeometry", PETSC_FALSE, &flg, &flg2));
51187f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckGeometry(dm));
51199566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_pointsf", "Check some necessary conditions for PointSF", "DMPlexCheckPointSF", PETSC_FALSE, &flg, &flg2));
5120d7d32a9aSMatthew G. Knepley       if (flg && flg2) PetscCall(DMPlexCheckPointSF(dm, NULL, PETSC_FALSE));
51219566063dSJacob 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));
51227f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckInterfaceCones(dm));
51237f9d8d6cSVaclav Hapla     }
51249566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-dm_plex_check_cell_shape", "Check cell shape", "DMPlexCheckCellShape", PETSC_FALSE, &flg, &flg2));
51259566063dSJacob Faibussowitsch     if (flg && flg2) PetscCall(DMPlexCheckCellShape(dm, PETSC_TRUE, PETSC_DETERMINE));
5126f12cf164SMatthew G. Knepley   }
51279318fe57SMatthew G. Knepley   {
51289318fe57SMatthew G. Knepley     PetscReal scale = 1.0;
51294f3833eaSMatthew G. Knepley 
51309566063dSJacob Faibussowitsch     PetscCall(PetscOptionsReal("-dm_plex_scale", "Scale factor for mesh coordinates", "DMPlexScale", scale, &scale, &flg));
51319318fe57SMatthew G. Knepley     if (flg) {
51329318fe57SMatthew G. Knepley       Vec coordinates, coordinatesLocal;
51339318fe57SMatthew G. Knepley 
51349566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinates(dm, &coordinates));
51359566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinatesLocal(dm, &coordinatesLocal));
51369566063dSJacob Faibussowitsch       PetscCall(VecScale(coordinates, scale));
51379566063dSJacob Faibussowitsch       PetscCall(VecScale(coordinatesLocal, scale));
51389318fe57SMatthew G. Knepley     }
51399318fe57SMatthew G. Knepley   }
51409566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerSetFromOptions(mesh->partitioner));
51413ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
514268d4fef7SMatthew G. Knepley }
514368d4fef7SMatthew G. Knepley 
5144ce78bad3SBarry Smith PetscErrorCode DMSetFromOptions_Overlap_Plex(DM dm, PetscOptionItems PetscOptionsObject, PetscInt *overlap)
5145d71ae5a4SJacob Faibussowitsch {
5146c506a872SMatthew G. Knepley   PetscInt  numOvLabels = 16, numOvExLabels = 16;
5147c506a872SMatthew G. Knepley   char     *ovLabelNames[16], *ovExLabelNames[16];
5148c506a872SMatthew G. Knepley   PetscInt  numOvValues = 16, numOvExValues = 16, l;
5149c506a872SMatthew G. Knepley   PetscBool flg;
5150c506a872SMatthew G. Knepley 
5151c506a872SMatthew G. Knepley   PetscFunctionBegin;
5152c506a872SMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_distribute_overlap", "The size of the overlap halo", "DMPlexDistribute", *overlap, overlap, NULL, 0));
5153c506a872SMatthew G. Knepley   PetscCall(PetscOptionsStringArray("-dm_distribute_overlap_labels", "List of overlap label names", "DMPlexDistribute", ovLabelNames, &numOvLabels, &flg));
5154c506a872SMatthew G. Knepley   if (!flg) numOvLabels = 0;
5155c506a872SMatthew G. Knepley   if (numOvLabels) {
5156c506a872SMatthew G. Knepley     ((DM_Plex *)dm->data)->numOvLabels = numOvLabels;
5157c506a872SMatthew G. Knepley     for (l = 0; l < numOvLabels; ++l) {
5158c506a872SMatthew G. Knepley       PetscCall(DMGetLabel(dm, ovLabelNames[l], &((DM_Plex *)dm->data)->ovLabels[l]));
5159c506a872SMatthew G. Knepley       PetscCheck(((DM_Plex *)dm->data)->ovLabels[l], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid label name %s", ovLabelNames[l]);
5160c506a872SMatthew G. Knepley       PetscCall(PetscFree(ovLabelNames[l]));
5161c506a872SMatthew G. Knepley     }
5162c506a872SMatthew G. Knepley     PetscCall(PetscOptionsIntArray("-dm_distribute_overlap_values", "List of overlap label values", "DMPlexDistribute", ((DM_Plex *)dm->data)->ovValues, &numOvValues, &flg));
5163c506a872SMatthew G. Knepley     if (!flg) numOvValues = 0;
5164c506a872SMatthew 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);
5165c506a872SMatthew G. Knepley 
5166c506a872SMatthew G. Knepley     PetscCall(PetscOptionsStringArray("-dm_distribute_overlap_exclude_labels", "List of overlap exclude label names", "DMPlexDistribute", ovExLabelNames, &numOvExLabels, &flg));
5167c506a872SMatthew G. Knepley     if (!flg) numOvExLabels = 0;
5168c506a872SMatthew G. Knepley     ((DM_Plex *)dm->data)->numOvExLabels = numOvExLabels;
5169c506a872SMatthew G. Knepley     for (l = 0; l < numOvExLabels; ++l) {
5170c506a872SMatthew G. Knepley       PetscCall(DMGetLabel(dm, ovExLabelNames[l], &((DM_Plex *)dm->data)->ovExLabels[l]));
5171c506a872SMatthew G. Knepley       PetscCheck(((DM_Plex *)dm->data)->ovExLabels[l], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid label name %s", ovExLabelNames[l]);
5172c506a872SMatthew G. Knepley       PetscCall(PetscFree(ovExLabelNames[l]));
5173c506a872SMatthew G. Knepley     }
5174c506a872SMatthew G. Knepley     PetscCall(PetscOptionsIntArray("-dm_distribute_overlap_exclude_values", "List of overlap exclude label values", "DMPlexDistribute", ((DM_Plex *)dm->data)->ovExValues, &numOvExValues, &flg));
5175c506a872SMatthew G. Knepley     if (!flg) numOvExValues = 0;
5176c506a872SMatthew 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);
5177c506a872SMatthew G. Knepley   }
51783ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5179c506a872SMatthew G. Knepley }
5180c506a872SMatthew G. Knepley 
5181ce78bad3SBarry Smith static PetscErrorCode DMSetFromOptions_Plex(DM dm, PetscOptionItems PetscOptionsObject)
5182d71ae5a4SJacob Faibussowitsch {
5183bdf63967SMatthew G. Knepley   PetscFunctionList    ordlist;
5184bdf63967SMatthew G. Knepley   char                 oname[256];
51854e22dd4cSMatthew G. Knepley   char                 sublabelname[PETSC_MAX_PATH_LEN] = "";
5186adc21957SMatthew G. Knepley   DMReorderDefaultFlag reorder;
5187d410b0cfSMatthew G. Knepley   PetscReal            volume    = -1.0;
51889318fe57SMatthew G. Knepley   PetscInt             prerefine = 0, refine = 0, r, coarsen = 0, overlap = 0, extLayers = 0, dim;
5189530e699aSMatthew G. Knepley   PetscBool            uniformOrig = PETSC_FALSE, uniform = PETSC_TRUE, distribute, saveSF = PETSC_FALSE, interpolate = PETSC_TRUE, coordSpace = PETSC_FALSE, remap = PETSC_TRUE, ghostCells = PETSC_FALSE, isHierarchy, flg;
519068d4fef7SMatthew G. Knepley 
519168d4fef7SMatthew G. Knepley   PetscFunctionBegin;
5192d0609cedSBarry Smith   PetscOptionsHeadBegin(PetscOptionsObject, "DMPlex Options");
5193dd4c3f67SMatthew G. Knepley   if (dm->cloneOpts) goto non_refine;
51949318fe57SMatthew G. Knepley   /* Handle automatic creation */
51959566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
5196530e699aSMatthew G. Knepley   if (dim < 0) PetscCall(DMPlexCreateFromOptions_Internal(PetscOptionsObject, &coordSpace, dm));
51976bc1bd01Sksagiyam   PetscCall(DMGetDimension(dm, &dim));
5198d89e6e46SMatthew G. Knepley   /* Handle interpolation before distribution */
51999566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_interpolate_pre", "Flag to interpolate mesh before distribution", "", interpolate, &interpolate, &flg));
5200d89e6e46SMatthew G. Knepley   if (flg) {
5201d89e6e46SMatthew G. Knepley     DMPlexInterpolatedFlag interpolated;
5202d89e6e46SMatthew G. Knepley 
52039566063dSJacob Faibussowitsch     PetscCall(DMPlexIsInterpolated(dm, &interpolated));
5204d89e6e46SMatthew G. Knepley     if (interpolated == DMPLEX_INTERPOLATED_FULL && !interpolate) {
5205d89e6e46SMatthew G. Knepley       DM udm;
5206d89e6e46SMatthew G. Knepley 
52079566063dSJacob Faibussowitsch       PetscCall(DMPlexUninterpolate(dm, &udm));
520869d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &udm));
5209d89e6e46SMatthew G. Knepley     } else if (interpolated != DMPLEX_INTERPOLATED_FULL && interpolate) {
5210d89e6e46SMatthew G. Knepley       DM idm;
5211d89e6e46SMatthew G. Knepley 
52129566063dSJacob Faibussowitsch       PetscCall(DMPlexInterpolate(dm, &idm));
521369d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &idm));
5214d89e6e46SMatthew G. Knepley     }
5215d89e6e46SMatthew G. Knepley   }
52164e22dd4cSMatthew G. Knepley   // Handle submesh selection before distribution
52174e22dd4cSMatthew G. Knepley   PetscCall(PetscOptionsString("-dm_plex_submesh", "Label to use for submesh selection", "", sublabelname, sublabelname, PETSC_MAX_PATH_LEN, &flg));
52184e22dd4cSMatthew G. Knepley   if (flg) {
52194e22dd4cSMatthew G. Knepley     DM              subdm;
52204e22dd4cSMatthew G. Knepley     DMLabel         label;
52214e22dd4cSMatthew G. Knepley     IS              valueIS, pointIS;
52224e22dd4cSMatthew G. Knepley     const PetscInt *values, *points;
52234e22dd4cSMatthew G. Knepley     PetscBool       markedFaces = PETSC_FALSE;
52244e22dd4cSMatthew G. Knepley     PetscInt        Nv, value, Np;
52254e22dd4cSMatthew G. Knepley 
52264e22dd4cSMatthew G. Knepley     PetscCall(DMGetLabel(dm, sublabelname, &label));
52274e22dd4cSMatthew G. Knepley     PetscCall(DMLabelGetNumValues(label, &Nv));
52284e22dd4cSMatthew 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);
52294e22dd4cSMatthew G. Knepley     PetscCall(DMLabelGetValueIS(label, &valueIS));
52304e22dd4cSMatthew G. Knepley     PetscCall(ISGetIndices(valueIS, &values));
52314e22dd4cSMatthew G. Knepley     value = values[0];
52324e22dd4cSMatthew G. Knepley     PetscCall(ISRestoreIndices(valueIS, &values));
52334e22dd4cSMatthew G. Knepley     PetscCall(ISDestroy(&valueIS));
52344e22dd4cSMatthew G. Knepley     PetscCall(DMLabelGetStratumSize(label, value, &Np));
52354e22dd4cSMatthew G. Knepley     PetscCall(DMLabelGetStratumIS(label, value, &pointIS));
52364e22dd4cSMatthew G. Knepley     PetscCall(ISGetIndices(pointIS, &points));
52374e22dd4cSMatthew G. Knepley     for (PetscInt p = 0; p < Np; ++p) {
52384e22dd4cSMatthew G. Knepley       PetscInt pdepth;
52394e22dd4cSMatthew G. Knepley 
52404e22dd4cSMatthew G. Knepley       PetscCall(DMPlexGetPointDepth(dm, points[p], &pdepth));
52414e22dd4cSMatthew G. Knepley       if (pdepth) {
52424e22dd4cSMatthew G. Knepley         markedFaces = PETSC_TRUE;
52434e22dd4cSMatthew G. Knepley         break;
52444e22dd4cSMatthew G. Knepley       }
52454e22dd4cSMatthew G. Knepley     }
52464e22dd4cSMatthew G. Knepley     PetscCall(ISRestoreIndices(pointIS, &points));
52474e22dd4cSMatthew G. Knepley     PetscCall(ISDestroy(&pointIS));
52484e22dd4cSMatthew G. Knepley     PetscCall(DMPlexCreateSubmesh(dm, label, value, markedFaces, &subdm));
52494e22dd4cSMatthew G. Knepley     PetscCall(DMPlexReplace_Internal(dm, &subdm));
52504e22dd4cSMatthew G. Knepley     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
52514e22dd4cSMatthew G. Knepley   }
52529b44eab4SMatthew G. Knepley   /* Handle DMPlex refinement before distribution */
52539566063dSJacob Faibussowitsch   PetscCall(DMPlexGetRefinementUniform(dm, &uniformOrig));
52549566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine_pre", "The number of refinements before distribution", "DMCreate", prerefine, &prerefine, NULL, 0));
52559566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_remap_pre", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL));
52569566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_uniform_pre", "Flag for uniform refinement before distribution", "DMCreate", uniform, &uniform, &flg));
52579566063dSJacob Faibussowitsch   if (flg) PetscCall(DMPlexSetRefinementUniform(dm, uniform));
52589566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-dm_refine_volume_limit_pre", "The maximum cell volume after refinement before distribution", "DMCreate", volume, &volume, &flg));
52599318fe57SMatthew G. Knepley   if (flg) {
52609566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementUniform(dm, PETSC_FALSE));
52619566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementLimit(dm, volume));
52629318fe57SMatthew G. Knepley     prerefine = PetscMax(prerefine, 1);
52639318fe57SMatthew G. Knepley   }
5264b23db253SStefano Zampini   if (prerefine) PetscCall(DMLocalizeCoordinates(dm));
52659b44eab4SMatthew G. Knepley   for (r = 0; r < prerefine; ++r) {
52669b44eab4SMatthew G. Knepley     DM            rdm;
52672192575eSBarry Smith     PetscPointFn *coordFunc;
52689b44eab4SMatthew G. Knepley 
5269509b31aaSMatthew G. Knepley     PetscCall(DMPlexGetCoordinateMap(dm, &coordFunc));
5270dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
52719566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &rdm));
527269d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &rdm));
5273dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
527461a622f3SMatthew G. Knepley     if (coordFunc && remap) {
52759566063dSJacob Faibussowitsch       PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
5276509b31aaSMatthew G. Knepley       PetscCall(DMPlexSetCoordinateMap(dm, coordFunc));
52779b44eab4SMatthew G. Knepley     }
52789b44eab4SMatthew G. Knepley   }
52799566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, uniformOrig));
52809318fe57SMatthew G. Knepley   /* Handle DMPlex extrusion before distribution */
52819566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_extrude", "The number of layers to extrude", "", extLayers, &extLayers, NULL, 0));
52829318fe57SMatthew G. Knepley   if (extLayers) {
52839318fe57SMatthew G. Knepley     DM edm;
52849318fe57SMatthew G. Knepley 
52859566063dSJacob Faibussowitsch     PetscCall(DMExtrude(dm, extLayers, &edm));
528669d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &edm));
5287509b31aaSMatthew G. Knepley     PetscCall(DMPlexSetCoordinateMap(dm, NULL));
5288dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
5289d410b0cfSMatthew G. Knepley     extLayers = 0;
52905e17fc22SAidan Hamilton     PetscCall(DMGetDimension(dm, &dim));
52919318fe57SMatthew G. Knepley   }
5292bdf63967SMatthew G. Knepley   /* Handle DMPlex reordering before distribution */
52936bc1bd01Sksagiyam   PetscCall(DMPlexReorderGetDefault(dm, &reorder));
52949566063dSJacob Faibussowitsch   PetscCall(MatGetOrderingList(&ordlist));
52956bc1bd01Sksagiyam   PetscCall(PetscStrncpy(oname, MATORDERINGNATURAL, sizeof(oname)));
52969566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_plex_reorder", "Set mesh reordering type", "DMPlexGetOrdering", ordlist, MATORDERINGNATURAL, oname, sizeof(oname), &flg));
5297adc21957SMatthew G. Knepley   if (reorder == DM_REORDER_DEFAULT_TRUE || flg) {
5298bdf63967SMatthew G. Knepley     DM pdm;
5299bdf63967SMatthew G. Knepley     IS perm;
5300bdf63967SMatthew G. Knepley 
53019566063dSJacob Faibussowitsch     PetscCall(DMPlexGetOrdering(dm, oname, NULL, &perm));
53029566063dSJacob Faibussowitsch     PetscCall(DMPlexPermute(dm, perm, &pdm));
53039566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&perm));
530469d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &pdm));
5305dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
5306bdf63967SMatthew G. Knepley   }
53079b44eab4SMatthew G. Knepley   /* Handle DMPlex distribution */
53089566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeGetDefault(dm, &distribute));
5309c506a872SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_distribute", "Flag to redistribute a mesh among processes", "DMPlexDistribute", distribute, &distribute, NULL));
5310a286e215SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_distribute_save_sf", "Flag to save the migration SF", "DMPlexSetMigrationSF", saveSF, &saveSF, NULL));
5311dbbe0bcdSBarry Smith   PetscCall(DMSetFromOptions_Overlap_Plex(dm, PetscOptionsObject, &overlap));
53129b44eab4SMatthew G. Knepley   if (distribute) {
53139b44eab4SMatthew G. Knepley     DM               pdm = NULL;
53149b44eab4SMatthew G. Knepley     PetscPartitioner part;
5315a286e215SMatthew G. Knepley     PetscSF          sfMigration;
53169566ead2SJames Wright     PetscBool        use_partition_balance;
53179b44eab4SMatthew G. Knepley 
53189566063dSJacob Faibussowitsch     PetscCall(DMPlexGetPartitioner(dm, &part));
53199566063dSJacob Faibussowitsch     PetscCall(PetscPartitionerSetFromOptions(part));
53209566ead2SJames Wright     PetscCall(DMPlexGetPartitionBalance(dm, &use_partition_balance));
53219566ead2SJames Wright     PetscCall(PetscOptionsBool("-dm_plex_partition_balance", "Attempt to evenly divide points on partition boundary between processes", "DMPlexSetPartitionBalance", use_partition_balance, &use_partition_balance, &flg));
53229566ead2SJames Wright     if (flg) PetscCall(DMPlexSetPartitionBalance(dm, use_partition_balance));
5323a286e215SMatthew G. Knepley     PetscCall(DMPlexDistribute(dm, overlap, &sfMigration, &pdm));
53245d2873a6SJames Wright     if (pdm) {
53255d2873a6SJames Wright       // Delete the local section to force the existing one to be rebuilt with the distributed DM
53265d2873a6SJames Wright       PetscCall(DMSetLocalSection(dm, pdm->localSection));
53275d2873a6SJames Wright       PetscCall(DMPlexReplace_Internal(dm, &pdm));
53285d2873a6SJames Wright     }
5329a286e215SMatthew G. Knepley     if (saveSF) PetscCall(DMPlexSetMigrationSF(dm, sfMigration));
5330a286e215SMatthew G. Knepley     PetscCall(PetscSFDestroy(&sfMigration));
53319b44eab4SMatthew G. Knepley   }
53324054ae39SJames Wright 
53334054ae39SJames Wright   {
53344054ae39SJames Wright     PetscBool useBoxLabel = PETSC_FALSE;
53354054ae39SJames Wright     PetscCall(PetscOptionsBool("-dm_plex_box_label", "Create 'Face Sets' assuming boundary faces align with cartesian directions", "DMCreate", useBoxLabel, &useBoxLabel, NULL));
5336d7d2d1d2SJames Wright     if (useBoxLabel) {
5337d7d2d1d2SJames Wright       PetscInt       n      = 3;
5338d7d2d1d2SJames Wright       DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
5339d7d2d1d2SJames Wright 
5340d7d2d1d2SJames Wright       PetscCall(PetscOptionsEnumArray("-dm_plex_box_label_bd", "Boundary type for each dimension when using -dm_plex_box_label", "", DMBoundaryTypes, (PetscEnum *)bdt, &n, &flg));
5341d7d2d1d2SJames 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);
5342d7d2d1d2SJames Wright       PetscCall(DMPlexSetBoxLabel_Internal(dm, bdt));
5343d7d2d1d2SJames Wright     }
53444054ae39SJames Wright   }
5345d2b2dc1eSMatthew G. Knepley   /* Must check CEED options before creating function space for coordinates */
5346d2b2dc1eSMatthew G. Knepley   {
5347d2b2dc1eSMatthew G. Knepley     PetscBool useCeed = PETSC_FALSE, flg;
5348d2b2dc1eSMatthew G. Knepley 
5349d2b2dc1eSMatthew G. Knepley     PetscCall(PetscOptionsBool("-dm_plex_use_ceed", "Use LibCEED as the FEM backend", "DMPlexSetUseCeed", useCeed, &useCeed, &flg));
5350d2b2dc1eSMatthew G. Knepley     if (flg) PetscCall(DMPlexSetUseCeed(dm, useCeed));
5351d2b2dc1eSMatthew G. Knepley   }
53529318fe57SMatthew G. Knepley   /* Create coordinate space */
5353530e699aSMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_coord_space", "Use an FEM space for coordinates", "", coordSpace, &coordSpace, NULL));
5354530e699aSMatthew G. Knepley   if (coordSpace) {
5355e44f6aebSMatthew G. Knepley     PetscInt  degree = 1, deg;
53565515ebd3SMatthew G. Knepley     PetscInt  height = 0;
53575515ebd3SMatthew G. Knepley     DM        cdm;
5358530e699aSMatthew G. Knepley     PetscBool localize = PETSC_TRUE, sparseLocalize = PETSC_TRUE;
53599318fe57SMatthew G. Knepley 
53609566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-dm_coord_petscspace_degree", "FEM degree for coordinate space", "", degree, &degree, NULL));
5361e44f6aebSMatthew G. Knepley     PetscCall(DMGetCoordinateDegree_Internal(dm, &deg));
5362509b31aaSMatthew G. Knepley     if (coordSpace && deg <= 1) PetscCall(DMPlexCreateCoordinateSpace(dm, degree, PETSC_TRUE, NULL));
53635515ebd3SMatthew G. Knepley     PetscCall(DMGetCoordinateDM(dm, &cdm));
5364530e699aSMatthew G. Knepley     if (!coordSpace) {
536561a622f3SMatthew G. Knepley       PetscDS      cds;
536661a622f3SMatthew G. Knepley       PetscObject  obj;
536761a622f3SMatthew G. Knepley       PetscClassId id;
536861a622f3SMatthew G. Knepley 
53699566063dSJacob Faibussowitsch       PetscCall(DMGetDS(cdm, &cds));
53709566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
53719566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
537261a622f3SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
537361a622f3SMatthew G. Knepley         PetscContainer dummy;
537461a622f3SMatthew G. Knepley 
53759566063dSJacob Faibussowitsch         PetscCall(PetscContainerCreate(PETSC_COMM_SELF, &dummy));
53769566063dSJacob Faibussowitsch         PetscCall(PetscObjectSetName((PetscObject)dummy, "coordinates"));
53779566063dSJacob Faibussowitsch         PetscCall(DMSetField(cdm, 0, NULL, (PetscObject)dummy));
53789566063dSJacob Faibussowitsch         PetscCall(PetscContainerDestroy(&dummy));
53799566063dSJacob Faibussowitsch         PetscCall(DMClearDS(cdm));
538061a622f3SMatthew G. Knepley       }
5381509b31aaSMatthew G. Knepley       PetscCall(DMPlexSetCoordinateMap(dm, NULL));
538261a622f3SMatthew G. Knepley     }
5383c3db174cSMatthew G. Knepley     PetscCall(PetscOptionsBool("-dm_localize", "Localize mesh coordinates", "", localize, &localize, NULL));
5384c3db174cSMatthew G. Knepley     PetscCall(PetscOptionsBool("-dm_sparse_localize", "Localize only necessary cells", "DMSetSparseLocalize", sparseLocalize, &sparseLocalize, &flg));
5385c3db174cSMatthew G. Knepley     if (flg) PetscCall(DMSetSparseLocalize(dm, sparseLocalize));
53865515ebd3SMatthew G. Knepley     PetscCall(PetscOptionsInt("-dm_localize_height", "Localize edges and faces in addition to cells", "", height, &height, &flg));
53875515ebd3SMatthew G. Knepley     if (flg) PetscCall(DMPlexSetMaxProjectionHeight(cdm, height));
5388c3db174cSMatthew G. Knepley     if (localize) PetscCall(DMLocalizeCoordinates(dm));
53899318fe57SMatthew G. Knepley   }
539068d4fef7SMatthew G. Knepley   /* Handle DMPlex refinement */
539161a622f3SMatthew G. Knepley   remap = PETSC_TRUE;
53929566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine", "The number of uniform refinements", "DMCreate", refine, &refine, NULL, 0));
53939566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_remap", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL));
53949566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine_hierarchy", "The number of uniform refinements", "DMCreate", refine, &refine, &isHierarchy, 0));
53959566063dSJacob Faibussowitsch   if (refine) PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
539668d4fef7SMatthew G. Knepley   if (refine && isHierarchy) {
5397acdc6f61SToby Isaac     DM *dms, coarseDM;
539868d4fef7SMatthew G. Knepley 
53999566063dSJacob Faibussowitsch     PetscCall(DMGetCoarseDM(dm, &coarseDM));
54009566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)coarseDM));
54019566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(refine, &dms));
54029566063dSJacob Faibussowitsch     PetscCall(DMRefineHierarchy(dm, refine, dms));
540368d4fef7SMatthew G. Knepley     /* Total hack since we do not pass in a pointer */
54049566063dSJacob Faibussowitsch     PetscCall(DMPlexSwap_Static(dm, dms[refine - 1]));
540568d4fef7SMatthew G. Knepley     if (refine == 1) {
54069566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dm, dms[0]));
54079566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dm, PETSC_TRUE));
540868d4fef7SMatthew G. Knepley     } else {
54099566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dm, dms[refine - 2]));
54109566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dm, PETSC_TRUE));
54119566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dms[0], dms[refine - 1]));
54129566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dms[0], PETSC_TRUE));
541368d4fef7SMatthew G. Knepley     }
54149566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dms[refine - 1], coarseDM));
54159566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)coarseDM));
541668d4fef7SMatthew G. Knepley     /* Free DMs */
541768d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
5418dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dms[r], PetscOptionsObject));
54199566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dms[r]));
542068d4fef7SMatthew G. Knepley     }
54219566063dSJacob Faibussowitsch     PetscCall(PetscFree(dms));
542268d4fef7SMatthew G. Knepley   } else {
542368d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
54249318fe57SMatthew G. Knepley       DM            rdm;
54252192575eSBarry Smith       PetscPointFn *coordFunc;
542668d4fef7SMatthew G. Knepley 
5427dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
54289566063dSJacob Faibussowitsch       PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &rdm));
542968d4fef7SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
543069d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &rdm));
5431dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
54323674be70SMatthew G. Knepley       PetscCall(DMPlexGetCoordinateMap(dm, &coordFunc));
543361a622f3SMatthew G. Knepley       if (coordFunc && remap) {
54349566063dSJacob Faibussowitsch         PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
5435509b31aaSMatthew G. Knepley         PetscCall(DMPlexSetCoordinateMap(dm, coordFunc));
543651a74b61SMatthew G. Knepley       }
543768d4fef7SMatthew G. Knepley     }
543868d4fef7SMatthew G. Knepley   }
54393cf6fe12SMatthew G. Knepley   /* Handle DMPlex coarsening */
54409566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_coarsen", "Coarsen the mesh", "DMCreate", coarsen, &coarsen, NULL, 0));
54419566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_coarsen_hierarchy", "The number of coarsenings", "DMCreate", coarsen, &coarsen, &isHierarchy, 0));
5442b653a561SMatthew G. Knepley   if (coarsen && isHierarchy) {
5443b653a561SMatthew G. Knepley     DM *dms;
5444b653a561SMatthew G. Knepley 
54459566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(coarsen, &dms));
54469566063dSJacob Faibussowitsch     PetscCall(DMCoarsenHierarchy(dm, coarsen, dms));
5447b653a561SMatthew G. Knepley     /* Free DMs */
5448b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
5449dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dms[r], PetscOptionsObject));
54509566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dms[r]));
5451b653a561SMatthew G. Knepley     }
54529566063dSJacob Faibussowitsch     PetscCall(PetscFree(dms));
5453b653a561SMatthew G. Knepley   } else {
5454b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
54559318fe57SMatthew G. Knepley       DM            cdm;
54562192575eSBarry Smith       PetscPointFn *coordFunc;
54573cf6fe12SMatthew G. Knepley 
5458509b31aaSMatthew G. Knepley       PetscCall(DMPlexGetCoordinateMap(dm, &coordFunc));
5459dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
54609566063dSJacob Faibussowitsch       PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &cdm));
54613cf6fe12SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
546269d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &cdm));
5463dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
54649318fe57SMatthew G. Knepley       if (coordFunc) {
54659566063dSJacob Faibussowitsch         PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
5466509b31aaSMatthew G. Knepley         PetscCall(DMPlexSetCoordinateMap(dm, coordFunc));
54679318fe57SMatthew G. Knepley       }
54683cf6fe12SMatthew G. Knepley     }
5469b653a561SMatthew G. Knepley   }
5470be664eb1SMatthew G. Knepley   // Handle coordinate remapping
5471be664eb1SMatthew G. Knepley   remap = PETSC_FALSE;
5472be664eb1SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_coord_remap", "Flag to control coordinate remapping", "", remap, &remap, NULL));
5473be664eb1SMatthew G. Knepley   if (remap) {
5474be664eb1SMatthew G. Knepley     DMPlexCoordMap map     = DM_COORD_MAP_NONE;
54752192575eSBarry Smith     PetscPointFn  *mapFunc = NULL;
5476be664eb1SMatthew G. Knepley     PetscScalar    params[16];
5477f45b553cSPierre Jolivet     PetscInt       Np = PETSC_STATIC_ARRAY_LENGTH(params), cdim;
5478be664eb1SMatthew G. Knepley     MPI_Comm       comm;
5479be664eb1SMatthew G. Knepley 
5480be664eb1SMatthew G. Knepley     PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
5481be664eb1SMatthew G. Knepley     PetscCall(DMGetCoordinateDim(dm, &cdim));
5482be664eb1SMatthew G. Knepley     PetscCall(PetscOptionsScalarArray("-dm_coord_map_params", "Parameters for the coordinate remapping", "", params, &Np, &flg));
5483be664eb1SMatthew G. Knepley     if (!flg) Np = 0;
5484be664eb1SMatthew G. Knepley     // TODO Allow user to pass a map function by name
5485be664eb1SMatthew G. Knepley     PetscCall(PetscOptionsEnum("-dm_coord_map", "Coordinate mapping for built-in mesh", "", DMPlexCoordMaps, (PetscEnum)map, (PetscEnum *)&map, &flg));
5486be664eb1SMatthew G. Knepley     if (flg) {
5487be664eb1SMatthew G. Knepley       switch (map) {
5488be664eb1SMatthew G. Knepley       case DM_COORD_MAP_NONE:
5489be664eb1SMatthew G. Knepley         mapFunc = coordMap_identity;
5490be664eb1SMatthew G. Knepley         break;
5491be664eb1SMatthew G. Knepley       case DM_COORD_MAP_SHEAR:
5492be664eb1SMatthew G. Knepley         mapFunc = coordMap_shear;
5493be664eb1SMatthew G. Knepley         if (!Np) {
5494be664eb1SMatthew G. Knepley           Np        = cdim + 1;
5495be664eb1SMatthew G. Knepley           params[0] = 0;
5496be664eb1SMatthew G. Knepley           for (PetscInt d = 1; d <= cdim; ++d) params[d] = 1.0;
5497be664eb1SMatthew G. Knepley         }
5498be664eb1SMatthew 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);
5499be664eb1SMatthew G. Knepley         break;
5500be664eb1SMatthew G. Knepley       case DM_COORD_MAP_FLARE:
5501be664eb1SMatthew G. Knepley         mapFunc = coordMap_flare;
5502be664eb1SMatthew G. Knepley         if (!Np) {
5503be664eb1SMatthew G. Knepley           Np        = cdim + 1;
5504be664eb1SMatthew G. Knepley           params[0] = 0;
5505be664eb1SMatthew G. Knepley           for (PetscInt d = 1; d <= cdim; ++d) params[d] = 1.0;
5506be664eb1SMatthew G. Knepley         }
5507be664eb1SMatthew 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);
5508be664eb1SMatthew G. Knepley         break;
5509be664eb1SMatthew G. Knepley       case DM_COORD_MAP_ANNULUS:
5510be664eb1SMatthew G. Knepley         mapFunc = coordMap_annulus;
5511be664eb1SMatthew G. Knepley         if (!Np) {
5512be664eb1SMatthew G. Knepley           Np        = 2;
5513be664eb1SMatthew G. Knepley           params[0] = 1.;
5514be664eb1SMatthew G. Knepley           params[1] = 2.;
5515be664eb1SMatthew G. Knepley         }
5516be664eb1SMatthew G. Knepley         PetscCheck(Np == 2, comm, PETSC_ERR_ARG_WRONG, "The annulus coordinate map must have 2 parameters, not %" PetscInt_FMT, Np);
5517be664eb1SMatthew G. Knepley         break;
5518be664eb1SMatthew G. Knepley       case DM_COORD_MAP_SHELL:
5519be664eb1SMatthew G. Knepley         mapFunc = coordMap_shell;
5520be664eb1SMatthew G. Knepley         if (!Np) {
5521be664eb1SMatthew G. Knepley           Np        = 2;
5522be664eb1SMatthew G. Knepley           params[0] = 1.;
5523be664eb1SMatthew G. Knepley           params[1] = 2.;
5524be664eb1SMatthew G. Knepley         }
5525be664eb1SMatthew G. Knepley         PetscCheck(Np == 2, comm, PETSC_ERR_ARG_WRONG, "The spherical shell coordinate map must have 2 parameters, not %" PetscInt_FMT, Np);
5526be664eb1SMatthew G. Knepley         break;
5527530e699aSMatthew G. Knepley       case DM_COORD_MAP_SINUSOID:
5528530e699aSMatthew G. Knepley         mapFunc = coordMap_sinusoid;
5529530e699aSMatthew G. Knepley         if (!Np) {
5530530e699aSMatthew G. Knepley           Np        = 3;
5531530e699aSMatthew G. Knepley           params[0] = 1.;
5532530e699aSMatthew G. Knepley           params[1] = 1.;
5533530e699aSMatthew G. Knepley           params[2] = 1.;
5534530e699aSMatthew G. Knepley         }
5535530e699aSMatthew G. Knepley         PetscCheck(Np == 3, comm, PETSC_ERR_ARG_WRONG, "The sinusoidal coordinate map must have 3 parameters, not %" PetscInt_FMT, Np);
5536530e699aSMatthew G. Knepley         break;
5537be664eb1SMatthew G. Knepley       default:
5538be664eb1SMatthew G. Knepley         mapFunc = coordMap_identity;
5539be664eb1SMatthew G. Knepley       }
5540be664eb1SMatthew G. Knepley     }
5541be664eb1SMatthew G. Knepley     if (Np) {
5542be664eb1SMatthew G. Knepley       DM      cdm;
5543be664eb1SMatthew G. Knepley       PetscDS cds;
5544be664eb1SMatthew G. Knepley 
5545be664eb1SMatthew G. Knepley       PetscCall(DMGetCoordinateDM(dm, &cdm));
5546be664eb1SMatthew G. Knepley       PetscCall(DMGetDS(cdm, &cds));
5547be664eb1SMatthew G. Knepley       PetscCall(PetscDSSetConstants(cds, Np, params));
5548be664eb1SMatthew G. Knepley     }
5549be664eb1SMatthew G. Knepley     PetscCall(DMPlexRemapGeometry(dm, 0.0, mapFunc));
5550be664eb1SMatthew G. Knepley   }
5551909dfd52SMatthew G. Knepley   /* Handle ghost cells */
55529566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_create_fv_ghost_cells", "Flag to create finite volume ghost cells on the boundary", "DMCreate", ghostCells, &ghostCells, NULL));
5553909dfd52SMatthew G. Knepley   if (ghostCells) {
5554909dfd52SMatthew G. Knepley     DM   gdm;
5555909dfd52SMatthew G. Knepley     char lname[PETSC_MAX_PATH_LEN];
5556909dfd52SMatthew G. Knepley 
5557909dfd52SMatthew G. Knepley     lname[0] = '\0';
55589566063dSJacob Faibussowitsch     PetscCall(PetscOptionsString("-dm_plex_fv_ghost_cells_label", "Label name for ghost cells boundary", "DMCreate", lname, lname, sizeof(lname), &flg));
55599566063dSJacob Faibussowitsch     PetscCall(DMPlexConstructGhostCells(dm, flg ? lname : NULL, NULL, &gdm));
556069d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &gdm));
5561909dfd52SMatthew G. Knepley   }
55626913077dSMatthew G. Knepley   /* Handle 1D order */
5563adc21957SMatthew G. Knepley   if (reorder != DM_REORDER_DEFAULT_FALSE && dim == 1) {
55646913077dSMatthew G. Knepley     DM           cdm, rdm;
55656913077dSMatthew G. Knepley     PetscDS      cds;
55666913077dSMatthew G. Knepley     PetscObject  obj;
55676913077dSMatthew G. Knepley     PetscClassId id = PETSC_OBJECT_CLASSID;
55686913077dSMatthew G. Knepley     IS           perm;
55696bc1bd01Sksagiyam     PetscInt     Nf;
55706913077dSMatthew G. Knepley     PetscBool    distributed;
55716913077dSMatthew G. Knepley 
55729566063dSJacob Faibussowitsch     PetscCall(DMPlexIsDistributed(dm, &distributed));
55739566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
55749566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
55759566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(cds, &Nf));
55766913077dSMatthew G. Knepley     if (Nf) {
55779566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
55789566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
55796913077dSMatthew G. Knepley     }
55806bc1bd01Sksagiyam     if (!distributed && id != PETSCFE_CLASSID) {
55819566063dSJacob Faibussowitsch       PetscCall(DMPlexGetOrdering1D(dm, &perm));
55829566063dSJacob Faibussowitsch       PetscCall(DMPlexPermute(dm, perm, &rdm));
558369d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &rdm));
55849566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&perm));
55856913077dSMatthew G. Knepley     }
55866913077dSMatthew G. Knepley   }
55873cf6fe12SMatthew G. Knepley /* Handle */
5588dd4c3f67SMatthew G. Knepley non_refine:
5589dbbe0bcdSBarry Smith   PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
559022d6dc08SStefano Zampini   char    *phases[16];
559122d6dc08SStefano Zampini   PetscInt Nphases = 16;
559222d6dc08SStefano Zampini   PetscCall(PetscOptionsStringArray("-dm_plex_option_phases", "Option phase prefixes", "DMSetFromOptions", phases, &Nphases, &flg));
5593d0609cedSBarry Smith   PetscOptionsHeadEnd();
559422d6dc08SStefano Zampini 
559522d6dc08SStefano Zampini   // Phases
559622d6dc08SStefano Zampini   if (flg) {
5597530e699aSMatthew G. Knepley     DM          cdm;
5598530e699aSMatthew G. Knepley     char       *oldPrefix, *oldCoordPrefix;
5599530e699aSMatthew G. Knepley     const char *tmp;
560022d6dc08SStefano Zampini 
5601530e699aSMatthew G. Knepley     PetscCall(DMGetCoordinateDM(dm, &cdm));
5602530e699aSMatthew G. Knepley     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, &tmp));
5603530e699aSMatthew G. Knepley     PetscCall(PetscStrallocpy(tmp, &oldPrefix));
5604530e699aSMatthew G. Knepley     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)cdm, &tmp));
5605530e699aSMatthew G. Knepley     PetscCall(PetscStrallocpy(tmp, &oldCoordPrefix));
560622d6dc08SStefano Zampini     for (PetscInt ph = 0; ph < Nphases; ++ph) {
560722d6dc08SStefano Zampini       PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)dm, phases[ph]));
5608530e699aSMatthew G. Knepley       PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)cdm, phases[ph]));
560922d6dc08SStefano Zampini       PetscCall(PetscInfo(dm, "Options phase %s for DM %s\n", phases[ph], dm->hdr.name));
561022d6dc08SStefano Zampini       PetscCall(DMSetFromOptions(dm));
561122d6dc08SStefano Zampini       PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, oldPrefix));
5612530e699aSMatthew G. Knepley       PetscCall(DMGetCoordinateDM(dm, &cdm));
5613530e699aSMatthew G. Knepley       PetscCall(PetscObjectSetOptionsPrefix((PetscObject)cdm, oldCoordPrefix));
561422d6dc08SStefano Zampini       PetscCall(PetscFree(phases[ph]));
561522d6dc08SStefano Zampini     }
5616530e699aSMatthew G. Knepley     PetscCall(PetscFree(oldPrefix));
5617530e699aSMatthew G. Knepley     PetscCall(PetscFree(oldCoordPrefix));
561822d6dc08SStefano Zampini   }
56193ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
56200a6ba040SMatthew G. Knepley }
56210a6ba040SMatthew G. Knepley 
5622d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMCreateGlobalVector_Plex(DM dm, Vec *vec)
5623d71ae5a4SJacob Faibussowitsch {
5624552f7358SJed Brown   PetscFunctionBegin;
56259566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector_Section_Private(dm, vec));
56269566063dSJacob Faibussowitsch   /* PetscCall(VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM)); */
56279566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEW, (void (*)(void))VecView_Plex));
56289566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEWNATIVE, (void (*)(void))VecView_Plex_Native));
56299566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOAD, (void (*)(void))VecLoad_Plex));
56309566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOADNATIVE, (void (*)(void))VecLoad_Plex_Native));
56313ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5632552f7358SJed Brown }
5633552f7358SJed Brown 
5634d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMCreateLocalVector_Plex(DM dm, Vec *vec)
5635d71ae5a4SJacob Faibussowitsch {
5636552f7358SJed Brown   PetscFunctionBegin;
56379566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector_Section_Private(dm, vec));
56389566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEW, (void (*)(void))VecView_Plex_Local));
56399566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOAD, (void (*)(void))VecLoad_Plex_Local));
56403ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5641552f7358SJed Brown }
5642552f7358SJed Brown 
5643d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGetDimPoints_Plex(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
5644d71ae5a4SJacob Faibussowitsch {
5645793f3fe5SMatthew G. Knepley   PetscInt depth, d;
5646793f3fe5SMatthew G. Knepley 
5647793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
56489566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
5649793f3fe5SMatthew G. Knepley   if (depth == 1) {
56509566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(dm, &d));
56519566063dSJacob Faibussowitsch     if (dim == 0) PetscCall(DMPlexGetDepthStratum(dm, dim, pStart, pEnd));
56529566063dSJacob Faibussowitsch     else if (dim == d) PetscCall(DMPlexGetDepthStratum(dm, 1, pStart, pEnd));
56539371c9d4SSatish Balay     else {
56549371c9d4SSatish Balay       *pStart = 0;
56559371c9d4SSatish Balay       *pEnd   = 0;
56569371c9d4SSatish Balay     }
5657793f3fe5SMatthew G. Knepley   } else {
56589566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, dim, pStart, pEnd));
5659793f3fe5SMatthew G. Knepley   }
56603ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5661793f3fe5SMatthew G. Knepley }
5662793f3fe5SMatthew G. Knepley 
5663d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGetNeighbors_Plex(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
5664d71ae5a4SJacob Faibussowitsch {
5665502a2867SDave May   PetscSF            sf;
56666497c311SBarry Smith   PetscMPIInt        niranks, njranks;
56676497c311SBarry Smith   PetscInt           n;
56680a19bb7dSprj-   const PetscMPIInt *iranks, *jranks;
56690a19bb7dSprj-   DM_Plex           *data = (DM_Plex *)dm->data;
5670502a2867SDave May 
56712f356facSMatthew G. Knepley   PetscFunctionBegin;
56729566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
56730a19bb7dSprj-   if (!data->neighbors) {
56749566063dSJacob Faibussowitsch     PetscCall(PetscSFSetUp(sf));
56759566063dSJacob Faibussowitsch     PetscCall(PetscSFGetRootRanks(sf, &njranks, &jranks, NULL, NULL, NULL));
56769566063dSJacob Faibussowitsch     PetscCall(PetscSFGetLeafRanks(sf, &niranks, &iranks, NULL, NULL));
56779566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(njranks + niranks + 1, &data->neighbors));
56789566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(data->neighbors + 1, jranks, njranks));
56799566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(data->neighbors + njranks + 1, iranks, niranks));
56800a19bb7dSprj-     n = njranks + niranks;
56819566063dSJacob Faibussowitsch     PetscCall(PetscSortRemoveDupsMPIInt(&n, data->neighbors + 1));
56820a19bb7dSprj-     /* The following cast should never fail: can't have more neighbors than PETSC_MPI_INT_MAX */
56839566063dSJacob Faibussowitsch     PetscCall(PetscMPIIntCast(n, data->neighbors));
56840a19bb7dSprj-   }
56850a19bb7dSprj-   if (nranks) *nranks = data->neighbors[0];
56860a19bb7dSprj-   if (ranks) {
56870a19bb7dSprj-     if (data->neighbors[0]) *ranks = data->neighbors + 1;
56880a19bb7dSprj-     else *ranks = NULL;
56890a19bb7dSprj-   }
56903ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5691502a2867SDave May }
5692502a2867SDave May 
56931eb70e55SToby Isaac PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM, DM, Mat, Vec, Vec);
56941eb70e55SToby Isaac 
5695d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMInitialize_Plex(DM dm)
5696d71ae5a4SJacob Faibussowitsch {
5697552f7358SJed Brown   PetscFunctionBegin;
5698552f7358SJed Brown   dm->ops->view                      = DMView_Plex;
56992c40f234SMatthew G. Knepley   dm->ops->load                      = DMLoad_Plex;
5700552f7358SJed Brown   dm->ops->setfromoptions            = DMSetFromOptions_Plex;
570138221697SMatthew G. Knepley   dm->ops->clone                     = DMClone_Plex;
5702552f7358SJed Brown   dm->ops->setup                     = DMSetUp_Plex;
57031bb6d2a8SBarry Smith   dm->ops->createlocalsection        = DMCreateLocalSection_Plex;
5704adc21957SMatthew G. Knepley   dm->ops->createsectionpermutation  = DMCreateSectionPermutation_Plex;
570566ad2231SToby Isaac   dm->ops->createdefaultconstraints  = DMCreateDefaultConstraints_Plex;
5706552f7358SJed Brown   dm->ops->createglobalvector        = DMCreateGlobalVector_Plex;
5707552f7358SJed Brown   dm->ops->createlocalvector         = DMCreateLocalVector_Plex;
5708184d77edSJed Brown   dm->ops->getlocaltoglobalmapping   = NULL;
57090298fd71SBarry Smith   dm->ops->createfieldis             = NULL;
5710552f7358SJed Brown   dm->ops->createcoordinatedm        = DMCreateCoordinateDM_Plex;
5711*99acd26cSksagiyam   dm->ops->createcellcoordinatedm    = DMCreateCellCoordinateDM_Plex;
5712f19dbd58SToby Isaac   dm->ops->createcoordinatefield     = DMCreateCoordinateField_Plex;
57130a6ba040SMatthew G. Knepley   dm->ops->getcoloring               = NULL;
5714552f7358SJed Brown   dm->ops->creatematrix              = DMCreateMatrix_Plex;
5715bceba477SMatthew G. Knepley   dm->ops->createinterpolation       = DMCreateInterpolation_Plex;
5716bd041c0cSMatthew G. Knepley   dm->ops->createmassmatrix          = DMCreateMassMatrix_Plex;
5717b4937a87SMatthew G. Knepley   dm->ops->createmassmatrixlumped    = DMCreateMassMatrixLumped_Plex;
57185a84ad33SLisandro Dalcin   dm->ops->createinjection           = DMCreateInjection_Plex;
5719552f7358SJed Brown   dm->ops->refine                    = DMRefine_Plex;
57200a6ba040SMatthew G. Knepley   dm->ops->coarsen                   = DMCoarsen_Plex;
57210a6ba040SMatthew G. Knepley   dm->ops->refinehierarchy           = DMRefineHierarchy_Plex;
5722b653a561SMatthew G. Knepley   dm->ops->coarsenhierarchy          = DMCoarsenHierarchy_Plex;
5723d410b0cfSMatthew G. Knepley   dm->ops->extrude                   = DMExtrude_Plex;
57240298fd71SBarry Smith   dm->ops->globaltolocalbegin        = NULL;
57250298fd71SBarry Smith   dm->ops->globaltolocalend          = NULL;
57260298fd71SBarry Smith   dm->ops->localtoglobalbegin        = NULL;
57270298fd71SBarry Smith   dm->ops->localtoglobalend          = NULL;
5728552f7358SJed Brown   dm->ops->destroy                   = DMDestroy_Plex;
5729552f7358SJed Brown   dm->ops->createsubdm               = DMCreateSubDM_Plex;
57302adcc780SMatthew G. Knepley   dm->ops->createsuperdm             = DMCreateSuperDM_Plex;
5731793f3fe5SMatthew G. Knepley   dm->ops->getdimpoints              = DMGetDimPoints_Plex;
5732552f7358SJed Brown   dm->ops->locatepoints              = DMLocatePoints_Plex;
57330709b2feSToby Isaac   dm->ops->projectfunctionlocal      = DMProjectFunctionLocal_Plex;
57340709b2feSToby Isaac   dm->ops->projectfunctionlabellocal = DMProjectFunctionLabelLocal_Plex;
5735bfc4295aSToby Isaac   dm->ops->projectfieldlocal         = DMProjectFieldLocal_Plex;
57368c6c5593SMatthew G. Knepley   dm->ops->projectfieldlabellocal    = DMProjectFieldLabelLocal_Plex;
5737ece3a9fcSMatthew G. Knepley   dm->ops->projectbdfieldlabellocal  = DMProjectBdFieldLabelLocal_Plex;
57380709b2feSToby Isaac   dm->ops->computel2diff             = DMComputeL2Diff_Plex;
5739b698f381SToby Isaac   dm->ops->computel2gradientdiff     = DMComputeL2GradientDiff_Plex;
57402a16baeaSToby Isaac   dm->ops->computel2fielddiff        = DMComputeL2FieldDiff_Plex;
574128d58a37SPierre Jolivet   dm->ops->getneighbors              = DMGetNeighbors_Plex;
57426c6a6b79SMatthew G. Knepley   dm->ops->getlocalboundingbox       = DMGetLocalBoundingBox_Coordinates;
5743907a3e9cSStefano Zampini   dm->ops->createdomaindecomposition = DMCreateDomainDecomposition_Plex;
5744907a3e9cSStefano Zampini   dm->ops->createddscatters          = DMCreateDomainDecompositionScatters_Plex;
57459566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertBoundaryValues_C", DMPlexInsertBoundaryValues_Plex));
57466c51210dSStefano Zampini   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertTimeDerivativeBoundaryValues_C", DMPlexInsertTimeDerivativeBoundaryValues_Plex));
574701468941SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertBounds_C", DMPlexInsertBounds_Plex));
57489566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMSetUpGLVisViewer_C", DMSetUpGLVisViewer_Plex));
57499566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMCreateNeumannOverlap_C", DMCreateNeumannOverlap_Plex));
57509566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeGetDefault_C", DMPlexDistributeGetDefault_Plex));
57519566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeSetDefault_C", DMPlexDistributeSetDefault_Plex));
57526bc1bd01Sksagiyam   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderGetDefault_C", DMPlexReorderGetDefault_Plex));
57536bc1bd01Sksagiyam   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderSetDefault_C", DMPlexReorderSetDefault_Plex));
5754adc21957SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionGetDefault_C", DMReorderSectionGetDefault_Plex));
5755adc21957SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionSetDefault_C", DMReorderSectionSetDefault_Plex));
5756adc21957SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionGetType_C", DMReorderSectionGetType_Plex));
5757adc21957SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMReorderSectionSetType_C", DMReorderSectionSetType_Plex));
57589566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMInterpolateSolution_C", DMInterpolateSolution_Plex));
5759c506a872SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetOverlap_C", DMPlexGetOverlap_Plex));
5760c506a872SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexSetOverlap_C", DMPlexSetOverlap_Plex));
5761d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetUseCeed_C", DMPlexGetUseCeed_Plex));
5762d2b2dc1eSMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexSetUseCeed_C", DMPlexSetUseCeed_Plex));
57633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5764552f7358SJed Brown }
5765552f7358SJed Brown 
5766d71ae5a4SJacob Faibussowitsch PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm)
5767d71ae5a4SJacob Faibussowitsch {
576863a16f15SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *)dm->data;
57691fca310dSJames Wright   const PetscSF *face_sfs;
57701fca310dSJames Wright   PetscInt       num_face_sfs;
577163a16f15SMatthew G. Knepley 
577263a16f15SMatthew G. Knepley   PetscFunctionBegin;
577363a16f15SMatthew G. Knepley   mesh->refct++;
577463a16f15SMatthew G. Knepley   (*newdm)->data = mesh;
57751fca310dSJames Wright   PetscCall(DMPlexGetIsoperiodicFaceSF(dm, &num_face_sfs, &face_sfs));
57761fca310dSJames Wright   PetscCall(DMPlexSetIsoperiodicFaceSF(*newdm, num_face_sfs, (PetscSF *)face_sfs));
57779566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)*newdm, DMPLEX));
57789566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(*newdm));
57793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
578063a16f15SMatthew G. Knepley }
578163a16f15SMatthew G. Knepley 
57828818961aSMatthew G Knepley /*MC
57830b4b7b1cSBarry Smith   DMPLEX = "plex" - A `DM` object that encapsulates an unstructured mesh (or grid), or CW Complex {cite}`cwcomplex`,
57840b4b7b1cSBarry Smith            which can be expressed using a Hasse Diagram {cite}`hassediagram`.
578520f4b53cSBarry Smith            In the local representation, `Vec`s contain all unknowns in the interior and shared boundary. This is
57860b4b7b1cSBarry Smith            specified by a `PetscSection` object. Ownership in the global representation is determined by
5787a1cb98faSBarry Smith            ownership of the underlying `DMPLEX` points. This is specified by another `PetscSection` object.
57888818961aSMatthew G Knepley 
5789e5893cccSMatthew G. Knepley   Options Database Keys:
5790250712c9SMatthew G. Knepley + -dm_refine_pre                     - Refine mesh before distribution
5791250712c9SMatthew G. Knepley + -dm_refine_uniform_pre             - Choose uniform or generator-based refinement
5792250712c9SMatthew G. Knepley + -dm_refine_volume_limit_pre        - Cell volume limit after pre-refinement using generator
5793250712c9SMatthew G. Knepley . -dm_distribute                     - Distribute mesh across processes
5794250712c9SMatthew G. Knepley . -dm_distribute_overlap             - Number of cells to overlap for distribution
5795250712c9SMatthew G. Knepley . -dm_refine                         - Refine mesh after distribution
5796c3db174cSMatthew G. Knepley . -dm_localize <bool>                - Whether to localize coordinates for periodic meshes
5797c3db174cSMatthew G. Knepley . -dm_sparse_localize <bool>         - Whether to only localize cells on the periodic boundary
5798250712c9SMatthew G. Knepley . -dm_plex_hash_location             - Use grid hashing for point location
5799ddce0771SMatthew G. Knepley . -dm_plex_hash_box_faces <n,m,p>    - The number of divisions in each direction of the grid hash
5800f12cf164SMatthew G. Knepley . -dm_plex_partition_balance         - Attempt to evenly divide points on partition boundary between processes
5801f12cf164SMatthew G. Knepley . -dm_plex_remesh_bd                 - Allow changes to the boundary on remeshing
5802d5b43468SJose E. Roman . -dm_plex_max_projection_height     - Maximum mesh point height used to project locally
5803f12cf164SMatthew G. Knepley . -dm_plex_regular_refinement        - Use special nested projection algorithm for regular refinement
5804d02c7345SMatthew G. Knepley . -dm_plex_reorder_section           - Use specialized blocking if available
5805aaa8cc7dSPierre Jolivet . -dm_plex_check_all                 - Perform all checks below
5806f12cf164SMatthew G. Knepley . -dm_plex_check_symmetry            - Check that the adjacency information in the mesh is symmetric
5807f12cf164SMatthew G. Knepley . -dm_plex_check_skeleton <celltype> - Check that each cell has the correct number of vertices
5808f12cf164SMatthew 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
5809f12cf164SMatthew G. Knepley . -dm_plex_check_geometry            - Check that cells have positive volume
5810f12cf164SMatthew G. Knepley . -dm_view :mesh.tex:ascii_latex     - View the mesh in LaTeX/TikZ
5811e5893cccSMatthew G. Knepley . -dm_plex_view_scale <num>          - Scale the TikZ
58125962854dSMatthew G. Knepley . -dm_plex_print_fem <num>           - View FEM assembly information, such as element vectors and matrices
58135962854dSMatthew G. Knepley - -dm_plex_print_fvm <num>           - View FVM assembly information, such as flux updates
5814e5893cccSMatthew G. Knepley 
58158818961aSMatthew G Knepley   Level: intermediate
58168818961aSMatthew G Knepley 
58171cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMType`, `DMPlexCreate()`, `DMCreate()`, `DMSetType()`, `PetscSection`
58188818961aSMatthew G Knepley M*/
58198818961aSMatthew G Knepley 
5820d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode DMCreate_Plex(DM dm)
5821d71ae5a4SJacob Faibussowitsch {
5822552f7358SJed Brown   DM_Plex *mesh;
5823412e9a14SMatthew G. Knepley   PetscInt unit;
5824552f7358SJed Brown 
5825552f7358SJed Brown   PetscFunctionBegin;
5826f39ec787SMatthew G. Knepley   PetscCall(PetscCitationsRegister(PlexCitation, &Plexcite));
5827552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
58284dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&mesh));
5829adc21957SMatthew G. Knepley   dm->reorderSection = DM_REORDER_DEFAULT_NOTSET;
5830552f7358SJed Brown   dm->data           = mesh;
5831552f7358SJed Brown 
5832552f7358SJed Brown   mesh->refct = 1;
58339566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->coneSection));
58349566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->supportSection));
5835552f7358SJed Brown   mesh->refinementUniform       = PETSC_TRUE;
5836552f7358SJed Brown   mesh->refinementLimit         = -1.0;
5837e600fa54SMatthew G. Knepley   mesh->distDefault             = PETSC_TRUE;
5838adc21957SMatthew G. Knepley   mesh->reorderDefault          = DM_REORDER_DEFAULT_NOTSET;
58391d1f2f2aSksagiyam   mesh->distributionName        = NULL;
58407d0f5628SVaclav Hapla   mesh->interpolated            = DMPLEX_INTERPOLATED_INVALID;
58417d0f5628SVaclav Hapla   mesh->interpolatedCollective  = DMPLEX_INTERPOLATED_INVALID;
58425e2c5519SMatthew G. Knepley   mesh->interpolatePreferTensor = PETSC_TRUE;
5843552f7358SJed Brown 
58449566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerCreate(PetscObjectComm((PetscObject)dm), &mesh->partitioner));
58452e62ab5aSMatthew G. Knepley   mesh->remeshBd = PETSC_FALSE;
5846d9deefdfSMatthew G. Knepley 
58478865f1eaSKarl Rupp   for (unit = 0; unit < NUM_PETSC_UNITS; ++unit) mesh->scale[unit] = 1.0;
5848552f7358SJed Brown 
5849df0420ecSMatthew G. Knepley   mesh->depthState    = -1;
5850ba2698f1SMatthew G. Knepley   mesh->celltypeState = -1;
58516113b454SMatthew G. Knepley   mesh->printTol      = 1.0e-10;
5852c29ce622SStefano Zampini   mesh->nonempty_comm = MPI_COMM_SELF;
5853552f7358SJed Brown 
58549566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(dm));
58553ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5856552f7358SJed Brown }
5857552f7358SJed Brown 
5858552f7358SJed Brown /*@
5859a1cb98faSBarry Smith   DMPlexCreate - Creates a `DMPLEX` object, which encapsulates an unstructured mesh, or CW complex, which can be expressed using a Hasse Diagram.
5860552f7358SJed Brown 
5861d083f849SBarry Smith   Collective
5862552f7358SJed Brown 
5863552f7358SJed Brown   Input Parameter:
5864a1cb98faSBarry Smith . comm - The communicator for the `DMPLEX` object
5865552f7358SJed Brown 
5866552f7358SJed Brown   Output Parameter:
5867a1cb98faSBarry Smith . mesh - The `DMPLEX` object
5868552f7358SJed Brown 
5869552f7358SJed Brown   Level: beginner
5870552f7358SJed Brown 
587142747ad1SJacob Faibussowitsch .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMType`, `DMCreate()`, `DMSetType()`
5872552f7358SJed Brown @*/
5873d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreate(MPI_Comm comm, DM *mesh)
5874d71ae5a4SJacob Faibussowitsch {
5875552f7358SJed Brown   PetscFunctionBegin;
58764f572ea9SToby Isaac   PetscAssertPointer(mesh, 2);
58779566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, mesh));
58789566063dSJacob Faibussowitsch   PetscCall(DMSetType(*mesh, DMPLEX));
58793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
5880552f7358SJed Brown }
5881552f7358SJed Brown 
5882b09969d6SVaclav Hapla /*@C
5883b0fe842aSMatthew 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
5884a1cb98faSBarry Smith 
588520f4b53cSBarry Smith   Collective; No Fortran Support
5886b09969d6SVaclav Hapla 
5887b09969d6SVaclav Hapla   Input Parameters:
5888a1cb98faSBarry Smith + dm          - The `DM`
5889b09969d6SVaclav Hapla . numCells    - The number of cells owned by this process
5890a1cb98faSBarry Smith . numVertices - The number of vertices to be owned by this process, or `PETSC_DECIDE`
5891a1cb98faSBarry Smith . NVertices   - The global number of vertices, or `PETSC_DETERMINE`
5892b09969d6SVaclav Hapla . numCorners  - The number of vertices for each cell
58935e488331SVaclav Hapla - cells       - An array of numCells*numCorners numbers, the global vertex numbers for each cell
5894b09969d6SVaclav Hapla 
5895be8c289dSNicolas Barral   Output Parameters:
5896a1cb98faSBarry Smith + vertexSF         - (Optional) `PetscSF` describing complete vertex ownership
5897be8c289dSNicolas Barral - verticesAdjSaved - (Optional) vertex adjacency array
5898b09969d6SVaclav Hapla 
5899b09969d6SVaclav Hapla   Level: advanced
5900b09969d6SVaclav Hapla 
5901a1cb98faSBarry Smith   Notes:
5902a1cb98faSBarry Smith   Two triangles sharing a face
5903a1cb98faSBarry Smith .vb
5904a1cb98faSBarry Smith 
5905a1cb98faSBarry Smith         2
5906a1cb98faSBarry Smith       / | \
5907a1cb98faSBarry Smith      /  |  \
5908a1cb98faSBarry Smith     /   |   \
5909a1cb98faSBarry Smith    0  0 | 1  3
5910a1cb98faSBarry Smith     \   |   /
5911a1cb98faSBarry Smith      \  |  /
5912a1cb98faSBarry Smith       \ | /
5913a1cb98faSBarry Smith         1
5914a1cb98faSBarry Smith .ve
5915a1cb98faSBarry Smith   would have input
5916a1cb98faSBarry Smith .vb
5917a1cb98faSBarry Smith   numCells = 2, numVertices = 4
5918a1cb98faSBarry Smith   cells = [0 1 2  1 3 2]
5919a1cb98faSBarry Smith .ve
5920a1cb98faSBarry Smith   which would result in the `DMPLEX`
5921a1cb98faSBarry Smith .vb
5922a1cb98faSBarry Smith 
5923a1cb98faSBarry Smith         4
5924a1cb98faSBarry Smith       / | \
5925a1cb98faSBarry Smith      /  |  \
5926a1cb98faSBarry Smith     /   |   \
5927a1cb98faSBarry Smith    2  0 | 1  5
5928a1cb98faSBarry Smith     \   |   /
5929a1cb98faSBarry Smith      \  |  /
5930a1cb98faSBarry Smith       \ | /
5931a1cb98faSBarry Smith         3
5932a1cb98faSBarry Smith .ve
5933a1cb98faSBarry Smith 
5934a1cb98faSBarry Smith   Vertices are implicitly numbered consecutively 0,...,NVertices.
5935a1cb98faSBarry Smith   Each rank owns a chunk of numVertices consecutive vertices.
5936a1cb98faSBarry Smith   If numVertices is `PETSC_DECIDE`, PETSc will distribute them as evenly as possible using PetscLayout.
5937a1cb98faSBarry Smith   If NVertices is `PETSC_DETERMINE` and numVertices is PETSC_DECIDE, NVertices is computed by PETSc as the maximum vertex index in cells + 1.
5938a1cb98faSBarry Smith   If only NVertices is `PETSC_DETERMINE`, it is computed as the sum of numVertices over all ranks.
5939a1cb98faSBarry Smith 
5940a1cb98faSBarry Smith   The cell distribution is arbitrary non-overlapping, independent of the vertex distribution.
5941a1cb98faSBarry Smith 
59421cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildFromCellList()`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildCoordinatesFromCellListParallel()`,
5943a1cb98faSBarry Smith           `PetscSF`
5944b09969d6SVaclav Hapla @*/
5945ce78bad3SBarry Smith PetscErrorCode DMPlexBuildFromCellListParallel(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt NVertices, PetscInt numCorners, const PetscInt cells[], PeOp PetscSF *vertexSF, PeOp PetscInt *verticesAdjSaved[])
5946d71ae5a4SJacob Faibussowitsch {
59472464107aSksagiyam   PetscSF     sfPoint;
59482464107aSksagiyam   PetscLayout layout;
594982fb893eSVaclav Hapla   PetscInt    numVerticesAdj, *verticesAdj, *cones, c, p;
5950a47d0d45SMatthew G. Knepley 
5951a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
595225b6865aSVaclav Hapla   PetscValidLogicalCollectiveInt(dm, NVertices, 4);
59539566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
595425b6865aSVaclav Hapla   /* Get/check global number of vertices */
595525b6865aSVaclav Hapla   {
595625b6865aSVaclav Hapla     PetscInt       NVerticesInCells, i;
595725b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
595825b6865aSVaclav Hapla 
595925b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
59601690c2aeSBarry Smith     NVerticesInCells = PETSC_INT_MIN;
59619371c9d4SSatish Balay     for (i = 0; i < len; i++)
59629371c9d4SSatish Balay       if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
596325b6865aSVaclav Hapla     ++NVerticesInCells;
5964462c564dSBarry Smith     PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &NVerticesInCells, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
596525b6865aSVaclav Hapla 
596625b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE && NVertices == PETSC_DECIDE) NVertices = NVerticesInCells;
59679371c9d4SSatish Balay     else
59689371c9d4SSatish 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);
596925b6865aSVaclav Hapla   }
59709079aca8SVaclav Hapla   /* Count locally unique vertices */
59719079aca8SVaclav Hapla   {
59729079aca8SVaclav Hapla     PetscHSetI vhash;
59739079aca8SVaclav Hapla     PetscInt   off = 0;
59749079aca8SVaclav Hapla 
59759566063dSJacob Faibussowitsch     PetscCall(PetscHSetICreate(&vhash));
5976a47d0d45SMatthew G. Knepley     for (c = 0; c < numCells; ++c) {
597748a46eb9SPierre Jolivet       for (p = 0; p < numCorners; ++p) PetscCall(PetscHSetIAdd(vhash, cells[c * numCorners + p]));
5978a47d0d45SMatthew G. Knepley     }
59799566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetSize(vhash, &numVerticesAdj));
59809566063dSJacob Faibussowitsch     if (!verticesAdjSaved) PetscCall(PetscMalloc1(numVerticesAdj, &verticesAdj));
5981ad540459SPierre Jolivet     else verticesAdj = *verticesAdjSaved;
59829566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetElems(vhash, &off, verticesAdj));
59839566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&vhash));
598463a3b9bcSJacob Faibussowitsch     PetscCheck(off == numVerticesAdj, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid number of local vertices %" PetscInt_FMT " should be %" PetscInt_FMT, off, numVerticesAdj);
5985a47d0d45SMatthew G. Knepley   }
59869566063dSJacob Faibussowitsch   PetscCall(PetscSortInt(numVerticesAdj, verticesAdj));
5987a47d0d45SMatthew G. Knepley   /* Create cones */
59889566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, 0, numCells + numVerticesAdj));
59899566063dSJacob Faibussowitsch   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, numCorners));
59909566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm));
59919566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCones(dm, &cones));
5992a47d0d45SMatthew G. Knepley   for (c = 0; c < numCells; ++c) {
5993a47d0d45SMatthew G. Knepley     for (p = 0; p < numCorners; ++p) {
5994a47d0d45SMatthew G. Knepley       const PetscInt gv = cells[c * numCorners + p];
5995a47d0d45SMatthew G. Knepley       PetscInt       lv;
5996a47d0d45SMatthew G. Knepley 
59979079aca8SVaclav Hapla       /* Positions within verticesAdj form 0-based local vertex numbering;
59989079aca8SVaclav Hapla          we need to shift it by numCells to get correct DAG points (cells go first) */
59999566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv));
600063a3b9bcSJacob Faibussowitsch       PetscCheck(lv >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not find global vertex %" PetscInt_FMT " in local connectivity", gv);
6001961cfab0SVaclav Hapla       cones[c * numCorners + p] = lv + numCells;
6002a47d0d45SMatthew G. Knepley     }
6003a47d0d45SMatthew G. Knepley   }
60042464107aSksagiyam   /* Build point sf */
60059566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(PetscObjectComm((PetscObject)dm), &layout));
60069566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetSize(layout, NVertices));
60079566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, numVertices));
60089566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
60099566063dSJacob Faibussowitsch   PetscCall(PetscSFCreateByMatchingIndices(layout, numVerticesAdj, verticesAdj, NULL, numCells, numVerticesAdj, verticesAdj, NULL, numCells, vertexSF, &sfPoint));
60109566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
60119566063dSJacob Faibussowitsch   if (!verticesAdjSaved) PetscCall(PetscFree(verticesAdj));
60129566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)sfPoint, "point SF"));
60132464107aSksagiyam   if (dm->sf) {
60142464107aSksagiyam     const char *prefix;
60152464107aSksagiyam 
60169566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm->sf, &prefix));
60179566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)sfPoint, prefix));
60182464107aSksagiyam   }
60199566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dm, sfPoint));
60209566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&sfPoint));
6021f4f49eeaSPierre Jolivet   if (vertexSF) PetscCall(PetscObjectSetName((PetscObject)*vertexSF, "Vertex Ownership SF"));
6022a47d0d45SMatthew G. Knepley   /* Fill in the rest of the topology structure */
60239566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
60249566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
60259566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
60263ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6027a47d0d45SMatthew G. Knepley }
6028a47d0d45SMatthew G. Knepley 
6029b0fe842aSMatthew G. Knepley /*@C
6030b0fe842aSMatthew G. Knepley   DMPlexBuildFromCellSectionParallel - Build distributed `DMPLEX` topology from a list of vertices for each cell (common mesh generator output) allowing multiple celltypes
6031b0fe842aSMatthew G. Knepley 
6032b0fe842aSMatthew G. Knepley   Collective; No Fortran Support
6033b0fe842aSMatthew G. Knepley 
6034b0fe842aSMatthew G. Knepley   Input Parameters:
6035b0fe842aSMatthew G. Knepley + dm          - The `DM`
6036b0fe842aSMatthew G. Knepley . numCells    - The number of cells owned by this process
6037b0fe842aSMatthew G. Knepley . numVertices - The number of vertices to be owned by this process, or `PETSC_DECIDE`
6038b0fe842aSMatthew G. Knepley . NVertices   - The global number of vertices, or `PETSC_DETERMINE`
6039b0fe842aSMatthew G. Knepley . cellSection - The `PetscSection` giving the number of vertices for each cell (layout of cells)
6040b0fe842aSMatthew G. Knepley - cells       - An array of the global vertex numbers for each cell
6041b0fe842aSMatthew G. Knepley 
6042b0fe842aSMatthew G. Knepley   Output Parameters:
6043b0fe842aSMatthew G. Knepley + vertexSF         - (Optional) `PetscSF` describing complete vertex ownership
6044b0fe842aSMatthew G. Knepley - verticesAdjSaved - (Optional) vertex adjacency array
6045b0fe842aSMatthew G. Knepley 
6046b0fe842aSMatthew G. Knepley   Level: advanced
6047b0fe842aSMatthew G. Knepley 
6048b0fe842aSMatthew G. Knepley   Notes:
6049b0fe842aSMatthew G. Knepley   A triangle and quadrilateral sharing a face
6050b0fe842aSMatthew G. Knepley .vb
6051b0fe842aSMatthew G. Knepley         2----------3
6052b0fe842aSMatthew G. Knepley       / |          |
6053b0fe842aSMatthew G. Knepley      /  |          |
6054b0fe842aSMatthew G. Knepley     /   |          |
6055b0fe842aSMatthew G. Knepley    0  0 |     1    |
6056b0fe842aSMatthew G. Knepley     \   |          |
6057b0fe842aSMatthew G. Knepley      \  |          |
6058b0fe842aSMatthew G. Knepley       \ |          |
6059b0fe842aSMatthew G. Knepley         1----------4
6060b0fe842aSMatthew G. Knepley .ve
6061b0fe842aSMatthew G. Knepley   would have input
6062b0fe842aSMatthew G. Knepley .vb
6063b0fe842aSMatthew G. Knepley   numCells = 2, numVertices = 5
6064b0fe842aSMatthew G. Knepley   cells = [0 1 2  1 4 3 2]
6065b0fe842aSMatthew G. Knepley .ve
6066b0fe842aSMatthew G. Knepley   which would result in the `DMPLEX`
6067b0fe842aSMatthew G. Knepley .vb
6068b0fe842aSMatthew G. Knepley         4----------5
6069b0fe842aSMatthew G. Knepley       / |          |
6070b0fe842aSMatthew G. Knepley      /  |          |
6071b0fe842aSMatthew G. Knepley     /   |          |
6072b0fe842aSMatthew G. Knepley    2  0 |     1    |
6073b0fe842aSMatthew G. Knepley     \   |          |
6074b0fe842aSMatthew G. Knepley      \  |          |
6075b0fe842aSMatthew G. Knepley       \ |          |
6076b0fe842aSMatthew G. Knepley         3----------6
6077b0fe842aSMatthew G. Knepley .ve
6078b0fe842aSMatthew G. Knepley 
6079b0fe842aSMatthew G. Knepley   Vertices are implicitly numbered consecutively 0,...,NVertices.
6080b0fe842aSMatthew G. Knepley   Each rank owns a chunk of numVertices consecutive vertices.
6081b0fe842aSMatthew G. Knepley   If numVertices is `PETSC_DECIDE`, PETSc will distribute them as evenly as possible using PetscLayout.
6082b0fe842aSMatthew 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.
6083b0fe842aSMatthew G. Knepley   If only NVertices is `PETSC_DETERMINE`, it is computed as the sum of numVertices over all ranks.
6084b0fe842aSMatthew G. Knepley 
6085b0fe842aSMatthew G. Knepley   The cell distribution is arbitrary non-overlapping, independent of the vertex distribution.
6086b0fe842aSMatthew G. Knepley 
6087b0fe842aSMatthew G. Knepley .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildFromCellListParallel()`, `DMPlexCreateFromCellSectionParallel()`, `DMPlexBuildCoordinatesFromCellListParallel()`,
6088b0fe842aSMatthew G. Knepley           `PetscSF`
6089b0fe842aSMatthew G. Knepley @*/
6090ce78bad3SBarry Smith PetscErrorCode DMPlexBuildFromCellSectionParallel(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt NVertices, PetscSection cellSection, const PetscInt cells[], PeOp PetscSF *vertexSF, PeOp PetscInt *verticesAdjSaved[])
6091b0fe842aSMatthew G. Knepley {
6092b0fe842aSMatthew G. Knepley   PetscSF     sfPoint;
6093b0fe842aSMatthew G. Knepley   PetscLayout layout;
6094b0fe842aSMatthew G. Knepley   PetscInt    numVerticesAdj, *verticesAdj, *cones, cStart, cEnd, len;
6095b0fe842aSMatthew G. Knepley 
6096b0fe842aSMatthew G. Knepley   PetscFunctionBegin;
6097b0fe842aSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, NVertices, 4);
6098b0fe842aSMatthew G. Knepley   PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
6099b0fe842aSMatthew G. Knepley   PetscCall(PetscSectionGetChart(cellSection, &cStart, &cEnd));
6100b0fe842aSMatthew G. Knepley   PetscCall(PetscSectionGetStorageSize(cellSection, &len));
6101b0fe842aSMatthew 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);
6102b0fe842aSMatthew G. Knepley   /* Get/check global number of vertices */
6103b0fe842aSMatthew G. Knepley   {
6104b0fe842aSMatthew G. Knepley     PetscInt NVerticesInCells;
6105b0fe842aSMatthew G. Knepley 
6106b0fe842aSMatthew G. Knepley     /* NVerticesInCells = max(cells) + 1 */
6107b0fe842aSMatthew G. Knepley     NVerticesInCells = PETSC_MIN_INT;
6108b0fe842aSMatthew G. Knepley     for (PetscInt i = 0; i < len; i++)
6109b0fe842aSMatthew G. Knepley       if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
6110b0fe842aSMatthew G. Knepley     ++NVerticesInCells;
6111b0fe842aSMatthew G. Knepley     PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &NVerticesInCells, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
6112b0fe842aSMatthew G. Knepley 
6113b0fe842aSMatthew G. Knepley     if (numVertices == PETSC_DECIDE && NVertices == PETSC_DECIDE) NVertices = NVerticesInCells;
6114b0fe842aSMatthew G. Knepley     else
6115b0fe842aSMatthew 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);
6116b0fe842aSMatthew G. Knepley   }
6117b0fe842aSMatthew G. Knepley   /* Count locally unique vertices */
6118b0fe842aSMatthew G. Knepley   {
6119b0fe842aSMatthew G. Knepley     PetscHSetI vhash;
6120b0fe842aSMatthew G. Knepley     PetscInt   off = 0;
6121b0fe842aSMatthew G. Knepley 
6122b0fe842aSMatthew G. Knepley     PetscCall(PetscHSetICreate(&vhash));
6123b0fe842aSMatthew G. Knepley     for (PetscInt i = 0; i < len; i++) PetscCall(PetscHSetIAdd(vhash, cells[i]));
6124b0fe842aSMatthew G. Knepley     PetscCall(PetscHSetIGetSize(vhash, &numVerticesAdj));
6125b0fe842aSMatthew G. Knepley     if (!verticesAdjSaved) PetscCall(PetscMalloc1(numVerticesAdj, &verticesAdj));
6126b0fe842aSMatthew G. Knepley     else verticesAdj = *verticesAdjSaved;
6127b0fe842aSMatthew G. Knepley     PetscCall(PetscHSetIGetElems(vhash, &off, verticesAdj));
6128b0fe842aSMatthew G. Knepley     PetscCall(PetscHSetIDestroy(&vhash));
6129b0fe842aSMatthew 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);
6130b0fe842aSMatthew G. Knepley   }
6131b0fe842aSMatthew G. Knepley   PetscCall(PetscSortInt(numVerticesAdj, verticesAdj));
6132b0fe842aSMatthew G. Knepley   /* Create cones */
6133b0fe842aSMatthew G. Knepley   PetscCall(DMPlexSetChart(dm, 0, numCells + numVerticesAdj));
6134b0fe842aSMatthew G. Knepley   for (PetscInt c = 0; c < numCells; ++c) {
6135b0fe842aSMatthew G. Knepley     PetscInt dof;
6136b0fe842aSMatthew G. Knepley 
6137b0fe842aSMatthew G. Knepley     PetscCall(PetscSectionGetDof(cellSection, c, &dof));
6138b0fe842aSMatthew G. Knepley     PetscCall(DMPlexSetConeSize(dm, c, dof));
6139b0fe842aSMatthew G. Knepley   }
6140b0fe842aSMatthew G. Knepley   PetscCall(DMSetUp(dm));
6141b0fe842aSMatthew G. Knepley   PetscCall(DMPlexGetCones(dm, &cones));
6142b0fe842aSMatthew G. Knepley   for (PetscInt c = 0; c < numCells; ++c) {
6143b0fe842aSMatthew G. Knepley     PetscInt dof, off;
6144b0fe842aSMatthew G. Knepley 
6145b0fe842aSMatthew G. Knepley     PetscCall(PetscSectionGetDof(cellSection, c, &dof));
6146b0fe842aSMatthew G. Knepley     PetscCall(PetscSectionGetOffset(cellSection, c, &off));
6147b0fe842aSMatthew G. Knepley     for (PetscInt p = off; p < off + dof; ++p) {
6148b0fe842aSMatthew G. Knepley       const PetscInt gv = cells[p];
6149b0fe842aSMatthew G. Knepley       PetscInt       lv;
6150b0fe842aSMatthew G. Knepley 
6151b0fe842aSMatthew G. Knepley       /* Positions within verticesAdj form 0-based local vertex numbering;
6152b0fe842aSMatthew G. Knepley          we need to shift it by numCells to get correct DAG points (cells go first) */
6153b0fe842aSMatthew G. Knepley       PetscCall(PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv));
6154b0fe842aSMatthew G. Knepley       PetscCheck(lv >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not find global vertex %" PetscInt_FMT " in local connectivity", gv);
6155b0fe842aSMatthew G. Knepley       cones[p] = lv + numCells;
6156b0fe842aSMatthew G. Knepley     }
6157b0fe842aSMatthew G. Knepley   }
6158b0fe842aSMatthew G. Knepley   /* Build point sf */
6159b0fe842aSMatthew G. Knepley   PetscCall(PetscLayoutCreate(PetscObjectComm((PetscObject)dm), &layout));
6160b0fe842aSMatthew G. Knepley   PetscCall(PetscLayoutSetSize(layout, NVertices));
6161b0fe842aSMatthew G. Knepley   PetscCall(PetscLayoutSetLocalSize(layout, numVertices));
6162b0fe842aSMatthew G. Knepley   PetscCall(PetscLayoutSetBlockSize(layout, 1));
6163b0fe842aSMatthew G. Knepley   PetscCall(PetscSFCreateByMatchingIndices(layout, numVerticesAdj, verticesAdj, NULL, numCells, numVerticesAdj, verticesAdj, NULL, numCells, vertexSF, &sfPoint));
6164b0fe842aSMatthew G. Knepley   PetscCall(PetscLayoutDestroy(&layout));
6165b0fe842aSMatthew G. Knepley   if (!verticesAdjSaved) PetscCall(PetscFree(verticesAdj));
6166b0fe842aSMatthew G. Knepley   PetscCall(PetscObjectSetName((PetscObject)sfPoint, "point SF"));
6167b0fe842aSMatthew G. Knepley   if (dm->sf) {
6168b0fe842aSMatthew G. Knepley     const char *prefix;
6169b0fe842aSMatthew G. Knepley 
6170b0fe842aSMatthew G. Knepley     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm->sf, &prefix));
6171b0fe842aSMatthew G. Knepley     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)sfPoint, prefix));
6172b0fe842aSMatthew G. Knepley   }
6173b0fe842aSMatthew G. Knepley   PetscCall(DMSetPointSF(dm, sfPoint));
6174b0fe842aSMatthew G. Knepley   PetscCall(PetscSFDestroy(&sfPoint));
6175b0fe842aSMatthew G. Knepley   if (vertexSF) PetscCall(PetscObjectSetName((PetscObject)*vertexSF, "Vertex Ownership SF"));
6176b0fe842aSMatthew G. Knepley   /* Fill in the rest of the topology structure */
6177b0fe842aSMatthew G. Knepley   PetscCall(DMPlexSymmetrize(dm));
6178b0fe842aSMatthew G. Knepley   PetscCall(DMPlexStratify(dm));
6179b0fe842aSMatthew G. Knepley   PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
6180b0fe842aSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
6181b0fe842aSMatthew G. Knepley }
6182b0fe842aSMatthew G. Knepley 
6183cc4c1da9SBarry Smith /*@
6184a1cb98faSBarry Smith   DMPlexBuildCoordinatesFromCellListParallel - Build `DM` coordinates from a list of coordinates for each owned vertex (common mesh generator output)
6185a1cb98faSBarry Smith 
618620f4b53cSBarry Smith   Collective; No Fortran Support
6187b09969d6SVaclav Hapla 
6188b09969d6SVaclav Hapla   Input Parameters:
6189a1cb98faSBarry Smith + dm           - The `DM`
6190b09969d6SVaclav Hapla . spaceDim     - The spatial dimension used for coordinates
6191a1cb98faSBarry Smith . sfVert       - `PetscSF` describing complete vertex ownership
6192b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
6193b09969d6SVaclav Hapla 
6194b09969d6SVaclav Hapla   Level: advanced
6195b09969d6SVaclav Hapla 
61961cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildFromCellListParallel()`
6197b09969d6SVaclav Hapla @*/
6198d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildCoordinatesFromCellListParallel(DM dm, PetscInt spaceDim, PetscSF sfVert, const PetscReal vertexCoords[])
6199d71ae5a4SJacob Faibussowitsch {
6200a47d0d45SMatthew G. Knepley   PetscSection coordSection;
6201a47d0d45SMatthew G. Knepley   Vec          coordinates;
6202a47d0d45SMatthew G. Knepley   PetscScalar *coords;
62031edcf0b2SVaclav Hapla   PetscInt     numVertices, numVerticesAdj, coordSize, v, vStart, vEnd;
6204835f2295SStefano Zampini   PetscMPIInt  spaceDimi;
6205a47d0d45SMatthew G. Knepley 
6206a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
62079566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
62089566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
62091dca8a05SBarry Smith   PetscCheck(vStart >= 0 && vEnd >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
62109566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, spaceDim));
62119566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfVert, &numVertices, &numVerticesAdj, NULL, NULL));
62121dca8a05SBarry 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);
62139566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
62149566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
62159566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, spaceDim));
62169566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, vStart, vEnd));
62171edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
62189566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, spaceDim));
62199566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, spaceDim));
6220a47d0d45SMatthew G. Knepley   }
62219566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
62229566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
62239566063dSJacob Faibussowitsch   PetscCall(VecCreate(PetscObjectComm((PetscObject)dm), &coordinates));
62249566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, spaceDim));
62259566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
62269566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
62279566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
62289566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
6229a47d0d45SMatthew G. Knepley   {
6230a47d0d45SMatthew G. Knepley     MPI_Datatype coordtype;
6231a47d0d45SMatthew G. Knepley 
6232a47d0d45SMatthew G. Knepley     /* Need a temp buffer for coords if we have complex/single */
6233835f2295SStefano Zampini     PetscCall(PetscMPIIntCast(spaceDim, &spaceDimi));
6234835f2295SStefano Zampini     PetscCallMPI(MPI_Type_contiguous(spaceDimi, MPIU_SCALAR, &coordtype));
62359566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_commit(&coordtype));
623621016a8bSBarry Smith #if defined(PETSC_USE_COMPLEX)
623721016a8bSBarry Smith     {
623821016a8bSBarry Smith       PetscScalar *svertexCoords;
623921016a8bSBarry Smith       PetscInt     i;
62409566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numVertices * spaceDim, &svertexCoords));
62413612f820SVaclav Hapla       for (i = 0; i < numVertices * spaceDim; i++) svertexCoords[i] = vertexCoords[i];
62429566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(sfVert, coordtype, svertexCoords, coords, MPI_REPLACE));
62439566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(sfVert, coordtype, svertexCoords, coords, MPI_REPLACE));
62449566063dSJacob Faibussowitsch       PetscCall(PetscFree(svertexCoords));
624521016a8bSBarry Smith     }
624621016a8bSBarry Smith #else
62479566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sfVert, coordtype, vertexCoords, coords, MPI_REPLACE));
62489566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sfVert, coordtype, vertexCoords, coords, MPI_REPLACE));
624921016a8bSBarry Smith #endif
62509566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_free(&coordtype));
6251a47d0d45SMatthew G. Knepley   }
62529566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
62539566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
62549566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
62559566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
62563ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6257a47d0d45SMatthew G. Knepley }
6258a47d0d45SMatthew G. Knepley 
6259c3edce3dSSatish Balay /*@
6260b0fe842aSMatthew 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
6261a1cb98faSBarry Smith 
6262a1cb98faSBarry Smith   Collective
6263a47d0d45SMatthew G. Knepley 
6264a47d0d45SMatthew G. Knepley   Input Parameters:
6265a47d0d45SMatthew G. Knepley + comm         - The communicator
6266a47d0d45SMatthew G. Knepley . dim          - The topological dimension of the mesh
6267a47d0d45SMatthew G. Knepley . numCells     - The number of cells owned by this process
6268a1cb98faSBarry Smith . numVertices  - The number of vertices owned by this process, or `PETSC_DECIDE`
6269a1cb98faSBarry Smith . NVertices    - The global number of vertices, or `PETSC_DECIDE`
6270a47d0d45SMatthew G. Knepley . numCorners   - The number of vertices for each cell
6271a47d0d45SMatthew G. Knepley . interpolate  - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
6272a47d0d45SMatthew G. Knepley . cells        - An array of numCells*numCorners numbers, the global vertex numbers for each cell
6273a47d0d45SMatthew G. Knepley . spaceDim     - The spatial dimension used for coordinates
6274a47d0d45SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
6275a47d0d45SMatthew G. Knepley 
6276d8d19677SJose E. Roman   Output Parameters:
6277a1cb98faSBarry Smith + dm          - The `DM`
6278a1cb98faSBarry Smith . vertexSF    - (Optional) `PetscSF` describing complete vertex ownership
627960225df5SJacob Faibussowitsch - verticesAdj - (Optional) vertex adjacency array
6280a47d0d45SMatthew G. Knepley 
6281b09969d6SVaclav Hapla   Level: intermediate
6282a47d0d45SMatthew G. Knepley 
6283a1cb98faSBarry Smith   Notes:
6284a1cb98faSBarry Smith   This function is just a convenient sequence of `DMCreate()`, `DMSetType()`, `DMSetDimension()`,
6285a1cb98faSBarry Smith   `DMPlexBuildFromCellListParallel()`, `DMPlexInterpolate()`, `DMPlexBuildCoordinatesFromCellListParallel()`
6286a1cb98faSBarry Smith 
6287a1cb98faSBarry Smith   See `DMPlexBuildFromCellListParallel()` for an example and details about the topology-related parameters.
6288a1cb98faSBarry Smith 
6289a1cb98faSBarry Smith   See `DMPlexBuildCoordinatesFromCellListParallel()` for details about the geometry-related parameters.
6290a1cb98faSBarry Smith 
62911cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()`
6292a47d0d45SMatthew G. Knepley @*/
6293ce78bad3SBarry Smith 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[], PeOp PetscSF *vertexSF, PeOp PetscInt *verticesAdj[], DM *dm)
6294d71ae5a4SJacob Faibussowitsch {
6295a47d0d45SMatthew G. Knepley   PetscSF sfVert;
6296a47d0d45SMatthew G. Knepley 
6297a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
62989566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
62999566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
6300a47d0d45SMatthew G. Knepley   PetscValidLogicalCollectiveInt(*dm, dim, 2);
6301064a246eSJacob Faibussowitsch   PetscValidLogicalCollectiveInt(*dm, spaceDim, 9);
63029566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
63039566063dSJacob Faibussowitsch   PetscCall(DMPlexBuildFromCellListParallel(*dm, numCells, numVertices, NVertices, numCorners, cells, &sfVert, verticesAdj));
6304a47d0d45SMatthew G. Knepley   if (interpolate) {
63055fd9971aSMatthew G. Knepley     DM idm;
6306a47d0d45SMatthew G. Knepley 
63079566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
63089566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
6309a47d0d45SMatthew G. Knepley     *dm = idm;
6310a47d0d45SMatthew G. Knepley   }
63119566063dSJacob Faibussowitsch   PetscCall(DMPlexBuildCoordinatesFromCellListParallel(*dm, spaceDim, sfVert, vertexCoords));
631218d54ad4SMichael Lange   if (vertexSF) *vertexSF = sfVert;
63139566063dSJacob Faibussowitsch   else PetscCall(PetscSFDestroy(&sfVert));
63143ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6315a47d0d45SMatthew G. Knepley }
6316a47d0d45SMatthew G. Knepley 
6317cc4c1da9SBarry Smith /*@
6318b0fe842aSMatthew G. Knepley   DMPlexCreateFromCellSectionParallel - Create distributed `DMPLEX` from a list of vertices for each cell (common mesh generator output) and supports multiple celltypes
6319b0fe842aSMatthew G. Knepley 
6320b0fe842aSMatthew G. Knepley   Collective
6321b0fe842aSMatthew G. Knepley 
6322b0fe842aSMatthew G. Knepley   Input Parameters:
6323b0fe842aSMatthew G. Knepley + comm         - The communicator
6324b0fe842aSMatthew G. Knepley . dim          - The topological dimension of the mesh
6325b0fe842aSMatthew G. Knepley . numCells     - The number of cells owned by this process
6326b0fe842aSMatthew G. Knepley . numVertices  - The number of vertices owned by this process, or `PETSC_DECIDE`
6327b0fe842aSMatthew G. Knepley . NVertices    - The global number of vertices, or `PETSC_DECIDE`
6328b0fe842aSMatthew G. Knepley . cellSection  - The `PetscSection` giving the number of vertices for each cell (layout of cells)
6329b0fe842aSMatthew G. Knepley . interpolate  - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
6330b0fe842aSMatthew G. Knepley . cells        - An array of the global vertex numbers for each cell
6331b0fe842aSMatthew G. Knepley . spaceDim     - The spatial dimension used for coordinates
6332b0fe842aSMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
6333b0fe842aSMatthew G. Knepley 
6334b0fe842aSMatthew G. Knepley   Output Parameters:
6335b0fe842aSMatthew G. Knepley + dm          - The `DM`
6336b0fe842aSMatthew G. Knepley . vertexSF    - (Optional) `PetscSF` describing complete vertex ownership
6337b0fe842aSMatthew G. Knepley - verticesAdj - (Optional) vertex adjacency array
6338b0fe842aSMatthew G. Knepley 
6339b0fe842aSMatthew G. Knepley   Level: intermediate
6340b0fe842aSMatthew G. Knepley 
6341b0fe842aSMatthew G. Knepley   Notes:
6342b0fe842aSMatthew G. Knepley   This function is just a convenient sequence of `DMCreate()`, `DMSetType()`, `DMSetDimension()`,
6343b0fe842aSMatthew G. Knepley   `DMPlexBuildFromCellSectionParallel()`, `DMPlexInterpolate()`, `DMPlexBuildCoordinatesFromCellListParallel()`
6344b0fe842aSMatthew G. Knepley 
6345b0fe842aSMatthew G. Knepley   See `DMPlexBuildFromCellSectionParallel()` for an example and details about the topology-related parameters.
6346b0fe842aSMatthew G. Knepley 
6347b0fe842aSMatthew G. Knepley   See `DMPlexBuildCoordinatesFromCellListParallel()` for details about the geometry-related parameters.
6348b0fe842aSMatthew G. Knepley 
6349b0fe842aSMatthew G. Knepley .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()`
6350b0fe842aSMatthew G. Knepley @*/
6351ce78bad3SBarry Smith 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[], PeOp PetscSF *vertexSF, PeOp PetscInt *verticesAdj[], DM *dm)
6352b0fe842aSMatthew G. Knepley {
6353b0fe842aSMatthew G. Knepley   PetscSF sfVert;
6354b0fe842aSMatthew G. Knepley 
6355b0fe842aSMatthew G. Knepley   PetscFunctionBegin;
6356b0fe842aSMatthew G. Knepley   PetscCall(DMCreate(comm, dm));
6357b0fe842aSMatthew G. Knepley   PetscCall(DMSetType(*dm, DMPLEX));
6358b0fe842aSMatthew G. Knepley   PetscValidLogicalCollectiveInt(*dm, dim, 2);
6359b0fe842aSMatthew G. Knepley   PetscValidLogicalCollectiveInt(*dm, spaceDim, 9);
6360b0fe842aSMatthew G. Knepley   PetscCall(DMSetDimension(*dm, dim));
6361b0fe842aSMatthew G. Knepley   PetscCall(DMPlexBuildFromCellSectionParallel(*dm, numCells, numVertices, NVertices, cellSection, cells, &sfVert, verticesAdj));
6362b0fe842aSMatthew G. Knepley   if (interpolate) {
6363b0fe842aSMatthew G. Knepley     DM idm;
6364b0fe842aSMatthew G. Knepley 
6365b0fe842aSMatthew G. Knepley     PetscCall(DMPlexInterpolate(*dm, &idm));
6366b0fe842aSMatthew G. Knepley     PetscCall(DMDestroy(dm));
6367b0fe842aSMatthew G. Knepley     *dm = idm;
6368b0fe842aSMatthew G. Knepley   }
6369b0fe842aSMatthew G. Knepley   PetscCall(DMPlexBuildCoordinatesFromCellListParallel(*dm, spaceDim, sfVert, vertexCoords));
6370b0fe842aSMatthew G. Knepley   if (vertexSF) *vertexSF = sfVert;
6371b0fe842aSMatthew G. Knepley   else PetscCall(PetscSFDestroy(&sfVert));
6372b0fe842aSMatthew G. Knepley   PetscFunctionReturn(PETSC_SUCCESS);
6373b0fe842aSMatthew G. Knepley }
6374b0fe842aSMatthew G. Knepley 
6375b0fe842aSMatthew G. Knepley /*@
6376a1cb98faSBarry Smith   DMPlexBuildFromCellList - Build `DMPLEX` topology from a list of vertices for each cell (common mesh generator output)
6377a1cb98faSBarry Smith 
637820f4b53cSBarry Smith   Collective; No Fortran Support
63799298eaa6SMatthew G Knepley 
63809298eaa6SMatthew G Knepley   Input Parameters:
6381a1cb98faSBarry Smith + dm          - The `DM`
6382b09969d6SVaclav Hapla . numCells    - The number of cells owned by this process
6383a1cb98faSBarry Smith . numVertices - The number of vertices owned by this process, or `PETSC_DETERMINE`
63849298eaa6SMatthew G Knepley . numCorners  - The number of vertices for each cell
6385a3b724e8SBarry Smith - cells       - An array of `numCells` x `numCorners` numbers, the global vertex numbers for each cell
63869298eaa6SMatthew G Knepley 
6387b09969d6SVaclav Hapla   Level: advanced
63889298eaa6SMatthew G Knepley 
6389b09969d6SVaclav Hapla   Notes:
6390b09969d6SVaclav Hapla   Two triangles sharing a face
6391a1cb98faSBarry Smith .vb
63929298eaa6SMatthew G Knepley 
6393a1cb98faSBarry Smith         2
6394a1cb98faSBarry Smith       / | \
6395a1cb98faSBarry Smith      /  |  \
6396a1cb98faSBarry Smith     /   |   \
6397a1cb98faSBarry Smith    0  0 | 1  3
6398a1cb98faSBarry Smith     \   |   /
6399a1cb98faSBarry Smith      \  |  /
6400a1cb98faSBarry Smith       \ | /
6401a1cb98faSBarry Smith         1
6402a1cb98faSBarry Smith .ve
6403a1cb98faSBarry Smith   would have input
6404a1cb98faSBarry Smith .vb
6405a1cb98faSBarry Smith   numCells = 2, numVertices = 4
6406a1cb98faSBarry Smith   cells = [0 1 2  1 3 2]
6407a1cb98faSBarry Smith .ve
6408a1cb98faSBarry Smith   which would result in the `DMPLEX`
6409a1cb98faSBarry Smith .vb
6410a1cb98faSBarry Smith 
6411a1cb98faSBarry Smith         4
6412a1cb98faSBarry Smith       / | \
6413a1cb98faSBarry Smith      /  |  \
6414a1cb98faSBarry Smith     /   |   \
6415a1cb98faSBarry Smith    2  0 | 1  5
6416a1cb98faSBarry Smith     \   |   /
6417a1cb98faSBarry Smith      \  |  /
6418a1cb98faSBarry Smith       \ | /
6419a1cb98faSBarry Smith         3
6420a1cb98faSBarry Smith .ve
6421a1cb98faSBarry Smith 
6422a1cb98faSBarry Smith   If numVertices is `PETSC_DETERMINE`, it is computed by PETSc as the maximum vertex index in cells + 1.
642325b6865aSVaclav Hapla 
64241cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromCellListPetsc()`
6425b09969d6SVaclav Hapla @*/
6426d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildFromCellList(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const PetscInt cells[])
6427d71ae5a4SJacob Faibussowitsch {
6428961cfab0SVaclav Hapla   PetscInt *cones, c, p, dim;
6429b09969d6SVaclav Hapla 
6430b09969d6SVaclav Hapla   PetscFunctionBegin;
64319566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
64329566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
643325b6865aSVaclav Hapla   /* Get/check global number of vertices */
643425b6865aSVaclav Hapla   {
643525b6865aSVaclav Hapla     PetscInt       NVerticesInCells, i;
643625b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
643725b6865aSVaclav Hapla 
643825b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
64391690c2aeSBarry Smith     NVerticesInCells = PETSC_INT_MIN;
64409371c9d4SSatish Balay     for (i = 0; i < len; i++)
64419371c9d4SSatish Balay       if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
644225b6865aSVaclav Hapla     ++NVerticesInCells;
644325b6865aSVaclav Hapla 
644425b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE) numVertices = NVerticesInCells;
64459371c9d4SSatish Balay     else
64469371c9d4SSatish 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);
644725b6865aSVaclav Hapla   }
64489566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
644948a46eb9SPierre Jolivet   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, numCorners));
64509566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm));
64519566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCones(dm, &cones));
6452b09969d6SVaclav Hapla   for (c = 0; c < numCells; ++c) {
6453ad540459SPierre Jolivet     for (p = 0; p < numCorners; ++p) cones[c * numCorners + p] = cells[c * numCorners + p] + numCells;
6454b09969d6SVaclav Hapla   }
64559566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
64569566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
64579566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
64583ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6459b09969d6SVaclav Hapla }
6460b09969d6SVaclav Hapla 
6461cc4c1da9SBarry Smith /*@
6462a1cb98faSBarry Smith   DMPlexBuildCoordinatesFromCellList - Build `DM` coordinates from a list of coordinates for each owned vertex (common mesh generator output)
6463a1cb98faSBarry Smith 
6464cc4c1da9SBarry Smith   Collective
6465b09969d6SVaclav Hapla 
6466b09969d6SVaclav Hapla   Input Parameters:
6467a1cb98faSBarry Smith + dm           - The `DM`
6468b09969d6SVaclav Hapla . spaceDim     - The spatial dimension used for coordinates
6469b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
6470b09969d6SVaclav Hapla 
6471b09969d6SVaclav Hapla   Level: advanced
6472b09969d6SVaclav Hapla 
64731cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellList()`
6474b09969d6SVaclav Hapla @*/
6475d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildCoordinatesFromCellList(DM dm, PetscInt spaceDim, const PetscReal vertexCoords[])
6476d71ae5a4SJacob Faibussowitsch {
6477b09969d6SVaclav Hapla   PetscSection coordSection;
6478b09969d6SVaclav Hapla   Vec          coordinates;
6479b09969d6SVaclav Hapla   DM           cdm;
6480b09969d6SVaclav Hapla   PetscScalar *coords;
64811edcf0b2SVaclav Hapla   PetscInt     v, vStart, vEnd, d;
6482b09969d6SVaclav Hapla 
6483b09969d6SVaclav Hapla   PetscFunctionBegin;
64849566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
64859566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
64861dca8a05SBarry Smith   PetscCheck(vStart >= 0 && vEnd >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
64879566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, spaceDim));
64889566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
64899566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
64909566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, spaceDim));
64919566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, vStart, vEnd));
64921edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
64939566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, spaceDim));
64949566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, spaceDim));
6495b09969d6SVaclav Hapla   }
64969566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
6497b09969d6SVaclav Hapla 
64989566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
64999566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(cdm, &coordinates));
65009566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, spaceDim));
65019566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
65029566063dSJacob Faibussowitsch   PetscCall(VecGetArrayWrite(coordinates, &coords));
65031edcf0b2SVaclav Hapla   for (v = 0; v < vEnd - vStart; ++v) {
6504ad540459SPierre Jolivet     for (d = 0; d < spaceDim; ++d) coords[v * spaceDim + d] = vertexCoords[v * spaceDim + d];
6505b09969d6SVaclav Hapla   }
65069566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayWrite(coordinates, &coords));
65079566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
65089566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
65099566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
65103ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
6511b09969d6SVaclav Hapla }
6512b09969d6SVaclav Hapla 
6513b09969d6SVaclav Hapla /*@
6514a1cb98faSBarry Smith   DMPlexCreateFromCellListPetsc - Create `DMPLEX` from a list of vertices for each cell (common mesh generator output), but only process 0 takes in the input
65153df08285SMatthew G. Knepley 
6516a1cb98faSBarry Smith   Collective
6517b09969d6SVaclav Hapla 
6518b09969d6SVaclav Hapla   Input Parameters:
6519b09969d6SVaclav Hapla + comm         - The communicator
6520b09969d6SVaclav Hapla . dim          - The topological dimension of the mesh
65213df08285SMatthew G. Knepley . numCells     - The number of cells, only on process 0
6522a1cb98faSBarry Smith . numVertices  - The number of vertices owned by this process, or `PETSC_DECIDE`, only on process 0
65233df08285SMatthew G. Knepley . numCorners   - The number of vertices for each cell, only on process 0
6524b09969d6SVaclav Hapla . interpolate  - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
6525ce78bad3SBarry Smith . cells        - An array of $ numCells \times numCorners$ numbers, the vertices for each cell, only on process 0
6526b09969d6SVaclav Hapla . spaceDim     - The spatial dimension used for coordinates
6527ce78bad3SBarry Smith - vertexCoords - An array of $ numVertices \times spaceDim$ numbers, the coordinates of each vertex, only on process 0
6528b09969d6SVaclav Hapla 
6529b09969d6SVaclav Hapla   Output Parameter:
6530a1cb98faSBarry Smith . dm - The `DM`, which only has points on process 0
653125b6865aSVaclav Hapla 
6532b09969d6SVaclav Hapla   Level: intermediate
6533b09969d6SVaclav Hapla 
6534a1cb98faSBarry Smith   Notes:
6535a1cb98faSBarry Smith   This function is just a convenient sequence of `DMCreate()`, `DMSetType()`, `DMSetDimension()`, `DMPlexBuildFromCellList()`,
6536a1cb98faSBarry Smith   `DMPlexInterpolate()`, `DMPlexBuildCoordinatesFromCellList()`
6537a1cb98faSBarry Smith 
6538a1cb98faSBarry Smith   See `DMPlexBuildFromCellList()` for an example and details about the topology-related parameters.
6539a1cb98faSBarry Smith   See `DMPlexBuildCoordinatesFromCellList()` for details about the geometry-related parameters.
6540a1cb98faSBarry Smith   See `DMPlexCreateFromCellListParallelPetsc()` for parallel input
6541a1cb98faSBarry Smith 
65421cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildFromCellList()`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()`
65439298eaa6SMatthew G Knepley @*/
6544d71ae5a4SJacob 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)
6545d71ae5a4SJacob Faibussowitsch {
65463df08285SMatthew G. Knepley   PetscMPIInt rank;
65479298eaa6SMatthew G Knepley 
65489298eaa6SMatthew G Knepley   PetscFunctionBegin;
654928b400f6SJacob 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.");
65509566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
65519566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
65529566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
65539566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
6554c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildFromCellList(*dm, numCells, numVertices, numCorners, cells));
65559566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildFromCellList(*dm, 0, 0, 0, NULL));
65569298eaa6SMatthew G Knepley   if (interpolate) {
65575fd9971aSMatthew G. Knepley     DM idm;
65589298eaa6SMatthew G Knepley 
65599566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
65609566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
65619298eaa6SMatthew G Knepley     *dm = idm;
65629298eaa6SMatthew G Knepley   }
6563c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, vertexCoords));
65649566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, NULL));
65653ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
65669298eaa6SMatthew G Knepley }
65679298eaa6SMatthew G Knepley 
6568939f6067SMatthew G. Knepley /*@
656920f4b53cSBarry Smith   DMPlexCreateFromDAG - This takes as input the adjacency-list representation of the Directed Acyclic Graph (Hasse Diagram) encoding a mesh, and produces a `DM`
6570939f6067SMatthew G. Knepley 
6571939f6067SMatthew G. Knepley   Input Parameters:
657220f4b53cSBarry Smith + dm               - The empty `DM` object, usually from `DMCreate()` and `DMSetDimension()`
6573939f6067SMatthew G. Knepley . depth            - The depth of the DAG
6574ce78bad3SBarry Smith . numPoints        - Array of size $ depth + 1 $ containing the number of points at each `depth`
6575939f6067SMatthew G. Knepley . coneSize         - The cone size of each point
6576939f6067SMatthew G. Knepley . cones            - The concatenation of the cone points for each point, the cone list must be oriented correctly for each point
6577939f6067SMatthew G. Knepley . coneOrientations - The orientation of each cone point
6578ce78bad3SBarry Smith - vertexCoords     - An array of $ numPoints[0] \times spacedim $ numbers representing the coordinates of each vertex, with `spacedim` the value set via `DMSetCoordinateDim()`
6579939f6067SMatthew G. Knepley 
6580939f6067SMatthew G. Knepley   Output Parameter:
658120f4b53cSBarry Smith . dm - The `DM`
658220f4b53cSBarry Smith 
658320f4b53cSBarry Smith   Level: advanced
6584939f6067SMatthew G. Knepley 
6585a1cb98faSBarry Smith   Note:
6586a1cb98faSBarry Smith   Two triangles sharing a face would have input
6587a1cb98faSBarry Smith .vb
6588a1cb98faSBarry Smith   depth = 1, numPoints = [4 2], coneSize = [3 3 0 0 0 0]
6589a1cb98faSBarry Smith   cones = [2 3 4  3 5 4], coneOrientations = [0 0 0  0 0 0]
6590a1cb98faSBarry Smith  vertexCoords = [-1.0 0.0  0.0 -1.0  0.0 1.0  1.0 0.0]
6591a1cb98faSBarry Smith .ve
6592939f6067SMatthew G. Knepley   which would result in the DMPlex
6593a1cb98faSBarry Smith .vb
6594a1cb98faSBarry Smith         4
6595a1cb98faSBarry Smith       / | \
6596a1cb98faSBarry Smith      /  |  \
6597a1cb98faSBarry Smith     /   |   \
6598a1cb98faSBarry Smith    2  0 | 1  5
6599a1cb98faSBarry Smith     \   |   /
6600a1cb98faSBarry Smith      \  |  /
6601a1cb98faSBarry Smith       \ | /
6602a1cb98faSBarry Smith         3
6603a1cb98faSBarry Smith .ve
6604a1cb98faSBarry Smith   Notice that all points are numbered consecutively, unlike `DMPlexCreateFromCellListPetsc()`
6605939f6067SMatthew G. Knepley 
6606ce78bad3SBarry Smith   Developer Note:
6607ce78bad3SBarry Smith   This does not create anything so should not have create in the name.
6608ce78bad3SBarry Smith 
66091cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`
6610939f6067SMatthew G. Knepley @*/
6611d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateFromDAG(DM dm, PetscInt depth, const PetscInt numPoints[], const PetscInt coneSize[], const PetscInt cones[], const PetscInt coneOrientations[], const PetscScalar vertexCoords[])
6612d71ae5a4SJacob Faibussowitsch {
66139298eaa6SMatthew G Knepley   Vec          coordinates;
66149298eaa6SMatthew G Knepley   PetscSection coordSection;
66159298eaa6SMatthew G Knepley   PetscScalar *coords;
6616811e8653SToby Isaac   PetscInt     coordSize, firstVertex = -1, pStart = 0, pEnd = 0, p, v, dim, dimEmbed, d, off;
66179298eaa6SMatthew G Knepley 
66189298eaa6SMatthew G Knepley   PetscFunctionBegin;
66199566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
66209566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dimEmbed));
662163a3b9bcSJacob Faibussowitsch   PetscCheck(dimEmbed >= dim, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Embedding dimension %" PetscInt_FMT " cannot be less than intrinsic dimension %" PetscInt_FMT, dimEmbed, dim);
66229298eaa6SMatthew G Knepley   for (d = 0; d <= depth; ++d) pEnd += numPoints[d];
66239566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, pStart, pEnd));
66249298eaa6SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
66259566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeSize(dm, p, coneSize[p - pStart]));
6626ad540459SPierre Jolivet     if (firstVertex < 0 && !coneSize[p - pStart]) firstVertex = p - pStart;
662797e052ccSToby Isaac   }
66281dca8a05SBarry Smith   PetscCheck(firstVertex >= 0 || !numPoints[0], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Expected %" PetscInt_FMT " vertices but could not find any", numPoints[0]);
66299566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm)); /* Allocate space for cones */
66309298eaa6SMatthew G Knepley   for (p = pStart, off = 0; p < pEnd; off += coneSize[p - pStart], ++p) {
66319566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(dm, p, &cones[off]));
66329566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeOrientation(dm, p, &coneOrientations[off]));
66339298eaa6SMatthew G Knepley   }
66349566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
66359566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
66369298eaa6SMatthew G Knepley   /* Build coordinates */
66379566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
66389566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
66399566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dimEmbed));
66409566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numPoints[0]));
66419298eaa6SMatthew G Knepley   for (v = firstVertex; v < firstVertex + numPoints[0]; ++v) {
66429566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, dimEmbed));
66439566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dimEmbed));
66449298eaa6SMatthew G Knepley   }
66459566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
66469566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
66479566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
66489566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
66499566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
665058b7e2c1SStefano Zampini   PetscCall(VecSetBlockSize(coordinates, PetscMax(dimEmbed, 1)));
66519566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
66529318fe57SMatthew G. Knepley   if (vertexCoords) {
66539566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
66549298eaa6SMatthew G Knepley     for (v = 0; v < numPoints[0]; ++v) {
66559298eaa6SMatthew G Knepley       PetscInt off;
66569298eaa6SMatthew G Knepley 
66579566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, v + firstVertex, &off));
6658ad540459SPierre Jolivet       for (d = 0; d < dimEmbed; ++d) coords[off + d] = vertexCoords[v * dimEmbed + d];
66599298eaa6SMatthew G Knepley     }
66609318fe57SMatthew G. Knepley   }
66619566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
66629566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
66639566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
66643ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
66659298eaa6SMatthew G Knepley }
66668415267dSToby Isaac 
6667a4e35b19SJacob Faibussowitsch /*
6668a1cb98faSBarry Smith   DMPlexCreateCellVertexFromFile - Create a `DMPLEX` mesh from a simple cell-vertex file.
6669a1cb98faSBarry Smith 
6670a1cb98faSBarry Smith   Collective
66718ca92349SMatthew G. Knepley 
66728ca92349SMatthew G. Knepley + comm        - The MPI communicator
66738ca92349SMatthew G. Knepley . filename    - Name of the .dat file
66748ca92349SMatthew G. Knepley - interpolate - Create faces and edges in the mesh
66758ca92349SMatthew G. Knepley 
66768ca92349SMatthew G. Knepley   Output Parameter:
6677a1cb98faSBarry Smith . dm  - The `DM` object representing the mesh
66788ca92349SMatthew G. Knepley 
66798ca92349SMatthew G. Knepley   Level: beginner
66808ca92349SMatthew G. Knepley 
6681a1cb98faSBarry Smith   Note:
6682a1cb98faSBarry Smith   The format is the simplest possible:
6683a1cb98faSBarry Smith .vb
6684d0812dedSMatthew G. Knepley   dim Ne Nv Nc Nl
6685d0812dedSMatthew G. Knepley   v_1 v_2 ... v_Nc
6686d0812dedSMatthew G. Knepley   ...
6687d0812dedSMatthew G. Knepley   x y z marker_1 ... marker_Nl
6688a1cb98faSBarry Smith .ve
6689a1cb98faSBarry Smith 
6690a1cb98faSBarry Smith   Developer Note:
6691a1cb98faSBarry Smith   Should use a `PetscViewer` not a filename
6692a1cb98faSBarry Smith 
66936afe31f6SMartin Diehl .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromFile()`, `DMPlexCreateGmsh()`, `DMPlexCreate()`
6694a4e35b19SJacob Faibussowitsch */
6695ff6a9541SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCellVertexFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
6696d71ae5a4SJacob Faibussowitsch {
66978ca92349SMatthew G. Knepley   DMLabel      marker;
66988ca92349SMatthew G. Knepley   PetscViewer  viewer;
66998ca92349SMatthew G. Knepley   Vec          coordinates;
67008ca92349SMatthew G. Knepley   PetscSection coordSection;
67018ca92349SMatthew G. Knepley   PetscScalar *coords;
67028ca92349SMatthew G. Knepley   char         line[PETSC_MAX_PATH_LEN];
6703d0812dedSMatthew G. Knepley   PetscInt     cdim, coordSize, v, c, d;
67048ca92349SMatthew G. Knepley   PetscMPIInt  rank;
6705d0812dedSMatthew G. Knepley   int          snum, dim, Nv, Nc, Ncn, Nl;
67068ca92349SMatthew G. Knepley 
67078ca92349SMatthew G. Knepley   PetscFunctionBegin;
67089566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
67099566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, &viewer));
67109566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(viewer, PETSCVIEWERASCII));
67119566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetMode(viewer, FILE_MODE_READ));
67129566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetName(viewer, filename));
6713dd400576SPatrick Sanan   if (rank == 0) {
6714d0812dedSMatthew G. Knepley     PetscCall(PetscViewerRead(viewer, line, 5, NULL, PETSC_STRING));
6715d0812dedSMatthew G. Knepley     snum = sscanf(line, "%d %d %d %d %d", &dim, &Nc, &Nv, &Ncn, &Nl);
6716d0812dedSMatthew G. Knepley     PetscCheck(snum == 5, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
671725ce1634SJed Brown   } else {
6718f8d5e320SMatthew G. Knepley     Nc = Nv = Ncn = Nl = 0;
67198ca92349SMatthew G. Knepley   }
6720d0812dedSMatthew G. Knepley   PetscCallMPI(MPI_Bcast(&dim, 1, MPI_INT, 0, comm));
6721835f2295SStefano Zampini   cdim = dim;
67229566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
67239566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
67249566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(*dm, 0, Nc + Nv));
6725835f2295SStefano Zampini   PetscCall(DMSetDimension(*dm, dim));
67269566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*dm, cdim));
67278ca92349SMatthew G. Knepley   /* Read topology */
6728dd400576SPatrick Sanan   if (rank == 0) {
6729f8d5e320SMatthew G. Knepley     char     format[PETSC_MAX_PATH_LEN];
6730f8d5e320SMatthew G. Knepley     PetscInt cone[8];
67318ca92349SMatthew G. Knepley     int      vbuf[8], v;
67328ca92349SMatthew G. Knepley 
67339371c9d4SSatish Balay     for (c = 0; c < Ncn; ++c) {
67349371c9d4SSatish Balay       format[c * 3 + 0] = '%';
67359371c9d4SSatish Balay       format[c * 3 + 1] = 'd';
67369371c9d4SSatish Balay       format[c * 3 + 2] = ' ';
67379371c9d4SSatish Balay     }
6738f8d5e320SMatthew G. Knepley     format[Ncn * 3 - 1] = '\0';
67399566063dSJacob Faibussowitsch     for (c = 0; c < Nc; ++c) PetscCall(DMPlexSetConeSize(*dm, c, Ncn));
67409566063dSJacob Faibussowitsch     PetscCall(DMSetUp(*dm));
67418ca92349SMatthew G. Knepley     for (c = 0; c < Nc; ++c) {
67429566063dSJacob Faibussowitsch       PetscCall(PetscViewerRead(viewer, line, Ncn, NULL, PETSC_STRING));
6743f8d5e320SMatthew G. Knepley       switch (Ncn) {
6744d71ae5a4SJacob Faibussowitsch       case 2:
6745d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1]);
6746d71ae5a4SJacob Faibussowitsch         break;
6747d71ae5a4SJacob Faibussowitsch       case 3:
6748d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2]);
6749d71ae5a4SJacob Faibussowitsch         break;
6750d71ae5a4SJacob Faibussowitsch       case 4:
6751d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3]);
6752d71ae5a4SJacob Faibussowitsch         break;
6753d71ae5a4SJacob Faibussowitsch       case 6:
6754d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5]);
6755d71ae5a4SJacob Faibussowitsch         break;
6756d71ae5a4SJacob Faibussowitsch       case 8:
6757d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5], &vbuf[6], &vbuf[7]);
6758d71ae5a4SJacob Faibussowitsch         break;
6759d71ae5a4SJacob Faibussowitsch       default:
6760d71ae5a4SJacob Faibussowitsch         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No cell shape with %d vertices", Ncn);
6761f8d5e320SMatthew G. Knepley       }
676208401ef6SPierre Jolivet       PetscCheck(snum == Ncn, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
6763f8d5e320SMatthew G. Knepley       for (v = 0; v < Ncn; ++v) cone[v] = vbuf[v] + Nc;
67648ca92349SMatthew G. Knepley       /* Hexahedra are inverted */
6765f8d5e320SMatthew G. Knepley       if (Ncn == 8) {
67668ca92349SMatthew G. Knepley         PetscInt tmp = cone[1];
67678ca92349SMatthew G. Knepley         cone[1]      = cone[3];
67688ca92349SMatthew G. Knepley         cone[3]      = tmp;
67698ca92349SMatthew G. Knepley       }
67709566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(*dm, c, cone));
67718ca92349SMatthew G. Knepley     }
67728ca92349SMatthew G. Knepley   }
67739566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(*dm));
67749566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(*dm));
67758ca92349SMatthew G. Knepley   /* Read coordinates */
67769566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(*dm, &coordSection));
67779566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
67789566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, cdim));
67799566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, Nc, Nc + Nv));
67808ca92349SMatthew G. Knepley   for (v = Nc; v < Nc + Nv; ++v) {
67819566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, cdim));
67829566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, cdim));
67838ca92349SMatthew G. Knepley   }
67849566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
67859566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
67869566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
67879566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
67889566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
67899566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, cdim));
67909566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
67919566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
6792dd400576SPatrick Sanan   if (rank == 0) {
6793f8d5e320SMatthew G. Knepley     char   format[PETSC_MAX_PATH_LEN];
67948ca92349SMatthew G. Knepley     double x[3];
6795f8d5e320SMatthew G. Knepley     int    l, val[3];
67968ca92349SMatthew G. Knepley 
6797f8d5e320SMatthew G. Knepley     if (Nl) {
67989371c9d4SSatish Balay       for (l = 0; l < Nl; ++l) {
67999371c9d4SSatish Balay         format[l * 3 + 0] = '%';
68009371c9d4SSatish Balay         format[l * 3 + 1] = 'd';
68019371c9d4SSatish Balay         format[l * 3 + 2] = ' ';
68029371c9d4SSatish Balay       }
6803f8d5e320SMatthew G. Knepley       format[Nl * 3 - 1] = '\0';
68049566063dSJacob Faibussowitsch       PetscCall(DMCreateLabel(*dm, "marker"));
68059566063dSJacob Faibussowitsch       PetscCall(DMGetLabel(*dm, "marker", &marker));
6806f8d5e320SMatthew G. Knepley     }
68078ca92349SMatthew G. Knepley     for (v = 0; v < Nv; ++v) {
68089566063dSJacob Faibussowitsch       PetscCall(PetscViewerRead(viewer, line, 3 + Nl, NULL, PETSC_STRING));
6809f8d5e320SMatthew G. Knepley       snum = sscanf(line, "%lg %lg %lg", &x[0], &x[1], &x[2]);
681008401ef6SPierre Jolivet       PetscCheck(snum == 3, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
6811f8d5e320SMatthew G. Knepley       switch (Nl) {
6812d71ae5a4SJacob Faibussowitsch       case 0:
6813d71ae5a4SJacob Faibussowitsch         snum = 0;
6814d71ae5a4SJacob Faibussowitsch         break;
6815d71ae5a4SJacob Faibussowitsch       case 1:
6816d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &val[0]);
6817d71ae5a4SJacob Faibussowitsch         break;
6818d71ae5a4SJacob Faibussowitsch       case 2:
6819d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &val[0], &val[1]);
6820d71ae5a4SJacob Faibussowitsch         break;
6821d71ae5a4SJacob Faibussowitsch       case 3:
6822d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &val[0], &val[1], &val[2]);
6823d71ae5a4SJacob Faibussowitsch         break;
6824d71ae5a4SJacob Faibussowitsch       default:
6825d71ae5a4SJacob Faibussowitsch         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Request support for %d labels", Nl);
6826f8d5e320SMatthew G. Knepley       }
682708401ef6SPierre Jolivet       PetscCheck(snum == Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
68288ca92349SMatthew G. Knepley       for (d = 0; d < cdim; ++d) coords[v * cdim + d] = x[d];
68299566063dSJacob Faibussowitsch       for (l = 0; l < Nl; ++l) PetscCall(DMLabelSetValue(marker, v + Nc, val[l]));
68308ca92349SMatthew G. Knepley     }
68318ca92349SMatthew G. Knepley   }
68329566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
68339566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(*dm, coordinates));
68349566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
68359566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(&viewer));
68368ca92349SMatthew G. Knepley   if (interpolate) {
68378ca92349SMatthew G. Knepley     DM      idm;
68388ca92349SMatthew G. Knepley     DMLabel bdlabel;
68398ca92349SMatthew G. Knepley 
68409566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
68419566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
68428ca92349SMatthew G. Knepley     *dm = idm;
68438ca92349SMatthew G. Knepley 
6844f8d5e320SMatthew G. Knepley     if (!Nl) {
68459566063dSJacob Faibussowitsch       PetscCall(DMCreateLabel(*dm, "marker"));
68469566063dSJacob Faibussowitsch       PetscCall(DMGetLabel(*dm, "marker", &bdlabel));
68479566063dSJacob Faibussowitsch       PetscCall(DMPlexMarkBoundaryFaces(*dm, PETSC_DETERMINE, bdlabel));
68489566063dSJacob Faibussowitsch       PetscCall(DMPlexLabelComplete(*dm, bdlabel));
68498ca92349SMatthew G. Knepley     }
6850f8d5e320SMatthew G. Knepley   }
68513ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
68528ca92349SMatthew G. Knepley }
68538ca92349SMatthew G. Knepley 
6854cc4c1da9SBarry Smith /*@
6855a1cb98faSBarry Smith   DMPlexCreateFromFile - This takes a filename and produces a `DM`
6856a1cb98faSBarry Smith 
6857a1cb98faSBarry Smith   Collective
6858ca522641SMatthew G. Knepley 
6859ca522641SMatthew G. Knepley   Input Parameters:
6860ca522641SMatthew G. Knepley + comm        - The communicator
6861ca522641SMatthew G. Knepley . filename    - A file name
6862a1cb98faSBarry Smith . plexname    - The object name of the resulting `DM`, also used for intra-datafile lookup by some formats
6863ca522641SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
6864ca522641SMatthew G. Knepley 
6865ca522641SMatthew G. Knepley   Output Parameter:
6866a1cb98faSBarry Smith . dm - The `DM`
6867ca522641SMatthew G. Knepley 
6868a1cb98faSBarry Smith   Options Database Key:
6869a1cb98faSBarry Smith . -dm_plex_create_from_hdf5_xdmf - use the `PETSC_VIEWER_HDF5_XDMF` format for reading HDF5
687002ef0d99SVaclav Hapla 
6871b44f4de4SBarry Smith   Use `-dm_plex_create_ prefix` to pass options to the internal `PetscViewer`, e.g. `-dm_plex_create_viewer_hdf5_collective`
6872bca97951SVaclav Hapla 
6873ca522641SMatthew G. Knepley   Level: beginner
6874ca522641SMatthew G. Knepley 
6875a1cb98faSBarry Smith   Notes:
6876a1cb98faSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
6877a1cb98faSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
6878a1cb98faSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
6879a1cb98faSBarry Smith   The input parameter name is thus used to name the `DMPLEX` object when `DMPlexCreateFromFile()` internally
6880a1cb98faSBarry Smith   calls `DMLoad()`. Currently, name is ignored for other viewer types and/or formats.
6881a1cb98faSBarry Smith 
68821cc06b55SBarry Smith .seealso: [](ch_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromDAG()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`, `PetscObjectSetName()`, `DMView()`, `DMLoad()`
6883ca522641SMatthew G. Knepley @*/
6884d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateFromFile(MPI_Comm comm, const char filename[], const char plexname[], PetscBool interpolate, DM *dm)
6885d71ae5a4SJacob Faibussowitsch {
6886ef3a5affSJacob Faibussowitsch   const char  extGmsh[]      = ".msh";
6887ef3a5affSJacob Faibussowitsch   const char  extGmsh2[]     = ".msh2";
6888ef3a5affSJacob Faibussowitsch   const char  extGmsh4[]     = ".msh4";
6889ef3a5affSJacob Faibussowitsch   const char  extCGNS[]      = ".cgns";
6890ef3a5affSJacob Faibussowitsch   const char  extExodus[]    = ".exo";
6891ef3a5affSJacob Faibussowitsch   const char  extExodus_e[]  = ".e";
6892ef3a5affSJacob Faibussowitsch   const char  extGenesis[]   = ".gen";
6893ef3a5affSJacob Faibussowitsch   const char  extFluent[]    = ".cas";
6894ef3a5affSJacob Faibussowitsch   const char  extHDF5[]      = ".h5";
68956f2c871aSStefano Zampini   const char  extXDMFHDF5[]  = ".xdmf.h5";
6896ef3a5affSJacob Faibussowitsch   const char  extPLY[]       = ".ply";
68975552b385SBrandon   const char  extEGADSlite[] = ".egadslite";
6898ef3a5affSJacob Faibussowitsch   const char  extEGADS[]     = ".egads";
6899ef3a5affSJacob Faibussowitsch   const char  extIGES[]      = ".igs";
69005552b385SBrandon   const char  extIGES2[]     = ".iges";
6901ef3a5affSJacob Faibussowitsch   const char  extSTEP[]      = ".stp";
69025552b385SBrandon   const char  extSTEP2[]     = ".step";
69035552b385SBrandon   const char  extBREP[]      = ".brep";
6904ef3a5affSJacob Faibussowitsch   const char  extCV[]        = ".dat";
6905ca522641SMatthew G. Knepley   size_t      len;
69065552b385SBrandon   PetscBool   isGmsh, isGmsh2, isGmsh4, isCGNS, isExodus, isGenesis, isFluent, isHDF5, isPLY, isEGADSlite, isEGADS, isIGES, isIGES2, isSTEP, isSTEP2, isBREP, isCV, isXDMFHDF5;
6907ca522641SMatthew G. Knepley   PetscMPIInt rank;
6908ca522641SMatthew G. Knepley 
6909ca522641SMatthew G. Knepley   PetscFunctionBegin;
69104f572ea9SToby Isaac   PetscAssertPointer(filename, 2);
69114f572ea9SToby Isaac   if (plexname) PetscAssertPointer(plexname, 3);
69124f572ea9SToby Isaac   PetscAssertPointer(dm, 5);
69139566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
69149566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_CreateFromFile, 0, 0, 0, 0));
69159566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
69169566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(filename, &len));
691728b400f6SJacob Faibussowitsch   PetscCheck(len, comm, PETSC_ERR_ARG_WRONG, "Filename must be a valid path");
6918ef3a5affSJacob Faibussowitsch 
69199371c9d4SSatish Balay #define CheckExtension(extension__, is_extension__) \
69209371c9d4SSatish Balay   do { \
6921274aaeaaSJacob Faibussowitsch     PetscAssert(sizeof(extension__), comm, PETSC_ERR_PLIB, "Zero-size extension: %s", extension__); \
6922274aaeaaSJacob Faibussowitsch     /* don't count the null-terminator at the end */ \
6923274aaeaaSJacob Faibussowitsch     const size_t ext_len = sizeof(extension__) - 1; \
6924274aaeaaSJacob Faibussowitsch     if (len < ext_len) { \
6925ef3a5affSJacob Faibussowitsch       is_extension__ = PETSC_FALSE; \
6926ef3a5affSJacob Faibussowitsch     } else { \
6927274aaeaaSJacob Faibussowitsch       PetscCall(PetscStrncmp(filename + len - ext_len, extension__, ext_len, &is_extension__)); \
6928ef3a5affSJacob Faibussowitsch     } \
6929ef3a5affSJacob Faibussowitsch   } while (0)
6930ef3a5affSJacob Faibussowitsch 
6931ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh, isGmsh);
6932ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh2, isGmsh2);
6933ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh4, isGmsh4);
6934ef3a5affSJacob Faibussowitsch   CheckExtension(extCGNS, isCGNS);
6935ef3a5affSJacob Faibussowitsch   CheckExtension(extExodus, isExodus);
6936ef3a5affSJacob Faibussowitsch   if (!isExodus) CheckExtension(extExodus_e, isExodus);
6937ef3a5affSJacob Faibussowitsch   CheckExtension(extGenesis, isGenesis);
6938ef3a5affSJacob Faibussowitsch   CheckExtension(extFluent, isFluent);
6939ef3a5affSJacob Faibussowitsch   CheckExtension(extHDF5, isHDF5);
6940ef3a5affSJacob Faibussowitsch   CheckExtension(extPLY, isPLY);
69415552b385SBrandon   CheckExtension(extEGADSlite, isEGADSlite);
6942ef3a5affSJacob Faibussowitsch   CheckExtension(extEGADS, isEGADS);
6943ef3a5affSJacob Faibussowitsch   CheckExtension(extIGES, isIGES);
69445552b385SBrandon   CheckExtension(extIGES2, isIGES2);
6945ef3a5affSJacob Faibussowitsch   CheckExtension(extSTEP, isSTEP);
69465552b385SBrandon   CheckExtension(extSTEP2, isSTEP2);
69475552b385SBrandon   CheckExtension(extBREP, isBREP);
6948ef3a5affSJacob Faibussowitsch   CheckExtension(extCV, isCV);
69496f2c871aSStefano Zampini   CheckExtension(extXDMFHDF5, isXDMFHDF5);
6950ef3a5affSJacob Faibussowitsch 
6951ef3a5affSJacob Faibussowitsch #undef CheckExtension
6952ef3a5affSJacob Faibussowitsch 
6953de78e4feSLisandro Dalcin   if (isGmsh || isGmsh2 || isGmsh4) {
69549566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateGmshFromFile(comm, filename, interpolate, dm));
6955ca522641SMatthew G. Knepley   } else if (isCGNS) {
69569566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCGNSFromFile(comm, filename, interpolate, dm));
695790c68965SMatthew G. Knepley   } else if (isExodus || isGenesis) {
69589566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateExodusFromFile(comm, filename, interpolate, dm));
69592f0bd6dcSMichael Lange   } else if (isFluent) {
69609566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFluentFromFile(comm, filename, interpolate, dm));
6961cc2f8f65SMatthew G. Knepley   } else if (isHDF5) {
6962cc2f8f65SMatthew G. Knepley     PetscViewer viewer;
6963cc2f8f65SMatthew G. Knepley 
696443b242b4SVaclav 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 */
69656f2c871aSStefano Zampini     PetscCall(PetscOptionsGetBool(NULL, NULL, "-dm_plex_create_from_hdf5_xdmf", &isXDMFHDF5, NULL));
69669566063dSJacob Faibussowitsch     PetscCall(PetscViewerCreate(comm, &viewer));
69679566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetType(viewer, PETSCVIEWERHDF5));
69689566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetOptionsPrefix(viewer, "dm_plex_create_"));
69699566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetFromOptions(viewer));
69709566063dSJacob Faibussowitsch     PetscCall(PetscViewerFileSetMode(viewer, FILE_MODE_READ));
69719566063dSJacob Faibussowitsch     PetscCall(PetscViewerFileSetName(viewer, filename));
6972cd7e8a5eSksagiyam 
69739566063dSJacob Faibussowitsch     PetscCall(DMCreate(comm, dm));
6974f4f49eeaSPierre Jolivet     PetscCall(PetscObjectSetName((PetscObject)*dm, plexname));
69759566063dSJacob Faibussowitsch     PetscCall(DMSetType(*dm, DMPLEX));
69766f2c871aSStefano Zampini     if (isXDMFHDF5) PetscCall(PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_XDMF));
69779566063dSJacob Faibussowitsch     PetscCall(DMLoad(*dm, viewer));
69786f2c871aSStefano Zampini     if (isXDMFHDF5) PetscCall(PetscViewerPopFormat(viewer));
69799566063dSJacob Faibussowitsch     PetscCall(PetscViewerDestroy(&viewer));
69805fd9971aSMatthew G. Knepley 
69815fd9971aSMatthew G. Knepley     if (interpolate) {
69825fd9971aSMatthew G. Knepley       DM idm;
69835fd9971aSMatthew G. Knepley 
69849566063dSJacob Faibussowitsch       PetscCall(DMPlexInterpolate(*dm, &idm));
69859566063dSJacob Faibussowitsch       PetscCall(DMDestroy(dm));
69865fd9971aSMatthew G. Knepley       *dm = idm;
69875fd9971aSMatthew G. Knepley     }
6988f2801cd6SMatthew G. Knepley   } else if (isPLY) {
69899566063dSJacob Faibussowitsch     PetscCall(DMPlexCreatePLYFromFile(comm, filename, interpolate, dm));
69905552b385SBrandon   } else if (isEGADSlite || isEGADS || isIGES || isIGES2 || isSTEP || isSTEP2 || isBREP) {
69915552b385SBrandon     PetscCall(DMPlexCreateGeomFromFile(comm, filename, dm, isEGADSlite));
69925552b385SBrandon 
69937bee2925SMatthew Knepley     if (!interpolate) {
69947bee2925SMatthew Knepley       DM udm;
69957bee2925SMatthew Knepley 
69969566063dSJacob Faibussowitsch       PetscCall(DMPlexUninterpolate(*dm, &udm));
69979566063dSJacob Faibussowitsch       PetscCall(DMDestroy(dm));
69987bee2925SMatthew Knepley       *dm = udm;
69997bee2925SMatthew Knepley     }
70008ca92349SMatthew G. Knepley   } else if (isCV) {
70019566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCellVertexFromFile(comm, filename, interpolate, dm));
700298921bdaSJacob Faibussowitsch   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot load file %s: unrecognized extension", filename);
70039566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(plexname, &len));
7004f4f49eeaSPierre Jolivet   if (len) PetscCall(PetscObjectSetName((PetscObject)*dm, plexname));
70059566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_CreateFromFile, 0, 0, 0, 0));
70063ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
7007ca522641SMatthew G. Knepley }
700812b8a6daSStefano Zampini 
7009cc4c1da9SBarry Smith /*@
70109f6c5813SMatthew 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.
70119f6c5813SMatthew G. Knepley 
70120528010dSStefano Zampini   Input Parameters:
70130528010dSStefano Zampini + tr     - The `DMPlexTransform`
70140528010dSStefano Zampini - prefix - An options prefix, or NULL
70159f6c5813SMatthew G. Knepley 
70169f6c5813SMatthew G. Knepley   Output Parameter:
70179f6c5813SMatthew G. Knepley . dm - The `DM`
70189f6c5813SMatthew G. Knepley 
70199f6c5813SMatthew G. Knepley   Level: beginner
70209f6c5813SMatthew G. Knepley 
702120f4b53cSBarry Smith   Notes:
702220f4b53cSBarry 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.
702320f4b53cSBarry Smith 
70249f6c5813SMatthew G. Knepley .seealso: `DMPlexCreateFromFile`, `DMPlexCreateFromDAG()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`
70259f6c5813SMatthew G. Knepley @*/
70260528010dSStefano Zampini PetscErrorCode DMPlexCreateEphemeral(DMPlexTransform tr, const char prefix[], DM *dm)
70279f6c5813SMatthew G. Knepley {
70280528010dSStefano Zampini   DM           bdm, bcdm, cdm;
70290528010dSStefano Zampini   Vec          coordinates, coordinatesNew;
70300528010dSStefano Zampini   PetscSection cs;
7031817b2c36SMatthew G. Knepley   PetscInt     cdim, Nl;
70329f6c5813SMatthew G. Knepley 
70339f6c5813SMatthew G. Knepley   PetscFunctionBegin;
70349f6c5813SMatthew G. Knepley   PetscCall(DMCreate(PetscObjectComm((PetscObject)tr), dm));
70359f6c5813SMatthew G. Knepley   PetscCall(DMSetType(*dm, DMPLEX));
70360528010dSStefano Zampini   ((DM_Plex *)(*dm)->data)->interpolated = DMPLEX_INTERPOLATED_FULL;
70370528010dSStefano Zampini   // Handle coordinates
70380528010dSStefano Zampini   PetscCall(DMPlexTransformGetDM(tr, &bdm));
7039817b2c36SMatthew G. Knepley   PetscCall(DMPlexTransformSetDimensions(tr, bdm, *dm));
7040817b2c36SMatthew G. Knepley   PetscCall(DMGetCoordinateDim(*dm, &cdim));
70410528010dSStefano Zampini   PetscCall(DMGetCoordinateDM(bdm, &bcdm));
70420528010dSStefano Zampini   PetscCall(DMGetCoordinateDM(*dm, &cdm));
70430528010dSStefano Zampini   PetscCall(DMCopyDisc(bcdm, cdm));
70440528010dSStefano Zampini   PetscCall(DMGetLocalSection(cdm, &cs));
70450528010dSStefano Zampini   PetscCall(PetscSectionSetNumFields(cs, 1));
70460528010dSStefano Zampini   PetscCall(PetscSectionSetFieldComponents(cs, 0, cdim));
70470528010dSStefano Zampini   PetscCall(DMGetCoordinatesLocal(bdm, &coordinates));
70480528010dSStefano Zampini   PetscCall(VecDuplicate(coordinates, &coordinatesNew));
70490528010dSStefano Zampini   PetscCall(VecCopy(coordinates, coordinatesNew));
70500528010dSStefano Zampini   PetscCall(DMSetCoordinatesLocal(*dm, coordinatesNew));
70510528010dSStefano Zampini   PetscCall(VecDestroy(&coordinatesNew));
70529f6c5813SMatthew G. Knepley 
70539f6c5813SMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)tr));
70549f6c5813SMatthew G. Knepley   PetscCall(DMPlexTransformDestroy(&((DM_Plex *)(*dm)->data)->tr));
70559f6c5813SMatthew G. Knepley   ((DM_Plex *)(*dm)->data)->tr = tr;
70560528010dSStefano Zampini   PetscCall(DMPlexDistributeSetDefault(*dm, PETSC_FALSE));
70570528010dSStefano Zampini   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)*dm, prefix));
70580528010dSStefano Zampini   PetscCall(DMSetFromOptions(*dm));
70599f6c5813SMatthew G. Knepley 
70609f6c5813SMatthew G. Knepley   PetscCall(DMGetNumLabels(bdm, &Nl));
70619f6c5813SMatthew G. Knepley   for (PetscInt l = 0; l < Nl; ++l) {
70629f6c5813SMatthew G. Knepley     DMLabel     label, labelNew;
70639f6c5813SMatthew G. Knepley     const char *lname;
70649f6c5813SMatthew G. Knepley     PetscBool   isDepth, isCellType;
70659f6c5813SMatthew G. Knepley 
70669f6c5813SMatthew G. Knepley     PetscCall(DMGetLabelName(bdm, l, &lname));
70679f6c5813SMatthew G. Knepley     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
70689f6c5813SMatthew G. Knepley     if (isDepth) continue;
70699f6c5813SMatthew G. Knepley     PetscCall(PetscStrcmp(lname, "celltype", &isCellType));
70709f6c5813SMatthew G. Knepley     if (isCellType) continue;
70719f6c5813SMatthew G. Knepley     PetscCall(DMCreateLabel(*dm, lname));
70729f6c5813SMatthew G. Knepley     PetscCall(DMGetLabel(bdm, lname, &label));
70739f6c5813SMatthew G. Knepley     PetscCall(DMGetLabel(*dm, lname, &labelNew));
70749f6c5813SMatthew G. Knepley     PetscCall(DMLabelSetType(labelNew, DMLABELEPHEMERAL));
70759f6c5813SMatthew G. Knepley     PetscCall(DMLabelEphemeralSetLabel(labelNew, label));
70769f6c5813SMatthew G. Knepley     PetscCall(DMLabelEphemeralSetTransform(labelNew, tr));
70779f6c5813SMatthew G. Knepley     PetscCall(DMLabelSetUp(labelNew));
70789f6c5813SMatthew G. Knepley   }
70793ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
70809f6c5813SMatthew G. Knepley }
7081