xref: /petsc/src/dm/impls/plex/plexcreate.c (revision cfb853bac00ae5e1ab5f28b65c44cfb296c56161)
1552f7358SJed Brown #define PETSCDM_DLL
2af0996ceSBarry Smith #include <petsc/private/dmpleximpl.h> /*I   "petscdmplex.h"   I*/
3e8f14785SLisandro Dalcin #include <petsc/private/hashseti.h>   /*I   "petscdmplex.h"   I*/
40c312b8eSJed Brown #include <petscsf.h>
54663dae6SJed Brown #include <petscdmplextransform.h>
69f6c5813SMatthew G. Knepley #include <petscdmlabelephemeral.h>
7b7f5c055SJed Brown #include <petsc/private/kernels/blockmatmult.h>
8b7f5c055SJed Brown #include <petsc/private/kernels/blockinvert.h>
9552f7358SJed Brown 
10b09969d6SVaclav Hapla PetscLogEvent DMPLEX_CreateFromFile, DMPLEX_BuildFromCellList, DMPLEX_BuildCoordinatesFromCellList;
1158cd63d5SVaclav Hapla 
129318fe57SMatthew G. Knepley /* External function declarations here */
139318fe57SMatthew G. Knepley static PetscErrorCode DMInitialize_Plex(DM dm);
149318fe57SMatthew G. Knepley 
15e600fa54SMatthew G. Knepley /* This copies internal things in the Plex structure that we generally want when making a new, related Plex */
16d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCopy_Internal(DM dmin, PetscBool copyPeriodicity, PetscBool copyOverlap, DM dmout)
17d71ae5a4SJacob Faibussowitsch {
184fb89dddSMatthew G. Knepley   const PetscReal         *maxCell, *Lstart, *L;
196858538eSMatthew G. Knepley   PetscBool                dist;
206bc1bd01Sksagiyam   DMPlexReorderDefaultFlag reorder;
21e600fa54SMatthew G. Knepley 
22e600fa54SMatthew G. Knepley   PetscFunctionBegin;
23e600fa54SMatthew G. Knepley   if (copyPeriodicity) {
244fb89dddSMatthew G. Knepley     PetscCall(DMGetPeriodicity(dmin, &maxCell, &Lstart, &L));
254fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dmout, maxCell, Lstart, L));
26e600fa54SMatthew G. Knepley   }
279566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeGetDefault(dmin, &dist));
289566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeSetDefault(dmout, dist));
296bc1bd01Sksagiyam   PetscCall(DMPlexReorderGetDefault(dmin, &reorder));
306bc1bd01Sksagiyam   PetscCall(DMPlexReorderSetDefault(dmout, reorder));
31e600fa54SMatthew G. Knepley   ((DM_Plex *)dmout->data)->useHashLocation = ((DM_Plex *)dmin->data)->useHashLocation;
321baa6e33SBarry Smith   if (copyOverlap) PetscCall(DMPlexSetOverlap_Plex(dmout, dmin, 0));
33e600fa54SMatthew G. Knepley   PetscFunctionReturn(0);
34e600fa54SMatthew G. Knepley }
35e600fa54SMatthew G. Knepley 
369318fe57SMatthew G. Knepley /* Replace dm with the contents of ndm, and then destroy ndm
379318fe57SMatthew G. Knepley    - Share the DM_Plex structure
389318fe57SMatthew G. Knepley    - Share the coordinates
399318fe57SMatthew G. Knepley    - Share the SF
409318fe57SMatthew G. Knepley */
41d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexReplace_Internal(DM dm, DM *ndm)
42d71ae5a4SJacob Faibussowitsch {
439318fe57SMatthew G. Knepley   PetscSF          sf;
449318fe57SMatthew G. Knepley   DM               dmNew = *ndm, coordDM, coarseDM;
459318fe57SMatthew G. Knepley   Vec              coords;
464fb89dddSMatthew G. Knepley   const PetscReal *maxCell, *Lstart, *L;
479318fe57SMatthew G. Knepley   PetscInt         dim, cdim;
489318fe57SMatthew G. Knepley 
499318fe57SMatthew G. Knepley   PetscFunctionBegin;
509318fe57SMatthew G. Knepley   if (dm == dmNew) {
519566063dSJacob Faibussowitsch     PetscCall(DMDestroy(ndm));
529318fe57SMatthew G. Knepley     PetscFunctionReturn(0);
539318fe57SMatthew G. Knepley   }
549318fe57SMatthew G. Knepley   dm->setupcalled = dmNew->setupcalled;
559566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dmNew, &dim));
569566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
579566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dmNew, &cdim));
589566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, cdim));
599566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmNew, &sf));
609566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dm, sf));
619566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmNew, &coordDM));
629566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmNew, &coords));
639566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dm, coordDM));
649566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coords));
656858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dmNew, &coordDM));
666858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dmNew, &coords));
676858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinateDM(dm, coordDM));
686858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinatesLocal(dm, coords));
699318fe57SMatthew G. Knepley   /* Do not want to create the coordinate field if it does not already exist, so do not call DMGetCoordinateField() */
706858538eSMatthew G. Knepley   PetscCall(DMFieldDestroy(&dm->coordinates[0].field));
716858538eSMatthew G. Knepley   dm->coordinates[0].field            = dmNew->coordinates[0].field;
7261a622f3SMatthew G. Knepley   ((DM_Plex *)dmNew->data)->coordFunc = ((DM_Plex *)dm->data)->coordFunc;
734fb89dddSMatthew G. Knepley   PetscCall(DMGetPeriodicity(dmNew, &maxCell, &Lstart, &L));
744fb89dddSMatthew G. Knepley   PetscCall(DMSetPeriodicity(dm, maxCell, Lstart, L));
759566063dSJacob Faibussowitsch   PetscCall(DMDestroy_Plex(dm));
769566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(dm));
779318fe57SMatthew G. Knepley   dm->data = dmNew->data;
789318fe57SMatthew G. Knepley   ((DM_Plex *)dmNew->data)->refct++;
799566063dSJacob Faibussowitsch   PetscCall(DMDestroyLabelLinkList_Internal(dm));
809566063dSJacob Faibussowitsch   PetscCall(DMCopyLabels(dmNew, dm, PETSC_OWN_POINTER, PETSC_TRUE, DM_COPY_LABELS_FAIL));
819566063dSJacob Faibussowitsch   PetscCall(DMGetCoarseDM(dmNew, &coarseDM));
829566063dSJacob Faibussowitsch   PetscCall(DMSetCoarseDM(dm, coarseDM));
839566063dSJacob Faibussowitsch   PetscCall(DMDestroy(ndm));
849318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
859318fe57SMatthew G. Knepley }
869318fe57SMatthew G. Knepley 
879318fe57SMatthew G. Knepley /* Swap dm with the contents of dmNew
889318fe57SMatthew G. Knepley    - Swap the DM_Plex structure
899318fe57SMatthew G. Knepley    - Swap the coordinates
909318fe57SMatthew G. Knepley    - Swap the point PetscSF
919318fe57SMatthew G. Knepley */
92d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexSwap_Static(DM dmA, DM dmB)
93d71ae5a4SJacob Faibussowitsch {
949318fe57SMatthew G. Knepley   DM          coordDMA, coordDMB;
959318fe57SMatthew G. Knepley   Vec         coordsA, coordsB;
969318fe57SMatthew G. Knepley   PetscSF     sfA, sfB;
979318fe57SMatthew G. Knepley   DMField     fieldTmp;
989318fe57SMatthew G. Knepley   void       *tmp;
999318fe57SMatthew G. Knepley   DMLabelLink listTmp;
1009318fe57SMatthew G. Knepley   DMLabel     depthTmp;
1019318fe57SMatthew G. Knepley   PetscInt    tmpI;
1029318fe57SMatthew G. Knepley 
1039318fe57SMatthew G. Knepley   PetscFunctionBegin;
1049318fe57SMatthew G. Knepley   if (dmA == dmB) PetscFunctionReturn(0);
1059566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmA, &sfA));
1069566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmB, &sfB));
1079566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)sfA));
1089566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dmA, sfB));
1099566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dmB, sfA));
1109566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)sfA));
1119318fe57SMatthew G. Knepley 
1129566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmA, &coordDMA));
1139566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmB, &coordDMB));
1149566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)coordDMA));
1159566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dmA, coordDMB));
1169566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dmB, coordDMA));
1179566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)coordDMA));
1189318fe57SMatthew G. Knepley 
1199566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmA, &coordsA));
1209566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmB, &coordsB));
1219566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject)coordsA));
1229566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmA, coordsB));
1239566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmB, coordsA));
1249566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject)coordsA));
1259318fe57SMatthew G. Knepley 
1266858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dmA, &coordDMA));
1276858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dmB, &coordDMB));
1286858538eSMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)coordDMA));
1296858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinateDM(dmA, coordDMB));
1306858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinateDM(dmB, coordDMA));
1316858538eSMatthew G. Knepley   PetscCall(PetscObjectDereference((PetscObject)coordDMA));
1326858538eSMatthew G. Knepley 
1336858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dmA, &coordsA));
1346858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dmB, &coordsB));
1356858538eSMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)coordsA));
1366858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinatesLocal(dmA, coordsB));
1376858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinatesLocal(dmB, coordsA));
1386858538eSMatthew G. Knepley   PetscCall(PetscObjectDereference((PetscObject)coordsA));
1396858538eSMatthew G. Knepley 
1406858538eSMatthew G. Knepley   fieldTmp                  = dmA->coordinates[0].field;
1416858538eSMatthew G. Knepley   dmA->coordinates[0].field = dmB->coordinates[0].field;
1426858538eSMatthew G. Knepley   dmB->coordinates[0].field = fieldTmp;
1436858538eSMatthew G. Knepley   fieldTmp                  = dmA->coordinates[1].field;
1446858538eSMatthew G. Knepley   dmA->coordinates[1].field = dmB->coordinates[1].field;
1456858538eSMatthew G. Knepley   dmB->coordinates[1].field = fieldTmp;
1469318fe57SMatthew G. Knepley   tmp                       = dmA->data;
1479318fe57SMatthew G. Knepley   dmA->data                 = dmB->data;
1489318fe57SMatthew G. Knepley   dmB->data                 = tmp;
1499318fe57SMatthew G. Knepley   listTmp                   = dmA->labels;
1509318fe57SMatthew G. Knepley   dmA->labels               = dmB->labels;
1519318fe57SMatthew G. Knepley   dmB->labels               = listTmp;
1529318fe57SMatthew G. Knepley   depthTmp                  = dmA->depthLabel;
1539318fe57SMatthew G. Knepley   dmA->depthLabel           = dmB->depthLabel;
1549318fe57SMatthew G. Knepley   dmB->depthLabel           = depthTmp;
1559318fe57SMatthew G. Knepley   depthTmp                  = dmA->celltypeLabel;
1569318fe57SMatthew G. Knepley   dmA->celltypeLabel        = dmB->celltypeLabel;
1579318fe57SMatthew G. Knepley   dmB->celltypeLabel        = depthTmp;
1589318fe57SMatthew G. Knepley   tmpI                      = dmA->levelup;
1599318fe57SMatthew G. Knepley   dmA->levelup              = dmB->levelup;
1609318fe57SMatthew G. Knepley   dmB->levelup              = tmpI;
1619318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
1629318fe57SMatthew G. Knepley }
1639318fe57SMatthew G. Knepley 
164d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexInterpolateInPlace_Internal(DM dm)
165d71ae5a4SJacob Faibussowitsch {
1669318fe57SMatthew G. Knepley   DM idm;
1679318fe57SMatthew G. Knepley 
1689318fe57SMatthew G. Knepley   PetscFunctionBegin;
1699566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolate(dm, &idm));
1709566063dSJacob Faibussowitsch   PetscCall(DMPlexCopyCoordinates(dm, idm));
17169d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &idm));
1729318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
1739318fe57SMatthew G. Knepley }
1749318fe57SMatthew G. Knepley 
1759318fe57SMatthew G. Knepley /*@C
1769318fe57SMatthew G. Knepley   DMPlexCreateCoordinateSpace - Creates a finite element space for the coordinates
1779318fe57SMatthew G. Knepley 
178a1cb98faSBarry Smith   Collective on dm
1799318fe57SMatthew G. Knepley 
1809318fe57SMatthew G. Knepley   Input Parameters:
1819318fe57SMatthew G. Knepley + DM        - The DM
1824f9ab2b4SJed Brown . degree    - The degree of the finite element or PETSC_DECIDE
1839318fe57SMatthew G. Knepley - coordFunc - An optional function to map new points from refinement to the surface
1849318fe57SMatthew G. Knepley 
1859318fe57SMatthew G. Knepley   Level: advanced
1869318fe57SMatthew G. Knepley 
187a1cb98faSBarry Smith .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `PetscPointFunc`, `PetscFECreateLagrange()`, `DMGetCoordinateDM()`
1889318fe57SMatthew G. Knepley @*/
189d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateCoordinateSpace(DM dm, PetscInt degree, PetscPointFunc coordFunc)
190d71ae5a4SJacob Faibussowitsch {
1919318fe57SMatthew G. Knepley   DM_Plex     *mesh = (DM_Plex *)dm->data;
1929318fe57SMatthew G. Knepley   DM           cdm;
1939318fe57SMatthew G. Knepley   PetscDS      cds;
1949318fe57SMatthew G. Knepley   PetscFE      fe;
1959318fe57SMatthew G. Knepley   PetscClassId id;
1969318fe57SMatthew G. Knepley 
1979318fe57SMatthew G. Knepley   PetscFunctionBegin;
1989566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
1999566063dSJacob Faibussowitsch   PetscCall(DMGetDS(cdm, &cds));
2009566063dSJacob Faibussowitsch   PetscCall(PetscDSGetDiscretization(cds, 0, (PetscObject *)&fe));
2019566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId((PetscObject)fe, &id));
2029318fe57SMatthew G. Knepley   if (id != PETSCFE_CLASSID) {
2039318fe57SMatthew G. Knepley     PetscInt dim, dE, qorder;
2049318fe57SMatthew G. Knepley 
2059566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(dm, &dim));
2069566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDim(dm, &dE));
2079318fe57SMatthew G. Knepley     qorder = degree;
208d0609cedSBarry Smith     PetscObjectOptionsBegin((PetscObject)cdm);
209dc431b0cSMatthew G. Knepley     PetscCall(PetscOptionsBoundedInt("-default_quadrature_order", "Quadrature order is one less than quadrature points per edge", "DMPlexCreateCoordinateSpace", qorder, &qorder, NULL, 0));
210d0609cedSBarry Smith     PetscOptionsEnd();
2114f9ab2b4SJed Brown     if (degree == PETSC_DECIDE) fe = NULL;
2124f9ab2b4SJed Brown     else {
213dc431b0cSMatthew G. Knepley       DMPolytopeType ct;
214dc431b0cSMatthew G. Knepley       PetscInt       cStart, cEnd, ctTmp;
215dc431b0cSMatthew G. Knepley 
216dc431b0cSMatthew G. Knepley       PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
217dc431b0cSMatthew G. Knepley       // Want to match cell types
218dc431b0cSMatthew G. Knepley       if (cEnd > cStart) PetscCall(DMPlexGetCellType(dm, cStart, &ct));
219dc431b0cSMatthew G. Knepley       else ct = DM_POLYTOPE_UNKNOWN;
220dc431b0cSMatthew G. Knepley       ctTmp = (PetscInt)ct;
221dc431b0cSMatthew G. Knepley       PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &ctTmp, 1, MPIU_INT, MPI_MIN, PetscObjectComm((PetscObject)dm)));
222dc431b0cSMatthew G. Knepley       ct = (DMPolytopeType)ctTmp;
223dc431b0cSMatthew G. Knepley       // Work around current bug
224dc431b0cSMatthew G. Knepley       if (ct == DM_POLYTOPE_SEG_PRISM_TENSOR || ct == DM_POLYTOPE_TRI_PRISM_TENSOR || ct == DM_POLYTOPE_QUAD_PRISM_TENSOR) fe = NULL;
225dc431b0cSMatthew G. Knepley       else PetscCall(PetscFECreateLagrangeByCell(PETSC_COMM_SELF, dim, dE, ct, degree, qorder, &fe));
2264f9ab2b4SJed Brown     }
227dc431b0cSMatthew G. Knepley     if (fe) PetscCall(DMProjectCoordinates(dm, fe));
2289566063dSJacob Faibussowitsch     PetscCall(PetscFEDestroy(&fe));
2299318fe57SMatthew G. Knepley   }
2309318fe57SMatthew G. Knepley   mesh->coordFunc = coordFunc;
2319318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
2329318fe57SMatthew G. Knepley }
2339318fe57SMatthew G. Knepley 
2341df5d5c5SMatthew G. Knepley /*@
2351df5d5c5SMatthew G. Knepley   DMPlexCreateDoublet - Creates a mesh of two cells of the specified type, optionally with later refinement.
2361df5d5c5SMatthew G. Knepley 
237d083f849SBarry Smith   Collective
2381df5d5c5SMatthew G. Knepley 
2391df5d5c5SMatthew G. Knepley   Input Parameters:
240a1cb98faSBarry Smith + comm - The communicator for the `DM` object
2411df5d5c5SMatthew G. Knepley . dim - The spatial dimension
2421df5d5c5SMatthew G. Knepley . simplex - Flag for simplicial cells, otherwise they are tensor product cells
2431df5d5c5SMatthew G. Knepley . interpolate - Flag to create intermediate mesh pieces (edges, faces)
2441df5d5c5SMatthew G. Knepley - refinementLimit - A nonzero number indicates the largest admissible volume for a refined cell
2451df5d5c5SMatthew G. Knepley 
2461df5d5c5SMatthew G. Knepley   Output Parameter:
247a1cb98faSBarry Smith . dm - The `DM` object
2481df5d5c5SMatthew G. Knepley 
2491df5d5c5SMatthew G. Knepley   Level: beginner
2501df5d5c5SMatthew G. Knepley 
251a1cb98faSBarry Smith .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMSetType()`, `DMCreate()`
2521df5d5c5SMatthew G. Knepley @*/
253d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateDoublet(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscBool interpolate, PetscReal refinementLimit, DM *newdm)
254d71ae5a4SJacob Faibussowitsch {
2551df5d5c5SMatthew G. Knepley   DM          dm;
2561df5d5c5SMatthew G. Knepley   PetscMPIInt rank;
2571df5d5c5SMatthew G. Knepley 
2581df5d5c5SMatthew G. Knepley   PetscFunctionBegin;
2599566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, &dm));
2609566063dSJacob Faibussowitsch   PetscCall(DMSetType(dm, DMPLEX));
2619566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
2629566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
263ce78fa2fSMatthew G. Knepley   switch (dim) {
264ce78fa2fSMatthew G. Knepley   case 2:
2659566063dSJacob Faibussowitsch     if (simplex) PetscCall(PetscObjectSetName((PetscObject)dm, "triangular"));
2669566063dSJacob Faibussowitsch     else PetscCall(PetscObjectSetName((PetscObject)dm, "quadrilateral"));
267ce78fa2fSMatthew G. Knepley     break;
268ce78fa2fSMatthew G. Knepley   case 3:
2699566063dSJacob Faibussowitsch     if (simplex) PetscCall(PetscObjectSetName((PetscObject)dm, "tetrahedral"));
2709566063dSJacob Faibussowitsch     else PetscCall(PetscObjectSetName((PetscObject)dm, "hexahedral"));
271ce78fa2fSMatthew G. Knepley     break;
272d71ae5a4SJacob Faibussowitsch   default:
273d71ae5a4SJacob Faibussowitsch     SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %" PetscInt_FMT, dim);
274ce78fa2fSMatthew G. Knepley   }
2751df5d5c5SMatthew G. Knepley   if (rank) {
2761df5d5c5SMatthew G. Knepley     PetscInt numPoints[2] = {0, 0};
2779566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, NULL, NULL, NULL, NULL));
2781df5d5c5SMatthew G. Knepley   } else {
2791df5d5c5SMatthew G. Knepley     switch (dim) {
2801df5d5c5SMatthew G. Knepley     case 2:
2811df5d5c5SMatthew G. Knepley       if (simplex) {
2821df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {4, 2};
2831df5d5c5SMatthew G. Knepley         PetscInt    coneSize[6]         = {3, 3, 0, 0, 0, 0};
2841df5d5c5SMatthew G. Knepley         PetscInt    cones[6]            = {2, 3, 4, 5, 4, 3};
2851df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
2861df5d5c5SMatthew G. Knepley         PetscScalar vertexCoords[8]     = {-0.5, 0.5, 0.0, 0.0, 0.0, 1.0, 0.5, 0.5};
2871df5d5c5SMatthew G. Knepley 
2889566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
2891df5d5c5SMatthew G. Knepley       } else {
2901df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {6, 2};
2911df5d5c5SMatthew G. Knepley         PetscInt    coneSize[8]         = {4, 4, 0, 0, 0, 0, 0, 0};
2921df5d5c5SMatthew G. Knepley         PetscInt    cones[8]            = {2, 3, 4, 5, 3, 6, 7, 4};
2931df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
2941df5d5c5SMatthew 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};
2951df5d5c5SMatthew G. Knepley 
2969566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
2971df5d5c5SMatthew G. Knepley       }
2981df5d5c5SMatthew G. Knepley       break;
2991df5d5c5SMatthew G. Knepley     case 3:
3001df5d5c5SMatthew G. Knepley       if (simplex) {
3011df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {5, 2};
3021df5d5c5SMatthew G. Knepley         PetscInt    coneSize[7]         = {4, 4, 0, 0, 0, 0, 0};
3031df5d5c5SMatthew G. Knepley         PetscInt    cones[8]            = {4, 3, 5, 2, 5, 3, 4, 6};
3041df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
3051df5d5c5SMatthew 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};
3061df5d5c5SMatthew G. Knepley 
3079566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
3081df5d5c5SMatthew G. Knepley       } else {
3091df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]         = {12, 2};
3101df5d5c5SMatthew G. Knepley         PetscInt    coneSize[14]         = {8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
3111df5d5c5SMatthew G. Knepley         PetscInt    cones[16]            = {2, 3, 4, 5, 6, 7, 8, 9, 5, 4, 10, 11, 7, 12, 13, 8};
3121df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[16] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
3139371c9d4SSatish 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};
3141df5d5c5SMatthew G. Knepley 
3159566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
3161df5d5c5SMatthew G. Knepley       }
3171df5d5c5SMatthew G. Knepley       break;
318d71ae5a4SJacob Faibussowitsch     default:
319d71ae5a4SJacob Faibussowitsch       SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %" PetscInt_FMT, dim);
3201df5d5c5SMatthew G. Knepley     }
3211df5d5c5SMatthew G. Knepley   }
3221df5d5c5SMatthew G. Knepley   *newdm = dm;
3231df5d5c5SMatthew G. Knepley   if (refinementLimit > 0.0) {
3241df5d5c5SMatthew G. Knepley     DM          rdm;
3251df5d5c5SMatthew G. Knepley     const char *name;
3261df5d5c5SMatthew G. Knepley 
3279566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementUniform(*newdm, PETSC_FALSE));
3289566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementLimit(*newdm, refinementLimit));
3299566063dSJacob Faibussowitsch     PetscCall(DMRefine(*newdm, comm, &rdm));
3309566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject)*newdm, &name));
3319566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)rdm, name));
3329566063dSJacob Faibussowitsch     PetscCall(DMDestroy(newdm));
3331df5d5c5SMatthew G. Knepley     *newdm = rdm;
3341df5d5c5SMatthew G. Knepley   }
3351df5d5c5SMatthew G. Knepley   if (interpolate) {
3365fd9971aSMatthew G. Knepley     DM idm;
3371df5d5c5SMatthew G. Knepley 
3389566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*newdm, &idm));
3399566063dSJacob Faibussowitsch     PetscCall(DMDestroy(newdm));
3401df5d5c5SMatthew G. Knepley     *newdm = idm;
3411df5d5c5SMatthew G. Knepley   }
3421df5d5c5SMatthew G. Knepley   PetscFunctionReturn(0);
3431df5d5c5SMatthew G. Knepley }
3441df5d5c5SMatthew G. Knepley 
345d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
346d71ae5a4SJacob Faibussowitsch {
3479318fe57SMatthew G. Knepley   const PetscInt numVertices    = 2;
3489318fe57SMatthew G. Knepley   PetscInt       markerRight    = 1;
3499318fe57SMatthew G. Knepley   PetscInt       markerLeft     = 1;
3509318fe57SMatthew G. Knepley   PetscBool      markerSeparate = PETSC_FALSE;
3519318fe57SMatthew G. Knepley   Vec            coordinates;
3529318fe57SMatthew G. Knepley   PetscSection   coordSection;
3539318fe57SMatthew G. Knepley   PetscScalar   *coords;
3549318fe57SMatthew G. Knepley   PetscInt       coordSize;
3559318fe57SMatthew G. Knepley   PetscMPIInt    rank;
3569318fe57SMatthew G. Knepley   PetscInt       cdim = 1, v;
357552f7358SJed Brown 
3589318fe57SMatthew G. Knepley   PetscFunctionBegin;
3599566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
3609318fe57SMatthew G. Knepley   if (markerSeparate) {
3619318fe57SMatthew G. Knepley     markerRight = 2;
3629318fe57SMatthew G. Knepley     markerLeft  = 1;
3639318fe57SMatthew G. Knepley   }
3649566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
365c5853193SPierre Jolivet   if (rank == 0) {
3669566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numVertices));
3679566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
3689566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", 0, markerLeft));
3699566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", 1, markerRight));
3709318fe57SMatthew G. Knepley   }
3719566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
3729566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
3739318fe57SMatthew G. Knepley   /* Build coordinates */
3749566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, cdim));
3759566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
3769566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
3779566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, 0, numVertices));
3789566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, cdim));
3799318fe57SMatthew G. Knepley   for (v = 0; v < numVertices; ++v) {
3809566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, cdim));
3819566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, cdim));
3829318fe57SMatthew G. Knepley   }
3839566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
3849566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
3859566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
3869566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
3879566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
3889566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, cdim));
3899566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
3909566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
3919318fe57SMatthew G. Knepley   coords[0] = lower[0];
3929318fe57SMatthew G. Knepley   coords[1] = upper[0];
3939566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
3949566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
3959566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
3969318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
3979318fe57SMatthew G. Knepley }
39826492d91SMatthew G. Knepley 
399d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
400d71ae5a4SJacob Faibussowitsch {
4011df21d24SMatthew G. Knepley   const PetscInt numVertices    = (edges[0] + 1) * (edges[1] + 1);
4021df21d24SMatthew G. Knepley   const PetscInt numEdges       = edges[0] * (edges[1] + 1) + (edges[0] + 1) * edges[1];
403552f7358SJed Brown   PetscInt       markerTop      = 1;
404552f7358SJed Brown   PetscInt       markerBottom   = 1;
405552f7358SJed Brown   PetscInt       markerRight    = 1;
406552f7358SJed Brown   PetscInt       markerLeft     = 1;
407552f7358SJed Brown   PetscBool      markerSeparate = PETSC_FALSE;
408552f7358SJed Brown   Vec            coordinates;
409552f7358SJed Brown   PetscSection   coordSection;
410552f7358SJed Brown   PetscScalar   *coords;
411552f7358SJed Brown   PetscInt       coordSize;
412552f7358SJed Brown   PetscMPIInt    rank;
413552f7358SJed Brown   PetscInt       v, vx, vy;
414552f7358SJed Brown 
415552f7358SJed Brown   PetscFunctionBegin;
4169566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
417552f7358SJed Brown   if (markerSeparate) {
4181df21d24SMatthew G. Knepley     markerTop    = 3;
4191df21d24SMatthew G. Knepley     markerBottom = 1;
4201df21d24SMatthew G. Knepley     markerRight  = 2;
4211df21d24SMatthew G. Knepley     markerLeft   = 4;
422552f7358SJed Brown   }
4239566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
424dd400576SPatrick Sanan   if (rank == 0) {
425552f7358SJed Brown     PetscInt e, ex, ey;
426552f7358SJed Brown 
4279566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numEdges + numVertices));
42848a46eb9SPierre Jolivet     for (e = 0; e < numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2));
4299566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
430552f7358SJed Brown     for (vx = 0; vx <= edges[0]; vx++) {
431552f7358SJed Brown       for (ey = 0; ey < edges[1]; ey++) {
432552f7358SJed Brown         PetscInt edge   = vx * edges[1] + ey + edges[0] * (edges[1] + 1);
433552f7358SJed Brown         PetscInt vertex = ey * (edges[0] + 1) + vx + numEdges;
434da80777bSKarl Rupp         PetscInt cone[2];
435552f7358SJed Brown 
4369371c9d4SSatish Balay         cone[0] = vertex;
4379371c9d4SSatish Balay         cone[1] = vertex + edges[0] + 1;
4389566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, edge, cone));
439552f7358SJed Brown         if (vx == edges[0]) {
4409566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
4419566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
442552f7358SJed Brown           if (ey == edges[1] - 1) {
4439566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
4449566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerRight));
445552f7358SJed Brown           }
446552f7358SJed Brown         } else if (vx == 0) {
4479566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
4489566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
449552f7358SJed Brown           if (ey == edges[1] - 1) {
4509566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
4519566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerLeft));
452552f7358SJed Brown           }
453552f7358SJed Brown         }
454552f7358SJed Brown       }
455552f7358SJed Brown     }
456552f7358SJed Brown     for (vy = 0; vy <= edges[1]; vy++) {
457552f7358SJed Brown       for (ex = 0; ex < edges[0]; ex++) {
458552f7358SJed Brown         PetscInt edge   = vy * edges[0] + ex;
459552f7358SJed Brown         PetscInt vertex = vy * (edges[0] + 1) + ex + numEdges;
460da80777bSKarl Rupp         PetscInt cone[2];
461552f7358SJed Brown 
4629371c9d4SSatish Balay         cone[0] = vertex;
4639371c9d4SSatish Balay         cone[1] = vertex + 1;
4649566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, edge, cone));
465552f7358SJed Brown         if (vy == edges[1]) {
4669566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
4679566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
468552f7358SJed Brown           if (ex == edges[0] - 1) {
4699566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
4709566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerTop));
471552f7358SJed Brown           }
472552f7358SJed Brown         } else if (vy == 0) {
4739566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
4749566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
475552f7358SJed Brown           if (ex == edges[0] - 1) {
4769566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
4779566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerBottom));
478552f7358SJed Brown           }
479552f7358SJed Brown         }
480552f7358SJed Brown       }
481552f7358SJed Brown     }
482552f7358SJed Brown   }
4839566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
4849566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
485552f7358SJed Brown   /* Build coordinates */
4869566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, 2));
4879566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
4889566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
4899566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, numEdges, numEdges + numVertices));
4909566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, 2));
491552f7358SJed Brown   for (v = numEdges; v < numEdges + numVertices; ++v) {
4929566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, 2));
4939566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, 2));
494552f7358SJed Brown   }
4959566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
4969566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
4979566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
4989566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
4999566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
5009566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, 2));
5019566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
5029566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
503552f7358SJed Brown   for (vy = 0; vy <= edges[1]; ++vy) {
504552f7358SJed Brown     for (vx = 0; vx <= edges[0]; ++vx) {
505552f7358SJed Brown       coords[(vy * (edges[0] + 1) + vx) * 2 + 0] = lower[0] + ((upper[0] - lower[0]) / edges[0]) * vx;
506552f7358SJed Brown       coords[(vy * (edges[0] + 1) + vx) * 2 + 1] = lower[1] + ((upper[1] - lower[1]) / edges[1]) * vy;
507552f7358SJed Brown     }
508552f7358SJed Brown   }
5099566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
5109566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
5119566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
512552f7358SJed Brown   PetscFunctionReturn(0);
513552f7358SJed Brown }
514552f7358SJed Brown 
515d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt faces[])
516d71ae5a4SJacob Faibussowitsch {
5179e8abbc3SMichael Lange   PetscInt     vertices[3], numVertices;
5187b59f5a9SMichael Lange   PetscInt     numFaces       = 2 * faces[0] * faces[1] + 2 * faces[1] * faces[2] + 2 * faces[0] * faces[2];
519c2df9bbfSMatthew G. Knepley   PetscInt     markerTop      = 1;
520c2df9bbfSMatthew G. Knepley   PetscInt     markerBottom   = 1;
521c2df9bbfSMatthew G. Knepley   PetscInt     markerFront    = 1;
522c2df9bbfSMatthew G. Knepley   PetscInt     markerBack     = 1;
523c2df9bbfSMatthew G. Knepley   PetscInt     markerRight    = 1;
524c2df9bbfSMatthew G. Knepley   PetscInt     markerLeft     = 1;
525c2df9bbfSMatthew G. Knepley   PetscBool    markerSeparate = PETSC_FALSE;
526552f7358SJed Brown   Vec          coordinates;
527552f7358SJed Brown   PetscSection coordSection;
528552f7358SJed Brown   PetscScalar *coords;
529552f7358SJed Brown   PetscInt     coordSize;
530552f7358SJed Brown   PetscMPIInt  rank;
531552f7358SJed Brown   PetscInt     v, vx, vy, vz;
5327b59f5a9SMichael Lange   PetscInt     voffset, iface = 0, cone[4];
533552f7358SJed Brown 
534552f7358SJed Brown   PetscFunctionBegin;
5351dca8a05SBarry 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");
5369566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
537c2df9bbfSMatthew G. Knepley   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
538c2df9bbfSMatthew G. Knepley   if (markerSeparate) {
539c2df9bbfSMatthew G. Knepley     markerBottom = 1;
540c2df9bbfSMatthew G. Knepley     markerTop    = 2;
541c2df9bbfSMatthew G. Knepley     markerFront  = 3;
542c2df9bbfSMatthew G. Knepley     markerBack   = 4;
543c2df9bbfSMatthew G. Knepley     markerRight  = 5;
544c2df9bbfSMatthew G. Knepley     markerLeft   = 6;
545c2df9bbfSMatthew G. Knepley   }
5469371c9d4SSatish Balay   vertices[0] = faces[0] + 1;
5479371c9d4SSatish Balay   vertices[1] = faces[1] + 1;
5489371c9d4SSatish Balay   vertices[2] = faces[2] + 1;
5499e8abbc3SMichael Lange   numVertices = vertices[0] * vertices[1] * vertices[2];
550dd400576SPatrick Sanan   if (rank == 0) {
551552f7358SJed Brown     PetscInt f;
552552f7358SJed Brown 
5539566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numFaces + numVertices));
55448a46eb9SPierre Jolivet     for (f = 0; f < numFaces; ++f) PetscCall(DMPlexSetConeSize(dm, f, 4));
5559566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
5567b59f5a9SMichael Lange 
5577b59f5a9SMichael Lange     /* Side 0 (Top) */
5587b59f5a9SMichael Lange     for (vy = 0; vy < faces[1]; vy++) {
5597b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5607b59f5a9SMichael Lange         voffset = numFaces + vertices[0] * vertices[1] * (vertices[2] - 1) + vy * vertices[0] + vx;
5619371c9d4SSatish Balay         cone[0] = voffset;
5629371c9d4SSatish Balay         cone[1] = voffset + 1;
5639371c9d4SSatish Balay         cone[2] = voffset + vertices[0] + 1;
5649371c9d4SSatish Balay         cone[3] = voffset + vertices[0];
5659566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
566c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerTop));
567c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerTop));
568c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerTop));
569c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerTop));
570c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 1, markerTop));
5717b59f5a9SMichael Lange         iface++;
572552f7358SJed Brown       }
573552f7358SJed Brown     }
5747b59f5a9SMichael Lange 
5757b59f5a9SMichael Lange     /* Side 1 (Bottom) */
5767b59f5a9SMichael Lange     for (vy = 0; vy < faces[1]; vy++) {
5777b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5787b59f5a9SMichael Lange         voffset = numFaces + vy * (faces[0] + 1) + vx;
5799371c9d4SSatish Balay         cone[0] = voffset + 1;
5809371c9d4SSatish Balay         cone[1] = voffset;
5819371c9d4SSatish Balay         cone[2] = voffset + vertices[0];
5829371c9d4SSatish Balay         cone[3] = voffset + vertices[0] + 1;
5839566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
584c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerBottom));
585c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerBottom));
586c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerBottom));
587c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerBottom));
588c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 1, markerBottom));
5897b59f5a9SMichael Lange         iface++;
590552f7358SJed Brown       }
591552f7358SJed Brown     }
5927b59f5a9SMichael Lange 
5937b59f5a9SMichael Lange     /* Side 2 (Front) */
5947b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
5957b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5967b59f5a9SMichael Lange         voffset = numFaces + vz * vertices[0] * vertices[1] + vx;
5979371c9d4SSatish Balay         cone[0] = voffset;
5989371c9d4SSatish Balay         cone[1] = voffset + 1;
5999371c9d4SSatish Balay         cone[2] = voffset + vertices[0] * vertices[1] + 1;
6009371c9d4SSatish Balay         cone[3] = voffset + vertices[0] * vertices[1];
6019566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
602c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerFront));
603c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerFront));
604c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerFront));
605c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 0, markerFront));
606c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 1, markerFront));
6077b59f5a9SMichael Lange         iface++;
608552f7358SJed Brown       }
6097b59f5a9SMichael Lange     }
6107b59f5a9SMichael Lange 
6117b59f5a9SMichael Lange     /* Side 3 (Back) */
6127b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
6137b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
6147b59f5a9SMichael Lange         voffset = numFaces + vz * vertices[0] * vertices[1] + vertices[0] * (vertices[1] - 1) + vx;
6159371c9d4SSatish Balay         cone[0] = voffset + vertices[0] * vertices[1];
6169371c9d4SSatish Balay         cone[1] = voffset + vertices[0] * vertices[1] + 1;
6179371c9d4SSatish Balay         cone[2] = voffset + 1;
6189371c9d4SSatish Balay         cone[3] = voffset;
6199566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
620c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerBack));
621c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerBack));
622c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 1, markerBack));
623c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 0, markerBack));
624c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 1, markerBack));
6257b59f5a9SMichael Lange         iface++;
6267b59f5a9SMichael Lange       }
6277b59f5a9SMichael Lange     }
6287b59f5a9SMichael Lange 
6297b59f5a9SMichael Lange     /* Side 4 (Left) */
6307b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
6317b59f5a9SMichael Lange       for (vy = 0; vy < faces[1]; vy++) {
6327b59f5a9SMichael Lange         voffset = numFaces + vz * vertices[0] * vertices[1] + vy * vertices[0];
6339371c9d4SSatish Balay         cone[0] = voffset;
6349371c9d4SSatish Balay         cone[1] = voffset + vertices[0] * vertices[1];
6359371c9d4SSatish Balay         cone[2] = voffset + vertices[0] * vertices[1] + vertices[0];
6369371c9d4SSatish Balay         cone[3] = voffset + vertices[0];
6379566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
638c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerLeft));
639c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerLeft));
640c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerLeft));
641c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[1] + 0, markerLeft));
642c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + vertices[0], markerLeft));
6437b59f5a9SMichael Lange         iface++;
6447b59f5a9SMichael Lange       }
6457b59f5a9SMichael Lange     }
6467b59f5a9SMichael Lange 
6477b59f5a9SMichael Lange     /* Side 5 (Right) */
6487b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
6497b59f5a9SMichael Lange       for (vy = 0; vy < faces[1]; vy++) {
650aab5bcd8SJed Brown         voffset = numFaces + vz * vertices[0] * vertices[1] + vy * vertices[0] + faces[0];
6519371c9d4SSatish Balay         cone[0] = voffset + vertices[0] * vertices[1];
6529371c9d4SSatish Balay         cone[1] = voffset;
6539371c9d4SSatish Balay         cone[2] = voffset + vertices[0];
6549371c9d4SSatish Balay         cone[3] = voffset + vertices[0] * vertices[1] + vertices[0];
6559566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
656c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerRight));
657c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + 0, markerRight));
658c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] + 0, markerRight));
659c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + 0, markerRight));
660c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset + vertices[0] * vertices[1] + vertices[0], markerRight));
6617b59f5a9SMichael Lange         iface++;
6627b59f5a9SMichael Lange       }
663552f7358SJed Brown     }
664552f7358SJed Brown   }
6659566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
6669566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
667552f7358SJed Brown   /* Build coordinates */
6689566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, 3));
6699566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
6709566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
6719566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, numFaces, numFaces + numVertices));
6729566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, 3));
673552f7358SJed Brown   for (v = numFaces; v < numFaces + numVertices; ++v) {
6749566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, 3));
6759566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, 3));
676552f7358SJed Brown   }
6779566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
6789566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
6799566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
6809566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
6819566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
6829566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, 3));
6839566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
6849566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
685552f7358SJed Brown   for (vz = 0; vz <= faces[2]; ++vz) {
686552f7358SJed Brown     for (vy = 0; vy <= faces[1]; ++vy) {
687552f7358SJed Brown       for (vx = 0; vx <= faces[0]; ++vx) {
688552f7358SJed Brown         coords[((vz * (faces[1] + 1) + vy) * (faces[0] + 1) + vx) * 3 + 0] = lower[0] + ((upper[0] - lower[0]) / faces[0]) * vx;
689552f7358SJed Brown         coords[((vz * (faces[1] + 1) + vy) * (faces[0] + 1) + vx) * 3 + 1] = lower[1] + ((upper[1] - lower[1]) / faces[1]) * vy;
690552f7358SJed Brown         coords[((vz * (faces[1] + 1) + vy) * (faces[0] + 1) + vx) * 3 + 2] = lower[2] + ((upper[2] - lower[2]) / faces[2]) * vz;
691552f7358SJed Brown       }
692552f7358SJed Brown     }
693552f7358SJed Brown   }
6949566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
6959566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
6969566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
697552f7358SJed Brown   PetscFunctionReturn(0);
698552f7358SJed Brown }
699552f7358SJed Brown 
700d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate)
701d71ae5a4SJacob Faibussowitsch {
7029318fe57SMatthew G. Knepley   PetscFunctionBegin;
7039318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
7049566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim - 1));
7059566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, dim));
7069318fe57SMatthew G. Knepley   switch (dim) {
707d71ae5a4SJacob Faibussowitsch   case 1:
708d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(dm, lower, upper, faces));
709d71ae5a4SJacob Faibussowitsch     break;
710d71ae5a4SJacob Faibussowitsch   case 2:
711d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(dm, lower, upper, faces));
712d71ae5a4SJacob Faibussowitsch     break;
713d71ae5a4SJacob Faibussowitsch   case 3:
714d71ae5a4SJacob Faibussowitsch     PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(dm, lower, upper, faces));
715d71ae5a4SJacob Faibussowitsch     break;
716d71ae5a4SJacob Faibussowitsch   default:
717d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Dimension not supported: %" PetscInt_FMT, dim);
7189318fe57SMatthew G. Knepley   }
7199566063dSJacob Faibussowitsch   if (interpolate) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
7209318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
7219318fe57SMatthew G. Knepley }
7229318fe57SMatthew G. Knepley 
7239318fe57SMatthew G. Knepley /*@C
7249318fe57SMatthew G. Knepley   DMPlexCreateBoxSurfaceMesh - Creates a mesh on the surface of the tensor product of unit intervals (box) using tensor cells (hexahedra).
7259318fe57SMatthew G. Knepley 
7269318fe57SMatthew G. Knepley   Collective
7279318fe57SMatthew G. Knepley 
7289318fe57SMatthew G. Knepley   Input Parameters:
729a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
7309318fe57SMatthew G. Knepley . dim         - The spatial dimension of the box, so the resulting mesh is has dimension dim-1
7319318fe57SMatthew G. Knepley . faces       - Number of faces per dimension, or NULL for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
7329318fe57SMatthew G. Knepley . lower       - The lower left corner, or NULL for (0, 0, 0)
7339318fe57SMatthew G. Knepley . upper       - The upper right corner, or NULL for (1, 1, 1)
7349318fe57SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
7359318fe57SMatthew G. Knepley 
7369318fe57SMatthew G. Knepley   Output Parameter:
737a1cb98faSBarry Smith . dm  - The `DM` object
7389318fe57SMatthew G. Knepley 
7399318fe57SMatthew G. Knepley   Level: beginner
7409318fe57SMatthew G. Knepley 
741a1cb98faSBarry Smith .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMSetFromOptions()`, `DMPlexCreateBoxMesh()`, `DMPlexCreateFromFile()`, `DMSetType()`, `DMCreate()`
7429318fe57SMatthew G. Knepley @*/
743d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateBoxSurfaceMesh(MPI_Comm comm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate, DM *dm)
744d71ae5a4SJacob Faibussowitsch {
7459318fe57SMatthew G. Knepley   PetscInt  fac[3] = {1, 1, 1};
7469318fe57SMatthew G. Knepley   PetscReal low[3] = {0, 0, 0};
7479318fe57SMatthew G. Knepley   PetscReal upp[3] = {1, 1, 1};
7489318fe57SMatthew G. Knepley 
7499318fe57SMatthew G. Knepley   PetscFunctionBegin;
7509566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
7519566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
7529566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(*dm, dim, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, interpolate));
7539318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
7549318fe57SMatthew G. Knepley }
7559318fe57SMatthew G. Knepley 
756d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateLineMesh_Internal(DM dm, PetscInt segments, PetscReal lower, PetscReal upper, DMBoundaryType bd)
757d71ae5a4SJacob Faibussowitsch {
758fdbf62faSLisandro Dalcin   PetscInt     i, fStart, fEnd, numCells = 0, numVerts = 0;
759fdbf62faSLisandro Dalcin   PetscInt     numPoints[2], *coneSize, *cones, *coneOrientations;
760fdbf62faSLisandro Dalcin   PetscScalar *vertexCoords;
761fdbf62faSLisandro Dalcin   PetscReal    L, maxCell;
762fdbf62faSLisandro Dalcin   PetscBool    markerSeparate = PETSC_FALSE;
763fdbf62faSLisandro Dalcin   PetscInt     markerLeft = 1, faceMarkerLeft = 1;
764fdbf62faSLisandro Dalcin   PetscInt     markerRight = 1, faceMarkerRight = 2;
765fdbf62faSLisandro Dalcin   PetscBool    wrap = (bd == DM_BOUNDARY_PERIODIC || bd == DM_BOUNDARY_TWIST) ? PETSC_TRUE : PETSC_FALSE;
766fdbf62faSLisandro Dalcin   PetscMPIInt  rank;
767fdbf62faSLisandro Dalcin 
768fdbf62faSLisandro Dalcin   PetscFunctionBegin;
7699318fe57SMatthew G. Knepley   PetscValidPointer(dm, 1);
770fdbf62faSLisandro Dalcin 
7719566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, 1));
7729566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
7739566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
774fdbf62faSLisandro Dalcin 
7759566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
776dd400576SPatrick Sanan   if (rank == 0) numCells = segments;
777dd400576SPatrick Sanan   if (rank == 0) numVerts = segments + (wrap ? 0 : 1);
778fdbf62faSLisandro Dalcin 
7799371c9d4SSatish Balay   numPoints[0] = numVerts;
7809371c9d4SSatish Balay   numPoints[1] = numCells;
7819566063dSJacob Faibussowitsch   PetscCall(PetscMalloc4(numCells + numVerts, &coneSize, numCells * 2, &cones, numCells + numVerts, &coneOrientations, numVerts, &vertexCoords));
7829566063dSJacob Faibussowitsch   PetscCall(PetscArrayzero(coneOrientations, numCells + numVerts));
783ad540459SPierre Jolivet   for (i = 0; i < numCells; ++i) coneSize[i] = 2;
784ad540459SPierre Jolivet   for (i = 0; i < numVerts; ++i) coneSize[numCells + i] = 0;
7859371c9d4SSatish Balay   for (i = 0; i < numCells; ++i) {
7869371c9d4SSatish Balay     cones[2 * i]     = numCells + i % numVerts;
7879371c9d4SSatish Balay     cones[2 * i + 1] = numCells + (i + 1) % numVerts;
7889371c9d4SSatish Balay   }
789ad540459SPierre Jolivet   for (i = 0; i < numVerts; ++i) vertexCoords[i] = lower + (upper - lower) * ((PetscReal)i / (PetscReal)numCells);
7909566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
7919566063dSJacob Faibussowitsch   PetscCall(PetscFree4(coneSize, cones, coneOrientations, vertexCoords));
792fdbf62faSLisandro Dalcin 
7939566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
7949371c9d4SSatish Balay   if (markerSeparate) {
7959371c9d4SSatish Balay     markerLeft  = faceMarkerLeft;
7969371c9d4SSatish Balay     markerRight = faceMarkerRight;
7979371c9d4SSatish Balay   }
798dd400576SPatrick Sanan   if (!wrap && rank == 0) {
7999566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm, 1, &fStart, &fEnd));
8009566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", fStart, markerLeft));
8019566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", fEnd - 1, markerRight));
8029566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "Face Sets", fStart, faceMarkerLeft));
8039566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "Face Sets", fEnd - 1, faceMarkerRight));
804fdbf62faSLisandro Dalcin   }
805fdbf62faSLisandro Dalcin   if (wrap) {
806fdbf62faSLisandro Dalcin     L       = upper - lower;
807fdbf62faSLisandro Dalcin     maxCell = (PetscReal)1.1 * (L / (PetscReal)PetscMax(1, segments));
8084fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, &maxCell, &lower, &L));
809fdbf62faSLisandro Dalcin   }
8109566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
811fdbf62faSLisandro Dalcin   PetscFunctionReturn(0);
812fdbf62faSLisandro Dalcin }
813fdbf62faSLisandro Dalcin 
814d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxMesh_Simplex_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate)
815d71ae5a4SJacob Faibussowitsch {
8169318fe57SMatthew G. Knepley   DM      boundary, vol;
817c22d3578SMatthew G. Knepley   DMLabel bdlabel;
818d6218766SMatthew G. Knepley 
819d6218766SMatthew G. Knepley   PetscFunctionBegin;
8209318fe57SMatthew G. Knepley   PetscValidPointer(dm, 1);
821c22d3578SMatthew 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");
8229566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &boundary));
8239566063dSJacob Faibussowitsch   PetscCall(DMSetType(boundary, DMPLEX));
8249566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(boundary, dim, faces, lower, upper, PETSC_FALSE));
8259566063dSJacob Faibussowitsch   PetscCall(DMPlexGenerate(boundary, NULL, interpolate, &vol));
826c22d3578SMatthew G. Knepley   PetscCall(DMGetLabel(vol, "marker", &bdlabel));
827c22d3578SMatthew G. Knepley   if (bdlabel) PetscCall(DMPlexLabelComplete(vol, bdlabel));
8285de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_FALSE, vol));
82969d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &vol));
8309566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&boundary));
831d6218766SMatthew G. Knepley   PetscFunctionReturn(0);
832d6218766SMatthew G. Knepley }
833d6218766SMatthew G. Knepley 
834d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateCubeMesh_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], DMBoundaryType bdX, DMBoundaryType bdY, DMBoundaryType bdZ)
835d71ae5a4SJacob Faibussowitsch {
836ed0e4b50SMatthew G. Knepley   DMLabel     cutLabel  = NULL;
837f4eb4c5dSMatthew G. Knepley   PetscInt    markerTop = 1, faceMarkerTop = 1;
838f4eb4c5dSMatthew G. Knepley   PetscInt    markerBottom = 1, faceMarkerBottom = 1;
839f4eb4c5dSMatthew G. Knepley   PetscInt    markerFront = 1, faceMarkerFront = 1;
840f4eb4c5dSMatthew G. Knepley   PetscInt    markerBack = 1, faceMarkerBack = 1;
841f4eb4c5dSMatthew G. Knepley   PetscInt    markerRight = 1, faceMarkerRight = 1;
842f4eb4c5dSMatthew G. Knepley   PetscInt    markerLeft = 1, faceMarkerLeft = 1;
8433dfda0b1SToby Isaac   PetscInt    dim;
844d8211ee3SMatthew G. Knepley   PetscBool   markerSeparate = PETSC_FALSE, cutMarker = PETSC_FALSE;
8453dfda0b1SToby Isaac   PetscMPIInt rank;
8463dfda0b1SToby Isaac 
8473dfda0b1SToby Isaac   PetscFunctionBegin;
8489566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
8499566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
8509566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
8519566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
8529566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL));
8539371c9d4SSatish 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) {
8549371c9d4SSatish Balay     if (cutMarker) {
8559371c9d4SSatish Balay       PetscCall(DMCreateLabel(dm, "periodic_cut"));
8569371c9d4SSatish Balay       PetscCall(DMGetLabel(dm, "periodic_cut", &cutLabel));
8579371c9d4SSatish Balay     }
858d8211ee3SMatthew G. Knepley   }
8593dfda0b1SToby Isaac   switch (dim) {
8603dfda0b1SToby Isaac   case 2:
861f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 3;
862f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
863f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 2;
864f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 4;
8653dfda0b1SToby Isaac     break;
8663dfda0b1SToby Isaac   case 3:
867f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
868f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 2;
869f4eb4c5dSMatthew G. Knepley     faceMarkerFront  = 3;
870f4eb4c5dSMatthew G. Knepley     faceMarkerBack   = 4;
871f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 5;
872f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 6;
8733dfda0b1SToby Isaac     break;
874d71ae5a4SJacob Faibussowitsch   default:
875d71ae5a4SJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Dimension %" PetscInt_FMT " not supported", dim);
8763dfda0b1SToby Isaac   }
8779566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
878f4eb4c5dSMatthew G. Knepley   if (markerSeparate) {
879f4eb4c5dSMatthew G. Knepley     markerBottom = faceMarkerBottom;
880f4eb4c5dSMatthew G. Knepley     markerTop    = faceMarkerTop;
881f4eb4c5dSMatthew G. Knepley     markerFront  = faceMarkerFront;
882f4eb4c5dSMatthew G. Knepley     markerBack   = faceMarkerBack;
883f4eb4c5dSMatthew G. Knepley     markerRight  = faceMarkerRight;
884f4eb4c5dSMatthew G. Knepley     markerLeft   = faceMarkerLeft;
8853dfda0b1SToby Isaac   }
8863dfda0b1SToby Isaac   {
887dd400576SPatrick Sanan     const PetscInt numXEdges    = rank == 0 ? edges[0] : 0;
888dd400576SPatrick Sanan     const PetscInt numYEdges    = rank == 0 ? edges[1] : 0;
889dd400576SPatrick Sanan     const PetscInt numZEdges    = rank == 0 ? edges[2] : 0;
890dd400576SPatrick Sanan     const PetscInt numXVertices = rank == 0 ? (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ? edges[0] : edges[0] + 1) : 0;
891dd400576SPatrick Sanan     const PetscInt numYVertices = rank == 0 ? (bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ? edges[1] : edges[1] + 1) : 0;
892dd400576SPatrick Sanan     const PetscInt numZVertices = rank == 0 ? (bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST ? edges[2] : edges[2] + 1) : 0;
8933dfda0b1SToby Isaac     const PetscInt numCells     = numXEdges * numYEdges * numZEdges;
8943dfda0b1SToby Isaac     const PetscInt numXFaces    = numYEdges * numZEdges;
8953dfda0b1SToby Isaac     const PetscInt numYFaces    = numXEdges * numZEdges;
8963dfda0b1SToby Isaac     const PetscInt numZFaces    = numXEdges * numYEdges;
8973dfda0b1SToby Isaac     const PetscInt numTotXFaces = numXVertices * numXFaces;
8983dfda0b1SToby Isaac     const PetscInt numTotYFaces = numYVertices * numYFaces;
8993dfda0b1SToby Isaac     const PetscInt numTotZFaces = numZVertices * numZFaces;
9003dfda0b1SToby Isaac     const PetscInt numFaces     = numTotXFaces + numTotYFaces + numTotZFaces;
9013dfda0b1SToby Isaac     const PetscInt numTotXEdges = numXEdges * numYVertices * numZVertices;
9023dfda0b1SToby Isaac     const PetscInt numTotYEdges = numYEdges * numXVertices * numZVertices;
9033dfda0b1SToby Isaac     const PetscInt numTotZEdges = numZEdges * numXVertices * numYVertices;
9043dfda0b1SToby Isaac     const PetscInt numVertices  = numXVertices * numYVertices * numZVertices;
9053dfda0b1SToby Isaac     const PetscInt numEdges     = numTotXEdges + numTotYEdges + numTotZEdges;
9063dfda0b1SToby Isaac     const PetscInt firstVertex  = (dim == 2) ? numFaces : numCells;
9073dfda0b1SToby Isaac     const PetscInt firstXFace   = (dim == 2) ? 0 : numCells + numVertices;
9083dfda0b1SToby Isaac     const PetscInt firstYFace   = firstXFace + numTotXFaces;
9093dfda0b1SToby Isaac     const PetscInt firstZFace   = firstYFace + numTotYFaces;
9103dfda0b1SToby Isaac     const PetscInt firstXEdge   = numCells + numFaces + numVertices;
9113dfda0b1SToby Isaac     const PetscInt firstYEdge   = firstXEdge + numTotXEdges;
9123dfda0b1SToby Isaac     const PetscInt firstZEdge   = firstYEdge + numTotYEdges;
9133dfda0b1SToby Isaac     Vec            coordinates;
9143dfda0b1SToby Isaac     PetscSection   coordSection;
9153dfda0b1SToby Isaac     PetscScalar   *coords;
9163dfda0b1SToby Isaac     PetscInt       coordSize;
9173dfda0b1SToby Isaac     PetscInt       v, vx, vy, vz;
9183dfda0b1SToby Isaac     PetscInt       c, f, fx, fy, fz, e, ex, ey, ez;
9193dfda0b1SToby Isaac 
9209566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells + numFaces + numEdges + numVertices));
92148a46eb9SPierre Jolivet     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 6));
92248a46eb9SPierre Jolivet     for (f = firstXFace; f < firstXFace + numFaces; ++f) PetscCall(DMPlexSetConeSize(dm, f, 4));
92348a46eb9SPierre Jolivet     for (e = firstXEdge; e < firstXEdge + numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2));
9249566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
9253dfda0b1SToby Isaac     /* Build cells */
9263dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
9273dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
9283dfda0b1SToby Isaac         for (fx = 0; fx < numXEdges; ++fx) {
9293dfda0b1SToby Isaac           PetscInt cell  = (fz * numYEdges + fy) * numXEdges + fx;
9303dfda0b1SToby Isaac           PetscInt faceB = firstZFace + (fy * numXEdges + fx) * numZVertices + fz;
9313dfda0b1SToby Isaac           PetscInt faceT = firstZFace + (fy * numXEdges + fx) * numZVertices + ((fz + 1) % numZVertices);
9323dfda0b1SToby Isaac           PetscInt faceF = firstYFace + (fz * numXEdges + fx) * numYVertices + fy;
9333dfda0b1SToby Isaac           PetscInt faceK = firstYFace + (fz * numXEdges + fx) * numYVertices + ((fy + 1) % numYVertices);
9343dfda0b1SToby Isaac           PetscInt faceL = firstXFace + (fz * numYEdges + fy) * numXVertices + fx;
9353dfda0b1SToby Isaac           PetscInt faceR = firstXFace + (fz * numYEdges + fy) * numXVertices + ((fx + 1) % numXVertices);
9363dfda0b1SToby Isaac           /* B,  T,  F,  K,  R,  L */
937b5a892a1SMatthew G. Knepley           PetscInt ornt[6] = {-2, 0, 0, -3, 0, -2}; /* ??? */
93842206facSLisandro Dalcin           PetscInt cone[6];
9393dfda0b1SToby Isaac 
9403dfda0b1SToby Isaac           /* no boundary twisting in 3D */
9419371c9d4SSatish Balay           cone[0] = faceB;
9429371c9d4SSatish Balay           cone[1] = faceT;
9439371c9d4SSatish Balay           cone[2] = faceF;
9449371c9d4SSatish Balay           cone[3] = faceK;
9459371c9d4SSatish Balay           cone[4] = faceR;
9469371c9d4SSatish Balay           cone[5] = faceL;
9479566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, cell, cone));
9489566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, cell, ornt));
9499566063dSJacob Faibussowitsch           if (bdX != DM_BOUNDARY_NONE && fx == numXEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
9509566063dSJacob Faibussowitsch           if (bdY != DM_BOUNDARY_NONE && fy == numYEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
9519566063dSJacob Faibussowitsch           if (bdZ != DM_BOUNDARY_NONE && fz == numZEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
9523dfda0b1SToby Isaac         }
9533dfda0b1SToby Isaac       }
9543dfda0b1SToby Isaac     }
9553dfda0b1SToby Isaac     /* Build x faces */
9563dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
9573dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
9583dfda0b1SToby Isaac         for (fx = 0; fx < numXVertices; ++fx) {
9593dfda0b1SToby Isaac           PetscInt face    = firstXFace + (fz * numYEdges + fy) * numXVertices + fx;
9603dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy * numXVertices + fx) * numZEdges + fz;
9613dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (((fy + 1) % numYVertices) * numXVertices + fx) * numZEdges + fz;
9623dfda0b1SToby Isaac           PetscInt edgeB   = firstYEdge + (fz * numXVertices + fx) * numYEdges + fy;
9633dfda0b1SToby Isaac           PetscInt edgeT   = firstYEdge + (((fz + 1) % numZVertices) * numXVertices + fx) * numYEdges + fy;
964b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
9653dfda0b1SToby Isaac           PetscInt cone[4];
9663dfda0b1SToby Isaac 
9673dfda0b1SToby Isaac           if (dim == 3) {
9683dfda0b1SToby Isaac             /* markers */
9693dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
9703dfda0b1SToby Isaac               if (fx == numXVertices - 1) {
9719566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerRight));
9729566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerRight));
9739371c9d4SSatish Balay               } else if (fx == 0) {
9749566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerLeft));
9759566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerLeft));
9763dfda0b1SToby Isaac               }
9773dfda0b1SToby Isaac             }
9783dfda0b1SToby Isaac           }
9799371c9d4SSatish Balay           cone[0] = edgeB;
9809371c9d4SSatish Balay           cone[1] = edgeR;
9819371c9d4SSatish Balay           cone[2] = edgeT;
9829371c9d4SSatish Balay           cone[3] = edgeL;
9839566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
9849566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
9853dfda0b1SToby Isaac         }
9863dfda0b1SToby Isaac       }
9873dfda0b1SToby Isaac     }
9883dfda0b1SToby Isaac     /* Build y faces */
9893dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
99042206facSLisandro Dalcin       for (fx = 0; fx < numXEdges; ++fx) {
9913dfda0b1SToby Isaac         for (fy = 0; fy < numYVertices; ++fy) {
9923dfda0b1SToby Isaac           PetscInt face    = firstYFace + (fz * numXEdges + fx) * numYVertices + fy;
9933dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy * numXVertices + fx) * numZEdges + fz;
9943dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (fy * numXVertices + ((fx + 1) % numXVertices)) * numZEdges + fz;
9953dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz * numYVertices + fy) * numXEdges + fx;
9963dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (((fz + 1) % numZVertices) * numYVertices + fy) * numXEdges + fx;
997b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
9983dfda0b1SToby Isaac           PetscInt cone[4];
9993dfda0b1SToby Isaac 
10003dfda0b1SToby Isaac           if (dim == 3) {
10013dfda0b1SToby Isaac             /* markers */
10023dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
10033dfda0b1SToby Isaac               if (fy == numYVertices - 1) {
10049566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerBack));
10059566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerBack));
10069371c9d4SSatish Balay               } else if (fy == 0) {
10079566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerFront));
10089566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerFront));
10093dfda0b1SToby Isaac               }
10103dfda0b1SToby Isaac             }
10113dfda0b1SToby Isaac           }
10129371c9d4SSatish Balay           cone[0] = edgeB;
10139371c9d4SSatish Balay           cone[1] = edgeR;
10149371c9d4SSatish Balay           cone[2] = edgeT;
10159371c9d4SSatish Balay           cone[3] = edgeL;
10169566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
10179566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
10183dfda0b1SToby Isaac         }
10193dfda0b1SToby Isaac       }
10203dfda0b1SToby Isaac     }
10213dfda0b1SToby Isaac     /* Build z faces */
10223dfda0b1SToby Isaac     for (fy = 0; fy < numYEdges; ++fy) {
10233dfda0b1SToby Isaac       for (fx = 0; fx < numXEdges; ++fx) {
10243dfda0b1SToby Isaac         for (fz = 0; fz < numZVertices; fz++) {
10253dfda0b1SToby Isaac           PetscInt face    = firstZFace + (fy * numXEdges + fx) * numZVertices + fz;
10263dfda0b1SToby Isaac           PetscInt edgeL   = firstYEdge + (fz * numXVertices + fx) * numYEdges + fy;
10273dfda0b1SToby Isaac           PetscInt edgeR   = firstYEdge + (fz * numXVertices + ((fx + 1) % numXVertices)) * numYEdges + fy;
10283dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz * numYVertices + fy) * numXEdges + fx;
10293dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (fz * numYVertices + ((fy + 1) % numYVertices)) * numXEdges + fx;
1030b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
10313dfda0b1SToby Isaac           PetscInt cone[4];
10323dfda0b1SToby Isaac 
10333dfda0b1SToby Isaac           if (dim == 2) {
10349371c9d4SSatish Balay             if (bdX == DM_BOUNDARY_TWIST && fx == numXEdges - 1) {
10359371c9d4SSatish Balay               edgeR += numYEdges - 1 - 2 * fy;
10369371c9d4SSatish Balay               ornt[1] = -1;
10379371c9d4SSatish Balay             }
10389371c9d4SSatish Balay             if (bdY == DM_BOUNDARY_TWIST && fy == numYEdges - 1) {
10399371c9d4SSatish Balay               edgeT += numXEdges - 1 - 2 * fx;
10409371c9d4SSatish Balay               ornt[2] = 0;
10419371c9d4SSatish Balay             }
10429566063dSJacob Faibussowitsch             if (bdX != DM_BOUNDARY_NONE && fx == numXEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, face, 2));
10439566063dSJacob Faibussowitsch             if (bdY != DM_BOUNDARY_NONE && fy == numYEdges - 1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, face, 2));
1044d1c88043SMatthew G. Knepley           } else {
10453dfda0b1SToby Isaac             /* markers */
10463dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
10473dfda0b1SToby Isaac               if (fz == numZVertices - 1) {
10489566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerTop));
10499566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerTop));
10509371c9d4SSatish Balay               } else if (fz == 0) {
10519566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerBottom));
10529566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerBottom));
10533dfda0b1SToby Isaac               }
10543dfda0b1SToby Isaac             }
10553dfda0b1SToby Isaac           }
10569371c9d4SSatish Balay           cone[0] = edgeB;
10579371c9d4SSatish Balay           cone[1] = edgeR;
10589371c9d4SSatish Balay           cone[2] = edgeT;
10599371c9d4SSatish Balay           cone[3] = edgeL;
10609566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
10619566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
10623dfda0b1SToby Isaac         }
10633dfda0b1SToby Isaac       }
10643dfda0b1SToby Isaac     }
10653dfda0b1SToby Isaac     /* Build Z edges*/
10663dfda0b1SToby Isaac     for (vy = 0; vy < numYVertices; vy++) {
10673dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
10683dfda0b1SToby Isaac         for (ez = 0; ez < numZEdges; ez++) {
10693dfda0b1SToby Isaac           const PetscInt edge    = firstZEdge + (vy * numXVertices + vx) * numZEdges + ez;
10703dfda0b1SToby Isaac           const PetscInt vertexB = firstVertex + (ez * numYVertices + vy) * numXVertices + vx;
10713dfda0b1SToby Isaac           const PetscInt vertexT = firstVertex + (((ez + 1) % numZVertices) * numYVertices + vy) * numXVertices + vx;
10723dfda0b1SToby Isaac           PetscInt       cone[2];
10733dfda0b1SToby Isaac 
10749371c9d4SSatish Balay           cone[0] = vertexB;
10759371c9d4SSatish Balay           cone[1] = vertexT;
1076c2df9bbfSMatthew G. Knepley           PetscCall(DMPlexSetCone(dm, edge, cone));
10773dfda0b1SToby Isaac           if (dim == 3) {
10783dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
10793dfda0b1SToby Isaac               if (vx == numXVertices - 1) {
10809566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
1081c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1082c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1083c2df9bbfSMatthew G. Knepley               } else if (vx == 0) {
10849566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
1085c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1086c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
10873dfda0b1SToby Isaac               }
10883dfda0b1SToby Isaac             }
10893dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
10903dfda0b1SToby Isaac               if (vy == numYVertices - 1) {
10919566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBack));
1092c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBack));
1093c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBack));
1094c2df9bbfSMatthew G. Knepley               } else if (vy == 0) {
10959566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerFront));
1096c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerFront));
1097c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerFront));
10983dfda0b1SToby Isaac               }
10993dfda0b1SToby Isaac             }
11003dfda0b1SToby Isaac           }
11013dfda0b1SToby Isaac         }
11023dfda0b1SToby Isaac       }
11033dfda0b1SToby Isaac     }
11043dfda0b1SToby Isaac     /* Build Y edges*/
11053dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
11063dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
11073dfda0b1SToby Isaac         for (ey = 0; ey < numYEdges; ey++) {
11083dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdY == DM_BOUNDARY_TWIST && ey == numYEdges - 1) ? (numXVertices - vx - 1) : (vz * numYVertices + ((ey + 1) % numYVertices)) * numXVertices + vx;
11093dfda0b1SToby Isaac           const PetscInt edge    = firstYEdge + (vz * numXVertices + vx) * numYEdges + ey;
11103dfda0b1SToby Isaac           const PetscInt vertexF = firstVertex + (vz * numYVertices + ey) * numXVertices + vx;
11113dfda0b1SToby Isaac           const PetscInt vertexK = firstVertex + nextv;
11123dfda0b1SToby Isaac           PetscInt       cone[2];
11133dfda0b1SToby Isaac 
11149371c9d4SSatish Balay           cone[0] = vertexF;
11159371c9d4SSatish Balay           cone[1] = vertexK;
11169566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, edge, cone));
11173dfda0b1SToby Isaac           if (dim == 2) {
11183dfda0b1SToby Isaac             if ((bdX != DM_BOUNDARY_PERIODIC) && (bdX != DM_BOUNDARY_TWIST)) {
11193dfda0b1SToby Isaac               if (vx == numXVertices - 1) {
11209566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerRight));
11219566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
11229566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1123c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1124d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
11259566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerLeft));
11269566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
11279566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1128c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
11293dfda0b1SToby Isaac               }
1130d8211ee3SMatthew G. Knepley             } else {
11314c67ea77SStefano Zampini               if (vx == 0 && cutLabel) {
11329566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, edge, 1));
11339566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, cone[0], 1));
1134c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMLabelSetValue(cutLabel, cone[1], 1));
11353dfda0b1SToby Isaac               }
1136d8211ee3SMatthew G. Knepley             }
1137d8211ee3SMatthew G. Knepley           } else {
11383dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
11393dfda0b1SToby Isaac               if (vx == numXVertices - 1) {
11409566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
1141c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1142c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1143d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
11449566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
1145c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1146c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
11473dfda0b1SToby Isaac               }
11483dfda0b1SToby Isaac             }
11493dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
11503dfda0b1SToby Isaac               if (vz == numZVertices - 1) {
11519566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
1152c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1153c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1154d8211ee3SMatthew G. Knepley               } else if (vz == 0) {
11559566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
1156c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1157c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
11583dfda0b1SToby Isaac               }
11593dfda0b1SToby Isaac             }
11603dfda0b1SToby Isaac           }
11613dfda0b1SToby Isaac         }
11623dfda0b1SToby Isaac       }
11633dfda0b1SToby Isaac     }
11643dfda0b1SToby Isaac     /* Build X edges*/
11653dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
11663dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; vy++) {
11673dfda0b1SToby Isaac         for (ex = 0; ex < numXEdges; ex++) {
11683dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdX == DM_BOUNDARY_TWIST && ex == numXEdges - 1) ? (numYVertices - vy - 1) * numXVertices : (vz * numYVertices + vy) * numXVertices + (ex + 1) % numXVertices;
11693dfda0b1SToby Isaac           const PetscInt edge    = firstXEdge + (vz * numYVertices + vy) * numXEdges + ex;
11703dfda0b1SToby Isaac           const PetscInt vertexL = firstVertex + (vz * numYVertices + vy) * numXVertices + ex;
11713dfda0b1SToby Isaac           const PetscInt vertexR = firstVertex + nextv;
11723dfda0b1SToby Isaac           PetscInt       cone[2];
11733dfda0b1SToby Isaac 
11749371c9d4SSatish Balay           cone[0] = vertexL;
11759371c9d4SSatish Balay           cone[1] = vertexR;
11769566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, edge, cone));
11773dfda0b1SToby Isaac           if (dim == 2) {
11783dfda0b1SToby Isaac             if ((bdY != DM_BOUNDARY_PERIODIC) && (bdY != DM_BOUNDARY_TWIST)) {
11793dfda0b1SToby Isaac               if (vy == numYVertices - 1) {
11809566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerTop));
11819566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
11829566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1183c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1184d8211ee3SMatthew G. Knepley               } else if (vy == 0) {
11859566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerBottom));
11869566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
11879566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1188c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
11893dfda0b1SToby Isaac               }
1190d8211ee3SMatthew G. Knepley             } else {
11914c67ea77SStefano Zampini               if (vy == 0 && cutLabel) {
11929566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, edge, 1));
11939566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, cone[0], 1));
1194c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMLabelSetValue(cutLabel, cone[1], 1));
11953dfda0b1SToby Isaac               }
1196d8211ee3SMatthew G. Knepley             }
1197d8211ee3SMatthew G. Knepley           } else {
11983dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
11993dfda0b1SToby Isaac               if (vy == numYVertices - 1) {
12009566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBack));
1201c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBack));
1202c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBack));
1203c2df9bbfSMatthew G. Knepley               } else if (vy == 0) {
12049566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerFront));
1205c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerFront));
1206c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerFront));
12073dfda0b1SToby Isaac               }
12083dfda0b1SToby Isaac             }
12093dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
12103dfda0b1SToby Isaac               if (vz == numZVertices - 1) {
12119566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
1212c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1213c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1214c2df9bbfSMatthew G. Knepley               } else if (vz == 0) {
12159566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
1216c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1217c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges - 1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
12183dfda0b1SToby Isaac               }
12193dfda0b1SToby Isaac             }
12203dfda0b1SToby Isaac           }
12213dfda0b1SToby Isaac         }
12223dfda0b1SToby Isaac       }
12233dfda0b1SToby Isaac     }
12249566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
12259566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
12263dfda0b1SToby Isaac     /* Build coordinates */
12279566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
12289566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
12299566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
12309566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numVertices));
12313dfda0b1SToby Isaac     for (v = firstVertex; v < firstVertex + numVertices; ++v) {
12329566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
12339566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
12343dfda0b1SToby Isaac     }
12359566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
12369566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
12379566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
12389566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
12399566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
12409566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
12419566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates, VECSTANDARD));
12429566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
12433dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; ++vz) {
12443dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; ++vy) {
12453dfda0b1SToby Isaac         for (vx = 0; vx < numXVertices; ++vx) {
12463dfda0b1SToby Isaac           coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 0] = lower[0] + ((upper[0] - lower[0]) / numXEdges) * vx;
12473dfda0b1SToby Isaac           coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 1] = lower[1] + ((upper[1] - lower[1]) / numYEdges) * vy;
1248ad540459SPierre Jolivet           if (dim == 3) coords[((vz * numYVertices + vy) * numXVertices + vx) * dim + 2] = lower[2] + ((upper[2] - lower[2]) / numZEdges) * vz;
12493dfda0b1SToby Isaac         }
12503dfda0b1SToby Isaac       }
12513dfda0b1SToby Isaac     }
12529566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
12539566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
12549566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
12553dfda0b1SToby Isaac   }
12563dfda0b1SToby Isaac   PetscFunctionReturn(0);
12573dfda0b1SToby Isaac }
12583dfda0b1SToby Isaac 
1259d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxMesh_Tensor_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
1260d71ae5a4SJacob Faibussowitsch {
12619318fe57SMatthew G. Knepley   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
12629318fe57SMatthew G. Knepley   PetscInt       fac[3] = {0, 0, 0}, d;
1263552f7358SJed Brown 
1264552f7358SJed Brown   PetscFunctionBegin;
12659318fe57SMatthew G. Knepley   PetscValidPointer(dm, 1);
12669318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
12679566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
12689371c9d4SSatish Balay   for (d = 0; d < dim; ++d) {
12699371c9d4SSatish Balay     fac[d] = faces[d];
12709371c9d4SSatish Balay     bdt[d] = periodicity[d];
12719371c9d4SSatish Balay   }
12729566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateCubeMesh_Internal(dm, lower, upper, fac, bdt[0], bdt[1], bdt[2]));
12739371c9d4SSatish 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))) {
12746858538eSMatthew G. Knepley     PetscReal L[3]       = {-1., -1., 0.};
12756858538eSMatthew G. Knepley     PetscReal maxCell[3] = {-1., -1., 0.};
1276552f7358SJed Brown 
12779318fe57SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
12786858538eSMatthew G. Knepley       if (periodicity[d] != DM_BOUNDARY_NONE) {
12799318fe57SMatthew G. Knepley         L[d]       = upper[d] - lower[d];
12809318fe57SMatthew G. Knepley         maxCell[d] = 1.1 * (L[d] / PetscMax(1, faces[d]));
1281768d5fceSMatthew G. Knepley       }
12826858538eSMatthew G. Knepley     }
12834fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, maxCell, lower, L));
1284768d5fceSMatthew G. Knepley   }
12859566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
12869318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
12879318fe57SMatthew G. Knepley }
12889318fe57SMatthew G. Knepley 
1289d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoxMesh_Internal(DM dm, PetscInt dim, PetscBool simplex, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate)
1290d71ae5a4SJacob Faibussowitsch {
12919318fe57SMatthew G. Knepley   PetscFunctionBegin;
12929566063dSJacob Faibussowitsch   if (dim == 1) PetscCall(DMPlexCreateLineMesh_Internal(dm, faces[0], lower[0], upper[0], periodicity[0]));
12939566063dSJacob Faibussowitsch   else if (simplex) PetscCall(DMPlexCreateBoxMesh_Simplex_Internal(dm, dim, faces, lower, upper, periodicity, interpolate));
12949566063dSJacob Faibussowitsch   else PetscCall(DMPlexCreateBoxMesh_Tensor_Internal(dm, dim, faces, lower, upper, periodicity));
12959318fe57SMatthew G. Knepley   if (!interpolate && dim > 1 && !simplex) {
1296768d5fceSMatthew G. Knepley     DM udm;
1297768d5fceSMatthew G. Knepley 
12989566063dSJacob Faibussowitsch     PetscCall(DMPlexUninterpolate(dm, &udm));
12999566063dSJacob Faibussowitsch     PetscCall(DMPlexCopyCoordinates(dm, udm));
130069d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &udm));
1301768d5fceSMatthew G. Knepley   }
1302768d5fceSMatthew G. Knepley   PetscFunctionReturn(0);
1303c8c68bd8SToby Isaac }
1304c8c68bd8SToby Isaac 
1305768d5fceSMatthew G. Knepley /*@C
1306768d5fceSMatthew G. Knepley   DMPlexCreateBoxMesh - Creates a mesh on the tensor product of unit intervals (box) using simplices or tensor cells (hexahedra).
1307768d5fceSMatthew G. Knepley 
1308d083f849SBarry Smith   Collective
1309768d5fceSMatthew G. Knepley 
1310768d5fceSMatthew G. Knepley   Input Parameters:
1311a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
1312768d5fceSMatthew G. Knepley . dim         - The spatial dimension
1313a1cb98faSBarry Smith . simplex     - `PETSC_TRUE` for simplices, `PETSC_FALSE` for tensor cells
1314fdbf62faSLisandro Dalcin . faces       - Number of faces per dimension, or NULL for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
1315768d5fceSMatthew G. Knepley . lower       - The lower left corner, or NULL for (0, 0, 0)
1316768d5fceSMatthew G. Knepley . upper       - The upper right corner, or NULL for (1, 1, 1)
1317a1cb98faSBarry Smith . periodicity - The boundary type for the X,Y,Z direction, or NULL for `DM_BOUNDARY_NONE`
1318768d5fceSMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
1319768d5fceSMatthew G. Knepley 
1320768d5fceSMatthew G. Knepley   Output Parameter:
1321a1cb98faSBarry Smith . dm  - The `DM` object
1322768d5fceSMatthew G. Knepley 
1323768d5fceSMatthew G. Knepley   Level: beginner
1324768d5fceSMatthew G. Knepley 
1325a1cb98faSBarry Smith   Note:
1326a1cb98faSBarry Smith    To customize this mesh using options, use
1327a1cb98faSBarry Smith .vb
1328a1cb98faSBarry Smith   DMCreate(comm, &dm);
1329a1cb98faSBarry Smith   DMSetType(dm, DMPLEX);
1330a1cb98faSBarry Smith   DMSetFromOptions(dm);
1331a1cb98faSBarry Smith .ve
1332a1cb98faSBarry Smith and use the options in `DMSetFromOptions()`.
1333a1cb98faSBarry Smith 
1334a1cb98faSBarry Smith   Here is the numbering returned for 2 faces in each direction for tensor cells:
1335a1cb98faSBarry Smith .vb
1336a1cb98faSBarry Smith  10---17---11---18----12
1337a1cb98faSBarry Smith   |         |         |
1338a1cb98faSBarry Smith   |         |         |
1339a1cb98faSBarry Smith  20    2   22    3    24
1340a1cb98faSBarry Smith   |         |         |
1341a1cb98faSBarry Smith   |         |         |
1342a1cb98faSBarry Smith   7---15----8---16----9
1343a1cb98faSBarry Smith   |         |         |
1344a1cb98faSBarry Smith   |         |         |
1345a1cb98faSBarry Smith  19    0   21    1   23
1346a1cb98faSBarry Smith   |         |         |
1347a1cb98faSBarry Smith   |         |         |
1348a1cb98faSBarry Smith   4---13----5---14----6
1349a1cb98faSBarry Smith .ve
1350a1cb98faSBarry Smith and for simplicial cells
1351a1cb98faSBarry Smith .vb
1352a1cb98faSBarry Smith  14----8---15----9----16
1353a1cb98faSBarry Smith   |\     5  |\      7 |
1354a1cb98faSBarry Smith   | \       | \       |
1355a1cb98faSBarry Smith  13   2    14    3    15
1356a1cb98faSBarry Smith   | 4   \   | 6   \   |
1357a1cb98faSBarry Smith   |       \ |       \ |
1358a1cb98faSBarry Smith  11----6---12----7----13
1359a1cb98faSBarry Smith   |\        |\        |
1360a1cb98faSBarry Smith   | \    1  | \     3 |
1361a1cb98faSBarry Smith  10   0    11    1    12
1362a1cb98faSBarry Smith   | 0   \   | 2   \   |
1363a1cb98faSBarry Smith   |       \ |       \ |
1364a1cb98faSBarry Smith   8----4----9----5----10
1365a1cb98faSBarry Smith .ve
1366a1cb98faSBarry Smith 
1367a1cb98faSBarry Smith .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMSetFromOptions()`, `DMPlexCreateFromFile()`, `DMPlexCreateHexCylinderMesh()`, `DMSetType()`, `DMCreate()`
1368768d5fceSMatthew G. Knepley @*/
1369d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateBoxMesh(MPI_Comm comm, PetscInt dim, PetscBool simplex, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate, DM *dm)
1370d71ae5a4SJacob Faibussowitsch {
13719318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
1372fdbf62faSLisandro Dalcin   PetscReal      low[3] = {0, 0, 0};
1373fdbf62faSLisandro Dalcin   PetscReal      upp[3] = {1, 1, 1};
1374fdbf62faSLisandro Dalcin   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
1375552f7358SJed Brown 
1376768d5fceSMatthew G. Knepley   PetscFunctionBegin;
13779566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
13789566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
13799566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxMesh_Internal(*dm, dim, simplex, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt, interpolate));
13807ff04441SMatthew G. Knepley   if (periodicity) PetscCall(DMLocalizeCoordinates(*dm));
13819318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
13829318fe57SMatthew G. Knepley }
1383fdbf62faSLisandro Dalcin 
1384d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateWedgeBoxMesh_Internal(DM dm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
1385d71ae5a4SJacob Faibussowitsch {
13869318fe57SMatthew G. Knepley   DM       bdm, vol;
13879318fe57SMatthew G. Knepley   PetscInt i;
13889318fe57SMatthew G. Knepley 
13899318fe57SMatthew G. Knepley   PetscFunctionBegin;
139008401ef6SPierre Jolivet   for (i = 0; i < 3; ++i) PetscCheck(periodicity[i] == DM_BOUNDARY_NONE, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Periodicity not yet supported");
13919566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &bdm));
13929566063dSJacob Faibussowitsch   PetscCall(DMSetType(bdm, DMPLEX));
13939566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(bdm, 2));
13949566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxMesh_Simplex_Internal(bdm, 2, faces, lower, upper, periodicity, PETSC_TRUE));
13959566063dSJacob Faibussowitsch   PetscCall(DMPlexExtrude(bdm, faces[2], upper[2] - lower[2], PETSC_TRUE, PETSC_FALSE, NULL, NULL, &vol));
13969566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&bdm));
139769d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &vol));
13989318fe57SMatthew G. Knepley   if (lower[2] != 0.0) {
13999318fe57SMatthew G. Knepley     Vec          v;
14009318fe57SMatthew G. Knepley     PetscScalar *x;
14019318fe57SMatthew G. Knepley     PetscInt     cDim, n;
14029318fe57SMatthew G. Knepley 
14039566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &v));
14049566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(v, &cDim));
14059566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(v, &n));
14069566063dSJacob Faibussowitsch     PetscCall(VecGetArray(v, &x));
14079318fe57SMatthew G. Knepley     x += cDim;
14089318fe57SMatthew G. Knepley     for (i = 0; i < n; i += cDim) x[i] += lower[2];
14099566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(v, &x));
14109566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, v));
14119318fe57SMatthew G. Knepley   }
1412552f7358SJed Brown   PetscFunctionReturn(0);
1413552f7358SJed Brown }
1414552f7358SJed Brown 
141500dabe28SStefano Zampini /*@
141600dabe28SStefano Zampini   DMPlexCreateWedgeBoxMesh - Creates a 3-D mesh tesselating the (x,y) plane and extruding in the third direction using wedge cells.
141700dabe28SStefano Zampini 
1418d083f849SBarry Smith   Collective
141900dabe28SStefano Zampini 
142000dabe28SStefano Zampini   Input Parameters:
1421a1cb98faSBarry Smith + comm        - The communicator for the `DM` object
142200dabe28SStefano Zampini . faces       - Number of faces per dimension, or NULL for (1, 1, 1)
142300dabe28SStefano Zampini . lower       - The lower left corner, or NULL for (0, 0, 0)
142400dabe28SStefano Zampini . upper       - The upper right corner, or NULL for (1, 1, 1)
1425a1cb98faSBarry Smith . periodicity - The boundary type for the X,Y,Z direction, or NULL for `DM_BOUNDARY_NONE`
1426a1cb98faSBarry Smith . orderHeight - If `PETSC_TRUE`, orders the extruded cells in the height first. Otherwise, orders the cell on the layers first
142700dabe28SStefano Zampini - interpolate - Flag to create intermediate mesh pieces (edges, faces)
142800dabe28SStefano Zampini 
142900dabe28SStefano Zampini   Output Parameter:
1430a1cb98faSBarry Smith . dm  - The `DM` object
143100dabe28SStefano Zampini 
143200dabe28SStefano Zampini   Level: beginner
143300dabe28SStefano Zampini 
1434a1cb98faSBarry Smith .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreateHexCylinderMesh()`, `DMPlexCreateWedgeCylinderMesh()`, `DMExtrude()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
143500dabe28SStefano Zampini @*/
1436d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateWedgeBoxMesh(MPI_Comm comm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool orderHeight, PetscBool interpolate, DM *dm)
1437d71ae5a4SJacob Faibussowitsch {
14389318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
143900dabe28SStefano Zampini   PetscReal      low[3] = {0, 0, 0};
144000dabe28SStefano Zampini   PetscReal      upp[3] = {1, 1, 1};
144100dabe28SStefano Zampini   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
144200dabe28SStefano Zampini 
144300dabe28SStefano Zampini   PetscFunctionBegin;
14449566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
14459566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
14469566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateWedgeBoxMesh_Internal(*dm, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt));
1447d410b0cfSMatthew G. Knepley   if (!interpolate) {
1448d410b0cfSMatthew G. Knepley     DM udm;
144900dabe28SStefano Zampini 
14509566063dSJacob Faibussowitsch     PetscCall(DMPlexUninterpolate(*dm, &udm));
145169d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(*dm, &udm));
145200dabe28SStefano Zampini   }
14537ff04441SMatthew G. Knepley   if (periodicity) PetscCall(DMLocalizeCoordinates(*dm));
145400dabe28SStefano Zampini   PetscFunctionReturn(0);
145500dabe28SStefano Zampini }
145600dabe28SStefano Zampini 
1457*cfb853baSMatthew G. Knepley /*
1458*cfb853baSMatthew 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.
1459*cfb853baSMatthew G. Knepley                                            Ordering is lexicographic with lowest index as least significant in ordering.
1460*cfb853baSMatthew G. Knepley                                            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}.
1461*cfb853baSMatthew G. Knepley 
1462*cfb853baSMatthew G. Knepley   Input Parameters:
1463*cfb853baSMatthew G. Knepley + len - The length of the tuple
1464*cfb853baSMatthew G. Knepley . max - The maximum for each dimension, so values are in [0, max)
1465*cfb853baSMatthew G. Knepley - tup - A tuple of length len+1: tup[len] > 0 indicates a stopping condition
1466*cfb853baSMatthew G. Knepley 
1467*cfb853baSMatthew G. Knepley   Output Parameter:
1468*cfb853baSMatthew G. Knepley . tup - A tuple of len integers whose entries are at most 'max'
1469*cfb853baSMatthew G. Knepley 
1470*cfb853baSMatthew G. Knepley   Level: developer
1471*cfb853baSMatthew G. Knepley 
1472*cfb853baSMatthew G. Knepley .seealso: PetscDualSpaceTensorPointLexicographic_Internal(), PetscDualSpaceLatticePointLexicographic_Internal()
1473*cfb853baSMatthew G. Knepley */
1474*cfb853baSMatthew G. Knepley static PetscErrorCode DMPlexTensorPointLexicographic_Private(PetscInt len, const PetscInt max[], PetscInt tup[])
1475*cfb853baSMatthew G. Knepley {
1476*cfb853baSMatthew G. Knepley   PetscInt i;
1477*cfb853baSMatthew G. Knepley 
1478*cfb853baSMatthew G. Knepley   PetscFunctionBegin;
1479*cfb853baSMatthew G. Knepley   for (i = 0; i < len; ++i) {
1480*cfb853baSMatthew G. Knepley     if (tup[i] < max[i] - 1) {
1481*cfb853baSMatthew G. Knepley       break;
1482*cfb853baSMatthew G. Knepley     } else {
1483*cfb853baSMatthew G. Knepley       tup[i] = 0;
1484*cfb853baSMatthew G. Knepley     }
1485*cfb853baSMatthew G. Knepley   }
1486*cfb853baSMatthew G. Knepley   if (i == len) tup[i - 1] = max[i - 1];
1487*cfb853baSMatthew G. Knepley   else ++tup[i];
1488*cfb853baSMatthew G. Knepley   PetscFunctionReturn(0);
1489*cfb853baSMatthew G. Knepley }
1490*cfb853baSMatthew G. Knepley 
1491*cfb853baSMatthew G. Knepley static PetscInt TupleToIndex_Private(PetscInt len, const PetscInt max[], const PetscInt tup[])
1492*cfb853baSMatthew G. Knepley {
1493*cfb853baSMatthew G. Knepley   PetscInt i, idx = tup[len - 1];
1494*cfb853baSMatthew G. Knepley 
1495*cfb853baSMatthew G. Knepley   for (i = len - 2; i >= 0; --i) {
1496*cfb853baSMatthew G. Knepley     idx *= max[i];
1497*cfb853baSMatthew G. Knepley     idx += tup[i];
1498*cfb853baSMatthew G. Knepley   }
1499*cfb853baSMatthew G. Knepley   return idx;
1500*cfb853baSMatthew G. Knepley }
1501*cfb853baSMatthew G. Knepley 
1502*cfb853baSMatthew G. Knepley static PetscErrorCode DestroyExtent_Private(void *extent)
1503*cfb853baSMatthew G. Knepley {
1504*cfb853baSMatthew G. Knepley   return PetscFree(extent);
1505*cfb853baSMatthew G. Knepley }
1506*cfb853baSMatthew G. Knepley 
1507*cfb853baSMatthew G. Knepley static PetscErrorCode DMPlexCreateHypercubicMesh_Internal(DM dm, PetscInt dim, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], const DMBoundaryType bd[])
1508*cfb853baSMatthew G. Knepley {
1509*cfb853baSMatthew G. Knepley   Vec          coordinates;
1510*cfb853baSMatthew G. Knepley   PetscSection coordSection;
1511*cfb853baSMatthew G. Knepley   DMLabel      cutLabel    = NULL;
1512*cfb853baSMatthew G. Knepley   PetscBool    cutMarker   = PETSC_FALSE;
1513*cfb853baSMatthew G. Knepley   PetscBool    periodic    = PETSC_FALSE;
1514*cfb853baSMatthew G. Knepley   PetscInt     numCells    = 1, c;
1515*cfb853baSMatthew G. Knepley   PetscInt     numVertices = 1, v;
1516*cfb853baSMatthew G. Knepley   PetscScalar *coords;
1517*cfb853baSMatthew G. Knepley   PetscInt    *vertices, *vert, *vtmp, *supp, cone[2];
1518*cfb853baSMatthew G. Knepley   PetscInt     d, e, cell = 0, coordSize;
1519*cfb853baSMatthew G. Knepley   PetscMPIInt  rank;
1520*cfb853baSMatthew G. Knepley 
1521*cfb853baSMatthew G. Knepley   PetscFunctionBegin;
1522*cfb853baSMatthew G. Knepley   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
1523*cfb853baSMatthew G. Knepley   PetscCall(DMSetDimension(dm, dim));
1524*cfb853baSMatthew G. Knepley   PetscCall(PetscCalloc4(dim, &vertices, dim, &vert, dim, &vtmp, 2 * dim, &supp));
1525*cfb853baSMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "marker"));
1526*cfb853baSMatthew G. Knepley   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options, ((PetscObject)dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL));
1527*cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) periodic = (periodic || bd[d] == DM_BOUNDARY_PERIODIC) ? PETSC_TRUE : PETSC_FALSE;
1528*cfb853baSMatthew G. Knepley   if (periodic && cutMarker) {
1529*cfb853baSMatthew G. Knepley     PetscCall(DMCreateLabel(dm, "periodic_cut"));
1530*cfb853baSMatthew G. Knepley     PetscCall(DMGetLabel(dm, "periodic_cut", &cutLabel));
1531*cfb853baSMatthew G. Knepley   }
1532*cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) PetscCheck(bd[d] == DM_BOUNDARY_PERIODIC, PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Hypercubic mesh must be periodic now");
1533*cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) {
1534*cfb853baSMatthew G. Knepley     vertices[d] = edges[d];
1535*cfb853baSMatthew G. Knepley     numVertices *= vertices[d];
1536*cfb853baSMatthew G. Knepley   }
1537*cfb853baSMatthew G. Knepley   numCells = numVertices * dim;
1538*cfb853baSMatthew G. Knepley   PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
1539*cfb853baSMatthew G. Knepley   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, 2));
1540*cfb853baSMatthew G. Knepley   for (v = numCells; v < numCells + numVertices; ++v) PetscCall(DMPlexSetSupportSize(dm, v, 2 * dim));
1541*cfb853baSMatthew G. Knepley   /* TODO Loop over boundary and reset support sizes */
1542*cfb853baSMatthew G. Knepley   PetscCall(DMSetUp(dm)); /* Allocate space for cones and supports */
1543*cfb853baSMatthew G. Knepley   /* Build cell cones and vertex supports */
1544*cfb853baSMatthew G. Knepley   PetscCall(DMCreateLabel(dm, "celltype"));
1545*cfb853baSMatthew G. Knepley   while (vert[dim - 1] < vertices[dim - 1]) {
1546*cfb853baSMatthew G. Knepley     const PetscInt vertex = TupleToIndex_Private(dim, vertices, vert) + numCells;
1547*cfb853baSMatthew G. Knepley     PetscInt       s      = 0;
1548*cfb853baSMatthew G. Knepley 
1549*cfb853baSMatthew G. Knepley     PetscPrintf(PETSC_COMM_SELF, "Vertex %" PetscInt_FMT ":", vertex);
1550*cfb853baSMatthew G. Knepley     for (d = 0; d < dim; ++d) PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, vert[d]);
1551*cfb853baSMatthew G. Knepley     PetscPrintf(PETSC_COMM_SELF, "\n");
1552*cfb853baSMatthew G. Knepley     PetscCall(DMPlexSetCellType(dm, vertex, DM_POLYTOPE_POINT));
1553*cfb853baSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
1554*cfb853baSMatthew G. Knepley       for (e = 0; e < dim; ++e) vtmp[e] = vert[e];
1555*cfb853baSMatthew G. Knepley       vtmp[d] = (vert[d] + 1) % vertices[d];
1556*cfb853baSMatthew G. Knepley       cone[0] = vertex;
1557*cfb853baSMatthew G. Knepley       cone[1] = TupleToIndex_Private(dim, vertices, vtmp) + numCells;
1558*cfb853baSMatthew G. Knepley       PetscPrintf(PETSC_COMM_SELF, "  Vertex %" PetscInt_FMT ":", cone[1]);
1559*cfb853baSMatthew G. Knepley       for (e = 0; e < dim; ++e) PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, vtmp[e]);
1560*cfb853baSMatthew G. Knepley       PetscPrintf(PETSC_COMM_SELF, "\n");
1561*cfb853baSMatthew G. Knepley       PetscCall(DMPlexSetCone(dm, cell, cone));
1562*cfb853baSMatthew G. Knepley       PetscCall(DMPlexSetCellType(dm, cell, DM_POLYTOPE_SEGMENT));
1563*cfb853baSMatthew G. Knepley       PetscPrintf(PETSC_COMM_SELF, "  Edge %" PetscInt_FMT " (%" PetscInt_FMT " %" PetscInt_FMT ")\n", cell, cone[0], cone[1]);
1564*cfb853baSMatthew G. Knepley       ++cell;
1565*cfb853baSMatthew G. Knepley     }
1566*cfb853baSMatthew G. Knepley     for (d = 0; d < dim; ++d) {
1567*cfb853baSMatthew G. Knepley       for (e = 0; e < dim; ++e) vtmp[e] = vert[e];
1568*cfb853baSMatthew G. Knepley       vtmp[d]   = (vert[d] + vertices[d] - 1) % vertices[d];
1569*cfb853baSMatthew G. Knepley       supp[s++] = TupleToIndex_Private(dim, vertices, vtmp) * dim + d;
1570*cfb853baSMatthew G. Knepley       supp[s++] = (vertex - numCells) * dim + d;
1571*cfb853baSMatthew G. Knepley       PetscCall(DMPlexSetSupport(dm, vertex, supp));
1572*cfb853baSMatthew G. Knepley     }
1573*cfb853baSMatthew G. Knepley     PetscCall(DMPlexTensorPointLexicographic_Private(dim, vertices, vert));
1574*cfb853baSMatthew G. Knepley   }
1575*cfb853baSMatthew G. Knepley   PetscCall(DMPlexStratify(dm));
1576*cfb853baSMatthew G. Knepley   /* Build coordinates */
1577*cfb853baSMatthew G. Knepley   PetscCall(DMGetCoordinateSection(dm, &coordSection));
1578*cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetNumFields(coordSection, 1));
1579*cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
1580*cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices));
1581*cfb853baSMatthew G. Knepley   for (v = numCells; v < numCells + numVertices; ++v) {
1582*cfb853baSMatthew G. Knepley     PetscCall(PetscSectionSetDof(coordSection, v, dim));
1583*cfb853baSMatthew G. Knepley     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
1584*cfb853baSMatthew G. Knepley   }
1585*cfb853baSMatthew G. Knepley   PetscCall(PetscSectionSetUp(coordSection));
1586*cfb853baSMatthew G. Knepley   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
1587*cfb853baSMatthew G. Knepley   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
1588*cfb853baSMatthew G. Knepley   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
1589*cfb853baSMatthew G. Knepley   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
1590*cfb853baSMatthew G. Knepley   PetscCall(VecSetBlockSize(coordinates, dim));
1591*cfb853baSMatthew G. Knepley   PetscCall(VecSetType(coordinates, VECSTANDARD));
1592*cfb853baSMatthew G. Knepley   PetscCall(VecGetArray(coordinates, &coords));
1593*cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) vert[d] = 0;
1594*cfb853baSMatthew G. Knepley   while (vert[dim - 1] < vertices[dim - 1]) {
1595*cfb853baSMatthew G. Knepley     const PetscInt vertex = TupleToIndex_Private(dim, vertices, vert);
1596*cfb853baSMatthew G. Knepley 
1597*cfb853baSMatthew G. Knepley     for (d = 0; d < dim; ++d) coords[vertex * dim + d] = lower[d] + ((upper[d] - lower[d]) / vertices[d]) * vert[d];
1598*cfb853baSMatthew G. Knepley     PetscPrintf(PETSC_COMM_SELF, "Vertex %" PetscInt_FMT ":", vertex);
1599*cfb853baSMatthew G. Knepley     for (d = 0; d < dim; ++d) PetscPrintf(PETSC_COMM_SELF, " %" PetscInt_FMT, vert[d]);
1600*cfb853baSMatthew G. Knepley     for (d = 0; d < dim; ++d) PetscPrintf(PETSC_COMM_SELF, " %g", (double)PetscRealPart(coords[vertex * dim + d]));
1601*cfb853baSMatthew G. Knepley     PetscPrintf(PETSC_COMM_SELF, "\n");
1602*cfb853baSMatthew G. Knepley     PetscCall(DMPlexTensorPointLexicographic_Private(dim, vertices, vert));
1603*cfb853baSMatthew G. Knepley   }
1604*cfb853baSMatthew G. Knepley   PetscCall(VecRestoreArray(coordinates, &coords));
1605*cfb853baSMatthew G. Knepley   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
1606*cfb853baSMatthew G. Knepley   PetscCall(VecDestroy(&coordinates));
1607*cfb853baSMatthew G. Knepley   PetscCall(PetscFree4(vertices, vert, vtmp, supp));
1608*cfb853baSMatthew G. Knepley   //PetscCall(DMSetPeriodicity(dm, NULL, lower, upper));
1609*cfb853baSMatthew G. Knepley   // Attach the extent
1610*cfb853baSMatthew G. Knepley   {
1611*cfb853baSMatthew G. Knepley     PetscContainer c;
1612*cfb853baSMatthew G. Knepley     PetscInt      *extent;
1613*cfb853baSMatthew G. Knepley 
1614*cfb853baSMatthew G. Knepley     PetscCall(PetscMalloc1(dim, &extent));
1615*cfb853baSMatthew G. Knepley     for (PetscInt d = 0; d < dim; ++d) extent[d] = edges[d];
1616*cfb853baSMatthew G. Knepley     PetscCall(PetscContainerCreate(PETSC_COMM_SELF, &c));
1617*cfb853baSMatthew G. Knepley     PetscCall(PetscContainerSetUserDestroy(c, DestroyExtent_Private));
1618*cfb853baSMatthew G. Knepley     PetscCall(PetscContainerSetPointer(c, extent));
1619*cfb853baSMatthew G. Knepley     PetscCall(PetscObjectCompose((PetscObject)dm, "_extent", (PetscObject)c));
1620*cfb853baSMatthew G. Knepley     PetscCall(PetscContainerDestroy(&c));
1621*cfb853baSMatthew G. Knepley   }
1622*cfb853baSMatthew G. Knepley   PetscFunctionReturn(0);
1623*cfb853baSMatthew G. Knepley }
1624*cfb853baSMatthew G. Knepley 
1625*cfb853baSMatthew G. Knepley /*@C
1626*cfb853baSMatthew G. Knepley   DMPlexCreateHypercubicMesh - Creates a peridoic mesh on the tensor product of unit intervals using only vertices and edges.
1627*cfb853baSMatthew G. Knepley 
1628*cfb853baSMatthew G. Knepley   Collective
1629*cfb853baSMatthew G. Knepley 
1630*cfb853baSMatthew G. Knepley   Input Parameters:
1631*cfb853baSMatthew G. Knepley + comm        - The communicator for the DM object
1632*cfb853baSMatthew G. Knepley . dim         - The spatial dimension
1633*cfb853baSMatthew G. Knepley . edges       - Number of edges per dimension, or NULL for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
1634*cfb853baSMatthew G. Knepley . lower       - The lower left corner, or NULL for (0, 0, 0)
1635*cfb853baSMatthew G. Knepley - upper       - The upper right corner, or NULL for (1, 1, 1)
1636*cfb853baSMatthew G. Knepley 
1637*cfb853baSMatthew G. Knepley   Output Parameter:
1638*cfb853baSMatthew G. Knepley . dm  - The DM object
1639*cfb853baSMatthew G. Knepley 
1640*cfb853baSMatthew G. Knepley   Note: If you want to customize this mesh using options, you just need to
1641*cfb853baSMatthew G. Knepley $  DMCreate(comm, &dm);
1642*cfb853baSMatthew G. Knepley $  DMSetType(dm, DMPLEX);
1643*cfb853baSMatthew G. Knepley $  DMSetFromOptions(dm);
1644*cfb853baSMatthew G. Knepley and use the options on the DMSetFromOptions() page.
1645*cfb853baSMatthew G. Knepley 
1646*cfb853baSMatthew G. Knepley   The vertices are numbered is lexicographic order, and the dim edges exiting a vertex in the positive orthant are number consecutively,
1647*cfb853baSMatthew G. Knepley $ 18--0-19--2-20--4-18
1648*cfb853baSMatthew G. Knepley $  |     |     |     |
1649*cfb853baSMatthew G. Knepley $ 13    15    17    13
1650*cfb853baSMatthew G. Knepley $  |     |     |     |
1651*cfb853baSMatthew G. Knepley $ 24-12-25-14-26-16-24
1652*cfb853baSMatthew G. Knepley $  |     |     |     |
1653*cfb853baSMatthew G. Knepley $  7     9    11     7
1654*cfb853baSMatthew G. Knepley $  |     |     |     |
1655*cfb853baSMatthew G. Knepley $ 21--6-22--8-23-10-21
1656*cfb853baSMatthew G. Knepley $  |     |     |     |
1657*cfb853baSMatthew G. Knepley $  1     3     5     1
1658*cfb853baSMatthew G. Knepley $  |     |     |     |
1659*cfb853baSMatthew G. Knepley $ 18--0-19--2-20--4-18
1660*cfb853baSMatthew G. Knepley 
1661*cfb853baSMatthew G. Knepley   Level: beginner
1662*cfb853baSMatthew G. Knepley 
1663*cfb853baSMatthew G. Knepley .seealso: DMSetFromOptions(), DMPlexCreateFromFile(), DMPlexCreateHexCylinderMesh(), DMSetType(), DMCreate()
1664*cfb853baSMatthew G. Knepley @*/
1665*cfb853baSMatthew G. Knepley PetscErrorCode DMPlexCreateHypercubicMesh(MPI_Comm comm, PetscInt dim, const PetscInt edges[], const PetscReal lower[], const PetscReal upper[], DM *dm)
1666*cfb853baSMatthew G. Knepley {
1667*cfb853baSMatthew G. Knepley   PetscInt       *edg;
1668*cfb853baSMatthew G. Knepley   PetscReal      *low, *upp;
1669*cfb853baSMatthew G. Knepley   DMBoundaryType *bdt;
1670*cfb853baSMatthew G. Knepley   PetscInt        d;
1671*cfb853baSMatthew G. Knepley 
1672*cfb853baSMatthew G. Knepley   PetscFunctionBegin;
1673*cfb853baSMatthew G. Knepley   PetscCall(DMCreate(comm, dm));
1674*cfb853baSMatthew G. Knepley   PetscCall(DMSetType(*dm, DMPLEX));
1675*cfb853baSMatthew G. Knepley   PetscCall(PetscMalloc4(dim, &edg, dim, &low, dim, &upp, dim, &bdt));
1676*cfb853baSMatthew G. Knepley   for (d = 0; d < dim; ++d) {
1677*cfb853baSMatthew G. Knepley     edg[d] = edges ? edges[d] : 1;
1678*cfb853baSMatthew G. Knepley     low[d] = lower ? lower[d] : 0.;
1679*cfb853baSMatthew G. Knepley     upp[d] = upper ? upper[d] : 1.;
1680*cfb853baSMatthew G. Knepley     bdt[d] = DM_BOUNDARY_PERIODIC;
1681*cfb853baSMatthew G. Knepley   }
1682*cfb853baSMatthew G. Knepley   PetscCall(DMPlexCreateHypercubicMesh_Internal(*dm, dim, low, upp, edg, bdt));
1683*cfb853baSMatthew G. Knepley   PetscCall(PetscFree4(edg, low, upp, bdt));
1684*cfb853baSMatthew G. Knepley   PetscFunctionReturn(0);
1685*cfb853baSMatthew G. Knepley }
1686*cfb853baSMatthew G. Knepley 
1687a9074c1eSMatthew G. Knepley /*@C
1688a1cb98faSBarry Smith   DMPlexSetOptionsPrefix - Sets the prefix used for searching for all `DM` options in the database.
1689a9074c1eSMatthew G. Knepley 
1690d083f849SBarry Smith   Logically Collective on dm
1691a9074c1eSMatthew G. Knepley 
1692a9074c1eSMatthew G. Knepley   Input Parameters:
1693a9074c1eSMatthew G. Knepley + dm - the DM context
1694a9074c1eSMatthew G. Knepley - prefix - the prefix to prepend to all option names
1695a9074c1eSMatthew G. Knepley 
1696a1cb98faSBarry Smith   Level: advanced
1697a1cb98faSBarry Smith 
1698a1cb98faSBarry Smith   Note:
1699a9074c1eSMatthew G. Knepley   A hyphen (-) must NOT be given at the beginning of the prefix name.
1700a9074c1eSMatthew G. Knepley   The first character of all runtime options is AUTOMATICALLY the hyphen.
1701a9074c1eSMatthew G. Knepley 
1702a1cb98faSBarry Smith .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `SNESSetFromOptions()`
1703a9074c1eSMatthew G. Knepley @*/
1704d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexSetOptionsPrefix(DM dm, const char prefix[])
1705d71ae5a4SJacob Faibussowitsch {
1706a9074c1eSMatthew G. Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
1707a9074c1eSMatthew G. Knepley 
1708a9074c1eSMatthew G. Knepley   PetscFunctionBegin;
1709a9074c1eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17109566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)dm, prefix));
17119566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)mesh->partitioner, prefix));
1712a9074c1eSMatthew G. Knepley   PetscFunctionReturn(0);
1713a9074c1eSMatthew G. Knepley }
1714a9074c1eSMatthew G. Knepley 
17159318fe57SMatthew G. Knepley /* Remap geometry to cylinder
171661a622f3SMatthew G. Knepley    TODO: This only works for a single refinement, then it is broken
171761a622f3SMatthew G. Knepley 
17189318fe57SMatthew G. Knepley      Interior square: Linear interpolation is correct
17199318fe57SMatthew G. Knepley      The other cells all have vertices on rays from the origin. We want to uniformly expand the spacing
17209318fe57SMatthew G. Knepley      such that the last vertex is on the unit circle. So the closest and farthest vertices are at distance
17210510c589SMatthew G. Knepley 
17229318fe57SMatthew G. Knepley        phi     = arctan(y/x)
17239318fe57SMatthew G. Knepley        d_close = sqrt(1/8 + 1/4 sin^2(phi))
17249318fe57SMatthew G. Knepley        d_far   = sqrt(1/2 + sin^2(phi))
17250510c589SMatthew G. Knepley 
17269318fe57SMatthew G. Knepley      so we remap them using
17270510c589SMatthew G. Knepley 
17289318fe57SMatthew G. Knepley        x_new = x_close + (x - x_close) (1 - d_close) / (d_far - d_close)
17299318fe57SMatthew G. Knepley        y_new = y_close + (y - y_close) (1 - d_close) / (d_far - d_close)
17300510c589SMatthew G. Knepley 
17319318fe57SMatthew G. Knepley      If pi/4 < phi < 3pi/4 or -3pi/4 < phi < -pi/4, then we switch x and y.
17329318fe57SMatthew G. Knepley */
1733d71ae5a4SJacob 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[])
1734d71ae5a4SJacob Faibussowitsch {
17359318fe57SMatthew G. Knepley   const PetscReal dis = 1.0 / PetscSqrtReal(2.0);
17369318fe57SMatthew G. Knepley   const PetscReal ds2 = 0.5 * dis;
173722cc497dSMatthew G. Knepley 
17389318fe57SMatthew G. Knepley   if ((PetscAbsScalar(u[0]) <= ds2) && (PetscAbsScalar(u[1]) <= ds2)) {
17399318fe57SMatthew G. Knepley     f0[0] = u[0];
17409318fe57SMatthew G. Knepley     f0[1] = u[1];
17419318fe57SMatthew G. Knepley   } else {
17429318fe57SMatthew G. Knepley     PetscReal phi, sinp, cosp, dc, df, x, y, xc, yc;
17430510c589SMatthew G. Knepley 
17449318fe57SMatthew G. Knepley     x    = PetscRealPart(u[0]);
17459318fe57SMatthew G. Knepley     y    = PetscRealPart(u[1]);
17469318fe57SMatthew G. Knepley     phi  = PetscAtan2Real(y, x);
17479318fe57SMatthew G. Knepley     sinp = PetscSinReal(phi);
17489318fe57SMatthew G. Knepley     cosp = PetscCosReal(phi);
17499318fe57SMatthew G. Knepley     if ((PetscAbsReal(phi) > PETSC_PI / 4.0) && (PetscAbsReal(phi) < 3.0 * PETSC_PI / 4.0)) {
17509318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2 / sinp);
17519318fe57SMatthew G. Knepley       df = PetscAbsReal(dis / sinp);
17529318fe57SMatthew G. Knepley       xc = ds2 * x / PetscAbsReal(y);
17539318fe57SMatthew G. Knepley       yc = ds2 * PetscSignReal(y);
17549318fe57SMatthew G. Knepley     } else {
17559318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2 / cosp);
17569318fe57SMatthew G. Knepley       df = PetscAbsReal(dis / cosp);
17579318fe57SMatthew G. Knepley       xc = ds2 * PetscSignReal(x);
17589318fe57SMatthew G. Knepley       yc = ds2 * y / PetscAbsReal(x);
17599318fe57SMatthew G. Knepley     }
17609318fe57SMatthew G. Knepley     f0[0] = xc + (u[0] - xc) * (1.0 - dc) / (df - dc);
17619318fe57SMatthew G. Knepley     f0[1] = yc + (u[1] - yc) * (1.0 - dc) / (df - dc);
17629318fe57SMatthew G. Knepley   }
17639318fe57SMatthew G. Knepley   f0[2] = u[2];
17649318fe57SMatthew G. Knepley }
17650510c589SMatthew G. Knepley 
1766d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateHexCylinderMesh_Internal(DM dm, DMBoundaryType periodicZ)
1767d71ae5a4SJacob Faibussowitsch {
17680510c589SMatthew G. Knepley   const PetscInt dim = 3;
17699318fe57SMatthew G. Knepley   PetscInt       numCells, numVertices;
1770d8c47e87SMatthew G. Knepley   PetscMPIInt    rank;
17710510c589SMatthew G. Knepley 
17720510c589SMatthew G. Knepley   PetscFunctionBegin;
17739566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
17749566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
17750510c589SMatthew G. Knepley   /* Create topology */
17760510c589SMatthew G. Knepley   {
17770510c589SMatthew G. Knepley     PetscInt cone[8], c;
17780510c589SMatthew G. Knepley 
1779dd400576SPatrick Sanan     numCells    = rank == 0 ? 5 : 0;
1780dd400576SPatrick Sanan     numVertices = rank == 0 ? 16 : 0;
1781006a8963SMatthew G. Knepley     if (periodicZ == DM_BOUNDARY_PERIODIC) {
1782ae8bcbbbSMatthew G. Knepley       numCells *= 3;
1783dd400576SPatrick Sanan       numVertices = rank == 0 ? 24 : 0;
1784006a8963SMatthew G. Knepley     }
17859566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
17869566063dSJacob Faibussowitsch     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 8));
17879566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm));
1788dd400576SPatrick Sanan     if (rank == 0) {
1789006a8963SMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
17909371c9d4SSatish Balay         cone[0] = 15;
17919371c9d4SSatish Balay         cone[1] = 18;
17929371c9d4SSatish Balay         cone[2] = 17;
17939371c9d4SSatish Balay         cone[3] = 16;
17949371c9d4SSatish Balay         cone[4] = 31;
17959371c9d4SSatish Balay         cone[5] = 32;
17969371c9d4SSatish Balay         cone[6] = 33;
17979371c9d4SSatish Balay         cone[7] = 34;
17989566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
17999371c9d4SSatish Balay         cone[0] = 16;
18009371c9d4SSatish Balay         cone[1] = 17;
18019371c9d4SSatish Balay         cone[2] = 24;
18029371c9d4SSatish Balay         cone[3] = 23;
18039371c9d4SSatish Balay         cone[4] = 32;
18049371c9d4SSatish Balay         cone[5] = 36;
18059371c9d4SSatish Balay         cone[6] = 37;
18069371c9d4SSatish Balay         cone[7] = 33; /* 22 25 26 21 */
18079566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
18089371c9d4SSatish Balay         cone[0] = 18;
18099371c9d4SSatish Balay         cone[1] = 27;
18109371c9d4SSatish Balay         cone[2] = 24;
18119371c9d4SSatish Balay         cone[3] = 17;
18129371c9d4SSatish Balay         cone[4] = 34;
18139371c9d4SSatish Balay         cone[5] = 33;
18149371c9d4SSatish Balay         cone[6] = 37;
18159371c9d4SSatish Balay         cone[7] = 38;
18169566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
18179371c9d4SSatish Balay         cone[0] = 29;
18189371c9d4SSatish Balay         cone[1] = 27;
18199371c9d4SSatish Balay         cone[2] = 18;
18209371c9d4SSatish Balay         cone[3] = 15;
18219371c9d4SSatish Balay         cone[4] = 35;
18229371c9d4SSatish Balay         cone[5] = 31;
18239371c9d4SSatish Balay         cone[6] = 34;
18249371c9d4SSatish Balay         cone[7] = 38;
18259566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
18269371c9d4SSatish Balay         cone[0] = 29;
18279371c9d4SSatish Balay         cone[1] = 15;
18289371c9d4SSatish Balay         cone[2] = 16;
18299371c9d4SSatish Balay         cone[3] = 23;
18309371c9d4SSatish Balay         cone[4] = 35;
18319371c9d4SSatish Balay         cone[5] = 36;
18329371c9d4SSatish Balay         cone[6] = 32;
18339371c9d4SSatish Balay         cone[7] = 31;
18349566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
1835006a8963SMatthew G. Knepley 
18369371c9d4SSatish Balay         cone[0] = 31;
18379371c9d4SSatish Balay         cone[1] = 34;
18389371c9d4SSatish Balay         cone[2] = 33;
18399371c9d4SSatish Balay         cone[3] = 32;
18409371c9d4SSatish Balay         cone[4] = 19;
18419371c9d4SSatish Balay         cone[5] = 22;
18429371c9d4SSatish Balay         cone[6] = 21;
18439371c9d4SSatish Balay         cone[7] = 20;
18449566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 5, cone));
18459371c9d4SSatish Balay         cone[0] = 32;
18469371c9d4SSatish Balay         cone[1] = 33;
18479371c9d4SSatish Balay         cone[2] = 37;
18489371c9d4SSatish Balay         cone[3] = 36;
18499371c9d4SSatish Balay         cone[4] = 22;
18509371c9d4SSatish Balay         cone[5] = 25;
18519371c9d4SSatish Balay         cone[6] = 26;
18529371c9d4SSatish Balay         cone[7] = 21;
18539566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 6, cone));
18549371c9d4SSatish Balay         cone[0] = 34;
18559371c9d4SSatish Balay         cone[1] = 38;
18569371c9d4SSatish Balay         cone[2] = 37;
18579371c9d4SSatish Balay         cone[3] = 33;
18589371c9d4SSatish Balay         cone[4] = 20;
18599371c9d4SSatish Balay         cone[5] = 21;
18609371c9d4SSatish Balay         cone[6] = 26;
18619371c9d4SSatish Balay         cone[7] = 28;
18629566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 7, cone));
18639371c9d4SSatish Balay         cone[0] = 35;
18649371c9d4SSatish Balay         cone[1] = 38;
18659371c9d4SSatish Balay         cone[2] = 34;
18669371c9d4SSatish Balay         cone[3] = 31;
18679371c9d4SSatish Balay         cone[4] = 30;
18689371c9d4SSatish Balay         cone[5] = 19;
18699371c9d4SSatish Balay         cone[6] = 20;
18709371c9d4SSatish Balay         cone[7] = 28;
18719566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 8, cone));
18729371c9d4SSatish Balay         cone[0] = 35;
18739371c9d4SSatish Balay         cone[1] = 31;
18749371c9d4SSatish Balay         cone[2] = 32;
18759371c9d4SSatish Balay         cone[3] = 36;
18769371c9d4SSatish Balay         cone[4] = 30;
18779371c9d4SSatish Balay         cone[5] = 25;
18789371c9d4SSatish Balay         cone[6] = 22;
18799371c9d4SSatish Balay         cone[7] = 19;
18809566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 9, cone));
1881ae8bcbbbSMatthew G. Knepley 
18829371c9d4SSatish Balay         cone[0] = 19;
18839371c9d4SSatish Balay         cone[1] = 20;
18849371c9d4SSatish Balay         cone[2] = 21;
18859371c9d4SSatish Balay         cone[3] = 22;
18869371c9d4SSatish Balay         cone[4] = 15;
18879371c9d4SSatish Balay         cone[5] = 16;
18889371c9d4SSatish Balay         cone[6] = 17;
18899371c9d4SSatish Balay         cone[7] = 18;
18909566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 10, cone));
18919371c9d4SSatish Balay         cone[0] = 22;
18929371c9d4SSatish Balay         cone[1] = 21;
18939371c9d4SSatish Balay         cone[2] = 26;
18949371c9d4SSatish Balay         cone[3] = 25;
18959371c9d4SSatish Balay         cone[4] = 16;
18969371c9d4SSatish Balay         cone[5] = 23;
18979371c9d4SSatish Balay         cone[6] = 24;
18989371c9d4SSatish Balay         cone[7] = 17;
18999566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 11, cone));
19009371c9d4SSatish Balay         cone[0] = 20;
19019371c9d4SSatish Balay         cone[1] = 28;
19029371c9d4SSatish Balay         cone[2] = 26;
19039371c9d4SSatish Balay         cone[3] = 21;
19049371c9d4SSatish Balay         cone[4] = 18;
19059371c9d4SSatish Balay         cone[5] = 17;
19069371c9d4SSatish Balay         cone[6] = 24;
19079371c9d4SSatish Balay         cone[7] = 27;
19089566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 12, cone));
19099371c9d4SSatish Balay         cone[0] = 30;
19109371c9d4SSatish Balay         cone[1] = 28;
19119371c9d4SSatish Balay         cone[2] = 20;
19129371c9d4SSatish Balay         cone[3] = 19;
19139371c9d4SSatish Balay         cone[4] = 29;
19149371c9d4SSatish Balay         cone[5] = 15;
19159371c9d4SSatish Balay         cone[6] = 18;
19169371c9d4SSatish Balay         cone[7] = 27;
19179566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 13, cone));
19189371c9d4SSatish Balay         cone[0] = 30;
19199371c9d4SSatish Balay         cone[1] = 19;
19209371c9d4SSatish Balay         cone[2] = 22;
19219371c9d4SSatish Balay         cone[3] = 25;
19229371c9d4SSatish Balay         cone[4] = 29;
19239371c9d4SSatish Balay         cone[5] = 23;
19249371c9d4SSatish Balay         cone[6] = 16;
19259371c9d4SSatish Balay         cone[7] = 15;
19269566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 14, cone));
1927006a8963SMatthew G. Knepley       } else {
19289371c9d4SSatish Balay         cone[0] = 5;
19299371c9d4SSatish Balay         cone[1] = 8;
19309371c9d4SSatish Balay         cone[2] = 7;
19319371c9d4SSatish Balay         cone[3] = 6;
19329371c9d4SSatish Balay         cone[4] = 9;
19339371c9d4SSatish Balay         cone[5] = 12;
19349371c9d4SSatish Balay         cone[6] = 11;
19359371c9d4SSatish Balay         cone[7] = 10;
19369566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
19379371c9d4SSatish Balay         cone[0] = 6;
19389371c9d4SSatish Balay         cone[1] = 7;
19399371c9d4SSatish Balay         cone[2] = 14;
19409371c9d4SSatish Balay         cone[3] = 13;
19419371c9d4SSatish Balay         cone[4] = 12;
19429371c9d4SSatish Balay         cone[5] = 15;
19439371c9d4SSatish Balay         cone[6] = 16;
19449371c9d4SSatish Balay         cone[7] = 11;
19459566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
19469371c9d4SSatish Balay         cone[0] = 8;
19479371c9d4SSatish Balay         cone[1] = 17;
19489371c9d4SSatish Balay         cone[2] = 14;
19499371c9d4SSatish Balay         cone[3] = 7;
19509371c9d4SSatish Balay         cone[4] = 10;
19519371c9d4SSatish Balay         cone[5] = 11;
19529371c9d4SSatish Balay         cone[6] = 16;
19539371c9d4SSatish Balay         cone[7] = 18;
19549566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
19559371c9d4SSatish Balay         cone[0] = 19;
19569371c9d4SSatish Balay         cone[1] = 17;
19579371c9d4SSatish Balay         cone[2] = 8;
19589371c9d4SSatish Balay         cone[3] = 5;
19599371c9d4SSatish Balay         cone[4] = 20;
19609371c9d4SSatish Balay         cone[5] = 9;
19619371c9d4SSatish Balay         cone[6] = 10;
19629371c9d4SSatish Balay         cone[7] = 18;
19639566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
19649371c9d4SSatish Balay         cone[0] = 19;
19659371c9d4SSatish Balay         cone[1] = 5;
19669371c9d4SSatish Balay         cone[2] = 6;
19679371c9d4SSatish Balay         cone[3] = 13;
19689371c9d4SSatish Balay         cone[4] = 20;
19699371c9d4SSatish Balay         cone[5] = 15;
19709371c9d4SSatish Balay         cone[6] = 12;
19719371c9d4SSatish Balay         cone[7] = 9;
19729566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
1973006a8963SMatthew G. Knepley       }
1974d8c47e87SMatthew G. Knepley     }
19759566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
19769566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
19770510c589SMatthew G. Knepley   }
1978dbc1dc17SMatthew G. Knepley   /* Create cube geometry */
19790510c589SMatthew G. Knepley   {
19800510c589SMatthew G. Knepley     Vec             coordinates;
19810510c589SMatthew G. Knepley     PetscSection    coordSection;
19820510c589SMatthew G. Knepley     PetscScalar    *coords;
19830510c589SMatthew G. Knepley     PetscInt        coordSize, v;
19840510c589SMatthew G. Knepley     const PetscReal dis = 1.0 / PetscSqrtReal(2.0);
19850510c589SMatthew G. Knepley     const PetscReal ds2 = dis / 2.0;
19860510c589SMatthew G. Knepley 
19870510c589SMatthew G. Knepley     /* Build coordinates */
19889566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
19899566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
19909566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
19919566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices));
19920510c589SMatthew G. Knepley     for (v = numCells; v < numCells + numVertices; ++v) {
19939566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
19949566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
19950510c589SMatthew G. Knepley     }
19969566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
19979566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
19989566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
19999566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
20009566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
20019566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
20029566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates, VECSTANDARD));
20039566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
2004dd400576SPatrick Sanan     if (rank == 0) {
20059371c9d4SSatish Balay       coords[0 * dim + 0]  = -ds2;
20069371c9d4SSatish Balay       coords[0 * dim + 1]  = -ds2;
20079371c9d4SSatish Balay       coords[0 * dim + 2]  = 0.0;
20089371c9d4SSatish Balay       coords[1 * dim + 0]  = ds2;
20099371c9d4SSatish Balay       coords[1 * dim + 1]  = -ds2;
20109371c9d4SSatish Balay       coords[1 * dim + 2]  = 0.0;
20119371c9d4SSatish Balay       coords[2 * dim + 0]  = ds2;
20129371c9d4SSatish Balay       coords[2 * dim + 1]  = ds2;
20139371c9d4SSatish Balay       coords[2 * dim + 2]  = 0.0;
20149371c9d4SSatish Balay       coords[3 * dim + 0]  = -ds2;
20159371c9d4SSatish Balay       coords[3 * dim + 1]  = ds2;
20169371c9d4SSatish Balay       coords[3 * dim + 2]  = 0.0;
20179371c9d4SSatish Balay       coords[4 * dim + 0]  = -ds2;
20189371c9d4SSatish Balay       coords[4 * dim + 1]  = -ds2;
20199371c9d4SSatish Balay       coords[4 * dim + 2]  = 1.0;
20209371c9d4SSatish Balay       coords[5 * dim + 0]  = -ds2;
20219371c9d4SSatish Balay       coords[5 * dim + 1]  = ds2;
20229371c9d4SSatish Balay       coords[5 * dim + 2]  = 1.0;
20239371c9d4SSatish Balay       coords[6 * dim + 0]  = ds2;
20249371c9d4SSatish Balay       coords[6 * dim + 1]  = ds2;
20259371c9d4SSatish Balay       coords[6 * dim + 2]  = 1.0;
20269371c9d4SSatish Balay       coords[7 * dim + 0]  = ds2;
20279371c9d4SSatish Balay       coords[7 * dim + 1]  = -ds2;
20289371c9d4SSatish Balay       coords[7 * dim + 2]  = 1.0;
20299371c9d4SSatish Balay       coords[8 * dim + 0]  = dis;
20309371c9d4SSatish Balay       coords[8 * dim + 1]  = -dis;
20319371c9d4SSatish Balay       coords[8 * dim + 2]  = 0.0;
20329371c9d4SSatish Balay       coords[9 * dim + 0]  = dis;
20339371c9d4SSatish Balay       coords[9 * dim + 1]  = dis;
20349371c9d4SSatish Balay       coords[9 * dim + 2]  = 0.0;
20359371c9d4SSatish Balay       coords[10 * dim + 0] = dis;
20369371c9d4SSatish Balay       coords[10 * dim + 1] = -dis;
20379371c9d4SSatish Balay       coords[10 * dim + 2] = 1.0;
20389371c9d4SSatish Balay       coords[11 * dim + 0] = dis;
20399371c9d4SSatish Balay       coords[11 * dim + 1] = dis;
20409371c9d4SSatish Balay       coords[11 * dim + 2] = 1.0;
20419371c9d4SSatish Balay       coords[12 * dim + 0] = -dis;
20429371c9d4SSatish Balay       coords[12 * dim + 1] = dis;
20439371c9d4SSatish Balay       coords[12 * dim + 2] = 0.0;
20449371c9d4SSatish Balay       coords[13 * dim + 0] = -dis;
20459371c9d4SSatish Balay       coords[13 * dim + 1] = dis;
20469371c9d4SSatish Balay       coords[13 * dim + 2] = 1.0;
20479371c9d4SSatish Balay       coords[14 * dim + 0] = -dis;
20489371c9d4SSatish Balay       coords[14 * dim + 1] = -dis;
20499371c9d4SSatish Balay       coords[14 * dim + 2] = 0.0;
20509371c9d4SSatish Balay       coords[15 * dim + 0] = -dis;
20519371c9d4SSatish Balay       coords[15 * dim + 1] = -dis;
20529371c9d4SSatish Balay       coords[15 * dim + 2] = 1.0;
2053ae8bcbbbSMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
20549371c9d4SSatish Balay         /* 15 31 19 */ coords[16 * dim + 0] = -ds2;
20559371c9d4SSatish Balay         coords[16 * dim + 1]                = -ds2;
20569371c9d4SSatish Balay         coords[16 * dim + 2]                = 0.5;
20579371c9d4SSatish Balay         /* 16 32 22 */ coords[17 * dim + 0] = ds2;
20589371c9d4SSatish Balay         coords[17 * dim + 1]                = -ds2;
20599371c9d4SSatish Balay         coords[17 * dim + 2]                = 0.5;
20609371c9d4SSatish Balay         /* 17 33 21 */ coords[18 * dim + 0] = ds2;
20619371c9d4SSatish Balay         coords[18 * dim + 1]                = ds2;
20629371c9d4SSatish Balay         coords[18 * dim + 2]                = 0.5;
20639371c9d4SSatish Balay         /* 18 34 20 */ coords[19 * dim + 0] = -ds2;
20649371c9d4SSatish Balay         coords[19 * dim + 1]                = ds2;
20659371c9d4SSatish Balay         coords[19 * dim + 2]                = 0.5;
20669371c9d4SSatish Balay         /* 29 35 30 */ coords[20 * dim + 0] = -dis;
20679371c9d4SSatish Balay         coords[20 * dim + 1]                = -dis;
20689371c9d4SSatish Balay         coords[20 * dim + 2]                = 0.5;
20699371c9d4SSatish Balay         /* 23 36 25 */ coords[21 * dim + 0] = dis;
20709371c9d4SSatish Balay         coords[21 * dim + 1]                = -dis;
20719371c9d4SSatish Balay         coords[21 * dim + 2]                = 0.5;
20729371c9d4SSatish Balay         /* 24 37 26 */ coords[22 * dim + 0] = dis;
20739371c9d4SSatish Balay         coords[22 * dim + 1]                = dis;
20749371c9d4SSatish Balay         coords[22 * dim + 2]                = 0.5;
20759371c9d4SSatish Balay         /* 27 38 28 */ coords[23 * dim + 0] = -dis;
20769371c9d4SSatish Balay         coords[23 * dim + 1]                = dis;
20779371c9d4SSatish Balay         coords[23 * dim + 2]                = 0.5;
2078ae8bcbbbSMatthew G. Knepley       }
2079d8c47e87SMatthew G. Knepley     }
20809566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
20819566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
20829566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
20830510c589SMatthew G. Knepley   }
2084006a8963SMatthew G. Knepley   /* Create periodicity */
2085006a8963SMatthew G. Knepley   if (periodicZ == DM_BOUNDARY_PERIODIC || periodicZ == DM_BOUNDARY_TWIST) {
20866858538eSMatthew G. Knepley     PetscReal L[3]       = {-1., -1., 0.};
20876858538eSMatthew G. Knepley     PetscReal maxCell[3] = {-1., -1., 0.};
2088006a8963SMatthew G. Knepley     PetscReal lower[3]   = {0.0, 0.0, 0.0};
2089ae8bcbbbSMatthew G. Knepley     PetscReal upper[3]   = {1.0, 1.0, 1.5};
20906858538eSMatthew G. Knepley     PetscInt  numZCells  = 3;
2091006a8963SMatthew G. Knepley 
20926858538eSMatthew G. Knepley     L[2]       = upper[2] - lower[2];
20936858538eSMatthew G. Knepley     maxCell[2] = 1.1 * (L[2] / numZCells);
20944fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, maxCell, lower, L));
2095006a8963SMatthew G. Knepley   }
2096dbc1dc17SMatthew G. Knepley   {
20979318fe57SMatthew G. Knepley     DM          cdm;
20989318fe57SMatthew G. Knepley     PetscDS     cds;
20999318fe57SMatthew G. Knepley     PetscScalar c[2] = {1.0, 1.0};
2100dbc1dc17SMatthew G. Knepley 
21019566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCoordinateSpace(dm, 1, snapToCylinder));
21029566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
21039566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
21049566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, 2, c));
2105dbc1dc17SMatthew G. Knepley   }
21069318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
21079566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolateInPlace_Internal(dm));
21080510c589SMatthew G. Knepley   PetscFunctionReturn(0);
21090510c589SMatthew G. Knepley }
21100510c589SMatthew G. Knepley 
211124119c2aSMatthew G. Knepley /*@
21129318fe57SMatthew G. Knepley   DMPlexCreateHexCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using hexahedra.
211324119c2aSMatthew G. Knepley 
2114d083f849SBarry Smith   Collective
211524119c2aSMatthew G. Knepley 
211624119c2aSMatthew G. Knepley   Input Parameters:
2117a1cb98faSBarry Smith + comm      - The communicator for the `DM` object
21189318fe57SMatthew G. Knepley - periodicZ - The boundary type for the Z direction
211924119c2aSMatthew G. Knepley 
212024119c2aSMatthew G. Knepley   Output Parameter:
212124119c2aSMatthew G. Knepley . dm  - The DM object
212224119c2aSMatthew G. Knepley 
212324119c2aSMatthew G. Knepley   Level: beginner
212424119c2aSMatthew G. Knepley 
2125a1cb98faSBarry Smith   Note:
2126a1cb98faSBarry Smith   Here is the output numbering looking from the bottom of the cylinder:
2127a1cb98faSBarry Smith .vb
2128a1cb98faSBarry Smith        17-----14
2129a1cb98faSBarry Smith         |     |
2130a1cb98faSBarry Smith         |  2  |
2131a1cb98faSBarry Smith         |     |
2132a1cb98faSBarry Smith  17-----8-----7-----14
2133a1cb98faSBarry Smith   |     |     |     |
2134a1cb98faSBarry Smith   |  3  |  0  |  1  |
2135a1cb98faSBarry Smith   |     |     |     |
2136a1cb98faSBarry Smith  19-----5-----6-----13
2137a1cb98faSBarry Smith         |     |
2138a1cb98faSBarry Smith         |  4  |
2139a1cb98faSBarry Smith         |     |
2140a1cb98faSBarry Smith        19-----13
2141a1cb98faSBarry Smith 
2142a1cb98faSBarry Smith  and up through the top
2143a1cb98faSBarry Smith 
2144a1cb98faSBarry Smith        18-----16
2145a1cb98faSBarry Smith         |     |
2146a1cb98faSBarry Smith         |  2  |
2147a1cb98faSBarry Smith         |     |
2148a1cb98faSBarry Smith  18----10----11-----16
2149a1cb98faSBarry Smith   |     |     |     |
2150a1cb98faSBarry Smith   |  3  |  0  |  1  |
2151a1cb98faSBarry Smith   |     |     |     |
2152a1cb98faSBarry Smith  20-----9----12-----15
2153a1cb98faSBarry Smith         |     |
2154a1cb98faSBarry Smith         |  4  |
2155a1cb98faSBarry Smith         |     |
2156a1cb98faSBarry Smith        20-----15
2157a1cb98faSBarry Smith .ve
2158a1cb98faSBarry Smith 
2159a1cb98faSBarry Smith .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
216024119c2aSMatthew G. Knepley @*/
2161d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateHexCylinderMesh(MPI_Comm comm, DMBoundaryType periodicZ, DM *dm)
2162d71ae5a4SJacob Faibussowitsch {
21639318fe57SMatthew G. Knepley   PetscFunctionBegin;
21649318fe57SMatthew G. Knepley   PetscValidPointer(dm, 3);
21659566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
21669566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
21679566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateHexCylinderMesh_Internal(*dm, periodicZ));
21689318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
21699318fe57SMatthew G. Knepley }
21709318fe57SMatthew G. Knepley 
2171d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateWedgeCylinderMesh_Internal(DM dm, PetscInt n, PetscBool interpolate)
2172d71ae5a4SJacob Faibussowitsch {
217324119c2aSMatthew G. Knepley   const PetscInt dim = 3;
2174412e9a14SMatthew G. Knepley   PetscInt       numCells, numVertices, v;
21759fe9f049SMatthew G. Knepley   PetscMPIInt    rank;
217624119c2aSMatthew G. Knepley 
217724119c2aSMatthew G. Knepley   PetscFunctionBegin;
217863a3b9bcSJacob Faibussowitsch   PetscCheck(n >= 0, PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of wedges %" PetscInt_FMT " cannot be negative", n);
21799566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
21809566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
2181412e9a14SMatthew G. Knepley   /* Must create the celltype label here so that we do not automatically try to compute the types */
21829566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "celltype"));
218324119c2aSMatthew G. Knepley   /* Create topology */
218424119c2aSMatthew G. Knepley   {
218524119c2aSMatthew G. Knepley     PetscInt cone[6], c;
218624119c2aSMatthew G. Knepley 
2187dd400576SPatrick Sanan     numCells    = rank == 0 ? n : 0;
2188dd400576SPatrick Sanan     numVertices = rank == 0 ? 2 * (n + 1) : 0;
21899566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
21909566063dSJacob Faibussowitsch     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 6));
21919566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm));
219224119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
21939371c9d4SSatish Balay       cone[0] = c + n * 1;
21949371c9d4SSatish Balay       cone[1] = (c + 1) % n + n * 1;
21959371c9d4SSatish Balay       cone[2] = 0 + 3 * n;
21969371c9d4SSatish Balay       cone[3] = c + n * 2;
21979371c9d4SSatish Balay       cone[4] = (c + 1) % n + n * 2;
21989371c9d4SSatish Balay       cone[5] = 1 + 3 * n;
21999566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(dm, c, cone));
22009566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(dm, c, DM_POLYTOPE_TRI_PRISM_TENSOR));
220124119c2aSMatthew G. Knepley     }
22029566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
22039566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
220424119c2aSMatthew G. Knepley   }
220548a46eb9SPierre Jolivet   for (v = numCells; v < numCells + numVertices; ++v) PetscCall(DMPlexSetCellType(dm, v, DM_POLYTOPE_POINT));
220624119c2aSMatthew G. Knepley   /* Create cylinder geometry */
220724119c2aSMatthew G. Knepley   {
220824119c2aSMatthew G. Knepley     Vec          coordinates;
220924119c2aSMatthew G. Knepley     PetscSection coordSection;
221024119c2aSMatthew G. Knepley     PetscScalar *coords;
2211412e9a14SMatthew G. Knepley     PetscInt     coordSize, c;
221224119c2aSMatthew G. Knepley 
221324119c2aSMatthew G. Knepley     /* Build coordinates */
22149566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
22159566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
22169566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
22179566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, numCells, numCells + numVertices));
221824119c2aSMatthew G. Knepley     for (v = numCells; v < numCells + numVertices; ++v) {
22199566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
22209566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
222124119c2aSMatthew G. Knepley     }
22229566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
22239566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
22249566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
22259566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
22269566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
22279566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
22289566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates, VECSTANDARD));
22299566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
223024119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
22319371c9d4SSatish Balay       coords[(c + 0 * n) * dim + 0] = PetscCosReal(2.0 * c * PETSC_PI / n);
22329371c9d4SSatish Balay       coords[(c + 0 * n) * dim + 1] = PetscSinReal(2.0 * c * PETSC_PI / n);
22339371c9d4SSatish Balay       coords[(c + 0 * n) * dim + 2] = 1.0;
22349371c9d4SSatish Balay       coords[(c + 1 * n) * dim + 0] = PetscCosReal(2.0 * c * PETSC_PI / n);
22359371c9d4SSatish Balay       coords[(c + 1 * n) * dim + 1] = PetscSinReal(2.0 * c * PETSC_PI / n);
22369371c9d4SSatish Balay       coords[(c + 1 * n) * dim + 2] = 0.0;
223724119c2aSMatthew G. Knepley     }
2238dd400576SPatrick Sanan     if (rank == 0) {
22399371c9d4SSatish Balay       coords[(2 * n + 0) * dim + 0] = 0.0;
22409371c9d4SSatish Balay       coords[(2 * n + 0) * dim + 1] = 0.0;
22419371c9d4SSatish Balay       coords[(2 * n + 0) * dim + 2] = 1.0;
22429371c9d4SSatish Balay       coords[(2 * n + 1) * dim + 0] = 0.0;
22439371c9d4SSatish Balay       coords[(2 * n + 1) * dim + 1] = 0.0;
22449371c9d4SSatish Balay       coords[(2 * n + 1) * dim + 2] = 0.0;
22459fe9f049SMatthew G. Knepley     }
22469566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
22479566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
22489566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
224924119c2aSMatthew G. Knepley   }
22509318fe57SMatthew G. Knepley   /* Interpolate */
22519566063dSJacob Faibussowitsch   if (interpolate) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
22529318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
22539318fe57SMatthew G. Knepley }
22549318fe57SMatthew G. Knepley 
22559318fe57SMatthew G. Knepley /*@
22569318fe57SMatthew G. Knepley   DMPlexCreateWedgeCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using wedges.
22579318fe57SMatthew G. Knepley 
22589318fe57SMatthew G. Knepley   Collective
22599318fe57SMatthew G. Knepley 
22609318fe57SMatthew G. Knepley   Input Parameters:
2261a1cb98faSBarry Smith + comm - The communicator for the `DM` object
22629318fe57SMatthew G. Knepley . n    - The number of wedges around the origin
22639318fe57SMatthew G. Knepley - interpolate - Create edges and faces
22649318fe57SMatthew G. Knepley 
22659318fe57SMatthew G. Knepley   Output Parameter:
2266a1cb98faSBarry Smith . dm  - The `DM` object
22679318fe57SMatthew G. Knepley 
22689318fe57SMatthew G. Knepley   Level: beginner
22699318fe57SMatthew G. Knepley 
2270a1cb98faSBarry Smith .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreateHexCylinderMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
22719318fe57SMatthew G. Knepley @*/
2272d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateWedgeCylinderMesh(MPI_Comm comm, PetscInt n, PetscBool interpolate, DM *dm)
2273d71ae5a4SJacob Faibussowitsch {
22749318fe57SMatthew G. Knepley   PetscFunctionBegin;
22759318fe57SMatthew G. Knepley   PetscValidPointer(dm, 4);
22769566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
22779566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
22789566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateWedgeCylinderMesh_Internal(*dm, n, interpolate));
227924119c2aSMatthew G. Knepley   PetscFunctionReturn(0);
228024119c2aSMatthew G. Knepley }
228124119c2aSMatthew G. Knepley 
2282d71ae5a4SJacob Faibussowitsch static inline PetscReal DiffNormReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
2283d71ae5a4SJacob Faibussowitsch {
228465a81367SMatthew G. Knepley   PetscReal prod = 0.0;
228565a81367SMatthew G. Knepley   PetscInt  i;
228665a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += PetscSqr(x[i] - y[i]);
228765a81367SMatthew G. Knepley   return PetscSqrtReal(prod);
228865a81367SMatthew G. Knepley }
2289d71ae5a4SJacob Faibussowitsch static inline PetscReal DotReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
2290d71ae5a4SJacob Faibussowitsch {
229165a81367SMatthew G. Knepley   PetscReal prod = 0.0;
229265a81367SMatthew G. Knepley   PetscInt  i;
229365a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += x[i] * y[i];
229465a81367SMatthew G. Knepley   return prod;
229565a81367SMatthew G. Knepley }
229665a81367SMatthew G. Knepley 
229751a74b61SMatthew G. Knepley /* The first constant is the sphere radius */
2298d71ae5a4SJacob 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[])
2299d71ae5a4SJacob Faibussowitsch {
230051a74b61SMatthew G. Knepley   PetscReal r     = PetscRealPart(constants[0]);
230151a74b61SMatthew G. Knepley   PetscReal norm2 = 0.0, fac;
230251a74b61SMatthew G. Knepley   PetscInt  n     = uOff[1] - uOff[0], d;
230351a74b61SMatthew G. Knepley 
230451a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) norm2 += PetscSqr(PetscRealPart(u[d]));
230551a74b61SMatthew G. Knepley   fac = r / PetscSqrtReal(norm2);
230651a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) f0[d] = u[d] * fac;
230751a74b61SMatthew G. Knepley }
230851a74b61SMatthew G. Knepley 
2309d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateSphereMesh_Internal(DM dm, PetscInt dim, PetscBool simplex, PetscReal R)
2310d71ae5a4SJacob Faibussowitsch {
231165a81367SMatthew G. Knepley   const PetscInt embedDim = dim + 1;
231265a81367SMatthew G. Knepley   PetscSection   coordSection;
231365a81367SMatthew G. Knepley   Vec            coordinates;
231465a81367SMatthew G. Knepley   PetscScalar   *coords;
231565a81367SMatthew G. Knepley   PetscReal     *coordsIn;
2316064cae4fSPierre Jolivet   PetscInt       numCells, numEdges, numVerts = 0, firstVertex = 0, v, firstEdge, coordSize, d, c, e;
231765a81367SMatthew G. Knepley   PetscMPIInt    rank;
231865a81367SMatthew G. Knepley 
231965a81367SMatthew G. Knepley   PetscFunctionBegin;
23209318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveBool(dm, simplex, 3);
23219566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
23229566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, dim + 1));
23239566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
232465a81367SMatthew G. Knepley   switch (dim) {
232565a81367SMatthew G. Knepley   case 2:
232665a81367SMatthew G. Knepley     if (simplex) {
232751a74b61SMatthew G. Knepley       const PetscReal radius    = PetscSqrtReal(1 + PETSC_PHI * PETSC_PHI) / (1.0 + PETSC_PHI);
232851a74b61SMatthew G. Knepley       const PetscReal edgeLen   = 2.0 / (1.0 + PETSC_PHI) * (R / radius);
232965a81367SMatthew G. Knepley       const PetscInt  degree    = 5;
233051a74b61SMatthew G. Knepley       PetscReal       vertex[3] = {0.0, 1.0 / (1.0 + PETSC_PHI), PETSC_PHI / (1.0 + PETSC_PHI)};
233165a81367SMatthew G. Knepley       PetscInt        s[3]      = {1, 1, 1};
233265a81367SMatthew G. Knepley       PetscInt        cone[3];
233365a81367SMatthew G. Knepley       PetscInt       *graph, p, i, j, k;
233465a81367SMatthew G. Knepley 
23359371c9d4SSatish Balay       vertex[0] *= R / radius;
23369371c9d4SSatish Balay       vertex[1] *= R / radius;
23379371c9d4SSatish Balay       vertex[2] *= R / radius;
2338dd400576SPatrick Sanan       numCells    = rank == 0 ? 20 : 0;
2339dd400576SPatrick Sanan       numVerts    = rank == 0 ? 12 : 0;
234065a81367SMatthew G. Knepley       firstVertex = numCells;
234151a74b61SMatthew G. Knepley       /* Use icosahedron, which for a R-sphere has coordinates which are all cyclic permutations of
234265a81367SMatthew G. Knepley 
234365a81367SMatthew G. Knepley            (0, \pm 1/\phi+1, \pm \phi/\phi+1)
234465a81367SMatthew G. Knepley 
234565a81367SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
234651a74b61SMatthew G. Knepley          length is then given by 2/(1+\phi) = 2 * 0.38197 = 0.76393.
234765a81367SMatthew G. Knepley       */
234865a81367SMatthew G. Knepley       /* Construct vertices */
23499566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
2350dd400576SPatrick Sanan       if (rank == 0) {
235165a81367SMatthew G. Knepley         for (p = 0, i = 0; p < embedDim; ++p) {
235265a81367SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
235365a81367SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
235465a81367SMatthew G. Knepley               for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[(d + p) % embedDim] * vertex[(d + p) % embedDim];
235565a81367SMatthew G. Knepley               ++i;
235665a81367SMatthew G. Knepley             }
235765a81367SMatthew G. Knepley           }
235865a81367SMatthew G. Knepley         }
235945da822fSValeria Barra       }
236065a81367SMatthew G. Knepley       /* Construct graph */
23619566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * numVerts, &graph));
236265a81367SMatthew G. Knepley       for (i = 0; i < numVerts; ++i) {
236365a81367SMatthew G. Knepley         for (j = 0, k = 0; j < numVerts; ++j) {
23649371c9d4SSatish Balay           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i * embedDim], &coordsIn[j * embedDim]) - edgeLen) < PETSC_SMALL) {
23659371c9d4SSatish Balay             graph[i * numVerts + j] = 1;
23669371c9d4SSatish Balay             ++k;
23679371c9d4SSatish Balay           }
236865a81367SMatthew G. Knepley         }
236963a3b9bcSJacob Faibussowitsch         PetscCheck(k == degree, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid icosahedron, vertex %" PetscInt_FMT " degree %" PetscInt_FMT " != %" PetscInt_FMT, i, k, degree);
237065a81367SMatthew G. Knepley       }
237165a81367SMatthew G. Knepley       /* Build Topology */
23729566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts));
237348a46eb9SPierre Jolivet       for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim));
23749566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
237565a81367SMatthew G. Knepley       /* Cells */
237665a81367SMatthew G. Knepley       for (i = 0, c = 0; i < numVerts; ++i) {
237765a81367SMatthew G. Knepley         for (j = 0; j < i; ++j) {
237865a81367SMatthew G. Knepley           for (k = 0; k < j; ++k) {
237965a81367SMatthew G. Knepley             if (graph[i * numVerts + j] && graph[j * numVerts + k] && graph[k * numVerts + i]) {
23809371c9d4SSatish Balay               cone[0] = firstVertex + i;
23819371c9d4SSatish Balay               cone[1] = firstVertex + j;
23829371c9d4SSatish Balay               cone[2] = firstVertex + k;
238365a81367SMatthew G. Knepley               /* Check orientation */
238465a81367SMatthew G. Knepley               {
23859371c9d4SSatish Balay                 const PetscInt epsilon[3][3][3] = {
23869371c9d4SSatish Balay                   {{0, 0, 0},  {0, 0, 1},  {0, -1, 0}},
23879371c9d4SSatish Balay                   {{0, 0, -1}, {0, 0, 0},  {1, 0, 0} },
23889371c9d4SSatish Balay                   {{0, 1, 0},  {-1, 0, 0}, {0, 0, 0} }
23899371c9d4SSatish Balay                 };
239065a81367SMatthew G. Knepley                 PetscReal normal[3];
239165a81367SMatthew G. Knepley                 PetscInt  e, f;
239265a81367SMatthew G. Knepley 
239365a81367SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) {
239465a81367SMatthew G. Knepley                   normal[d] = 0.0;
239565a81367SMatthew G. Knepley                   for (e = 0; e < embedDim; ++e) {
2396ad540459SPierre 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]);
239765a81367SMatthew G. Knepley                   }
239865a81367SMatthew G. Knepley                 }
23999371c9d4SSatish Balay                 if (DotReal(embedDim, normal, &coordsIn[i * embedDim]) < 0) {
24009371c9d4SSatish Balay                   PetscInt tmp = cone[1];
24019371c9d4SSatish Balay                   cone[1]      = cone[2];
24029371c9d4SSatish Balay                   cone[2]      = tmp;
240365a81367SMatthew G. Knepley                 }
240465a81367SMatthew G. Knepley               }
24059566063dSJacob Faibussowitsch               PetscCall(DMPlexSetCone(dm, c++, cone));
240665a81367SMatthew G. Knepley             }
240765a81367SMatthew G. Knepley           }
240865a81367SMatthew G. Knepley         }
240965a81367SMatthew G. Knepley       }
24109566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
24119566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
24129566063dSJacob Faibussowitsch       PetscCall(PetscFree(graph));
241365a81367SMatthew G. Knepley     } else {
24142829fed8SMatthew G. Knepley       /*
24152829fed8SMatthew G. Knepley         12-21--13
24162829fed8SMatthew G. Knepley          |     |
24172829fed8SMatthew G. Knepley         25  4  24
24182829fed8SMatthew G. Knepley          |     |
24192829fed8SMatthew G. Knepley   12-25--9-16--8-24--13
24202829fed8SMatthew G. Knepley    |     |     |     |
24212829fed8SMatthew G. Knepley   23  5 17  0 15  3  22
24222829fed8SMatthew G. Knepley    |     |     |     |
24232829fed8SMatthew G. Knepley   10-20--6-14--7-19--11
24242829fed8SMatthew G. Knepley          |     |
24252829fed8SMatthew G. Knepley         20  1  19
24262829fed8SMatthew G. Knepley          |     |
24272829fed8SMatthew G. Knepley         10-18--11
24282829fed8SMatthew G. Knepley          |     |
24292829fed8SMatthew G. Knepley         23  2  22
24302829fed8SMatthew G. Knepley          |     |
24312829fed8SMatthew G. Knepley         12-21--13
24322829fed8SMatthew G. Knepley        */
24332829fed8SMatthew G. Knepley       PetscInt cone[4], ornt[4];
24342829fed8SMatthew G. Knepley 
2435dd400576SPatrick Sanan       numCells    = rank == 0 ? 6 : 0;
2436dd400576SPatrick Sanan       numEdges    = rank == 0 ? 12 : 0;
2437dd400576SPatrick Sanan       numVerts    = rank == 0 ? 8 : 0;
243865a81367SMatthew G. Knepley       firstVertex = numCells;
243965a81367SMatthew G. Knepley       firstEdge   = numCells + numVerts;
24402829fed8SMatthew G. Knepley       /* Build Topology */
24419566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells + numEdges + numVerts));
244248a46eb9SPierre Jolivet       for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 4));
244348a46eb9SPierre Jolivet       for (e = firstEdge; e < firstEdge + numEdges; ++e) PetscCall(DMPlexSetConeSize(dm, e, 2));
24449566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
2445dd400576SPatrick Sanan       if (rank == 0) {
24462829fed8SMatthew G. Knepley         /* Cell 0 */
24479371c9d4SSatish Balay         cone[0] = 14;
24489371c9d4SSatish Balay         cone[1] = 15;
24499371c9d4SSatish Balay         cone[2] = 16;
24509371c9d4SSatish Balay         cone[3] = 17;
24519566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
24529371c9d4SSatish Balay         ornt[0] = 0;
24539371c9d4SSatish Balay         ornt[1] = 0;
24549371c9d4SSatish Balay         ornt[2] = 0;
24559371c9d4SSatish Balay         ornt[3] = 0;
24569566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 0, ornt));
24572829fed8SMatthew G. Knepley         /* Cell 1 */
24589371c9d4SSatish Balay         cone[0] = 18;
24599371c9d4SSatish Balay         cone[1] = 19;
24609371c9d4SSatish Balay         cone[2] = 14;
24619371c9d4SSatish Balay         cone[3] = 20;
24629566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
24639371c9d4SSatish Balay         ornt[0] = 0;
24649371c9d4SSatish Balay         ornt[1] = 0;
24659371c9d4SSatish Balay         ornt[2] = -1;
24669371c9d4SSatish Balay         ornt[3] = 0;
24679566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 1, ornt));
24682829fed8SMatthew G. Knepley         /* Cell 2 */
24699371c9d4SSatish Balay         cone[0] = 21;
24709371c9d4SSatish Balay         cone[1] = 22;
24719371c9d4SSatish Balay         cone[2] = 18;
24729371c9d4SSatish Balay         cone[3] = 23;
24739566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
24749371c9d4SSatish Balay         ornt[0] = 0;
24759371c9d4SSatish Balay         ornt[1] = 0;
24769371c9d4SSatish Balay         ornt[2] = -1;
24779371c9d4SSatish Balay         ornt[3] = 0;
24789566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 2, ornt));
24792829fed8SMatthew G. Knepley         /* Cell 3 */
24809371c9d4SSatish Balay         cone[0] = 19;
24819371c9d4SSatish Balay         cone[1] = 22;
24829371c9d4SSatish Balay         cone[2] = 24;
24839371c9d4SSatish Balay         cone[3] = 15;
24849566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
24859371c9d4SSatish Balay         ornt[0] = -1;
24869371c9d4SSatish Balay         ornt[1] = -1;
24879371c9d4SSatish Balay         ornt[2] = 0;
24889371c9d4SSatish Balay         ornt[3] = -1;
24899566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 3, ornt));
24902829fed8SMatthew G. Knepley         /* Cell 4 */
24919371c9d4SSatish Balay         cone[0] = 16;
24929371c9d4SSatish Balay         cone[1] = 24;
24939371c9d4SSatish Balay         cone[2] = 21;
24949371c9d4SSatish Balay         cone[3] = 25;
24959566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
24969371c9d4SSatish Balay         ornt[0] = -1;
24979371c9d4SSatish Balay         ornt[1] = -1;
24989371c9d4SSatish Balay         ornt[2] = -1;
24999371c9d4SSatish Balay         ornt[3] = 0;
25009566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 4, ornt));
25012829fed8SMatthew G. Knepley         /* Cell 5 */
25029371c9d4SSatish Balay         cone[0] = 20;
25039371c9d4SSatish Balay         cone[1] = 17;
25049371c9d4SSatish Balay         cone[2] = 25;
25059371c9d4SSatish Balay         cone[3] = 23;
25069566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 5, cone));
25079371c9d4SSatish Balay         ornt[0] = -1;
25089371c9d4SSatish Balay         ornt[1] = -1;
25099371c9d4SSatish Balay         ornt[2] = -1;
25109371c9d4SSatish Balay         ornt[3] = -1;
25119566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 5, ornt));
25122829fed8SMatthew G. Knepley         /* Edges */
25139371c9d4SSatish Balay         cone[0] = 6;
25149371c9d4SSatish Balay         cone[1] = 7;
25159566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 14, cone));
25169371c9d4SSatish Balay         cone[0] = 7;
25179371c9d4SSatish Balay         cone[1] = 8;
25189566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 15, cone));
25199371c9d4SSatish Balay         cone[0] = 8;
25209371c9d4SSatish Balay         cone[1] = 9;
25219566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 16, cone));
25229371c9d4SSatish Balay         cone[0] = 9;
25239371c9d4SSatish Balay         cone[1] = 6;
25249566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 17, cone));
25259371c9d4SSatish Balay         cone[0] = 10;
25269371c9d4SSatish Balay         cone[1] = 11;
25279566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 18, cone));
25289371c9d4SSatish Balay         cone[0] = 11;
25299371c9d4SSatish Balay         cone[1] = 7;
25309566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 19, cone));
25319371c9d4SSatish Balay         cone[0] = 6;
25329371c9d4SSatish Balay         cone[1] = 10;
25339566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 20, cone));
25349371c9d4SSatish Balay         cone[0] = 12;
25359371c9d4SSatish Balay         cone[1] = 13;
25369566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 21, cone));
25379371c9d4SSatish Balay         cone[0] = 13;
25389371c9d4SSatish Balay         cone[1] = 11;
25399566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 22, cone));
25409371c9d4SSatish Balay         cone[0] = 10;
25419371c9d4SSatish Balay         cone[1] = 12;
25429566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 23, cone));
25439371c9d4SSatish Balay         cone[0] = 13;
25449371c9d4SSatish Balay         cone[1] = 8;
25459566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 24, cone));
25469371c9d4SSatish Balay         cone[0] = 12;
25479371c9d4SSatish Balay         cone[1] = 9;
25489566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 25, cone));
254945da822fSValeria Barra       }
25509566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
25519566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
25522829fed8SMatthew G. Knepley       /* Build coordinates */
25539566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
2554dd400576SPatrick Sanan       if (rank == 0) {
25559371c9d4SSatish Balay         coordsIn[0 * embedDim + 0] = -R;
25569371c9d4SSatish Balay         coordsIn[0 * embedDim + 1] = R;
25579371c9d4SSatish Balay         coordsIn[0 * embedDim + 2] = -R;
25589371c9d4SSatish Balay         coordsIn[1 * embedDim + 0] = R;
25599371c9d4SSatish Balay         coordsIn[1 * embedDim + 1] = R;
25609371c9d4SSatish Balay         coordsIn[1 * embedDim + 2] = -R;
25619371c9d4SSatish Balay         coordsIn[2 * embedDim + 0] = R;
25629371c9d4SSatish Balay         coordsIn[2 * embedDim + 1] = -R;
25639371c9d4SSatish Balay         coordsIn[2 * embedDim + 2] = -R;
25649371c9d4SSatish Balay         coordsIn[3 * embedDim + 0] = -R;
25659371c9d4SSatish Balay         coordsIn[3 * embedDim + 1] = -R;
25669371c9d4SSatish Balay         coordsIn[3 * embedDim + 2] = -R;
25679371c9d4SSatish Balay         coordsIn[4 * embedDim + 0] = -R;
25689371c9d4SSatish Balay         coordsIn[4 * embedDim + 1] = R;
25699371c9d4SSatish Balay         coordsIn[4 * embedDim + 2] = R;
25709371c9d4SSatish Balay         coordsIn[5 * embedDim + 0] = R;
25719371c9d4SSatish Balay         coordsIn[5 * embedDim + 1] = R;
25729371c9d4SSatish Balay         coordsIn[5 * embedDim + 2] = R;
25739371c9d4SSatish Balay         coordsIn[6 * embedDim + 0] = -R;
25749371c9d4SSatish Balay         coordsIn[6 * embedDim + 1] = -R;
25759371c9d4SSatish Balay         coordsIn[6 * embedDim + 2] = R;
25769371c9d4SSatish Balay         coordsIn[7 * embedDim + 0] = R;
25779371c9d4SSatish Balay         coordsIn[7 * embedDim + 1] = -R;
25789371c9d4SSatish Balay         coordsIn[7 * embedDim + 2] = R;
257965a81367SMatthew G. Knepley       }
258045da822fSValeria Barra     }
258165a81367SMatthew G. Knepley     break;
258265a81367SMatthew G. Knepley   case 3:
2583116ded15SMatthew G. Knepley     if (simplex) {
2584116ded15SMatthew G. Knepley       const PetscReal edgeLen         = 1.0 / PETSC_PHI;
258551a74b61SMatthew G. Knepley       PetscReal       vertexA[4]      = {0.5, 0.5, 0.5, 0.5};
258651a74b61SMatthew G. Knepley       PetscReal       vertexB[4]      = {1.0, 0.0, 0.0, 0.0};
258751a74b61SMatthew G. Knepley       PetscReal       vertexC[4]      = {0.5, 0.5 * PETSC_PHI, 0.5 / PETSC_PHI, 0.0};
2588116ded15SMatthew G. Knepley       const PetscInt  degree          = 12;
2589116ded15SMatthew G. Knepley       PetscInt        s[4]            = {1, 1, 1};
25909371c9d4SSatish Balay       PetscInt        evenPerm[12][4] = {
25919371c9d4SSatish Balay         {0, 1, 2, 3},
25929371c9d4SSatish Balay         {0, 2, 3, 1},
25939371c9d4SSatish Balay         {0, 3, 1, 2},
25949371c9d4SSatish Balay         {1, 0, 3, 2},
25959371c9d4SSatish Balay         {1, 2, 0, 3},
25969371c9d4SSatish Balay         {1, 3, 2, 0},
25979371c9d4SSatish Balay         {2, 0, 1, 3},
25989371c9d4SSatish Balay         {2, 1, 3, 0},
25999371c9d4SSatish Balay         {2, 3, 0, 1},
26009371c9d4SSatish Balay         {3, 0, 2, 1},
26019371c9d4SSatish Balay         {3, 1, 0, 2},
26029371c9d4SSatish Balay         {3, 2, 1, 0}
26039371c9d4SSatish Balay       };
2604116ded15SMatthew G. Knepley       PetscInt  cone[4];
2605116ded15SMatthew G. Knepley       PetscInt *graph, p, i, j, k, l;
2606116ded15SMatthew G. Knepley 
26079371c9d4SSatish Balay       vertexA[0] *= R;
26089371c9d4SSatish Balay       vertexA[1] *= R;
26099371c9d4SSatish Balay       vertexA[2] *= R;
26109371c9d4SSatish Balay       vertexA[3] *= R;
26119371c9d4SSatish Balay       vertexB[0] *= R;
26129371c9d4SSatish Balay       vertexB[1] *= R;
26139371c9d4SSatish Balay       vertexB[2] *= R;
26149371c9d4SSatish Balay       vertexB[3] *= R;
26159371c9d4SSatish Balay       vertexC[0] *= R;
26169371c9d4SSatish Balay       vertexC[1] *= R;
26179371c9d4SSatish Balay       vertexC[2] *= R;
26189371c9d4SSatish Balay       vertexC[3] *= R;
2619dd400576SPatrick Sanan       numCells    = rank == 0 ? 600 : 0;
2620dd400576SPatrick Sanan       numVerts    = rank == 0 ? 120 : 0;
2621116ded15SMatthew G. Knepley       firstVertex = numCells;
2622116ded15SMatthew G. Knepley       /* Use the 600-cell, which for a unit sphere has coordinates which are
2623116ded15SMatthew G. Knepley 
2624116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm 1,    \pm 1, \pm 1)                          16
2625116ded15SMatthew G. Knepley                (\pm 1,    0,       0,      0)  all cyclic permutations   8
2626116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm phi, \pm 1/phi, 0)  all even permutations    96
2627116ded15SMatthew G. Knepley 
2628116ded15SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
26296333ae4fSvaleriabarra          length is then given by 1/\phi = 0.61803.
2630116ded15SMatthew G. Knepley 
2631116ded15SMatthew G. Knepley          http://buzzard.pugetsound.edu/sage-practice/ch03s03.html
2632116ded15SMatthew G. Knepley          http://mathworld.wolfram.com/600-Cell.html
2633116ded15SMatthew G. Knepley       */
2634116ded15SMatthew G. Knepley       /* Construct vertices */
26359566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
2636116ded15SMatthew G. Knepley       i = 0;
2637dd400576SPatrick Sanan       if (rank == 0) {
2638116ded15SMatthew G. Knepley         for (s[0] = -1; s[0] < 2; s[0] += 2) {
2639116ded15SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
2640116ded15SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
2641116ded15SMatthew G. Knepley               for (s[3] = -1; s[3] < 2; s[3] += 2) {
2642116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[d] * vertexA[d];
2643116ded15SMatthew G. Knepley                 ++i;
2644116ded15SMatthew G. Knepley               }
2645116ded15SMatthew G. Knepley             }
2646116ded15SMatthew G. Knepley           }
2647116ded15SMatthew G. Knepley         }
2648116ded15SMatthew G. Knepley         for (p = 0; p < embedDim; ++p) {
2649116ded15SMatthew G. Knepley           s[1] = s[2] = s[3] = 1;
2650116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
2651116ded15SMatthew G. Knepley             for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[(d + p) % embedDim] * vertexB[(d + p) % embedDim];
2652116ded15SMatthew G. Knepley             ++i;
2653116ded15SMatthew G. Knepley           }
2654116ded15SMatthew G. Knepley         }
2655116ded15SMatthew G. Knepley         for (p = 0; p < 12; ++p) {
2656116ded15SMatthew G. Knepley           s[3] = 1;
2657116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
2658116ded15SMatthew G. Knepley             for (s[1] = -1; s[1] < 2; s[1] += 2) {
2659116ded15SMatthew G. Knepley               for (s[2] = -1; s[2] < 2; s[2] += 2) {
2660116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i * embedDim + d] = s[evenPerm[p][d]] * vertexC[evenPerm[p][d]];
2661116ded15SMatthew G. Knepley                 ++i;
2662116ded15SMatthew G. Knepley               }
2663116ded15SMatthew G. Knepley             }
2664116ded15SMatthew G. Knepley           }
2665116ded15SMatthew G. Knepley         }
266645da822fSValeria Barra       }
266763a3b9bcSJacob Faibussowitsch       PetscCheck(i == numVerts, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertices %" PetscInt_FMT " != %" PetscInt_FMT, i, numVerts);
2668116ded15SMatthew G. Knepley       /* Construct graph */
26699566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * numVerts, &graph));
2670116ded15SMatthew G. Knepley       for (i = 0; i < numVerts; ++i) {
2671116ded15SMatthew G. Knepley         for (j = 0, k = 0; j < numVerts; ++j) {
26729371c9d4SSatish Balay           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i * embedDim], &coordsIn[j * embedDim]) - edgeLen) < PETSC_SMALL) {
26739371c9d4SSatish Balay             graph[i * numVerts + j] = 1;
26749371c9d4SSatish Balay             ++k;
26759371c9d4SSatish Balay           }
2676116ded15SMatthew G. Knepley         }
267763a3b9bcSJacob Faibussowitsch         PetscCheck(k == degree, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertex %" PetscInt_FMT " degree %" PetscInt_FMT " != %" PetscInt_FMT, i, k, degree);
2678116ded15SMatthew G. Knepley       }
2679116ded15SMatthew G. Knepley       /* Build Topology */
26809566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells + numVerts));
268148a46eb9SPierre Jolivet       for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, embedDim));
26829566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
2683116ded15SMatthew G. Knepley       /* Cells */
2684dd400576SPatrick Sanan       if (rank == 0) {
2685116ded15SMatthew G. Knepley         for (i = 0, c = 0; i < numVerts; ++i) {
2686116ded15SMatthew G. Knepley           for (j = 0; j < i; ++j) {
2687116ded15SMatthew G. Knepley             for (k = 0; k < j; ++k) {
2688116ded15SMatthew G. Knepley               for (l = 0; l < k; ++l) {
26899371c9d4SSatish 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]) {
26909371c9d4SSatish Balay                   cone[0] = firstVertex + i;
26919371c9d4SSatish Balay                   cone[1] = firstVertex + j;
26929371c9d4SSatish Balay                   cone[2] = firstVertex + k;
26939371c9d4SSatish Balay                   cone[3] = firstVertex + l;
2694116ded15SMatthew G. Knepley                   /* Check orientation: https://ef.gy/linear-algebra:normal-vectors-in-higher-dimensional-spaces */
2695116ded15SMatthew G. Knepley                   {
26969371c9d4SSatish Balay                     const PetscInt epsilon[4][4][4][4] = {
26979371c9d4SSatish 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}}},
2698116ded15SMatthew G. Knepley 
26999371c9d4SSatish 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}}},
2700116ded15SMatthew G. Knepley 
27019371c9d4SSatish 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}}},
2702116ded15SMatthew G. Knepley 
27039371c9d4SSatish 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}} }
27049371c9d4SSatish Balay                     };
2705116ded15SMatthew G. Knepley                     PetscReal normal[4];
2706116ded15SMatthew G. Knepley                     PetscInt  e, f, g;
2707116ded15SMatthew G. Knepley 
2708116ded15SMatthew G. Knepley                     for (d = 0; d < embedDim; ++d) {
2709116ded15SMatthew G. Knepley                       normal[d] = 0.0;
2710116ded15SMatthew G. Knepley                       for (e = 0; e < embedDim; ++e) {
2711116ded15SMatthew G. Knepley                         for (f = 0; f < embedDim; ++f) {
2712116ded15SMatthew G. Knepley                           for (g = 0; g < embedDim; ++g) {
2713116ded15SMatthew 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]);
2714116ded15SMatthew G. Knepley                           }
2715116ded15SMatthew G. Knepley                         }
2716116ded15SMatthew G. Knepley                       }
2717116ded15SMatthew G. Knepley                     }
27189371c9d4SSatish Balay                     if (DotReal(embedDim, normal, &coordsIn[i * embedDim]) < 0) {
27199371c9d4SSatish Balay                       PetscInt tmp = cone[1];
27209371c9d4SSatish Balay                       cone[1]      = cone[2];
27219371c9d4SSatish Balay                       cone[2]      = tmp;
27229371c9d4SSatish Balay                     }
2723116ded15SMatthew G. Knepley                   }
27249566063dSJacob Faibussowitsch                   PetscCall(DMPlexSetCone(dm, c++, cone));
2725116ded15SMatthew G. Knepley                 }
2726116ded15SMatthew G. Knepley               }
2727116ded15SMatthew G. Knepley             }
2728116ded15SMatthew G. Knepley           }
2729116ded15SMatthew G. Knepley         }
273045da822fSValeria Barra       }
27319566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
27329566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
27339566063dSJacob Faibussowitsch       PetscCall(PetscFree(graph));
2734116ded15SMatthew G. Knepley     }
2735f4d061e9SPierre Jolivet     break;
2736d71ae5a4SJacob Faibussowitsch   default:
2737d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unsupported dimension for sphere: %" PetscInt_FMT, dim);
273865a81367SMatthew G. Knepley   }
273965a81367SMatthew G. Knepley   /* Create coordinates */
27409566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
27419566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
27429566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, embedDim));
27439566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numVerts));
27442829fed8SMatthew G. Knepley   for (v = firstVertex; v < firstVertex + numVerts; ++v) {
27459566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, embedDim));
27469566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, embedDim));
27472829fed8SMatthew G. Knepley   }
27489566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
27499566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
27509566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
27519566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, embedDim));
27529566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
27539566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
27549566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
27559566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
27569371c9d4SSatish Balay   for (v = 0; v < numVerts; ++v)
2757ad540459SPierre Jolivet     for (d = 0; d < embedDim; ++d) coords[v * embedDim + d] = coordsIn[v * embedDim + d];
27589566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
27599566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
27609566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
27619566063dSJacob Faibussowitsch   PetscCall(PetscFree(coordsIn));
276251a74b61SMatthew G. Knepley   {
276351a74b61SMatthew G. Knepley     DM          cdm;
276451a74b61SMatthew G. Knepley     PetscDS     cds;
27659318fe57SMatthew G. Knepley     PetscScalar c = R;
276651a74b61SMatthew G. Knepley 
27679566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCoordinateSpace(dm, 1, snapToSphere));
27689566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
27699566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
27709566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, 1, &c));
277151a74b61SMatthew G. Knepley   }
27729318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
27739566063dSJacob Faibussowitsch   if (simplex) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
27749318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
27759318fe57SMatthew G. Knepley }
27769318fe57SMatthew G. Knepley 
2777b7f5c055SJed Brown typedef void (*TPSEvaluateFunc)(const PetscReal[], PetscReal *, PetscReal[], PetscReal (*)[3]);
2778b7f5c055SJed Brown 
2779b7f5c055SJed Brown /*
2780b7f5c055SJed Brown  The Schwarz P implicit surface is
2781b7f5c055SJed Brown 
2782b7f5c055SJed Brown      f(x) = cos(x0) + cos(x1) + cos(x2) = 0
2783b7f5c055SJed Brown */
2784d71ae5a4SJacob Faibussowitsch static void TPSEvaluate_SchwarzP(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3])
2785d71ae5a4SJacob Faibussowitsch {
2786b7f5c055SJed Brown   PetscReal c[3] = {PetscCosReal(y[0] * PETSC_PI), PetscCosReal(y[1] * PETSC_PI), PetscCosReal(y[2] * PETSC_PI)};
2787b7f5c055SJed Brown   PetscReal g[3] = {-PetscSinReal(y[0] * PETSC_PI), -PetscSinReal(y[1] * PETSC_PI), -PetscSinReal(y[2] * PETSC_PI)};
2788b7f5c055SJed Brown   f[0]           = c[0] + c[1] + c[2];
2789b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) {
2790b7f5c055SJed Brown     grad[i] = PETSC_PI * g[i];
2791ad540459SPierre Jolivet     for (PetscInt j = 0; j < 3; j++) hess[i][j] = (i == j) ? -PetscSqr(PETSC_PI) * c[i] : 0.;
2792b7f5c055SJed Brown   }
2793b7f5c055SJed Brown }
2794b7f5c055SJed Brown 
27954663dae6SJed Brown // u[] is a tentative normal on input. Replace with the implicit function gradient in the same direction
2796d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSExtrudeNormalFunc_SchwarzP(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt r, PetscScalar u[], void *ctx)
2797d71ae5a4SJacob Faibussowitsch {
2798ad540459SPierre Jolivet   for (PetscInt i = 0; i < 3; i++) u[i] = -PETSC_PI * PetscSinReal(x[i] * PETSC_PI);
27994663dae6SJed Brown   return 0;
28004663dae6SJed Brown }
28014663dae6SJed Brown 
2802b7f5c055SJed Brown /*
2803b7f5c055SJed Brown  The Gyroid implicit surface is
2804b7f5c055SJed Brown 
2805b7f5c055SJed 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)
2806b7f5c055SJed Brown 
2807b7f5c055SJed Brown */
2808d71ae5a4SJacob Faibussowitsch static void TPSEvaluate_Gyroid(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3])
2809d71ae5a4SJacob Faibussowitsch {
2810b7f5c055SJed Brown   PetscReal s[3] = {PetscSinReal(PETSC_PI * y[0]), PetscSinReal(PETSC_PI * (y[1] + .5)), PetscSinReal(PETSC_PI * (y[2] + .25))};
2811b7f5c055SJed Brown   PetscReal c[3] = {PetscCosReal(PETSC_PI * y[0]), PetscCosReal(PETSC_PI * (y[1] + .5)), PetscCosReal(PETSC_PI * (y[2] + .25))};
2812b7f5c055SJed Brown   f[0]           = s[0] * c[1] + s[1] * c[2] + s[2] * c[0];
2813b7f5c055SJed Brown   grad[0]        = PETSC_PI * (c[0] * c[1] - s[2] * s[0]);
2814b7f5c055SJed Brown   grad[1]        = PETSC_PI * (c[1] * c[2] - s[0] * s[1]);
2815b7f5c055SJed Brown   grad[2]        = PETSC_PI * (c[2] * c[0] - s[1] * s[2]);
2816b7f5c055SJed Brown   hess[0][0]     = -PetscSqr(PETSC_PI) * (s[0] * c[1] + s[2] * c[0]);
2817b7f5c055SJed Brown   hess[0][1]     = -PetscSqr(PETSC_PI) * (c[0] * s[1]);
2818b7f5c055SJed Brown   hess[0][2]     = -PetscSqr(PETSC_PI) * (c[2] * s[0]);
2819b7f5c055SJed Brown   hess[1][0]     = -PetscSqr(PETSC_PI) * (s[1] * c[2] + s[0] * c[1]);
2820b7f5c055SJed Brown   hess[1][1]     = -PetscSqr(PETSC_PI) * (c[1] * s[2]);
2821b7f5c055SJed Brown   hess[2][2]     = -PetscSqr(PETSC_PI) * (c[0] * s[1]);
2822b7f5c055SJed Brown   hess[2][0]     = -PetscSqr(PETSC_PI) * (s[2] * c[0] + s[1] * c[2]);
2823b7f5c055SJed Brown   hess[2][1]     = -PetscSqr(PETSC_PI) * (c[2] * s[0]);
2824b7f5c055SJed Brown   hess[2][2]     = -PetscSqr(PETSC_PI) * (c[1] * s[2]);
2825b7f5c055SJed Brown }
2826b7f5c055SJed Brown 
28274663dae6SJed Brown // u[] is a tentative normal on input. Replace with the implicit function gradient in the same direction
2828d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSExtrudeNormalFunc_Gyroid(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt r, PetscScalar u[], void *ctx)
2829d71ae5a4SJacob Faibussowitsch {
28304663dae6SJed Brown   PetscReal s[3] = {PetscSinReal(PETSC_PI * x[0]), PetscSinReal(PETSC_PI * (x[1] + .5)), PetscSinReal(PETSC_PI * (x[2] + .25))};
28314663dae6SJed Brown   PetscReal c[3] = {PetscCosReal(PETSC_PI * x[0]), PetscCosReal(PETSC_PI * (x[1] + .5)), PetscCosReal(PETSC_PI * (x[2] + .25))};
28324663dae6SJed Brown   u[0]           = PETSC_PI * (c[0] * c[1] - s[2] * s[0]);
28334663dae6SJed Brown   u[1]           = PETSC_PI * (c[1] * c[2] - s[0] * s[1]);
28344663dae6SJed Brown   u[2]           = PETSC_PI * (c[2] * c[0] - s[1] * s[2]);
28354663dae6SJed Brown   return 0;
28364663dae6SJed Brown }
28374663dae6SJed Brown 
2838b7f5c055SJed Brown /*
2839b7f5c055SJed Brown    We wish to solve
2840b7f5c055SJed Brown 
2841b7f5c055SJed Brown          min_y || y - x ||^2  subject to f(y) = 0
2842b7f5c055SJed Brown 
2843b7f5c055SJed Brown    Let g(y) = grad(f).  The minimization problem is equivalent to asking to satisfy
2844b7f5c055SJed 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
2845b7f5c055SJed Brown    tangent space and ask for both components in the tangent space to be zero.
2846b7f5c055SJed Brown 
2847b7f5c055SJed Brown    Take g to be a column vector and compute the "full QR" factorization Q R = g,
2848b7f5c055SJed Brown    where Q = I - 2 n n^T is a symmetric orthogonal matrix.
2849b7f5c055SJed Brown    The first column of Q is parallel to g so the remaining two columns span the null space.
2850b7f5c055SJed Brown    Let Qn = Q[:,1:] be those remaining columns.  Then Qn Qn^T is an orthogonal projector into the tangent space.
2851b7f5c055SJed Brown    Since Q is symmetric, this is equivalent to multipyling by Q and taking the last two entries.
2852b7f5c055SJed Brown    In total, we have a system of 3 equations in 3 unknowns:
2853b7f5c055SJed Brown 
2854b7f5c055SJed Brown      f(y) = 0                       1 equation
2855b7f5c055SJed Brown      Qn^T (y - x) = 0               2 equations
2856b7f5c055SJed Brown 
2857b7f5c055SJed Brown    Here, we compute the residual and Jacobian of this system.
2858b7f5c055SJed Brown */
2859d71ae5a4SJacob Faibussowitsch static void TPSNearestPointResJac(TPSEvaluateFunc feval, const PetscScalar x[], const PetscScalar y[], PetscScalar res[], PetscScalar J[])
2860d71ae5a4SJacob Faibussowitsch {
2861b7f5c055SJed Brown   PetscReal yreal[3] = {PetscRealPart(y[0]), PetscRealPart(y[1]), PetscRealPart(y[2])};
2862b7f5c055SJed Brown   PetscReal d[3]     = {PetscRealPart(y[0] - x[0]), PetscRealPart(y[1] - x[1]), PetscRealPart(y[2] - x[2])};
28632f0490c0SSatish Balay   PetscReal f, grad[3], n[3], norm, norm_y[3], nd, nd_y[3], sign;
28649371c9d4SSatish Balay   PetscReal n_y[3][3] = {
28659371c9d4SSatish Balay     {0, 0, 0},
28669371c9d4SSatish Balay     {0, 0, 0},
28679371c9d4SSatish Balay     {0, 0, 0}
28689371c9d4SSatish Balay   };
2869b7f5c055SJed Brown 
2870b7f5c055SJed Brown   feval(yreal, &f, grad, n_y);
2871b7f5c055SJed Brown 
2872b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) n[i] = grad[i];
2873b7f5c055SJed Brown   norm = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2]));
2874ad540459SPierre Jolivet   for (PetscInt i = 0; i < 3; i++) norm_y[i] = 1. / norm * n[i] * n_y[i][i];
2875b7f5c055SJed Brown 
2876b7f5c055SJed Brown   // Define the Householder reflector
2877b7f5c055SJed Brown   sign = n[0] >= 0 ? 1. : -1.;
2878b7f5c055SJed Brown   n[0] += norm * sign;
2879b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) n_y[0][i] += norm_y[i] * sign;
2880b7f5c055SJed Brown 
2881b7f5c055SJed Brown   norm      = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2]));
2882b7f5c055SJed Brown   norm_y[0] = 1. / norm * (n[0] * n_y[0][0]);
2883b7f5c055SJed Brown   norm_y[1] = 1. / norm * (n[0] * n_y[0][1] + n[1] * n_y[1][1]);
2884b7f5c055SJed Brown   norm_y[2] = 1. / norm * (n[0] * n_y[0][2] + n[2] * n_y[2][2]);
2885b7f5c055SJed Brown 
2886b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) {
2887b7f5c055SJed Brown     n[i] /= norm;
2888b7f5c055SJed Brown     for (PetscInt j = 0; j < 3; j++) {
2889b7f5c055SJed Brown       // note that n[i] is n_old[i]/norm when executing the code below
2890b7f5c055SJed Brown       n_y[i][j] = n_y[i][j] / norm - n[i] / norm * norm_y[j];
2891b7f5c055SJed Brown     }
2892b7f5c055SJed Brown   }
2893b7f5c055SJed Brown 
2894b7f5c055SJed Brown   nd = n[0] * d[0] + n[1] * d[1] + n[2] * d[2];
2895b7f5c055SJed 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];
2896b7f5c055SJed Brown 
2897b7f5c055SJed Brown   res[0] = f;
2898b7f5c055SJed Brown   res[1] = d[1] - 2 * n[1] * nd;
2899b7f5c055SJed Brown   res[2] = d[2] - 2 * n[2] * nd;
2900b7f5c055SJed Brown   // J[j][i] is J_{ij} (column major)
2901b7f5c055SJed Brown   for (PetscInt j = 0; j < 3; j++) {
2902b7f5c055SJed Brown     J[0 + j * 3] = grad[j];
2903b7f5c055SJed Brown     J[1 + j * 3] = (j == 1) * 1. - 2 * (n_y[1][j] * nd + n[1] * nd_y[j]);
2904b7f5c055SJed Brown     J[2 + j * 3] = (j == 2) * 1. - 2 * (n_y[2][j] * nd + n[2] * nd_y[j]);
2905b7f5c055SJed Brown   }
2906b7f5c055SJed Brown }
2907b7f5c055SJed Brown 
2908b7f5c055SJed Brown /*
2909b7f5c055SJed Brown    Project x to the nearest point on the implicit surface using Newton's method.
2910b7f5c055SJed Brown */
2911d71ae5a4SJacob Faibussowitsch static PetscErrorCode TPSNearestPoint(TPSEvaluateFunc feval, PetscScalar x[])
2912d71ae5a4SJacob Faibussowitsch {
2913b7f5c055SJed Brown   PetscScalar y[3] = {x[0], x[1], x[2]}; // Initial guess
2914b7f5c055SJed Brown 
2915b7f5c055SJed Brown   PetscFunctionBegin;
2916b7f5c055SJed Brown   for (PetscInt iter = 0; iter < 10; iter++) {
2917b7f5c055SJed Brown     PetscScalar res[3], J[9];
2918b7f5c055SJed Brown     PetscReal   resnorm;
2919b7f5c055SJed Brown     TPSNearestPointResJac(feval, x, y, res, J);
2920b7f5c055SJed Brown     resnorm = PetscSqrtReal(PetscSqr(PetscRealPart(res[0])) + PetscSqr(PetscRealPart(res[1])) + PetscSqr(PetscRealPart(res[2])));
2921b7f5c055SJed Brown     if (0) { // Turn on this monitor if you need to confirm quadratic convergence
292263a3b9bcSJacob 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])));
2923b7f5c055SJed Brown     }
2924b7f5c055SJed Brown     if (resnorm < PETSC_SMALL) break;
2925b7f5c055SJed Brown 
2926b7f5c055SJed Brown     // Take the Newton step
29279566063dSJacob Faibussowitsch     PetscCall(PetscKernel_A_gets_inverse_A_3(J, 0., PETSC_FALSE, NULL));
2928b7f5c055SJed Brown     PetscKernel_v_gets_v_minus_A_times_w_3(y, J, res);
2929b7f5c055SJed Brown   }
2930b7f5c055SJed Brown   for (PetscInt i = 0; i < 3; i++) x[i] = y[i];
2931b7f5c055SJed Brown   PetscFunctionReturn(0);
2932b7f5c055SJed Brown }
2933b7f5c055SJed Brown 
2934b7f5c055SJed Brown const char *const DMPlexTPSTypes[] = {"SCHWARZ_P", "GYROID", "DMPlexTPSType", "DMPLEX_TPS_", NULL};
2935b7f5c055SJed Brown 
2936d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateTPSMesh_Internal(DM dm, DMPlexTPSType tpstype, const PetscInt extent[], const DMBoundaryType periodic[], PetscBool tps_distribute, PetscInt refinements, PetscInt layers, PetscReal thickness)
2937d71ae5a4SJacob Faibussowitsch {
2938b7f5c055SJed Brown   PetscMPIInt rank;
2939b7f5c055SJed Brown   PetscInt    topoDim = 2, spaceDim = 3, numFaces = 0, numVertices = 0, numEdges = 0;
2940b7f5c055SJed Brown   PetscInt(*edges)[2] = NULL, *edgeSets = NULL;
2941b7f5c055SJed Brown   PetscInt            *cells_flat = NULL;
2942b7f5c055SJed Brown   PetscReal           *vtxCoords  = NULL;
2943b7f5c055SJed Brown   TPSEvaluateFunc      evalFunc   = NULL;
29444663dae6SJed Brown   PetscSimplePointFunc normalFunc = NULL;
2945b7f5c055SJed Brown   DMLabel              label;
2946b7f5c055SJed Brown 
2947b7f5c055SJed Brown   PetscFunctionBegin;
29489566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
294963a3b9bcSJacob 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);
2950b7f5c055SJed Brown   switch (tpstype) {
2951b7f5c055SJed Brown   case DMPLEX_TPS_SCHWARZ_P:
2952b7f5c055SJed 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");
2953c5853193SPierre Jolivet     if (rank == 0) {
2954b7f5c055SJed Brown       PetscInt(*cells)[6][4][4] = NULL; // [junction, junction-face, cell, conn]
2955b7f5c055SJed Brown       PetscInt  Njunctions = 0, Ncuts = 0, Npipes[3], vcount;
2956b7f5c055SJed Brown       PetscReal L = 1;
2957b7f5c055SJed Brown 
2958b7f5c055SJed Brown       Npipes[0]   = (extent[0] + 1) * extent[1] * extent[2];
2959b7f5c055SJed Brown       Npipes[1]   = extent[0] * (extent[1] + 1) * extent[2];
2960b7f5c055SJed Brown       Npipes[2]   = extent[0] * extent[1] * (extent[2] + 1);
2961b7f5c055SJed Brown       Njunctions  = extent[0] * extent[1] * extent[2];
2962b7f5c055SJed Brown       Ncuts       = 2 * (extent[0] * extent[1] + extent[1] * extent[2] + extent[2] * extent[0]);
2963b7f5c055SJed Brown       numVertices = 4 * (Npipes[0] + Npipes[1] + Npipes[2]) + 8 * Njunctions;
29649566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(3 * numVertices, &vtxCoords));
29659566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Njunctions, &cells));
29669566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Ncuts * 4, &edges));
29679566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Ncuts * 4, &edgeSets));
2968b7f5c055SJed Brown       // x-normal pipes
2969b7f5c055SJed Brown       vcount = 0;
2970b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0] + 1; i++) {
2971b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
2972b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2]; k++) {
2973b7f5c055SJed Brown             for (PetscInt l = 0; l < 4; l++) {
2974b7f5c055SJed Brown               vtxCoords[vcount++] = (2 * i - 1) * L;
2975b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * j * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
2976b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * k * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
2977b7f5c055SJed Brown             }
2978b7f5c055SJed Brown           }
2979b7f5c055SJed Brown         }
2980b7f5c055SJed Brown       }
2981b7f5c055SJed Brown       // y-normal pipes
2982b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0]; i++) {
2983b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1] + 1; j++) {
2984b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2]; k++) {
2985b7f5c055SJed Brown             for (PetscInt l = 0; l < 4; l++) {
2986b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * i * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
2987b7f5c055SJed Brown               vtxCoords[vcount++] = (2 * j - 1) * L;
2988b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * k * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
2989b7f5c055SJed Brown             }
2990b7f5c055SJed Brown           }
2991b7f5c055SJed Brown         }
2992b7f5c055SJed Brown       }
2993b7f5c055SJed Brown       // z-normal pipes
2994b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0]; i++) {
2995b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
2996b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2] + 1; k++) {
2997b7f5c055SJed Brown             for (PetscInt l = 0; l < 4; l++) {
2998b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * i * L + PetscCosReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
2999b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * j * L + PetscSinReal((2 * l + 1) * PETSC_PI / 4) * L / 2;
3000b7f5c055SJed Brown               vtxCoords[vcount++] = (2 * k - 1) * L;
3001b7f5c055SJed Brown             }
3002b7f5c055SJed Brown           }
3003b7f5c055SJed Brown         }
3004b7f5c055SJed Brown       }
3005b7f5c055SJed Brown       // junctions
3006b7f5c055SJed Brown       for (PetscInt i = 0; i < extent[0]; i++) {
3007b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3008b7f5c055SJed Brown           for (PetscInt k = 0; k < extent[2]; k++) {
3009b7f5c055SJed Brown             const PetscInt J = (i * extent[1] + j) * extent[2] + k, Jvoff = (Npipes[0] + Npipes[1] + Npipes[2]) * 4 + J * 8;
3010b7f5c055SJed Brown             PetscCheck(vcount / 3 == Jvoff, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unexpected vertex count");
3011b7f5c055SJed Brown             for (PetscInt ii = 0; ii < 2; ii++) {
3012b7f5c055SJed Brown               for (PetscInt jj = 0; jj < 2; jj++) {
3013b7f5c055SJed Brown                 for (PetscInt kk = 0; kk < 2; kk++) {
3014b7f5c055SJed Brown                   double Ls           = (1 - sqrt(2) / 4) * L;
3015b7f5c055SJed Brown                   vtxCoords[vcount++] = 2 * i * L + (2 * ii - 1) * Ls;
3016b7f5c055SJed Brown                   vtxCoords[vcount++] = 2 * j * L + (2 * jj - 1) * Ls;
3017b7f5c055SJed Brown                   vtxCoords[vcount++] = 2 * k * L + (2 * kk - 1) * Ls;
3018b7f5c055SJed Brown                 }
3019b7f5c055SJed Brown               }
3020b7f5c055SJed Brown             }
3021b7f5c055SJed Brown             const PetscInt jfaces[3][2][4] = {
3022b7f5c055SJed Brown               {{3, 1, 0, 2}, {7, 5, 4, 6}}, // x-aligned
3023b7f5c055SJed Brown               {{5, 4, 0, 1}, {7, 6, 2, 3}}, // y-aligned
3024b7f5c055SJed Brown               {{6, 2, 0, 4}, {7, 3, 1, 5}}  // z-aligned
3025b7f5c055SJed Brown             };
3026b7f5c055SJed Brown             const PetscInt pipe_lo[3] = {// vertex numbers of pipes
30279371c9d4SSatish 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};
3028b7f5c055SJed Brown             const PetscInt pipe_hi[3] = {// vertex numbers of pipes
30299371c9d4SSatish 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};
3030b7f5c055SJed Brown             for (PetscInt dir = 0; dir < 3; dir++) { // x,y,z
3031b7f5c055SJed Brown               const PetscInt ijk[3] = {i, j, k};
3032b7f5c055SJed Brown               for (PetscInt l = 0; l < 4; l++) { // rotations
3033b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][0] = pipe_lo[dir] + l;
3034b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][1] = Jvoff + jfaces[dir][0][l];
3035b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][2] = Jvoff + jfaces[dir][0][(l - 1 + 4) % 4];
3036b7f5c055SJed Brown                 cells[J][dir * 2 + 0][l][3] = pipe_lo[dir] + (l - 1 + 4) % 4;
3037b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][0] = Jvoff + jfaces[dir][1][l];
3038b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][1] = pipe_hi[dir] + l;
3039b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][2] = pipe_hi[dir] + (l - 1 + 4) % 4;
3040b7f5c055SJed Brown                 cells[J][dir * 2 + 1][l][3] = Jvoff + jfaces[dir][1][(l - 1 + 4) % 4];
3041b7f5c055SJed Brown                 if (ijk[dir] == 0) {
3042b7f5c055SJed Brown                   edges[numEdges][0] = pipe_lo[dir] + l;
3043b7f5c055SJed Brown                   edges[numEdges][1] = pipe_lo[dir] + (l + 1) % 4;
3044b7f5c055SJed Brown                   edgeSets[numEdges] = dir * 2 + 1;
3045b7f5c055SJed Brown                   numEdges++;
3046b7f5c055SJed Brown                 }
3047b7f5c055SJed Brown                 if (ijk[dir] + 1 == extent[dir]) {
3048b7f5c055SJed Brown                   edges[numEdges][0] = pipe_hi[dir] + l;
3049b7f5c055SJed Brown                   edges[numEdges][1] = pipe_hi[dir] + (l + 1) % 4;
3050b7f5c055SJed Brown                   edgeSets[numEdges] = dir * 2 + 2;
3051b7f5c055SJed Brown                   numEdges++;
3052b7f5c055SJed Brown                 }
3053b7f5c055SJed Brown               }
3054b7f5c055SJed Brown             }
3055b7f5c055SJed Brown           }
3056b7f5c055SJed Brown         }
3057b7f5c055SJed Brown       }
305863a3b9bcSJacob Faibussowitsch       PetscCheck(numEdges == Ncuts * 4, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Edge count %" PetscInt_FMT " incompatible with number of cuts %" PetscInt_FMT, numEdges, Ncuts);
3059b7f5c055SJed Brown       numFaces   = 24 * Njunctions;
3060b7f5c055SJed Brown       cells_flat = cells[0][0][0];
3061b7f5c055SJed Brown     }
3062b7f5c055SJed Brown     evalFunc   = TPSEvaluate_SchwarzP;
30634663dae6SJed Brown     normalFunc = TPSExtrudeNormalFunc_SchwarzP;
3064b7f5c055SJed Brown     break;
3065b7f5c055SJed Brown   case DMPLEX_TPS_GYROID:
3066c5853193SPierre Jolivet     if (rank == 0) {
3067b7f5c055SJed Brown       // This is a coarse mesh approximation of the gyroid shifted to being the zero of the level set
3068b7f5c055SJed Brown       //
3069b7f5c055SJed 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)
3070b7f5c055SJed Brown       //
3071b7f5c055SJed Brown       // on the cell [0,2]^3.
3072b7f5c055SJed Brown       //
3073b7f5c055SJed Brown       // Think about dividing that cell into four columns, and focus on the column [0,1]x[0,1]x[0,2].
3074b7f5c055SJed Brown       // If you looked at the gyroid in that column at different slices of z you would see that it kind of spins
3075b7f5c055SJed Brown       // like a boomerang:
3076b7f5c055SJed Brown       //
3077b7f5c055SJed Brown       //     z = 0          z = 1/4        z = 1/2        z = 3/4     //
3078b7f5c055SJed Brown       //     -----          -------        -------        -------     //
3079b7f5c055SJed Brown       //                                                              //
3080b7f5c055SJed Brown       //     +       +      +       +      +       +      +   \   +   //
3081b7f5c055SJed Brown       //      \                                   /            \      //
3082b7f5c055SJed Brown       //       \            `-_   _-'            /              }     //
3083b7f5c055SJed Brown       //        *-_            `-'            _-'              /      //
3084b7f5c055SJed Brown       //     +     `-+      +       +      +-'     +      +   /   +   //
3085b7f5c055SJed Brown       //                                                              //
3086b7f5c055SJed Brown       //                                                              //
3087b7f5c055SJed Brown       //     z = 1          z = 5/4        z = 3/2        z = 7/4     //
3088b7f5c055SJed Brown       //     -----          -------        -------        -------     //
3089b7f5c055SJed Brown       //                                                              //
3090b7f5c055SJed Brown       //     +-_     +      +       +      +     _-+      +   /   +   //
3091b7f5c055SJed Brown       //        `-_            _-_            _-`            /        //
3092b7f5c055SJed Brown       //           \        _-'   `-_        /              {         //
3093b7f5c055SJed Brown       //            \                       /                \        //
3094b7f5c055SJed Brown       //     +       +      +       +      +       +      +   \   +   //
3095b7f5c055SJed Brown       //
3096b7f5c055SJed Brown       //
3097b7f5c055SJed Brown       // This course mesh approximates each of these slices by two line segments,
3098b7f5c055SJed Brown       // and then connects the segments in consecutive layers with quadrilateral faces.
3099b7f5c055SJed Brown       // All of the end points of the segments are multiples of 1/4 except for the
3100b7f5c055SJed Brown       // point * in the picture for z = 0 above and the similar points in other layers.
3101b7f5c055SJed Brown       // That point is at (gamma, gamma, 0), where gamma is calculated below.
3102b7f5c055SJed Brown       //
3103b7f5c055SJed Brown       // The column  [1,2]x[1,2]x[0,2] looks the same as this column;
3104b7f5c055SJed Brown       // The columns [1,2]x[0,1]x[0,2] and [0,1]x[1,2]x[0,2] are mirror images.
3105b7f5c055SJed Brown       //
3106b7f5c055SJed Brown       // As for how this method turned into the names given to the vertices:
3107b7f5c055SJed Brown       // that was not systematic, it was just the way it worked out in my handwritten notes.
3108b7f5c055SJed Brown 
3109b7f5c055SJed Brown       PetscInt facesPerBlock = 64;
3110b7f5c055SJed Brown       PetscInt vertsPerBlock = 56;
3111b7f5c055SJed Brown       PetscInt extentPlus[3];
3112b7f5c055SJed Brown       PetscInt numBlocks, numBlocksPlus;
31139371c9d4SSatish 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;
31149371c9d4SSatish Balay       const PetscInt pattern[64][4] = {
31159371c9d4SSatish Balay   /* face to vertex within the coarse discretization of a single gyroid block */
3116b7f5c055SJed Brown   /* layer 0 */
31179371c9d4SSatish Balay         {A,           C,           K,           G          },
31189371c9d4SSatish Balay         {C,           B,           II,          K          },
31199371c9d4SSatish Balay         {D,           A,           H,           L          },
31209371c9d4SSatish Balay         {B + 56 * 1,  D,           L,           J          },
31219371c9d4SSatish Balay         {E,           B + 56 * 1,  J,           N          },
31229371c9d4SSatish Balay         {A + 56 * 2,  E,           N,           H + 56 * 2 },
31239371c9d4SSatish Balay         {F,           A + 56 * 2,  G + 56 * 2,  M          },
31249371c9d4SSatish Balay         {B,           F,           M,           II         },
3125b7f5c055SJed Brown  /* layer 1 */
31269371c9d4SSatish Balay         {G,           K,           Q,           O          },
31279371c9d4SSatish Balay         {K,           II,          P,           Q          },
31289371c9d4SSatish Balay         {L,           H,           O + 56 * 1,  R          },
31299371c9d4SSatish Balay         {J,           L,           R,           P          },
31309371c9d4SSatish Balay         {N,           J,           P,           S          },
31319371c9d4SSatish Balay         {H + 56 * 2,  N,           S,           O + 56 * 3 },
31329371c9d4SSatish Balay         {M,           G + 56 * 2,  O + 56 * 2,  T          },
31339371c9d4SSatish Balay         {II,          M,           T,           P          },
3134b7f5c055SJed Brown  /* layer 2 */
31359371c9d4SSatish Balay         {O,           Q,           Y,           U          },
31369371c9d4SSatish Balay         {Q,           P,           W,           Y          },
31379371c9d4SSatish Balay         {R,           O + 56 * 1,  U + 56 * 1,  Ap         },
31389371c9d4SSatish Balay         {P,           R,           Ap,          W          },
31399371c9d4SSatish Balay         {S,           P,           X,           Bp         },
31409371c9d4SSatish Balay         {O + 56 * 3,  S,           Bp,          V + 56 * 1 },
31419371c9d4SSatish Balay         {T,           O + 56 * 2,  V,           Z          },
31429371c9d4SSatish Balay         {P,           T,           Z,           X          },
3143b7f5c055SJed Brown  /* layer 3 */
31449371c9d4SSatish Balay         {U,           Y,           Ep,          Dp         },
31459371c9d4SSatish Balay         {Y,           W,           Cp,          Ep         },
31469371c9d4SSatish Balay         {Ap,          U + 56 * 1,  Dp + 56 * 1, Gp         },
31479371c9d4SSatish Balay         {W,           Ap,          Gp,          Cp         },
31489371c9d4SSatish Balay         {Bp,          X,           Cp + 56 * 2, Fp         },
31499371c9d4SSatish Balay         {V + 56 * 1,  Bp,          Fp,          Dp + 56 * 1},
31509371c9d4SSatish Balay         {Z,           V,           Dp,          Hp         },
31519371c9d4SSatish Balay         {X,           Z,           Hp,          Cp + 56 * 2},
3152b7f5c055SJed Brown  /* layer 4 */
31539371c9d4SSatish Balay         {Dp,          Ep,          Mp,          Kp         },
31549371c9d4SSatish Balay         {Ep,          Cp,          Ip,          Mp         },
31559371c9d4SSatish Balay         {Gp,          Dp + 56 * 1, Lp,          Np         },
31569371c9d4SSatish Balay         {Cp,          Gp,          Np,          Jp         },
31579371c9d4SSatish Balay         {Fp,          Cp + 56 * 2, Jp + 56 * 2, Pp         },
31589371c9d4SSatish Balay         {Dp + 56 * 1, Fp,          Pp,          Lp         },
31599371c9d4SSatish Balay         {Hp,          Dp,          Kp,          Op         },
31609371c9d4SSatish Balay         {Cp + 56 * 2, Hp,          Op,          Ip + 56 * 2},
3161b7f5c055SJed Brown  /* layer 5 */
31629371c9d4SSatish Balay         {Kp,          Mp,          Sp,          Rp         },
31639371c9d4SSatish Balay         {Mp,          Ip,          Qp,          Sp         },
31649371c9d4SSatish Balay         {Np,          Lp,          Rp,          Tp         },
31659371c9d4SSatish Balay         {Jp,          Np,          Tp,          Qp + 56 * 1},
31669371c9d4SSatish Balay         {Pp,          Jp + 56 * 2, Qp + 56 * 3, Up         },
31679371c9d4SSatish Balay         {Lp,          Pp,          Up,          Rp         },
31689371c9d4SSatish Balay         {Op,          Kp,          Rp,          Vp         },
31699371c9d4SSatish Balay         {Ip + 56 * 2, Op,          Vp,          Qp + 56 * 2},
3170b7f5c055SJed Brown  /* layer 6 */
31719371c9d4SSatish Balay         {Rp,          Sp,          Aq,          Yp         },
31729371c9d4SSatish Balay         {Sp,          Qp,          Wp,          Aq         },
31739371c9d4SSatish Balay         {Tp,          Rp,          Yp,          Cq         },
31749371c9d4SSatish Balay         {Qp + 56 * 1, Tp,          Cq,          Wp + 56 * 1},
31759371c9d4SSatish Balay         {Up,          Qp + 56 * 3, Xp + 56 * 1, Dq         },
31769371c9d4SSatish Balay         {Rp,          Up,          Dq,          Zp         },
31779371c9d4SSatish Balay         {Vp,          Rp,          Zp,          Bq         },
31789371c9d4SSatish Balay         {Qp + 56 * 2, Vp,          Bq,          Xp         },
3179b7f5c055SJed Brown  /* layer 7 (the top is the periodic image of the bottom of layer 0) */
31809371c9d4SSatish Balay         {Yp,          Aq,          C + 56 * 4,  A + 56 * 4 },
31819371c9d4SSatish Balay         {Aq,          Wp,          B + 56 * 4,  C + 56 * 4 },
31829371c9d4SSatish Balay         {Cq,          Yp,          A + 56 * 4,  D + 56 * 4 },
31839371c9d4SSatish Balay         {Wp + 56 * 1, Cq,          D + 56 * 4,  B + 56 * 5 },
31849371c9d4SSatish Balay         {Dq,          Xp + 56 * 1, B + 56 * 5,  E + 56 * 4 },
31859371c9d4SSatish Balay         {Zp,          Dq,          E + 56 * 4,  A + 56 * 6 },
31869371c9d4SSatish Balay         {Bq,          Zp,          A + 56 * 6,  F + 56 * 4 },
31879371c9d4SSatish Balay         {Xp,          Bq,          F + 56 * 4,  B + 56 * 4 }
3188b7f5c055SJed Brown       };
3189b7f5c055SJed Brown       const PetscReal gamma                = PetscAcosReal((PetscSqrtReal(3.) - 1.) / PetscSqrtReal(2.)) / PETSC_PI;
31909371c9d4SSatish Balay       const PetscReal patternCoords[56][3] = {
3191bee3fc89SBarry Smith         {1.,        0.,        0.  }, /* A  */
3192bee3fc89SBarry Smith         {0.,        1.,        0.  }, /* B  */
3193bee3fc89SBarry Smith         {gamma,     gamma,     0.  }, /* C  */
3194bee3fc89SBarry Smith         {1 + gamma, 1 - gamma, 0.  }, /* D  */
3195bee3fc89SBarry Smith         {2 - gamma, 2 - gamma, 0.  }, /* E  */
3196bee3fc89SBarry Smith         {1 - gamma, 1 + gamma, 0.  }, /* F  */
3197b7f5c055SJed Brown 
3198bee3fc89SBarry Smith         {.5,        0,         .25 }, /* G  */
3199bee3fc89SBarry Smith         {1.5,       0.,        .25 }, /* H  */
3200bee3fc89SBarry Smith         {.5,        1.,        .25 }, /* II */
3201bee3fc89SBarry Smith         {1.5,       1.,        .25 }, /* J  */
3202bee3fc89SBarry Smith         {.25,       .5,        .25 }, /* K  */
3203bee3fc89SBarry Smith         {1.25,      .5,        .25 }, /* L  */
3204bee3fc89SBarry Smith         {.75,       1.5,       .25 }, /* M  */
3205bee3fc89SBarry Smith         {1.75,      1.5,       .25 }, /* N  */
3206b7f5c055SJed Brown 
3207bee3fc89SBarry Smith         {0.,        0.,        .5  }, /* O  */
3208bee3fc89SBarry Smith         {1.,        1.,        .5  }, /* P  */
3209bee3fc89SBarry Smith         {gamma,     1 - gamma, .5  }, /* Q  */
3210bee3fc89SBarry Smith         {1 + gamma, gamma,     .5  }, /* R  */
3211bee3fc89SBarry Smith         {2 - gamma, 1 + gamma, .5  }, /* S  */
3212bee3fc89SBarry Smith         {1 - gamma, 2 - gamma, .5  }, /* T  */
3213b7f5c055SJed Brown 
3214bee3fc89SBarry Smith         {0.,        .5,        .75 }, /* U  */
3215bee3fc89SBarry Smith         {0.,        1.5,       .75 }, /* V  */
3216bee3fc89SBarry Smith         {1.,        .5,        .75 }, /* W  */
3217bee3fc89SBarry Smith         {1.,        1.5,       .75 }, /* X  */
3218bee3fc89SBarry Smith         {.5,        .75,       .75 }, /* Y  */
3219bee3fc89SBarry Smith         {.5,        1.75,      .75 }, /* Z  */
3220bee3fc89SBarry Smith         {1.5,       .25,       .75 }, /* Ap */
3221bee3fc89SBarry Smith         {1.5,       1.25,      .75 }, /* Bp */
3222b7f5c055SJed Brown 
3223bee3fc89SBarry Smith         {1.,        0.,        1.  }, /* Cp */
3224bee3fc89SBarry Smith         {0.,        1.,        1.  }, /* Dp */
3225bee3fc89SBarry Smith         {1 - gamma, 1 - gamma, 1.  }, /* Ep */
3226bee3fc89SBarry Smith         {1 + gamma, 1 + gamma, 1.  }, /* Fp */
3227bee3fc89SBarry Smith         {2 - gamma, gamma,     1.  }, /* Gp */
3228bee3fc89SBarry Smith         {gamma,     2 - gamma, 1.  }, /* Hp */
3229b7f5c055SJed Brown 
3230bee3fc89SBarry Smith         {.5,        0.,        1.25}, /* Ip */
3231bee3fc89SBarry Smith         {1.5,       0.,        1.25}, /* Jp */
3232bee3fc89SBarry Smith         {.5,        1.,        1.25}, /* Kp */
3233bee3fc89SBarry Smith         {1.5,       1.,        1.25}, /* Lp */
3234bee3fc89SBarry Smith         {.75,       .5,        1.25}, /* Mp */
3235bee3fc89SBarry Smith         {1.75,      .5,        1.25}, /* Np */
3236bee3fc89SBarry Smith         {.25,       1.5,       1.25}, /* Op */
3237bee3fc89SBarry Smith         {1.25,      1.5,       1.25}, /* Pp */
3238b7f5c055SJed Brown 
3239bee3fc89SBarry Smith         {0.,        0.,        1.5 }, /* Qp */
3240bee3fc89SBarry Smith         {1.,        1.,        1.5 }, /* Rp */
3241bee3fc89SBarry Smith         {1 - gamma, gamma,     1.5 }, /* Sp */
3242bee3fc89SBarry Smith         {2 - gamma, 1 - gamma, 1.5 }, /* Tp */
3243bee3fc89SBarry Smith         {1 + gamma, 2 - gamma, 1.5 }, /* Up */
3244bee3fc89SBarry Smith         {gamma,     1 + gamma, 1.5 }, /* Vp */
3245b7f5c055SJed Brown 
3246bee3fc89SBarry Smith         {0.,        .5,        1.75}, /* Wp */
3247bee3fc89SBarry Smith         {0.,        1.5,       1.75}, /* Xp */
3248bee3fc89SBarry Smith         {1.,        .5,        1.75}, /* Yp */
3249bee3fc89SBarry Smith         {1.,        1.5,       1.75}, /* Zp */
3250bee3fc89SBarry Smith         {.5,        .25,       1.75}, /* Aq */
3251bee3fc89SBarry Smith         {.5,        1.25,      1.75}, /* Bq */
3252bee3fc89SBarry Smith         {1.5,       .75,       1.75}, /* Cq */
3253bee3fc89SBarry Smith         {1.5,       1.75,      1.75}, /* Dq */
3254b7f5c055SJed Brown       };
3255b7f5c055SJed Brown       PetscInt(*cells)[64][4] = NULL;
3256b7f5c055SJed Brown       PetscBool *seen;
3257b7f5c055SJed Brown       PetscInt  *vertToTrueVert;
3258b7f5c055SJed Brown       PetscInt   count;
3259b7f5c055SJed Brown 
3260b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) extentPlus[i] = extent[i] + 1;
3261b7f5c055SJed Brown       numBlocks = 1;
3262b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) numBlocks *= extent[i];
3263b7f5c055SJed Brown       numBlocksPlus = 1;
3264b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) numBlocksPlus *= extentPlus[i];
3265b7f5c055SJed Brown       numFaces = numBlocks * facesPerBlock;
32669566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numBlocks, &cells));
32679566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numBlocksPlus * vertsPerBlock, &seen));
3268b7f5c055SJed Brown       for (PetscInt k = 0; k < extent[2]; k++) {
3269b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
3270b7f5c055SJed Brown           for (PetscInt i = 0; i < extent[0]; i++) {
3271b7f5c055SJed Brown             for (PetscInt f = 0; f < facesPerBlock; f++) {
3272b7f5c055SJed Brown               for (PetscInt v = 0; v < 4; v++) {
3273b7f5c055SJed Brown                 PetscInt vertRaw     = pattern[f][v];
3274b7f5c055SJed Brown                 PetscInt blockidx    = vertRaw / 56;
3275b7f5c055SJed Brown                 PetscInt patternvert = vertRaw % 56;
3276b7f5c055SJed Brown                 PetscInt xplus       = (blockidx & 1);
3277b7f5c055SJed Brown                 PetscInt yplus       = (blockidx & 2) >> 1;
3278b7f5c055SJed Brown                 PetscInt zplus       = (blockidx & 4) >> 2;
3279b7f5c055SJed Brown                 PetscInt zcoord      = (periodic && periodic[2] == DM_BOUNDARY_PERIODIC) ? ((k + zplus) % extent[2]) : (k + zplus);
3280b7f5c055SJed Brown                 PetscInt ycoord      = (periodic && periodic[1] == DM_BOUNDARY_PERIODIC) ? ((j + yplus) % extent[1]) : (j + yplus);
3281b7f5c055SJed Brown                 PetscInt xcoord      = (periodic && periodic[0] == DM_BOUNDARY_PERIODIC) ? ((i + xplus) % extent[0]) : (i + xplus);
3282b7f5c055SJed Brown                 PetscInt vert        = ((zcoord * extentPlus[1] + ycoord) * extentPlus[0] + xcoord) * 56 + patternvert;
3283b7f5c055SJed Brown 
3284b7f5c055SJed Brown                 cells[(k * extent[1] + j) * extent[0] + i][f][v] = vert;
3285b7f5c055SJed Brown                 seen[vert]                                       = PETSC_TRUE;
3286b7f5c055SJed Brown               }
3287b7f5c055SJed Brown             }
3288b7f5c055SJed Brown           }
3289b7f5c055SJed Brown         }
3290b7f5c055SJed Brown       }
32919371c9d4SSatish Balay       for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++)
32929371c9d4SSatish Balay         if (seen[i]) numVertices++;
3293b7f5c055SJed Brown       count = 0;
32949566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numBlocksPlus * vertsPerBlock, &vertToTrueVert));
32959566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numVertices * 3, &vtxCoords));
3296b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++) vertToTrueVert[i] = -1;
3297b7f5c055SJed Brown       for (PetscInt k = 0; k < extentPlus[2]; k++) {
3298b7f5c055SJed Brown         for (PetscInt j = 0; j < extentPlus[1]; j++) {
3299b7f5c055SJed Brown           for (PetscInt i = 0; i < extentPlus[0]; i++) {
3300b7f5c055SJed Brown             for (PetscInt v = 0; v < vertsPerBlock; v++) {
3301b7f5c055SJed Brown               PetscInt vIdx = ((k * extentPlus[1] + j) * extentPlus[0] + i) * vertsPerBlock + v;
3302b7f5c055SJed Brown 
3303b7f5c055SJed Brown               if (seen[vIdx]) {
3304b7f5c055SJed Brown                 PetscInt thisVert;
3305b7f5c055SJed Brown 
3306b7f5c055SJed Brown                 vertToTrueVert[vIdx] = thisVert = count++;
3307b7f5c055SJed Brown 
3308b7f5c055SJed Brown                 for (PetscInt d = 0; d < 3; d++) vtxCoords[3 * thisVert + d] = patternCoords[v][d];
3309b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 0] += i * 2;
3310b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 1] += j * 2;
3311b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 2] += k * 2;
3312b7f5c055SJed Brown               }
3313b7f5c055SJed Brown             }
3314b7f5c055SJed Brown           }
3315b7f5c055SJed Brown         }
3316b7f5c055SJed Brown       }
3317b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocks; i++) {
3318b7f5c055SJed Brown         for (PetscInt f = 0; f < facesPerBlock; f++) {
3319ad540459SPierre Jolivet           for (PetscInt v = 0; v < 4; v++) cells[i][f][v] = vertToTrueVert[cells[i][f][v]];
3320b7f5c055SJed Brown         }
3321b7f5c055SJed Brown       }
33229566063dSJacob Faibussowitsch       PetscCall(PetscFree(vertToTrueVert));
33239566063dSJacob Faibussowitsch       PetscCall(PetscFree(seen));
3324b7f5c055SJed Brown       cells_flat = cells[0][0];
3325b7f5c055SJed Brown       numEdges   = 0;
3326b7f5c055SJed Brown       for (PetscInt i = 0; i < numFaces; i++) {
3327b7f5c055SJed Brown         for (PetscInt e = 0; e < 4; e++) {
3328b7f5c055SJed Brown           PetscInt         ev[]       = {cells_flat[i * 4 + e], cells_flat[i * 4 + ((e + 1) % 4)]};
3329b7f5c055SJed Brown           const PetscReal *evCoords[] = {&vtxCoords[3 * ev[0]], &vtxCoords[3 * ev[1]]};
3330b7f5c055SJed Brown 
3331b7f5c055SJed Brown           for (PetscInt d = 0; d < 3; d++) {
3332b7f5c055SJed Brown             if (!periodic || periodic[0] != DM_BOUNDARY_PERIODIC) {
3333b7f5c055SJed Brown               if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) numEdges++;
3334b7f5c055SJed Brown               if (evCoords[0][d] == 2. * extent[d] && evCoords[1][d] == 2. * extent[d]) numEdges++;
3335b7f5c055SJed Brown             }
3336b7f5c055SJed Brown           }
3337b7f5c055SJed Brown         }
3338b7f5c055SJed Brown       }
33399566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numEdges, &edges));
33409566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numEdges, &edgeSets));
3341b7f5c055SJed Brown       for (PetscInt edge = 0, i = 0; i < numFaces; i++) {
3342b7f5c055SJed Brown         for (PetscInt e = 0; e < 4; e++) {
3343b7f5c055SJed Brown           PetscInt         ev[]       = {cells_flat[i * 4 + e], cells_flat[i * 4 + ((e + 1) % 4)]};
3344b7f5c055SJed Brown           const PetscReal *evCoords[] = {&vtxCoords[3 * ev[0]], &vtxCoords[3 * ev[1]]};
3345b7f5c055SJed Brown 
3346b7f5c055SJed Brown           for (PetscInt d = 0; d < 3; d++) {
3347b7f5c055SJed Brown             if (!periodic || periodic[d] != DM_BOUNDARY_PERIODIC) {
3348b7f5c055SJed Brown               if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) {
3349b7f5c055SJed Brown                 edges[edge][0]   = ev[0];
3350b7f5c055SJed Brown                 edges[edge][1]   = ev[1];
3351b7f5c055SJed Brown                 edgeSets[edge++] = 2 * d;
3352b7f5c055SJed Brown               }
3353b7f5c055SJed Brown               if (evCoords[0][d] == 2. * extent[d] && evCoords[1][d] == 2. * extent[d]) {
3354b7f5c055SJed Brown                 edges[edge][0]   = ev[0];
3355b7f5c055SJed Brown                 edges[edge][1]   = ev[1];
3356b7f5c055SJed Brown                 edgeSets[edge++] = 2 * d + 1;
3357b7f5c055SJed Brown               }
3358b7f5c055SJed Brown             }
3359b7f5c055SJed Brown           }
3360b7f5c055SJed Brown         }
3361b7f5c055SJed Brown       }
3362b7f5c055SJed Brown     }
3363b7f5c055SJed Brown     evalFunc   = TPSEvaluate_Gyroid;
33644663dae6SJed Brown     normalFunc = TPSExtrudeNormalFunc_Gyroid;
3365b7f5c055SJed Brown     break;
3366b7f5c055SJed Brown   }
3367b7f5c055SJed Brown 
33689566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, topoDim));
3369c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildFromCellList(dm, numFaces, numVertices, 4, cells_flat));
33709566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildFromCellList(dm, 0, 0, 0, NULL));
33719566063dSJacob Faibussowitsch   PetscCall(PetscFree(cells_flat));
3372b7f5c055SJed Brown   {
3373b7f5c055SJed Brown     DM idm;
33749566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(dm, &idm));
337569d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &idm));
3376b7f5c055SJed Brown   }
3377c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildCoordinatesFromCellList(dm, spaceDim, vtxCoords));
33789566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildCoordinatesFromCellList(dm, spaceDim, NULL));
33799566063dSJacob Faibussowitsch   PetscCall(PetscFree(vtxCoords));
3380b7f5c055SJed Brown 
33819566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
33829566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "Face Sets", &label));
3383b7f5c055SJed Brown   for (PetscInt e = 0; e < numEdges; e++) {
3384b7f5c055SJed Brown     PetscInt        njoin;
3385b7f5c055SJed Brown     const PetscInt *join, verts[] = {numFaces + edges[e][0], numFaces + edges[e][1]};
33869566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, 2, verts, &njoin, &join));
338763a3b9bcSJacob 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]);
33889566063dSJacob Faibussowitsch     PetscCall(DMLabelSetValue(label, join[0], edgeSets[e]));
33899566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, 2, verts, &njoin, &join));
3390b7f5c055SJed Brown   }
33919566063dSJacob Faibussowitsch   PetscCall(PetscFree(edges));
33929566063dSJacob Faibussowitsch   PetscCall(PetscFree(edgeSets));
33931436d7faSJed Brown   if (tps_distribute) {
33941436d7faSJed Brown     DM               pdm = NULL;
33951436d7faSJed Brown     PetscPartitioner part;
33961436d7faSJed Brown 
33979566063dSJacob Faibussowitsch     PetscCall(DMPlexGetPartitioner(dm, &part));
33989566063dSJacob Faibussowitsch     PetscCall(PetscPartitionerSetFromOptions(part));
33999566063dSJacob Faibussowitsch     PetscCall(DMPlexDistribute(dm, 0, NULL, &pdm));
340048a46eb9SPierre Jolivet     if (pdm) PetscCall(DMPlexReplace_Internal(dm, &pdm));
34011436d7faSJed Brown     // Do not auto-distribute again
34029566063dSJacob Faibussowitsch     PetscCall(DMPlexDistributeSetDefault(dm, PETSC_FALSE));
34031436d7faSJed Brown   }
3404b7f5c055SJed Brown 
34059566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
3406b7f5c055SJed Brown   for (PetscInt refine = 0; refine < refinements; refine++) {
3407b7f5c055SJed Brown     PetscInt     m;
3408b7f5c055SJed Brown     DM           dmf;
3409b7f5c055SJed Brown     Vec          X;
3410b7f5c055SJed Brown     PetscScalar *x;
34119566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, MPI_COMM_NULL, &dmf));
341269d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &dmf));
3413b7f5c055SJed Brown 
34149566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &X));
34159566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(X, &m));
34169566063dSJacob Faibussowitsch     PetscCall(VecGetArray(X, &x));
341748a46eb9SPierre Jolivet     for (PetscInt i = 0; i < m; i += 3) PetscCall(TPSNearestPoint(evalFunc, &x[i]));
34189566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(X, &x));
3419b7f5c055SJed Brown   }
3420b7f5c055SJed Brown 
3421b7f5c055SJed Brown   // Face Sets has already been propagated to new vertices during refinement; this propagates to the initial vertices.
34229566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "Face Sets", &label));
34239566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete(dm, label));
3424b7f5c055SJed Brown 
3425b7f5c055SJed Brown   if (thickness > 0) {
34264663dae6SJed Brown     DM              edm, cdm, ecdm;
34274663dae6SJed Brown     DMPlexTransform tr;
34284663dae6SJed Brown     const char     *prefix;
34294663dae6SJed Brown     PetscOptions    options;
34304663dae6SJed Brown     // Code from DMPlexExtrude
34314663dae6SJed Brown     PetscCall(DMPlexTransformCreate(PetscObjectComm((PetscObject)dm), &tr));
34324663dae6SJed Brown     PetscCall(DMPlexTransformSetDM(tr, dm));
34334663dae6SJed Brown     PetscCall(DMPlexTransformSetType(tr, DMPLEXEXTRUDE));
34344663dae6SJed Brown     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm, &prefix));
34354663dae6SJed Brown     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)tr, prefix));
34364663dae6SJed Brown     PetscCall(PetscObjectGetOptions((PetscObject)dm, &options));
34374663dae6SJed Brown     PetscCall(PetscObjectSetOptions((PetscObject)tr, options));
34384663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetLayers(tr, layers));
34394663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetThickness(tr, thickness));
34404663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetTensor(tr, PETSC_FALSE));
34414663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetSymmetric(tr, PETSC_TRUE));
34424663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetNormalFunction(tr, normalFunc));
34434663dae6SJed Brown     PetscCall(DMPlexTransformSetFromOptions(tr));
34444663dae6SJed Brown     PetscCall(PetscObjectSetOptions((PetscObject)tr, NULL));
34454663dae6SJed Brown     PetscCall(DMPlexTransformSetUp(tr));
34464663dae6SJed Brown     PetscCall(PetscObjectViewFromOptions((PetscObject)tr, NULL, "-dm_plex_tps_transform_view"));
34474663dae6SJed Brown     PetscCall(DMPlexTransformApply(tr, dm, &edm));
34484663dae6SJed Brown     PetscCall(DMCopyDisc(dm, edm));
34494663dae6SJed Brown     PetscCall(DMGetCoordinateDM(dm, &cdm));
34504663dae6SJed Brown     PetscCall(DMGetCoordinateDM(edm, &ecdm));
34514663dae6SJed Brown     PetscCall(DMCopyDisc(cdm, ecdm));
34524663dae6SJed Brown     PetscCall(DMPlexTransformCreateDiscLabels(tr, edm));
34534663dae6SJed Brown     PetscCall(DMPlexTransformDestroy(&tr));
34544663dae6SJed Brown     if (edm) {
34554663dae6SJed Brown       ((DM_Plex *)edm->data)->printFEM    = ((DM_Plex *)dm->data)->printFEM;
34564663dae6SJed Brown       ((DM_Plex *)edm->data)->printL2     = ((DM_Plex *)dm->data)->printL2;
3457f5867de0SMatthew G. Knepley       ((DM_Plex *)edm->data)->printLocate = ((DM_Plex *)dm->data)->printLocate;
34584663dae6SJed Brown     }
345969d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &edm));
3460b7f5c055SJed Brown   }
3461b7f5c055SJed Brown   PetscFunctionReturn(0);
3462b7f5c055SJed Brown }
3463b7f5c055SJed Brown 
3464b7f5c055SJed Brown /*@
3465b7f5c055SJed Brown   DMPlexCreateTPSMesh - Create a distributed, interpolated mesh of a triply-periodic surface
3466b7f5c055SJed Brown 
3467b7f5c055SJed Brown   Collective
3468b7f5c055SJed Brown 
3469b7f5c055SJed Brown   Input Parameters:
3470a1cb98faSBarry Smith + comm   - The communicator for the `DM` object
3471b7f5c055SJed Brown . tpstype - Type of triply-periodic surface
3472b7f5c055SJed Brown . extent - Array of length 3 containing number of periods in each direction
3473b7f5c055SJed Brown . periodic - array of length 3 with periodicity, or NULL for non-periodic
34741436d7faSJed Brown . tps_distribute - Distribute 2D manifold mesh prior to refinement and extrusion (more scalable)
3475817da375SSatish Balay . refinements - Number of factor-of-2 refinements of 2D manifold mesh
34761436d7faSJed Brown . layers - Number of cell layers extruded in normal direction
3477817da375SSatish Balay - thickness - Thickness in normal direction
3478b7f5c055SJed Brown 
3479b7f5c055SJed Brown   Output Parameter:
3480a1cb98faSBarry Smith . dm  - The `DM` object
3481a1cb98faSBarry Smith 
3482a1cb98faSBarry Smith   Level: beginner
3483b7f5c055SJed Brown 
3484b7f5c055SJed Brown   Notes:
3485b7f5c055SJed Brown   This meshes the surface of the Schwarz P or Gyroid surfaces.  Schwarz P is is the simplest member of the triply-periodic minimal surfaces.
3486b7f5c055SJed Brown   https://en.wikipedia.org/wiki/Schwarz_minimal_surface#Schwarz_P_(%22Primitive%22) and can be cut with "clean" boundaries.
3487b7f5c055SJed Brown   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.
3488b7f5c055SJed Brown   Our implementation creates a very coarse mesh of the surface and refines (by 4-way splitting) as many times as requested.
3489b7f5c055SJed Brown   On each refinement, all vertices are projected to their nearest point on the surface.
3490b7f5c055SJed Brown   This projection could readily be extended to related surfaces.
3491b7f5c055SJed Brown 
3492b7f5c055SJed Brown   The face (edge) sets for the Schwarz P surface are numbered 1(-x), 2(+x), 3(-y), 4(+y), 5(-z), 6(+z).
3493b7f5c055SJed Brown   When the mesh is refined, "Face Sets" contain the new vertices (created during refinement).  Use DMPlexLabelComplete() to propagate to coarse-level vertices.
3494b7f5c055SJed Brown 
3495a1cb98faSBarry Smith   Developer Note:
3496b7f5c055SJed Brown   The Gyroid mesh does not currently mark boundary sets.
3497b7f5c055SJed Brown 
3498a1cb98faSBarry Smith   References:
3499a1cb98faSBarry Smith . * - Maskery et al, Insights into the mechanical properties of several triply periodic minimal surface lattice structures made by polymer additive manufacturing, 2017.
3500a1cb98faSBarry Smith     https://doi.org/10.1016/j.polymer.2017.11.049
3501b7f5c055SJed Brown 
3502a1cb98faSBarry Smith .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSphereMesh()`, `DMSetType()`, `DMCreate()`
3503b7f5c055SJed Brown @*/
3504d71ae5a4SJacob 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)
3505d71ae5a4SJacob Faibussowitsch {
3506b7f5c055SJed Brown   PetscFunctionBegin;
35079566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
35089566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
35099566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateTPSMesh_Internal(*dm, tpstype, extent, periodic, tps_distribute, refinements, layers, thickness));
3510b7f5c055SJed Brown   PetscFunctionReturn(0);
3511b7f5c055SJed Brown }
3512b7f5c055SJed Brown 
35139318fe57SMatthew G. Knepley /*@
35149318fe57SMatthew G. Knepley   DMPlexCreateSphereMesh - Creates a mesh on the d-dimensional sphere, S^d.
35159318fe57SMatthew G. Knepley 
35169318fe57SMatthew G. Knepley   Collective
35179318fe57SMatthew G. Knepley 
35189318fe57SMatthew G. Knepley   Input Parameters:
3519a1cb98faSBarry Smith + comm    - The communicator for the `DM` object
35209318fe57SMatthew G. Knepley . dim     - The dimension
35219318fe57SMatthew G. Knepley . simplex - Use simplices, or tensor product cells
35229318fe57SMatthew G. Knepley - R       - The radius
35239318fe57SMatthew G. Knepley 
35249318fe57SMatthew G. Knepley   Output Parameter:
3525a1cb98faSBarry Smith . dm  - The `DM` object
35269318fe57SMatthew G. Knepley 
35279318fe57SMatthew G. Knepley   Level: beginner
35289318fe57SMatthew G. Knepley 
3529a1cb98faSBarry Smith .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreateBallMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
35309318fe57SMatthew G. Knepley @*/
3531d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateSphereMesh(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscReal R, DM *dm)
3532d71ae5a4SJacob Faibussowitsch {
35339318fe57SMatthew G. Knepley   PetscFunctionBegin;
35349318fe57SMatthew G. Knepley   PetscValidPointer(dm, 5);
35359566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
35369566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
35379566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSphereMesh_Internal(*dm, dim, simplex, R));
35389318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
35399318fe57SMatthew G. Knepley }
35409318fe57SMatthew G. Knepley 
3541d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBallMesh_Internal(DM dm, PetscInt dim, PetscReal R)
3542d71ae5a4SJacob Faibussowitsch {
35439318fe57SMatthew G. Knepley   DM      sdm, vol;
35449318fe57SMatthew G. Knepley   DMLabel bdlabel;
35459318fe57SMatthew G. Knepley 
35469318fe57SMatthew G. Knepley   PetscFunctionBegin;
35479566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject)dm), &sdm));
35489566063dSJacob Faibussowitsch   PetscCall(DMSetType(sdm, DMPLEX));
35499566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject)sdm, "bd_"));
35509566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSphereMesh_Internal(sdm, dim - 1, PETSC_TRUE, R));
35519566063dSJacob Faibussowitsch   PetscCall(DMSetFromOptions(sdm));
35529566063dSJacob Faibussowitsch   PetscCall(DMViewFromOptions(sdm, NULL, "-dm_view"));
35539566063dSJacob Faibussowitsch   PetscCall(DMPlexGenerate(sdm, NULL, PETSC_TRUE, &vol));
35549566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&sdm));
355569d8a87bSksagiyam   PetscCall(DMPlexReplace_Internal(dm, &vol));
35569566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
35579566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "marker", &bdlabel));
35589566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkBoundaryFaces(dm, PETSC_DETERMINE, bdlabel));
35599566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete(dm, bdlabel));
356051a74b61SMatthew G. Knepley   PetscFunctionReturn(0);
356151a74b61SMatthew G. Knepley }
356251a74b61SMatthew G. Knepley 
356351a74b61SMatthew G. Knepley /*@
356451a74b61SMatthew G. Knepley   DMPlexCreateBallMesh - Creates a simplex mesh on the d-dimensional ball, B^d.
356551a74b61SMatthew G. Knepley 
356651a74b61SMatthew G. Knepley   Collective
356751a74b61SMatthew G. Knepley 
356851a74b61SMatthew G. Knepley   Input Parameters:
3569a1cb98faSBarry Smith + comm  - The communicator for the `DM` object
357051a74b61SMatthew G. Knepley . dim   - The dimension
357151a74b61SMatthew G. Knepley - R     - The radius
357251a74b61SMatthew G. Knepley 
357351a74b61SMatthew G. Knepley   Output Parameter:
3574a1cb98faSBarry Smith . dm  - The `DM` object
357551a74b61SMatthew G. Knepley 
3576a1cb98faSBarry Smith   Options Database Key:
357751a74b61SMatthew G. Knepley - bd_dm_refine - This will refine the surface mesh preserving the sphere geometry
357851a74b61SMatthew G. Knepley 
357951a74b61SMatthew G. Knepley   Level: beginner
358051a74b61SMatthew G. Knepley 
3581a1cb98faSBarry Smith .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreateSphereMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
358251a74b61SMatthew G. Knepley @*/
3583d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateBallMesh(MPI_Comm comm, PetscInt dim, PetscReal R, DM *dm)
3584d71ae5a4SJacob Faibussowitsch {
358551a74b61SMatthew G. Knepley   PetscFunctionBegin;
35869566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
35879566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
35889566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBallMesh_Internal(*dm, dim, R));
35892829fed8SMatthew G. Knepley   PetscFunctionReturn(0);
35902829fed8SMatthew G. Knepley }
35912829fed8SMatthew G. Knepley 
3592d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateReferenceCell_Internal(DM rdm, DMPolytopeType ct)
3593d71ae5a4SJacob Faibussowitsch {
35940a6ba040SMatthew G. Knepley   PetscFunctionBegin;
35959318fe57SMatthew G. Knepley   switch (ct) {
35969371c9d4SSatish Balay   case DM_POLYTOPE_POINT: {
35979318fe57SMatthew G. Knepley     PetscInt    numPoints[1]        = {1};
35989318fe57SMatthew G. Knepley     PetscInt    coneSize[1]         = {0};
35999318fe57SMatthew G. Knepley     PetscInt    cones[1]            = {0};
36009318fe57SMatthew G. Knepley     PetscInt    coneOrientations[1] = {0};
36019318fe57SMatthew G. Knepley     PetscScalar vertexCoords[1]     = {0.0};
36029318fe57SMatthew G. Knepley 
36039566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 0));
36049566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 0, numPoints, coneSize, cones, coneOrientations, vertexCoords));
36059371c9d4SSatish Balay   } break;
36069371c9d4SSatish Balay   case DM_POLYTOPE_SEGMENT: {
36079318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {2, 1};
36089318fe57SMatthew G. Knepley     PetscInt    coneSize[3]         = {2, 0, 0};
36099318fe57SMatthew G. Knepley     PetscInt    cones[2]            = {1, 2};
36109318fe57SMatthew G. Knepley     PetscInt    coneOrientations[2] = {0, 0};
36119318fe57SMatthew G. Knepley     PetscScalar vertexCoords[2]     = {-1.0, 1.0};
36129318fe57SMatthew G. Knepley 
36139566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 1));
36149566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
36159371c9d4SSatish Balay   } break;
36169371c9d4SSatish Balay   case DM_POLYTOPE_POINT_PRISM_TENSOR: {
3617b5a892a1SMatthew G. Knepley     PetscInt    numPoints[2]        = {2, 1};
3618b5a892a1SMatthew G. Knepley     PetscInt    coneSize[3]         = {2, 0, 0};
3619b5a892a1SMatthew G. Knepley     PetscInt    cones[2]            = {1, 2};
3620b5a892a1SMatthew G. Knepley     PetscInt    coneOrientations[2] = {0, 0};
3621b5a892a1SMatthew G. Knepley     PetscScalar vertexCoords[2]     = {-1.0, 1.0};
3622b5a892a1SMatthew G. Knepley 
36239566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 1));
36249566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
36259371c9d4SSatish Balay   } break;
36269371c9d4SSatish Balay   case DM_POLYTOPE_TRIANGLE: {
36279318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {3, 1};
36289318fe57SMatthew G. Knepley     PetscInt    coneSize[4]         = {3, 0, 0, 0};
36299318fe57SMatthew G. Knepley     PetscInt    cones[3]            = {1, 2, 3};
36309318fe57SMatthew G. Knepley     PetscInt    coneOrientations[3] = {0, 0, 0};
36319318fe57SMatthew G. Knepley     PetscScalar vertexCoords[6]     = {-1.0, -1.0, 1.0, -1.0, -1.0, 1.0};
36329318fe57SMatthew G. Knepley 
36339566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 2));
36349566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
36359371c9d4SSatish Balay   } break;
36369371c9d4SSatish Balay   case DM_POLYTOPE_QUADRILATERAL: {
36379318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {4, 1};
36389318fe57SMatthew G. Knepley     PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
36399318fe57SMatthew G. Knepley     PetscInt    cones[4]            = {1, 2, 3, 4};
36409318fe57SMatthew G. Knepley     PetscInt    coneOrientations[4] = {0, 0, 0, 0};
36419318fe57SMatthew G. Knepley     PetscScalar vertexCoords[8]     = {-1.0, -1.0, 1.0, -1.0, 1.0, 1.0, -1.0, 1.0};
36429318fe57SMatthew G. Knepley 
36439566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 2));
36449566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
36459371c9d4SSatish Balay   } break;
36469371c9d4SSatish Balay   case DM_POLYTOPE_SEG_PRISM_TENSOR: {
36479318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {4, 1};
36489318fe57SMatthew G. Knepley     PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
36499318fe57SMatthew G. Knepley     PetscInt    cones[4]            = {1, 2, 3, 4};
36509318fe57SMatthew G. Knepley     PetscInt    coneOrientations[4] = {0, 0, 0, 0};
36519318fe57SMatthew G. Knepley     PetscScalar vertexCoords[8]     = {-1.0, -1.0, 1.0, -1.0, -1.0, 1.0, 1.0, 1.0};
36529318fe57SMatthew G. Knepley 
36539566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 2));
36549566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
36559371c9d4SSatish Balay   } break;
36569371c9d4SSatish Balay   case DM_POLYTOPE_TETRAHEDRON: {
36579318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {4, 1};
36589318fe57SMatthew G. Knepley     PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
3659f0edb160SMatthew G. Knepley     PetscInt    cones[4]            = {1, 2, 3, 4};
36609318fe57SMatthew G. Knepley     PetscInt    coneOrientations[4] = {0, 0, 0, 0};
3661f0edb160SMatthew 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};
36629318fe57SMatthew G. Knepley 
36639566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
36649566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
36659371c9d4SSatish Balay   } break;
36669371c9d4SSatish Balay   case DM_POLYTOPE_HEXAHEDRON: {
36679318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {8, 1};
36689318fe57SMatthew G. Knepley     PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
3669f0edb160SMatthew G. Knepley     PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
36709318fe57SMatthew G. Knepley     PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
36719371c9d4SSatish 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};
36729318fe57SMatthew G. Knepley 
36739566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
36749566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
36759371c9d4SSatish Balay   } break;
36769371c9d4SSatish Balay   case DM_POLYTOPE_TRI_PRISM: {
36779318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {6, 1};
36789318fe57SMatthew G. Knepley     PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
3679f0edb160SMatthew G. Knepley     PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
36809318fe57SMatthew G. Knepley     PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
36819371c9d4SSatish 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};
36829318fe57SMatthew G. Knepley 
36839566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
36849566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
36859371c9d4SSatish Balay   } break;
36869371c9d4SSatish Balay   case DM_POLYTOPE_TRI_PRISM_TENSOR: {
36879318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {6, 1};
36889318fe57SMatthew G. Knepley     PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
36899318fe57SMatthew G. Knepley     PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
36909318fe57SMatthew G. Knepley     PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
36919371c9d4SSatish 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};
36929318fe57SMatthew G. Knepley 
36939566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
36949566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
36959371c9d4SSatish Balay   } break;
36969371c9d4SSatish Balay   case DM_POLYTOPE_QUAD_PRISM_TENSOR: {
36979318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {8, 1};
36989318fe57SMatthew G. Knepley     PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
36999318fe57SMatthew G. Knepley     PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
37009318fe57SMatthew G. Knepley     PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
37019371c9d4SSatish 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};
37029318fe57SMatthew G. Knepley 
37039566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
37049566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37059371c9d4SSatish Balay   } break;
37069371c9d4SSatish Balay   case DM_POLYTOPE_PYRAMID: {
37079318fe57SMatthew G. Knepley     PetscInt    numPoints[2]        = {5, 1};
37089318fe57SMatthew G. Knepley     PetscInt    coneSize[6]         = {5, 0, 0, 0, 0, 0};
3709f0edb160SMatthew G. Knepley     PetscInt    cones[5]            = {1, 2, 3, 4, 5};
37109318fe57SMatthew G. Knepley     PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
37119371c9d4SSatish 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};
37129318fe57SMatthew G. Knepley 
37139566063dSJacob Faibussowitsch     PetscCall(DMSetDimension(rdm, 3));
37149566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
37159371c9d4SSatish Balay   } break;
3716d71ae5a4SJacob Faibussowitsch   default:
3717d71ae5a4SJacob Faibussowitsch     SETERRQ(PetscObjectComm((PetscObject)rdm), PETSC_ERR_ARG_WRONG, "Cannot create reference cell for cell type %s", DMPolytopeTypes[ct]);
37189318fe57SMatthew G. Knepley   }
37199318fe57SMatthew G. Knepley   {
37209318fe57SMatthew G. Knepley     PetscInt Nv, v;
37219318fe57SMatthew G. Knepley 
37229318fe57SMatthew G. Knepley     /* Must create the celltype label here so that we do not automatically try to compute the types */
37239566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(rdm, "celltype"));
37249566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCellType(rdm, 0, ct));
37259566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(rdm, NULL, &Nv));
37269566063dSJacob Faibussowitsch     for (v = 1; v < Nv; ++v) PetscCall(DMPlexSetCellType(rdm, v, DM_POLYTOPE_POINT));
37279318fe57SMatthew G. Knepley   }
37289566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolateInPlace_Internal(rdm));
37299566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)rdm, DMPolytopeTypes[ct]));
37300a6ba040SMatthew G. Knepley   PetscFunctionReturn(0);
37310a6ba040SMatthew G. Knepley }
37320a6ba040SMatthew G. Knepley 
37339318fe57SMatthew G. Knepley /*@
3734a1cb98faSBarry Smith   DMPlexCreateReferenceCell - Create a `DMPLEX` with the appropriate FEM reference cell
37359318fe57SMatthew G. Knepley 
37369318fe57SMatthew G. Knepley   Collective
37379318fe57SMatthew G. Knepley 
37389318fe57SMatthew G. Knepley   Input Parameters:
37399318fe57SMatthew G. Knepley + comm - The communicator
37409318fe57SMatthew G. Knepley - ct   - The cell type of the reference cell
37419318fe57SMatthew G. Knepley 
37429318fe57SMatthew G. Knepley   Output Parameter:
37439318fe57SMatthew G. Knepley . refdm - The reference cell
37449318fe57SMatthew G. Knepley 
37459318fe57SMatthew G. Knepley   Level: intermediate
37469318fe57SMatthew G. Knepley 
3747a1cb98faSBarry Smith .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreateReferenceCell()`, `DMPlexCreateBoxMesh()`
37489318fe57SMatthew G. Knepley @*/
3749d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateReferenceCell(MPI_Comm comm, DMPolytopeType ct, DM *refdm)
3750d71ae5a4SJacob Faibussowitsch {
37510a6ba040SMatthew G. Knepley   PetscFunctionBegin;
37529566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, refdm));
37539566063dSJacob Faibussowitsch   PetscCall(DMSetType(*refdm, DMPLEX));
37549566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateReferenceCell_Internal(*refdm, ct));
37559318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
37569318fe57SMatthew G. Knepley }
375779a015ccSMatthew G. Knepley 
3758d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateBoundaryLabel_Private(DM dm, const char name[])
3759d71ae5a4SJacob Faibussowitsch {
37609318fe57SMatthew G. Knepley   DM        plex;
37619318fe57SMatthew G. Knepley   DMLabel   label;
37629318fe57SMatthew G. Knepley   PetscBool hasLabel;
37630a6ba040SMatthew G. Knepley 
3764c22d3578SMatthew G. Knepley   PetscFunctionBegin;
37659566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &hasLabel));
37669318fe57SMatthew G. Knepley   if (hasLabel) PetscFunctionReturn(0);
37679566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, name));
37689566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
37699566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
37709566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkBoundaryFaces(plex, 1, label));
37711c8afea9SMatthew G. Knepley   PetscCall(DMPlexLabelComplete(plex, label));
37729566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plex));
37739318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
37749318fe57SMatthew G. Knepley }
3775acdc6f61SToby Isaac 
3776669647acSMatthew G. Knepley /*
3777669647acSMatthew 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.
3778669647acSMatthew G. Knepley 
3779669647acSMatthew G. Knepley     (x, y) -> (r, theta) = (x[1], (x[0] - lower[0]) * 2\pi/(upper[0] - lower[0]))
3780669647acSMatthew G. Knepley */
3781d71ae5a4SJacob 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[])
3782d71ae5a4SJacob Faibussowitsch {
3783669647acSMatthew G. Knepley   const PetscReal low = PetscRealPart(constants[0]);
3784669647acSMatthew G. Knepley   const PetscReal upp = PetscRealPart(constants[1]);
3785669647acSMatthew G. Knepley   const PetscReal r   = PetscRealPart(u[1]);
3786669647acSMatthew G. Knepley   const PetscReal th  = 2. * PETSC_PI * (PetscRealPart(u[0]) - low) / (upp - low);
3787669647acSMatthew G. Knepley 
3788669647acSMatthew G. Knepley   f0[0] = r * PetscCosReal(th);
3789669647acSMatthew G. Knepley   f0[1] = r * PetscSinReal(th);
3790669647acSMatthew G. Knepley }
3791669647acSMatthew G. Knepley 
3792*cfb853baSMatthew G. Knepley const char *const DMPlexShapes[] = {"box", "box_surface", "ball", "sphere", "cylinder", "schwarz_p", "gyroid", "doublet", "annulus", "hypercubic", "unknown", "DMPlexShape", "DM_SHAPE_", NULL};
37939318fe57SMatthew G. Knepley 
3794d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMPlexCreateFromOptions_Internal(PetscOptionItems *PetscOptionsObject, PetscBool *useCoordSpace, DM dm)
3795d71ae5a4SJacob Faibussowitsch {
37969318fe57SMatthew G. Knepley   DMPlexShape    shape   = DM_SHAPE_BOX;
37979318fe57SMatthew G. Knepley   DMPolytopeType cell    = DM_POLYTOPE_TRIANGLE;
37989318fe57SMatthew G. Knepley   PetscInt       dim     = 2;
37999318fe57SMatthew G. Knepley   PetscBool      simplex = PETSC_TRUE, interpolate = PETSC_TRUE, adjCone = PETSC_FALSE, adjClosure = PETSC_TRUE, refDomain = PETSC_FALSE;
3800cd7e8a5eSksagiyam   PetscBool      flg, flg2, fflg, bdfflg, nameflg;
38019318fe57SMatthew G. Knepley   MPI_Comm       comm;
3802ed5e4e85SVaclav Hapla   char           filename[PETSC_MAX_PATH_LEN]   = "<unspecified>";
3803ed5e4e85SVaclav Hapla   char           bdFilename[PETSC_MAX_PATH_LEN] = "<unspecified>";
3804ed5e4e85SVaclav Hapla   char           plexname[PETSC_MAX_PATH_LEN]   = "";
38059318fe57SMatthew G. Knepley 
38069318fe57SMatthew G. Knepley   PetscFunctionBegin;
38079566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject)dm, &comm));
38089318fe57SMatthew G. Knepley   /* TODO Turn this into a registration interface */
38099566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_filename", "File containing a mesh", "DMPlexCreateFromFile", filename, filename, sizeof(filename), &fflg));
38109566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_boundary_filename", "File containing a mesh boundary", "DMPlexCreateFromFile", bdFilename, bdFilename, sizeof(bdFilename), &bdfflg));
38119566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_name", "Name of the mesh in the file", "DMPlexCreateFromFile", plexname, plexname, sizeof(plexname), &nameflg));
38129566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_plex_cell", "Cell shape", "", DMPolytopeTypes, (PetscEnum)cell, (PetscEnum *)&cell, NULL));
38139566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_reference_cell_domain", "Use a reference cell domain", "", refDomain, &refDomain, NULL));
38149566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_plex_shape", "Shape for built-in mesh", "", DMPlexShapes, (PetscEnum)shape, (PetscEnum *)&shape, &flg));
38159566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_dim", "Topological dimension of the mesh", "DMGetDimension", dim, &dim, &flg, 0));
3816*cfb853baSMatthew G. Knepley   PetscCheck(dim >= 0, comm, PETSC_ERR_ARG_OUTOFRANGE, "Dimension %" PetscInt_FMT " should be in [0, infinity)", dim);
38179566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_simplex", "Mesh cell shape", "", simplex, &simplex, &flg));
38189566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_interpolate", "Flag to create edges and faces automatically", "", interpolate, &interpolate, &flg));
38199566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_adj_cone", "Set adjacency direction", "DMSetBasicAdjacency", adjCone, &adjCone, &flg));
38209566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_adj_closure", "Set adjacency size", "DMSetBasicAdjacency", adjClosure, &adjClosure, &flg2));
38219566063dSJacob Faibussowitsch   if (flg || flg2) PetscCall(DMSetBasicAdjacency(dm, adjCone, adjClosure));
38229318fe57SMatthew G. Knepley 
382361a622f3SMatthew G. Knepley   switch (cell) {
382461a622f3SMatthew G. Knepley   case DM_POLYTOPE_POINT:
382561a622f3SMatthew G. Knepley   case DM_POLYTOPE_SEGMENT:
382661a622f3SMatthew G. Knepley   case DM_POLYTOPE_POINT_PRISM_TENSOR:
382761a622f3SMatthew G. Knepley   case DM_POLYTOPE_TRIANGLE:
382861a622f3SMatthew G. Knepley   case DM_POLYTOPE_QUADRILATERAL:
382961a622f3SMatthew G. Knepley   case DM_POLYTOPE_TETRAHEDRON:
3830d71ae5a4SJacob Faibussowitsch   case DM_POLYTOPE_HEXAHEDRON:
3831d71ae5a4SJacob Faibussowitsch     *useCoordSpace = PETSC_TRUE;
3832d71ae5a4SJacob Faibussowitsch     break;
3833d71ae5a4SJacob Faibussowitsch   default:
3834d71ae5a4SJacob Faibussowitsch     *useCoordSpace = PETSC_FALSE;
3835d71ae5a4SJacob Faibussowitsch     break;
383661a622f3SMatthew G. Knepley   }
383761a622f3SMatthew G. Knepley 
38389318fe57SMatthew G. Knepley   if (fflg) {
38399318fe57SMatthew G. Knepley     DM dmnew;
38409318fe57SMatthew G. Knepley 
38419566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), filename, plexname, interpolate, &dmnew));
38425de52c6dSVaclav Hapla     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
384369d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &dmnew));
38449318fe57SMatthew G. Knepley   } else if (refDomain) {
38459566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateReferenceCell_Internal(dm, cell));
38469318fe57SMatthew G. Knepley   } else if (bdfflg) {
38479318fe57SMatthew G. Knepley     DM bdm, dmnew;
38489318fe57SMatthew G. Knepley 
38499566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject)dm), bdFilename, plexname, interpolate, &bdm));
38509566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)bdm, "bd_"));
38519566063dSJacob Faibussowitsch     PetscCall(DMSetFromOptions(bdm));
38529566063dSJacob Faibussowitsch     PetscCall(DMPlexGenerate(bdm, NULL, interpolate, &dmnew));
38539566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&bdm));
38545de52c6dSVaclav Hapla     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
385569d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &dmnew));
38569318fe57SMatthew G. Knepley   } else {
38579566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)dm, DMPlexShapes[shape]));
38589318fe57SMatthew G. Knepley     switch (shape) {
3859669647acSMatthew G. Knepley     case DM_SHAPE_BOX:
3860669647acSMatthew G. Knepley     case DM_SHAPE_ANNULUS: {
38619318fe57SMatthew G. Knepley       PetscInt       faces[3]  = {0, 0, 0};
38629318fe57SMatthew G. Knepley       PetscReal      lower[3]  = {0, 0, 0};
38639318fe57SMatthew G. Knepley       PetscReal      upper[3]  = {1, 1, 1};
38649318fe57SMatthew G. Knepley       DMBoundaryType bdt[3]    = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
3865669647acSMatthew G. Knepley       PetscBool      isAnnular = shape == DM_SHAPE_ANNULUS ? PETSC_TRUE : PETSC_FALSE;
38669318fe57SMatthew G. Knepley       PetscInt       i, n;
38679318fe57SMatthew G. Knepley 
38689318fe57SMatthew G. Knepley       n = dim;
38699318fe57SMatthew G. Knepley       for (i = 0; i < dim; ++i) faces[i] = (dim == 1 ? 1 : 4 - dim);
38709566063dSJacob Faibussowitsch       PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg));
38719318fe57SMatthew G. Knepley       n = 3;
38729566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
387363a3b9bcSJacob Faibussowitsch       PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Lower box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
38749318fe57SMatthew G. Knepley       n = 3;
38759566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
387663a3b9bcSJacob Faibussowitsch       PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Upper box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
38779318fe57SMatthew G. Knepley       n = 3;
38789566063dSJacob Faibussowitsch       PetscCall(PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *)bdt, &n, &flg));
387963a3b9bcSJacob Faibussowitsch       PetscCheck(!flg || !(n != dim), comm, PETSC_ERR_ARG_SIZ, "Box boundary types had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
3880669647acSMatthew G. Knepley 
3881669647acSMatthew G. Knepley       PetscCheck(!isAnnular || dim == 2, comm, PETSC_ERR_ARG_OUTOFRANGE, "Only two dimensional annuli have been implemented");
3882669647acSMatthew G. Knepley       if (isAnnular)
3883669647acSMatthew G. Knepley         for (i = 0; i < dim - 1; ++i) bdt[i] = DM_BOUNDARY_PERIODIC;
3884669647acSMatthew G. Knepley 
38859318fe57SMatthew G. Knepley       switch (cell) {
388661a622f3SMatthew G. Knepley       case DM_POLYTOPE_TRI_PRISM_TENSOR:
38879566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateWedgeBoxMesh_Internal(dm, faces, lower, upper, bdt));
3888d410b0cfSMatthew G. Knepley         if (!interpolate) {
3889d410b0cfSMatthew G. Knepley           DM udm;
3890d410b0cfSMatthew G. Knepley 
38919566063dSJacob Faibussowitsch           PetscCall(DMPlexUninterpolate(dm, &udm));
389269d8a87bSksagiyam           PetscCall(DMPlexReplace_Internal(dm, &udm));
3893d410b0cfSMatthew G. Knepley         }
38949318fe57SMatthew G. Knepley         break;
3895d71ae5a4SJacob Faibussowitsch       default:
3896d71ae5a4SJacob Faibussowitsch         PetscCall(DMPlexCreateBoxMesh_Internal(dm, dim, simplex, faces, lower, upper, bdt, interpolate));
3897d71ae5a4SJacob Faibussowitsch         break;
38989318fe57SMatthew G. Knepley       }
3899669647acSMatthew G. Knepley       if (isAnnular) {
3900669647acSMatthew G. Knepley         DM          cdm;
3901669647acSMatthew G. Knepley         PetscDS     cds;
3902669647acSMatthew G. Knepley         PetscScalar bounds[2] = {lower[0], upper[0]};
3903669647acSMatthew G. Knepley 
3904669647acSMatthew G. Knepley         // Fix coordinates for annular region
3905669647acSMatthew G. Knepley         PetscCall(DMSetPeriodicity(dm, NULL, NULL, NULL));
3906669647acSMatthew G. Knepley         PetscCall(DMSetCellCoordinatesLocal(dm, NULL));
3907669647acSMatthew G. Knepley         PetscCall(DMSetCellCoordinates(dm, NULL));
3908669647acSMatthew G. Knepley         PetscCall(DMPlexCreateCoordinateSpace(dm, 1, NULL));
3909669647acSMatthew G. Knepley         PetscCall(DMGetCoordinateDM(dm, &cdm));
3910669647acSMatthew G. Knepley         PetscCall(DMGetDS(cdm, &cds));
3911669647acSMatthew G. Knepley         PetscCall(PetscDSSetConstants(cds, 2, bounds));
3912669647acSMatthew G. Knepley         PetscCall(DMPlexRemapGeometry(dm, 0.0, boxToAnnulus));
3913669647acSMatthew G. Knepley       }
39149371c9d4SSatish Balay     } break;
39159371c9d4SSatish Balay     case DM_SHAPE_BOX_SURFACE: {
39169318fe57SMatthew G. Knepley       PetscInt  faces[3] = {0, 0, 0};
39179318fe57SMatthew G. Knepley       PetscReal lower[3] = {0, 0, 0};
39189318fe57SMatthew G. Knepley       PetscReal upper[3] = {1, 1, 1};
39199318fe57SMatthew G. Knepley       PetscInt  i, n;
39209318fe57SMatthew G. Knepley 
39219318fe57SMatthew G. Knepley       n = dim + 1;
39229318fe57SMatthew G. Knepley       for (i = 0; i < dim + 1; ++i) faces[i] = (dim + 1 == 1 ? 1 : 4 - (dim + 1));
39239566063dSJacob Faibussowitsch       PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg));
39249318fe57SMatthew G. Knepley       n = 3;
39259566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
392663a3b9bcSJacob 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);
39279318fe57SMatthew G. Knepley       n = 3;
39289566063dSJacob Faibussowitsch       PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
392963a3b9bcSJacob 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);
39309566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(dm, dim + 1, faces, lower, upper, interpolate));
39319371c9d4SSatish Balay     } break;
39329371c9d4SSatish Balay     case DM_SHAPE_SPHERE: {
39339318fe57SMatthew G. Knepley       PetscReal R = 1.0;
39349318fe57SMatthew G. Knepley 
39359566063dSJacob Faibussowitsch       PetscCall(PetscOptionsReal("-dm_plex_sphere_radius", "Radius of the sphere", "", R, &R, &flg));
39369566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateSphereMesh_Internal(dm, dim, simplex, R));
39379371c9d4SSatish Balay     } break;
39389371c9d4SSatish Balay     case DM_SHAPE_BALL: {
39399318fe57SMatthew G. Knepley       PetscReal R = 1.0;
39409318fe57SMatthew G. Knepley 
39419566063dSJacob Faibussowitsch       PetscCall(PetscOptionsReal("-dm_plex_ball_radius", "Radius of the ball", "", R, &R, &flg));
39429566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateBallMesh_Internal(dm, dim, R));
39439371c9d4SSatish Balay     } break;
39449371c9d4SSatish Balay     case DM_SHAPE_CYLINDER: {
39459318fe57SMatthew G. Knepley       DMBoundaryType bdt = DM_BOUNDARY_NONE;
39469318fe57SMatthew G. Knepley       PetscInt       Nw  = 6;
39479318fe57SMatthew G. Knepley 
39489566063dSJacob Faibussowitsch       PetscCall(PetscOptionsEnum("-dm_plex_cylinder_bd", "Boundary type in the z direction", "", DMBoundaryTypes, (PetscEnum)bdt, (PetscEnum *)&bdt, NULL));
39499566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-dm_plex_cylinder_num_wedges", "Number of wedges around the cylinder", "", Nw, &Nw, NULL));
39509318fe57SMatthew G. Knepley       switch (cell) {
3951d71ae5a4SJacob Faibussowitsch       case DM_POLYTOPE_TRI_PRISM_TENSOR:
3952d71ae5a4SJacob Faibussowitsch         PetscCall(DMPlexCreateWedgeCylinderMesh_Internal(dm, Nw, interpolate));
3953d71ae5a4SJacob Faibussowitsch         break;
3954d71ae5a4SJacob Faibussowitsch       default:
3955d71ae5a4SJacob Faibussowitsch         PetscCall(DMPlexCreateHexCylinderMesh_Internal(dm, bdt));
3956d71ae5a4SJacob Faibussowitsch         break;
39579318fe57SMatthew G. Knepley       }
39589371c9d4SSatish Balay     } break;
3959b7f5c055SJed Brown     case DM_SHAPE_SCHWARZ_P: // fallthrough
39609371c9d4SSatish Balay     case DM_SHAPE_GYROID: {
3961b7f5c055SJed Brown       PetscInt       extent[3] = {1, 1, 1}, refine = 0, layers = 0, three;
3962b7f5c055SJed Brown       PetscReal      thickness   = 0.;
3963b7f5c055SJed Brown       DMBoundaryType periodic[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
3964b7f5c055SJed Brown       DMPlexTPSType  tps_type    = shape == DM_SHAPE_SCHWARZ_P ? DMPLEX_TPS_SCHWARZ_P : DMPLEX_TPS_GYROID;
39651436d7faSJed Brown       PetscBool      tps_distribute;
39669566063dSJacob Faibussowitsch       PetscCall(PetscOptionsIntArray("-dm_plex_tps_extent", "Number of replicas for each of three dimensions", NULL, extent, (three = 3, &three), NULL));
39679566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-dm_plex_tps_refine", "Number of refinements", NULL, refine, &refine, NULL));
39689566063dSJacob Faibussowitsch       PetscCall(PetscOptionsEnumArray("-dm_plex_tps_periodic", "Periodicity in each of three dimensions", NULL, DMBoundaryTypes, (PetscEnum *)periodic, (three = 3, &three), NULL));
39699566063dSJacob Faibussowitsch       PetscCall(PetscOptionsInt("-dm_plex_tps_layers", "Number of layers in volumetric extrusion (or zero to not extrude)", NULL, layers, &layers, NULL));
39709566063dSJacob Faibussowitsch       PetscCall(PetscOptionsReal("-dm_plex_tps_thickness", "Thickness of volumetric extrusion", NULL, thickness, &thickness, NULL));
39719566063dSJacob Faibussowitsch       PetscCall(DMPlexDistributeGetDefault(dm, &tps_distribute));
39729566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_tps_distribute", "Distribute the 2D mesh prior to refinement and extrusion", NULL, tps_distribute, &tps_distribute, NULL));
39739566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateTPSMesh_Internal(dm, tps_type, extent, periodic, tps_distribute, refine, layers, thickness));
39749371c9d4SSatish Balay     } break;
39759371c9d4SSatish Balay     case DM_SHAPE_DOUBLET: {
397605bd46c0SStefano Zampini       DM        dmnew;
397705bd46c0SStefano Zampini       PetscReal rl = 0.0;
397805bd46c0SStefano Zampini 
397905bd46c0SStefano Zampini       PetscCall(PetscOptionsReal("-dm_plex_doublet_refinementlimit", "Refinement limit", NULL, rl, &rl, NULL));
398005bd46c0SStefano Zampini       PetscCall(DMPlexCreateDoublet(PetscObjectComm((PetscObject)dm), dim, simplex, interpolate, rl, &dmnew));
39815de52c6dSVaclav Hapla       PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
398269d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &dmnew));
39839371c9d4SSatish Balay     } break;
3984*cfb853baSMatthew G. Knepley     case DM_SHAPE_HYPERCUBIC: {
3985*cfb853baSMatthew G. Knepley       PetscInt       *edges;
3986*cfb853baSMatthew G. Knepley       PetscReal      *lower, *upper;
3987*cfb853baSMatthew G. Knepley       DMBoundaryType *bdt;
3988*cfb853baSMatthew G. Knepley       PetscInt        n, d;
3989*cfb853baSMatthew G. Knepley 
3990*cfb853baSMatthew G. Knepley       *useCoordSpace = PETSC_FALSE;
3991*cfb853baSMatthew G. Knepley       PetscCall(PetscMalloc4(dim, &edges, dim, &lower, dim, &upper, dim, &bdt));
3992*cfb853baSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
3993*cfb853baSMatthew G. Knepley         edges[d] = 1;
3994*cfb853baSMatthew G. Knepley         lower[d] = 0.;
3995*cfb853baSMatthew G. Knepley         upper[d] = 1.;
3996*cfb853baSMatthew G. Knepley         bdt[d]   = DM_BOUNDARY_PERIODIC;
3997*cfb853baSMatthew G. Knepley       }
3998*cfb853baSMatthew G. Knepley       n = dim;
3999*cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", edges, &n, &flg));
4000*cfb853baSMatthew G. Knepley       n = dim;
4001*cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
4002*cfb853baSMatthew 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);
4003*cfb853baSMatthew G. Knepley       n = dim;
4004*cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
4005*cfb853baSMatthew 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);
4006*cfb853baSMatthew G. Knepley       n = dim;
4007*cfb853baSMatthew G. Knepley       PetscCall(PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *)bdt, &n, &flg));
4008*cfb853baSMatthew 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);
4009*cfb853baSMatthew G. Knepley       PetscCall(DMPlexCreateHypercubicMesh_Internal(dm, dim, lower, upper, edges, bdt));
4010*cfb853baSMatthew G. Knepley       PetscCall(PetscFree4(edges, lower, upper, bdt));
4011*cfb853baSMatthew G. Knepley     } break;
4012d71ae5a4SJacob Faibussowitsch     default:
4013d71ae5a4SJacob Faibussowitsch       SETERRQ(comm, PETSC_ERR_SUP, "Domain shape %s is unsupported", DMPlexShapes[shape]);
40149318fe57SMatthew G. Knepley     }
40159318fe57SMatthew G. Knepley   }
40169566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
401748a46eb9SPierre Jolivet   if (!((PetscObject)dm)->name && nameflg) PetscCall(PetscObjectSetName((PetscObject)dm, plexname));
40180a6ba040SMatthew G. Knepley   PetscFunctionReturn(0);
40190a6ba040SMatthew G. Knepley }
40200a6ba040SMatthew G. Knepley 
4021d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions_NonRefinement_Plex(DM dm, PetscOptionItems *PetscOptionsObject)
4022d71ae5a4SJacob Faibussowitsch {
40230a6ba040SMatthew G. Knepley   DM_Plex  *mesh = (DM_Plex *)dm->data;
40247f9d8d6cSVaclav Hapla   PetscBool flg, flg2;
40259318fe57SMatthew G. Knepley   char      bdLabel[PETSC_MAX_PATH_LEN];
40260a6ba040SMatthew G. Knepley 
40270a6ba040SMatthew G. Knepley   PetscFunctionBegin;
40280a6ba040SMatthew G. Knepley   /* Handle viewing */
40299566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_print_set_values", "Output all set values info", "DMPlexMatSetClosure", PETSC_FALSE, &mesh->printSetValues, NULL));
40309566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_fem", "Debug output level all fem computations", "DMPlexSNESComputeResidualFEM", 0, &mesh->printFEM, NULL, 0));
40319566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-dm_plex_print_tol", "Tolerance for FEM output", "DMPlexSNESComputeResidualFEM", mesh->printTol, &mesh->printTol, NULL));
40329566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_l2", "Debug output level all L2 diff computations", "DMComputeL2Diff", 0, &mesh->printL2, NULL, 0));
4033f5867de0SMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_locate", "Debug output level all point location computations", "DMLocatePoints", 0, &mesh->printLocate, NULL, 0));
40349566063dSJacob Faibussowitsch   PetscCall(DMMonitorSetFromOptions(dm, "-dm_plex_monitor_throughput", "Monitor the simulation throughput", "DMPlexMonitorThroughput", DMPlexMonitorThroughput, NULL, &flg));
40359566063dSJacob Faibussowitsch   if (flg) PetscCall(PetscLogDefaultBegin());
40369318fe57SMatthew G. Knepley   /* Labeling */
40379566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_boundary_label", "Label to mark the mesh boundary", "", bdLabel, bdLabel, sizeof(bdLabel), &flg));
40389566063dSJacob Faibussowitsch   if (flg) PetscCall(DMPlexCreateBoundaryLabel_Private(dm, bdLabel));
4039953fc75cSMatthew G. Knepley   /* Point Location */
40409566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_hash_location", "Use grid hashing for point location", "DMInterpolate", PETSC_FALSE, &mesh->useHashLocation, NULL));
40410848f4b5SMatthew G. Knepley   /* Partitioning and distribution */
40429566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_partition_balance", "Attempt to evenly divide points on partition boundary between processes", "DMPlexSetPartitionBalance", PETSC_FALSE, &mesh->partitionBalance, NULL));
40432e62ab5aSMatthew G. Knepley   /* Generation and remeshing */
40449566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_remesh_bd", "Allow changes to the boundary on remeshing", "DMAdapt", PETSC_FALSE, &mesh->remeshBd, NULL));
4045b29cfa1cSToby Isaac   /* Projection behavior */
4046d5b43468SJose E. Roman   PetscCall(PetscOptionsBoundedInt("-dm_plex_max_projection_height", "Maximum mesh point height used to project locally", "DMPlexSetMaxProjectionHeight", 0, &mesh->maxProjectionHeight, NULL, 0));
40479566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_regular_refinement", "Use special nested projection algorithm for regular refinement", "DMPlexSetRegularRefinement", mesh->regularRefinement, &mesh->regularRefinement, NULL));
4048f12cf164SMatthew G. Knepley   /* Checking structure */
4049f12cf164SMatthew G. Knepley   {
40507f9d8d6cSVaclav Hapla     PetscBool all = PETSC_FALSE;
4051f12cf164SMatthew G. Knepley 
40527f9d8d6cSVaclav Hapla     PetscCall(PetscOptionsBool("-dm_plex_check_all", "Perform all basic checks", "DMPlexCheck", PETSC_FALSE, &all, NULL));
40537f9d8d6cSVaclav Hapla     if (all) {
40547f9d8d6cSVaclav Hapla       PetscCall(DMPlexCheck(dm));
40557f9d8d6cSVaclav Hapla     } else {
40569566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_symmetry", "Check that the adjacency information in the mesh is symmetric", "DMPlexCheckSymmetry", PETSC_FALSE, &flg, &flg2));
40577f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckSymmetry(dm));
40589566063dSJacob 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));
40597f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckSkeleton(dm, 0));
40609566063dSJacob 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));
40617f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckFaces(dm, 0));
40629566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_geometry", "Check that cells have positive volume", "DMPlexCheckGeometry", PETSC_FALSE, &flg, &flg2));
40637f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckGeometry(dm));
40649566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_pointsf", "Check some necessary conditions for PointSF", "DMPlexCheckPointSF", PETSC_FALSE, &flg, &flg2));
4065d7d32a9aSMatthew G. Knepley       if (flg && flg2) PetscCall(DMPlexCheckPointSF(dm, NULL, PETSC_FALSE));
40669566063dSJacob 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));
40677f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckInterfaceCones(dm));
40687f9d8d6cSVaclav Hapla     }
40699566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-dm_plex_check_cell_shape", "Check cell shape", "DMPlexCheckCellShape", PETSC_FALSE, &flg, &flg2));
40709566063dSJacob Faibussowitsch     if (flg && flg2) PetscCall(DMPlexCheckCellShape(dm, PETSC_TRUE, PETSC_DETERMINE));
4071f12cf164SMatthew G. Knepley   }
40729318fe57SMatthew G. Knepley   {
40739318fe57SMatthew G. Knepley     PetscReal scale = 1.0;
40744f3833eaSMatthew G. Knepley 
40759566063dSJacob Faibussowitsch     PetscCall(PetscOptionsReal("-dm_plex_scale", "Scale factor for mesh coordinates", "DMPlexScale", scale, &scale, &flg));
40769318fe57SMatthew G. Knepley     if (flg) {
40779318fe57SMatthew G. Knepley       Vec coordinates, coordinatesLocal;
40789318fe57SMatthew G. Knepley 
40799566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinates(dm, &coordinates));
40809566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinatesLocal(dm, &coordinatesLocal));
40819566063dSJacob Faibussowitsch       PetscCall(VecScale(coordinates, scale));
40829566063dSJacob Faibussowitsch       PetscCall(VecScale(coordinatesLocal, scale));
40839318fe57SMatthew G. Knepley     }
40849318fe57SMatthew G. Knepley   }
40859566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerSetFromOptions(mesh->partitioner));
408668d4fef7SMatthew G. Knepley   PetscFunctionReturn(0);
408768d4fef7SMatthew G. Knepley }
408868d4fef7SMatthew G. Knepley 
4089d71ae5a4SJacob Faibussowitsch PetscErrorCode DMSetFromOptions_Overlap_Plex(DM dm, PetscOptionItems *PetscOptionsObject, PetscInt *overlap)
4090d71ae5a4SJacob Faibussowitsch {
4091c506a872SMatthew G. Knepley   PetscInt  numOvLabels = 16, numOvExLabels = 16;
4092c506a872SMatthew G. Knepley   char     *ovLabelNames[16], *ovExLabelNames[16];
4093c506a872SMatthew G. Knepley   PetscInt  numOvValues = 16, numOvExValues = 16, l;
4094c506a872SMatthew G. Knepley   PetscBool flg;
4095c506a872SMatthew G. Knepley 
4096c506a872SMatthew G. Knepley   PetscFunctionBegin;
4097c506a872SMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_distribute_overlap", "The size of the overlap halo", "DMPlexDistribute", *overlap, overlap, NULL, 0));
4098c506a872SMatthew G. Knepley   PetscCall(PetscOptionsStringArray("-dm_distribute_overlap_labels", "List of overlap label names", "DMPlexDistribute", ovLabelNames, &numOvLabels, &flg));
4099c506a872SMatthew G. Knepley   if (!flg) numOvLabels = 0;
4100c506a872SMatthew G. Knepley   if (numOvLabels) {
4101c506a872SMatthew G. Knepley     ((DM_Plex *)dm->data)->numOvLabels = numOvLabels;
4102c506a872SMatthew G. Knepley     for (l = 0; l < numOvLabels; ++l) {
4103c506a872SMatthew G. Knepley       PetscCall(DMGetLabel(dm, ovLabelNames[l], &((DM_Plex *)dm->data)->ovLabels[l]));
4104c506a872SMatthew G. Knepley       PetscCheck(((DM_Plex *)dm->data)->ovLabels[l], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid label name %s", ovLabelNames[l]);
4105c506a872SMatthew G. Knepley       PetscCall(PetscFree(ovLabelNames[l]));
4106c506a872SMatthew G. Knepley     }
4107c506a872SMatthew G. Knepley     PetscCall(PetscOptionsIntArray("-dm_distribute_overlap_values", "List of overlap label values", "DMPlexDistribute", ((DM_Plex *)dm->data)->ovValues, &numOvValues, &flg));
4108c506a872SMatthew G. Knepley     if (!flg) numOvValues = 0;
4109c506a872SMatthew 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);
4110c506a872SMatthew G. Knepley 
4111c506a872SMatthew G. Knepley     PetscCall(PetscOptionsStringArray("-dm_distribute_overlap_exclude_labels", "List of overlap exclude label names", "DMPlexDistribute", ovExLabelNames, &numOvExLabels, &flg));
4112c506a872SMatthew G. Knepley     if (!flg) numOvExLabels = 0;
4113c506a872SMatthew G. Knepley     ((DM_Plex *)dm->data)->numOvExLabels = numOvExLabels;
4114c506a872SMatthew G. Knepley     for (l = 0; l < numOvExLabels; ++l) {
4115c506a872SMatthew G. Knepley       PetscCall(DMGetLabel(dm, ovExLabelNames[l], &((DM_Plex *)dm->data)->ovExLabels[l]));
4116c506a872SMatthew G. Knepley       PetscCheck(((DM_Plex *)dm->data)->ovExLabels[l], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid label name %s", ovExLabelNames[l]);
4117c506a872SMatthew G. Knepley       PetscCall(PetscFree(ovExLabelNames[l]));
4118c506a872SMatthew G. Knepley     }
4119c506a872SMatthew G. Knepley     PetscCall(PetscOptionsIntArray("-dm_distribute_overlap_exclude_values", "List of overlap exclude label values", "DMPlexDistribute", ((DM_Plex *)dm->data)->ovExValues, &numOvExValues, &flg));
4120c506a872SMatthew G. Knepley     if (!flg) numOvExValues = 0;
4121c506a872SMatthew 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);
4122c506a872SMatthew G. Knepley   }
4123c506a872SMatthew G. Knepley   PetscFunctionReturn(0);
4124c506a872SMatthew G. Knepley }
4125c506a872SMatthew G. Knepley 
4126d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMSetFromOptions_Plex(DM dm, PetscOptionItems *PetscOptionsObject)
4127d71ae5a4SJacob Faibussowitsch {
4128bdf63967SMatthew G. Knepley   PetscFunctionList        ordlist;
4129bdf63967SMatthew G. Knepley   char                     oname[256];
4130d410b0cfSMatthew G. Knepley   PetscReal                volume    = -1.0;
41319318fe57SMatthew G. Knepley   PetscInt                 prerefine = 0, refine = 0, r, coarsen = 0, overlap = 0, extLayers = 0, dim;
4132e600fa54SMatthew G. Knepley   PetscBool                uniformOrig, created = PETSC_FALSE, uniform = PETSC_TRUE, distribute, interpolate = PETSC_TRUE, coordSpace = PETSC_TRUE, remap = PETSC_TRUE, ghostCells = PETSC_FALSE, isHierarchy, ignoreModel = PETSC_FALSE, flg;
41336bc1bd01Sksagiyam   DMPlexReorderDefaultFlag reorder;
413468d4fef7SMatthew G. Knepley 
413568d4fef7SMatthew G. Knepley   PetscFunctionBegin;
4136d0609cedSBarry Smith   PetscOptionsHeadBegin(PetscOptionsObject, "DMPlex Options");
4137dd4c3f67SMatthew G. Knepley   if (dm->cloneOpts) goto non_refine;
41389318fe57SMatthew G. Knepley   /* Handle automatic creation */
41399566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
41406bc1bd01Sksagiyam   if (dim < 0) {
41416bc1bd01Sksagiyam     PetscCall(DMPlexCreateFromOptions_Internal(PetscOptionsObject, &coordSpace, dm));
41426bc1bd01Sksagiyam     created = PETSC_TRUE;
41436bc1bd01Sksagiyam   }
41446bc1bd01Sksagiyam   PetscCall(DMGetDimension(dm, &dim));
4145d89e6e46SMatthew G. Knepley   /* Handle interpolation before distribution */
41469566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_interpolate_pre", "Flag to interpolate mesh before distribution", "", interpolate, &interpolate, &flg));
4147d89e6e46SMatthew G. Knepley   if (flg) {
4148d89e6e46SMatthew G. Knepley     DMPlexInterpolatedFlag interpolated;
4149d89e6e46SMatthew G. Knepley 
41509566063dSJacob Faibussowitsch     PetscCall(DMPlexIsInterpolated(dm, &interpolated));
4151d89e6e46SMatthew G. Knepley     if (interpolated == DMPLEX_INTERPOLATED_FULL && !interpolate) {
4152d89e6e46SMatthew G. Knepley       DM udm;
4153d89e6e46SMatthew G. Knepley 
41549566063dSJacob Faibussowitsch       PetscCall(DMPlexUninterpolate(dm, &udm));
415569d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &udm));
4156d89e6e46SMatthew G. Knepley     } else if (interpolated != DMPLEX_INTERPOLATED_FULL && interpolate) {
4157d89e6e46SMatthew G. Knepley       DM idm;
4158d89e6e46SMatthew G. Knepley 
41599566063dSJacob Faibussowitsch       PetscCall(DMPlexInterpolate(dm, &idm));
416069d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &idm));
4161d89e6e46SMatthew G. Knepley     }
4162d89e6e46SMatthew G. Knepley   }
41639b44eab4SMatthew G. Knepley   /* Handle DMPlex refinement before distribution */
41649566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_ignore_model", "Flag to ignore the geometry model when refining", "DMCreate", ignoreModel, &ignoreModel, &flg));
4165ad540459SPierre Jolivet   if (flg) ((DM_Plex *)dm->data)->ignoreModel = ignoreModel;
41669566063dSJacob Faibussowitsch   PetscCall(DMPlexGetRefinementUniform(dm, &uniformOrig));
41679566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine_pre", "The number of refinements before distribution", "DMCreate", prerefine, &prerefine, NULL, 0));
41689566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_remap_pre", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL));
41699566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_uniform_pre", "Flag for uniform refinement before distribution", "DMCreate", uniform, &uniform, &flg));
41709566063dSJacob Faibussowitsch   if (flg) PetscCall(DMPlexSetRefinementUniform(dm, uniform));
41719566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-dm_refine_volume_limit_pre", "The maximum cell volume after refinement before distribution", "DMCreate", volume, &volume, &flg));
41729318fe57SMatthew G. Knepley   if (flg) {
41739566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementUniform(dm, PETSC_FALSE));
41749566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementLimit(dm, volume));
41759318fe57SMatthew G. Knepley     prerefine = PetscMax(prerefine, 1);
41769318fe57SMatthew G. Knepley   }
41779b44eab4SMatthew G. Knepley   for (r = 0; r < prerefine; ++r) {
41789b44eab4SMatthew G. Knepley     DM             rdm;
41799b44eab4SMatthew G. Knepley     PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc;
41809b44eab4SMatthew G. Knepley 
4181dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
41829566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &rdm));
418369d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &rdm));
4184dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
418561a622f3SMatthew G. Knepley     if (coordFunc && remap) {
41869566063dSJacob Faibussowitsch       PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
41879b44eab4SMatthew G. Knepley       ((DM_Plex *)dm->data)->coordFunc = coordFunc;
41889b44eab4SMatthew G. Knepley     }
41899b44eab4SMatthew G. Knepley   }
41909566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, uniformOrig));
41919318fe57SMatthew G. Knepley   /* Handle DMPlex extrusion before distribution */
41929566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_extrude", "The number of layers to extrude", "", extLayers, &extLayers, NULL, 0));
41939318fe57SMatthew G. Knepley   if (extLayers) {
41949318fe57SMatthew G. Knepley     DM edm;
41959318fe57SMatthew G. Knepley 
41969566063dSJacob Faibussowitsch     PetscCall(DMExtrude(dm, extLayers, &edm));
419769d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &edm));
419848d16a33SMatthew G. Knepley     ((DM_Plex *)dm->data)->coordFunc = NULL;
4199dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
4200d410b0cfSMatthew G. Knepley     extLayers = 0;
42015e17fc22SAidan Hamilton     PetscCall(DMGetDimension(dm, &dim));
42029318fe57SMatthew G. Knepley   }
4203bdf63967SMatthew G. Knepley   /* Handle DMPlex reordering before distribution */
42046bc1bd01Sksagiyam   PetscCall(DMPlexReorderGetDefault(dm, &reorder));
42059566063dSJacob Faibussowitsch   PetscCall(MatGetOrderingList(&ordlist));
42066bc1bd01Sksagiyam   PetscCall(PetscStrncpy(oname, MATORDERINGNATURAL, sizeof(oname)));
42079566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_plex_reorder", "Set mesh reordering type", "DMPlexGetOrdering", ordlist, MATORDERINGNATURAL, oname, sizeof(oname), &flg));
42086bc1bd01Sksagiyam   if (reorder == DMPLEX_REORDER_DEFAULT_TRUE || flg) {
4209bdf63967SMatthew G. Knepley     DM pdm;
4210bdf63967SMatthew G. Knepley     IS perm;
4211bdf63967SMatthew G. Knepley 
42129566063dSJacob Faibussowitsch     PetscCall(DMPlexGetOrdering(dm, oname, NULL, &perm));
42139566063dSJacob Faibussowitsch     PetscCall(DMPlexPermute(dm, perm, &pdm));
42149566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&perm));
421569d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &pdm));
4216dbbe0bcdSBarry Smith     PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
4217bdf63967SMatthew G. Knepley   }
42189b44eab4SMatthew G. Knepley   /* Handle DMPlex distribution */
42199566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeGetDefault(dm, &distribute));
4220c506a872SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_distribute", "Flag to redistribute a mesh among processes", "DMPlexDistribute", distribute, &distribute, NULL));
4221dbbe0bcdSBarry Smith   PetscCall(DMSetFromOptions_Overlap_Plex(dm, PetscOptionsObject, &overlap));
42229b44eab4SMatthew G. Knepley   if (distribute) {
42239b44eab4SMatthew G. Knepley     DM               pdm = NULL;
42249b44eab4SMatthew G. Knepley     PetscPartitioner part;
42259b44eab4SMatthew G. Knepley 
42269566063dSJacob Faibussowitsch     PetscCall(DMPlexGetPartitioner(dm, &part));
42279566063dSJacob Faibussowitsch     PetscCall(PetscPartitionerSetFromOptions(part));
42289566063dSJacob Faibussowitsch     PetscCall(DMPlexDistribute(dm, overlap, NULL, &pdm));
422948a46eb9SPierre Jolivet     if (pdm) PetscCall(DMPlexReplace_Internal(dm, &pdm));
42309b44eab4SMatthew G. Knepley   }
42319318fe57SMatthew G. Knepley   /* Create coordinate space */
42329318fe57SMatthew G. Knepley   if (created) {
423361a622f3SMatthew G. Knepley     DM_Plex  *mesh   = (DM_Plex *)dm->data;
42349318fe57SMatthew G. Knepley     PetscInt  degree = 1;
42355515ebd3SMatthew G. Knepley     PetscInt  height = 0;
42365515ebd3SMatthew G. Knepley     DM        cdm;
42376858538eSMatthew G. Knepley     PetscBool flg;
42389318fe57SMatthew G. Knepley 
42399566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-dm_coord_space", "Use an FEM space for coordinates", "", coordSpace, &coordSpace, &flg));
42409566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-dm_coord_petscspace_degree", "FEM degree for coordinate space", "", degree, &degree, NULL));
42419566063dSJacob Faibussowitsch     if (coordSpace) PetscCall(DMPlexCreateCoordinateSpace(dm, degree, mesh->coordFunc));
42425515ebd3SMatthew G. Knepley     PetscCall(DMGetCoordinateDM(dm, &cdm));
424361a622f3SMatthew G. Knepley     if (flg && !coordSpace) {
424461a622f3SMatthew G. Knepley       PetscDS      cds;
424561a622f3SMatthew G. Knepley       PetscObject  obj;
424661a622f3SMatthew G. Knepley       PetscClassId id;
424761a622f3SMatthew G. Knepley 
42489566063dSJacob Faibussowitsch       PetscCall(DMGetDS(cdm, &cds));
42499566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
42509566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
425161a622f3SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
425261a622f3SMatthew G. Knepley         PetscContainer dummy;
425361a622f3SMatthew G. Knepley 
42549566063dSJacob Faibussowitsch         PetscCall(PetscContainerCreate(PETSC_COMM_SELF, &dummy));
42559566063dSJacob Faibussowitsch         PetscCall(PetscObjectSetName((PetscObject)dummy, "coordinates"));
42569566063dSJacob Faibussowitsch         PetscCall(DMSetField(cdm, 0, NULL, (PetscObject)dummy));
42579566063dSJacob Faibussowitsch         PetscCall(PetscContainerDestroy(&dummy));
42589566063dSJacob Faibussowitsch         PetscCall(DMClearDS(cdm));
425961a622f3SMatthew G. Knepley       }
426061a622f3SMatthew G. Knepley       mesh->coordFunc = NULL;
426161a622f3SMatthew G. Knepley     }
42626858538eSMatthew G. Knepley     PetscCall(PetscOptionsBool("-dm_sparse_localize", "Localize only necessary cells", "", dm->sparseLocalize, &dm->sparseLocalize, &flg));
42635515ebd3SMatthew G. Knepley     PetscCall(PetscOptionsInt("-dm_localize_height", "Localize edges and faces in addition to cells", "", height, &height, &flg));
42645515ebd3SMatthew G. Knepley     if (flg) PetscCall(DMPlexSetMaxProjectionHeight(cdm, height));
42659566063dSJacob Faibussowitsch     PetscCall(DMLocalizeCoordinates(dm));
42669318fe57SMatthew G. Knepley   }
426768d4fef7SMatthew G. Knepley   /* Handle DMPlex refinement */
426861a622f3SMatthew G. Knepley   remap = PETSC_TRUE;
42699566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine", "The number of uniform refinements", "DMCreate", refine, &refine, NULL, 0));
42709566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_remap", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL));
42719566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine_hierarchy", "The number of uniform refinements", "DMCreate", refine, &refine, &isHierarchy, 0));
42729566063dSJacob Faibussowitsch   if (refine) PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
427368d4fef7SMatthew G. Knepley   if (refine && isHierarchy) {
4274acdc6f61SToby Isaac     DM *dms, coarseDM;
427568d4fef7SMatthew G. Knepley 
42769566063dSJacob Faibussowitsch     PetscCall(DMGetCoarseDM(dm, &coarseDM));
42779566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)coarseDM));
42789566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(refine, &dms));
42799566063dSJacob Faibussowitsch     PetscCall(DMRefineHierarchy(dm, refine, dms));
428068d4fef7SMatthew G. Knepley     /* Total hack since we do not pass in a pointer */
42819566063dSJacob Faibussowitsch     PetscCall(DMPlexSwap_Static(dm, dms[refine - 1]));
428268d4fef7SMatthew G. Knepley     if (refine == 1) {
42839566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dm, dms[0]));
42849566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dm, PETSC_TRUE));
428568d4fef7SMatthew G. Knepley     } else {
42869566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dm, dms[refine - 2]));
42879566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dm, PETSC_TRUE));
42889566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dms[0], dms[refine - 1]));
42899566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dms[0], PETSC_TRUE));
429068d4fef7SMatthew G. Knepley     }
42919566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dms[refine - 1], coarseDM));
42929566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)coarseDM));
429368d4fef7SMatthew G. Knepley     /* Free DMs */
429468d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
4295dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dms[r], PetscOptionsObject));
42969566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dms[r]));
429768d4fef7SMatthew G. Knepley     }
42989566063dSJacob Faibussowitsch     PetscCall(PetscFree(dms));
429968d4fef7SMatthew G. Knepley   } else {
430068d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
43019318fe57SMatthew G. Knepley       DM             rdm;
430251a74b61SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc;
430368d4fef7SMatthew G. Knepley 
4304dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
43059566063dSJacob Faibussowitsch       PetscCall(DMRefine(dm, PetscObjectComm((PetscObject)dm), &rdm));
430668d4fef7SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
430769d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &rdm));
4308dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
430961a622f3SMatthew G. Knepley       if (coordFunc && remap) {
43109566063dSJacob Faibussowitsch         PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
431151a74b61SMatthew G. Knepley         ((DM_Plex *)dm->data)->coordFunc = coordFunc;
431251a74b61SMatthew G. Knepley       }
431368d4fef7SMatthew G. Knepley     }
431468d4fef7SMatthew G. Knepley   }
43153cf6fe12SMatthew G. Knepley   /* Handle DMPlex coarsening */
43169566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_coarsen", "Coarsen the mesh", "DMCreate", coarsen, &coarsen, NULL, 0));
43179566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_coarsen_hierarchy", "The number of coarsenings", "DMCreate", coarsen, &coarsen, &isHierarchy, 0));
4318b653a561SMatthew G. Knepley   if (coarsen && isHierarchy) {
4319b653a561SMatthew G. Knepley     DM *dms;
4320b653a561SMatthew G. Knepley 
43219566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(coarsen, &dms));
43229566063dSJacob Faibussowitsch     PetscCall(DMCoarsenHierarchy(dm, coarsen, dms));
4323b653a561SMatthew G. Knepley     /* Free DMs */
4324b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
4325dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dms[r], PetscOptionsObject));
43269566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dms[r]));
4327b653a561SMatthew G. Knepley     }
43289566063dSJacob Faibussowitsch     PetscCall(PetscFree(dms));
4329b653a561SMatthew G. Knepley   } else {
4330b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
43319318fe57SMatthew G. Knepley       DM             cdm;
43329318fe57SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex *)dm->data)->coordFunc;
43333cf6fe12SMatthew G. Knepley 
4334dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
43359566063dSJacob Faibussowitsch       PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject)dm), &cdm));
43363cf6fe12SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
433769d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &cdm));
4338dbbe0bcdSBarry Smith       PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
43399318fe57SMatthew G. Knepley       if (coordFunc) {
43409566063dSJacob Faibussowitsch         PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
43419318fe57SMatthew G. Knepley         ((DM_Plex *)dm->data)->coordFunc = coordFunc;
43429318fe57SMatthew G. Knepley       }
43433cf6fe12SMatthew G. Knepley     }
4344b653a561SMatthew G. Knepley   }
4345909dfd52SMatthew G. Knepley   /* Handle ghost cells */
43469566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_create_fv_ghost_cells", "Flag to create finite volume ghost cells on the boundary", "DMCreate", ghostCells, &ghostCells, NULL));
4347909dfd52SMatthew G. Knepley   if (ghostCells) {
4348909dfd52SMatthew G. Knepley     DM   gdm;
4349909dfd52SMatthew G. Knepley     char lname[PETSC_MAX_PATH_LEN];
4350909dfd52SMatthew G. Knepley 
4351909dfd52SMatthew G. Knepley     lname[0] = '\0';
43529566063dSJacob Faibussowitsch     PetscCall(PetscOptionsString("-dm_plex_fv_ghost_cells_label", "Label name for ghost cells boundary", "DMCreate", lname, lname, sizeof(lname), &flg));
43539566063dSJacob Faibussowitsch     PetscCall(DMPlexConstructGhostCells(dm, flg ? lname : NULL, NULL, &gdm));
435469d8a87bSksagiyam     PetscCall(DMPlexReplace_Internal(dm, &gdm));
4355909dfd52SMatthew G. Knepley   }
43566913077dSMatthew G. Knepley   /* Handle 1D order */
43576bc1bd01Sksagiyam   if (reorder != DMPLEX_REORDER_DEFAULT_FALSE && dim == 1) {
43586913077dSMatthew G. Knepley     DM           cdm, rdm;
43596913077dSMatthew G. Knepley     PetscDS      cds;
43606913077dSMatthew G. Knepley     PetscObject  obj;
43616913077dSMatthew G. Knepley     PetscClassId id = PETSC_OBJECT_CLASSID;
43626913077dSMatthew G. Knepley     IS           perm;
43636bc1bd01Sksagiyam     PetscInt     Nf;
43646913077dSMatthew G. Knepley     PetscBool    distributed;
43656913077dSMatthew G. Knepley 
43669566063dSJacob Faibussowitsch     PetscCall(DMPlexIsDistributed(dm, &distributed));
43679566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
43689566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
43699566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(cds, &Nf));
43706913077dSMatthew G. Knepley     if (Nf) {
43719566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
43729566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
43736913077dSMatthew G. Knepley     }
43746bc1bd01Sksagiyam     if (!distributed && id != PETSCFE_CLASSID) {
43759566063dSJacob Faibussowitsch       PetscCall(DMPlexGetOrdering1D(dm, &perm));
43769566063dSJacob Faibussowitsch       PetscCall(DMPlexPermute(dm, perm, &rdm));
437769d8a87bSksagiyam       PetscCall(DMPlexReplace_Internal(dm, &rdm));
43789566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&perm));
43796913077dSMatthew G. Knepley     }
43806913077dSMatthew G. Knepley   }
43813cf6fe12SMatthew G. Knepley /* Handle */
4382dd4c3f67SMatthew G. Knepley non_refine:
4383dbbe0bcdSBarry Smith   PetscCall(DMSetFromOptions_NonRefinement_Plex(dm, PetscOptionsObject));
4384d0609cedSBarry Smith   PetscOptionsHeadEnd();
43850a6ba040SMatthew G. Knepley   PetscFunctionReturn(0);
43860a6ba040SMatthew G. Knepley }
43870a6ba040SMatthew G. Knepley 
4388d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMCreateGlobalVector_Plex(DM dm, Vec *vec)
4389d71ae5a4SJacob Faibussowitsch {
4390552f7358SJed Brown   PetscFunctionBegin;
43919566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector_Section_Private(dm, vec));
43929566063dSJacob Faibussowitsch   /* PetscCall(VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM)); */
43939566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEW, (void (*)(void))VecView_Plex));
43949566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEWNATIVE, (void (*)(void))VecView_Plex_Native));
43959566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOAD, (void (*)(void))VecLoad_Plex));
43969566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOADNATIVE, (void (*)(void))VecLoad_Plex_Native));
4397552f7358SJed Brown   PetscFunctionReturn(0);
4398552f7358SJed Brown }
4399552f7358SJed Brown 
4400d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMCreateLocalVector_Plex(DM dm, Vec *vec)
4401d71ae5a4SJacob Faibussowitsch {
4402552f7358SJed Brown   PetscFunctionBegin;
44039566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector_Section_Private(dm, vec));
44049566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEW, (void (*)(void))VecView_Plex_Local));
44059566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOAD, (void (*)(void))VecLoad_Plex_Local));
4406552f7358SJed Brown   PetscFunctionReturn(0);
4407552f7358SJed Brown }
4408552f7358SJed Brown 
4409d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGetDimPoints_Plex(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
4410d71ae5a4SJacob Faibussowitsch {
4411793f3fe5SMatthew G. Knepley   PetscInt depth, d;
4412793f3fe5SMatthew G. Knepley 
4413793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
44149566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
4415793f3fe5SMatthew G. Knepley   if (depth == 1) {
44169566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(dm, &d));
44179566063dSJacob Faibussowitsch     if (dim == 0) PetscCall(DMPlexGetDepthStratum(dm, dim, pStart, pEnd));
44189566063dSJacob Faibussowitsch     else if (dim == d) PetscCall(DMPlexGetDepthStratum(dm, 1, pStart, pEnd));
44199371c9d4SSatish Balay     else {
44209371c9d4SSatish Balay       *pStart = 0;
44219371c9d4SSatish Balay       *pEnd   = 0;
44229371c9d4SSatish Balay     }
4423793f3fe5SMatthew G. Knepley   } else {
44249566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, dim, pStart, pEnd));
4425793f3fe5SMatthew G. Knepley   }
4426793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
4427793f3fe5SMatthew G. Knepley }
4428793f3fe5SMatthew G. Knepley 
4429d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMGetNeighbors_Plex(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
4430d71ae5a4SJacob Faibussowitsch {
4431502a2867SDave May   PetscSF            sf;
44320a19bb7dSprj-   PetscInt           niranks, njranks, n;
44330a19bb7dSprj-   const PetscMPIInt *iranks, *jranks;
44340a19bb7dSprj-   DM_Plex           *data = (DM_Plex *)dm->data;
4435502a2867SDave May 
44362f356facSMatthew G. Knepley   PetscFunctionBegin;
44379566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
44380a19bb7dSprj-   if (!data->neighbors) {
44399566063dSJacob Faibussowitsch     PetscCall(PetscSFSetUp(sf));
44409566063dSJacob Faibussowitsch     PetscCall(PetscSFGetRootRanks(sf, &njranks, &jranks, NULL, NULL, NULL));
44419566063dSJacob Faibussowitsch     PetscCall(PetscSFGetLeafRanks(sf, &niranks, &iranks, NULL, NULL));
44429566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(njranks + niranks + 1, &data->neighbors));
44439566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(data->neighbors + 1, jranks, njranks));
44449566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(data->neighbors + njranks + 1, iranks, niranks));
44450a19bb7dSprj-     n = njranks + niranks;
44469566063dSJacob Faibussowitsch     PetscCall(PetscSortRemoveDupsMPIInt(&n, data->neighbors + 1));
44470a19bb7dSprj-     /* The following cast should never fail: can't have more neighbors than PETSC_MPI_INT_MAX */
44489566063dSJacob Faibussowitsch     PetscCall(PetscMPIIntCast(n, data->neighbors));
44490a19bb7dSprj-   }
44500a19bb7dSprj-   if (nranks) *nranks = data->neighbors[0];
44510a19bb7dSprj-   if (ranks) {
44520a19bb7dSprj-     if (data->neighbors[0]) *ranks = data->neighbors + 1;
44530a19bb7dSprj-     else *ranks = NULL;
44540a19bb7dSprj-   }
4455502a2867SDave May   PetscFunctionReturn(0);
4456502a2867SDave May }
4457502a2867SDave May 
44581eb70e55SToby Isaac PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM, DM, Mat, Vec, Vec);
44591eb70e55SToby Isaac 
4460d71ae5a4SJacob Faibussowitsch static PetscErrorCode DMInitialize_Plex(DM dm)
4461d71ae5a4SJacob Faibussowitsch {
4462552f7358SJed Brown   PetscFunctionBegin;
4463552f7358SJed Brown   dm->ops->view                      = DMView_Plex;
44642c40f234SMatthew G. Knepley   dm->ops->load                      = DMLoad_Plex;
4465552f7358SJed Brown   dm->ops->setfromoptions            = DMSetFromOptions_Plex;
446638221697SMatthew G. Knepley   dm->ops->clone                     = DMClone_Plex;
4467552f7358SJed Brown   dm->ops->setup                     = DMSetUp_Plex;
44681bb6d2a8SBarry Smith   dm->ops->createlocalsection        = DMCreateLocalSection_Plex;
446966ad2231SToby Isaac   dm->ops->createdefaultconstraints  = DMCreateDefaultConstraints_Plex;
4470552f7358SJed Brown   dm->ops->createglobalvector        = DMCreateGlobalVector_Plex;
4471552f7358SJed Brown   dm->ops->createlocalvector         = DMCreateLocalVector_Plex;
4472184d77edSJed Brown   dm->ops->getlocaltoglobalmapping   = NULL;
44730298fd71SBarry Smith   dm->ops->createfieldis             = NULL;
4474552f7358SJed Brown   dm->ops->createcoordinatedm        = DMCreateCoordinateDM_Plex;
4475f19dbd58SToby Isaac   dm->ops->createcoordinatefield     = DMCreateCoordinateField_Plex;
44760a6ba040SMatthew G. Knepley   dm->ops->getcoloring               = NULL;
4477552f7358SJed Brown   dm->ops->creatematrix              = DMCreateMatrix_Plex;
4478bceba477SMatthew G. Knepley   dm->ops->createinterpolation       = DMCreateInterpolation_Plex;
4479bd041c0cSMatthew G. Knepley   dm->ops->createmassmatrix          = DMCreateMassMatrix_Plex;
4480b4937a87SMatthew G. Knepley   dm->ops->createmassmatrixlumped    = DMCreateMassMatrixLumped_Plex;
44815a84ad33SLisandro Dalcin   dm->ops->createinjection           = DMCreateInjection_Plex;
4482552f7358SJed Brown   dm->ops->refine                    = DMRefine_Plex;
44830a6ba040SMatthew G. Knepley   dm->ops->coarsen                   = DMCoarsen_Plex;
44840a6ba040SMatthew G. Knepley   dm->ops->refinehierarchy           = DMRefineHierarchy_Plex;
4485b653a561SMatthew G. Knepley   dm->ops->coarsenhierarchy          = DMCoarsenHierarchy_Plex;
4486d410b0cfSMatthew G. Knepley   dm->ops->extrude                   = DMExtrude_Plex;
44870298fd71SBarry Smith   dm->ops->globaltolocalbegin        = NULL;
44880298fd71SBarry Smith   dm->ops->globaltolocalend          = NULL;
44890298fd71SBarry Smith   dm->ops->localtoglobalbegin        = NULL;
44900298fd71SBarry Smith   dm->ops->localtoglobalend          = NULL;
4491552f7358SJed Brown   dm->ops->destroy                   = DMDestroy_Plex;
4492552f7358SJed Brown   dm->ops->createsubdm               = DMCreateSubDM_Plex;
44932adcc780SMatthew G. Knepley   dm->ops->createsuperdm             = DMCreateSuperDM_Plex;
4494793f3fe5SMatthew G. Knepley   dm->ops->getdimpoints              = DMGetDimPoints_Plex;
4495552f7358SJed Brown   dm->ops->locatepoints              = DMLocatePoints_Plex;
44960709b2feSToby Isaac   dm->ops->projectfunctionlocal      = DMProjectFunctionLocal_Plex;
44970709b2feSToby Isaac   dm->ops->projectfunctionlabellocal = DMProjectFunctionLabelLocal_Plex;
4498bfc4295aSToby Isaac   dm->ops->projectfieldlocal         = DMProjectFieldLocal_Plex;
44998c6c5593SMatthew G. Knepley   dm->ops->projectfieldlabellocal    = DMProjectFieldLabelLocal_Plex;
4500ece3a9fcSMatthew G. Knepley   dm->ops->projectbdfieldlabellocal  = DMProjectBdFieldLabelLocal_Plex;
45010709b2feSToby Isaac   dm->ops->computel2diff             = DMComputeL2Diff_Plex;
4502b698f381SToby Isaac   dm->ops->computel2gradientdiff     = DMComputeL2GradientDiff_Plex;
45032a16baeaSToby Isaac   dm->ops->computel2fielddiff        = DMComputeL2FieldDiff_Plex;
450428d58a37SPierre Jolivet   dm->ops->getneighbors              = DMGetNeighbors_Plex;
45059566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertBoundaryValues_C", DMPlexInsertBoundaryValues_Plex));
45069566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexInsertTimeDerviativeBoundaryValues_C", DMPlexInsertTimeDerivativeBoundaryValues_Plex));
45079566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMSetUpGLVisViewer_C", DMSetUpGLVisViewer_Plex));
45089566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMCreateNeumannOverlap_C", DMCreateNeumannOverlap_Plex));
45099566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetOverlap_C", DMPlexGetOverlap_Plex));
45109566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeGetDefault_C", DMPlexDistributeGetDefault_Plex));
45119566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexDistributeSetDefault_C", DMPlexDistributeSetDefault_Plex));
45126bc1bd01Sksagiyam   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderGetDefault_C", DMPlexReorderGetDefault_Plex));
45136bc1bd01Sksagiyam   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexReorderSetDefault_C", DMPlexReorderSetDefault_Plex));
45149566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMInterpolateSolution_C", DMInterpolateSolution_Plex));
4515c506a872SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexGetOverlap_C", DMPlexGetOverlap_Plex));
4516c506a872SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm, "DMPlexSetOverlap_C", DMPlexSetOverlap_Plex));
4517552f7358SJed Brown   PetscFunctionReturn(0);
4518552f7358SJed Brown }
4519552f7358SJed Brown 
4520d71ae5a4SJacob Faibussowitsch PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm)
4521d71ae5a4SJacob Faibussowitsch {
452263a16f15SMatthew G. Knepley   DM_Plex *mesh = (DM_Plex *)dm->data;
452363a16f15SMatthew G. Knepley 
452463a16f15SMatthew G. Knepley   PetscFunctionBegin;
452563a16f15SMatthew G. Knepley   mesh->refct++;
452663a16f15SMatthew G. Knepley   (*newdm)->data = mesh;
45279566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)*newdm, DMPLEX));
45289566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(*newdm));
452963a16f15SMatthew G. Knepley   PetscFunctionReturn(0);
453063a16f15SMatthew G. Knepley }
453163a16f15SMatthew G. Knepley 
45328818961aSMatthew G Knepley /*MC
4533a1cb98faSBarry Smith   DMPLEX = "plex" - A `DM` object that encapsulates an unstructured mesh, or CW Complex, which can be expressed using a Hasse Diagram.
45348818961aSMatthew G Knepley                     In the local representation, Vecs contain all unknowns in the interior and shared boundary. This is
45358818961aSMatthew G Knepley                     specified by a PetscSection object. Ownership in the global representation is determined by
4536a1cb98faSBarry Smith                     ownership of the underlying `DMPLEX` points. This is specified by another `PetscSection` object.
45378818961aSMatthew G Knepley 
4538e5893cccSMatthew G. Knepley   Options Database Keys:
4539250712c9SMatthew G. Knepley + -dm_refine_pre                     - Refine mesh before distribution
4540250712c9SMatthew G. Knepley + -dm_refine_uniform_pre             - Choose uniform or generator-based refinement
4541250712c9SMatthew G. Knepley + -dm_refine_volume_limit_pre        - Cell volume limit after pre-refinement using generator
4542250712c9SMatthew G. Knepley . -dm_distribute                     - Distribute mesh across processes
4543250712c9SMatthew G. Knepley . -dm_distribute_overlap             - Number of cells to overlap for distribution
4544250712c9SMatthew G. Knepley . -dm_refine                         - Refine mesh after distribution
4545250712c9SMatthew G. Knepley . -dm_plex_hash_location             - Use grid hashing for point location
4546ddce0771SMatthew G. Knepley . -dm_plex_hash_box_faces <n,m,p>    - The number of divisions in each direction of the grid hash
4547f12cf164SMatthew G. Knepley . -dm_plex_partition_balance         - Attempt to evenly divide points on partition boundary between processes
4548f12cf164SMatthew G. Knepley . -dm_plex_remesh_bd                 - Allow changes to the boundary on remeshing
4549d5b43468SJose E. Roman . -dm_plex_max_projection_height     - Maximum mesh point height used to project locally
4550f12cf164SMatthew G. Knepley . -dm_plex_regular_refinement        - Use special nested projection algorithm for regular refinement
4551250712c9SMatthew G. Knepley . -dm_plex_check_all                 - Perform all shecks below
4552f12cf164SMatthew G. Knepley . -dm_plex_check_symmetry            - Check that the adjacency information in the mesh is symmetric
4553f12cf164SMatthew G. Knepley . -dm_plex_check_skeleton <celltype> - Check that each cell has the correct number of vertices
4554f12cf164SMatthew 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
4555f12cf164SMatthew G. Knepley . -dm_plex_check_geometry            - Check that cells have positive volume
4556f12cf164SMatthew G. Knepley . -dm_view :mesh.tex:ascii_latex     - View the mesh in LaTeX/TikZ
4557e5893cccSMatthew G. Knepley . -dm_plex_view_scale <num>          - Scale the TikZ
4558e5893cccSMatthew G. Knepley - -dm_plex_print_fem <num>           - View FEM assembly information, such as element vectors and matrices
4559e5893cccSMatthew G. Knepley 
45608818961aSMatthew G Knepley   Level: intermediate
45618818961aSMatthew G Knepley 
4562a1cb98faSBarry Smith .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMType`, `DMPlexCreate()`, `DMCreate()`, `DMSetType()`, `PetscSection`
45638818961aSMatthew G Knepley M*/
45648818961aSMatthew G Knepley 
4565d71ae5a4SJacob Faibussowitsch PETSC_EXTERN PetscErrorCode DMCreate_Plex(DM dm)
4566d71ae5a4SJacob Faibussowitsch {
4567552f7358SJed Brown   DM_Plex *mesh;
4568412e9a14SMatthew G. Knepley   PetscInt unit;
4569552f7358SJed Brown 
4570552f7358SJed Brown   PetscFunctionBegin;
4571552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45724dfa11a4SJacob Faibussowitsch   PetscCall(PetscNew(&mesh));
4573552f7358SJed Brown   dm->data = mesh;
4574552f7358SJed Brown 
4575552f7358SJed Brown   mesh->refct = 1;
45769566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->coneSection));
45779566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->supportSection));
4578552f7358SJed Brown   mesh->refinementUniform      = PETSC_TRUE;
4579552f7358SJed Brown   mesh->refinementLimit        = -1.0;
4580e600fa54SMatthew G. Knepley   mesh->distDefault            = PETSC_TRUE;
45816bc1bd01Sksagiyam   mesh->reorderDefault         = DMPLEX_REORDER_DEFAULT_NOTSET;
45821d1f2f2aSksagiyam   mesh->distributionName       = NULL;
45837d0f5628SVaclav Hapla   mesh->interpolated           = DMPLEX_INTERPOLATED_INVALID;
45847d0f5628SVaclav Hapla   mesh->interpolatedCollective = DMPLEX_INTERPOLATED_INVALID;
4585552f7358SJed Brown 
45869566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerCreate(PetscObjectComm((PetscObject)dm), &mesh->partitioner));
45872e62ab5aSMatthew G. Knepley   mesh->remeshBd = PETSC_FALSE;
4588d9deefdfSMatthew G. Knepley 
45898865f1eaSKarl Rupp   for (unit = 0; unit < NUM_PETSC_UNITS; ++unit) mesh->scale[unit] = 1.0;
4590552f7358SJed Brown 
4591df0420ecSMatthew G. Knepley   mesh->depthState    = -1;
4592ba2698f1SMatthew G. Knepley   mesh->celltypeState = -1;
45936113b454SMatthew G. Knepley   mesh->printTol      = 1.0e-10;
4594552f7358SJed Brown 
45959566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(dm));
4596552f7358SJed Brown   PetscFunctionReturn(0);
4597552f7358SJed Brown }
4598552f7358SJed Brown 
4599552f7358SJed Brown /*@
4600a1cb98faSBarry Smith   DMPlexCreate - Creates a `DMPLEX` object, which encapsulates an unstructured mesh, or CW complex, which can be expressed using a Hasse Diagram.
4601552f7358SJed Brown 
4602d083f849SBarry Smith   Collective
4603552f7358SJed Brown 
4604552f7358SJed Brown   Input Parameter:
4605a1cb98faSBarry Smith . comm - The communicator for the `DMPLEX` object
4606552f7358SJed Brown 
4607552f7358SJed Brown   Output Parameter:
4608a1cb98faSBarry Smith . mesh  - The `DMPLEX` object
4609552f7358SJed Brown 
4610552f7358SJed Brown   Level: beginner
4611552f7358SJed Brown 
4612a1cb98faSBarry Smith .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMType`, `DMPlexCreate()`, `DMCreate()`, `DMSetType()`
4613552f7358SJed Brown @*/
4614d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreate(MPI_Comm comm, DM *mesh)
4615d71ae5a4SJacob Faibussowitsch {
4616552f7358SJed Brown   PetscFunctionBegin;
4617552f7358SJed Brown   PetscValidPointer(mesh, 2);
46189566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, mesh));
46199566063dSJacob Faibussowitsch   PetscCall(DMSetType(*mesh, DMPLEX));
4620552f7358SJed Brown   PetscFunctionReturn(0);
4621552f7358SJed Brown }
4622552f7358SJed Brown 
4623b09969d6SVaclav Hapla /*@C
4624a1cb98faSBarry Smith   DMPlexBuildFromCellListParallel - Build distributed `DMPLEX` topology from a list of vertices for each cell (common mesh generator output)
4625a1cb98faSBarry Smith 
4626a1cb98faSBarry Smith   Collective on dm
4627b09969d6SVaclav Hapla 
4628b09969d6SVaclav Hapla   Input Parameters:
4629a1cb98faSBarry Smith + dm - The `DM`
4630b09969d6SVaclav Hapla . numCells - The number of cells owned by this process
4631a1cb98faSBarry Smith . numVertices - The number of vertices to be owned by this process, or `PETSC_DECIDE`
4632a1cb98faSBarry Smith . NVertices - The global number of vertices, or `PETSC_DETERMINE`
4633b09969d6SVaclav Hapla . numCorners - The number of vertices for each cell
46345e488331SVaclav Hapla - cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
4635b09969d6SVaclav Hapla 
4636be8c289dSNicolas Barral   Output Parameters:
4637a1cb98faSBarry Smith + vertexSF - (Optional) `PetscSF` describing complete vertex ownership
4638be8c289dSNicolas Barral - verticesAdjSaved - (Optional) vertex adjacency array
4639b09969d6SVaclav Hapla 
4640b09969d6SVaclav Hapla   Level: advanced
4641b09969d6SVaclav Hapla 
4642a1cb98faSBarry Smith   Notes:
4643a1cb98faSBarry Smith   Two triangles sharing a face
4644a1cb98faSBarry Smith .vb
4645a1cb98faSBarry Smith 
4646a1cb98faSBarry Smith         2
4647a1cb98faSBarry Smith       / | \
4648a1cb98faSBarry Smith      /  |  \
4649a1cb98faSBarry Smith     /   |   \
4650a1cb98faSBarry Smith    0  0 | 1  3
4651a1cb98faSBarry Smith     \   |   /
4652a1cb98faSBarry Smith      \  |  /
4653a1cb98faSBarry Smith       \ | /
4654a1cb98faSBarry Smith         1
4655a1cb98faSBarry Smith .ve
4656a1cb98faSBarry Smith would have input
4657a1cb98faSBarry Smith .vb
4658a1cb98faSBarry Smith   numCells = 2, numVertices = 4
4659a1cb98faSBarry Smith   cells = [0 1 2  1 3 2]
4660a1cb98faSBarry Smith .ve
4661a1cb98faSBarry Smith which would result in the `DMPLEX`
4662a1cb98faSBarry Smith .vb
4663a1cb98faSBarry Smith 
4664a1cb98faSBarry Smith         4
4665a1cb98faSBarry Smith       / | \
4666a1cb98faSBarry Smith      /  |  \
4667a1cb98faSBarry Smith     /   |   \
4668a1cb98faSBarry Smith    2  0 | 1  5
4669a1cb98faSBarry Smith     \   |   /
4670a1cb98faSBarry Smith      \  |  /
4671a1cb98faSBarry Smith       \ | /
4672a1cb98faSBarry Smith         3
4673a1cb98faSBarry Smith .ve
4674a1cb98faSBarry Smith 
4675a1cb98faSBarry Smith   Vertices are implicitly numbered consecutively 0,...,NVertices.
4676a1cb98faSBarry Smith   Each rank owns a chunk of numVertices consecutive vertices.
4677a1cb98faSBarry Smith   If numVertices is `PETSC_DECIDE`, PETSc will distribute them as evenly as possible using PetscLayout.
4678a1cb98faSBarry Smith   If NVertices is `PETSC_DETERMINE` and numVertices is PETSC_DECIDE, NVertices is computed by PETSc as the maximum vertex index in cells + 1.
4679a1cb98faSBarry Smith   If only NVertices is `PETSC_DETERMINE`, it is computed as the sum of numVertices over all ranks.
4680a1cb98faSBarry Smith 
4681a1cb98faSBarry Smith   The cell distribution is arbitrary non-overlapping, independent of the vertex distribution.
4682a1cb98faSBarry Smith 
4683a1cb98faSBarry Smith   Fortran Note:
4684a1cb98faSBarry Smith   Not currently supported in Fortran.
4685a1cb98faSBarry Smith 
4686a1cb98faSBarry Smith .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexBuildFromCellList()`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildCoordinatesFromCellListParallel()`,
4687a1cb98faSBarry Smith           `PetscSF`
4688b09969d6SVaclav Hapla @*/
4689d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildFromCellListParallel(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt NVertices, PetscInt numCorners, const PetscInt cells[], PetscSF *vertexSF, PetscInt **verticesAdjSaved)
4690d71ae5a4SJacob Faibussowitsch {
46912464107aSksagiyam   PetscSF     sfPoint;
46922464107aSksagiyam   PetscLayout layout;
469382fb893eSVaclav Hapla   PetscInt    numVerticesAdj, *verticesAdj, *cones, c, p;
4694a47d0d45SMatthew G. Knepley 
4695a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
469625b6865aSVaclav Hapla   PetscValidLogicalCollectiveInt(dm, NVertices, 4);
46979566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
469825b6865aSVaclav Hapla   /* Get/check global number of vertices */
469925b6865aSVaclav Hapla   {
470025b6865aSVaclav Hapla     PetscInt       NVerticesInCells, i;
470125b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
470225b6865aSVaclav Hapla 
470325b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
470425b6865aSVaclav Hapla     NVerticesInCells = PETSC_MIN_INT;
47059371c9d4SSatish Balay     for (i = 0; i < len; i++)
47069371c9d4SSatish Balay       if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
470725b6865aSVaclav Hapla     ++NVerticesInCells;
47089566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &NVerticesInCells, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm)));
470925b6865aSVaclav Hapla 
471025b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE && NVertices == PETSC_DECIDE) NVertices = NVerticesInCells;
47119371c9d4SSatish Balay     else
47129371c9d4SSatish 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);
471325b6865aSVaclav Hapla   }
47149079aca8SVaclav Hapla   /* Count locally unique vertices */
47159079aca8SVaclav Hapla   {
47169079aca8SVaclav Hapla     PetscHSetI vhash;
47179079aca8SVaclav Hapla     PetscInt   off = 0;
47189079aca8SVaclav Hapla 
47199566063dSJacob Faibussowitsch     PetscCall(PetscHSetICreate(&vhash));
4720a47d0d45SMatthew G. Knepley     for (c = 0; c < numCells; ++c) {
472148a46eb9SPierre Jolivet       for (p = 0; p < numCorners; ++p) PetscCall(PetscHSetIAdd(vhash, cells[c * numCorners + p]));
4722a47d0d45SMatthew G. Knepley     }
47239566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetSize(vhash, &numVerticesAdj));
47249566063dSJacob Faibussowitsch     if (!verticesAdjSaved) PetscCall(PetscMalloc1(numVerticesAdj, &verticesAdj));
4725ad540459SPierre Jolivet     else verticesAdj = *verticesAdjSaved;
47269566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetElems(vhash, &off, verticesAdj));
47279566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&vhash));
472863a3b9bcSJacob Faibussowitsch     PetscCheck(off == numVerticesAdj, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid number of local vertices %" PetscInt_FMT " should be %" PetscInt_FMT, off, numVerticesAdj);
4729a47d0d45SMatthew G. Knepley   }
47309566063dSJacob Faibussowitsch   PetscCall(PetscSortInt(numVerticesAdj, verticesAdj));
4731a47d0d45SMatthew G. Knepley   /* Create cones */
47329566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, 0, numCells + numVerticesAdj));
47339566063dSJacob Faibussowitsch   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, numCorners));
47349566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm));
47359566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCones(dm, &cones));
4736a47d0d45SMatthew G. Knepley   for (c = 0; c < numCells; ++c) {
4737a47d0d45SMatthew G. Knepley     for (p = 0; p < numCorners; ++p) {
4738a47d0d45SMatthew G. Knepley       const PetscInt gv = cells[c * numCorners + p];
4739a47d0d45SMatthew G. Knepley       PetscInt       lv;
4740a47d0d45SMatthew G. Knepley 
47419079aca8SVaclav Hapla       /* Positions within verticesAdj form 0-based local vertex numbering;
47429079aca8SVaclav Hapla          we need to shift it by numCells to get correct DAG points (cells go first) */
47439566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv));
474463a3b9bcSJacob Faibussowitsch       PetscCheck(lv >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not find global vertex %" PetscInt_FMT " in local connectivity", gv);
4745961cfab0SVaclav Hapla       cones[c * numCorners + p] = lv + numCells;
4746a47d0d45SMatthew G. Knepley     }
4747a47d0d45SMatthew G. Knepley   }
47482464107aSksagiyam   /* Build point sf */
47499566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(PetscObjectComm((PetscObject)dm), &layout));
47509566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetSize(layout, NVertices));
47519566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, numVertices));
47529566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
47539566063dSJacob Faibussowitsch   PetscCall(PetscSFCreateByMatchingIndices(layout, numVerticesAdj, verticesAdj, NULL, numCells, numVerticesAdj, verticesAdj, NULL, numCells, vertexSF, &sfPoint));
47549566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
47559566063dSJacob Faibussowitsch   if (!verticesAdjSaved) PetscCall(PetscFree(verticesAdj));
47569566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)sfPoint, "point SF"));
47572464107aSksagiyam   if (dm->sf) {
47582464107aSksagiyam     const char *prefix;
47592464107aSksagiyam 
47609566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm->sf, &prefix));
47619566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)sfPoint, prefix));
47622464107aSksagiyam   }
47639566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dm, sfPoint));
47649566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&sfPoint));
47659566063dSJacob Faibussowitsch   if (vertexSF) PetscCall(PetscObjectSetName((PetscObject)(*vertexSF), "Vertex Ownership SF"));
4766a47d0d45SMatthew G. Knepley   /* Fill in the rest of the topology structure */
47679566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
47689566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
47699566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
4770a47d0d45SMatthew G. Knepley   PetscFunctionReturn(0);
4771a47d0d45SMatthew G. Knepley }
4772a47d0d45SMatthew G. Knepley 
4773b09969d6SVaclav Hapla /*@C
4774a1cb98faSBarry Smith   DMPlexBuildCoordinatesFromCellListParallel - Build `DM` coordinates from a list of coordinates for each owned vertex (common mesh generator output)
4775a1cb98faSBarry Smith 
4776a1cb98faSBarry Smith   Collective on dm
4777b09969d6SVaclav Hapla 
4778b09969d6SVaclav Hapla   Input Parameters:
4779a1cb98faSBarry Smith + dm - The `DM`
4780b09969d6SVaclav Hapla . spaceDim - The spatial dimension used for coordinates
4781a1cb98faSBarry Smith . sfVert - `PetscSF` describing complete vertex ownership
4782b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
4783b09969d6SVaclav Hapla 
4784b09969d6SVaclav Hapla   Level: advanced
4785b09969d6SVaclav Hapla 
4786a1cb98faSBarry Smith   Fortran Note:
4787b09969d6SVaclav Hapla   Not currently supported in Fortran.
4788b09969d6SVaclav Hapla 
4789a1cb98faSBarry Smith .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildFromCellListParallel()`
4790b09969d6SVaclav Hapla @*/
4791d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildCoordinatesFromCellListParallel(DM dm, PetscInt spaceDim, PetscSF sfVert, const PetscReal vertexCoords[])
4792d71ae5a4SJacob Faibussowitsch {
4793a47d0d45SMatthew G. Knepley   PetscSection coordSection;
4794a47d0d45SMatthew G. Knepley   Vec          coordinates;
4795a47d0d45SMatthew G. Knepley   PetscScalar *coords;
47961edcf0b2SVaclav Hapla   PetscInt     numVertices, numVerticesAdj, coordSize, v, vStart, vEnd;
4797a47d0d45SMatthew G. Knepley 
4798a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
47999566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
48009566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
48011dca8a05SBarry Smith   PetscCheck(vStart >= 0 && vEnd >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
48029566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, spaceDim));
48039566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfVert, &numVertices, &numVerticesAdj, NULL, NULL));
48041dca8a05SBarry 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);
48059566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
48069566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
48079566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, spaceDim));
48089566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, vStart, vEnd));
48091edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
48109566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, spaceDim));
48119566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, spaceDim));
4812a47d0d45SMatthew G. Knepley   }
48139566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
48149566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
48159566063dSJacob Faibussowitsch   PetscCall(VecCreate(PetscObjectComm((PetscObject)dm), &coordinates));
48169566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, spaceDim));
48179566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
48189566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
48199566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
48209566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
4821a47d0d45SMatthew G. Knepley   {
4822a47d0d45SMatthew G. Knepley     MPI_Datatype coordtype;
4823a47d0d45SMatthew G. Knepley 
4824a47d0d45SMatthew G. Knepley     /* Need a temp buffer for coords if we have complex/single */
48259566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_contiguous(spaceDim, MPIU_SCALAR, &coordtype));
48269566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_commit(&coordtype));
482721016a8bSBarry Smith #if defined(PETSC_USE_COMPLEX)
482821016a8bSBarry Smith     {
482921016a8bSBarry Smith       PetscScalar *svertexCoords;
483021016a8bSBarry Smith       PetscInt     i;
48319566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numVertices * spaceDim, &svertexCoords));
48323612f820SVaclav Hapla       for (i = 0; i < numVertices * spaceDim; i++) svertexCoords[i] = vertexCoords[i];
48339566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastBegin(sfVert, coordtype, svertexCoords, coords, MPI_REPLACE));
48349566063dSJacob Faibussowitsch       PetscCall(PetscSFBcastEnd(sfVert, coordtype, svertexCoords, coords, MPI_REPLACE));
48359566063dSJacob Faibussowitsch       PetscCall(PetscFree(svertexCoords));
483621016a8bSBarry Smith     }
483721016a8bSBarry Smith #else
48389566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sfVert, coordtype, vertexCoords, coords, MPI_REPLACE));
48399566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sfVert, coordtype, vertexCoords, coords, MPI_REPLACE));
484021016a8bSBarry Smith #endif
48419566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_free(&coordtype));
4842a47d0d45SMatthew G. Knepley   }
48439566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
48449566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
48459566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
48469566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
4847a47d0d45SMatthew G. Knepley   PetscFunctionReturn(0);
4848a47d0d45SMatthew G. Knepley }
4849a47d0d45SMatthew G. Knepley 
4850c3edce3dSSatish Balay /*@
4851a1cb98faSBarry Smith   DMPlexCreateFromCellListParallelPetsc - Create distributed `DMPLEX` from a list of vertices for each cell (common mesh generator output)
4852a1cb98faSBarry Smith 
4853a1cb98faSBarry Smith   Collective
4854a47d0d45SMatthew G. Knepley 
4855a47d0d45SMatthew G. Knepley   Input Parameters:
4856a47d0d45SMatthew G. Knepley + comm - The communicator
4857a47d0d45SMatthew G. Knepley . dim - The topological dimension of the mesh
4858a47d0d45SMatthew G. Knepley . numCells - The number of cells owned by this process
4859a1cb98faSBarry Smith . numVertices - The number of vertices owned by this process, or `PETSC_DECIDE`
4860a1cb98faSBarry Smith . NVertices - The global number of vertices, or `PETSC_DECIDE`
4861a47d0d45SMatthew G. Knepley . numCorners - The number of vertices for each cell
4862a47d0d45SMatthew G. Knepley . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
4863a47d0d45SMatthew G. Knepley . cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
4864a47d0d45SMatthew G. Knepley . spaceDim - The spatial dimension used for coordinates
4865a47d0d45SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
4866a47d0d45SMatthew G. Knepley 
4867d8d19677SJose E. Roman   Output Parameters:
4868a1cb98faSBarry Smith + dm - The `DM`
4869a1cb98faSBarry Smith . vertexSF - (Optional) `PetscSF` describing complete vertex ownership
4870be8c289dSNicolas Barral - verticesAdjSaved - (Optional) vertex adjacency array
4871a47d0d45SMatthew G. Knepley 
4872b09969d6SVaclav Hapla   Level: intermediate
4873a47d0d45SMatthew G. Knepley 
4874a1cb98faSBarry Smith   Notes:
4875a1cb98faSBarry Smith   This function is just a convenient sequence of `DMCreate()`, `DMSetType()`, `DMSetDimension()`,
4876a1cb98faSBarry Smith   `DMPlexBuildFromCellListParallel()`, `DMPlexInterpolate()`, `DMPlexBuildCoordinatesFromCellListParallel()`
4877a1cb98faSBarry Smith 
4878a1cb98faSBarry Smith   See `DMPlexBuildFromCellListParallel()` for an example and details about the topology-related parameters.
4879a1cb98faSBarry Smith 
4880a1cb98faSBarry Smith   See `DMPlexBuildCoordinatesFromCellListParallel()` for details about the geometry-related parameters.
4881a1cb98faSBarry Smith 
4882a1cb98faSBarry Smith .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()`
4883a47d0d45SMatthew G. Knepley @*/
4884d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateFromCellListParallelPetsc(MPI_Comm comm, PetscInt dim, PetscInt numCells, PetscInt numVertices, PetscInt NVertices, PetscInt numCorners, PetscBool interpolate, const PetscInt cells[], PetscInt spaceDim, const PetscReal vertexCoords[], PetscSF *vertexSF, PetscInt **verticesAdj, DM *dm)
4885d71ae5a4SJacob Faibussowitsch {
4886a47d0d45SMatthew G. Knepley   PetscSF sfVert;
4887a47d0d45SMatthew G. Knepley 
4888a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
48899566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
48909566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
4891a47d0d45SMatthew G. Knepley   PetscValidLogicalCollectiveInt(*dm, dim, 2);
4892064a246eSJacob Faibussowitsch   PetscValidLogicalCollectiveInt(*dm, spaceDim, 9);
48939566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
48949566063dSJacob Faibussowitsch   PetscCall(DMPlexBuildFromCellListParallel(*dm, numCells, numVertices, NVertices, numCorners, cells, &sfVert, verticesAdj));
4895a47d0d45SMatthew G. Knepley   if (interpolate) {
48965fd9971aSMatthew G. Knepley     DM idm;
4897a47d0d45SMatthew G. Knepley 
48989566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
48999566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
4900a47d0d45SMatthew G. Knepley     *dm = idm;
4901a47d0d45SMatthew G. Knepley   }
49029566063dSJacob Faibussowitsch   PetscCall(DMPlexBuildCoordinatesFromCellListParallel(*dm, spaceDim, sfVert, vertexCoords));
490318d54ad4SMichael Lange   if (vertexSF) *vertexSF = sfVert;
49049566063dSJacob Faibussowitsch   else PetscCall(PetscSFDestroy(&sfVert));
4905a47d0d45SMatthew G. Knepley   PetscFunctionReturn(0);
4906a47d0d45SMatthew G. Knepley }
4907a47d0d45SMatthew G. Knepley 
4908b09969d6SVaclav Hapla /*@C
4909a1cb98faSBarry Smith   DMPlexBuildFromCellList - Build `DMPLEX` topology from a list of vertices for each cell (common mesh generator output)
4910a1cb98faSBarry Smith 
4911a1cb98faSBarry Smith   Collective on dm
49129298eaa6SMatthew G Knepley 
49139298eaa6SMatthew G Knepley   Input Parameters:
4914a1cb98faSBarry Smith + dm - The `DM`
4915b09969d6SVaclav Hapla . numCells - The number of cells owned by this process
4916a1cb98faSBarry Smith . numVertices - The number of vertices owned by this process, or `PETSC_DETERMINE`
49179298eaa6SMatthew G Knepley . numCorners - The number of vertices for each cell
49185e488331SVaclav Hapla - cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
49199298eaa6SMatthew G Knepley 
4920b09969d6SVaclav Hapla   Level: advanced
49219298eaa6SMatthew G Knepley 
4922b09969d6SVaclav Hapla   Notes:
4923b09969d6SVaclav Hapla   Two triangles sharing a face
4924a1cb98faSBarry Smith .vb
49259298eaa6SMatthew G Knepley 
4926a1cb98faSBarry Smith         2
4927a1cb98faSBarry Smith       / | \
4928a1cb98faSBarry Smith      /  |  \
4929a1cb98faSBarry Smith     /   |   \
4930a1cb98faSBarry Smith    0  0 | 1  3
4931a1cb98faSBarry Smith     \   |   /
4932a1cb98faSBarry Smith      \  |  /
4933a1cb98faSBarry Smith       \ | /
4934a1cb98faSBarry Smith         1
4935a1cb98faSBarry Smith .ve
4936a1cb98faSBarry Smith would have input
4937a1cb98faSBarry Smith .vb
4938a1cb98faSBarry Smith   numCells = 2, numVertices = 4
4939a1cb98faSBarry Smith   cells = [0 1 2  1 3 2]
4940a1cb98faSBarry Smith .ve
4941a1cb98faSBarry Smith which would result in the `DMPLEX`
4942a1cb98faSBarry Smith .vb
4943a1cb98faSBarry Smith 
4944a1cb98faSBarry Smith         4
4945a1cb98faSBarry Smith       / | \
4946a1cb98faSBarry Smith      /  |  \
4947a1cb98faSBarry Smith     /   |   \
4948a1cb98faSBarry Smith    2  0 | 1  5
4949a1cb98faSBarry Smith     \   |   /
4950a1cb98faSBarry Smith      \  |  /
4951a1cb98faSBarry Smith       \ | /
4952a1cb98faSBarry Smith         3
4953a1cb98faSBarry Smith .ve
4954a1cb98faSBarry Smith 
4955a1cb98faSBarry Smith   If numVertices is `PETSC_DETERMINE`, it is computed by PETSc as the maximum vertex index in cells + 1.
495625b6865aSVaclav Hapla 
4957b09969d6SVaclav Hapla   Not currently supported in Fortran.
49589298eaa6SMatthew G Knepley 
4959a1cb98faSBarry Smith .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromCellListPetsc()`
4960b09969d6SVaclav Hapla @*/
4961d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildFromCellList(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const PetscInt cells[])
4962d71ae5a4SJacob Faibussowitsch {
4963961cfab0SVaclav Hapla   PetscInt *cones, c, p, dim;
4964b09969d6SVaclav Hapla 
4965b09969d6SVaclav Hapla   PetscFunctionBegin;
49669566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
49679566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
496825b6865aSVaclav Hapla   /* Get/check global number of vertices */
496925b6865aSVaclav Hapla   {
497025b6865aSVaclav Hapla     PetscInt       NVerticesInCells, i;
497125b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
497225b6865aSVaclav Hapla 
497325b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
497425b6865aSVaclav Hapla     NVerticesInCells = PETSC_MIN_INT;
49759371c9d4SSatish Balay     for (i = 0; i < len; i++)
49769371c9d4SSatish Balay       if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
497725b6865aSVaclav Hapla     ++NVerticesInCells;
497825b6865aSVaclav Hapla 
497925b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE) numVertices = NVerticesInCells;
49809371c9d4SSatish Balay     else
49819371c9d4SSatish 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);
498225b6865aSVaclav Hapla   }
49839566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, 0, numCells + numVertices));
498448a46eb9SPierre Jolivet   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, numCorners));
49859566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm));
49869566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCones(dm, &cones));
4987b09969d6SVaclav Hapla   for (c = 0; c < numCells; ++c) {
4988ad540459SPierre Jolivet     for (p = 0; p < numCorners; ++p) cones[c * numCorners + p] = cells[c * numCorners + p] + numCells;
4989b09969d6SVaclav Hapla   }
49909566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
49919566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
49929566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList, dm, 0, 0, 0));
4993b09969d6SVaclav Hapla   PetscFunctionReturn(0);
4994b09969d6SVaclav Hapla }
4995b09969d6SVaclav Hapla 
4996b09969d6SVaclav Hapla /*@C
4997a1cb98faSBarry Smith   DMPlexBuildCoordinatesFromCellList - Build `DM` coordinates from a list of coordinates for each owned vertex (common mesh generator output)
4998a1cb98faSBarry Smith 
4999a1cb98faSBarry Smith   Collective on dm
5000b09969d6SVaclav Hapla 
5001b09969d6SVaclav Hapla   Input Parameters:
5002a1cb98faSBarry Smith + dm - The `DM`
5003b09969d6SVaclav Hapla . spaceDim - The spatial dimension used for coordinates
5004b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
5005b09969d6SVaclav Hapla 
5006b09969d6SVaclav Hapla   Level: advanced
5007b09969d6SVaclav Hapla 
5008a1cb98faSBarry Smith   Fortran Note:
5009b09969d6SVaclav Hapla   Not currently supported in Fortran.
5010b09969d6SVaclav Hapla 
5011a1cb98faSBarry Smith .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellList()`
5012b09969d6SVaclav Hapla @*/
5013d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexBuildCoordinatesFromCellList(DM dm, PetscInt spaceDim, const PetscReal vertexCoords[])
5014d71ae5a4SJacob Faibussowitsch {
5015b09969d6SVaclav Hapla   PetscSection coordSection;
5016b09969d6SVaclav Hapla   Vec          coordinates;
5017b09969d6SVaclav Hapla   DM           cdm;
5018b09969d6SVaclav Hapla   PetscScalar *coords;
50191edcf0b2SVaclav Hapla   PetscInt     v, vStart, vEnd, d;
5020b09969d6SVaclav Hapla 
5021b09969d6SVaclav Hapla   PetscFunctionBegin;
50229566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
50239566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
50241dca8a05SBarry Smith   PetscCheck(vStart >= 0 && vEnd >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
50259566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, spaceDim));
50269566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
50279566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
50289566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, spaceDim));
50299566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, vStart, vEnd));
50301edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
50319566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, spaceDim));
50329566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, spaceDim));
5033b09969d6SVaclav Hapla   }
50349566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
5035b09969d6SVaclav Hapla 
50369566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
50379566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(cdm, &coordinates));
50389566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, spaceDim));
50399566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
50409566063dSJacob Faibussowitsch   PetscCall(VecGetArrayWrite(coordinates, &coords));
50411edcf0b2SVaclav Hapla   for (v = 0; v < vEnd - vStart; ++v) {
5042ad540459SPierre Jolivet     for (d = 0; d < spaceDim; ++d) coords[v * spaceDim + d] = vertexCoords[v * spaceDim + d];
5043b09969d6SVaclav Hapla   }
50449566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayWrite(coordinates, &coords));
50459566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
50469566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
50479566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList, dm, 0, 0, 0));
5048b09969d6SVaclav Hapla   PetscFunctionReturn(0);
5049b09969d6SVaclav Hapla }
5050b09969d6SVaclav Hapla 
5051b09969d6SVaclav Hapla /*@
5052a1cb98faSBarry Smith   DMPlexCreateFromCellListPetsc - Create `DMPLEX` from a list of vertices for each cell (common mesh generator output), but only process 0 takes in the input
50533df08285SMatthew G. Knepley 
5054a1cb98faSBarry Smith   Collective
5055b09969d6SVaclav Hapla 
5056b09969d6SVaclav Hapla   Input Parameters:
5057b09969d6SVaclav Hapla + comm - The communicator
5058b09969d6SVaclav Hapla . dim - The topological dimension of the mesh
50593df08285SMatthew G. Knepley . numCells - The number of cells, only on process 0
5060a1cb98faSBarry Smith . numVertices - The number of vertices owned by this process, or `PETSC_DECIDE`, only on process 0
50613df08285SMatthew G. Knepley . numCorners - The number of vertices for each cell, only on process 0
5062b09969d6SVaclav Hapla . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
50633df08285SMatthew G. Knepley . cells - An array of numCells*numCorners numbers, the vertices for each cell, only on process 0
5064b09969d6SVaclav Hapla . spaceDim - The spatial dimension used for coordinates
50653df08285SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex, only on process 0
5066b09969d6SVaclav Hapla 
5067b09969d6SVaclav Hapla   Output Parameter:
5068a1cb98faSBarry Smith . dm - The `DM`, which only has points on process 0
506925b6865aSVaclav Hapla 
5070b09969d6SVaclav Hapla   Level: intermediate
5071b09969d6SVaclav Hapla 
5072a1cb98faSBarry Smith   Notes:
5073a1cb98faSBarry Smith   This function is just a convenient sequence of `DMCreate()`, `DMSetType()`, `DMSetDimension()`, `DMPlexBuildFromCellList()`,
5074a1cb98faSBarry Smith   `DMPlexInterpolate()`, `DMPlexBuildCoordinatesFromCellList()`
5075a1cb98faSBarry Smith 
5076a1cb98faSBarry Smith   See `DMPlexBuildFromCellList()` for an example and details about the topology-related parameters.
5077a1cb98faSBarry Smith   See `DMPlexBuildCoordinatesFromCellList()` for details about the geometry-related parameters.
5078a1cb98faSBarry Smith   See `DMPlexCreateFromCellListParallelPetsc()` for parallel input
5079a1cb98faSBarry Smith 
5080a1cb98faSBarry Smith .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildFromCellList()`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()`
50819298eaa6SMatthew G Knepley @*/
5082d71ae5a4SJacob 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)
5083d71ae5a4SJacob Faibussowitsch {
50843df08285SMatthew G. Knepley   PetscMPIInt rank;
50859298eaa6SMatthew G Knepley 
50869298eaa6SMatthew G Knepley   PetscFunctionBegin;
508728b400f6SJacob 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.");
50889566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
50899566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
50909566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
50919566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
5092c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildFromCellList(*dm, numCells, numVertices, numCorners, cells));
50939566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildFromCellList(*dm, 0, 0, 0, NULL));
50949298eaa6SMatthew G Knepley   if (interpolate) {
50955fd9971aSMatthew G. Knepley     DM idm;
50969298eaa6SMatthew G Knepley 
50979566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
50989566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
50999298eaa6SMatthew G Knepley     *dm = idm;
51009298eaa6SMatthew G Knepley   }
5101c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, vertexCoords));
51029566063dSJacob Faibussowitsch   else PetscCall(DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, NULL));
51039298eaa6SMatthew G Knepley   PetscFunctionReturn(0);
51049298eaa6SMatthew G Knepley }
51059298eaa6SMatthew G Knepley 
5106939f6067SMatthew G. Knepley /*@
5107939f6067SMatthew G. Knepley   DMPlexCreateFromDAG - This takes as input the adjacency-list representation of the Directed Acyclic Graph (Hasse Diagram) encoding a mesh, and produces a DM
5108939f6067SMatthew G. Knepley 
5109939f6067SMatthew G. Knepley   Input Parameters:
5110c73cfb54SMatthew G. Knepley + dm - The empty DM object, usually from DMCreate() and DMSetDimension()
5111939f6067SMatthew G. Knepley . depth - The depth of the DAG
5112367003a6SStefano Zampini . numPoints - Array of size depth + 1 containing the number of points at each depth
5113939f6067SMatthew G. Knepley . coneSize - The cone size of each point
5114939f6067SMatthew G. Knepley . cones - The concatenation of the cone points for each point, the cone list must be oriented correctly for each point
5115939f6067SMatthew G. Knepley . coneOrientations - The orientation of each cone point
5116367003a6SStefano Zampini - vertexCoords - An array of numPoints[0]*spacedim numbers representing the coordinates of each vertex, with spacedim the value set via DMSetCoordinateDim()
5117939f6067SMatthew G. Knepley 
5118939f6067SMatthew G. Knepley   Output Parameter:
5119939f6067SMatthew G. Knepley . dm - The DM
5120939f6067SMatthew G. Knepley 
5121a1cb98faSBarry Smith   Note:
5122a1cb98faSBarry Smith   Two triangles sharing a face would have input
5123a1cb98faSBarry Smith .vb
5124a1cb98faSBarry Smith   depth = 1, numPoints = [4 2], coneSize = [3 3 0 0 0 0]
5125a1cb98faSBarry Smith   cones = [2 3 4  3 5 4], coneOrientations = [0 0 0  0 0 0]
5126a1cb98faSBarry Smith  vertexCoords = [-1.0 0.0  0.0 -1.0  0.0 1.0  1.0 0.0]
5127a1cb98faSBarry Smith .ve
5128939f6067SMatthew G. Knepley which would result in the DMPlex
5129a1cb98faSBarry Smith .vb
5130a1cb98faSBarry Smith         4
5131a1cb98faSBarry Smith       / | \
5132a1cb98faSBarry Smith      /  |  \
5133a1cb98faSBarry Smith     /   |   \
5134a1cb98faSBarry Smith    2  0 | 1  5
5135a1cb98faSBarry Smith     \   |   /
5136a1cb98faSBarry Smith      \  |  /
5137a1cb98faSBarry Smith       \ | /
5138a1cb98faSBarry Smith         3
5139a1cb98faSBarry Smith .ve
5140a1cb98faSBarry Smith  Notice that all points are numbered consecutively, unlike `DMPlexCreateFromCellListPetsc()`
5141939f6067SMatthew G. Knepley 
5142939f6067SMatthew G. Knepley   Level: advanced
5143939f6067SMatthew G. Knepley 
5144a1cb98faSBarry Smith .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`
5145939f6067SMatthew G. Knepley @*/
5146d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateFromDAG(DM dm, PetscInt depth, const PetscInt numPoints[], const PetscInt coneSize[], const PetscInt cones[], const PetscInt coneOrientations[], const PetscScalar vertexCoords[])
5147d71ae5a4SJacob Faibussowitsch {
51489298eaa6SMatthew G Knepley   Vec          coordinates;
51499298eaa6SMatthew G Knepley   PetscSection coordSection;
51509298eaa6SMatthew G Knepley   PetscScalar *coords;
5151811e8653SToby Isaac   PetscInt     coordSize, firstVertex = -1, pStart = 0, pEnd = 0, p, v, dim, dimEmbed, d, off;
51529298eaa6SMatthew G Knepley 
51539298eaa6SMatthew G Knepley   PetscFunctionBegin;
51549566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
51559566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dimEmbed));
515663a3b9bcSJacob Faibussowitsch   PetscCheck(dimEmbed >= dim, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Embedding dimension %" PetscInt_FMT " cannot be less than intrinsic dimension %" PetscInt_FMT, dimEmbed, dim);
51579298eaa6SMatthew G Knepley   for (d = 0; d <= depth; ++d) pEnd += numPoints[d];
51589566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, pStart, pEnd));
51599298eaa6SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
51609566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeSize(dm, p, coneSize[p - pStart]));
5161ad540459SPierre Jolivet     if (firstVertex < 0 && !coneSize[p - pStart]) firstVertex = p - pStart;
516297e052ccSToby Isaac   }
51631dca8a05SBarry Smith   PetscCheck(firstVertex >= 0 || !numPoints[0], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Expected %" PetscInt_FMT " vertices but could not find any", numPoints[0]);
51649566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm)); /* Allocate space for cones */
51659298eaa6SMatthew G Knepley   for (p = pStart, off = 0; p < pEnd; off += coneSize[p - pStart], ++p) {
51669566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(dm, p, &cones[off]));
51679566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeOrientation(dm, p, &coneOrientations[off]));
51689298eaa6SMatthew G Knepley   }
51699566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
51709566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
51719298eaa6SMatthew G Knepley   /* Build coordinates */
51729566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
51739566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
51749566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dimEmbed));
51759566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex + numPoints[0]));
51769298eaa6SMatthew G Knepley   for (v = firstVertex; v < firstVertex + numPoints[0]; ++v) {
51779566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, dimEmbed));
51789566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dimEmbed));
51799298eaa6SMatthew G Knepley   }
51809566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
51819566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
51829566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
51839566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
51849566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
51859566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, dimEmbed));
51869566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
51879318fe57SMatthew G. Knepley   if (vertexCoords) {
51889566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
51899298eaa6SMatthew G Knepley     for (v = 0; v < numPoints[0]; ++v) {
51909298eaa6SMatthew G Knepley       PetscInt off;
51919298eaa6SMatthew G Knepley 
51929566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, v + firstVertex, &off));
5193ad540459SPierre Jolivet       for (d = 0; d < dimEmbed; ++d) coords[off + d] = vertexCoords[v * dimEmbed + d];
51949298eaa6SMatthew G Knepley     }
51959318fe57SMatthew G. Knepley   }
51969566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
51979566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
51989566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
51999298eaa6SMatthew G Knepley   PetscFunctionReturn(0);
52009298eaa6SMatthew G Knepley }
52018415267dSToby Isaac 
5202ca522641SMatthew G. Knepley /*@C
5203a1cb98faSBarry Smith   DMPlexCreateCellVertexFromFile - Create a `DMPLEX` mesh from a simple cell-vertex file.
5204a1cb98faSBarry Smith 
5205a1cb98faSBarry Smith   Collective
52068ca92349SMatthew G. Knepley 
52078ca92349SMatthew G. Knepley + comm        - The MPI communicator
52088ca92349SMatthew G. Knepley . filename    - Name of the .dat file
52098ca92349SMatthew G. Knepley - interpolate - Create faces and edges in the mesh
52108ca92349SMatthew G. Knepley 
52118ca92349SMatthew G. Knepley   Output Parameter:
5212a1cb98faSBarry Smith . dm  - The `DM` object representing the mesh
52138ca92349SMatthew G. Knepley 
52148ca92349SMatthew G. Knepley   Level: beginner
52158ca92349SMatthew G. Knepley 
5216a1cb98faSBarry Smith   Note:
5217a1cb98faSBarry Smith   The format is the simplest possible:
5218a1cb98faSBarry Smith .vb
5219a1cb98faSBarry Smith   Ne
5220a1cb98faSBarry Smith   v0 v1 ... vk
5221a1cb98faSBarry Smith   Nv
5222a1cb98faSBarry Smith   x y z marker
5223a1cb98faSBarry Smith .ve
5224a1cb98faSBarry Smith 
5225a1cb98faSBarry Smith   Developer Note:
5226a1cb98faSBarry Smith   Should use a `PetscViewer` not a filename
5227a1cb98faSBarry Smith 
5228a1cb98faSBarry Smith .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromFile()`, `DMPlexCreateMedFromFile()`, `DMPlexCreateGmsh()`, `DMPlexCreate()`
52298ca92349SMatthew G. Knepley @*/
5230d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateCellVertexFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
5231d71ae5a4SJacob Faibussowitsch {
52328ca92349SMatthew G. Knepley   DMLabel      marker;
52338ca92349SMatthew G. Knepley   PetscViewer  viewer;
52348ca92349SMatthew G. Knepley   Vec          coordinates;
52358ca92349SMatthew G. Knepley   PetscSection coordSection;
52368ca92349SMatthew G. Knepley   PetscScalar *coords;
52378ca92349SMatthew G. Knepley   char         line[PETSC_MAX_PATH_LEN];
52388ca92349SMatthew G. Knepley   PetscInt     dim = 3, cdim = 3, coordSize, v, c, d;
52398ca92349SMatthew G. Knepley   PetscMPIInt  rank;
5240f8d5e320SMatthew G. Knepley   int          snum, Nv, Nc, Ncn, Nl;
52418ca92349SMatthew G. Knepley 
52428ca92349SMatthew G. Knepley   PetscFunctionBegin;
52439566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
52449566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, &viewer));
52459566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(viewer, PETSCVIEWERASCII));
52469566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetMode(viewer, FILE_MODE_READ));
52479566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetName(viewer, filename));
5248dd400576SPatrick Sanan   if (rank == 0) {
52499566063dSJacob Faibussowitsch     PetscCall(PetscViewerRead(viewer, line, 4, NULL, PETSC_STRING));
5250f8d5e320SMatthew G. Knepley     snum = sscanf(line, "%d %d %d %d", &Nc, &Nv, &Ncn, &Nl);
525108401ef6SPierre Jolivet     PetscCheck(snum == 4, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
525225ce1634SJed Brown   } else {
5253f8d5e320SMatthew G. Knepley     Nc = Nv = Ncn = Nl = 0;
52548ca92349SMatthew G. Knepley   }
52559566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
52569566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
52579566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(*dm, 0, Nc + Nv));
52589566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
52599566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*dm, cdim));
52608ca92349SMatthew G. Knepley   /* Read topology */
5261dd400576SPatrick Sanan   if (rank == 0) {
5262f8d5e320SMatthew G. Knepley     char     format[PETSC_MAX_PATH_LEN];
5263f8d5e320SMatthew G. Knepley     PetscInt cone[8];
52648ca92349SMatthew G. Knepley     int      vbuf[8], v;
52658ca92349SMatthew G. Knepley 
52669371c9d4SSatish Balay     for (c = 0; c < Ncn; ++c) {
52679371c9d4SSatish Balay       format[c * 3 + 0] = '%';
52689371c9d4SSatish Balay       format[c * 3 + 1] = 'd';
52699371c9d4SSatish Balay       format[c * 3 + 2] = ' ';
52709371c9d4SSatish Balay     }
5271f8d5e320SMatthew G. Knepley     format[Ncn * 3 - 1] = '\0';
52729566063dSJacob Faibussowitsch     for (c = 0; c < Nc; ++c) PetscCall(DMPlexSetConeSize(*dm, c, Ncn));
52739566063dSJacob Faibussowitsch     PetscCall(DMSetUp(*dm));
52748ca92349SMatthew G. Knepley     for (c = 0; c < Nc; ++c) {
52759566063dSJacob Faibussowitsch       PetscCall(PetscViewerRead(viewer, line, Ncn, NULL, PETSC_STRING));
5276f8d5e320SMatthew G. Knepley       switch (Ncn) {
5277d71ae5a4SJacob Faibussowitsch       case 2:
5278d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1]);
5279d71ae5a4SJacob Faibussowitsch         break;
5280d71ae5a4SJacob Faibussowitsch       case 3:
5281d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2]);
5282d71ae5a4SJacob Faibussowitsch         break;
5283d71ae5a4SJacob Faibussowitsch       case 4:
5284d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3]);
5285d71ae5a4SJacob Faibussowitsch         break;
5286d71ae5a4SJacob Faibussowitsch       case 6:
5287d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5]);
5288d71ae5a4SJacob Faibussowitsch         break;
5289d71ae5a4SJacob Faibussowitsch       case 8:
5290d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5], &vbuf[6], &vbuf[7]);
5291d71ae5a4SJacob Faibussowitsch         break;
5292d71ae5a4SJacob Faibussowitsch       default:
5293d71ae5a4SJacob Faibussowitsch         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No cell shape with %d vertices", Ncn);
5294f8d5e320SMatthew G. Knepley       }
529508401ef6SPierre Jolivet       PetscCheck(snum == Ncn, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
5296f8d5e320SMatthew G. Knepley       for (v = 0; v < Ncn; ++v) cone[v] = vbuf[v] + Nc;
52978ca92349SMatthew G. Knepley       /* Hexahedra are inverted */
5298f8d5e320SMatthew G. Knepley       if (Ncn == 8) {
52998ca92349SMatthew G. Knepley         PetscInt tmp = cone[1];
53008ca92349SMatthew G. Knepley         cone[1]      = cone[3];
53018ca92349SMatthew G. Knepley         cone[3]      = tmp;
53028ca92349SMatthew G. Knepley       }
53039566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(*dm, c, cone));
53048ca92349SMatthew G. Knepley     }
53058ca92349SMatthew G. Knepley   }
53069566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(*dm));
53079566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(*dm));
53088ca92349SMatthew G. Knepley   /* Read coordinates */
53099566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(*dm, &coordSection));
53109566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
53119566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, cdim));
53129566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, Nc, Nc + Nv));
53138ca92349SMatthew G. Knepley   for (v = Nc; v < Nc + Nv; ++v) {
53149566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, cdim));
53159566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, cdim));
53168ca92349SMatthew G. Knepley   }
53179566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
53189566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
53199566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
53209566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject)coordinates, "coordinates"));
53219566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
53229566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, cdim));
53239566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
53249566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
5325dd400576SPatrick Sanan   if (rank == 0) {
5326f8d5e320SMatthew G. Knepley     char   format[PETSC_MAX_PATH_LEN];
53278ca92349SMatthew G. Knepley     double x[3];
5328f8d5e320SMatthew G. Knepley     int    l, val[3];
53298ca92349SMatthew G. Knepley 
5330f8d5e320SMatthew G. Knepley     if (Nl) {
53319371c9d4SSatish Balay       for (l = 0; l < Nl; ++l) {
53329371c9d4SSatish Balay         format[l * 3 + 0] = '%';
53339371c9d4SSatish Balay         format[l * 3 + 1] = 'd';
53349371c9d4SSatish Balay         format[l * 3 + 2] = ' ';
53359371c9d4SSatish Balay       }
5336f8d5e320SMatthew G. Knepley       format[Nl * 3 - 1] = '\0';
53379566063dSJacob Faibussowitsch       PetscCall(DMCreateLabel(*dm, "marker"));
53389566063dSJacob Faibussowitsch       PetscCall(DMGetLabel(*dm, "marker", &marker));
5339f8d5e320SMatthew G. Knepley     }
53408ca92349SMatthew G. Knepley     for (v = 0; v < Nv; ++v) {
53419566063dSJacob Faibussowitsch       PetscCall(PetscViewerRead(viewer, line, 3 + Nl, NULL, PETSC_STRING));
5342f8d5e320SMatthew G. Knepley       snum = sscanf(line, "%lg %lg %lg", &x[0], &x[1], &x[2]);
534308401ef6SPierre Jolivet       PetscCheck(snum == 3, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
5344f8d5e320SMatthew G. Knepley       switch (Nl) {
5345d71ae5a4SJacob Faibussowitsch       case 0:
5346d71ae5a4SJacob Faibussowitsch         snum = 0;
5347d71ae5a4SJacob Faibussowitsch         break;
5348d71ae5a4SJacob Faibussowitsch       case 1:
5349d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &val[0]);
5350d71ae5a4SJacob Faibussowitsch         break;
5351d71ae5a4SJacob Faibussowitsch       case 2:
5352d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &val[0], &val[1]);
5353d71ae5a4SJacob Faibussowitsch         break;
5354d71ae5a4SJacob Faibussowitsch       case 3:
5355d71ae5a4SJacob Faibussowitsch         snum = sscanf(line, format, &val[0], &val[1], &val[2]);
5356d71ae5a4SJacob Faibussowitsch         break;
5357d71ae5a4SJacob Faibussowitsch       default:
5358d71ae5a4SJacob Faibussowitsch         SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Request support for %d labels", Nl);
5359f8d5e320SMatthew G. Knepley       }
536008401ef6SPierre Jolivet       PetscCheck(snum == Nl, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
53618ca92349SMatthew G. Knepley       for (d = 0; d < cdim; ++d) coords[v * cdim + d] = x[d];
53629566063dSJacob Faibussowitsch       for (l = 0; l < Nl; ++l) PetscCall(DMLabelSetValue(marker, v + Nc, val[l]));
53638ca92349SMatthew G. Knepley     }
53648ca92349SMatthew G. Knepley   }
53659566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
53669566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(*dm, coordinates));
53679566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
53689566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(&viewer));
53698ca92349SMatthew G. Knepley   if (interpolate) {
53708ca92349SMatthew G. Knepley     DM      idm;
53718ca92349SMatthew G. Knepley     DMLabel bdlabel;
53728ca92349SMatthew G. Knepley 
53739566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
53749566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
53758ca92349SMatthew G. Knepley     *dm = idm;
53768ca92349SMatthew G. Knepley 
5377f8d5e320SMatthew G. Knepley     if (!Nl) {
53789566063dSJacob Faibussowitsch       PetscCall(DMCreateLabel(*dm, "marker"));
53799566063dSJacob Faibussowitsch       PetscCall(DMGetLabel(*dm, "marker", &bdlabel));
53809566063dSJacob Faibussowitsch       PetscCall(DMPlexMarkBoundaryFaces(*dm, PETSC_DETERMINE, bdlabel));
53819566063dSJacob Faibussowitsch       PetscCall(DMPlexLabelComplete(*dm, bdlabel));
53828ca92349SMatthew G. Knepley     }
5383f8d5e320SMatthew G. Knepley   }
53848ca92349SMatthew G. Knepley   PetscFunctionReturn(0);
53858ca92349SMatthew G. Knepley }
53868ca92349SMatthew G. Knepley 
53878ca92349SMatthew G. Knepley /*@C
5388a1cb98faSBarry Smith   DMPlexCreateFromFile - This takes a filename and produces a `DM`
5389a1cb98faSBarry Smith 
5390a1cb98faSBarry Smith   Collective
5391ca522641SMatthew G. Knepley 
5392ca522641SMatthew G. Knepley   Input Parameters:
5393ca522641SMatthew G. Knepley + comm - The communicator
5394ca522641SMatthew G. Knepley . filename - A file name
5395a1cb98faSBarry Smith . plexname - The object name of the resulting `DM`, also used for intra-datafile lookup by some formats
5396ca522641SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
5397ca522641SMatthew G. Knepley 
5398ca522641SMatthew G. Knepley   Output Parameter:
5399a1cb98faSBarry Smith . dm - The `DM`
5400ca522641SMatthew G. Knepley 
5401a1cb98faSBarry Smith   Options Database Key:
5402a1cb98faSBarry Smith . -dm_plex_create_from_hdf5_xdmf - use the `PETSC_VIEWER_HDF5_XDMF` format for reading HDF5
540302ef0d99SVaclav Hapla 
5404bca97951SVaclav Hapla   Use -dm_plex_create_ prefix to pass options to the internal PetscViewer, e.g.
5405bca97951SVaclav Hapla $ -dm_plex_create_viewer_hdf5_collective
5406bca97951SVaclav Hapla 
5407ca522641SMatthew G. Knepley   Level: beginner
5408ca522641SMatthew G. Knepley 
5409a1cb98faSBarry Smith   Notes:
5410a1cb98faSBarry Smith   Using `PETSCVIEWERHDF5` type with `PETSC_VIEWER_HDF5_PETSC` format, one can save multiple `DMPLEX`
5411a1cb98faSBarry Smith   meshes in a single HDF5 file. This in turn requires one to name the `DMPLEX` object with `PetscObjectSetName()`
5412a1cb98faSBarry Smith   before saving it with `DMView()` and before loading it with `DMLoad()` for identification of the mesh object.
5413a1cb98faSBarry Smith   The input parameter name is thus used to name the `DMPLEX` object when `DMPlexCreateFromFile()` internally
5414a1cb98faSBarry Smith   calls `DMLoad()`. Currently, name is ignored for other viewer types and/or formats.
5415a1cb98faSBarry Smith 
5416a1cb98faSBarry Smith .seealso: [](chapter_unstructured), `DM`, `DMPLEX`, `DMPlexCreateFromDAG()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`, `PetscObjectSetName()`, `DMView()`, `DMLoad()`
5417ca522641SMatthew G. Knepley @*/
5418d71ae5a4SJacob Faibussowitsch PetscErrorCode DMPlexCreateFromFile(MPI_Comm comm, const char filename[], const char plexname[], PetscBool interpolate, DM *dm)
5419d71ae5a4SJacob Faibussowitsch {
5420ef3a5affSJacob Faibussowitsch   const char  extGmsh[]      = ".msh";
5421ef3a5affSJacob Faibussowitsch   const char  extGmsh2[]     = ".msh2";
5422ef3a5affSJacob Faibussowitsch   const char  extGmsh4[]     = ".msh4";
5423ef3a5affSJacob Faibussowitsch   const char  extCGNS[]      = ".cgns";
5424ef3a5affSJacob Faibussowitsch   const char  extExodus[]    = ".exo";
5425ef3a5affSJacob Faibussowitsch   const char  extExodus_e[]  = ".e";
5426ef3a5affSJacob Faibussowitsch   const char  extGenesis[]   = ".gen";
5427ef3a5affSJacob Faibussowitsch   const char  extFluent[]    = ".cas";
5428ef3a5affSJacob Faibussowitsch   const char  extHDF5[]      = ".h5";
5429ef3a5affSJacob Faibussowitsch   const char  extMed[]       = ".med";
5430ef3a5affSJacob Faibussowitsch   const char  extPLY[]       = ".ply";
5431ef3a5affSJacob Faibussowitsch   const char  extEGADSLite[] = ".egadslite";
5432ef3a5affSJacob Faibussowitsch   const char  extEGADS[]     = ".egads";
5433ef3a5affSJacob Faibussowitsch   const char  extIGES[]      = ".igs";
5434ef3a5affSJacob Faibussowitsch   const char  extSTEP[]      = ".stp";
5435ef3a5affSJacob Faibussowitsch   const char  extCV[]        = ".dat";
5436ca522641SMatthew G. Knepley   size_t      len;
5437c1cad2e7SMatthew G. Knepley   PetscBool   isGmsh, isGmsh2, isGmsh4, isCGNS, isExodus, isGenesis, isFluent, isHDF5, isMed, isPLY, isEGADSLite, isEGADS, isIGES, isSTEP, isCV;
5438ca522641SMatthew G. Knepley   PetscMPIInt rank;
5439ca522641SMatthew G. Knepley 
5440ca522641SMatthew G. Knepley   PetscFunctionBegin;
54415d80c0bfSVaclav Hapla   PetscValidCharPointer(filename, 2);
54420d862eaeSPierre Jolivet   if (plexname) PetscValidCharPointer(plexname, 3);
5443cd7e8a5eSksagiyam   PetscValidPointer(dm, 5);
54449566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
54459566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_CreateFromFile, 0, 0, 0, 0));
54469566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
54479566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(filename, &len));
544828b400f6SJacob Faibussowitsch   PetscCheck(len, comm, PETSC_ERR_ARG_WRONG, "Filename must be a valid path");
5449ef3a5affSJacob Faibussowitsch 
54509371c9d4SSatish Balay #define CheckExtension(extension__, is_extension__) \
54519371c9d4SSatish Balay   do { \
5452274aaeaaSJacob Faibussowitsch     PetscAssert(sizeof(extension__), comm, PETSC_ERR_PLIB, "Zero-size extension: %s", extension__); \
5453274aaeaaSJacob Faibussowitsch     /* don't count the null-terminator at the end */ \
5454274aaeaaSJacob Faibussowitsch     const size_t ext_len = sizeof(extension__) - 1; \
5455274aaeaaSJacob Faibussowitsch     if (len < ext_len) { \
5456ef3a5affSJacob Faibussowitsch       is_extension__ = PETSC_FALSE; \
5457ef3a5affSJacob Faibussowitsch     } else { \
5458274aaeaaSJacob Faibussowitsch       PetscCall(PetscStrncmp(filename + len - ext_len, extension__, ext_len, &is_extension__)); \
5459ef3a5affSJacob Faibussowitsch     } \
5460ef3a5affSJacob Faibussowitsch   } while (0)
5461ef3a5affSJacob Faibussowitsch 
5462ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh, isGmsh);
5463ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh2, isGmsh2);
5464ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh4, isGmsh4);
5465ef3a5affSJacob Faibussowitsch   CheckExtension(extCGNS, isCGNS);
5466ef3a5affSJacob Faibussowitsch   CheckExtension(extExodus, isExodus);
5467ef3a5affSJacob Faibussowitsch   if (!isExodus) CheckExtension(extExodus_e, isExodus);
5468ef3a5affSJacob Faibussowitsch   CheckExtension(extGenesis, isGenesis);
5469ef3a5affSJacob Faibussowitsch   CheckExtension(extFluent, isFluent);
5470ef3a5affSJacob Faibussowitsch   CheckExtension(extHDF5, isHDF5);
5471ef3a5affSJacob Faibussowitsch   CheckExtension(extMed, isMed);
5472ef3a5affSJacob Faibussowitsch   CheckExtension(extPLY, isPLY);
5473ef3a5affSJacob Faibussowitsch   CheckExtension(extEGADSLite, isEGADSLite);
5474ef3a5affSJacob Faibussowitsch   CheckExtension(extEGADS, isEGADS);
5475ef3a5affSJacob Faibussowitsch   CheckExtension(extIGES, isIGES);
5476ef3a5affSJacob Faibussowitsch   CheckExtension(extSTEP, isSTEP);
5477ef3a5affSJacob Faibussowitsch   CheckExtension(extCV, isCV);
5478ef3a5affSJacob Faibussowitsch 
5479ef3a5affSJacob Faibussowitsch #undef CheckExtension
5480ef3a5affSJacob Faibussowitsch 
5481de78e4feSLisandro Dalcin   if (isGmsh || isGmsh2 || isGmsh4) {
54829566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateGmshFromFile(comm, filename, interpolate, dm));
5483ca522641SMatthew G. Knepley   } else if (isCGNS) {
54849566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCGNSFromFile(comm, filename, interpolate, dm));
548590c68965SMatthew G. Knepley   } else if (isExodus || isGenesis) {
54869566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateExodusFromFile(comm, filename, interpolate, dm));
54872f0bd6dcSMichael Lange   } else if (isFluent) {
54889566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFluentFromFile(comm, filename, interpolate, dm));
5489cc2f8f65SMatthew G. Knepley   } else if (isHDF5) {
54909c48423bSVaclav Hapla     PetscBool   load_hdf5_xdmf = PETSC_FALSE;
5491cc2f8f65SMatthew G. Knepley     PetscViewer viewer;
5492cc2f8f65SMatthew G. Knepley 
549343b242b4SVaclav 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 */
54949566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(&filename[PetscMax(0, len - 8)], ".xdmf", 5, &load_hdf5_xdmf));
54959566063dSJacob Faibussowitsch     PetscCall(PetscOptionsGetBool(NULL, NULL, "-dm_plex_create_from_hdf5_xdmf", &load_hdf5_xdmf, NULL));
54969566063dSJacob Faibussowitsch     PetscCall(PetscViewerCreate(comm, &viewer));
54979566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetType(viewer, PETSCVIEWERHDF5));
54989566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetOptionsPrefix(viewer, "dm_plex_create_"));
54999566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetFromOptions(viewer));
55009566063dSJacob Faibussowitsch     PetscCall(PetscViewerFileSetMode(viewer, FILE_MODE_READ));
55019566063dSJacob Faibussowitsch     PetscCall(PetscViewerFileSetName(viewer, filename));
5502cd7e8a5eSksagiyam 
55039566063dSJacob Faibussowitsch     PetscCall(DMCreate(comm, dm));
55049566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)(*dm), plexname));
55059566063dSJacob Faibussowitsch     PetscCall(DMSetType(*dm, DMPLEX));
55069566063dSJacob Faibussowitsch     if (load_hdf5_xdmf) PetscCall(PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_XDMF));
55079566063dSJacob Faibussowitsch     PetscCall(DMLoad(*dm, viewer));
55089566063dSJacob Faibussowitsch     if (load_hdf5_xdmf) PetscCall(PetscViewerPopFormat(viewer));
55099566063dSJacob Faibussowitsch     PetscCall(PetscViewerDestroy(&viewer));
55105fd9971aSMatthew G. Knepley 
55115fd9971aSMatthew G. Knepley     if (interpolate) {
55125fd9971aSMatthew G. Knepley       DM idm;
55135fd9971aSMatthew G. Knepley 
55149566063dSJacob Faibussowitsch       PetscCall(DMPlexInterpolate(*dm, &idm));
55159566063dSJacob Faibussowitsch       PetscCall(DMDestroy(dm));
55165fd9971aSMatthew G. Knepley       *dm = idm;
55175fd9971aSMatthew G. Knepley     }
5518707dd687SMichael Lange   } else if (isMed) {
55199566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateMedFromFile(comm, filename, interpolate, dm));
5520f2801cd6SMatthew G. Knepley   } else if (isPLY) {
55219566063dSJacob Faibussowitsch     PetscCall(DMPlexCreatePLYFromFile(comm, filename, interpolate, dm));
5522c1cad2e7SMatthew G. Knepley   } else if (isEGADSLite || isEGADS || isIGES || isSTEP) {
55239566063dSJacob Faibussowitsch     if (isEGADSLite) PetscCall(DMPlexCreateEGADSLiteFromFile(comm, filename, dm));
55249566063dSJacob Faibussowitsch     else PetscCall(DMPlexCreateEGADSFromFile(comm, filename, dm));
55257bee2925SMatthew Knepley     if (!interpolate) {
55267bee2925SMatthew Knepley       DM udm;
55277bee2925SMatthew Knepley 
55289566063dSJacob Faibussowitsch       PetscCall(DMPlexUninterpolate(*dm, &udm));
55299566063dSJacob Faibussowitsch       PetscCall(DMDestroy(dm));
55307bee2925SMatthew Knepley       *dm = udm;
55317bee2925SMatthew Knepley     }
55328ca92349SMatthew G. Knepley   } else if (isCV) {
55339566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCellVertexFromFile(comm, filename, interpolate, dm));
553498921bdaSJacob Faibussowitsch   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot load file %s: unrecognized extension", filename);
55359566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(plexname, &len));
55369566063dSJacob Faibussowitsch   if (len) PetscCall(PetscObjectSetName((PetscObject)(*dm), plexname));
55379566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_CreateFromFile, 0, 0, 0, 0));
5538ca522641SMatthew G. Knepley   PetscFunctionReturn(0);
5539ca522641SMatthew G. Knepley }
55409f6c5813SMatthew G. Knepley /*@C
55419f6c5813SMatthew 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.
55429f6c5813SMatthew G. Knepley 
55439f6c5813SMatthew G. Knepley   Input Parameter:
55449f6c5813SMatthew G. Knepley . tr - The `DMPlexTransform`
55459f6c5813SMatthew G. Knepley 
55469f6c5813SMatthew G. Knepley   Output Parameter:
55479f6c5813SMatthew G. Knepley . dm - The `DM`
55489f6c5813SMatthew G. Knepley 
55499f6c5813SMatthew G. Knepley   Notes:
55509f6c5813SMatthew G. Knepley   An emphemeral mesh is one that is not stored concretely, as in the default Plex implementation, but rather is produced on the fly in response to queries, using information from the transform and the base mesh.
55519f6c5813SMatthew G. Knepley 
55529f6c5813SMatthew G. Knepley   Level: beginner
55539f6c5813SMatthew G. Knepley 
55549f6c5813SMatthew G. Knepley .seealso: `DMPlexCreateFromFile`, `DMPlexCreateFromDAG()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`
55559f6c5813SMatthew G. Knepley @*/
55569f6c5813SMatthew G. Knepley PetscErrorCode DMPlexCreateEphemeral(DMPlexTransform tr, DM *dm)
55579f6c5813SMatthew G. Knepley {
55589f6c5813SMatthew G. Knepley   DM       bdm;
55599f6c5813SMatthew G. Knepley   PetscInt Nl;
55609f6c5813SMatthew G. Knepley 
55619f6c5813SMatthew G. Knepley   PetscFunctionBegin;
55629f6c5813SMatthew G. Knepley   PetscCall(DMCreate(PetscObjectComm((PetscObject)tr), dm));
55639f6c5813SMatthew G. Knepley   PetscCall(DMSetType(*dm, DMPLEX));
55649f6c5813SMatthew G. Knepley   PetscCall(DMSetFromOptions(*dm));
55659f6c5813SMatthew G. Knepley 
55669f6c5813SMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject)tr));
55679f6c5813SMatthew G. Knepley   PetscCall(DMPlexTransformDestroy(&((DM_Plex *)(*dm)->data)->tr));
55689f6c5813SMatthew G. Knepley   ((DM_Plex *)(*dm)->data)->tr = tr;
55699f6c5813SMatthew G. Knepley 
55709f6c5813SMatthew G. Knepley   PetscCall(DMPlexTransformGetDM(tr, &bdm));
55719f6c5813SMatthew G. Knepley   PetscCall(DMGetNumLabels(bdm, &Nl));
55729f6c5813SMatthew G. Knepley   for (PetscInt l = 0; l < Nl; ++l) {
55739f6c5813SMatthew G. Knepley     DMLabel     label, labelNew;
55749f6c5813SMatthew G. Knepley     const char *lname;
55759f6c5813SMatthew G. Knepley     PetscBool   isDepth, isCellType;
55769f6c5813SMatthew G. Knepley 
55779f6c5813SMatthew G. Knepley     PetscCall(DMGetLabelName(bdm, l, &lname));
55789f6c5813SMatthew G. Knepley     PetscCall(PetscStrcmp(lname, "depth", &isDepth));
55799f6c5813SMatthew G. Knepley     if (isDepth) continue;
55809f6c5813SMatthew G. Knepley     PetscCall(PetscStrcmp(lname, "celltype", &isCellType));
55819f6c5813SMatthew G. Knepley     if (isCellType) continue;
55829f6c5813SMatthew G. Knepley     PetscCall(DMCreateLabel(*dm, lname));
55839f6c5813SMatthew G. Knepley     PetscCall(DMGetLabel(bdm, lname, &label));
55849f6c5813SMatthew G. Knepley     PetscCall(DMGetLabel(*dm, lname, &labelNew));
55859f6c5813SMatthew G. Knepley     PetscCall(DMLabelSetType(labelNew, DMLABELEPHEMERAL));
55869f6c5813SMatthew G. Knepley     PetscCall(DMLabelEphemeralSetLabel(labelNew, label));
55879f6c5813SMatthew G. Knepley     PetscCall(DMLabelEphemeralSetTransform(labelNew, tr));
55889f6c5813SMatthew G. Knepley     PetscCall(DMLabelSetUp(labelNew));
55899f6c5813SMatthew G. Knepley   }
55909f6c5813SMatthew G. Knepley   PetscFunctionReturn(0);
55919f6c5813SMatthew G. Knepley }
5592