xref: /petsc/src/dm/impls/plex/plexcreate.c (revision c22d3578198e541d0b2f39fc5e16a721df30154e)
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>
6b7f5c055SJed Brown #include <petsc/private/kernels/blockmatmult.h>
7b7f5c055SJed Brown #include <petsc/private/kernels/blockinvert.h>
8552f7358SJed Brown 
9b09969d6SVaclav Hapla PetscLogEvent DMPLEX_CreateFromFile, DMPLEX_BuildFromCellList, DMPLEX_BuildCoordinatesFromCellList;
1058cd63d5SVaclav Hapla 
119318fe57SMatthew G. Knepley /* External function declarations here */
129318fe57SMatthew G. Knepley static PetscErrorCode DMInitialize_Plex(DM dm);
139318fe57SMatthew G. Knepley 
14e600fa54SMatthew G. Knepley /* This copies internal things in the Plex structure that we generally want when making a new, related Plex */
155de52c6dSVaclav Hapla PetscErrorCode DMPlexCopy_Internal(DM dmin, PetscBool copyPeriodicity, PetscBool copyOverlap, DM dmout)
16e600fa54SMatthew G. Knepley {
174fb89dddSMatthew G. Knepley   const PetscReal         *maxCell, *Lstart, *L;
186858538eSMatthew G. Knepley   PetscBool                dist;
196bc1bd01Sksagiyam   DMPlexReorderDefaultFlag reorder;
20e600fa54SMatthew G. Knepley 
21e600fa54SMatthew G. Knepley   PetscFunctionBegin;
22e600fa54SMatthew G. Knepley   if (copyPeriodicity) {
234fb89dddSMatthew G. Knepley     PetscCall(DMGetPeriodicity(dmin, &maxCell, &Lstart, &L));
244fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dmout, maxCell,  Lstart,  L));
25e600fa54SMatthew G. Knepley   }
269566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeGetDefault(dmin, &dist));
279566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeSetDefault(dmout, dist));
286bc1bd01Sksagiyam   PetscCall(DMPlexReorderGetDefault(dmin, &reorder));
296bc1bd01Sksagiyam   PetscCall(DMPlexReorderSetDefault(dmout, reorder));
30e600fa54SMatthew G. Knepley   ((DM_Plex *) dmout->data)->useHashLocation  = ((DM_Plex *) dmin->data)->useHashLocation;
311baa6e33SBarry Smith   if (copyOverlap) PetscCall(DMPlexSetOverlap_Plex(dmout, dmin, 0));
32e600fa54SMatthew G. Knepley   PetscFunctionReturn(0);
33e600fa54SMatthew G. Knepley }
34e600fa54SMatthew G. Knepley 
359318fe57SMatthew G. Knepley /* Replace dm with the contents of ndm, and then destroy ndm
369318fe57SMatthew G. Knepley    - Share the DM_Plex structure
379318fe57SMatthew G. Knepley    - Share the coordinates
389318fe57SMatthew G. Knepley    - Share the SF
399318fe57SMatthew G. Knepley */
409318fe57SMatthew G. Knepley static PetscErrorCode DMPlexReplace_Static(DM dm, DM *ndm)
419318fe57SMatthew G. Knepley {
429318fe57SMatthew G. Knepley   PetscSF          sf;
439318fe57SMatthew G. Knepley   DM               dmNew = *ndm, coordDM, coarseDM;
449318fe57SMatthew G. Knepley   Vec              coords;
454fb89dddSMatthew G. Knepley   const PetscReal *maxCell, *Lstart, *L;
469318fe57SMatthew G. Knepley   PetscInt         dim, cdim;
479318fe57SMatthew G. Knepley 
489318fe57SMatthew G. Knepley   PetscFunctionBegin;
499318fe57SMatthew G. Knepley   if (dm == dmNew) {
509566063dSJacob Faibussowitsch     PetscCall(DMDestroy(ndm));
519318fe57SMatthew G. Knepley     PetscFunctionReturn(0);
529318fe57SMatthew G. Knepley   }
539318fe57SMatthew G. Knepley   dm->setupcalled = dmNew->setupcalled;
549566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dmNew, &dim));
559566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
569566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dmNew, &cdim));
579566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, cdim));
589566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmNew, &sf));
599566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dm, sf));
609566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmNew, &coordDM));
619566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmNew, &coords));
629566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dm, coordDM));
639566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coords));
646858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dmNew, &coordDM));
656858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dmNew, &coords));
666858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinateDM(dm, coordDM));
676858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinatesLocal(dm, coords));
689318fe57SMatthew G. Knepley   /* Do not want to create the coordinate field if it does not already exist, so do not call DMGetCoordinateField() */
696858538eSMatthew G. Knepley   PetscCall(DMFieldDestroy(&dm->coordinates[0].field));
706858538eSMatthew G. Knepley   dm->coordinates[0].field = dmNew->coordinates[0].field;
7161a622f3SMatthew G. Knepley   ((DM_Plex *) dmNew->data)->coordFunc = ((DM_Plex *) dm->data)->coordFunc;
724fb89dddSMatthew G. Knepley   PetscCall(DMGetPeriodicity(dmNew, &maxCell, &Lstart, &L));
734fb89dddSMatthew G. Knepley   PetscCall(DMSetPeriodicity(dm,     maxCell,  Lstart,  L));
749566063dSJacob Faibussowitsch   PetscCall(DMDestroy_Plex(dm));
759566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(dm));
769318fe57SMatthew G. Knepley   dm->data = dmNew->data;
779318fe57SMatthew G. Knepley   ((DM_Plex *) dmNew->data)->refct++;
789566063dSJacob Faibussowitsch   PetscCall(DMDestroyLabelLinkList_Internal(dm));
799566063dSJacob Faibussowitsch   PetscCall(DMCopyLabels(dmNew, dm, PETSC_OWN_POINTER, PETSC_TRUE, DM_COPY_LABELS_FAIL));
809566063dSJacob Faibussowitsch   PetscCall(DMGetCoarseDM(dmNew,&coarseDM));
819566063dSJacob Faibussowitsch   PetscCall(DMSetCoarseDM(dm,coarseDM));
829566063dSJacob Faibussowitsch   PetscCall(DMDestroy(ndm));
839318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
849318fe57SMatthew G. Knepley }
859318fe57SMatthew G. Knepley 
869318fe57SMatthew G. Knepley /* Swap dm with the contents of dmNew
879318fe57SMatthew G. Knepley    - Swap the DM_Plex structure
889318fe57SMatthew G. Knepley    - Swap the coordinates
899318fe57SMatthew G. Knepley    - Swap the point PetscSF
909318fe57SMatthew G. Knepley */
919318fe57SMatthew G. Knepley static PetscErrorCode DMPlexSwap_Static(DM dmA, DM dmB)
929318fe57SMatthew G. Knepley {
939318fe57SMatthew G. Knepley   DM              coordDMA, coordDMB;
949318fe57SMatthew G. Knepley   Vec             coordsA,  coordsB;
959318fe57SMatthew G. Knepley   PetscSF         sfA,      sfB;
969318fe57SMatthew G. Knepley   DMField         fieldTmp;
979318fe57SMatthew G. Knepley   void            *tmp;
989318fe57SMatthew G. Knepley   DMLabelLink     listTmp;
999318fe57SMatthew G. Knepley   DMLabel         depthTmp;
1009318fe57SMatthew G. Knepley   PetscInt        tmpI;
1019318fe57SMatthew G. Knepley 
1029318fe57SMatthew G. Knepley   PetscFunctionBegin;
1039318fe57SMatthew G. Knepley   if (dmA == dmB) PetscFunctionReturn(0);
1049566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmA, &sfA));
1059566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dmB, &sfB));
1069566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) sfA));
1079566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dmA, sfB));
1089566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dmB, sfA));
1099566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject) sfA));
1109318fe57SMatthew G. Knepley 
1119566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmA, &coordDMA));
1129566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dmB, &coordDMB));
1139566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) coordDMA));
1149566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dmA, coordDMB));
1159566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDM(dmB, coordDMA));
1169566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject) coordDMA));
1179318fe57SMatthew G. Knepley 
1189566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmA, &coordsA));
1199566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinatesLocal(dmB, &coordsB));
1209566063dSJacob Faibussowitsch   PetscCall(PetscObjectReference((PetscObject) coordsA));
1219566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmA, coordsB));
1229566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dmB, coordsA));
1239566063dSJacob Faibussowitsch   PetscCall(PetscObjectDereference((PetscObject) coordsA));
1249318fe57SMatthew G. Knepley 
1256858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dmA, &coordDMA));
1266858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinateDM(dmB, &coordDMB));
1276858538eSMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject) coordDMA));
1286858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinateDM(dmA, coordDMB));
1296858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinateDM(dmB, coordDMA));
1306858538eSMatthew G. Knepley   PetscCall(PetscObjectDereference((PetscObject) coordDMA));
1316858538eSMatthew G. Knepley 
1326858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dmA, &coordsA));
1336858538eSMatthew G. Knepley   PetscCall(DMGetCellCoordinatesLocal(dmB, &coordsB));
1346858538eSMatthew G. Knepley   PetscCall(PetscObjectReference((PetscObject) coordsA));
1356858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinatesLocal(dmA, coordsB));
1366858538eSMatthew G. Knepley   PetscCall(DMSetCellCoordinatesLocal(dmB, coordsA));
1376858538eSMatthew G. Knepley   PetscCall(PetscObjectDereference((PetscObject) coordsA));
1386858538eSMatthew G. Knepley 
1396858538eSMatthew G. Knepley   fieldTmp                  = dmA->coordinates[0].field;
1406858538eSMatthew G. Knepley   dmA->coordinates[0].field = dmB->coordinates[0].field;
1416858538eSMatthew G. Knepley   dmB->coordinates[0].field = fieldTmp;
1426858538eSMatthew G. Knepley   fieldTmp                  = dmA->coordinates[1].field;
1436858538eSMatthew G. Knepley   dmA->coordinates[1].field = dmB->coordinates[1].field;
1446858538eSMatthew G. Knepley   dmB->coordinates[1].field = fieldTmp;
1459318fe57SMatthew G. Knepley   tmp       = dmA->data;
1469318fe57SMatthew G. Knepley   dmA->data = dmB->data;
1479318fe57SMatthew G. Knepley   dmB->data = tmp;
1489318fe57SMatthew G. Knepley   listTmp   = dmA->labels;
1499318fe57SMatthew G. Knepley   dmA->labels = dmB->labels;
1509318fe57SMatthew G. Knepley   dmB->labels = listTmp;
1519318fe57SMatthew G. Knepley   depthTmp  = dmA->depthLabel;
1529318fe57SMatthew G. Knepley   dmA->depthLabel = dmB->depthLabel;
1539318fe57SMatthew G. Knepley   dmB->depthLabel = depthTmp;
1549318fe57SMatthew G. Knepley   depthTmp  = dmA->celltypeLabel;
1559318fe57SMatthew G. Knepley   dmA->celltypeLabel = dmB->celltypeLabel;
1569318fe57SMatthew G. Knepley   dmB->celltypeLabel = depthTmp;
1579318fe57SMatthew G. Knepley   tmpI         = dmA->levelup;
1589318fe57SMatthew G. Knepley   dmA->levelup = dmB->levelup;
1599318fe57SMatthew G. Knepley   dmB->levelup = tmpI;
1609318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
1619318fe57SMatthew G. Knepley }
1629318fe57SMatthew G. Knepley 
1639318fe57SMatthew G. Knepley static PetscErrorCode DMPlexInterpolateInPlace_Internal(DM dm)
1649318fe57SMatthew G. Knepley {
1659318fe57SMatthew G. Knepley   DM             idm;
1669318fe57SMatthew G. Knepley 
1679318fe57SMatthew G. Knepley   PetscFunctionBegin;
1689566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolate(dm, &idm));
1699566063dSJacob Faibussowitsch   PetscCall(DMPlexCopyCoordinates(dm, idm));
1709566063dSJacob Faibussowitsch   PetscCall(DMPlexReplace_Static(dm, &idm));
1719318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
1729318fe57SMatthew G. Knepley }
1739318fe57SMatthew G. Knepley 
1749318fe57SMatthew G. Knepley /*@C
1759318fe57SMatthew G. Knepley   DMPlexCreateCoordinateSpace - Creates a finite element space for the coordinates
1769318fe57SMatthew G. Knepley 
1779318fe57SMatthew G. Knepley   Collective
1789318fe57SMatthew G. Knepley 
1799318fe57SMatthew G. Knepley   Input Parameters:
1809318fe57SMatthew G. Knepley + DM        - The DM
1814f9ab2b4SJed Brown . degree    - The degree of the finite element or PETSC_DECIDE
1829318fe57SMatthew G. Knepley - coordFunc - An optional function to map new points from refinement to the surface
1839318fe57SMatthew G. Knepley 
1849318fe57SMatthew G. Knepley   Level: advanced
1859318fe57SMatthew G. Knepley 
186db781477SPatrick Sanan .seealso: `PetscFECreateLagrange()`, `DMGetCoordinateDM()`
1879318fe57SMatthew G. Knepley @*/
1889318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateCoordinateSpace(DM dm, PetscInt degree, PetscPointFunc coordFunc)
1899318fe57SMatthew G. Knepley {
1909318fe57SMatthew G. Knepley   DM_Plex      *mesh = (DM_Plex *) dm->data;
1919318fe57SMatthew G. Knepley   DM            cdm;
1929318fe57SMatthew G. Knepley   PetscDS       cds;
1939318fe57SMatthew G. Knepley   PetscFE       fe;
1949318fe57SMatthew G. Knepley   PetscClassId  id;
1959318fe57SMatthew G. Knepley 
1969318fe57SMatthew G. Knepley   PetscFunctionBegin;
1979566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
1989566063dSJacob Faibussowitsch   PetscCall(DMGetDS(cdm, &cds));
1999566063dSJacob Faibussowitsch   PetscCall(PetscDSGetDiscretization(cds, 0, (PetscObject *) &fe));
2009566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetClassId((PetscObject) fe, &id));
2019318fe57SMatthew G. Knepley   if (id != PETSCFE_CLASSID) {
2029318fe57SMatthew G. Knepley     PetscBool      simplex;
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);
2099566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBoundedInt("-coord_dm_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 {
2138c4475acSStefano Zampini       PetscCall(DMPlexIsSimplex(dm, &simplex));
2149566063dSJacob Faibussowitsch       PetscCall(PetscFECreateLagrange(PETSC_COMM_SELF, dim, dE, simplex, degree, qorder, &fe));
2154f9ab2b4SJed Brown     }
2169566063dSJacob Faibussowitsch     PetscCall(DMProjectCoordinates(dm, fe));
2179566063dSJacob Faibussowitsch     PetscCall(PetscFEDestroy(&fe));
2189318fe57SMatthew G. Knepley   }
2199318fe57SMatthew G. Knepley   mesh->coordFunc = coordFunc;
2209318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
2219318fe57SMatthew G. Knepley }
2229318fe57SMatthew G. Knepley 
2231df5d5c5SMatthew G. Knepley /*@
2241df5d5c5SMatthew G. Knepley   DMPlexCreateDoublet - Creates a mesh of two cells of the specified type, optionally with later refinement.
2251df5d5c5SMatthew G. Knepley 
226d083f849SBarry Smith   Collective
2271df5d5c5SMatthew G. Knepley 
2281df5d5c5SMatthew G. Knepley   Input Parameters:
2291df5d5c5SMatthew G. Knepley + comm - The communicator for the DM object
2301df5d5c5SMatthew G. Knepley . dim - The spatial dimension
2311df5d5c5SMatthew G. Knepley . simplex - Flag for simplicial cells, otherwise they are tensor product cells
2321df5d5c5SMatthew G. Knepley . interpolate - Flag to create intermediate mesh pieces (edges, faces)
2331df5d5c5SMatthew G. Knepley - refinementLimit - A nonzero number indicates the largest admissible volume for a refined cell
2341df5d5c5SMatthew G. Knepley 
2351df5d5c5SMatthew G. Knepley   Output Parameter:
2361df5d5c5SMatthew G. Knepley . dm - The DM object
2371df5d5c5SMatthew G. Knepley 
2381df5d5c5SMatthew G. Knepley   Level: beginner
2391df5d5c5SMatthew G. Knepley 
240db781477SPatrick Sanan .seealso: `DMSetType()`, `DMCreate()`
2411df5d5c5SMatthew G. Knepley @*/
2420fdc7489SMatthew Knepley PetscErrorCode DMPlexCreateDoublet(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscBool interpolate, PetscReal refinementLimit, DM *newdm)
2431df5d5c5SMatthew G. Knepley {
2441df5d5c5SMatthew G. Knepley   DM             dm;
2451df5d5c5SMatthew G. Knepley   PetscMPIInt    rank;
2461df5d5c5SMatthew G. Knepley 
2471df5d5c5SMatthew G. Knepley   PetscFunctionBegin;
2489566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, &dm));
2499566063dSJacob Faibussowitsch   PetscCall(DMSetType(dm, DMPLEX));
2509566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
2519566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
252ce78fa2fSMatthew G. Knepley   switch (dim) {
253ce78fa2fSMatthew G. Knepley   case 2:
2549566063dSJacob Faibussowitsch     if (simplex) PetscCall(PetscObjectSetName((PetscObject) dm, "triangular"));
2559566063dSJacob Faibussowitsch     else         PetscCall(PetscObjectSetName((PetscObject) dm, "quadrilateral"));
256ce78fa2fSMatthew G. Knepley     break;
257ce78fa2fSMatthew G. Knepley   case 3:
2589566063dSJacob Faibussowitsch     if (simplex) PetscCall(PetscObjectSetName((PetscObject) dm, "tetrahedral"));
2599566063dSJacob Faibussowitsch     else         PetscCall(PetscObjectSetName((PetscObject) dm, "hexahedral"));
260ce78fa2fSMatthew G. Knepley     break;
261ce78fa2fSMatthew G. Knepley   default:
26263a3b9bcSJacob Faibussowitsch     SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %" PetscInt_FMT, dim);
263ce78fa2fSMatthew G. Knepley   }
2641df5d5c5SMatthew G. Knepley   if (rank) {
2651df5d5c5SMatthew G. Knepley     PetscInt numPoints[2] = {0, 0};
2669566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, NULL, NULL, NULL, NULL));
2671df5d5c5SMatthew G. Knepley   } else {
2681df5d5c5SMatthew G. Knepley     switch (dim) {
2691df5d5c5SMatthew G. Knepley     case 2:
2701df5d5c5SMatthew G. Knepley       if (simplex) {
2711df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {4, 2};
2721df5d5c5SMatthew G. Knepley         PetscInt    coneSize[6]         = {3, 3, 0, 0, 0, 0};
2731df5d5c5SMatthew G. Knepley         PetscInt    cones[6]            = {2, 3, 4,  5, 4, 3};
2741df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[6] = {0, 0, 0,  0, 0, 0};
2751df5d5c5SMatthew G. Knepley         PetscScalar vertexCoords[8]     = {-0.5, 0.5, 0.0, 0.0, 0.0, 1.0, 0.5, 0.5};
2761df5d5c5SMatthew G. Knepley 
2779566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
2781df5d5c5SMatthew G. Knepley       } else {
2791df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {6, 2};
2801df5d5c5SMatthew G. Knepley         PetscInt    coneSize[8]         = {4, 4, 0, 0, 0, 0, 0, 0};
2811df5d5c5SMatthew G. Knepley         PetscInt    cones[8]            = {2, 3, 4, 5,  3, 6, 7, 4};
2821df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[8] = {0, 0, 0, 0,  0, 0, 0, 0};
2831df5d5c5SMatthew 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};
2841df5d5c5SMatthew G. Knepley 
2859566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
2861df5d5c5SMatthew G. Knepley       }
2871df5d5c5SMatthew G. Knepley       break;
2881df5d5c5SMatthew G. Knepley     case 3:
2891df5d5c5SMatthew G. Knepley       if (simplex) {
2901df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]        = {5, 2};
2911df5d5c5SMatthew G. Knepley         PetscInt    coneSize[7]         = {4, 4, 0, 0, 0, 0, 0};
2921df5d5c5SMatthew G. Knepley         PetscInt    cones[8]            = {4, 3, 5, 2,  5, 3, 4, 6};
2931df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[8] = {0, 0, 0, 0,  0, 0, 0, 0};
2941df5d5c5SMatthew 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};
2951df5d5c5SMatthew G. Knepley 
2969566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
2971df5d5c5SMatthew G. Knepley       } else {
2981df5d5c5SMatthew G. Knepley         PetscInt    numPoints[2]         = {12, 2};
2991df5d5c5SMatthew G. Knepley         PetscInt    coneSize[14]         = {8, 8, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
3001df5d5c5SMatthew G. Knepley         PetscInt    cones[16]            = {2, 3, 4, 5, 6, 7, 8, 9,  5, 4, 10, 11, 7, 12, 13, 8};
3011df5d5c5SMatthew G. Knepley         PetscInt    coneOrientations[16] = {0, 0, 0, 0, 0, 0, 0, 0,  0, 0,  0,  0, 0,  0,  0, 0};
3021df5d5c5SMatthew G. Knepley         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,
3031df5d5c5SMatthew G. Knepley                                             -1.0, -0.5,  0.5,   0.0, -0.5,  0.5,  0.0,  0.5,  0.5,  -1.0,  0.5,  0.5,
3041df5d5c5SMatthew G. Knepley                                              1.0,  0.5, -0.5,   1.0, -0.5, -0.5,  1.0, -0.5,  0.5,   1.0,  0.5,  0.5};
3051df5d5c5SMatthew G. Knepley 
3069566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateFromDAG(dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
3071df5d5c5SMatthew G. Knepley       }
3081df5d5c5SMatthew G. Knepley       break;
3091df5d5c5SMatthew G. Knepley     default:
31063a3b9bcSJacob Faibussowitsch       SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %" PetscInt_FMT, dim);
3111df5d5c5SMatthew G. Knepley     }
3121df5d5c5SMatthew G. Knepley   }
3131df5d5c5SMatthew G. Knepley   *newdm = dm;
3141df5d5c5SMatthew G. Knepley   if (refinementLimit > 0.0) {
3151df5d5c5SMatthew G. Knepley     DM rdm;
3161df5d5c5SMatthew G. Knepley     const char *name;
3171df5d5c5SMatthew G. Knepley 
3189566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementUniform(*newdm, PETSC_FALSE));
3199566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementLimit(*newdm, refinementLimit));
3209566063dSJacob Faibussowitsch     PetscCall(DMRefine(*newdm, comm, &rdm));
3219566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetName((PetscObject) *newdm, &name));
3229566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)    rdm,  name));
3239566063dSJacob Faibussowitsch     PetscCall(DMDestroy(newdm));
3241df5d5c5SMatthew G. Knepley     *newdm = rdm;
3251df5d5c5SMatthew G. Knepley   }
3261df5d5c5SMatthew G. Knepley   if (interpolate) {
3275fd9971aSMatthew G. Knepley     DM idm;
3281df5d5c5SMatthew G. Knepley 
3299566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*newdm, &idm));
3309566063dSJacob Faibussowitsch     PetscCall(DMDestroy(newdm));
3311df5d5c5SMatthew G. Knepley     *newdm = idm;
3321df5d5c5SMatthew G. Knepley   }
3331df5d5c5SMatthew G. Knepley   PetscFunctionReturn(0);
3341df5d5c5SMatthew G. Knepley }
3351df5d5c5SMatthew G. Knepley 
3369318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
3379318fe57SMatthew G. Knepley {
3389318fe57SMatthew G. Knepley   const PetscInt numVertices    = 2;
3399318fe57SMatthew G. Knepley   PetscInt       markerRight    = 1;
3409318fe57SMatthew G. Knepley   PetscInt       markerLeft     = 1;
3419318fe57SMatthew G. Knepley   PetscBool      markerSeparate = PETSC_FALSE;
3429318fe57SMatthew G. Knepley   Vec            coordinates;
3439318fe57SMatthew G. Knepley   PetscSection   coordSection;
3449318fe57SMatthew G. Knepley   PetscScalar   *coords;
3459318fe57SMatthew G. Knepley   PetscInt       coordSize;
3469318fe57SMatthew G. Knepley   PetscMPIInt    rank;
3479318fe57SMatthew G. Knepley   PetscInt       cdim = 1, v;
348552f7358SJed Brown 
3499318fe57SMatthew G. Knepley   PetscFunctionBegin;
3509566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
3519318fe57SMatthew G. Knepley   if (markerSeparate) {
3529318fe57SMatthew G. Knepley     markerRight  = 2;
3539318fe57SMatthew G. Knepley     markerLeft   = 1;
3549318fe57SMatthew G. Knepley   }
3559566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
356c5853193SPierre Jolivet   if (rank == 0) {
3579566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numVertices));
3589566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
3599566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", 0, markerLeft));
3609566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm, "marker", 1, markerRight));
3619318fe57SMatthew G. Knepley   }
3629566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
3639566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
3649318fe57SMatthew G. Knepley   /* Build coordinates */
3659566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, cdim));
3669566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
3679566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
3689566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, 0, numVertices));
3699566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, cdim));
3709318fe57SMatthew G. Knepley   for (v = 0; v < numVertices; ++v) {
3719566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, cdim));
3729566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, cdim));
3739318fe57SMatthew G. Knepley   }
3749566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
3759566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
3769566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
3779566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
3789566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
3799566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, cdim));
3809566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates,VECSTANDARD));
3819566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
3829318fe57SMatthew G. Knepley   coords[0] = lower[0];
3839318fe57SMatthew G. Knepley   coords[1] = upper[0];
3849566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
3859566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
3869566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
3879318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
3889318fe57SMatthew G. Knepley }
38926492d91SMatthew G. Knepley 
3909318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[])
391552f7358SJed Brown {
3921df21d24SMatthew G. Knepley   const PetscInt numVertices    = (edges[0]+1)*(edges[1]+1);
3931df21d24SMatthew G. Knepley   const PetscInt numEdges       = edges[0]*(edges[1]+1) + (edges[0]+1)*edges[1];
394552f7358SJed Brown   PetscInt       markerTop      = 1;
395552f7358SJed Brown   PetscInt       markerBottom   = 1;
396552f7358SJed Brown   PetscInt       markerRight    = 1;
397552f7358SJed Brown   PetscInt       markerLeft     = 1;
398552f7358SJed Brown   PetscBool      markerSeparate = PETSC_FALSE;
399552f7358SJed Brown   Vec            coordinates;
400552f7358SJed Brown   PetscSection   coordSection;
401552f7358SJed Brown   PetscScalar    *coords;
402552f7358SJed Brown   PetscInt       coordSize;
403552f7358SJed Brown   PetscMPIInt    rank;
404552f7358SJed Brown   PetscInt       v, vx, vy;
405552f7358SJed Brown 
406552f7358SJed Brown   PetscFunctionBegin;
4079566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
408552f7358SJed Brown   if (markerSeparate) {
4091df21d24SMatthew G. Knepley     markerTop    = 3;
4101df21d24SMatthew G. Knepley     markerBottom = 1;
4111df21d24SMatthew G. Knepley     markerRight  = 2;
4121df21d24SMatthew G. Knepley     markerLeft   = 4;
413552f7358SJed Brown   }
4149566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
415dd400576SPatrick Sanan   if (rank == 0) {
416552f7358SJed Brown     PetscInt e, ex, ey;
417552f7358SJed Brown 
4189566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numEdges+numVertices));
419552f7358SJed Brown     for (e = 0; e < numEdges; ++e) {
4209566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(dm, e, 2));
421552f7358SJed Brown     }
4229566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
423552f7358SJed Brown     for (vx = 0; vx <= edges[0]; vx++) {
424552f7358SJed Brown       for (ey = 0; ey < edges[1]; ey++) {
425552f7358SJed Brown         PetscInt edge   = vx*edges[1] + ey + edges[0]*(edges[1]+1);
426552f7358SJed Brown         PetscInt vertex = ey*(edges[0]+1) + vx + numEdges;
427da80777bSKarl Rupp         PetscInt cone[2];
428552f7358SJed Brown 
429da80777bSKarl Rupp         cone[0] = vertex; cone[1] = vertex+edges[0]+1;
4309566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, edge, cone));
431552f7358SJed Brown         if (vx == edges[0]) {
4329566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge,    markerRight));
4339566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
434552f7358SJed Brown           if (ey == edges[1]-1) {
4359566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
4369566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerRight));
437552f7358SJed Brown           }
438552f7358SJed Brown         } else if (vx == 0) {
4399566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge,    markerLeft));
4409566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
441552f7358SJed Brown           if (ey == edges[1]-1) {
4429566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
4439566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerLeft));
444552f7358SJed Brown           }
445552f7358SJed Brown         }
446552f7358SJed Brown       }
447552f7358SJed Brown     }
448552f7358SJed Brown     for (vy = 0; vy <= edges[1]; vy++) {
449552f7358SJed Brown       for (ex = 0; ex < edges[0]; ex++) {
450552f7358SJed Brown         PetscInt edge   = vy*edges[0]     + ex;
451552f7358SJed Brown         PetscInt vertex = vy*(edges[0]+1) + ex + numEdges;
452da80777bSKarl Rupp         PetscInt cone[2];
453552f7358SJed Brown 
454da80777bSKarl Rupp         cone[0] = vertex; cone[1] = vertex+1;
4559566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, edge, cone));
456552f7358SJed Brown         if (vy == edges[1]) {
4579566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge,    markerTop));
4589566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
459552f7358SJed Brown           if (ex == edges[0]-1) {
4609566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
4619566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerTop));
462552f7358SJed Brown           }
463552f7358SJed Brown         } else if (vy == 0) {
4649566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", edge,    markerBottom));
4659566063dSJacob Faibussowitsch           PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
466552f7358SJed Brown           if (ex == edges[0]-1) {
4679566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
4689566063dSJacob Faibussowitsch             PetscCall(DMSetLabelValue(dm, "Face Sets", cone[1], markerBottom));
469552f7358SJed Brown           }
470552f7358SJed Brown         }
471552f7358SJed Brown       }
472552f7358SJed Brown     }
473552f7358SJed Brown   }
4749566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
4759566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
476552f7358SJed Brown   /* Build coordinates */
4779566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, 2));
4789566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
4799566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
4809566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, numEdges, numEdges + numVertices));
4819566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, 2));
482552f7358SJed Brown   for (v = numEdges; v < numEdges+numVertices; ++v) {
4839566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, 2));
4849566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, 2));
485552f7358SJed Brown   }
4869566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
4879566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
4889566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
4899566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
4909566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
4919566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, 2));
4929566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates,VECSTANDARD));
4939566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
494552f7358SJed Brown   for (vy = 0; vy <= edges[1]; ++vy) {
495552f7358SJed Brown     for (vx = 0; vx <= edges[0]; ++vx) {
496552f7358SJed Brown       coords[(vy*(edges[0]+1)+vx)*2+0] = lower[0] + ((upper[0] - lower[0])/edges[0])*vx;
497552f7358SJed Brown       coords[(vy*(edges[0]+1)+vx)*2+1] = lower[1] + ((upper[1] - lower[1])/edges[1])*vy;
498552f7358SJed Brown     }
499552f7358SJed Brown   }
5009566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
5019566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
5029566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
503552f7358SJed Brown   PetscFunctionReturn(0);
504552f7358SJed Brown }
505552f7358SJed Brown 
5069318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt faces[])
507552f7358SJed Brown {
5089e8abbc3SMichael Lange   PetscInt       vertices[3], numVertices;
5097b59f5a9SMichael Lange   PetscInt       numFaces       = 2*faces[0]*faces[1] + 2*faces[1]*faces[2] + 2*faces[0]*faces[2];
510c2df9bbfSMatthew G. Knepley   PetscInt       markerTop      = 1;
511c2df9bbfSMatthew G. Knepley   PetscInt       markerBottom   = 1;
512c2df9bbfSMatthew G. Knepley   PetscInt       markerFront    = 1;
513c2df9bbfSMatthew G. Knepley   PetscInt       markerBack     = 1;
514c2df9bbfSMatthew G. Knepley   PetscInt       markerRight    = 1;
515c2df9bbfSMatthew G. Knepley   PetscInt       markerLeft     = 1;
516c2df9bbfSMatthew G. Knepley   PetscBool      markerSeparate = PETSC_FALSE;
517552f7358SJed Brown   Vec            coordinates;
518552f7358SJed Brown   PetscSection   coordSection;
519552f7358SJed Brown   PetscScalar    *coords;
520552f7358SJed Brown   PetscInt       coordSize;
521552f7358SJed Brown   PetscMPIInt    rank;
522552f7358SJed Brown   PetscInt       v, vx, vy, vz;
5237b59f5a9SMichael Lange   PetscInt       voffset, iface=0, cone[4];
524552f7358SJed Brown 
525552f7358SJed Brown   PetscFunctionBegin;
5261dca8a05SBarry 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");
5279566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
528c2df9bbfSMatthew G. Knepley   PetscCall(PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
529c2df9bbfSMatthew G. Knepley   if (markerSeparate) {
530c2df9bbfSMatthew G. Knepley     markerBottom = 1;
531c2df9bbfSMatthew G. Knepley     markerTop    = 2;
532c2df9bbfSMatthew G. Knepley     markerFront  = 3;
533c2df9bbfSMatthew G. Knepley     markerBack   = 4;
534c2df9bbfSMatthew G. Knepley     markerRight  = 5;
535c2df9bbfSMatthew G. Knepley     markerLeft   = 6;
536c2df9bbfSMatthew G. Knepley   }
5379e8abbc3SMichael Lange   vertices[0] = faces[0]+1; vertices[1] = faces[1]+1; vertices[2] = faces[2]+1;
5389e8abbc3SMichael Lange   numVertices = vertices[0]*vertices[1]*vertices[2];
539dd400576SPatrick Sanan   if (rank == 0) {
540552f7358SJed Brown     PetscInt f;
541552f7358SJed Brown 
5429566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numFaces+numVertices));
543552f7358SJed Brown     for (f = 0; f < numFaces; ++f) {
5449566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(dm, f, 4));
545552f7358SJed Brown     }
5469566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
5477b59f5a9SMichael Lange 
5487b59f5a9SMichael Lange     /* Side 0 (Top) */
5497b59f5a9SMichael Lange     for (vy = 0; vy < faces[1]; vy++) {
5507b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5517b59f5a9SMichael Lange         voffset = numFaces + vertices[0]*vertices[1]*(vertices[2]-1) + vy*vertices[0] + vx;
5527b59f5a9SMichael Lange         cone[0] = voffset; cone[1] = voffset+1; cone[2] = voffset+vertices[0]+1; cone[3] = voffset+vertices[0];
5539566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
554c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerTop));
555c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset+0, markerTop));
556c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset+1, markerTop));
557c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, markerTop));
558c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]+1, markerTop));
5597b59f5a9SMichael Lange         iface++;
560552f7358SJed Brown       }
561552f7358SJed Brown     }
5627b59f5a9SMichael Lange 
5637b59f5a9SMichael Lange     /* Side 1 (Bottom) */
5647b59f5a9SMichael Lange     for (vy = 0; vy < faces[1]; vy++) {
5657b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5667b59f5a9SMichael Lange         voffset = numFaces + vy*(faces[0]+1) + vx;
5677b59f5a9SMichael Lange         cone[0] = voffset+1; cone[1] = voffset; cone[2] = voffset+vertices[0]; cone[3] = voffset+vertices[0]+1;
5689566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
569c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerBottom));
570c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset+0, markerBottom));
571c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset+1, markerBottom));
572c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, markerBottom));
573c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]+1, markerBottom));
5747b59f5a9SMichael Lange         iface++;
575552f7358SJed Brown       }
576552f7358SJed Brown     }
5777b59f5a9SMichael Lange 
5787b59f5a9SMichael Lange     /* Side 2 (Front) */
5797b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
5807b59f5a9SMichael Lange       for (vx = 0; vx < faces[0]; vx++) {
5817b59f5a9SMichael Lange         voffset = numFaces + vz*vertices[0]*vertices[1] + vx;
5827b59f5a9SMichael Lange         cone[0] = voffset; cone[1] = voffset+1; cone[2] = voffset+vertices[0]*vertices[1]+1; cone[3] = voffset+vertices[0]*vertices[1];
5839566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
584c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerFront));
585c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset+0, markerFront));
586c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset+1, markerFront));
587c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+0, markerFront));
588c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+1, markerFront));
5897b59f5a9SMichael Lange         iface++;
590552f7358SJed Brown       }
5917b59f5a9SMichael Lange     }
5927b59f5a9SMichael Lange 
5937b59f5a9SMichael Lange     /* Side 3 (Back) */
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] + vertices[0]*(vertices[1]-1) + vx;
5977b59f5a9SMichael Lange         cone[0] = voffset+vertices[0]*vertices[1]; cone[1] = voffset+vertices[0]*vertices[1]+1;
5987b59f5a9SMichael Lange         cone[2] = voffset+1; cone[3] = voffset;
5999566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
600c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerBack));
601c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset+0, markerBack));
602c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset+1, markerBack));
603c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+0, markerBack));
604c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+1, markerBack));
6057b59f5a9SMichael Lange         iface++;
6067b59f5a9SMichael Lange       }
6077b59f5a9SMichael Lange     }
6087b59f5a9SMichael Lange 
6097b59f5a9SMichael Lange     /* Side 4 (Left) */
6107b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
6117b59f5a9SMichael Lange       for (vy = 0; vy < faces[1]; vy++) {
6127b59f5a9SMichael Lange         voffset = numFaces + vz*vertices[0]*vertices[1] + vy*vertices[0];
6137b59f5a9SMichael Lange         cone[0] = voffset; cone[1] = voffset+vertices[0]*vertices[1];
6147b59f5a9SMichael Lange         cone[2] = voffset+vertices[0]*vertices[1]+vertices[0]; cone[3] = voffset+vertices[0];
6159566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
616c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerLeft));
617c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset+0, markerLeft));
618c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, markerLeft));
619c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[1]+0, markerLeft));
620c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+vertices[0], markerLeft));
6217b59f5a9SMichael Lange         iface++;
6227b59f5a9SMichael Lange       }
6237b59f5a9SMichael Lange     }
6247b59f5a9SMichael Lange 
6257b59f5a9SMichael Lange     /* Side 5 (Right) */
6267b59f5a9SMichael Lange     for (vz = 0; vz < faces[2]; vz++) {
6277b59f5a9SMichael Lange       for (vy = 0; vy < faces[1]; vy++) {
628aab5bcd8SJed Brown         voffset = numFaces + vz*vertices[0]*vertices[1] + vy*vertices[0] + faces[0];
6297b59f5a9SMichael Lange         cone[0] = voffset+vertices[0]*vertices[1]; cone[1] = voffset;
6307b59f5a9SMichael Lange         cone[2] = voffset+vertices[0]; cone[3] = voffset+vertices[0]*vertices[1]+vertices[0];
6319566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, iface, cone));
632c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", iface, markerRight));
633c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset+0, markerRight));
634c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]+0, markerRight));
635c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+0, markerRight));
636c2df9bbfSMatthew G. Knepley         PetscCall(DMSetLabelValue(dm, "marker", voffset+vertices[0]*vertices[1]+vertices[0], markerRight));
6377b59f5a9SMichael Lange         iface++;
6387b59f5a9SMichael Lange       }
639552f7358SJed Brown     }
640552f7358SJed Brown   }
6419566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
6429566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
643552f7358SJed Brown   /* Build coordinates */
6449566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, 3));
6459566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
6469566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
6479566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, numFaces, numFaces + numVertices));
6489566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, 3));
649552f7358SJed Brown   for (v = numFaces; v < numFaces+numVertices; ++v) {
6509566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, 3));
6519566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, 3));
652552f7358SJed Brown   }
6539566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
6549566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
6559566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
6569566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
6579566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
6589566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, 3));
6599566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates,VECSTANDARD));
6609566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
661552f7358SJed Brown   for (vz = 0; vz <= faces[2]; ++vz) {
662552f7358SJed Brown     for (vy = 0; vy <= faces[1]; ++vy) {
663552f7358SJed Brown       for (vx = 0; vx <= faces[0]; ++vx) {
664552f7358SJed Brown         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+0] = lower[0] + ((upper[0] - lower[0])/faces[0])*vx;
665552f7358SJed Brown         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+1] = lower[1] + ((upper[1] - lower[1])/faces[1])*vy;
666552f7358SJed Brown         coords[((vz*(faces[1]+1)+vy)*(faces[0]+1)+vx)*3+2] = lower[2] + ((upper[2] - lower[2])/faces[2])*vz;
667552f7358SJed Brown       }
668552f7358SJed Brown     }
669552f7358SJed Brown   }
6709566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
6719566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
6729566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
673552f7358SJed Brown   PetscFunctionReturn(0);
674552f7358SJed Brown }
675552f7358SJed Brown 
6769318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxSurfaceMesh_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate)
6779318fe57SMatthew G. Knepley {
6789318fe57SMatthew G. Knepley   PetscFunctionBegin;
6799318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
6809566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim-1));
6819566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, dim));
6829318fe57SMatthew G. Knepley   switch (dim) {
6839566063dSJacob Faibussowitsch     case 1: PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_1D_Internal(dm, lower, upper, faces));break;
6849566063dSJacob Faibussowitsch     case 2: PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_2D_Internal(dm, lower, upper, faces));break;
6859566063dSJacob Faibussowitsch     case 3: PetscCall(DMPlexCreateBoxSurfaceMesh_Tensor_3D_Internal(dm, lower, upper, faces));break;
68663a3b9bcSJacob Faibussowitsch     default: SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Dimension not supported: %" PetscInt_FMT, dim);
6879318fe57SMatthew G. Knepley   }
6889566063dSJacob Faibussowitsch   if (interpolate) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
6899318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
6909318fe57SMatthew G. Knepley }
6919318fe57SMatthew G. Knepley 
6929318fe57SMatthew G. Knepley /*@C
6939318fe57SMatthew G. Knepley   DMPlexCreateBoxSurfaceMesh - Creates a mesh on the surface of the tensor product of unit intervals (box) using tensor cells (hexahedra).
6949318fe57SMatthew G. Knepley 
6959318fe57SMatthew G. Knepley   Collective
6969318fe57SMatthew G. Knepley 
6979318fe57SMatthew G. Knepley   Input Parameters:
6989318fe57SMatthew G. Knepley + comm        - The communicator for the DM object
6999318fe57SMatthew G. Knepley . dim         - The spatial dimension of the box, so the resulting mesh is has dimension dim-1
7009318fe57SMatthew 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
7019318fe57SMatthew G. Knepley . lower       - The lower left corner, or NULL for (0, 0, 0)
7029318fe57SMatthew G. Knepley . upper       - The upper right corner, or NULL for (1, 1, 1)
7039318fe57SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
7049318fe57SMatthew G. Knepley 
7059318fe57SMatthew G. Knepley   Output Parameter:
7069318fe57SMatthew G. Knepley . dm  - The DM object
7079318fe57SMatthew G. Knepley 
7089318fe57SMatthew G. Knepley   Level: beginner
7099318fe57SMatthew G. Knepley 
710db781477SPatrick Sanan .seealso: `DMSetFromOptions()`, `DMPlexCreateBoxMesh()`, `DMPlexCreateFromFile()`, `DMSetType()`, `DMCreate()`
7119318fe57SMatthew G. Knepley @*/
7129318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateBoxSurfaceMesh(MPI_Comm comm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], PetscBool interpolate, DM *dm)
7139318fe57SMatthew G. Knepley {
7149318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
7159318fe57SMatthew G. Knepley   PetscReal      low[3] = {0, 0, 0};
7169318fe57SMatthew G. Knepley   PetscReal      upp[3] = {1, 1, 1};
7179318fe57SMatthew G. Knepley 
7189318fe57SMatthew G. Knepley   PetscFunctionBegin;
7199566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm,dm));
7209566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm,DMPLEX));
7219566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(*dm, dim, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, interpolate));
7229318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
7239318fe57SMatthew G. Knepley }
7249318fe57SMatthew G. Knepley 
7259318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateLineMesh_Internal(DM dm,PetscInt segments,PetscReal lower,PetscReal upper,DMBoundaryType bd)
726fdbf62faSLisandro Dalcin {
727fdbf62faSLisandro Dalcin   PetscInt       i,fStart,fEnd,numCells = 0,numVerts = 0;
728fdbf62faSLisandro Dalcin   PetscInt       numPoints[2],*coneSize,*cones,*coneOrientations;
729fdbf62faSLisandro Dalcin   PetscScalar    *vertexCoords;
730fdbf62faSLisandro Dalcin   PetscReal      L,maxCell;
731fdbf62faSLisandro Dalcin   PetscBool      markerSeparate = PETSC_FALSE;
732fdbf62faSLisandro Dalcin   PetscInt       markerLeft  = 1, faceMarkerLeft  = 1;
733fdbf62faSLisandro Dalcin   PetscInt       markerRight = 1, faceMarkerRight = 2;
734fdbf62faSLisandro Dalcin   PetscBool      wrap = (bd == DM_BOUNDARY_PERIODIC || bd == DM_BOUNDARY_TWIST) ? PETSC_TRUE : PETSC_FALSE;
735fdbf62faSLisandro Dalcin   PetscMPIInt    rank;
736fdbf62faSLisandro Dalcin 
737fdbf62faSLisandro Dalcin   PetscFunctionBegin;
7389318fe57SMatthew G. Knepley   PetscValidPointer(dm,1);
739fdbf62faSLisandro Dalcin 
7409566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm,1));
7419566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm,"marker"));
7429566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm,"Face Sets"));
743fdbf62faSLisandro Dalcin 
7449566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject) dm),&rank));
745dd400576SPatrick Sanan   if (rank == 0) numCells = segments;
746dd400576SPatrick Sanan   if (rank == 0) numVerts = segments + (wrap ? 0 : 1);
747fdbf62faSLisandro Dalcin 
748fdbf62faSLisandro Dalcin   numPoints[0] = numVerts ; numPoints[1] = numCells;
7499566063dSJacob Faibussowitsch   PetscCall(PetscMalloc4(numCells+numVerts,&coneSize,numCells*2,&cones,numCells+numVerts,&coneOrientations,numVerts,&vertexCoords));
7509566063dSJacob Faibussowitsch   PetscCall(PetscArrayzero(coneOrientations,numCells+numVerts));
751fdbf62faSLisandro Dalcin   for (i = 0; i < numCells; ++i) { coneSize[i] = 2; }
752fdbf62faSLisandro Dalcin   for (i = 0; i < numVerts; ++i) { coneSize[numCells+i] = 0; }
753fdbf62faSLisandro Dalcin   for (i = 0; i < numCells; ++i) { cones[2*i] = numCells + i%numVerts; cones[2*i+1] = numCells + (i+1)%numVerts; }
754fdbf62faSLisandro Dalcin   for (i = 0; i < numVerts; ++i) { vertexCoords[i] = lower + (upper-lower)*((PetscReal)i/(PetscReal)numCells); }
7559566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateFromDAG(dm,1,numPoints,coneSize,cones,coneOrientations,vertexCoords));
7569566063dSJacob Faibussowitsch   PetscCall(PetscFree4(coneSize,cones,coneOrientations,vertexCoords));
757fdbf62faSLisandro Dalcin 
7589566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject)dm)->options,((PetscObject)dm)->prefix,"-dm_plex_separate_marker",&markerSeparate,NULL));
759fdbf62faSLisandro Dalcin   if (markerSeparate) { markerLeft = faceMarkerLeft; markerRight = faceMarkerRight;}
760dd400576SPatrick Sanan   if (!wrap && rank == 0) {
7619566063dSJacob Faibussowitsch     PetscCall(DMPlexGetHeightStratum(dm,1,&fStart,&fEnd));
7629566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm,"marker",fStart,markerLeft));
7639566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm,"marker",fEnd-1,markerRight));
7649566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm,"Face Sets",fStart,faceMarkerLeft));
7659566063dSJacob Faibussowitsch     PetscCall(DMSetLabelValue(dm,"Face Sets",fEnd-1,faceMarkerRight));
766fdbf62faSLisandro Dalcin   }
767fdbf62faSLisandro Dalcin   if (wrap) {
768fdbf62faSLisandro Dalcin     L       = upper - lower;
769fdbf62faSLisandro Dalcin     maxCell = (PetscReal)1.1*(L/(PetscReal)PetscMax(1,segments));
7704fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, &maxCell, &lower, &L));
771fdbf62faSLisandro Dalcin   }
7729566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
773fdbf62faSLisandro Dalcin   PetscFunctionReturn(0);
774fdbf62faSLisandro Dalcin }
775fdbf62faSLisandro Dalcin 
7769318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxMesh_Simplex_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate)
777d6218766SMatthew G. Knepley {
7789318fe57SMatthew G. Knepley   DM      boundary, vol;
779*c22d3578SMatthew G. Knepley   DMLabel bdlabel;
780d6218766SMatthew G. Knepley 
781d6218766SMatthew G. Knepley   PetscFunctionBegin;
7829318fe57SMatthew G. Knepley   PetscValidPointer(dm, 1);
783*c22d3578SMatthew 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");
7849566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject) dm), &boundary));
7859566063dSJacob Faibussowitsch   PetscCall(DMSetType(boundary, DMPLEX));
7869566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(boundary, dim, faces, lower, upper, PETSC_FALSE));
7879566063dSJacob Faibussowitsch   PetscCall(DMPlexGenerate(boundary, NULL, interpolate, &vol));
788*c22d3578SMatthew G. Knepley   PetscCall(DMGetLabel(vol, "marker", &bdlabel));
789*c22d3578SMatthew G. Knepley   if (bdlabel) PetscCall(DMPlexLabelComplete(vol, bdlabel));
7905de52c6dSVaclav Hapla   PetscCall(DMPlexCopy_Internal(dm, PETSC_TRUE, PETSC_FALSE, vol));
7919566063dSJacob Faibussowitsch   PetscCall(DMPlexReplace_Static(dm, &vol));
7929566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&boundary));
793d6218766SMatthew G. Knepley   PetscFunctionReturn(0);
794d6218766SMatthew G. Knepley }
795d6218766SMatthew G. Knepley 
7963dfda0b1SToby Isaac static PetscErrorCode DMPlexCreateCubeMesh_Internal(DM dm, const PetscReal lower[], const PetscReal upper[], const PetscInt edges[], DMBoundaryType bdX, DMBoundaryType bdY, DMBoundaryType bdZ)
7973dfda0b1SToby Isaac {
798ed0e4b50SMatthew G. Knepley   DMLabel        cutLabel = NULL;
799f4eb4c5dSMatthew G. Knepley   PetscInt       markerTop      = 1, faceMarkerTop      = 1;
800f4eb4c5dSMatthew G. Knepley   PetscInt       markerBottom   = 1, faceMarkerBottom   = 1;
801f4eb4c5dSMatthew G. Knepley   PetscInt       markerFront    = 1, faceMarkerFront    = 1;
802f4eb4c5dSMatthew G. Knepley   PetscInt       markerBack     = 1, faceMarkerBack     = 1;
803f4eb4c5dSMatthew G. Knepley   PetscInt       markerRight    = 1, faceMarkerRight    = 1;
804f4eb4c5dSMatthew G. Knepley   PetscInt       markerLeft     = 1, faceMarkerLeft     = 1;
8053dfda0b1SToby Isaac   PetscInt       dim;
806d8211ee3SMatthew G. Knepley   PetscBool      markerSeparate = PETSC_FALSE, cutMarker = PETSC_FALSE;
8073dfda0b1SToby Isaac   PetscMPIInt    rank;
8083dfda0b1SToby Isaac 
8093dfda0b1SToby Isaac   PetscFunctionBegin;
8109566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm,&dim));
8119566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
8129566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm,"marker"));
8139566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm,"Face Sets"));
8149566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_periodic_cut", &cutMarker, NULL));
815d8211ee3SMatthew G. Knepley   if (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ||
816d8211ee3SMatthew G. Knepley       bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ||
817d8211ee3SMatthew G. Knepley       bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST) {
8184c67ea77SStefano Zampini 
8199566063dSJacob Faibussowitsch     if (cutMarker) {PetscCall(DMCreateLabel(dm, "periodic_cut")); PetscCall(DMGetLabel(dm, "periodic_cut", &cutLabel));}
820d8211ee3SMatthew G. Knepley   }
8213dfda0b1SToby Isaac   switch (dim) {
8223dfda0b1SToby Isaac   case 2:
823f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 3;
824f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
825f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 2;
826f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 4;
8273dfda0b1SToby Isaac     break;
8283dfda0b1SToby Isaac   case 3:
829f4eb4c5dSMatthew G. Knepley     faceMarkerBottom = 1;
830f4eb4c5dSMatthew G. Knepley     faceMarkerTop    = 2;
831f4eb4c5dSMatthew G. Knepley     faceMarkerFront  = 3;
832f4eb4c5dSMatthew G. Knepley     faceMarkerBack   = 4;
833f4eb4c5dSMatthew G. Knepley     faceMarkerRight  = 5;
834f4eb4c5dSMatthew G. Knepley     faceMarkerLeft   = 6;
8353dfda0b1SToby Isaac     break;
8363dfda0b1SToby Isaac   default:
83763a3b9bcSJacob Faibussowitsch     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Dimension %" PetscInt_FMT " not supported",dim);
8383dfda0b1SToby Isaac   }
8399566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetBool(((PetscObject) dm)->options,((PetscObject) dm)->prefix, "-dm_plex_separate_marker", &markerSeparate, NULL));
840f4eb4c5dSMatthew G. Knepley   if (markerSeparate) {
841f4eb4c5dSMatthew G. Knepley     markerBottom = faceMarkerBottom;
842f4eb4c5dSMatthew G. Knepley     markerTop    = faceMarkerTop;
843f4eb4c5dSMatthew G. Knepley     markerFront  = faceMarkerFront;
844f4eb4c5dSMatthew G. Knepley     markerBack   = faceMarkerBack;
845f4eb4c5dSMatthew G. Knepley     markerRight  = faceMarkerRight;
846f4eb4c5dSMatthew G. Knepley     markerLeft   = faceMarkerLeft;
8473dfda0b1SToby Isaac   }
8483dfda0b1SToby Isaac   {
849dd400576SPatrick Sanan     const PetscInt numXEdges    = rank == 0 ? edges[0] : 0;
850dd400576SPatrick Sanan     const PetscInt numYEdges    = rank == 0 ? edges[1] : 0;
851dd400576SPatrick Sanan     const PetscInt numZEdges    = rank == 0 ? edges[2] : 0;
852dd400576SPatrick Sanan     const PetscInt numXVertices = rank == 0 ? (bdX == DM_BOUNDARY_PERIODIC || bdX == DM_BOUNDARY_TWIST ? edges[0] : edges[0]+1) : 0;
853dd400576SPatrick Sanan     const PetscInt numYVertices = rank == 0 ? (bdY == DM_BOUNDARY_PERIODIC || bdY == DM_BOUNDARY_TWIST ? edges[1] : edges[1]+1) : 0;
854dd400576SPatrick Sanan     const PetscInt numZVertices = rank == 0 ? (bdZ == DM_BOUNDARY_PERIODIC || bdZ == DM_BOUNDARY_TWIST ? edges[2] : edges[2]+1) : 0;
8553dfda0b1SToby Isaac     const PetscInt numCells     = numXEdges*numYEdges*numZEdges;
8563dfda0b1SToby Isaac     const PetscInt numXFaces    = numYEdges*numZEdges;
8573dfda0b1SToby Isaac     const PetscInt numYFaces    = numXEdges*numZEdges;
8583dfda0b1SToby Isaac     const PetscInt numZFaces    = numXEdges*numYEdges;
8593dfda0b1SToby Isaac     const PetscInt numTotXFaces = numXVertices*numXFaces;
8603dfda0b1SToby Isaac     const PetscInt numTotYFaces = numYVertices*numYFaces;
8613dfda0b1SToby Isaac     const PetscInt numTotZFaces = numZVertices*numZFaces;
8623dfda0b1SToby Isaac     const PetscInt numFaces     = numTotXFaces + numTotYFaces + numTotZFaces;
8633dfda0b1SToby Isaac     const PetscInt numTotXEdges = numXEdges*numYVertices*numZVertices;
8643dfda0b1SToby Isaac     const PetscInt numTotYEdges = numYEdges*numXVertices*numZVertices;
8653dfda0b1SToby Isaac     const PetscInt numTotZEdges = numZEdges*numXVertices*numYVertices;
8663dfda0b1SToby Isaac     const PetscInt numVertices  = numXVertices*numYVertices*numZVertices;
8673dfda0b1SToby Isaac     const PetscInt numEdges     = numTotXEdges + numTotYEdges + numTotZEdges;
8683dfda0b1SToby Isaac     const PetscInt firstVertex  = (dim == 2) ? numFaces : numCells;
8693dfda0b1SToby Isaac     const PetscInt firstXFace   = (dim == 2) ? 0 : numCells + numVertices;
8703dfda0b1SToby Isaac     const PetscInt firstYFace   = firstXFace + numTotXFaces;
8713dfda0b1SToby Isaac     const PetscInt firstZFace   = firstYFace + numTotYFaces;
8723dfda0b1SToby Isaac     const PetscInt firstXEdge   = numCells + numFaces + numVertices;
8733dfda0b1SToby Isaac     const PetscInt firstYEdge   = firstXEdge + numTotXEdges;
8743dfda0b1SToby Isaac     const PetscInt firstZEdge   = firstYEdge + numTotYEdges;
8753dfda0b1SToby Isaac     Vec            coordinates;
8763dfda0b1SToby Isaac     PetscSection   coordSection;
8773dfda0b1SToby Isaac     PetscScalar   *coords;
8783dfda0b1SToby Isaac     PetscInt       coordSize;
8793dfda0b1SToby Isaac     PetscInt       v, vx, vy, vz;
8803dfda0b1SToby Isaac     PetscInt       c, f, fx, fy, fz, e, ex, ey, ez;
8813dfda0b1SToby Isaac 
8829566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells+numFaces+numEdges+numVertices));
8833dfda0b1SToby Isaac     for (c = 0; c < numCells; c++) {
8849566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(dm, c, 6));
8853dfda0b1SToby Isaac     }
8863dfda0b1SToby Isaac     for (f = firstXFace; f < firstXFace+numFaces; ++f) {
8879566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(dm, f, 4));
8883dfda0b1SToby Isaac     }
8893dfda0b1SToby Isaac     for (e = firstXEdge; e < firstXEdge+numEdges; ++e) {
8909566063dSJacob Faibussowitsch       PetscCall(DMPlexSetConeSize(dm, e, 2));
8913dfda0b1SToby Isaac     }
8929566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm)); /* Allocate space for cones */
8933dfda0b1SToby Isaac     /* Build cells */
8943dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
8953dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
8963dfda0b1SToby Isaac         for (fx = 0; fx < numXEdges; ++fx) {
8973dfda0b1SToby Isaac           PetscInt cell    = (fz*numYEdges + fy)*numXEdges + fx;
8983dfda0b1SToby Isaac           PetscInt faceB   = firstZFace + (fy*numXEdges+fx)*numZVertices +   fz;
8993dfda0b1SToby Isaac           PetscInt faceT   = firstZFace + (fy*numXEdges+fx)*numZVertices + ((fz+1)%numZVertices);
9003dfda0b1SToby Isaac           PetscInt faceF   = firstYFace + (fz*numXEdges+fx)*numYVertices +   fy;
9013dfda0b1SToby Isaac           PetscInt faceK   = firstYFace + (fz*numXEdges+fx)*numYVertices + ((fy+1)%numYVertices);
9023dfda0b1SToby Isaac           PetscInt faceL   = firstXFace + (fz*numYEdges+fy)*numXVertices +   fx;
9033dfda0b1SToby Isaac           PetscInt faceR   = firstXFace + (fz*numYEdges+fy)*numXVertices + ((fx+1)%numXVertices);
9043dfda0b1SToby Isaac                             /* B,  T,  F,  K,  R,  L */
905b5a892a1SMatthew G. Knepley           PetscInt ornt[6] = {-2,  0,  0, -3,  0, -2}; /* ??? */
90642206facSLisandro Dalcin           PetscInt cone[6];
9073dfda0b1SToby Isaac 
9083dfda0b1SToby Isaac           /* no boundary twisting in 3D */
9093dfda0b1SToby Isaac           cone[0] = faceB; cone[1] = faceT; cone[2] = faceF; cone[3] = faceK; cone[4] = faceR; cone[5] = faceL;
9109566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, cell, cone));
9119566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, cell, ornt));
9129566063dSJacob Faibussowitsch           if (bdX != DM_BOUNDARY_NONE && fx == numXEdges-1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
9139566063dSJacob Faibussowitsch           if (bdY != DM_BOUNDARY_NONE && fy == numYEdges-1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
9149566063dSJacob Faibussowitsch           if (bdZ != DM_BOUNDARY_NONE && fz == numZEdges-1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, cell, 2));
9153dfda0b1SToby Isaac         }
9163dfda0b1SToby Isaac       }
9173dfda0b1SToby Isaac     }
9183dfda0b1SToby Isaac     /* Build x faces */
9193dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
9203dfda0b1SToby Isaac       for (fy = 0; fy < numYEdges; ++fy) {
9213dfda0b1SToby Isaac         for (fx = 0; fx < numXVertices; ++fx) {
9223dfda0b1SToby Isaac           PetscInt face    = firstXFace + (fz*numYEdges+fy)     *numXVertices+fx;
9233dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy                   *numXVertices+fx)*numZEdges + fz;
9243dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (((fy+1)%numYVertices)*numXVertices+fx)*numZEdges + fz;
9253dfda0b1SToby Isaac           PetscInt edgeB   = firstYEdge + (fz                   *numXVertices+fx)*numYEdges + fy;
9263dfda0b1SToby Isaac           PetscInt edgeT   = firstYEdge + (((fz+1)%numZVertices)*numXVertices+fx)*numYEdges + fy;
927b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
9283dfda0b1SToby Isaac           PetscInt cone[4];
9293dfda0b1SToby Isaac 
9303dfda0b1SToby Isaac           if (dim == 3) {
9313dfda0b1SToby Isaac             /* markers */
9323dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
9333dfda0b1SToby Isaac               if (fx == numXVertices-1) {
9349566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerRight));
9359566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerRight));
9363dfda0b1SToby Isaac               }
9373dfda0b1SToby Isaac               else if (fx == 0) {
9389566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerLeft));
9399566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerLeft));
9403dfda0b1SToby Isaac               }
9413dfda0b1SToby Isaac             }
9423dfda0b1SToby Isaac           }
9433dfda0b1SToby Isaac           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
9449566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
9459566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
9463dfda0b1SToby Isaac         }
9473dfda0b1SToby Isaac       }
9483dfda0b1SToby Isaac     }
9493dfda0b1SToby Isaac     /* Build y faces */
9503dfda0b1SToby Isaac     for (fz = 0; fz < numZEdges; ++fz) {
95142206facSLisandro Dalcin       for (fx = 0; fx < numXEdges; ++fx) {
9523dfda0b1SToby Isaac         for (fy = 0; fy < numYVertices; ++fy) {
9533dfda0b1SToby Isaac           PetscInt face    = firstYFace + (fz*numXEdges+fx)*numYVertices + fy;
9543dfda0b1SToby Isaac           PetscInt edgeL   = firstZEdge + (fy*numXVertices+  fx)*numZEdges + fz;
9553dfda0b1SToby Isaac           PetscInt edgeR   = firstZEdge + (fy*numXVertices+((fx+1)%numXVertices))*numZEdges + fz;
9563dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz                   *numYVertices+fy)*numXEdges + fx;
9573dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (((fz+1)%numZVertices)*numYVertices+fy)*numXEdges + fx;
958b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
9593dfda0b1SToby Isaac           PetscInt cone[4];
9603dfda0b1SToby Isaac 
9613dfda0b1SToby Isaac           if (dim == 3) {
9623dfda0b1SToby Isaac             /* markers */
9633dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
9643dfda0b1SToby Isaac               if (fy == numYVertices-1) {
9659566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerBack));
9669566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerBack));
9673dfda0b1SToby Isaac               }
9683dfda0b1SToby Isaac               else if (fy == 0) {
9699566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerFront));
9709566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerFront));
9713dfda0b1SToby Isaac               }
9723dfda0b1SToby Isaac             }
9733dfda0b1SToby Isaac           }
9743dfda0b1SToby Isaac           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
9759566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
9769566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
9773dfda0b1SToby Isaac         }
9783dfda0b1SToby Isaac       }
9793dfda0b1SToby Isaac     }
9803dfda0b1SToby Isaac     /* Build z faces */
9813dfda0b1SToby Isaac     for (fy = 0; fy < numYEdges; ++fy) {
9823dfda0b1SToby Isaac       for (fx = 0; fx < numXEdges; ++fx) {
9833dfda0b1SToby Isaac         for (fz = 0; fz < numZVertices; fz++) {
9843dfda0b1SToby Isaac           PetscInt face    = firstZFace + (fy*numXEdges+fx)*numZVertices + fz;
9853dfda0b1SToby Isaac           PetscInt edgeL   = firstYEdge + (fz*numXVertices+  fx)*numYEdges + fy;
9863dfda0b1SToby Isaac           PetscInt edgeR   = firstYEdge + (fz*numXVertices+((fx+1)%numXVertices))*numYEdges + fy;
9873dfda0b1SToby Isaac           PetscInt edgeB   = firstXEdge + (fz*numYVertices+  fy)*numXEdges + fx;
9883dfda0b1SToby Isaac           PetscInt edgeT   = firstXEdge + (fz*numYVertices+((fy+1)%numYVertices))*numXEdges + fx;
989b5a892a1SMatthew G. Knepley           PetscInt ornt[4] = {0, 0, -1, -1};
9903dfda0b1SToby Isaac           PetscInt cone[4];
9913dfda0b1SToby Isaac 
9923dfda0b1SToby Isaac           if (dim == 2) {
993b5a892a1SMatthew G. Knepley             if (bdX == DM_BOUNDARY_TWIST && fx == numXEdges-1) {edgeR += numYEdges-1-2*fy; ornt[1] = -1;}
9943dfda0b1SToby Isaac             if (bdY == DM_BOUNDARY_TWIST && fy == numYEdges-1) {edgeT += numXEdges-1-2*fx; ornt[2] =  0;}
9959566063dSJacob Faibussowitsch             if (bdX != DM_BOUNDARY_NONE && fx == numXEdges-1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, face, 2));
9969566063dSJacob Faibussowitsch             if (bdY != DM_BOUNDARY_NONE && fy == numYEdges-1 && cutLabel) PetscCall(DMLabelSetValue(cutLabel, face, 2));
997d1c88043SMatthew G. Knepley           } else {
9983dfda0b1SToby Isaac             /* markers */
9993dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
10003dfda0b1SToby Isaac               if (fz == numZVertices-1) {
10019566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerTop));
10029566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerTop));
10033dfda0b1SToby Isaac               }
10043dfda0b1SToby Isaac               else if (fz == 0) {
10059566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", face, faceMarkerBottom));
10069566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", face, markerBottom));
10073dfda0b1SToby Isaac               }
10083dfda0b1SToby Isaac             }
10093dfda0b1SToby Isaac           }
10103dfda0b1SToby Isaac           cone[0] = edgeB; cone[1] = edgeR; cone[2] = edgeT; cone[3] = edgeL;
10119566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, face, cone));
10129566063dSJacob Faibussowitsch           PetscCall(DMPlexSetConeOrientation(dm, face, ornt));
10133dfda0b1SToby Isaac         }
10143dfda0b1SToby Isaac       }
10153dfda0b1SToby Isaac     }
10163dfda0b1SToby Isaac     /* Build Z edges*/
10173dfda0b1SToby Isaac     for (vy = 0; vy < numYVertices; vy++) {
10183dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
10193dfda0b1SToby Isaac         for (ez = 0; ez < numZEdges; ez++) {
10203dfda0b1SToby Isaac           const PetscInt edge    = firstZEdge  + (vy*numXVertices+vx)*numZEdges + ez;
10213dfda0b1SToby Isaac           const PetscInt vertexB = firstVertex + (ez                   *numYVertices+vy)*numXVertices + vx;
10223dfda0b1SToby Isaac           const PetscInt vertexT = firstVertex + (((ez+1)%numZVertices)*numYVertices+vy)*numXVertices + vx;
10233dfda0b1SToby Isaac           PetscInt       cone[2];
10243dfda0b1SToby Isaac 
1025c2df9bbfSMatthew G. Knepley           cone[0] = vertexB; cone[1] = vertexT;
1026c2df9bbfSMatthew G. Knepley           PetscCall(DMPlexSetCone(dm, edge, cone));
10273dfda0b1SToby Isaac           if (dim == 3) {
10283dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
10293dfda0b1SToby Isaac               if (vx == numXVertices-1) {
10309566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
1031c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1032c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges-1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1033c2df9bbfSMatthew G. Knepley               } else if (vx == 0) {
10349566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
1035c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1036c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges-1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
10373dfda0b1SToby Isaac               }
10383dfda0b1SToby Isaac             }
10393dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
10403dfda0b1SToby Isaac               if (vy == numYVertices-1) {
10419566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBack));
1042c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBack));
1043c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges-1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBack));
1044c2df9bbfSMatthew G. Knepley               } else if (vy == 0) {
10459566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerFront));
1046c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerFront));
1047c2df9bbfSMatthew G. Knepley                 if (ez == numZEdges-1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerFront));
10483dfda0b1SToby Isaac               }
10493dfda0b1SToby Isaac             }
10503dfda0b1SToby Isaac           }
10513dfda0b1SToby Isaac         }
10523dfda0b1SToby Isaac       }
10533dfda0b1SToby Isaac     }
10543dfda0b1SToby Isaac     /* Build Y edges*/
10553dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
10563dfda0b1SToby Isaac       for (vx = 0; vx < numXVertices; vx++) {
10573dfda0b1SToby Isaac         for (ey = 0; ey < numYEdges; ey++) {
10583dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdY == DM_BOUNDARY_TWIST && ey == numYEdges-1) ? (numXVertices-vx-1) : (vz*numYVertices+((ey+1)%numYVertices))*numXVertices + vx;
10593dfda0b1SToby Isaac           const PetscInt edge    = firstYEdge  + (vz*numXVertices+vx)*numYEdges + ey;
10603dfda0b1SToby Isaac           const PetscInt vertexF = firstVertex + (vz*numYVertices+ey)*numXVertices + vx;
10613dfda0b1SToby Isaac           const PetscInt vertexK = firstVertex + nextv;
10623dfda0b1SToby Isaac           PetscInt       cone[2];
10633dfda0b1SToby Isaac 
10643dfda0b1SToby Isaac           cone[0] = vertexF; cone[1] = vertexK;
10659566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, edge, cone));
10663dfda0b1SToby Isaac           if (dim == 2) {
10673dfda0b1SToby Isaac             if ((bdX != DM_BOUNDARY_PERIODIC) && (bdX != DM_BOUNDARY_TWIST)) {
10683dfda0b1SToby Isaac               if (vx == numXVertices-1) {
10699566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerRight));
10709566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge,    markerRight));
10719566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1072c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges-1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1073d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
10749566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerLeft));
10759566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge,    markerLeft));
10769566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1077c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges-1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
10783dfda0b1SToby Isaac               }
1079d8211ee3SMatthew G. Knepley             } else {
10804c67ea77SStefano Zampini               if (vx == 0 && cutLabel) {
10819566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, edge,    1));
10829566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, cone[0], 1));
1083c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges-1) PetscCall(DMLabelSetValue(cutLabel, cone[1], 1));
10843dfda0b1SToby Isaac               }
1085d8211ee3SMatthew G. Knepley             }
1086d8211ee3SMatthew G. Knepley           } else {
10873dfda0b1SToby Isaac             if (bdX != DM_BOUNDARY_PERIODIC) {
10883dfda0b1SToby Isaac               if (vx == numXVertices-1) {
10899566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerRight));
1090c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerRight));
1091c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges-1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerRight));
1092d8211ee3SMatthew G. Knepley               } else if (vx == 0) {
10939566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerLeft));
1094c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerLeft));
1095c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges-1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerLeft));
10963dfda0b1SToby Isaac               }
10973dfda0b1SToby Isaac             }
10983dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
10993dfda0b1SToby Isaac               if (vz == numZVertices-1) {
11009566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
1101c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1102c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges-1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1103d8211ee3SMatthew G. Knepley               } else if (vz == 0) {
11049566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
1105c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1106c2df9bbfSMatthew G. Knepley                 if (ey == numYEdges-1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
11073dfda0b1SToby Isaac               }
11083dfda0b1SToby Isaac             }
11093dfda0b1SToby Isaac           }
11103dfda0b1SToby Isaac         }
11113dfda0b1SToby Isaac       }
11123dfda0b1SToby Isaac     }
11133dfda0b1SToby Isaac     /* Build X edges*/
11143dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; vz++) {
11153dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; vy++) {
11163dfda0b1SToby Isaac         for (ex = 0; ex < numXEdges; ex++) {
11173dfda0b1SToby Isaac           const PetscInt nextv   = (dim == 2 && bdX == DM_BOUNDARY_TWIST && ex == numXEdges-1) ? (numYVertices-vy-1)*numXVertices : (vz*numYVertices+vy)*numXVertices + (ex+1)%numXVertices;
11183dfda0b1SToby Isaac           const PetscInt edge    = firstXEdge  + (vz*numYVertices+vy)*numXEdges + ex;
11193dfda0b1SToby Isaac           const PetscInt vertexL = firstVertex + (vz*numYVertices+vy)*numXVertices + ex;
11203dfda0b1SToby Isaac           const PetscInt vertexR = firstVertex + nextv;
11213dfda0b1SToby Isaac           PetscInt       cone[2];
11223dfda0b1SToby Isaac 
11233dfda0b1SToby Isaac           cone[0] = vertexL; cone[1] = vertexR;
11249566063dSJacob Faibussowitsch           PetscCall(DMPlexSetCone(dm, edge, cone));
11253dfda0b1SToby Isaac           if (dim == 2) {
11263dfda0b1SToby Isaac             if ((bdY != DM_BOUNDARY_PERIODIC) && (bdY != DM_BOUNDARY_TWIST)) {
11273dfda0b1SToby Isaac               if (vy == numYVertices-1) {
11289566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerTop));
11299566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge,    markerTop));
11309566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1131c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges-1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1132d8211ee3SMatthew G. Knepley               } else if (vy == 0) {
11339566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "Face Sets", edge, faceMarkerBottom));
11349566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge,    markerBottom));
11359566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1136c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges-1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
11373dfda0b1SToby Isaac               }
1138d8211ee3SMatthew G. Knepley             } else {
11394c67ea77SStefano Zampini               if (vy == 0 && cutLabel) {
11409566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, edge,    1));
11419566063dSJacob Faibussowitsch                 PetscCall(DMLabelSetValue(cutLabel, cone[0], 1));
1142c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges-1) PetscCall(DMLabelSetValue(cutLabel, cone[1], 1));
11433dfda0b1SToby Isaac               }
1144d8211ee3SMatthew G. Knepley             }
1145d8211ee3SMatthew G. Knepley           } else {
11463dfda0b1SToby Isaac             if (bdY != DM_BOUNDARY_PERIODIC) {
11473dfda0b1SToby Isaac               if (vy == numYVertices-1) {
11489566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBack));
1149c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBack));
1150c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges-1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBack));
1151c2df9bbfSMatthew G. Knepley               } else if (vy == 0) {
11529566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerFront));
1153c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerFront));
1154c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges-1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerFront));
11553dfda0b1SToby Isaac               }
11563dfda0b1SToby Isaac             }
11573dfda0b1SToby Isaac             if (bdZ != DM_BOUNDARY_PERIODIC) {
11583dfda0b1SToby Isaac               if (vz == numZVertices-1) {
11599566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerTop));
1160c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerTop));
1161c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges-1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerTop));
1162c2df9bbfSMatthew G. Knepley               } else if (vz == 0) {
11639566063dSJacob Faibussowitsch                 PetscCall(DMSetLabelValue(dm, "marker", edge, markerBottom));
1164c2df9bbfSMatthew G. Knepley                 PetscCall(DMSetLabelValue(dm, "marker", cone[0], markerBottom));
1165c2df9bbfSMatthew G. Knepley                 if (ex == numXEdges-1) PetscCall(DMSetLabelValue(dm, "marker", cone[1], markerBottom));
11663dfda0b1SToby Isaac               }
11673dfda0b1SToby Isaac             }
11683dfda0b1SToby Isaac           }
11693dfda0b1SToby Isaac         }
11703dfda0b1SToby Isaac       }
11713dfda0b1SToby Isaac     }
11729566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
11739566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
11743dfda0b1SToby Isaac     /* Build coordinates */
11759566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
11769566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
11779566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
11789566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex+numVertices));
11793dfda0b1SToby Isaac     for (v = firstVertex; v < firstVertex+numVertices; ++v) {
11809566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
11819566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
11823dfda0b1SToby Isaac     }
11839566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
11849566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
11859566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
11869566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
11879566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
11889566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
11899566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates,VECSTANDARD));
11909566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
11913dfda0b1SToby Isaac     for (vz = 0; vz < numZVertices; ++vz) {
11923dfda0b1SToby Isaac       for (vy = 0; vy < numYVertices; ++vy) {
11933dfda0b1SToby Isaac         for (vx = 0; vx < numXVertices; ++vx) {
11943dfda0b1SToby Isaac           coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+0] = lower[0] + ((upper[0] - lower[0])/numXEdges)*vx;
11953dfda0b1SToby Isaac           coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+1] = lower[1] + ((upper[1] - lower[1])/numYEdges)*vy;
11963dfda0b1SToby Isaac           if (dim == 3) {
11973dfda0b1SToby Isaac             coords[((vz*numYVertices+vy)*numXVertices+vx)*dim+2] = lower[2] + ((upper[2] - lower[2])/numZEdges)*vz;
11983dfda0b1SToby Isaac           }
11993dfda0b1SToby Isaac         }
12003dfda0b1SToby Isaac       }
12013dfda0b1SToby Isaac     }
12029566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
12039566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
12049566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
12053dfda0b1SToby Isaac   }
12063dfda0b1SToby Isaac   PetscFunctionReturn(0);
12073dfda0b1SToby Isaac }
12083dfda0b1SToby Isaac 
12099318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxMesh_Tensor_Internal(DM dm, PetscInt dim, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
1210a6dfd86eSKarl Rupp {
12119318fe57SMatthew G. Knepley   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
12129318fe57SMatthew G. Knepley   PetscInt       fac[3] = {0, 0, 0}, d;
1213552f7358SJed Brown 
1214552f7358SJed Brown   PetscFunctionBegin;
12159318fe57SMatthew G. Knepley   PetscValidPointer(dm, 1);
12169318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
12179566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
12189318fe57SMatthew G. Knepley   for (d = 0; d < dim; ++d) {fac[d] = faces[d]; bdt[d] = periodicity[d];}
12199566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateCubeMesh_Internal(dm, lower, upper, fac, bdt[0], bdt[1], bdt[2]));
1220768d5fceSMatthew G. Knepley   if (periodicity[0] == DM_BOUNDARY_PERIODIC || periodicity[0] == DM_BOUNDARY_TWIST ||
1221768d5fceSMatthew G. Knepley       periodicity[1] == DM_BOUNDARY_PERIODIC || periodicity[1] == DM_BOUNDARY_TWIST ||
1222768d5fceSMatthew G. Knepley       (dim > 2 && (periodicity[2] == DM_BOUNDARY_PERIODIC || periodicity[2] == DM_BOUNDARY_TWIST))) {
12236858538eSMatthew G. Knepley     PetscReal L[3]       = {-1., -1., 0.};
12246858538eSMatthew G. Knepley     PetscReal maxCell[3] = {-1., -1., 0.};
1225552f7358SJed Brown 
12269318fe57SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
12276858538eSMatthew G. Knepley       if (periodicity[d] != DM_BOUNDARY_NONE) {
12289318fe57SMatthew G. Knepley         L[d]       = upper[d] - lower[d];
12299318fe57SMatthew G. Knepley         maxCell[d] = 1.1 * (L[d] / PetscMax(1, faces[d]));
1230768d5fceSMatthew G. Knepley       }
12316858538eSMatthew G. Knepley     }
12324fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, maxCell, lower, L));
1233768d5fceSMatthew G. Knepley   }
12349566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
12359318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
12369318fe57SMatthew G. Knepley }
12379318fe57SMatthew G. Knepley 
12389318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoxMesh_Internal(DM dm, PetscInt dim, PetscBool simplex, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool interpolate)
12399318fe57SMatthew G. Knepley {
12409318fe57SMatthew G. Knepley   PetscFunctionBegin;
12419566063dSJacob Faibussowitsch   if (dim == 1)      PetscCall(DMPlexCreateLineMesh_Internal(dm, faces[0], lower[0], upper[0], periodicity[0]));
12429566063dSJacob Faibussowitsch   else if (simplex)  PetscCall(DMPlexCreateBoxMesh_Simplex_Internal(dm, dim, faces, lower, upper, periodicity, interpolate));
12439566063dSJacob Faibussowitsch   else               PetscCall(DMPlexCreateBoxMesh_Tensor_Internal(dm, dim, faces, lower, upper, periodicity));
12449318fe57SMatthew G. Knepley   if (!interpolate && dim > 1 && !simplex) {
1245768d5fceSMatthew G. Knepley     DM udm;
1246768d5fceSMatthew G. Knepley 
12479566063dSJacob Faibussowitsch     PetscCall(DMPlexUninterpolate(dm, &udm));
12489566063dSJacob Faibussowitsch     PetscCall(DMPlexCopyCoordinates(dm, udm));
12499566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(dm, &udm));
1250768d5fceSMatthew G. Knepley   }
1251768d5fceSMatthew G. Knepley   PetscFunctionReturn(0);
1252c8c68bd8SToby Isaac }
1253c8c68bd8SToby Isaac 
1254768d5fceSMatthew G. Knepley /*@C
1255768d5fceSMatthew G. Knepley   DMPlexCreateBoxMesh - Creates a mesh on the tensor product of unit intervals (box) using simplices or tensor cells (hexahedra).
1256768d5fceSMatthew G. Knepley 
1257d083f849SBarry Smith   Collective
1258768d5fceSMatthew G. Knepley 
1259768d5fceSMatthew G. Knepley   Input Parameters:
1260768d5fceSMatthew G. Knepley + comm        - The communicator for the DM object
1261768d5fceSMatthew G. Knepley . dim         - The spatial dimension
1262768d5fceSMatthew G. Knepley . simplex     - PETSC_TRUE for simplices, PETSC_FALSE for tensor cells
1263fdbf62faSLisandro Dalcin . faces       - Number of faces per dimension, or NULL for (1,) in 1D and (2, 2) in 2D and (1, 1, 1) in 3D
1264768d5fceSMatthew G. Knepley . lower       - The lower left corner, or NULL for (0, 0, 0)
1265768d5fceSMatthew G. Knepley . upper       - The upper right corner, or NULL for (1, 1, 1)
1266fdbf62faSLisandro Dalcin . periodicity - The boundary type for the X,Y,Z direction, or NULL for DM_BOUNDARY_NONE
1267768d5fceSMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
1268768d5fceSMatthew G. Knepley 
1269768d5fceSMatthew G. Knepley   Output Parameter:
1270768d5fceSMatthew G. Knepley . dm  - The DM object
1271768d5fceSMatthew G. Knepley 
12729318fe57SMatthew G. Knepley   Note: If you want to customize this mesh using options, you just need to
12739318fe57SMatthew G. Knepley $  DMCreate(comm, &dm);
12749318fe57SMatthew G. Knepley $  DMSetType(dm, DMPLEX);
12759318fe57SMatthew G. Knepley $  DMSetFromOptions(dm);
12769318fe57SMatthew G. Knepley and use the options on the DMSetFromOptions() page.
12771367e252SJed Brown 
12781367e252SJed Brown   Here is the numbering returned for 2 faces in each direction for tensor cells:
1279768d5fceSMatthew G. Knepley $ 10---17---11---18----12
1280768d5fceSMatthew G. Knepley $  |         |         |
1281768d5fceSMatthew G. Knepley $  |         |         |
1282768d5fceSMatthew G. Knepley $ 20    2   22    3    24
1283768d5fceSMatthew G. Knepley $  |         |         |
1284768d5fceSMatthew G. Knepley $  |         |         |
1285768d5fceSMatthew G. Knepley $  7---15----8---16----9
1286768d5fceSMatthew G. Knepley $  |         |         |
1287768d5fceSMatthew G. Knepley $  |         |         |
1288768d5fceSMatthew G. Knepley $ 19    0   21    1   23
1289768d5fceSMatthew G. Knepley $  |         |         |
1290768d5fceSMatthew G. Knepley $  |         |         |
1291768d5fceSMatthew G. Knepley $  4---13----5---14----6
1292768d5fceSMatthew G. Knepley 
1293768d5fceSMatthew G. Knepley and for simplicial cells
1294768d5fceSMatthew G. Knepley 
1295768d5fceSMatthew G. Knepley $ 14----8---15----9----16
1296768d5fceSMatthew G. Knepley $  |\     5  |\      7 |
1297768d5fceSMatthew G. Knepley $  | \       | \       |
1298768d5fceSMatthew G. Knepley $ 13   2    14    3    15
1299768d5fceSMatthew G. Knepley $  | 4   \   | 6   \   |
1300768d5fceSMatthew G. Knepley $  |       \ |       \ |
1301768d5fceSMatthew G. Knepley $ 11----6---12----7----13
1302768d5fceSMatthew G. Knepley $  |\        |\        |
1303768d5fceSMatthew G. Knepley $  | \    1  | \     3 |
1304768d5fceSMatthew G. Knepley $ 10   0    11    1    12
1305768d5fceSMatthew G. Knepley $  | 0   \   | 2   \   |
1306768d5fceSMatthew G. Knepley $  |       \ |       \ |
1307768d5fceSMatthew G. Knepley $  8----4----9----5----10
1308768d5fceSMatthew G. Knepley 
1309768d5fceSMatthew G. Knepley   Level: beginner
1310768d5fceSMatthew G. Knepley 
1311db781477SPatrick Sanan .seealso: `DMSetFromOptions()`, `DMPlexCreateFromFile()`, `DMPlexCreateHexCylinderMesh()`, `DMSetType()`, `DMCreate()`
1312768d5fceSMatthew G. Knepley @*/
1313768d5fceSMatthew G. Knepley 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)
1314552f7358SJed Brown {
13159318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
1316fdbf62faSLisandro Dalcin   PetscReal      low[3] = {0, 0, 0};
1317fdbf62faSLisandro Dalcin   PetscReal      upp[3] = {1, 1, 1};
1318fdbf62faSLisandro Dalcin   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
1319552f7358SJed Brown 
1320768d5fceSMatthew G. Knepley   PetscFunctionBegin;
13219566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm,dm));
13229566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm,DMPLEX));
13239566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxMesh_Internal(*dm, dim, simplex, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt, interpolate));
13247ff04441SMatthew G. Knepley   if (periodicity) PetscCall(DMLocalizeCoordinates(*dm));
13259318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
13269318fe57SMatthew G. Knepley }
1327fdbf62faSLisandro Dalcin 
1328d410b0cfSMatthew G. Knepley static PetscErrorCode DMPlexCreateWedgeBoxMesh_Internal(DM dm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[])
13299318fe57SMatthew G. Knepley {
13309318fe57SMatthew G. Knepley   DM             bdm, vol;
13319318fe57SMatthew G. Knepley   PetscInt       i;
13329318fe57SMatthew G. Knepley 
13339318fe57SMatthew G. Knepley   PetscFunctionBegin;
133408401ef6SPierre Jolivet   for (i = 0; i < 3; ++i) PetscCheck(periodicity[i] == DM_BOUNDARY_NONE,PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Periodicity not yet supported");
13359566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject) dm), &bdm));
13369566063dSJacob Faibussowitsch   PetscCall(DMSetType(bdm, DMPLEX));
13379566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(bdm, 2));
13389566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBoxMesh_Simplex_Internal(bdm, 2, faces, lower, upper, periodicity, PETSC_TRUE));
13399566063dSJacob Faibussowitsch   PetscCall(DMPlexExtrude(bdm, faces[2], upper[2] - lower[2], PETSC_TRUE, PETSC_FALSE, NULL, NULL, &vol));
13409566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&bdm));
13419566063dSJacob Faibussowitsch   PetscCall(DMPlexReplace_Static(dm, &vol));
13429318fe57SMatthew G. Knepley   if (lower[2] != 0.0) {
13439318fe57SMatthew G. Knepley     Vec          v;
13449318fe57SMatthew G. Knepley     PetscScalar *x;
13459318fe57SMatthew G. Knepley     PetscInt     cDim, n;
13469318fe57SMatthew G. Knepley 
13479566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &v));
13489566063dSJacob Faibussowitsch     PetscCall(VecGetBlockSize(v, &cDim));
13499566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(v, &n));
13509566063dSJacob Faibussowitsch     PetscCall(VecGetArray(v, &x));
13519318fe57SMatthew G. Knepley     x   += cDim;
13529318fe57SMatthew G. Knepley     for (i = 0; i < n; i += cDim) x[i] += lower[2];
13539566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(v,&x));
13549566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, v));
13559318fe57SMatthew G. Knepley   }
1356552f7358SJed Brown   PetscFunctionReturn(0);
1357552f7358SJed Brown }
1358552f7358SJed Brown 
135900dabe28SStefano Zampini /*@
136000dabe28SStefano Zampini   DMPlexCreateWedgeBoxMesh - Creates a 3-D mesh tesselating the (x,y) plane and extruding in the third direction using wedge cells.
136100dabe28SStefano Zampini 
1362d083f849SBarry Smith   Collective
136300dabe28SStefano Zampini 
136400dabe28SStefano Zampini   Input Parameters:
136500dabe28SStefano Zampini + comm        - The communicator for the DM object
136600dabe28SStefano Zampini . faces       - Number of faces per dimension, or NULL for (1, 1, 1)
136700dabe28SStefano Zampini . lower       - The lower left corner, or NULL for (0, 0, 0)
136800dabe28SStefano Zampini . upper       - The upper right corner, or NULL for (1, 1, 1)
136900dabe28SStefano Zampini . periodicity - The boundary type for the X,Y,Z direction, or NULL for DM_BOUNDARY_NONE
1370d0fcb9c2SMatthew G. Knepley . orderHeight - If PETSC_TRUE, orders the extruded cells in the height first. Otherwise, orders the cell on the layers first
137100dabe28SStefano Zampini - interpolate - Flag to create intermediate mesh pieces (edges, faces)
137200dabe28SStefano Zampini 
137300dabe28SStefano Zampini   Output Parameter:
137400dabe28SStefano Zampini . dm  - The DM object
137500dabe28SStefano Zampini 
137600dabe28SStefano Zampini   Level: beginner
137700dabe28SStefano Zampini 
1378db781477SPatrick Sanan .seealso: `DMPlexCreateHexCylinderMesh()`, `DMPlexCreateWedgeCylinderMesh()`, `DMExtrude()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
137900dabe28SStefano Zampini @*/
1380d0fcb9c2SMatthew G. Knepley PetscErrorCode DMPlexCreateWedgeBoxMesh(MPI_Comm comm, const PetscInt faces[], const PetscReal lower[], const PetscReal upper[], const DMBoundaryType periodicity[], PetscBool orderHeight, PetscBool interpolate, DM *dm)
138100dabe28SStefano Zampini {
13829318fe57SMatthew G. Knepley   PetscInt       fac[3] = {1, 1, 1};
138300dabe28SStefano Zampini   PetscReal      low[3] = {0, 0, 0};
138400dabe28SStefano Zampini   PetscReal      upp[3] = {1, 1, 1};
138500dabe28SStefano Zampini   DMBoundaryType bdt[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
138600dabe28SStefano Zampini 
138700dabe28SStefano Zampini   PetscFunctionBegin;
13889566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm,dm));
13899566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm,DMPLEX));
13909566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateWedgeBoxMesh_Internal(*dm, faces ? faces : fac, lower ? lower : low, upper ? upper : upp, periodicity ? periodicity : bdt));
1391d410b0cfSMatthew G. Knepley   if (!interpolate) {
1392d410b0cfSMatthew G. Knepley     DM udm;
139300dabe28SStefano Zampini 
13949566063dSJacob Faibussowitsch     PetscCall(DMPlexUninterpolate(*dm, &udm));
13959566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(*dm, &udm));
139600dabe28SStefano Zampini   }
13977ff04441SMatthew G. Knepley   if (periodicity) PetscCall(DMLocalizeCoordinates(*dm));
139800dabe28SStefano Zampini   PetscFunctionReturn(0);
139900dabe28SStefano Zampini }
140000dabe28SStefano Zampini 
1401a9074c1eSMatthew G. Knepley /*@C
1402a9074c1eSMatthew G. Knepley   DMPlexSetOptionsPrefix - Sets the prefix used for searching for all DM options in the database.
1403a9074c1eSMatthew G. Knepley 
1404d083f849SBarry Smith   Logically Collective on dm
1405a9074c1eSMatthew G. Knepley 
1406a9074c1eSMatthew G. Knepley   Input Parameters:
1407a9074c1eSMatthew G. Knepley + dm - the DM context
1408a9074c1eSMatthew G. Knepley - prefix - the prefix to prepend to all option names
1409a9074c1eSMatthew G. Knepley 
1410a9074c1eSMatthew G. Knepley   Notes:
1411a9074c1eSMatthew G. Knepley   A hyphen (-) must NOT be given at the beginning of the prefix name.
1412a9074c1eSMatthew G. Knepley   The first character of all runtime options is AUTOMATICALLY the hyphen.
1413a9074c1eSMatthew G. Knepley 
1414a9074c1eSMatthew G. Knepley   Level: advanced
1415a9074c1eSMatthew G. Knepley 
1416db781477SPatrick Sanan .seealso: `SNESSetFromOptions()`
1417a9074c1eSMatthew G. Knepley @*/
1418a9074c1eSMatthew G. Knepley PetscErrorCode DMPlexSetOptionsPrefix(DM dm, const char prefix[])
1419a9074c1eSMatthew G. Knepley {
1420a9074c1eSMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex *) dm->data;
1421a9074c1eSMatthew G. Knepley 
1422a9074c1eSMatthew G. Knepley   PetscFunctionBegin;
1423a9074c1eSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
14249566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject) dm, prefix));
14259566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject) mesh->partitioner, prefix));
1426a9074c1eSMatthew G. Knepley   PetscFunctionReturn(0);
1427a9074c1eSMatthew G. Knepley }
1428a9074c1eSMatthew G. Knepley 
14299318fe57SMatthew G. Knepley /* Remap geometry to cylinder
143061a622f3SMatthew G. Knepley    TODO: This only works for a single refinement, then it is broken
143161a622f3SMatthew G. Knepley 
14329318fe57SMatthew G. Knepley      Interior square: Linear interpolation is correct
14339318fe57SMatthew G. Knepley      The other cells all have vertices on rays from the origin. We want to uniformly expand the spacing
14349318fe57SMatthew G. Knepley      such that the last vertex is on the unit circle. So the closest and farthest vertices are at distance
14350510c589SMatthew G. Knepley 
14369318fe57SMatthew G. Knepley        phi     = arctan(y/x)
14379318fe57SMatthew G. Knepley        d_close = sqrt(1/8 + 1/4 sin^2(phi))
14389318fe57SMatthew G. Knepley        d_far   = sqrt(1/2 + sin^2(phi))
14390510c589SMatthew G. Knepley 
14409318fe57SMatthew G. Knepley      so we remap them using
14410510c589SMatthew G. Knepley 
14429318fe57SMatthew G. Knepley        x_new = x_close + (x - x_close) (1 - d_close) / (d_far - d_close)
14439318fe57SMatthew G. Knepley        y_new = y_close + (y - y_close) (1 - d_close) / (d_far - d_close)
14440510c589SMatthew G. Knepley 
14459318fe57SMatthew G. Knepley      If pi/4 < phi < 3pi/4 or -3pi/4 < phi < -pi/4, then we switch x and y.
14469318fe57SMatthew G. Knepley */
14479318fe57SMatthew G. Knepley static void snapToCylinder(PetscInt dim, PetscInt Nf, PetscInt NfAux,
14489318fe57SMatthew G. Knepley                            const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
14499318fe57SMatthew G. Knepley                            const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
14509318fe57SMatthew G. Knepley                            PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
14519318fe57SMatthew G. Knepley {
14529318fe57SMatthew G. Knepley   const PetscReal dis = 1.0/PetscSqrtReal(2.0);
14539318fe57SMatthew G. Knepley   const PetscReal ds2 = 0.5*dis;
145422cc497dSMatthew G. Knepley 
14559318fe57SMatthew G. Knepley   if ((PetscAbsScalar(u[0]) <= ds2) && (PetscAbsScalar(u[1]) <= ds2)) {
14569318fe57SMatthew G. Knepley     f0[0] = u[0];
14579318fe57SMatthew G. Knepley     f0[1] = u[1];
14589318fe57SMatthew G. Knepley   } else {
14599318fe57SMatthew G. Knepley     PetscReal phi, sinp, cosp, dc, df, x, y, xc, yc;
14600510c589SMatthew G. Knepley 
14619318fe57SMatthew G. Knepley     x    = PetscRealPart(u[0]);
14629318fe57SMatthew G. Knepley     y    = PetscRealPart(u[1]);
14639318fe57SMatthew G. Knepley     phi  = PetscAtan2Real(y, x);
14649318fe57SMatthew G. Knepley     sinp = PetscSinReal(phi);
14659318fe57SMatthew G. Knepley     cosp = PetscCosReal(phi);
14669318fe57SMatthew G. Knepley     if ((PetscAbsReal(phi) > PETSC_PI/4.0) && (PetscAbsReal(phi) < 3.0*PETSC_PI/4.0)) {
14679318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2/sinp);
14689318fe57SMatthew G. Knepley       df = PetscAbsReal(dis/sinp);
14699318fe57SMatthew G. Knepley       xc = ds2*x/PetscAbsReal(y);
14709318fe57SMatthew G. Knepley       yc = ds2*PetscSignReal(y);
14719318fe57SMatthew G. Knepley     } else {
14729318fe57SMatthew G. Knepley       dc = PetscAbsReal(ds2/cosp);
14739318fe57SMatthew G. Knepley       df = PetscAbsReal(dis/cosp);
14749318fe57SMatthew G. Knepley       xc = ds2*PetscSignReal(x);
14759318fe57SMatthew G. Knepley       yc = ds2*y/PetscAbsReal(x);
14769318fe57SMatthew G. Knepley     }
14779318fe57SMatthew G. Knepley     f0[0] = xc + (u[0] - xc)*(1.0 - dc)/(df - dc);
14789318fe57SMatthew G. Knepley     f0[1] = yc + (u[1] - yc)*(1.0 - dc)/(df - dc);
14799318fe57SMatthew G. Knepley   }
14809318fe57SMatthew G. Knepley   f0[2] = u[2];
14819318fe57SMatthew G. Knepley }
14820510c589SMatthew G. Knepley 
14839318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateHexCylinderMesh_Internal(DM dm, DMBoundaryType periodicZ)
14840510c589SMatthew G. Knepley {
14850510c589SMatthew G. Knepley   const PetscInt dim = 3;
14869318fe57SMatthew G. Knepley   PetscInt       numCells, numVertices;
1487d8c47e87SMatthew G. Knepley   PetscMPIInt    rank;
14880510c589SMatthew G. Knepley 
14890510c589SMatthew G. Knepley   PetscFunctionBegin;
14909566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank));
14919566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
14920510c589SMatthew G. Knepley   /* Create topology */
14930510c589SMatthew G. Knepley   {
14940510c589SMatthew G. Knepley     PetscInt cone[8], c;
14950510c589SMatthew G. Knepley 
1496dd400576SPatrick Sanan     numCells    = rank == 0 ?  5 : 0;
1497dd400576SPatrick Sanan     numVertices = rank == 0 ? 16 : 0;
1498006a8963SMatthew G. Knepley     if (periodicZ == DM_BOUNDARY_PERIODIC) {
1499ae8bcbbbSMatthew G. Knepley       numCells   *= 3;
1500dd400576SPatrick Sanan       numVertices = rank == 0 ? 24 : 0;
1501006a8963SMatthew G. Knepley     }
15029566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells+numVertices));
15039566063dSJacob Faibussowitsch     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 8));
15049566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm));
1505dd400576SPatrick Sanan     if (rank == 0) {
1506006a8963SMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
1507ae8bcbbbSMatthew G. Knepley         cone[0] = 15; cone[1] = 18; cone[2] = 17; cone[3] = 16;
1508ae8bcbbbSMatthew G. Knepley         cone[4] = 31; cone[5] = 32; cone[6] = 33; cone[7] = 34;
15099566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
1510ae8bcbbbSMatthew G. Knepley         cone[0] = 16; cone[1] = 17; cone[2] = 24; cone[3] = 23;
1511ae8bcbbbSMatthew G. Knepley         cone[4] = 32; cone[5] = 36; cone[6] = 37; cone[7] = 33; /* 22 25 26 21 */
15129566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
1513ae8bcbbbSMatthew G. Knepley         cone[0] = 18; cone[1] = 27; cone[2] = 24; cone[3] = 17;
1514ae8bcbbbSMatthew G. Knepley         cone[4] = 34; cone[5] = 33; cone[6] = 37; cone[7] = 38;
15159566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
1516ae8bcbbbSMatthew G. Knepley         cone[0] = 29; cone[1] = 27; cone[2] = 18; cone[3] = 15;
1517ae8bcbbbSMatthew G. Knepley         cone[4] = 35; cone[5] = 31; cone[6] = 34; cone[7] = 38;
15189566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
1519ae8bcbbbSMatthew G. Knepley         cone[0] = 29; cone[1] = 15; cone[2] = 16; cone[3] = 23;
1520ae8bcbbbSMatthew G. Knepley         cone[4] = 35; cone[5] = 36; cone[6] = 32; cone[7] = 31;
15219566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
1522006a8963SMatthew G. Knepley 
1523ae8bcbbbSMatthew G. Knepley         cone[0] = 31; cone[1] = 34; cone[2] = 33; cone[3] = 32;
1524ae8bcbbbSMatthew G. Knepley         cone[4] = 19; cone[5] = 22; cone[6] = 21; cone[7] = 20;
15259566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 5, cone));
1526ae8bcbbbSMatthew G. Knepley         cone[0] = 32; cone[1] = 33; cone[2] = 37; cone[3] = 36;
1527ae8bcbbbSMatthew G. Knepley         cone[4] = 22; cone[5] = 25; cone[6] = 26; cone[7] = 21;
15289566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 6, cone));
1529ae8bcbbbSMatthew G. Knepley         cone[0] = 34; cone[1] = 38; cone[2] = 37; cone[3] = 33;
1530ae8bcbbbSMatthew G. Knepley         cone[4] = 20; cone[5] = 21; cone[6] = 26; cone[7] = 28;
15319566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 7, cone));
1532ae8bcbbbSMatthew G. Knepley         cone[0] = 35; cone[1] = 38; cone[2] = 34; cone[3] = 31;
1533ae8bcbbbSMatthew G. Knepley         cone[4] = 30; cone[5] = 19; cone[6] = 20; cone[7] = 28;
15349566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 8, cone));
1535ae8bcbbbSMatthew G. Knepley         cone[0] = 35; cone[1] = 31; cone[2] = 32; cone[3] = 36;
1536ae8bcbbbSMatthew G. Knepley         cone[4] = 30; cone[5] = 25; cone[6] = 22; cone[7] = 19;
15379566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 9, cone));
1538ae8bcbbbSMatthew G. Knepley 
1539ae8bcbbbSMatthew G. Knepley         cone[0] = 19; cone[1] = 20; cone[2] = 21; cone[3] = 22;
1540ae8bcbbbSMatthew G. Knepley         cone[4] = 15; cone[5] = 16; cone[6] = 17; cone[7] = 18;
15419566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 10, cone));
1542ae8bcbbbSMatthew G. Knepley         cone[0] = 22; cone[1] = 21; cone[2] = 26; cone[3] = 25;
1543ae8bcbbbSMatthew G. Knepley         cone[4] = 16; cone[5] = 23; cone[6] = 24; cone[7] = 17;
15449566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 11, cone));
1545ae8bcbbbSMatthew G. Knepley         cone[0] = 20; cone[1] = 28; cone[2] = 26; cone[3] = 21;
1546ae8bcbbbSMatthew G. Knepley         cone[4] = 18; cone[5] = 17; cone[6] = 24; cone[7] = 27;
15479566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 12, cone));
1548ae8bcbbbSMatthew G. Knepley         cone[0] = 30; cone[1] = 28; cone[2] = 20; cone[3] = 19;
1549ae8bcbbbSMatthew G. Knepley         cone[4] = 29; cone[5] = 15; cone[6] = 18; cone[7] = 27;
15509566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 13, cone));
1551ae8bcbbbSMatthew G. Knepley         cone[0] = 30; cone[1] = 19; cone[2] = 22; cone[3] = 25;
1552ae8bcbbbSMatthew G. Knepley         cone[4] = 29; cone[5] = 23; cone[6] = 16; cone[7] = 15;
15539566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 14, cone));
1554006a8963SMatthew G. Knepley       } else {
155510c6f908SMatthew G. Knepley         cone[0] =  5; cone[1] =  8; cone[2] =  7; cone[3] =  6;
155610c6f908SMatthew G. Knepley         cone[4] =  9; cone[5] = 12; cone[6] = 11; cone[7] = 10;
15579566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
155810c6f908SMatthew G. Knepley         cone[0] =  6; cone[1] =  7; cone[2] = 14; cone[3] = 13;
155910c6f908SMatthew G. Knepley         cone[4] = 12; cone[5] = 15; cone[6] = 16; cone[7] = 11;
15609566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
156110c6f908SMatthew G. Knepley         cone[0] =  8; cone[1] = 17; cone[2] = 14; cone[3] =  7;
156210c6f908SMatthew G. Knepley         cone[4] = 10; cone[5] = 11; cone[6] = 16; cone[7] = 18;
15639566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
156410c6f908SMatthew G. Knepley         cone[0] = 19; cone[1] = 17; cone[2] =  8; cone[3] =  5;
156510c6f908SMatthew G. Knepley         cone[4] = 20; cone[5] =  9; cone[6] = 10; cone[7] = 18;
15669566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
156710c6f908SMatthew G. Knepley         cone[0] = 19; cone[1] =  5; cone[2] =  6; cone[3] = 13;
156810c6f908SMatthew G. Knepley         cone[4] = 20; cone[5] = 15; cone[6] = 12; cone[7] =  9;
15699566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
1570006a8963SMatthew G. Knepley       }
1571d8c47e87SMatthew G. Knepley     }
15729566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
15739566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
15740510c589SMatthew G. Knepley   }
1575dbc1dc17SMatthew G. Knepley   /* Create cube geometry */
15760510c589SMatthew G. Knepley   {
15770510c589SMatthew G. Knepley     Vec             coordinates;
15780510c589SMatthew G. Knepley     PetscSection    coordSection;
15790510c589SMatthew G. Knepley     PetscScalar    *coords;
15800510c589SMatthew G. Knepley     PetscInt        coordSize, v;
15810510c589SMatthew G. Knepley     const PetscReal dis = 1.0/PetscSqrtReal(2.0);
15820510c589SMatthew G. Knepley     const PetscReal ds2 = dis/2.0;
15830510c589SMatthew G. Knepley 
15840510c589SMatthew G. Knepley     /* Build coordinates */
15859566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
15869566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
15879566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
15889566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, numCells, numCells+numVertices));
15890510c589SMatthew G. Knepley     for (v = numCells; v < numCells+numVertices; ++v) {
15909566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
15919566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
15920510c589SMatthew G. Knepley     }
15939566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
15949566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
15959566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
15969566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
15979566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
15989566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
15999566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates,VECSTANDARD));
16009566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
1601dd400576SPatrick Sanan     if (rank == 0) {
16020510c589SMatthew G. Knepley       coords[0*dim+0] = -ds2; coords[0*dim+1] = -ds2; coords[0*dim+2] = 0.0;
16030510c589SMatthew G. Knepley       coords[1*dim+0] =  ds2; coords[1*dim+1] = -ds2; coords[1*dim+2] = 0.0;
16040510c589SMatthew G. Knepley       coords[2*dim+0] =  ds2; coords[2*dim+1] =  ds2; coords[2*dim+2] = 0.0;
16050510c589SMatthew G. Knepley       coords[3*dim+0] = -ds2; coords[3*dim+1] =  ds2; coords[3*dim+2] = 0.0;
16060510c589SMatthew G. Knepley       coords[4*dim+0] = -ds2; coords[4*dim+1] = -ds2; coords[4*dim+2] = 1.0;
16070510c589SMatthew G. Knepley       coords[5*dim+0] = -ds2; coords[5*dim+1] =  ds2; coords[5*dim+2] = 1.0;
16080510c589SMatthew G. Knepley       coords[6*dim+0] =  ds2; coords[6*dim+1] =  ds2; coords[6*dim+2] = 1.0;
16090510c589SMatthew G. Knepley       coords[7*dim+0] =  ds2; coords[7*dim+1] = -ds2; coords[7*dim+2] = 1.0;
16100510c589SMatthew G. Knepley       coords[ 8*dim+0] =  dis; coords[ 8*dim+1] = -dis; coords[ 8*dim+2] = 0.0;
16110510c589SMatthew G. Knepley       coords[ 9*dim+0] =  dis; coords[ 9*dim+1] =  dis; coords[ 9*dim+2] = 0.0;
16120510c589SMatthew G. Knepley       coords[10*dim+0] =  dis; coords[10*dim+1] = -dis; coords[10*dim+2] = 1.0;
16130510c589SMatthew G. Knepley       coords[11*dim+0] =  dis; coords[11*dim+1] =  dis; coords[11*dim+2] = 1.0;
16140510c589SMatthew G. Knepley       coords[12*dim+0] = -dis; coords[12*dim+1] =  dis; coords[12*dim+2] = 0.0;
16150510c589SMatthew G. Knepley       coords[13*dim+0] = -dis; coords[13*dim+1] =  dis; coords[13*dim+2] = 1.0;
16160510c589SMatthew G. Knepley       coords[14*dim+0] = -dis; coords[14*dim+1] = -dis; coords[14*dim+2] = 0.0;
16170510c589SMatthew G. Knepley       coords[15*dim+0] = -dis; coords[15*dim+1] = -dis; coords[15*dim+2] = 1.0;
1618ae8bcbbbSMatthew G. Knepley       if (periodicZ == DM_BOUNDARY_PERIODIC) {
1619ae8bcbbbSMatthew G. Knepley         /* 15 31 19 */ coords[16*dim+0] = -ds2; coords[16*dim+1] = -ds2; coords[16*dim+2] = 0.5;
1620ae8bcbbbSMatthew G. Knepley         /* 16 32 22 */ coords[17*dim+0] =  ds2; coords[17*dim+1] = -ds2; coords[17*dim+2] = 0.5;
1621ae8bcbbbSMatthew G. Knepley         /* 17 33 21 */ coords[18*dim+0] =  ds2; coords[18*dim+1] =  ds2; coords[18*dim+2] = 0.5;
1622ae8bcbbbSMatthew G. Knepley         /* 18 34 20 */ coords[19*dim+0] = -ds2; coords[19*dim+1] =  ds2; coords[19*dim+2] = 0.5;
1623ae8bcbbbSMatthew G. Knepley         /* 29 35 30 */ coords[20*dim+0] = -dis; coords[20*dim+1] = -dis; coords[20*dim+2] = 0.5;
1624ae8bcbbbSMatthew G. Knepley         /* 23 36 25 */ coords[21*dim+0] =  dis; coords[21*dim+1] = -dis; coords[21*dim+2] = 0.5;
1625ae8bcbbbSMatthew G. Knepley         /* 24 37 26 */ coords[22*dim+0] =  dis; coords[22*dim+1] =  dis; coords[22*dim+2] = 0.5;
1626ae8bcbbbSMatthew G. Knepley         /* 27 38 28 */ coords[23*dim+0] = -dis; coords[23*dim+1] =  dis; coords[23*dim+2] = 0.5;
1627ae8bcbbbSMatthew G. Knepley       }
1628d8c47e87SMatthew G. Knepley     }
16299566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
16309566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
16319566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
16320510c589SMatthew G. Knepley   }
1633006a8963SMatthew G. Knepley   /* Create periodicity */
1634006a8963SMatthew G. Knepley   if (periodicZ == DM_BOUNDARY_PERIODIC || periodicZ == DM_BOUNDARY_TWIST) {
16356858538eSMatthew G. Knepley     PetscReal      L[3]       = {-1., -1., 0.};
16366858538eSMatthew G. Knepley     PetscReal      maxCell[3] = {-1., -1., 0.};
1637006a8963SMatthew G. Knepley     PetscReal      lower[3]   = {0.0, 0.0, 0.0};
1638ae8bcbbbSMatthew G. Knepley     PetscReal      upper[3]   = {1.0, 1.0, 1.5};
16396858538eSMatthew G. Knepley     PetscInt       numZCells  = 3;
1640006a8963SMatthew G. Knepley 
16416858538eSMatthew G. Knepley     L[2]       = upper[2] - lower[2];
16426858538eSMatthew G. Knepley     maxCell[2] = 1.1 * (L[2] / numZCells);
16434fb89dddSMatthew G. Knepley     PetscCall(DMSetPeriodicity(dm, maxCell, lower, L));
1644006a8963SMatthew G. Knepley   }
1645dbc1dc17SMatthew G. Knepley   {
16469318fe57SMatthew G. Knepley     DM          cdm;
16479318fe57SMatthew G. Knepley     PetscDS     cds;
16489318fe57SMatthew G. Knepley     PetscScalar c[2] = {1.0, 1.0};
1649dbc1dc17SMatthew G. Knepley 
16509566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCoordinateSpace(dm, 1, snapToCylinder));
16519566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
16529566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
16539566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, 2, c));
1654dbc1dc17SMatthew G. Knepley   }
16559318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
16569566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolateInPlace_Internal(dm));
16570510c589SMatthew G. Knepley   PetscFunctionReturn(0);
16580510c589SMatthew G. Knepley }
16590510c589SMatthew G. Knepley 
166024119c2aSMatthew G. Knepley /*@
16619318fe57SMatthew G. Knepley   DMPlexCreateHexCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using hexahedra.
166224119c2aSMatthew G. Knepley 
1663d083f849SBarry Smith   Collective
166424119c2aSMatthew G. Knepley 
166524119c2aSMatthew G. Knepley   Input Parameters:
166624119c2aSMatthew G. Knepley + comm      - The communicator for the DM object
16679318fe57SMatthew G. Knepley - periodicZ - The boundary type for the Z direction
166824119c2aSMatthew G. Knepley 
166924119c2aSMatthew G. Knepley   Output Parameter:
167024119c2aSMatthew G. Knepley . dm  - The DM object
167124119c2aSMatthew G. Knepley 
16729318fe57SMatthew G. Knepley   Note:
16739318fe57SMatthew G. Knepley   Here is the output numbering looking from the bottom of the cylinder:
16749318fe57SMatthew G. Knepley $       17-----14
16759318fe57SMatthew G. Knepley $        |     |
16769318fe57SMatthew G. Knepley $        |  2  |
16779318fe57SMatthew G. Knepley $        |     |
16789318fe57SMatthew G. Knepley $ 17-----8-----7-----14
16799318fe57SMatthew G. Knepley $  |     |     |     |
16809318fe57SMatthew G. Knepley $  |  3  |  0  |  1  |
16819318fe57SMatthew G. Knepley $  |     |     |     |
16829318fe57SMatthew G. Knepley $ 19-----5-----6-----13
16839318fe57SMatthew G. Knepley $        |     |
16849318fe57SMatthew G. Knepley $        |  4  |
16859318fe57SMatthew G. Knepley $        |     |
16869318fe57SMatthew G. Knepley $       19-----13
16879318fe57SMatthew G. Knepley $
16889318fe57SMatthew G. Knepley $ and up through the top
16899318fe57SMatthew G. Knepley $
16909318fe57SMatthew G. Knepley $       18-----16
16919318fe57SMatthew G. Knepley $        |     |
16929318fe57SMatthew G. Knepley $        |  2  |
16939318fe57SMatthew G. Knepley $        |     |
16949318fe57SMatthew G. Knepley $ 18----10----11-----16
16959318fe57SMatthew G. Knepley $  |     |     |     |
16969318fe57SMatthew G. Knepley $  |  3  |  0  |  1  |
16979318fe57SMatthew G. Knepley $  |     |     |     |
16989318fe57SMatthew G. Knepley $ 20-----9----12-----15
16999318fe57SMatthew G. Knepley $        |     |
17009318fe57SMatthew G. Knepley $        |  4  |
17019318fe57SMatthew G. Knepley $        |     |
17029318fe57SMatthew G. Knepley $       20-----15
17039318fe57SMatthew G. Knepley 
170424119c2aSMatthew G. Knepley   Level: beginner
170524119c2aSMatthew G. Knepley 
1706db781477SPatrick Sanan .seealso: `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
170724119c2aSMatthew G. Knepley @*/
17089318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateHexCylinderMesh(MPI_Comm comm, DMBoundaryType periodicZ, DM *dm)
17099318fe57SMatthew G. Knepley {
17109318fe57SMatthew G. Knepley   PetscFunctionBegin;
17119318fe57SMatthew G. Knepley   PetscValidPointer(dm, 3);
17129566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
17139566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
17149566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateHexCylinderMesh_Internal(*dm, periodicZ));
17159318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
17169318fe57SMatthew G. Knepley }
17179318fe57SMatthew G. Knepley 
17189318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateWedgeCylinderMesh_Internal(DM dm, PetscInt n, PetscBool interpolate)
171924119c2aSMatthew G. Knepley {
172024119c2aSMatthew G. Knepley   const PetscInt dim = 3;
1721412e9a14SMatthew G. Knepley   PetscInt       numCells, numVertices, v;
17229fe9f049SMatthew G. Knepley   PetscMPIInt    rank;
172324119c2aSMatthew G. Knepley 
172424119c2aSMatthew G. Knepley   PetscFunctionBegin;
172563a3b9bcSJacob Faibussowitsch   PetscCheck(n >= 0,PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Number of wedges %" PetscInt_FMT " cannot be negative", n);
17269566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank));
17279566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
1728412e9a14SMatthew G. Knepley   /* Must create the celltype label here so that we do not automatically try to compute the types */
17299566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "celltype"));
173024119c2aSMatthew G. Knepley   /* Create topology */
173124119c2aSMatthew G. Knepley   {
173224119c2aSMatthew G. Knepley     PetscInt cone[6], c;
173324119c2aSMatthew G. Knepley 
1734dd400576SPatrick Sanan     numCells    = rank == 0 ?        n : 0;
1735dd400576SPatrick Sanan     numVertices = rank == 0 ?  2*(n+1) : 0;
17369566063dSJacob Faibussowitsch     PetscCall(DMPlexSetChart(dm, 0, numCells+numVertices));
17379566063dSJacob Faibussowitsch     for (c = 0; c < numCells; c++) PetscCall(DMPlexSetConeSize(dm, c, 6));
17389566063dSJacob Faibussowitsch     PetscCall(DMSetUp(dm));
173924119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
174024119c2aSMatthew G. Knepley       cone[0] =  c+n*1; cone[1] = (c+1)%n+n*1; cone[2] = 0+3*n;
174124119c2aSMatthew G. Knepley       cone[3] =  c+n*2; cone[4] = (c+1)%n+n*2; cone[5] = 1+3*n;
17429566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(dm, c, cone));
17439566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCellType(dm, c, DM_POLYTOPE_TRI_PRISM_TENSOR));
174424119c2aSMatthew G. Knepley     }
17459566063dSJacob Faibussowitsch     PetscCall(DMPlexSymmetrize(dm));
17469566063dSJacob Faibussowitsch     PetscCall(DMPlexStratify(dm));
174724119c2aSMatthew G. Knepley   }
1748412e9a14SMatthew G. Knepley   for (v = numCells; v < numCells+numVertices; ++v) {
17499566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCellType(dm, v, DM_POLYTOPE_POINT));
175024119c2aSMatthew G. Knepley   }
175124119c2aSMatthew G. Knepley   /* Create cylinder geometry */
175224119c2aSMatthew G. Knepley   {
175324119c2aSMatthew G. Knepley     Vec          coordinates;
175424119c2aSMatthew G. Knepley     PetscSection coordSection;
175524119c2aSMatthew G. Knepley     PetscScalar *coords;
1756412e9a14SMatthew G. Knepley     PetscInt     coordSize, c;
175724119c2aSMatthew G. Knepley 
175824119c2aSMatthew G. Knepley     /* Build coordinates */
17599566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateSection(dm, &coordSection));
17609566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetNumFields(coordSection, 1));
17619566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dim));
17629566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetChart(coordSection, numCells, numCells+numVertices));
176324119c2aSMatthew G. Knepley     for (v = numCells; v < numCells+numVertices; ++v) {
17649566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetDof(coordSection, v, dim));
17659566063dSJacob Faibussowitsch       PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dim));
176624119c2aSMatthew G. Knepley     }
17679566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetUp(coordSection));
17689566063dSJacob Faibussowitsch     PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
17699566063dSJacob Faibussowitsch     PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
17709566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
17719566063dSJacob Faibussowitsch     PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
17729566063dSJacob Faibussowitsch     PetscCall(VecSetBlockSize(coordinates, dim));
17739566063dSJacob Faibussowitsch     PetscCall(VecSetType(coordinates,VECSTANDARD));
17749566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
177524119c2aSMatthew G. Knepley     for (c = 0; c < numCells; c++) {
177624119c2aSMatthew G. Knepley       coords[(c+0*n)*dim+0] = PetscCosReal(2.0*c*PETSC_PI/n); coords[(c+0*n)*dim+1] = PetscSinReal(2.0*c*PETSC_PI/n); coords[(c+0*n)*dim+2] = 1.0;
177724119c2aSMatthew G. Knepley       coords[(c+1*n)*dim+0] = PetscCosReal(2.0*c*PETSC_PI/n); coords[(c+1*n)*dim+1] = PetscSinReal(2.0*c*PETSC_PI/n); coords[(c+1*n)*dim+2] = 0.0;
177824119c2aSMatthew G. Knepley     }
1779dd400576SPatrick Sanan     if (rank == 0) {
178024119c2aSMatthew G. Knepley       coords[(2*n+0)*dim+0] = 0.0; coords[(2*n+0)*dim+1] = 0.0; coords[(2*n+0)*dim+2] = 1.0;
178124119c2aSMatthew G. Knepley       coords[(2*n+1)*dim+0] = 0.0; coords[(2*n+1)*dim+1] = 0.0; coords[(2*n+1)*dim+2] = 0.0;
17829fe9f049SMatthew G. Knepley     }
17839566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(coordinates, &coords));
17849566063dSJacob Faibussowitsch     PetscCall(DMSetCoordinatesLocal(dm, coordinates));
17859566063dSJacob Faibussowitsch     PetscCall(VecDestroy(&coordinates));
178624119c2aSMatthew G. Knepley   }
17879318fe57SMatthew G. Knepley   /* Interpolate */
17889566063dSJacob Faibussowitsch   if (interpolate) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
17899318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
17909318fe57SMatthew G. Knepley }
17919318fe57SMatthew G. Knepley 
17929318fe57SMatthew G. Knepley /*@
17939318fe57SMatthew G. Knepley   DMPlexCreateWedgeCylinderMesh - Creates a mesh on the tensor product of the unit interval with the circle (cylinder) using wedges.
17949318fe57SMatthew G. Knepley 
17959318fe57SMatthew G. Knepley   Collective
17969318fe57SMatthew G. Knepley 
17979318fe57SMatthew G. Knepley   Input Parameters:
17989318fe57SMatthew G. Knepley + comm - The communicator for the DM object
17999318fe57SMatthew G. Knepley . n    - The number of wedges around the origin
18009318fe57SMatthew G. Knepley - interpolate - Create edges and faces
18019318fe57SMatthew G. Knepley 
18029318fe57SMatthew G. Knepley   Output Parameter:
18039318fe57SMatthew G. Knepley . dm  - The DM object
18049318fe57SMatthew G. Knepley 
18059318fe57SMatthew G. Knepley   Level: beginner
18069318fe57SMatthew G. Knepley 
1807db781477SPatrick Sanan .seealso: `DMPlexCreateHexCylinderMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
18089318fe57SMatthew G. Knepley @*/
18099318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateWedgeCylinderMesh(MPI_Comm comm, PetscInt n, PetscBool interpolate, DM *dm)
18109318fe57SMatthew G. Knepley {
18119318fe57SMatthew G. Knepley   PetscFunctionBegin;
18129318fe57SMatthew G. Knepley   PetscValidPointer(dm, 4);
18139566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
18149566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
18159566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateWedgeCylinderMesh_Internal(*dm, n, interpolate));
181624119c2aSMatthew G. Knepley   PetscFunctionReturn(0);
181724119c2aSMatthew G. Knepley }
181824119c2aSMatthew G. Knepley 
18199fbee547SJacob Faibussowitsch static inline PetscReal DiffNormReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
182065a81367SMatthew G. Knepley {
182165a81367SMatthew G. Knepley   PetscReal prod = 0.0;
182265a81367SMatthew G. Knepley   PetscInt  i;
182365a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += PetscSqr(x[i] - y[i]);
182465a81367SMatthew G. Knepley   return PetscSqrtReal(prod);
182565a81367SMatthew G. Knepley }
18269fbee547SJacob Faibussowitsch static inline PetscReal DotReal(PetscInt dim, const PetscReal x[], const PetscReal y[])
182765a81367SMatthew G. Knepley {
182865a81367SMatthew G. Knepley   PetscReal prod = 0.0;
182965a81367SMatthew G. Knepley   PetscInt  i;
183065a81367SMatthew G. Knepley   for (i = 0; i < dim; ++i) prod += x[i]*y[i];
183165a81367SMatthew G. Knepley   return prod;
183265a81367SMatthew G. Knepley }
183365a81367SMatthew G. Knepley 
183451a74b61SMatthew G. Knepley /* The first constant is the sphere radius */
183551a74b61SMatthew G. Knepley static void snapToSphere(PetscInt dim, PetscInt Nf, PetscInt NfAux,
183651a74b61SMatthew G. Knepley                          const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
183751a74b61SMatthew G. Knepley                          const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
183851a74b61SMatthew G. Knepley                          PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
183951a74b61SMatthew G. Knepley {
184051a74b61SMatthew G. Knepley   PetscReal r = PetscRealPart(constants[0]);
184151a74b61SMatthew G. Knepley   PetscReal norm2 = 0.0, fac;
184251a74b61SMatthew G. Knepley   PetscInt  n = uOff[1] - uOff[0], d;
184351a74b61SMatthew G. Knepley 
184451a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) norm2 += PetscSqr(PetscRealPart(u[d]));
184551a74b61SMatthew G. Knepley   fac = r/PetscSqrtReal(norm2);
184651a74b61SMatthew G. Knepley   for (d = 0; d < n; ++d) f0[d] = u[d]*fac;
184751a74b61SMatthew G. Knepley }
184851a74b61SMatthew G. Knepley 
18499318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateSphereMesh_Internal(DM dm, PetscInt dim, PetscBool simplex, PetscReal R)
18502829fed8SMatthew G. Knepley {
185165a81367SMatthew G. Knepley   const PetscInt  embedDim = dim+1;
185265a81367SMatthew G. Knepley   PetscSection    coordSection;
185365a81367SMatthew G. Knepley   Vec             coordinates;
185465a81367SMatthew G. Knepley   PetscScalar    *coords;
185565a81367SMatthew G. Knepley   PetscReal      *coordsIn;
185665a81367SMatthew G. Knepley   PetscInt        numCells, numEdges, numVerts, firstVertex, v, firstEdge, coordSize, d, c, e;
185765a81367SMatthew G. Knepley   PetscMPIInt     rank;
185865a81367SMatthew G. Knepley 
185965a81367SMatthew G. Knepley   PetscFunctionBegin;
18609318fe57SMatthew G. Knepley   PetscValidLogicalCollectiveBool(dm, simplex, 3);
18619566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, dim));
18629566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, dim+1));
18639566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank));
186465a81367SMatthew G. Knepley   switch (dim) {
186565a81367SMatthew G. Knepley   case 2:
186665a81367SMatthew G. Knepley     if (simplex) {
186751a74b61SMatthew G. Knepley       const PetscReal radius    = PetscSqrtReal(1 + PETSC_PHI*PETSC_PHI)/(1.0 + PETSC_PHI);
186851a74b61SMatthew G. Knepley       const PetscReal edgeLen   = 2.0/(1.0 + PETSC_PHI) * (R/radius);
186965a81367SMatthew G. Knepley       const PetscInt  degree    = 5;
187051a74b61SMatthew G. Knepley       PetscReal       vertex[3] = {0.0, 1.0/(1.0 + PETSC_PHI), PETSC_PHI/(1.0 + PETSC_PHI)};
187165a81367SMatthew G. Knepley       PetscInt        s[3]      = {1, 1, 1};
187265a81367SMatthew G. Knepley       PetscInt        cone[3];
187365a81367SMatthew G. Knepley       PetscInt       *graph, p, i, j, k;
187465a81367SMatthew G. Knepley 
187551a74b61SMatthew G. Knepley       vertex[0] *= R/radius; vertex[1] *= R/radius; vertex[2] *= R/radius;
1876dd400576SPatrick Sanan       numCells    = rank == 0 ? 20 : 0;
1877dd400576SPatrick Sanan       numVerts    = rank == 0 ? 12 : 0;
187865a81367SMatthew G. Knepley       firstVertex = numCells;
187951a74b61SMatthew G. Knepley       /* Use icosahedron, which for a R-sphere has coordinates which are all cyclic permutations of
188065a81367SMatthew G. Knepley 
188165a81367SMatthew G. Knepley            (0, \pm 1/\phi+1, \pm \phi/\phi+1)
188265a81367SMatthew G. Knepley 
188365a81367SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
188451a74b61SMatthew G. Knepley          length is then given by 2/(1+\phi) = 2 * 0.38197 = 0.76393.
188565a81367SMatthew G. Knepley       */
188665a81367SMatthew G. Knepley       /* Construct vertices */
18879566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
1888dd400576SPatrick Sanan       if (rank == 0) {
188965a81367SMatthew G. Knepley         for (p = 0, i = 0; p < embedDim; ++p) {
189065a81367SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
189165a81367SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
189265a81367SMatthew G. Knepley               for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[(d+p)%embedDim]*vertex[(d+p)%embedDim];
189365a81367SMatthew G. Knepley               ++i;
189465a81367SMatthew G. Knepley             }
189565a81367SMatthew G. Knepley           }
189665a81367SMatthew G. Knepley         }
189745da822fSValeria Barra       }
189865a81367SMatthew G. Knepley       /* Construct graph */
18999566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * numVerts, &graph));
190065a81367SMatthew G. Knepley       for (i = 0; i < numVerts; ++i) {
190165a81367SMatthew G. Knepley         for (j = 0, k = 0; j < numVerts; ++j) {
190265a81367SMatthew G. Knepley           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i*embedDim], &coordsIn[j*embedDim]) - edgeLen) < PETSC_SMALL) {graph[i*numVerts+j] = 1; ++k;}
190365a81367SMatthew G. Knepley         }
190463a3b9bcSJacob Faibussowitsch         PetscCheck(k == degree,PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid icosahedron, vertex %" PetscInt_FMT " degree %" PetscInt_FMT " != %" PetscInt_FMT, i, k, degree);
190565a81367SMatthew G. Knepley       }
190665a81367SMatthew G. Knepley       /* Build Topology */
19079566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells+numVerts));
190865a81367SMatthew G. Knepley       for (c = 0; c < numCells; c++) {
19099566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(dm, c, embedDim));
191065a81367SMatthew G. Knepley       }
19119566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
191265a81367SMatthew G. Knepley       /* Cells */
191365a81367SMatthew G. Knepley       for (i = 0, c = 0; i < numVerts; ++i) {
191465a81367SMatthew G. Knepley         for (j = 0; j < i; ++j) {
191565a81367SMatthew G. Knepley           for (k = 0; k < j; ++k) {
191665a81367SMatthew G. Knepley             if (graph[i*numVerts+j] && graph[j*numVerts+k] && graph[k*numVerts+i]) {
191765a81367SMatthew G. Knepley               cone[0] = firstVertex+i; cone[1] = firstVertex+j; cone[2] = firstVertex+k;
191865a81367SMatthew G. Knepley               /* Check orientation */
191965a81367SMatthew G. Knepley               {
192065a81367SMatthew G. Knepley                 const PetscInt epsilon[3][3][3] = {{{0, 0, 0}, {0, 0, 1}, {0, -1, 0}}, {{0, 0, -1}, {0, 0, 0}, {1, 0, 0}}, {{0, 1, 0}, {-1, 0, 0}, {0, 0, 0}}};
192165a81367SMatthew G. Knepley                 PetscReal normal[3];
192265a81367SMatthew G. Knepley                 PetscInt  e, f;
192365a81367SMatthew G. Knepley 
192465a81367SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) {
192565a81367SMatthew G. Knepley                   normal[d] = 0.0;
192665a81367SMatthew G. Knepley                   for (e = 0; e < embedDim; ++e) {
192765a81367SMatthew G. Knepley                     for (f = 0; f < embedDim; ++f) {
192865a81367SMatthew G. Knepley                       normal[d] += epsilon[d][e][f]*(coordsIn[j*embedDim+e] - coordsIn[i*embedDim+e])*(coordsIn[k*embedDim+f] - coordsIn[i*embedDim+f]);
192965a81367SMatthew G. Knepley                     }
193065a81367SMatthew G. Knepley                   }
193165a81367SMatthew G. Knepley                 }
193265a81367SMatthew G. Knepley                 if (DotReal(embedDim, normal, &coordsIn[i*embedDim]) < 0) {PetscInt tmp = cone[1]; cone[1] = cone[2]; cone[2] = tmp;}
193365a81367SMatthew G. Knepley               }
19349566063dSJacob Faibussowitsch               PetscCall(DMPlexSetCone(dm, c++, cone));
193565a81367SMatthew G. Knepley             }
193665a81367SMatthew G. Knepley           }
193765a81367SMatthew G. Knepley         }
193865a81367SMatthew G. Knepley       }
19399566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
19409566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
19419566063dSJacob Faibussowitsch       PetscCall(PetscFree(graph));
194265a81367SMatthew G. Knepley     } else {
19432829fed8SMatthew G. Knepley       /*
19442829fed8SMatthew G. Knepley         12-21--13
19452829fed8SMatthew G. Knepley          |     |
19462829fed8SMatthew G. Knepley         25  4  24
19472829fed8SMatthew G. Knepley          |     |
19482829fed8SMatthew G. Knepley   12-25--9-16--8-24--13
19492829fed8SMatthew G. Knepley    |     |     |     |
19502829fed8SMatthew G. Knepley   23  5 17  0 15  3  22
19512829fed8SMatthew G. Knepley    |     |     |     |
19522829fed8SMatthew G. Knepley   10-20--6-14--7-19--11
19532829fed8SMatthew G. Knepley          |     |
19542829fed8SMatthew G. Knepley         20  1  19
19552829fed8SMatthew G. Knepley          |     |
19562829fed8SMatthew G. Knepley         10-18--11
19572829fed8SMatthew G. Knepley          |     |
19582829fed8SMatthew G. Knepley         23  2  22
19592829fed8SMatthew G. Knepley          |     |
19602829fed8SMatthew G. Knepley         12-21--13
19612829fed8SMatthew G. Knepley        */
19622829fed8SMatthew G. Knepley       PetscInt cone[4], ornt[4];
19632829fed8SMatthew G. Knepley 
1964dd400576SPatrick Sanan       numCells    = rank == 0 ?  6 : 0;
1965dd400576SPatrick Sanan       numEdges    = rank == 0 ? 12 : 0;
1966dd400576SPatrick Sanan       numVerts    = rank == 0 ?  8 : 0;
196765a81367SMatthew G. Knepley       firstVertex = numCells;
196865a81367SMatthew G. Knepley       firstEdge   = numCells + numVerts;
19692829fed8SMatthew G. Knepley       /* Build Topology */
19709566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells+numEdges+numVerts));
19712829fed8SMatthew G. Knepley       for (c = 0; c < numCells; c++) {
19729566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(dm, c, 4));
19732829fed8SMatthew G. Knepley       }
19742829fed8SMatthew G. Knepley       for (e = firstEdge; e < firstEdge+numEdges; ++e) {
19759566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(dm, e, 2));
19762829fed8SMatthew G. Knepley       }
19779566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
1978dd400576SPatrick Sanan       if (rank == 0) {
19792829fed8SMatthew G. Knepley         /* Cell 0 */
19802829fed8SMatthew G. Knepley         cone[0] = 14; cone[1] = 15; cone[2] = 16; cone[3] = 17;
19819566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 0, cone));
19822829fed8SMatthew G. Knepley         ornt[0] = 0; ornt[1] = 0; ornt[2] = 0; ornt[3] = 0;
19839566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 0, ornt));
19842829fed8SMatthew G. Knepley         /* Cell 1 */
19852829fed8SMatthew G. Knepley         cone[0] = 18; cone[1] = 19; cone[2] = 14; cone[3] = 20;
19869566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 1, cone));
1987b5a892a1SMatthew G. Knepley         ornt[0] = 0; ornt[1] = 0; ornt[2] = -1; ornt[3] = 0;
19889566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 1, ornt));
19892829fed8SMatthew G. Knepley         /* Cell 2 */
19902829fed8SMatthew G. Knepley         cone[0] = 21; cone[1] = 22; cone[2] = 18; cone[3] = 23;
19919566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 2, cone));
1992b5a892a1SMatthew G. Knepley         ornt[0] = 0; ornt[1] = 0; ornt[2] = -1; ornt[3] = 0;
19939566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 2, ornt));
19942829fed8SMatthew G. Knepley         /* Cell 3 */
19952829fed8SMatthew G. Knepley         cone[0] = 19; cone[1] = 22; cone[2] = 24; cone[3] = 15;
19969566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 3, cone));
1997b5a892a1SMatthew G. Knepley         ornt[0] = -1; ornt[1] = -1; ornt[2] = 0; ornt[3] = -1;
19989566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 3, ornt));
19992829fed8SMatthew G. Knepley         /* Cell 4 */
20002829fed8SMatthew G. Knepley         cone[0] = 16; cone[1] = 24; cone[2] = 21; cone[3] = 25;
20019566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 4, cone));
2002b5a892a1SMatthew G. Knepley         ornt[0] = -1; ornt[1] = -1; ornt[2] = -1; ornt[3] = 0;
20039566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 4, ornt));
20042829fed8SMatthew G. Knepley         /* Cell 5 */
20052829fed8SMatthew G. Knepley         cone[0] = 20; cone[1] = 17; cone[2] = 25; cone[3] = 23;
20069566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 5, cone));
2007b5a892a1SMatthew G. Knepley         ornt[0] = -1; ornt[1] = -1; ornt[2] = -1; ornt[3] = -1;
20089566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeOrientation(dm, 5, ornt));
20092829fed8SMatthew G. Knepley         /* Edges */
20102829fed8SMatthew G. Knepley         cone[0] =  6; cone[1] =  7;
20119566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 14, cone));
20122829fed8SMatthew G. Knepley         cone[0] =  7; cone[1] =  8;
20139566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 15, cone));
20142829fed8SMatthew G. Knepley         cone[0] =  8; cone[1] =  9;
20159566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 16, cone));
20162829fed8SMatthew G. Knepley         cone[0] =  9; cone[1] =  6;
20179566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 17, cone));
20182829fed8SMatthew G. Knepley         cone[0] = 10; cone[1] = 11;
20199566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 18, cone));
20202829fed8SMatthew G. Knepley         cone[0] = 11; cone[1] =  7;
20219566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 19, cone));
20222829fed8SMatthew G. Knepley         cone[0] =  6; cone[1] = 10;
20239566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 20, cone));
20242829fed8SMatthew G. Knepley         cone[0] = 12; cone[1] = 13;
20259566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 21, cone));
20262829fed8SMatthew G. Knepley         cone[0] = 13; cone[1] = 11;
20279566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 22, cone));
20282829fed8SMatthew G. Knepley         cone[0] = 10; cone[1] = 12;
20299566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 23, cone));
20302829fed8SMatthew G. Knepley         cone[0] = 13; cone[1] =  8;
20319566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 24, cone));
20322829fed8SMatthew G. Knepley         cone[0] = 12; cone[1] =  9;
20339566063dSJacob Faibussowitsch         PetscCall(DMPlexSetCone(dm, 25, cone));
203445da822fSValeria Barra       }
20359566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
20369566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
20372829fed8SMatthew G. Knepley       /* Build coordinates */
20389566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
2039dd400576SPatrick Sanan       if (rank == 0) {
204051a74b61SMatthew G. Knepley         coordsIn[0*embedDim+0] = -R; coordsIn[0*embedDim+1] =  R; coordsIn[0*embedDim+2] = -R;
204151a74b61SMatthew G. Knepley         coordsIn[1*embedDim+0] =  R; coordsIn[1*embedDim+1] =  R; coordsIn[1*embedDim+2] = -R;
204251a74b61SMatthew G. Knepley         coordsIn[2*embedDim+0] =  R; coordsIn[2*embedDim+1] = -R; coordsIn[2*embedDim+2] = -R;
204351a74b61SMatthew G. Knepley         coordsIn[3*embedDim+0] = -R; coordsIn[3*embedDim+1] = -R; coordsIn[3*embedDim+2] = -R;
204451a74b61SMatthew G. Knepley         coordsIn[4*embedDim+0] = -R; coordsIn[4*embedDim+1] =  R; coordsIn[4*embedDim+2] =  R;
204551a74b61SMatthew G. Knepley         coordsIn[5*embedDim+0] =  R; coordsIn[5*embedDim+1] =  R; coordsIn[5*embedDim+2] =  R;
204651a74b61SMatthew G. Knepley         coordsIn[6*embedDim+0] = -R; coordsIn[6*embedDim+1] = -R; coordsIn[6*embedDim+2] =  R;
204751a74b61SMatthew G. Knepley         coordsIn[7*embedDim+0] =  R; coordsIn[7*embedDim+1] = -R; coordsIn[7*embedDim+2] =  R;
204865a81367SMatthew G. Knepley       }
204945da822fSValeria Barra     }
205065a81367SMatthew G. Knepley     break;
205165a81367SMatthew G. Knepley   case 3:
2052116ded15SMatthew G. Knepley     if (simplex) {
2053116ded15SMatthew G. Knepley       const PetscReal edgeLen         = 1.0/PETSC_PHI;
205451a74b61SMatthew G. Knepley       PetscReal       vertexA[4]      = {0.5, 0.5, 0.5, 0.5};
205551a74b61SMatthew G. Knepley       PetscReal       vertexB[4]      = {1.0, 0.0, 0.0, 0.0};
205651a74b61SMatthew G. Knepley       PetscReal       vertexC[4]      = {0.5, 0.5*PETSC_PHI, 0.5/PETSC_PHI, 0.0};
2057116ded15SMatthew G. Knepley       const PetscInt  degree          = 12;
2058116ded15SMatthew G. Knepley       PetscInt        s[4]            = {1, 1, 1};
2059116ded15SMatthew G. Knepley       PetscInt        evenPerm[12][4] = {{0, 1, 2, 3}, {0, 2, 3, 1}, {0, 3, 1, 2}, {1, 0, 3, 2}, {1, 2, 0, 3}, {1, 3, 2, 0},
2060116ded15SMatthew G. Knepley                                          {2, 0, 1, 3}, {2, 1, 3, 0}, {2, 3, 0, 1}, {3, 0, 2, 1}, {3, 1, 0, 2}, {3, 2, 1, 0}};
2061116ded15SMatthew G. Knepley       PetscInt        cone[4];
2062116ded15SMatthew G. Knepley       PetscInt       *graph, p, i, j, k, l;
2063116ded15SMatthew G. Knepley 
206451a74b61SMatthew G. Knepley       vertexA[0] *= R; vertexA[1] *= R; vertexA[2] *= R; vertexA[3] *= R;
206551a74b61SMatthew G. Knepley       vertexB[0] *= R; vertexB[1] *= R; vertexB[2] *= R; vertexB[3] *= R;
206651a74b61SMatthew G. Knepley       vertexC[0] *= R; vertexC[1] *= R; vertexC[2] *= R; vertexC[3] *= R;
2067dd400576SPatrick Sanan       numCells    = rank == 0 ? 600 : 0;
2068dd400576SPatrick Sanan       numVerts    = rank == 0 ? 120 : 0;
2069116ded15SMatthew G. Knepley       firstVertex = numCells;
2070116ded15SMatthew G. Knepley       /* Use the 600-cell, which for a unit sphere has coordinates which are
2071116ded15SMatthew G. Knepley 
2072116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm 1,    \pm 1, \pm 1)                          16
2073116ded15SMatthew G. Knepley                (\pm 1,    0,       0,      0)  all cyclic permutations   8
2074116ded15SMatthew G. Knepley            1/2 (\pm 1, \pm phi, \pm 1/phi, 0)  all even permutations    96
2075116ded15SMatthew G. Knepley 
2076116ded15SMatthew G. Knepley          where \phi^2 - \phi - 1 = 0, meaning \phi is the golden ratio \frac{1 + \sqrt{5}}{2}. The edge
20776333ae4fSvaleriabarra          length is then given by 1/\phi = 0.61803.
2078116ded15SMatthew G. Knepley 
2079116ded15SMatthew G. Knepley          http://buzzard.pugetsound.edu/sage-practice/ch03s03.html
2080116ded15SMatthew G. Knepley          http://mathworld.wolfram.com/600-Cell.html
2081116ded15SMatthew G. Knepley       */
2082116ded15SMatthew G. Knepley       /* Construct vertices */
20839566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * embedDim, &coordsIn));
2084116ded15SMatthew G. Knepley       i    = 0;
2085dd400576SPatrick Sanan       if (rank == 0) {
2086116ded15SMatthew G. Knepley         for (s[0] = -1; s[0] < 2; s[0] += 2) {
2087116ded15SMatthew G. Knepley           for (s[1] = -1; s[1] < 2; s[1] += 2) {
2088116ded15SMatthew G. Knepley             for (s[2] = -1; s[2] < 2; s[2] += 2) {
2089116ded15SMatthew G. Knepley               for (s[3] = -1; s[3] < 2; s[3] += 2) {
2090116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[d]*vertexA[d];
2091116ded15SMatthew G. Knepley                 ++i;
2092116ded15SMatthew G. Knepley               }
2093116ded15SMatthew G. Knepley             }
2094116ded15SMatthew G. Knepley           }
2095116ded15SMatthew G. Knepley         }
2096116ded15SMatthew G. Knepley         for (p = 0; p < embedDim; ++p) {
2097116ded15SMatthew G. Knepley           s[1] = s[2] = s[3] = 1;
2098116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
2099116ded15SMatthew G. Knepley             for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[(d+p)%embedDim]*vertexB[(d+p)%embedDim];
2100116ded15SMatthew G. Knepley             ++i;
2101116ded15SMatthew G. Knepley           }
2102116ded15SMatthew G. Knepley         }
2103116ded15SMatthew G. Knepley         for (p = 0; p < 12; ++p) {
2104116ded15SMatthew G. Knepley           s[3] = 1;
2105116ded15SMatthew G. Knepley           for (s[0] = -1; s[0] < 2; s[0] += 2) {
2106116ded15SMatthew G. Knepley             for (s[1] = -1; s[1] < 2; s[1] += 2) {
2107116ded15SMatthew G. Knepley               for (s[2] = -1; s[2] < 2; s[2] += 2) {
2108116ded15SMatthew G. Knepley                 for (d = 0; d < embedDim; ++d) coordsIn[i*embedDim+d] = s[evenPerm[p][d]]*vertexC[evenPerm[p][d]];
2109116ded15SMatthew G. Knepley                 ++i;
2110116ded15SMatthew G. Knepley               }
2111116ded15SMatthew G. Knepley             }
2112116ded15SMatthew G. Knepley           }
2113116ded15SMatthew G. Knepley         }
211445da822fSValeria Barra       }
211563a3b9bcSJacob Faibussowitsch       PetscCheck(i == numVerts,PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertices %" PetscInt_FMT " != %" PetscInt_FMT, i, numVerts);
2116116ded15SMatthew G. Knepley       /* Construct graph */
21179566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numVerts * numVerts, &graph));
2118116ded15SMatthew G. Knepley       for (i = 0; i < numVerts; ++i) {
2119116ded15SMatthew G. Knepley         for (j = 0, k = 0; j < numVerts; ++j) {
2120116ded15SMatthew G. Knepley           if (PetscAbsReal(DiffNormReal(embedDim, &coordsIn[i*embedDim], &coordsIn[j*embedDim]) - edgeLen) < PETSC_SMALL) {graph[i*numVerts+j] = 1; ++k;}
2121116ded15SMatthew G. Knepley         }
212263a3b9bcSJacob Faibussowitsch         PetscCheck(k == degree,PetscObjectComm((PetscObject) dm), PETSC_ERR_PLIB, "Invalid 600-cell, vertex %" PetscInt_FMT " degree %" PetscInt_FMT " != %" PetscInt_FMT, i, k, degree);
2123116ded15SMatthew G. Knepley       }
2124116ded15SMatthew G. Knepley       /* Build Topology */
21259566063dSJacob Faibussowitsch       PetscCall(DMPlexSetChart(dm, 0, numCells+numVerts));
2126116ded15SMatthew G. Knepley       for (c = 0; c < numCells; c++) {
21279566063dSJacob Faibussowitsch         PetscCall(DMPlexSetConeSize(dm, c, embedDim));
2128116ded15SMatthew G. Knepley       }
21299566063dSJacob Faibussowitsch       PetscCall(DMSetUp(dm)); /* Allocate space for cones */
2130116ded15SMatthew G. Knepley       /* Cells */
2131dd400576SPatrick Sanan       if (rank == 0) {
2132116ded15SMatthew G. Knepley         for (i = 0, c = 0; i < numVerts; ++i) {
2133116ded15SMatthew G. Knepley           for (j = 0; j < i; ++j) {
2134116ded15SMatthew G. Knepley             for (k = 0; k < j; ++k) {
2135116ded15SMatthew G. Knepley               for (l = 0; l < k; ++l) {
2136116ded15SMatthew G. Knepley                 if (graph[i*numVerts+j] && graph[j*numVerts+k] && graph[k*numVerts+i] &&
2137116ded15SMatthew G. Knepley                     graph[l*numVerts+i] && graph[l*numVerts+j] && graph[l*numVerts+k]) {
2138116ded15SMatthew G. Knepley                   cone[0] = firstVertex+i; cone[1] = firstVertex+j; cone[2] = firstVertex+k; cone[3] = firstVertex+l;
2139116ded15SMatthew G. Knepley                   /* Check orientation: https://ef.gy/linear-algebra:normal-vectors-in-higher-dimensional-spaces */
2140116ded15SMatthew G. Knepley                   {
2141116ded15SMatthew G. Knepley                     const PetscInt epsilon[4][4][4][4] = {{{{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
2142116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  1}, { 0,  0, -1, 0}},
2143116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  0, -1}, { 0,  0, 0,  0}, { 0,  1,  0, 0}},
2144116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  1,  0}, { 0, -1, 0,  0}, { 0,  0,  0, 0}}},
2145116ded15SMatthew G. Knepley 
2146116ded15SMatthew G. Knepley                                                           {{{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0, -1}, { 0,  0,  1, 0}},
2147116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
2148116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  1}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, {-1,  0,  0, 0}},
2149116ded15SMatthew G. Knepley                                                            {{0,  0, -1,  0}, { 0, 0,  0,  0}, { 1,  0, 0,  0}, { 0,  0,  0, 0}}},
2150116ded15SMatthew G. Knepley 
2151116ded15SMatthew G. Knepley                                                           {{{0,  0,  0,  0}, { 0, 0,  0,  1}, { 0,  0, 0,  0}, { 0, -1,  0, 0}},
2152116ded15SMatthew G. Knepley                                                            {{0,  0,  0, -1}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 1,  0,  0, 0}},
2153116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
2154116ded15SMatthew G. Knepley                                                            {{0,  1,  0,  0}, {-1, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}}},
2155116ded15SMatthew G. Knepley 
2156116ded15SMatthew G. Knepley                                                           {{{0,  0,  0,  0}, { 0, 0, -1,  0}, { 0,  1, 0,  0}, { 0,  0,  0, 0}},
2157116ded15SMatthew G. Knepley                                                            {{0,  0,  1,  0}, { 0, 0,  0,  0}, {-1,  0, 0,  0}, { 0,  0,  0, 0}},
2158116ded15SMatthew G. Knepley                                                            {{0, -1,  0,  0}, { 1, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}},
2159116ded15SMatthew G. Knepley                                                            {{0,  0,  0,  0}, { 0, 0,  0,  0}, { 0,  0, 0,  0}, { 0,  0,  0, 0}}}};
2160116ded15SMatthew G. Knepley                     PetscReal normal[4];
2161116ded15SMatthew G. Knepley                     PetscInt  e, f, g;
2162116ded15SMatthew G. Knepley 
2163116ded15SMatthew G. Knepley                     for (d = 0; d < embedDim; ++d) {
2164116ded15SMatthew G. Knepley                       normal[d] = 0.0;
2165116ded15SMatthew G. Knepley                       for (e = 0; e < embedDim; ++e) {
2166116ded15SMatthew G. Knepley                         for (f = 0; f < embedDim; ++f) {
2167116ded15SMatthew G. Knepley                           for (g = 0; g < embedDim; ++g) {
2168116ded15SMatthew 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]);
2169116ded15SMatthew G. Knepley                           }
2170116ded15SMatthew G. Knepley                         }
2171116ded15SMatthew G. Knepley                       }
2172116ded15SMatthew G. Knepley                     }
2173116ded15SMatthew G. Knepley                     if (DotReal(embedDim, normal, &coordsIn[i*embedDim]) < 0) {PetscInt tmp = cone[1]; cone[1] = cone[2]; cone[2] = tmp;}
2174116ded15SMatthew G. Knepley                   }
21759566063dSJacob Faibussowitsch                   PetscCall(DMPlexSetCone(dm, c++, cone));
2176116ded15SMatthew G. Knepley                 }
2177116ded15SMatthew G. Knepley               }
2178116ded15SMatthew G. Knepley             }
2179116ded15SMatthew G. Knepley           }
2180116ded15SMatthew G. Knepley         }
218145da822fSValeria Barra       }
21829566063dSJacob Faibussowitsch       PetscCall(DMPlexSymmetrize(dm));
21839566063dSJacob Faibussowitsch       PetscCall(DMPlexStratify(dm));
21849566063dSJacob Faibussowitsch       PetscCall(PetscFree(graph));
2185116ded15SMatthew G. Knepley       break;
2186116ded15SMatthew G. Knepley     }
218763a3b9bcSJacob Faibussowitsch   default: SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_SUP, "Unsupported dimension for sphere: %" PetscInt_FMT, dim);
218865a81367SMatthew G. Knepley   }
218965a81367SMatthew G. Knepley   /* Create coordinates */
21909566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
21919566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
21929566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, embedDim));
21939566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex+numVerts));
21942829fed8SMatthew G. Knepley   for (v = firstVertex; v < firstVertex+numVerts; ++v) {
21959566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, embedDim));
21969566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, embedDim));
21972829fed8SMatthew G. Knepley   }
21989566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
21999566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
22009566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
22019566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, embedDim));
22029566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
22039566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
22049566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates,VECSTANDARD));
22059566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
220665a81367SMatthew G. Knepley   for (v = 0; v < numVerts; ++v) for (d = 0; d < embedDim; ++d) {coords[v*embedDim+d] = coordsIn[v*embedDim+d];}
22079566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
22089566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
22099566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
22109566063dSJacob Faibussowitsch   PetscCall(PetscFree(coordsIn));
221151a74b61SMatthew G. Knepley   {
221251a74b61SMatthew G. Knepley     DM          cdm;
221351a74b61SMatthew G. Knepley     PetscDS     cds;
22149318fe57SMatthew G. Knepley     PetscScalar c = R;
221551a74b61SMatthew G. Knepley 
22169566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCoordinateSpace(dm, 1, snapToSphere));
22179566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
22189566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
22199566063dSJacob Faibussowitsch     PetscCall(PetscDSSetConstants(cds, 1, &c));
222051a74b61SMatthew G. Knepley   }
22219318fe57SMatthew G. Knepley   /* Wait for coordinate creation before doing in-place modification */
22229566063dSJacob Faibussowitsch   if (simplex) PetscCall(DMPlexInterpolateInPlace_Internal(dm));
22239318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
22249318fe57SMatthew G. Knepley }
22259318fe57SMatthew G. Knepley 
2226b7f5c055SJed Brown typedef void (*TPSEvaluateFunc)(const PetscReal[], PetscReal*, PetscReal[], PetscReal(*)[3]);
2227b7f5c055SJed Brown 
2228b7f5c055SJed Brown /*
2229b7f5c055SJed Brown  The Schwarz P implicit surface is
2230b7f5c055SJed Brown 
2231b7f5c055SJed Brown      f(x) = cos(x0) + cos(x1) + cos(x2) = 0
2232b7f5c055SJed Brown */
2233b7f5c055SJed Brown static void TPSEvaluate_SchwarzP(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3])
2234b7f5c055SJed Brown {
2235b7f5c055SJed Brown   PetscReal c[3] = {PetscCosReal(y[0] * PETSC_PI), PetscCosReal(y[1] * PETSC_PI), PetscCosReal(y[2] * PETSC_PI)};
2236b7f5c055SJed Brown   PetscReal g[3] = {-PetscSinReal(y[0] * PETSC_PI), -PetscSinReal(y[1] * PETSC_PI), -PetscSinReal(y[2] * PETSC_PI)};
2237b7f5c055SJed Brown   f[0] = c[0] + c[1] + c[2];
2238b7f5c055SJed Brown   for (PetscInt i=0; i<3; i++) {
2239b7f5c055SJed Brown     grad[i] = PETSC_PI * g[i];
2240b7f5c055SJed Brown     for (PetscInt j=0; j<3; j++) {
2241b7f5c055SJed Brown       hess[i][j] = (i == j) ? -PetscSqr(PETSC_PI) * c[i] : 0.;
2242b7f5c055SJed Brown     }
2243b7f5c055SJed Brown   }
2244b7f5c055SJed Brown }
2245b7f5c055SJed Brown 
22464663dae6SJed Brown // u[] is a tentative normal on input. Replace with the implicit function gradient in the same direction
22474663dae6SJed Brown static PetscErrorCode TPSExtrudeNormalFunc_SchwarzP(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt r, PetscScalar u[], void *ctx) {
22484663dae6SJed Brown   for (PetscInt i=0; i<3; i++) {
22494663dae6SJed Brown     u[i] = -PETSC_PI * PetscSinReal(x[i] * PETSC_PI);
22504663dae6SJed Brown   }
22514663dae6SJed Brown   return 0;
22524663dae6SJed Brown }
22534663dae6SJed Brown 
2254b7f5c055SJed Brown /*
2255b7f5c055SJed Brown  The Gyroid implicit surface is
2256b7f5c055SJed Brown 
2257b7f5c055SJed 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)
2258b7f5c055SJed Brown 
2259b7f5c055SJed Brown */
2260b7f5c055SJed Brown static void TPSEvaluate_Gyroid(const PetscReal y[3], PetscReal *f, PetscReal grad[], PetscReal (*hess)[3])
2261b7f5c055SJed Brown {
2262b7f5c055SJed Brown   PetscReal s[3] = {PetscSinReal(PETSC_PI * y[0]), PetscSinReal(PETSC_PI * (y[1] + .5)), PetscSinReal(PETSC_PI * (y[2] + .25))};
2263b7f5c055SJed Brown   PetscReal c[3] = {PetscCosReal(PETSC_PI * y[0]), PetscCosReal(PETSC_PI * (y[1] + .5)), PetscCosReal(PETSC_PI * (y[2] + .25))};
2264b7f5c055SJed Brown   f[0] = s[0] * c[1] + s[1] * c[2] + s[2] * c[0];
2265b7f5c055SJed Brown   grad[0] = PETSC_PI * (c[0] * c[1] - s[2] * s[0]);
2266b7f5c055SJed Brown   grad[1] = PETSC_PI * (c[1] * c[2] - s[0] * s[1]);
2267b7f5c055SJed Brown   grad[2] = PETSC_PI * (c[2] * c[0] - s[1] * s[2]);
2268b7f5c055SJed Brown   hess[0][0] = -PetscSqr(PETSC_PI) * (s[0] * c[1] + s[2] * c[0]);
2269b7f5c055SJed Brown   hess[0][1] = -PetscSqr(PETSC_PI) * (c[0] * s[1]);
2270b7f5c055SJed Brown   hess[0][2] = -PetscSqr(PETSC_PI) * (c[2] * s[0]);
2271b7f5c055SJed Brown   hess[1][0] = -PetscSqr(PETSC_PI) * (s[1] * c[2] + s[0] * c[1]);
2272b7f5c055SJed Brown   hess[1][1] = -PetscSqr(PETSC_PI) * (c[1] * s[2]);
2273b7f5c055SJed Brown   hess[2][2] = -PetscSqr(PETSC_PI) * (c[0] * s[1]);
2274b7f5c055SJed Brown   hess[2][0] = -PetscSqr(PETSC_PI) * (s[2] * c[0] + s[1] * c[2]);
2275b7f5c055SJed Brown   hess[2][1] = -PetscSqr(PETSC_PI) * (c[2] * s[0]);
2276b7f5c055SJed Brown   hess[2][2] = -PetscSqr(PETSC_PI) * (c[1] * s[2]);
2277b7f5c055SJed Brown }
2278b7f5c055SJed Brown 
22794663dae6SJed Brown // u[] is a tentative normal on input. Replace with the implicit function gradient in the same direction
22804663dae6SJed Brown static PetscErrorCode TPSExtrudeNormalFunc_Gyroid(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt r, PetscScalar u[], void *ctx) {
22814663dae6SJed Brown   PetscReal s[3] = {PetscSinReal(PETSC_PI * x[0]), PetscSinReal(PETSC_PI * (x[1] + .5)), PetscSinReal(PETSC_PI * (x[2] + .25))};
22824663dae6SJed Brown   PetscReal c[3] = {PetscCosReal(PETSC_PI * x[0]), PetscCosReal(PETSC_PI * (x[1] + .5)), PetscCosReal(PETSC_PI * (x[2] + .25))};
22834663dae6SJed Brown   u[0] = PETSC_PI * (c[0] * c[1] - s[2] * s[0]);
22844663dae6SJed Brown   u[1] = PETSC_PI * (c[1] * c[2] - s[0] * s[1]);
22854663dae6SJed Brown   u[2] = PETSC_PI * (c[2] * c[0] - s[1] * s[2]);
22864663dae6SJed Brown   return 0;
22874663dae6SJed Brown }
22884663dae6SJed Brown 
2289b7f5c055SJed Brown /*
2290b7f5c055SJed Brown    We wish to solve
2291b7f5c055SJed Brown 
2292b7f5c055SJed Brown          min_y || y - x ||^2  subject to f(y) = 0
2293b7f5c055SJed Brown 
2294b7f5c055SJed Brown    Let g(y) = grad(f).  The minimization problem is equivalent to asking to satisfy
2295b7f5c055SJed 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
2296b7f5c055SJed Brown    tangent space and ask for both components in the tangent space to be zero.
2297b7f5c055SJed Brown 
2298b7f5c055SJed Brown    Take g to be a column vector and compute the "full QR" factorization Q R = g,
2299b7f5c055SJed Brown    where Q = I - 2 n n^T is a symmetric orthogonal matrix.
2300b7f5c055SJed Brown    The first column of Q is parallel to g so the remaining two columns span the null space.
2301b7f5c055SJed Brown    Let Qn = Q[:,1:] be those remaining columns.  Then Qn Qn^T is an orthogonal projector into the tangent space.
2302b7f5c055SJed Brown    Since Q is symmetric, this is equivalent to multipyling by Q and taking the last two entries.
2303b7f5c055SJed Brown    In total, we have a system of 3 equations in 3 unknowns:
2304b7f5c055SJed Brown 
2305b7f5c055SJed Brown      f(y) = 0                       1 equation
2306b7f5c055SJed Brown      Qn^T (y - x) = 0               2 equations
2307b7f5c055SJed Brown 
2308b7f5c055SJed Brown    Here, we compute the residual and Jacobian of this system.
2309b7f5c055SJed Brown */
2310b7f5c055SJed Brown static void TPSNearestPointResJac(TPSEvaluateFunc feval, const PetscScalar x[], const PetscScalar y[], PetscScalar res[], PetscScalar J[])
2311b7f5c055SJed Brown {
2312b7f5c055SJed Brown   PetscReal yreal[3] = {PetscRealPart(y[0]), PetscRealPart(y[1]), PetscRealPart(y[2])};
2313b7f5c055SJed Brown   PetscReal d[3] = {PetscRealPart(y[0] - x[0]), PetscRealPart(y[1] - x[1]), PetscRealPart(y[2] - x[2])};
23142f0490c0SSatish Balay   PetscReal f, grad[3], n[3], norm, norm_y[3], nd, nd_y[3], sign;
23152f0490c0SSatish Balay   PetscReal n_y[3][3] = {{0,0,0},{0,0,0},{0,0,0}};
2316b7f5c055SJed Brown 
2317b7f5c055SJed Brown   feval(yreal, &f, grad, n_y);
2318b7f5c055SJed Brown 
2319b7f5c055SJed Brown   for (PetscInt i=0; i<3; i++) n[i] = grad[i];
2320b7f5c055SJed Brown   norm = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2]));
2321b7f5c055SJed Brown   for (PetscInt i=0; i<3; i++) {
2322b7f5c055SJed Brown     norm_y[i] = 1. / norm * n[i] * n_y[i][i];
2323b7f5c055SJed Brown   }
2324b7f5c055SJed Brown 
2325b7f5c055SJed Brown   // Define the Householder reflector
2326b7f5c055SJed Brown   sign = n[0] >= 0 ? 1. : -1.;
2327b7f5c055SJed Brown   n[0] += norm * sign;
2328b7f5c055SJed Brown   for (PetscInt i=0; i<3; i++) n_y[0][i] += norm_y[i] * sign;
2329b7f5c055SJed Brown 
2330b7f5c055SJed Brown   norm = PetscSqrtReal(PetscSqr(n[0]) + PetscSqr(n[1]) + PetscSqr(n[2]));
2331b7f5c055SJed Brown   norm_y[0] = 1. / norm * (n[0] * n_y[0][0]);
2332b7f5c055SJed Brown   norm_y[1] = 1. / norm * (n[0] * n_y[0][1] + n[1] * n_y[1][1]);
2333b7f5c055SJed Brown   norm_y[2] = 1. / norm * (n[0] * n_y[0][2] + n[2] * n_y[2][2]);
2334b7f5c055SJed Brown 
2335b7f5c055SJed Brown   for (PetscInt i=0; i<3; i++) {
2336b7f5c055SJed Brown     n[i] /= norm;
2337b7f5c055SJed Brown     for (PetscInt j=0; j<3; j++) {
2338b7f5c055SJed Brown       // note that n[i] is n_old[i]/norm when executing the code below
2339b7f5c055SJed Brown       n_y[i][j] = n_y[i][j] / norm - n[i] / norm * norm_y[j];
2340b7f5c055SJed Brown     }
2341b7f5c055SJed Brown   }
2342b7f5c055SJed Brown 
2343b7f5c055SJed Brown   nd = n[0] * d[0] + n[1] * d[1] + n[2] * d[2];
2344b7f5c055SJed 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];
2345b7f5c055SJed Brown 
2346b7f5c055SJed Brown   res[0] = f;
2347b7f5c055SJed Brown   res[1] = d[1] - 2 * n[1] * nd;
2348b7f5c055SJed Brown   res[2] = d[2] - 2 * n[2] * nd;
2349b7f5c055SJed Brown   // J[j][i] is J_{ij} (column major)
2350b7f5c055SJed Brown   for (PetscInt j=0; j<3; j++) {
2351b7f5c055SJed Brown     J[0 + j*3] = grad[j];
2352b7f5c055SJed Brown     J[1 + j*3] = (j == 1)*1. - 2 * (n_y[1][j] * nd + n[1] * nd_y[j]);
2353b7f5c055SJed Brown     J[2 + j*3] = (j == 2)*1. - 2 * (n_y[2][j] * nd + n[2] * nd_y[j]);
2354b7f5c055SJed Brown   }
2355b7f5c055SJed Brown }
2356b7f5c055SJed Brown 
2357b7f5c055SJed Brown /*
2358b7f5c055SJed Brown    Project x to the nearest point on the implicit surface using Newton's method.
2359b7f5c055SJed Brown */
2360b7f5c055SJed Brown static PetscErrorCode TPSNearestPoint(TPSEvaluateFunc feval, PetscScalar x[])
2361b7f5c055SJed Brown {
2362b7f5c055SJed Brown   PetscScalar y[3] = {x[0], x[1], x[2]}; // Initial guess
2363b7f5c055SJed Brown 
2364b7f5c055SJed Brown   PetscFunctionBegin;
2365b7f5c055SJed Brown   for (PetscInt iter=0; iter<10; iter++) {
2366b7f5c055SJed Brown     PetscScalar res[3], J[9];
2367b7f5c055SJed Brown     PetscReal resnorm;
2368b7f5c055SJed Brown     TPSNearestPointResJac(feval, x, y, res, J);
2369b7f5c055SJed Brown     resnorm = PetscSqrtReal(PetscSqr(PetscRealPart(res[0])) + PetscSqr(PetscRealPart(res[1])) + PetscSqr(PetscRealPart(res[2])));
2370b7f5c055SJed Brown     if (0) { // Turn on this monitor if you need to confirm quadratic convergence
237163a3b9bcSJacob 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])));
2372b7f5c055SJed Brown     }
2373b7f5c055SJed Brown     if (resnorm < PETSC_SMALL) break;
2374b7f5c055SJed Brown 
2375b7f5c055SJed Brown     // Take the Newton step
23769566063dSJacob Faibussowitsch     PetscCall(PetscKernel_A_gets_inverse_A_3(J, 0., PETSC_FALSE, NULL));
2377b7f5c055SJed Brown     PetscKernel_v_gets_v_minus_A_times_w_3(y, J, res);
2378b7f5c055SJed Brown   }
2379b7f5c055SJed Brown   for (PetscInt i=0; i<3; i++) x[i] = y[i];
2380b7f5c055SJed Brown   PetscFunctionReturn(0);
2381b7f5c055SJed Brown }
2382b7f5c055SJed Brown 
2383b7f5c055SJed Brown const char *const DMPlexTPSTypes[] = {"SCHWARZ_P", "GYROID", "DMPlexTPSType", "DMPLEX_TPS_", NULL};
2384b7f5c055SJed Brown 
23851436d7faSJed Brown static PetscErrorCode DMPlexCreateTPSMesh_Internal(DM dm, DMPlexTPSType tpstype, const PetscInt extent[], const DMBoundaryType periodic[], PetscBool tps_distribute, PetscInt refinements, PetscInt layers, PetscReal thickness)
2386b7f5c055SJed Brown {
2387b7f5c055SJed Brown   PetscMPIInt rank;
2388b7f5c055SJed Brown   PetscInt topoDim = 2, spaceDim = 3, numFaces = 0, numVertices = 0, numEdges = 0;
2389b7f5c055SJed Brown   PetscInt (*edges)[2] = NULL, *edgeSets = NULL;
2390b7f5c055SJed Brown   PetscInt *cells_flat = NULL;
2391b7f5c055SJed Brown   PetscReal *vtxCoords = NULL;
2392b7f5c055SJed Brown   TPSEvaluateFunc evalFunc = NULL;
23934663dae6SJed Brown   PetscSimplePointFunc normalFunc = NULL;
2394b7f5c055SJed Brown   DMLabel label;
2395b7f5c055SJed Brown 
2396b7f5c055SJed Brown   PetscFunctionBegin;
23979566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)dm), &rank));
239863a3b9bcSJacob 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);
2399b7f5c055SJed Brown   switch (tpstype) {
2400b7f5c055SJed Brown   case DMPLEX_TPS_SCHWARZ_P:
2401b7f5c055SJed 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");
2402c5853193SPierre Jolivet     if (rank == 0) {
2403b7f5c055SJed Brown       PetscInt (*cells)[6][4][4] = NULL; // [junction, junction-face, cell, conn]
2404b7f5c055SJed Brown       PetscInt Njunctions = 0, Ncuts = 0, Npipes[3], vcount;
2405b7f5c055SJed Brown       PetscReal L = 1;
2406b7f5c055SJed Brown 
2407b7f5c055SJed Brown       Npipes[0] = (extent[0] + 1) * extent[1] * extent[2];
2408b7f5c055SJed Brown       Npipes[1] = extent[0] * (extent[1] + 1) * extent[2];
2409b7f5c055SJed Brown       Npipes[2] = extent[0] * extent[1] * (extent[2] + 1);
2410b7f5c055SJed Brown       Njunctions = extent[0] * extent[1] * extent[2];
2411b7f5c055SJed Brown       Ncuts = 2 * (extent[0] * extent[1] + extent[1] * extent[2] + extent[2] * extent[0]);
2412b7f5c055SJed Brown       numVertices = 4 * (Npipes[0] + Npipes[1] + Npipes[2]) + 8 * Njunctions;
24139566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(3*numVertices, &vtxCoords));
24149566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Njunctions, &cells));
24159566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Ncuts*4, &edges));
24169566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(Ncuts*4, &edgeSets));
2417b7f5c055SJed Brown       // x-normal pipes
2418b7f5c055SJed Brown       vcount = 0;
2419b7f5c055SJed Brown       for (PetscInt i=0; i<extent[0]+1; i++) {
2420b7f5c055SJed Brown         for (PetscInt j=0; j<extent[1]; j++) {
2421b7f5c055SJed Brown           for (PetscInt k=0; k<extent[2]; k++) {
2422b7f5c055SJed Brown             for (PetscInt l=0; l<4; l++) {
2423b7f5c055SJed Brown               vtxCoords[vcount++] = (2*i - 1) * L;
2424b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * j * L + PetscCosReal((2*l + 1) * PETSC_PI / 4) * L / 2;
2425b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * k * L + PetscSinReal((2*l + 1) * PETSC_PI / 4) * L / 2;
2426b7f5c055SJed Brown             }
2427b7f5c055SJed Brown           }
2428b7f5c055SJed Brown         }
2429b7f5c055SJed Brown       }
2430b7f5c055SJed Brown       // y-normal pipes
2431b7f5c055SJed Brown       for (PetscInt i=0; i<extent[0]; i++) {
2432b7f5c055SJed Brown         for (PetscInt j=0; j<extent[1]+1; j++) {
2433b7f5c055SJed Brown           for (PetscInt k=0; k<extent[2]; k++) {
2434b7f5c055SJed Brown             for (PetscInt l=0; l<4; l++) {
2435b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * i * L + PetscSinReal((2*l + 1) * PETSC_PI / 4) * L / 2;
2436b7f5c055SJed Brown               vtxCoords[vcount++] = (2*j - 1) * L;
2437b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * k * L + PetscCosReal((2*l + 1) * PETSC_PI / 4) * L / 2;
2438b7f5c055SJed Brown             }
2439b7f5c055SJed Brown           }
2440b7f5c055SJed Brown         }
2441b7f5c055SJed Brown       }
2442b7f5c055SJed Brown       // z-normal pipes
2443b7f5c055SJed Brown       for (PetscInt i=0; i<extent[0]; i++) {
2444b7f5c055SJed Brown         for (PetscInt j=0; j<extent[1]; j++) {
2445b7f5c055SJed Brown           for (PetscInt k=0; k<extent[2]+1; k++) {
2446b7f5c055SJed Brown             for (PetscInt l=0; l<4; l++) {
2447b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * i * L + PetscCosReal((2*l + 1) * PETSC_PI / 4) * L / 2;
2448b7f5c055SJed Brown               vtxCoords[vcount++] = 2 * j * L + PetscSinReal((2*l + 1) * PETSC_PI / 4) * L / 2;
2449b7f5c055SJed Brown               vtxCoords[vcount++] = (2*k - 1) * L;
2450b7f5c055SJed Brown             }
2451b7f5c055SJed Brown           }
2452b7f5c055SJed Brown         }
2453b7f5c055SJed Brown       }
2454b7f5c055SJed Brown       // junctions
2455b7f5c055SJed Brown       for (PetscInt i=0; i<extent[0]; i++) {
2456b7f5c055SJed Brown         for (PetscInt j=0; j<extent[1]; j++) {
2457b7f5c055SJed Brown           for (PetscInt k=0; k<extent[2]; k++) {
2458b7f5c055SJed Brown             const PetscInt J = (i*extent[1] + j)*extent[2] + k, Jvoff = (Npipes[0] + Npipes[1] + Npipes[2])*4 + J*8;
2459b7f5c055SJed Brown             PetscCheck(vcount / 3 == Jvoff, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unexpected vertex count");
2460b7f5c055SJed Brown             for (PetscInt ii=0; ii<2; ii++) {
2461b7f5c055SJed Brown               for (PetscInt jj=0; jj<2; jj++) {
2462b7f5c055SJed Brown                 for (PetscInt kk=0; kk<2; kk++) {
2463b7f5c055SJed Brown                   double Ls = (1 - sqrt(2) / 4) * L;
2464b7f5c055SJed Brown                   vtxCoords[vcount++] = 2*i*L + (2*ii-1) * Ls;
2465b7f5c055SJed Brown                   vtxCoords[vcount++] = 2*j*L + (2*jj-1) * Ls;
2466b7f5c055SJed Brown                   vtxCoords[vcount++] = 2*k*L + (2*kk-1) * Ls;
2467b7f5c055SJed Brown                 }
2468b7f5c055SJed Brown               }
2469b7f5c055SJed Brown             }
2470b7f5c055SJed Brown             const PetscInt jfaces[3][2][4] = {
2471b7f5c055SJed Brown               {{3,1,0,2}, {7,5,4,6}}, // x-aligned
2472b7f5c055SJed Brown               {{5,4,0,1}, {7,6,2,3}}, // y-aligned
2473b7f5c055SJed Brown               {{6,2,0,4}, {7,3,1,5}}  // z-aligned
2474b7f5c055SJed Brown             };
2475b7f5c055SJed Brown             const PetscInt pipe_lo[3] = { // vertex numbers of pipes
2476b7f5c055SJed Brown               ((i * extent[1] + j) * extent[2] + k)*4,
2477b7f5c055SJed Brown               ((i * (extent[1] + 1) + j) * extent[2] + k + Npipes[0])*4,
2478b7f5c055SJed Brown               ((i * extent[1] + j) * (extent[2]+1) + k + Npipes[0] + Npipes[1])*4
2479b7f5c055SJed Brown             };
2480b7f5c055SJed Brown             const PetscInt pipe_hi[3] = { // vertex numbers of pipes
2481b7f5c055SJed Brown               (((i + 1) * extent[1] + j) * extent[2] + k)*4,
2482b7f5c055SJed Brown               ((i * (extent[1] + 1) + j + 1) * extent[2] + k + Npipes[0])*4,
2483b7f5c055SJed Brown               ((i * extent[1] + j) * (extent[2]+1) + k + 1 + Npipes[0] + Npipes[1])*4
2484b7f5c055SJed Brown             };
2485b7f5c055SJed Brown             for (PetscInt dir=0; dir<3; dir++) { // x,y,z
2486b7f5c055SJed Brown               const PetscInt ijk[3] = {i, j, k};
2487b7f5c055SJed Brown               for (PetscInt l=0; l<4; l++) { // rotations
2488b7f5c055SJed Brown                 cells[J][dir*2+0][l][0] = pipe_lo[dir] + l;
2489b7f5c055SJed Brown                 cells[J][dir*2+0][l][1] = Jvoff + jfaces[dir][0][l];
2490b7f5c055SJed Brown                 cells[J][dir*2+0][l][2] = Jvoff + jfaces[dir][0][(l-1+4)%4];
2491b7f5c055SJed Brown                 cells[J][dir*2+0][l][3] = pipe_lo[dir] + (l-1+4)%4;
2492b7f5c055SJed Brown                 cells[J][dir*2+1][l][0] = Jvoff + jfaces[dir][1][l];
2493b7f5c055SJed Brown                 cells[J][dir*2+1][l][1] = pipe_hi[dir] + l;
2494b7f5c055SJed Brown                 cells[J][dir*2+1][l][2] = pipe_hi[dir] + (l-1+4)%4;
2495b7f5c055SJed Brown                 cells[J][dir*2+1][l][3] = Jvoff + jfaces[dir][1][(l-1+4)%4];
2496b7f5c055SJed Brown                 if (ijk[dir] == 0) {
2497b7f5c055SJed Brown                   edges[numEdges][0] = pipe_lo[dir] + l;
2498b7f5c055SJed Brown                   edges[numEdges][1] = pipe_lo[dir] + (l+1) % 4;
2499b7f5c055SJed Brown                   edgeSets[numEdges] = dir*2 + 1;
2500b7f5c055SJed Brown                   numEdges++;
2501b7f5c055SJed Brown                 }
2502b7f5c055SJed Brown                 if (ijk[dir] + 1 == extent[dir]) {
2503b7f5c055SJed Brown                   edges[numEdges][0] = pipe_hi[dir] + l;
2504b7f5c055SJed Brown                   edges[numEdges][1] = pipe_hi[dir] + (l+1) % 4;
2505b7f5c055SJed Brown                   edgeSets[numEdges] = dir*2 + 2;
2506b7f5c055SJed Brown                   numEdges++;
2507b7f5c055SJed Brown                 }
2508b7f5c055SJed Brown               }
2509b7f5c055SJed Brown             }
2510b7f5c055SJed Brown           }
2511b7f5c055SJed Brown         }
2512b7f5c055SJed Brown       }
251363a3b9bcSJacob Faibussowitsch       PetscCheck(numEdges == Ncuts * 4, PetscObjectComm((PetscObject)dm), PETSC_ERR_PLIB, "Edge count %" PetscInt_FMT " incompatible with number of cuts %" PetscInt_FMT, numEdges, Ncuts);
2514b7f5c055SJed Brown       numFaces = 24 * Njunctions;
2515b7f5c055SJed Brown       cells_flat = cells[0][0][0];
2516b7f5c055SJed Brown     }
2517b7f5c055SJed Brown     evalFunc = TPSEvaluate_SchwarzP;
25184663dae6SJed Brown     normalFunc = TPSExtrudeNormalFunc_SchwarzP;
2519b7f5c055SJed Brown     break;
2520b7f5c055SJed Brown   case DMPLEX_TPS_GYROID:
2521c5853193SPierre Jolivet     if (rank == 0) {
2522b7f5c055SJed Brown       // This is a coarse mesh approximation of the gyroid shifted to being the zero of the level set
2523b7f5c055SJed Brown       //
2524b7f5c055SJed 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)
2525b7f5c055SJed Brown       //
2526b7f5c055SJed Brown       // on the cell [0,2]^3.
2527b7f5c055SJed Brown       //
2528b7f5c055SJed Brown       // Think about dividing that cell into four columns, and focus on the column [0,1]x[0,1]x[0,2].
2529b7f5c055SJed Brown       // If you looked at the gyroid in that column at different slices of z you would see that it kind of spins
2530b7f5c055SJed Brown       // like a boomerang:
2531b7f5c055SJed Brown       //
2532b7f5c055SJed Brown       //     z = 0          z = 1/4        z = 1/2        z = 3/4     //
2533b7f5c055SJed Brown       //     -----          -------        -------        -------     //
2534b7f5c055SJed Brown       //                                                              //
2535b7f5c055SJed Brown       //     +       +      +       +      +       +      +   \   +   //
2536b7f5c055SJed Brown       //      \                                   /            \      //
2537b7f5c055SJed Brown       //       \            `-_   _-'            /              }     //
2538b7f5c055SJed Brown       //        *-_            `-'            _-'              /      //
2539b7f5c055SJed Brown       //     +     `-+      +       +      +-'     +      +   /   +   //
2540b7f5c055SJed Brown       //                                                              //
2541b7f5c055SJed Brown       //                                                              //
2542b7f5c055SJed Brown       //     z = 1          z = 5/4        z = 3/2        z = 7/4     //
2543b7f5c055SJed Brown       //     -----          -------        -------        -------     //
2544b7f5c055SJed Brown       //                                                              //
2545b7f5c055SJed Brown       //     +-_     +      +       +      +     _-+      +   /   +   //
2546b7f5c055SJed Brown       //        `-_            _-_            _-`            /        //
2547b7f5c055SJed Brown       //           \        _-'   `-_        /              {         //
2548b7f5c055SJed Brown       //            \                       /                \        //
2549b7f5c055SJed Brown       //     +       +      +       +      +       +      +   \   +   //
2550b7f5c055SJed Brown       //
2551b7f5c055SJed Brown       //
2552b7f5c055SJed Brown       // This course mesh approximates each of these slices by two line segments,
2553b7f5c055SJed Brown       // and then connects the segments in consecutive layers with quadrilateral faces.
2554b7f5c055SJed Brown       // All of the end points of the segments are multiples of 1/4 except for the
2555b7f5c055SJed Brown       // point * in the picture for z = 0 above and the similar points in other layers.
2556b7f5c055SJed Brown       // That point is at (gamma, gamma, 0), where gamma is calculated below.
2557b7f5c055SJed Brown       //
2558b7f5c055SJed Brown       // The column  [1,2]x[1,2]x[0,2] looks the same as this column;
2559b7f5c055SJed Brown       // The columns [1,2]x[0,1]x[0,2] and [0,1]x[1,2]x[0,2] are mirror images.
2560b7f5c055SJed Brown       //
2561b7f5c055SJed Brown       // As for how this method turned into the names given to the vertices:
2562b7f5c055SJed Brown       // that was not systematic, it was just the way it worked out in my handwritten notes.
2563b7f5c055SJed Brown 
2564b7f5c055SJed Brown       PetscInt facesPerBlock = 64;
2565b7f5c055SJed Brown       PetscInt vertsPerBlock = 56;
2566b7f5c055SJed Brown       PetscInt extentPlus[3];
2567b7f5c055SJed Brown       PetscInt numBlocks, numBlocksPlus;
2568b7f5c055SJed Brown       const PetscInt A =  0,   B =  1,   C =  2,   D =  3,   E =  4,   F =  5,   G =  6,   H =  7,
2569b7f5c055SJed Brown         II =  8,   J =  9,   K = 10,   L = 11,   M = 12,   N = 13,   O = 14,   P = 15,
2570b7f5c055SJed Brown         Q = 16,   R = 17,   S = 18,   T = 19,   U = 20,   V = 21,   W = 22,   X = 23,
2571b7f5c055SJed Brown         Y = 24,   Z = 25,  Ap = 26,  Bp = 27,  Cp = 28,  Dp = 29,  Ep = 30,  Fp = 31,
2572b7f5c055SJed Brown         Gp = 32,  Hp = 33,  Ip = 34,  Jp = 35,  Kp = 36,  Lp = 37,  Mp = 38,  Np = 39,
2573b7f5c055SJed Brown         Op = 40,  Pp = 41,  Qp = 42,  Rp = 43,  Sp = 44,  Tp = 45,  Up = 46,  Vp = 47,
2574b7f5c055SJed Brown         Wp = 48,  Xp = 49,  Yp = 50,  Zp = 51,  Aq = 52,  Bq = 53,  Cq = 54,  Dq = 55;
2575b7f5c055SJed Brown       const PetscInt pattern[64][4] =
2576b7f5c055SJed Brown         { /* face to vertex within the coarse discretization of a single gyroid block */
2577b7f5c055SJed Brown           /* layer 0 */
2578b7f5c055SJed Brown           {A,C,K,G},{C,B,II,K},{D,A,H,L},{B+56*1,D,L,J},{E,B+56*1,J,N},{A+56*2,E,N,H+56*2},{F,A+56*2,G+56*2,M},{B,F,M,II},
2579b7f5c055SJed Brown           /* layer 1 */
2580b7f5c055SJed Brown           {G,K,Q,O},{K,II,P,Q},{L,H,O+56*1,R},{J,L,R,P},{N,J,P,S},{H+56*2,N,S,O+56*3},{M,G+56*2,O+56*2,T},{II,M,T,P},
2581b7f5c055SJed Brown           /* layer 2 */
2582b7f5c055SJed Brown           {O,Q,Y,U},{Q,P,W,Y},{R,O+56*1,U+56*1,Ap},{P,R,Ap,W},{S,P,X,Bp},{O+56*3,S,Bp,V+56*1},{T,O+56*2,V,Z},{P,T,Z,X},
2583b7f5c055SJed Brown           /* layer 3 */
2584b7f5c055SJed Brown           {U,Y,Ep,Dp},{Y,W,Cp,Ep},{Ap,U+56*1,Dp+56*1,Gp},{W,Ap,Gp,Cp},{Bp,X,Cp+56*2,Fp},{V+56*1,Bp,Fp,Dp+56*1},{Z,V,Dp,Hp},{X,Z,Hp,Cp+56*2},
2585b7f5c055SJed Brown           /* layer 4 */
2586b7f5c055SJed Brown           {Dp,Ep,Mp,Kp},{Ep,Cp,Ip,Mp},{Gp,Dp+56*1,Lp,Np},{Cp,Gp,Np,Jp},{Fp,Cp+56*2,Jp+56*2,Pp},{Dp+56*1,Fp,Pp,Lp},{Hp,Dp,Kp,Op},{Cp+56*2,Hp,Op,Ip+56*2},
2587b7f5c055SJed Brown           /* layer 5 */
2588b7f5c055SJed Brown           {Kp,Mp,Sp,Rp},{Mp,Ip,Qp,Sp},{Np,Lp,Rp,Tp},{Jp,Np,Tp,Qp+56*1},{Pp,Jp+56*2,Qp+56*3,Up},{Lp,Pp,Up,Rp},{Op,Kp,Rp,Vp},{Ip+56*2,Op,Vp,Qp+56*2},
2589b7f5c055SJed Brown           /* layer 6 */
2590b7f5c055SJed Brown           {Rp,Sp,Aq,Yp},{Sp,Qp,Wp,Aq},{Tp,Rp,Yp,Cq},{Qp+56*1,Tp,Cq,Wp+56*1},{Up,Qp+56*3,Xp+56*1,Dq},{Rp,Up,Dq,Zp},{Vp,Rp,Zp,Bq},{Qp+56*2,Vp,Bq,Xp},
2591b7f5c055SJed Brown           /* layer 7 (the top is the periodic image of the bottom of layer 0) */
2592b7f5c055SJed Brown           {Yp,Aq,C+56*4,A+56*4},{Aq,Wp,B+56*4,C+56*4},{Cq,Yp,A+56*4,D+56*4},{Wp+56*1,Cq,D+56*4,B+56*5},{Dq,Xp+56*1,B+56*5,E+56*4},{Zp,Dq,E+56*4,A+56*6},{Bq,Zp,A+56*6,F+56*4},{Xp,Bq,F+56*4,B+56*4}
2593b7f5c055SJed Brown         };
2594b7f5c055SJed Brown       const PetscReal gamma = PetscAcosReal((PetscSqrtReal(3.)-1.) / PetscSqrtReal(2.)) / PETSC_PI;
2595b7f5c055SJed Brown       const PetscReal patternCoords[56][3] =
2596b7f5c055SJed Brown         {
2597b7f5c055SJed Brown           /* A  */ {1.,0.,0.},
2598b7f5c055SJed Brown           /* B  */ {0.,1.,0.},
2599b7f5c055SJed Brown           /* C  */ {gamma,gamma,0.},
2600b7f5c055SJed Brown           /* D  */ {1+gamma,1-gamma,0.},
2601b7f5c055SJed Brown           /* E  */ {2-gamma,2-gamma,0.},
2602b7f5c055SJed Brown           /* F  */ {1-gamma,1+gamma,0.},
2603b7f5c055SJed Brown 
2604b7f5c055SJed Brown           /* G  */ {.5,0,.25},
2605b7f5c055SJed Brown           /* H  */ {1.5,0.,.25},
2606b7f5c055SJed Brown           /* II */ {.5,1.,.25},
2607b7f5c055SJed Brown           /* J  */ {1.5,1.,.25},
2608b7f5c055SJed Brown           /* K  */ {.25,.5,.25},
2609b7f5c055SJed Brown           /* L  */ {1.25,.5,.25},
2610b7f5c055SJed Brown           /* M  */ {.75,1.5,.25},
2611b7f5c055SJed Brown           /* N  */ {1.75,1.5,.25},
2612b7f5c055SJed Brown 
2613b7f5c055SJed Brown           /* O  */ {0.,0.,.5},
2614b7f5c055SJed Brown           /* P  */ {1.,1.,.5},
2615b7f5c055SJed Brown           /* Q  */ {gamma,1-gamma,.5},
2616b7f5c055SJed Brown           /* R  */ {1+gamma,gamma,.5},
2617b7f5c055SJed Brown           /* S  */ {2-gamma,1+gamma,.5},
2618b7f5c055SJed Brown           /* T  */ {1-gamma,2-gamma,.5},
2619b7f5c055SJed Brown 
2620b7f5c055SJed Brown           /* U  */ {0.,.5,.75},
2621b7f5c055SJed Brown           /* V  */ {0.,1.5,.75},
2622b7f5c055SJed Brown           /* W  */ {1.,.5,.75},
2623b7f5c055SJed Brown           /* X  */ {1.,1.5,.75},
2624b7f5c055SJed Brown           /* Y  */ {.5,.75,.75},
2625b7f5c055SJed Brown           /* Z  */ {.5,1.75,.75},
2626b7f5c055SJed Brown           /* Ap */ {1.5,.25,.75},
2627b7f5c055SJed Brown           /* Bp */ {1.5,1.25,.75},
2628b7f5c055SJed Brown 
2629b7f5c055SJed Brown           /* Cp */ {1.,0.,1.},
2630b7f5c055SJed Brown           /* Dp */ {0.,1.,1.},
2631b7f5c055SJed Brown           /* Ep */ {1-gamma,1-gamma,1.},
2632b7f5c055SJed Brown           /* Fp */ {1+gamma,1+gamma,1.},
2633b7f5c055SJed Brown           /* Gp */ {2-gamma,gamma,1.},
2634b7f5c055SJed Brown           /* Hp */ {gamma,2-gamma,1.},
2635b7f5c055SJed Brown 
2636b7f5c055SJed Brown           /* Ip */ {.5,0.,1.25},
2637b7f5c055SJed Brown           /* Jp */ {1.5,0.,1.25},
2638b7f5c055SJed Brown           /* Kp */ {.5,1.,1.25},
2639b7f5c055SJed Brown           /* Lp */ {1.5,1.,1.25},
2640b7f5c055SJed Brown           /* Mp */ {.75,.5,1.25},
2641b7f5c055SJed Brown           /* Np */ {1.75,.5,1.25},
2642b7f5c055SJed Brown           /* Op */ {.25,1.5,1.25},
2643b7f5c055SJed Brown           /* Pp */ {1.25,1.5,1.25},
2644b7f5c055SJed Brown 
2645b7f5c055SJed Brown           /* Qp */ {0.,0.,1.5},
2646b7f5c055SJed Brown           /* Rp */ {1.,1.,1.5},
2647b7f5c055SJed Brown           /* Sp */ {1-gamma,gamma,1.5},
2648b7f5c055SJed Brown           /* Tp */ {2-gamma,1-gamma,1.5},
2649b7f5c055SJed Brown           /* Up */ {1+gamma,2-gamma,1.5},
2650b7f5c055SJed Brown           /* Vp */ {gamma,1+gamma,1.5},
2651b7f5c055SJed Brown 
2652b7f5c055SJed Brown           /* Wp */ {0.,.5,1.75},
2653b7f5c055SJed Brown           /* Xp */ {0.,1.5,1.75},
2654b7f5c055SJed Brown           /* Yp */ {1.,.5,1.75},
2655b7f5c055SJed Brown           /* Zp */ {1.,1.5,1.75},
2656b7f5c055SJed Brown           /* Aq */ {.5,.25,1.75},
2657b7f5c055SJed Brown           /* Bq */ {.5,1.25,1.75},
2658b7f5c055SJed Brown           /* Cq */ {1.5,.75,1.75},
2659b7f5c055SJed Brown           /* Dq */ {1.5,1.75,1.75},
2660b7f5c055SJed Brown         };
2661b7f5c055SJed Brown       PetscInt  (*cells)[64][4] = NULL;
2662b7f5c055SJed Brown       PetscBool *seen;
2663b7f5c055SJed Brown       PetscInt  *vertToTrueVert;
2664b7f5c055SJed Brown       PetscInt  count;
2665b7f5c055SJed Brown 
2666b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) extentPlus[i]  = extent[i] + 1;
2667b7f5c055SJed Brown       numBlocks = 1;
2668b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++)     numBlocks *= extent[i];
2669b7f5c055SJed Brown       numBlocksPlus = 1;
2670b7f5c055SJed Brown       for (PetscInt i = 0; i < 3; i++) numBlocksPlus *= extentPlus[i];
2671b7f5c055SJed Brown       numFaces = numBlocks * facesPerBlock;
26729566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numBlocks, &cells));
26739566063dSJacob Faibussowitsch       PetscCall(PetscCalloc1(numBlocksPlus * vertsPerBlock,&seen));
2674b7f5c055SJed Brown       for (PetscInt k = 0; k < extent[2]; k++) {
2675b7f5c055SJed Brown         for (PetscInt j = 0; j < extent[1]; j++) {
2676b7f5c055SJed Brown           for (PetscInt i = 0; i < extent[0]; i++) {
2677b7f5c055SJed Brown             for (PetscInt f = 0; f < facesPerBlock; f++) {
2678b7f5c055SJed Brown               for (PetscInt v = 0; v < 4; v++) {
2679b7f5c055SJed Brown                 PetscInt vertRaw = pattern[f][v];
2680b7f5c055SJed Brown                 PetscInt blockidx = vertRaw / 56;
2681b7f5c055SJed Brown                 PetscInt patternvert = vertRaw % 56;
2682b7f5c055SJed Brown                 PetscInt xplus = (blockidx & 1);
2683b7f5c055SJed Brown                 PetscInt yplus = (blockidx & 2) >> 1;
2684b7f5c055SJed Brown                 PetscInt zplus = (blockidx & 4) >> 2;
2685b7f5c055SJed Brown                 PetscInt zcoord = (periodic && periodic[2] == DM_BOUNDARY_PERIODIC) ? ((k + zplus) % extent[2]) : (k + zplus);
2686b7f5c055SJed Brown                 PetscInt ycoord = (periodic && periodic[1] == DM_BOUNDARY_PERIODIC) ? ((j + yplus) % extent[1]) : (j + yplus);
2687b7f5c055SJed Brown                 PetscInt xcoord = (periodic && periodic[0] == DM_BOUNDARY_PERIODIC) ? ((i + xplus) % extent[0]) : (i + xplus);
2688b7f5c055SJed Brown                 PetscInt vert = ((zcoord * extentPlus[1] + ycoord) * extentPlus[0] + xcoord) * 56 + patternvert;
2689b7f5c055SJed Brown 
2690b7f5c055SJed Brown                 cells[(k * extent[1] + j) * extent[0] + i][f][v] = vert;
2691b7f5c055SJed Brown                 seen[vert] = PETSC_TRUE;
2692b7f5c055SJed Brown               }
2693b7f5c055SJed Brown             }
2694b7f5c055SJed Brown           }
2695b7f5c055SJed Brown         }
2696b7f5c055SJed Brown       }
2697b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++) if (seen[i]) numVertices++;
2698b7f5c055SJed Brown       count = 0;
26999566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numBlocksPlus * vertsPerBlock, &vertToTrueVert));
27009566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numVertices * 3, &vtxCoords));
2701b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocksPlus * vertsPerBlock; i++) vertToTrueVert[i] = -1;
2702b7f5c055SJed Brown       for (PetscInt k = 0; k < extentPlus[2]; k++) {
2703b7f5c055SJed Brown         for (PetscInt j = 0; j < extentPlus[1]; j++) {
2704b7f5c055SJed Brown           for (PetscInt i = 0; i < extentPlus[0]; i++) {
2705b7f5c055SJed Brown             for (PetscInt v = 0; v < vertsPerBlock; v++) {
2706b7f5c055SJed Brown               PetscInt vIdx = ((k * extentPlus[1] + j) * extentPlus[0] + i) * vertsPerBlock + v;
2707b7f5c055SJed Brown 
2708b7f5c055SJed Brown               if (seen[vIdx]) {
2709b7f5c055SJed Brown                 PetscInt thisVert;
2710b7f5c055SJed Brown 
2711b7f5c055SJed Brown                 vertToTrueVert[vIdx] = thisVert = count++;
2712b7f5c055SJed Brown 
2713b7f5c055SJed Brown                 for (PetscInt d = 0; d < 3; d++) vtxCoords[3 * thisVert + d] = patternCoords[v][d];
2714b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 0] += i * 2;
2715b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 1] += j * 2;
2716b7f5c055SJed Brown                 vtxCoords[3 * thisVert + 2] += k * 2;
2717b7f5c055SJed Brown               }
2718b7f5c055SJed Brown             }
2719b7f5c055SJed Brown           }
2720b7f5c055SJed Brown         }
2721b7f5c055SJed Brown       }
2722b7f5c055SJed Brown       for (PetscInt i = 0; i < numBlocks; i++) {
2723b7f5c055SJed Brown         for (PetscInt f = 0; f < facesPerBlock; f++) {
2724b7f5c055SJed Brown           for (PetscInt v = 0; v < 4; v++) {
2725b7f5c055SJed Brown             cells[i][f][v] = vertToTrueVert[cells[i][f][v]];
2726b7f5c055SJed Brown           }
2727b7f5c055SJed Brown         }
2728b7f5c055SJed Brown       }
27299566063dSJacob Faibussowitsch       PetscCall(PetscFree(vertToTrueVert));
27309566063dSJacob Faibussowitsch       PetscCall(PetscFree(seen));
2731b7f5c055SJed Brown       cells_flat = cells[0][0];
2732b7f5c055SJed Brown       numEdges = 0;
2733b7f5c055SJed Brown       for (PetscInt i = 0; i < numFaces; i++) {
2734b7f5c055SJed Brown         for (PetscInt e = 0; e < 4; e++) {
2735b7f5c055SJed Brown           PetscInt ev[] = {cells_flat[i*4 + e], cells_flat[i*4 + ((e+1)%4)]};
2736b7f5c055SJed Brown           const PetscReal *evCoords[] = {&vtxCoords[3*ev[0]], &vtxCoords[3*ev[1]]};
2737b7f5c055SJed Brown 
2738b7f5c055SJed Brown           for (PetscInt d = 0; d < 3; d++) {
2739b7f5c055SJed Brown             if (!periodic || periodic[0] != DM_BOUNDARY_PERIODIC) {
2740b7f5c055SJed Brown               if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) numEdges++;
2741b7f5c055SJed Brown               if (evCoords[0][d] == 2.*extent[d] && evCoords[1][d] == 2.*extent[d]) numEdges++;
2742b7f5c055SJed Brown             }
2743b7f5c055SJed Brown           }
2744b7f5c055SJed Brown         }
2745b7f5c055SJed Brown       }
27469566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numEdges, &edges));
27479566063dSJacob Faibussowitsch       PetscCall(PetscMalloc1(numEdges, &edgeSets));
2748b7f5c055SJed Brown       for (PetscInt edge = 0, i = 0; i < numFaces; i++) {
2749b7f5c055SJed Brown         for (PetscInt e = 0; e < 4; e++) {
2750b7f5c055SJed Brown           PetscInt ev[] = {cells_flat[i*4 + e], cells_flat[i*4 + ((e+1)%4)]};
2751b7f5c055SJed Brown           const PetscReal *evCoords[] = {&vtxCoords[3*ev[0]], &vtxCoords[3*ev[1]]};
2752b7f5c055SJed Brown 
2753b7f5c055SJed Brown           for (PetscInt d = 0; d < 3; d++) {
2754b7f5c055SJed Brown             if (!periodic || periodic[d] != DM_BOUNDARY_PERIODIC) {
2755b7f5c055SJed Brown               if (evCoords[0][d] == 0. && evCoords[1][d] == 0.) {
2756b7f5c055SJed Brown                 edges[edge][0] = ev[0];
2757b7f5c055SJed Brown                 edges[edge][1] = ev[1];
2758b7f5c055SJed Brown                 edgeSets[edge++] = 2 * d;
2759b7f5c055SJed Brown               }
2760b7f5c055SJed Brown               if (evCoords[0][d] == 2.*extent[d] && evCoords[1][d] == 2.*extent[d]) {
2761b7f5c055SJed Brown                 edges[edge][0] = ev[0];
2762b7f5c055SJed Brown                 edges[edge][1] = ev[1];
2763b7f5c055SJed Brown                 edgeSets[edge++] = 2 * d + 1;
2764b7f5c055SJed Brown               }
2765b7f5c055SJed Brown             }
2766b7f5c055SJed Brown           }
2767b7f5c055SJed Brown         }
2768b7f5c055SJed Brown       }
2769b7f5c055SJed Brown     }
2770b7f5c055SJed Brown     evalFunc = TPSEvaluate_Gyroid;
27714663dae6SJed Brown     normalFunc = TPSExtrudeNormalFunc_Gyroid;
2772b7f5c055SJed Brown     break;
2773b7f5c055SJed Brown   }
2774b7f5c055SJed Brown 
27759566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(dm, topoDim));
2776c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildFromCellList(dm, numFaces, numVertices, 4, cells_flat));
27779566063dSJacob Faibussowitsch   else           PetscCall(DMPlexBuildFromCellList(dm, 0, 0, 0, NULL));
27789566063dSJacob Faibussowitsch   PetscCall(PetscFree(cells_flat));
2779b7f5c055SJed Brown   {
2780b7f5c055SJed Brown     DM idm;
27819566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(dm, &idm));
27829566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(dm, &idm));
2783b7f5c055SJed Brown   }
2784c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildCoordinatesFromCellList(dm, spaceDim, vtxCoords));
27859566063dSJacob Faibussowitsch   else           PetscCall(DMPlexBuildCoordinatesFromCellList(dm, spaceDim, NULL));
27869566063dSJacob Faibussowitsch   PetscCall(PetscFree(vtxCoords));
2787b7f5c055SJed Brown 
27889566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "Face Sets"));
27899566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "Face Sets", &label));
2790b7f5c055SJed Brown   for (PetscInt e=0; e<numEdges; e++) {
2791b7f5c055SJed Brown     PetscInt njoin;
2792b7f5c055SJed Brown     const PetscInt *join, verts[] = {numFaces + edges[e][0], numFaces + edges[e][1]};
27939566063dSJacob Faibussowitsch     PetscCall(DMPlexGetJoin(dm, 2, verts, &njoin, &join));
279463a3b9bcSJacob 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]);
27959566063dSJacob Faibussowitsch     PetscCall(DMLabelSetValue(label, join[0], edgeSets[e]));
27969566063dSJacob Faibussowitsch     PetscCall(DMPlexRestoreJoin(dm, 2, verts, &njoin, &join));
2797b7f5c055SJed Brown   }
27989566063dSJacob Faibussowitsch   PetscCall(PetscFree(edges));
27999566063dSJacob Faibussowitsch   PetscCall(PetscFree(edgeSets));
28001436d7faSJed Brown   if (tps_distribute) {
28011436d7faSJed Brown     DM               pdm = NULL;
28021436d7faSJed Brown     PetscPartitioner part;
28031436d7faSJed Brown 
28049566063dSJacob Faibussowitsch     PetscCall(DMPlexGetPartitioner(dm, &part));
28059566063dSJacob Faibussowitsch     PetscCall(PetscPartitionerSetFromOptions(part));
28069566063dSJacob Faibussowitsch     PetscCall(DMPlexDistribute(dm, 0, NULL, &pdm));
28071436d7faSJed Brown     if (pdm) {
28089566063dSJacob Faibussowitsch       PetscCall(DMPlexReplace_Static(dm, &pdm));
28091436d7faSJed Brown     }
28101436d7faSJed Brown     // Do not auto-distribute again
28119566063dSJacob Faibussowitsch     PetscCall(DMPlexDistributeSetDefault(dm, PETSC_FALSE));
28121436d7faSJed Brown   }
2813b7f5c055SJed Brown 
28149566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
2815b7f5c055SJed Brown   for (PetscInt refine=0; refine<refinements; refine++) {
2816b7f5c055SJed Brown     PetscInt m;
2817b7f5c055SJed Brown     DM dmf;
2818b7f5c055SJed Brown     Vec X;
2819b7f5c055SJed Brown     PetscScalar *x;
28209566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, MPI_COMM_NULL, &dmf));
28219566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(dm, &dmf));
2822b7f5c055SJed Brown 
28239566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinatesLocal(dm, &X));
28249566063dSJacob Faibussowitsch     PetscCall(VecGetLocalSize(X, &m));
28259566063dSJacob Faibussowitsch     PetscCall(VecGetArray(X, &x));
2826b7f5c055SJed Brown     for (PetscInt i=0; i<m; i+=3) {
28279566063dSJacob Faibussowitsch       PetscCall(TPSNearestPoint(evalFunc, &x[i]));
2828b7f5c055SJed Brown     }
28299566063dSJacob Faibussowitsch     PetscCall(VecRestoreArray(X, &x));
2830b7f5c055SJed Brown   }
2831b7f5c055SJed Brown 
2832b7f5c055SJed Brown   // Face Sets has already been propagated to new vertices during refinement; this propagates to the initial vertices.
28339566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "Face Sets", &label));
28349566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete(dm, label));
2835b7f5c055SJed Brown 
2836b7f5c055SJed Brown   if (thickness > 0) {
28374663dae6SJed Brown     DM edm,cdm,ecdm;
28384663dae6SJed Brown     DMPlexTransform tr;
28394663dae6SJed Brown     const char *prefix;
28404663dae6SJed Brown     PetscOptions options;
28414663dae6SJed Brown     // Code from DMPlexExtrude
28424663dae6SJed Brown     PetscCall(DMPlexTransformCreate(PetscObjectComm((PetscObject)dm), &tr));
28434663dae6SJed Brown     PetscCall(DMPlexTransformSetDM(tr, dm));
28444663dae6SJed Brown     PetscCall(DMPlexTransformSetType(tr, DMPLEXEXTRUDE));
28454663dae6SJed Brown     PetscCall(PetscObjectGetOptionsPrefix((PetscObject) dm, &prefix));
28464663dae6SJed Brown     PetscCall(PetscObjectSetOptionsPrefix((PetscObject) tr,  prefix));
28474663dae6SJed Brown     PetscCall(PetscObjectGetOptions((PetscObject) dm, &options));
28484663dae6SJed Brown     PetscCall(PetscObjectSetOptions((PetscObject) tr, options));
28494663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetLayers(tr, layers));
28504663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetThickness(tr, thickness));
28514663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetTensor(tr, PETSC_FALSE));
28524663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetSymmetric(tr, PETSC_TRUE));
28534663dae6SJed Brown     PetscCall(DMPlexTransformExtrudeSetNormalFunction(tr, normalFunc));
28544663dae6SJed Brown     PetscCall(DMPlexTransformSetFromOptions(tr));
28554663dae6SJed Brown     PetscCall(PetscObjectSetOptions((PetscObject) tr, NULL));
28564663dae6SJed Brown     PetscCall(DMPlexTransformSetUp(tr));
28574663dae6SJed Brown     PetscCall(PetscObjectViewFromOptions((PetscObject) tr, NULL, "-dm_plex_tps_transform_view"));
28584663dae6SJed Brown     PetscCall(DMPlexTransformApply(tr, dm, &edm));
28594663dae6SJed Brown     PetscCall(DMCopyDisc(dm, edm));
28604663dae6SJed Brown     PetscCall(DMGetCoordinateDM(dm, &cdm));
28614663dae6SJed Brown     PetscCall(DMGetCoordinateDM(edm, &ecdm));
28624663dae6SJed Brown     PetscCall(DMCopyDisc(cdm, ecdm));
28634663dae6SJed Brown     PetscCall(DMPlexTransformCreateDiscLabels(tr, edm));
28644663dae6SJed Brown     PetscCall(DMPlexTransformDestroy(&tr));
28654663dae6SJed Brown     if (edm) {
28664663dae6SJed Brown       ((DM_Plex *)edm->data)->printFEM = ((DM_Plex *)dm->data)->printFEM;
28674663dae6SJed Brown       ((DM_Plex *)edm->data)->printL2  = ((DM_Plex *)dm->data)->printL2;
28684663dae6SJed Brown     }
28694663dae6SJed Brown     PetscCall(DMPlexReplace_Static(dm, &edm));
2870b7f5c055SJed Brown   }
2871b7f5c055SJed Brown   PetscFunctionReturn(0);
2872b7f5c055SJed Brown }
2873b7f5c055SJed Brown 
2874b7f5c055SJed Brown /*@
2875b7f5c055SJed Brown   DMPlexCreateTPSMesh - Create a distributed, interpolated mesh of a triply-periodic surface
2876b7f5c055SJed Brown 
2877b7f5c055SJed Brown   Collective
2878b7f5c055SJed Brown 
2879b7f5c055SJed Brown   Input Parameters:
2880b7f5c055SJed Brown + comm   - The communicator for the DM object
2881b7f5c055SJed Brown . tpstype - Type of triply-periodic surface
2882b7f5c055SJed Brown . extent - Array of length 3 containing number of periods in each direction
2883b7f5c055SJed Brown . periodic - array of length 3 with periodicity, or NULL for non-periodic
28841436d7faSJed Brown . tps_distribute - Distribute 2D manifold mesh prior to refinement and extrusion (more scalable)
2885817da375SSatish Balay . refinements - Number of factor-of-2 refinements of 2D manifold mesh
28861436d7faSJed Brown . layers - Number of cell layers extruded in normal direction
2887817da375SSatish Balay - thickness - Thickness in normal direction
2888b7f5c055SJed Brown 
2889b7f5c055SJed Brown   Output Parameter:
2890b7f5c055SJed Brown . dm  - The DM object
2891b7f5c055SJed Brown 
2892b7f5c055SJed Brown   Notes:
2893b7f5c055SJed 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.
2894b7f5c055SJed Brown   https://en.wikipedia.org/wiki/Schwarz_minimal_surface#Schwarz_P_(%22Primitive%22) and can be cut with "clean" boundaries.
2895b7f5c055SJed 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.
2896b7f5c055SJed Brown   Our implementation creates a very coarse mesh of the surface and refines (by 4-way splitting) as many times as requested.
2897b7f5c055SJed Brown   On each refinement, all vertices are projected to their nearest point on the surface.
2898b7f5c055SJed Brown   This projection could readily be extended to related surfaces.
2899b7f5c055SJed Brown 
2900b7f5c055SJed Brown   The face (edge) sets for the Schwarz P surface are numbered 1(-x), 2(+x), 3(-y), 4(+y), 5(-z), 6(+z).
2901b7f5c055SJed Brown   When the mesh is refined, "Face Sets" contain the new vertices (created during refinement).  Use DMPlexLabelComplete() to propagate to coarse-level vertices.
2902b7f5c055SJed Brown 
2903b7f5c055SJed Brown   References:
2904606c0280SSatish Balay . * - Maskery et al, Insights into the mechanical properties of several triply periodic minimal surface lattice structures made by polymer additive manufacturing, 2017. https://doi.org/10.1016/j.polymer.2017.11.049
2905b7f5c055SJed Brown 
2906b7f5c055SJed Brown   Developer Notes:
2907b7f5c055SJed Brown   The Gyroid mesh does not currently mark boundary sets.
2908b7f5c055SJed Brown 
2909b7f5c055SJed Brown   Level: beginner
2910b7f5c055SJed Brown 
2911db781477SPatrick Sanan .seealso: `DMPlexCreateSphereMesh()`, `DMSetType()`, `DMCreate()`
2912b7f5c055SJed Brown @*/
29131436d7faSJed Brown PetscErrorCode DMPlexCreateTPSMesh(MPI_Comm comm, DMPlexTPSType tpstype, const PetscInt extent[], const DMBoundaryType periodic[], PetscBool tps_distribute, PetscInt refinements, PetscInt layers, PetscReal thickness, DM *dm)
2914b7f5c055SJed Brown {
2915b7f5c055SJed Brown   PetscFunctionBegin;
29169566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
29179566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
29189566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateTPSMesh_Internal(*dm, tpstype, extent, periodic, tps_distribute, refinements, layers, thickness));
2919b7f5c055SJed Brown   PetscFunctionReturn(0);
2920b7f5c055SJed Brown }
2921b7f5c055SJed Brown 
29229318fe57SMatthew G. Knepley /*@
29239318fe57SMatthew G. Knepley   DMPlexCreateSphereMesh - Creates a mesh on the d-dimensional sphere, S^d.
29249318fe57SMatthew G. Knepley 
29259318fe57SMatthew G. Knepley   Collective
29269318fe57SMatthew G. Knepley 
29279318fe57SMatthew G. Knepley   Input Parameters:
29289318fe57SMatthew G. Knepley + comm    - The communicator for the DM object
29299318fe57SMatthew G. Knepley . dim     - The dimension
29309318fe57SMatthew G. Knepley . simplex - Use simplices, or tensor product cells
29319318fe57SMatthew G. Knepley - R       - The radius
29329318fe57SMatthew G. Knepley 
29339318fe57SMatthew G. Knepley   Output Parameter:
29349318fe57SMatthew G. Knepley . dm  - The DM object
29359318fe57SMatthew G. Knepley 
29369318fe57SMatthew G. Knepley   Level: beginner
29379318fe57SMatthew G. Knepley 
2938db781477SPatrick Sanan .seealso: `DMPlexCreateBallMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
29399318fe57SMatthew G. Knepley @*/
29409318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateSphereMesh(MPI_Comm comm, PetscInt dim, PetscBool simplex, PetscReal R, DM *dm)
29419318fe57SMatthew G. Knepley {
29429318fe57SMatthew G. Knepley   PetscFunctionBegin;
29439318fe57SMatthew G. Knepley   PetscValidPointer(dm, 5);
29449566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
29459566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
29469566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSphereMesh_Internal(*dm, dim, simplex, R));
29479318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
29489318fe57SMatthew G. Knepley }
29499318fe57SMatthew G. Knepley 
29509318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBallMesh_Internal(DM dm, PetscInt dim, PetscReal R)
29519318fe57SMatthew G. Knepley {
29529318fe57SMatthew G. Knepley   DM             sdm, vol;
29539318fe57SMatthew G. Knepley   DMLabel        bdlabel;
29549318fe57SMatthew G. Knepley 
29559318fe57SMatthew G. Knepley   PetscFunctionBegin;
29569566063dSJacob Faibussowitsch   PetscCall(DMCreate(PetscObjectComm((PetscObject) dm), &sdm));
29579566063dSJacob Faibussowitsch   PetscCall(DMSetType(sdm, DMPLEX));
29589566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetOptionsPrefix((PetscObject) sdm, "bd_"));
29599566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateSphereMesh_Internal(sdm, dim-1, PETSC_TRUE, R));
29609566063dSJacob Faibussowitsch   PetscCall(DMSetFromOptions(sdm));
29619566063dSJacob Faibussowitsch   PetscCall(DMViewFromOptions(sdm, NULL, "-dm_view"));
29629566063dSJacob Faibussowitsch   PetscCall(DMPlexGenerate(sdm, NULL, PETSC_TRUE, &vol));
29639566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&sdm));
29649566063dSJacob Faibussowitsch   PetscCall(DMPlexReplace_Static(dm, &vol));
29659566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, "marker"));
29669566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, "marker", &bdlabel));
29679566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkBoundaryFaces(dm, PETSC_DETERMINE, bdlabel));
29689566063dSJacob Faibussowitsch   PetscCall(DMPlexLabelComplete(dm, bdlabel));
296951a74b61SMatthew G. Knepley   PetscFunctionReturn(0);
297051a74b61SMatthew G. Knepley }
297151a74b61SMatthew G. Knepley 
297251a74b61SMatthew G. Knepley /*@
297351a74b61SMatthew G. Knepley   DMPlexCreateBallMesh - Creates a simplex mesh on the d-dimensional ball, B^d.
297451a74b61SMatthew G. Knepley 
297551a74b61SMatthew G. Knepley   Collective
297651a74b61SMatthew G. Knepley 
297751a74b61SMatthew G. Knepley   Input Parameters:
297851a74b61SMatthew G. Knepley + comm  - The communicator for the DM object
297951a74b61SMatthew G. Knepley . dim   - The dimension
298051a74b61SMatthew G. Knepley - R     - The radius
298151a74b61SMatthew G. Knepley 
298251a74b61SMatthew G. Knepley   Output Parameter:
298351a74b61SMatthew G. Knepley . dm  - The DM object
298451a74b61SMatthew G. Knepley 
298551a74b61SMatthew G. Knepley   Options Database Keys:
298651a74b61SMatthew G. Knepley - bd_dm_refine - This will refine the surface mesh preserving the sphere geometry
298751a74b61SMatthew G. Knepley 
298851a74b61SMatthew G. Knepley   Level: beginner
298951a74b61SMatthew G. Knepley 
2990db781477SPatrick Sanan .seealso: `DMPlexCreateSphereMesh()`, `DMPlexCreateBoxMesh()`, `DMSetType()`, `DMCreate()`
299151a74b61SMatthew G. Knepley @*/
299251a74b61SMatthew G. Knepley PetscErrorCode DMPlexCreateBallMesh(MPI_Comm comm, PetscInt dim, PetscReal R, DM *dm)
299351a74b61SMatthew G. Knepley {
299451a74b61SMatthew G. Knepley   PetscFunctionBegin;
29959566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
29969566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
29979566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateBallMesh_Internal(*dm, dim, R));
29982829fed8SMatthew G. Knepley   PetscFunctionReturn(0);
29992829fed8SMatthew G. Knepley }
30002829fed8SMatthew G. Knepley 
30019318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateReferenceCell_Internal(DM rdm, DMPolytopeType ct)
30020a6ba040SMatthew G. Knepley {
30030a6ba040SMatthew G. Knepley   PetscFunctionBegin;
30049318fe57SMatthew G. Knepley   switch (ct) {
30059318fe57SMatthew G. Knepley     case DM_POLYTOPE_POINT:
30069318fe57SMatthew G. Knepley     {
30079318fe57SMatthew G. Knepley       PetscInt    numPoints[1]        = {1};
30089318fe57SMatthew G. Knepley       PetscInt    coneSize[1]         = {0};
30099318fe57SMatthew G. Knepley       PetscInt    cones[1]            = {0};
30109318fe57SMatthew G. Knepley       PetscInt    coneOrientations[1] = {0};
30119318fe57SMatthew G. Knepley       PetscScalar vertexCoords[1]     = {0.0};
30129318fe57SMatthew G. Knepley 
30139566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 0));
30149566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 0, numPoints, coneSize, cones, coneOrientations, vertexCoords));
30159318fe57SMatthew G. Knepley     }
30169318fe57SMatthew G. Knepley     break;
30179318fe57SMatthew G. Knepley     case DM_POLYTOPE_SEGMENT:
30189318fe57SMatthew G. Knepley     {
30199318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {2, 1};
30209318fe57SMatthew G. Knepley       PetscInt    coneSize[3]         = {2, 0, 0};
30219318fe57SMatthew G. Knepley       PetscInt    cones[2]            = {1, 2};
30229318fe57SMatthew G. Knepley       PetscInt    coneOrientations[2] = {0, 0};
30239318fe57SMatthew G. Knepley       PetscScalar vertexCoords[2]     = {-1.0,  1.0};
30249318fe57SMatthew G. Knepley 
30259566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 1));
30269566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
30279318fe57SMatthew G. Knepley     }
30289318fe57SMatthew G. Knepley     break;
3029b5a892a1SMatthew G. Knepley     case DM_POLYTOPE_POINT_PRISM_TENSOR:
3030b5a892a1SMatthew G. Knepley     {
3031b5a892a1SMatthew G. Knepley       PetscInt    numPoints[2]        = {2, 1};
3032b5a892a1SMatthew G. Knepley       PetscInt    coneSize[3]         = {2, 0, 0};
3033b5a892a1SMatthew G. Knepley       PetscInt    cones[2]            = {1, 2};
3034b5a892a1SMatthew G. Knepley       PetscInt    coneOrientations[2] = {0, 0};
3035b5a892a1SMatthew G. Knepley       PetscScalar vertexCoords[2]     = {-1.0,  1.0};
3036b5a892a1SMatthew G. Knepley 
30379566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 1));
30389566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
3039b5a892a1SMatthew G. Knepley     }
3040b5a892a1SMatthew G. Knepley     break;
30419318fe57SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
30429318fe57SMatthew G. Knepley     {
30439318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {3, 1};
30449318fe57SMatthew G. Knepley       PetscInt    coneSize[4]         = {3, 0, 0, 0};
30459318fe57SMatthew G. Knepley       PetscInt    cones[3]            = {1, 2, 3};
30469318fe57SMatthew G. Knepley       PetscInt    coneOrientations[3] = {0, 0, 0};
30479318fe57SMatthew G. Knepley       PetscScalar vertexCoords[6]     = {-1.0, -1.0,  1.0, -1.0,  -1.0, 1.0};
30489318fe57SMatthew G. Knepley 
30499566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 2));
30509566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
30519318fe57SMatthew G. Knepley     }
30529318fe57SMatthew G. Knepley     break;
30539318fe57SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
30549318fe57SMatthew G. Knepley     {
30559318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {4, 1};
30569318fe57SMatthew G. Knepley       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
30579318fe57SMatthew G. Knepley       PetscInt    cones[4]            = {1, 2, 3, 4};
30589318fe57SMatthew G. Knepley       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
30599318fe57SMatthew G. Knepley       PetscScalar vertexCoords[8]     = {-1.0, -1.0,  1.0, -1.0,  1.0, 1.0,  -1.0, 1.0};
30609318fe57SMatthew G. Knepley 
30619566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 2));
30629566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
30639318fe57SMatthew G. Knepley     }
30649318fe57SMatthew G. Knepley     break;
30659318fe57SMatthew G. Knepley     case DM_POLYTOPE_SEG_PRISM_TENSOR:
30669318fe57SMatthew G. Knepley     {
30679318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {4, 1};
30689318fe57SMatthew G. Knepley       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
30699318fe57SMatthew G. Knepley       PetscInt    cones[4]            = {1, 2, 3, 4};
30709318fe57SMatthew G. Knepley       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
30719318fe57SMatthew G. Knepley       PetscScalar vertexCoords[8]     = {-1.0, -1.0,  1.0, -1.0,  -1.0, 1.0,  1.0, 1.0};
30729318fe57SMatthew G. Knepley 
30739566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 2));
30749566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
30759318fe57SMatthew G. Knepley     }
30769318fe57SMatthew G. Knepley     break;
30779318fe57SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
30789318fe57SMatthew G. Knepley     {
30799318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {4, 1};
30809318fe57SMatthew G. Knepley       PetscInt    coneSize[5]         = {4, 0, 0, 0, 0};
3081f0edb160SMatthew G. Knepley       PetscInt    cones[4]            = {1, 2, 3, 4};
30829318fe57SMatthew G. Knepley       PetscInt    coneOrientations[4] = {0, 0, 0, 0};
3083f0edb160SMatthew 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};
30849318fe57SMatthew G. Knepley 
30859566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 3));
30869566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
30879318fe57SMatthew G. Knepley     }
30889318fe57SMatthew G. Knepley     break;
30899318fe57SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
30909318fe57SMatthew G. Knepley     {
30919318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {8, 1};
30929318fe57SMatthew G. Knepley       PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
3093f0edb160SMatthew G. Knepley       PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
30949318fe57SMatthew G. Knepley       PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
3095f0edb160SMatthew G. Knepley       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,
30969318fe57SMatthew G. Knepley                                          -1.0, -1.0,  1.0,   1.0, -1.0,  1.0,  1.0, 1.0,  1.0,  -1.0,  1.0,  1.0};
30979318fe57SMatthew G. Knepley 
30989566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 3));
30999566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
31009318fe57SMatthew G. Knepley     }
31019318fe57SMatthew G. Knepley     break;
31029318fe57SMatthew G. Knepley     case DM_POLYTOPE_TRI_PRISM:
31039318fe57SMatthew G. Knepley     {
31049318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {6, 1};
31059318fe57SMatthew G. Knepley       PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
3106f0edb160SMatthew G. Knepley       PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
31079318fe57SMatthew G. Knepley       PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
3108f0edb160SMatthew G. Knepley       PetscScalar vertexCoords[18]    = {-1.0, -1.0, -1.0, -1.0,  1.0, -1.0,   1.0, -1.0, -1.0,
31099318fe57SMatthew G. Knepley                                          -1.0, -1.0,  1.0,  1.0, -1.0,  1.0,  -1.0,  1.0,  1.0};
31109318fe57SMatthew G. Knepley 
31119566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 3));
31129566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
31139318fe57SMatthew G. Knepley     }
31149318fe57SMatthew G. Knepley     break;
31159318fe57SMatthew G. Knepley     case DM_POLYTOPE_TRI_PRISM_TENSOR:
31169318fe57SMatthew G. Knepley     {
31179318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {6, 1};
31189318fe57SMatthew G. Knepley       PetscInt    coneSize[7]         = {6, 0, 0, 0, 0, 0, 0};
31199318fe57SMatthew G. Knepley       PetscInt    cones[6]            = {1, 2, 3, 4, 5, 6};
31209318fe57SMatthew G. Knepley       PetscInt    coneOrientations[6] = {0, 0, 0, 0, 0, 0};
31219318fe57SMatthew G. Knepley       PetscScalar vertexCoords[18]    = {-1.0, -1.0, -1.0,  1.0, -1.0, -1.0,  -1.0, 1.0, -1.0,
31229318fe57SMatthew G. Knepley                                          -1.0, -1.0,  1.0,  1.0, -1.0,  1.0,  -1.0, 1.0,  1.0};
31239318fe57SMatthew G. Knepley 
31249566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 3));
31259566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
31269318fe57SMatthew G. Knepley     }
31279318fe57SMatthew G. Knepley     break;
31289318fe57SMatthew G. Knepley     case DM_POLYTOPE_QUAD_PRISM_TENSOR:
31299318fe57SMatthew G. Knepley     {
31309318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {8, 1};
31319318fe57SMatthew G. Knepley       PetscInt    coneSize[9]         = {8, 0, 0, 0, 0, 0, 0, 0, 0};
31329318fe57SMatthew G. Knepley       PetscInt    cones[8]            = {1, 2, 3, 4, 5, 6, 7, 8};
31339318fe57SMatthew G. Knepley       PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
31349318fe57SMatthew G. Knepley       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,
31359318fe57SMatthew G. Knepley                                          -1.0, -1.0,  1.0,  1.0, -1.0,  1.0,  1.0, 1.0,  1.0,  -1.0, 1.0,  1.0};
31369318fe57SMatthew G. Knepley 
31379566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 3));
31389566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
31399318fe57SMatthew G. Knepley     }
31409318fe57SMatthew G. Knepley     break;
31419318fe57SMatthew G. Knepley     case DM_POLYTOPE_PYRAMID:
31429318fe57SMatthew G. Knepley     {
31439318fe57SMatthew G. Knepley       PetscInt    numPoints[2]        = {5, 1};
31449318fe57SMatthew G. Knepley       PetscInt    coneSize[6]         = {5, 0, 0, 0, 0, 0};
3145f0edb160SMatthew G. Knepley       PetscInt    cones[5]            = {1, 2, 3, 4, 5};
31469318fe57SMatthew G. Knepley       PetscInt    coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0};
3147f0edb160SMatthew G. Knepley       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,
31489318fe57SMatthew G. Knepley                                           0.0,  0.0,  1.0};
31499318fe57SMatthew G. Knepley 
31509566063dSJacob Faibussowitsch       PetscCall(DMSetDimension(rdm, 3));
31519566063dSJacob Faibussowitsch       PetscCall(DMPlexCreateFromDAG(rdm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords));
31529318fe57SMatthew G. Knepley     }
31539318fe57SMatthew G. Knepley     break;
315498921bdaSJacob Faibussowitsch     default: SETERRQ(PetscObjectComm((PetscObject) rdm), PETSC_ERR_ARG_WRONG, "Cannot create reference cell for cell type %s", DMPolytopeTypes[ct]);
31559318fe57SMatthew G. Knepley   }
31569318fe57SMatthew G. Knepley   {
31579318fe57SMatthew G. Knepley     PetscInt Nv, v;
31589318fe57SMatthew G. Knepley 
31599318fe57SMatthew G. Knepley     /* Must create the celltype label here so that we do not automatically try to compute the types */
31609566063dSJacob Faibussowitsch     PetscCall(DMCreateLabel(rdm, "celltype"));
31619566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCellType(rdm, 0, ct));
31629566063dSJacob Faibussowitsch     PetscCall(DMPlexGetChart(rdm, NULL, &Nv));
31639566063dSJacob Faibussowitsch     for (v = 1; v < Nv; ++v) PetscCall(DMPlexSetCellType(rdm, v, DM_POLYTOPE_POINT));
31649318fe57SMatthew G. Knepley   }
31659566063dSJacob Faibussowitsch   PetscCall(DMPlexInterpolateInPlace_Internal(rdm));
31669566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject) rdm, DMPolytopeTypes[ct]));
31670a6ba040SMatthew G. Knepley   PetscFunctionReturn(0);
31680a6ba040SMatthew G. Knepley }
31690a6ba040SMatthew G. Knepley 
31709318fe57SMatthew G. Knepley /*@
31719318fe57SMatthew G. Knepley   DMPlexCreateReferenceCell - Create a DMPLEX with the appropriate FEM reference cell
31729318fe57SMatthew G. Knepley 
31739318fe57SMatthew G. Knepley   Collective
31749318fe57SMatthew G. Knepley 
31759318fe57SMatthew G. Knepley   Input Parameters:
31769318fe57SMatthew G. Knepley + comm - The communicator
31779318fe57SMatthew G. Knepley - ct   - The cell type of the reference cell
31789318fe57SMatthew G. Knepley 
31799318fe57SMatthew G. Knepley   Output Parameter:
31809318fe57SMatthew G. Knepley . refdm - The reference cell
31819318fe57SMatthew G. Knepley 
31829318fe57SMatthew G. Knepley   Level: intermediate
31839318fe57SMatthew G. Knepley 
3184db781477SPatrick Sanan .seealso: `DMPlexCreateReferenceCell()`, `DMPlexCreateBoxMesh()`
31859318fe57SMatthew G. Knepley @*/
31869318fe57SMatthew G. Knepley PetscErrorCode DMPlexCreateReferenceCell(MPI_Comm comm, DMPolytopeType ct, DM *refdm)
31870a6ba040SMatthew G. Knepley {
31880a6ba040SMatthew G. Knepley   PetscFunctionBegin;
31899566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, refdm));
31909566063dSJacob Faibussowitsch   PetscCall(DMSetType(*refdm, DMPLEX));
31919566063dSJacob Faibussowitsch   PetscCall(DMPlexCreateReferenceCell_Internal(*refdm, ct));
31929318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
31939318fe57SMatthew G. Knepley }
319479a015ccSMatthew G. Knepley 
31959318fe57SMatthew G. Knepley static PetscErrorCode DMPlexCreateBoundaryLabel_Private(DM dm, const char name[])
31969318fe57SMatthew G. Knepley {
31979318fe57SMatthew G. Knepley   DM             plex;
31989318fe57SMatthew G. Knepley   DMLabel        label;
31999318fe57SMatthew G. Knepley   PetscBool      hasLabel;
32000a6ba040SMatthew G. Knepley 
3201*c22d3578SMatthew G. Knepley   PetscFunctionBegin;
32029566063dSJacob Faibussowitsch   PetscCall(DMHasLabel(dm, name, &hasLabel));
32039318fe57SMatthew G. Knepley   if (hasLabel) PetscFunctionReturn(0);
32049566063dSJacob Faibussowitsch   PetscCall(DMCreateLabel(dm, name));
32059566063dSJacob Faibussowitsch   PetscCall(DMGetLabel(dm, name, &label));
32069566063dSJacob Faibussowitsch   PetscCall(DMConvert(dm, DMPLEX, &plex));
32079566063dSJacob Faibussowitsch   PetscCall(DMPlexMarkBoundaryFaces(plex, 1, label));
32081c8afea9SMatthew G. Knepley   PetscCall(DMPlexLabelComplete(plex, label));
32099566063dSJacob Faibussowitsch   PetscCall(DMDestroy(&plex));
32109318fe57SMatthew G. Knepley   PetscFunctionReturn(0);
32119318fe57SMatthew G. Knepley }
3212acdc6f61SToby Isaac 
321305bd46c0SStefano Zampini const char * const DMPlexShapes[] = {"box", "box_surface", "ball", "sphere", "cylinder", "schwarz_p", "gyroid", "doublet", "unknown", "DMPlexShape", "DM_SHAPE_", NULL};
32149318fe57SMatthew G. Knepley 
321561a622f3SMatthew G. Knepley static PetscErrorCode DMPlexCreateFromOptions_Internal(PetscOptionItems *PetscOptionsObject, PetscBool *useCoordSpace, DM dm)
32169318fe57SMatthew G. Knepley {
32179318fe57SMatthew G. Knepley   DMPlexShape    shape = DM_SHAPE_BOX;
32189318fe57SMatthew G. Knepley   DMPolytopeType cell  = DM_POLYTOPE_TRIANGLE;
32199318fe57SMatthew G. Knepley   PetscInt       dim   = 2;
32209318fe57SMatthew G. Knepley   PetscBool      simplex = PETSC_TRUE, interpolate = PETSC_TRUE, adjCone = PETSC_FALSE, adjClosure = PETSC_TRUE, refDomain = PETSC_FALSE;
3221cd7e8a5eSksagiyam   PetscBool      flg, flg2, fflg, bdfflg, nameflg;
32229318fe57SMatthew G. Knepley   MPI_Comm       comm;
3223ed5e4e85SVaclav Hapla   char           filename[PETSC_MAX_PATH_LEN]   = "<unspecified>";
3224ed5e4e85SVaclav Hapla   char           bdFilename[PETSC_MAX_PATH_LEN] = "<unspecified>";
3225ed5e4e85SVaclav Hapla   char           plexname[PETSC_MAX_PATH_LEN]   = "";
32269318fe57SMatthew G. Knepley 
32279318fe57SMatthew G. Knepley   PetscFunctionBegin;
32289566063dSJacob Faibussowitsch   PetscCall(PetscObjectGetComm((PetscObject) dm, &comm));
32299318fe57SMatthew G. Knepley   /* TODO Turn this into a registration interface */
32309566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_filename", "File containing a mesh", "DMPlexCreateFromFile", filename, filename, sizeof(filename), &fflg));
32319566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_boundary_filename", "File containing a mesh boundary", "DMPlexCreateFromFile", bdFilename, bdFilename, sizeof(bdFilename), &bdfflg));
32329566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_name", "Name of the mesh in the file", "DMPlexCreateFromFile", plexname, plexname, sizeof(plexname), &nameflg));
32339566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_plex_cell", "Cell shape", "", DMPolytopeTypes, (PetscEnum) cell, (PetscEnum *) &cell, NULL));
32349566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_reference_cell_domain", "Use a reference cell domain", "", refDomain, &refDomain, NULL));
32359566063dSJacob Faibussowitsch   PetscCall(PetscOptionsEnum("-dm_plex_shape", "Shape for built-in mesh", "", DMPlexShapes, (PetscEnum) shape, (PetscEnum *) &shape, &flg));
32369566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_dim", "Topological dimension of the mesh", "DMGetDimension", dim, &dim, &flg, 0));
323763a3b9bcSJacob Faibussowitsch   PetscCheck(!(dim < 0) && !(dim > 3),comm, PETSC_ERR_ARG_OUTOFRANGE, "Dimension %" PetscInt_FMT " should be in [1, 3]", dim);
32389566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_simplex", "Mesh cell shape", "", simplex,  &simplex, &flg));
32399566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_interpolate", "Flag to create edges and faces automatically", "", interpolate, &interpolate, &flg));
32409566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_adj_cone", "Set adjacency direction", "DMSetBasicAdjacency", adjCone,  &adjCone, &flg));
32419566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_adj_closure", "Set adjacency size", "DMSetBasicAdjacency", adjClosure,  &adjClosure, &flg2));
32429566063dSJacob Faibussowitsch   if (flg || flg2) PetscCall(DMSetBasicAdjacency(dm, adjCone, adjClosure));
32439318fe57SMatthew G. Knepley 
324461a622f3SMatthew G. Knepley   switch (cell) {
324561a622f3SMatthew G. Knepley     case DM_POLYTOPE_POINT:
324661a622f3SMatthew G. Knepley     case DM_POLYTOPE_SEGMENT:
324761a622f3SMatthew G. Knepley     case DM_POLYTOPE_POINT_PRISM_TENSOR:
324861a622f3SMatthew G. Knepley     case DM_POLYTOPE_TRIANGLE:
324961a622f3SMatthew G. Knepley     case DM_POLYTOPE_QUADRILATERAL:
325061a622f3SMatthew G. Knepley     case DM_POLYTOPE_TETRAHEDRON:
325161a622f3SMatthew G. Knepley     case DM_POLYTOPE_HEXAHEDRON:
325261a622f3SMatthew G. Knepley       *useCoordSpace = PETSC_TRUE;break;
325361a622f3SMatthew G. Knepley     default: *useCoordSpace = PETSC_FALSE;break;
325461a622f3SMatthew G. Knepley   }
325561a622f3SMatthew G. Knepley 
32569318fe57SMatthew G. Knepley   if (fflg) {
32579318fe57SMatthew G. Knepley     DM dmnew;
32589318fe57SMatthew G. Knepley 
32599566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject) dm), filename, plexname, interpolate, &dmnew));
32605de52c6dSVaclav Hapla     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
32619566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(dm, &dmnew));
32629318fe57SMatthew G. Knepley   } else if (refDomain) {
32639566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateReferenceCell_Internal(dm, cell));
32649318fe57SMatthew G. Knepley   } else if (bdfflg) {
32659318fe57SMatthew G. Knepley     DM bdm, dmnew;
32669318fe57SMatthew G. Knepley 
32679566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFromFile(PetscObjectComm((PetscObject) dm), bdFilename, plexname, interpolate, &bdm));
32689566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject) bdm, "bd_"));
32699566063dSJacob Faibussowitsch     PetscCall(DMSetFromOptions(bdm));
32709566063dSJacob Faibussowitsch     PetscCall(DMPlexGenerate(bdm, NULL, interpolate, &dmnew));
32719566063dSJacob Faibussowitsch     PetscCall(DMDestroy(&bdm));
32725de52c6dSVaclav Hapla     PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
32739566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(dm, &dmnew));
32749318fe57SMatthew G. Knepley   } else {
32759566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject) dm, DMPlexShapes[shape]));
32769318fe57SMatthew G. Knepley     switch (shape) {
32779318fe57SMatthew G. Knepley       case DM_SHAPE_BOX:
32789318fe57SMatthew G. Knepley       {
32799318fe57SMatthew G. Knepley         PetscInt       faces[3] = {0, 0, 0};
32809318fe57SMatthew G. Knepley         PetscReal      lower[3] = {0, 0, 0};
32819318fe57SMatthew G. Knepley         PetscReal      upper[3] = {1, 1, 1};
32829318fe57SMatthew G. Knepley         DMBoundaryType bdt[3]   = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
32839318fe57SMatthew G. Knepley         PetscInt       i, n;
32849318fe57SMatthew G. Knepley 
32859318fe57SMatthew G. Knepley         n    = dim;
32869318fe57SMatthew G. Knepley         for (i = 0; i < dim; ++i) faces[i] = (dim == 1 ? 1 : 4-dim);
32879566063dSJacob Faibussowitsch         PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg));
32889318fe57SMatthew G. Knepley         n    = 3;
32899566063dSJacob Faibussowitsch         PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
329063a3b9bcSJacob Faibussowitsch         PetscCheck(!flg || !(n != dim),comm, PETSC_ERR_ARG_SIZ, "Lower box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
32919318fe57SMatthew G. Knepley         n    = 3;
32929566063dSJacob Faibussowitsch         PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
329363a3b9bcSJacob Faibussowitsch         PetscCheck(!flg || !(n != dim),comm, PETSC_ERR_ARG_SIZ, "Upper box point had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
32949318fe57SMatthew G. Knepley         n    = 3;
32959566063dSJacob Faibussowitsch         PetscCall(PetscOptionsEnumArray("-dm_plex_box_bd", "Boundary type for each dimension", "", DMBoundaryTypes, (PetscEnum *) bdt, &n, &flg));
329663a3b9bcSJacob Faibussowitsch         PetscCheck(!flg || !(n != dim),comm, PETSC_ERR_ARG_SIZ, "Box boundary types had %" PetscInt_FMT " values, should have been %" PetscInt_FMT, n, dim);
32979318fe57SMatthew G. Knepley         switch (cell) {
329861a622f3SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
32999566063dSJacob Faibussowitsch             PetscCall(DMPlexCreateWedgeBoxMesh_Internal(dm, faces, lower, upper, bdt));
3300d410b0cfSMatthew G. Knepley             if (!interpolate) {
3301d410b0cfSMatthew G. Knepley               DM udm;
3302d410b0cfSMatthew G. Knepley 
33039566063dSJacob Faibussowitsch               PetscCall(DMPlexUninterpolate(dm, &udm));
33049566063dSJacob Faibussowitsch               PetscCall(DMPlexReplace_Static(dm, &udm));
3305d410b0cfSMatthew G. Knepley             }
33069318fe57SMatthew G. Knepley             break;
33079318fe57SMatthew G. Knepley           default:
33089566063dSJacob Faibussowitsch             PetscCall(DMPlexCreateBoxMesh_Internal(dm, dim, simplex, faces, lower, upper, bdt, interpolate));
33099318fe57SMatthew G. Knepley             break;
33109318fe57SMatthew G. Knepley         }
33119318fe57SMatthew G. Knepley       }
33129318fe57SMatthew G. Knepley       break;
33139318fe57SMatthew G. Knepley       case DM_SHAPE_BOX_SURFACE:
33149318fe57SMatthew G. Knepley       {
33159318fe57SMatthew G. Knepley         PetscInt  faces[3] = {0, 0, 0};
33169318fe57SMatthew G. Knepley         PetscReal lower[3] = {0, 0, 0};
33179318fe57SMatthew G. Knepley         PetscReal upper[3] = {1, 1, 1};
33189318fe57SMatthew G. Knepley         PetscInt  i, n;
33199318fe57SMatthew G. Knepley 
33209318fe57SMatthew G. Knepley         n    = dim+1;
33219318fe57SMatthew G. Knepley         for (i = 0; i < dim+1; ++i) faces[i] = (dim+1 == 1 ? 1 : 4-(dim+1));
33229566063dSJacob Faibussowitsch         PetscCall(PetscOptionsIntArray("-dm_plex_box_faces", "Number of faces along each dimension", "", faces, &n, &flg));
33239318fe57SMatthew G. Knepley         n    = 3;
33249566063dSJacob Faibussowitsch         PetscCall(PetscOptionsRealArray("-dm_plex_box_lower", "Lower left corner of box", "", lower, &n, &flg));
332563a3b9bcSJacob 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);
33269318fe57SMatthew G. Knepley         n    = 3;
33279566063dSJacob Faibussowitsch         PetscCall(PetscOptionsRealArray("-dm_plex_box_upper", "Upper right corner of box", "", upper, &n, &flg));
332863a3b9bcSJacob 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);
33299566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateBoxSurfaceMesh_Internal(dm, dim+1, faces, lower, upper, interpolate));
33309318fe57SMatthew G. Knepley       }
33319318fe57SMatthew G. Knepley       break;
33329318fe57SMatthew G. Knepley       case DM_SHAPE_SPHERE:
33339318fe57SMatthew G. Knepley       {
33349318fe57SMatthew G. Knepley         PetscReal R = 1.0;
33359318fe57SMatthew G. Knepley 
33369566063dSJacob Faibussowitsch         PetscCall(PetscOptionsReal("-dm_plex_sphere_radius", "Radius of the sphere", "", R,  &R, &flg));
33379566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateSphereMesh_Internal(dm, dim, simplex, R));
33389318fe57SMatthew G. Knepley       }
33399318fe57SMatthew G. Knepley       break;
33409318fe57SMatthew G. Knepley       case DM_SHAPE_BALL:
33419318fe57SMatthew G. Knepley       {
33429318fe57SMatthew G. Knepley         PetscReal R = 1.0;
33439318fe57SMatthew G. Knepley 
33449566063dSJacob Faibussowitsch         PetscCall(PetscOptionsReal("-dm_plex_ball_radius", "Radius of the ball", "", R,  &R, &flg));
33459566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateBallMesh_Internal(dm, dim, R));
33469318fe57SMatthew G. Knepley       }
33479318fe57SMatthew G. Knepley       break;
33489318fe57SMatthew G. Knepley       case DM_SHAPE_CYLINDER:
33499318fe57SMatthew G. Knepley       {
33509318fe57SMatthew G. Knepley         DMBoundaryType bdt = DM_BOUNDARY_NONE;
33519318fe57SMatthew G. Knepley         PetscInt       Nw  = 6;
33529318fe57SMatthew G. Knepley 
33539566063dSJacob Faibussowitsch         PetscCall(PetscOptionsEnum("-dm_plex_cylinder_bd", "Boundary type in the z direction", "", DMBoundaryTypes, (PetscEnum) bdt, (PetscEnum *) &bdt, NULL));
33549566063dSJacob Faibussowitsch         PetscCall(PetscOptionsInt("-dm_plex_cylinder_num_wedges", "Number of wedges around the cylinder", "", Nw, &Nw, NULL));
33559318fe57SMatthew G. Knepley         switch (cell) {
335661a622f3SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
33579566063dSJacob Faibussowitsch             PetscCall(DMPlexCreateWedgeCylinderMesh_Internal(dm, Nw, interpolate));
33589318fe57SMatthew G. Knepley             break;
33599318fe57SMatthew G. Knepley           default:
33609566063dSJacob Faibussowitsch             PetscCall(DMPlexCreateHexCylinderMesh_Internal(dm, bdt));
33619318fe57SMatthew G. Knepley             break;
33629318fe57SMatthew G. Knepley         }
33639318fe57SMatthew G. Knepley       }
33649318fe57SMatthew G. Knepley       break;
3365b7f5c055SJed Brown       case DM_SHAPE_SCHWARZ_P: // fallthrough
3366b7f5c055SJed Brown       case DM_SHAPE_GYROID:
3367b7f5c055SJed Brown       {
3368b7f5c055SJed Brown         PetscInt       extent[3] = {1,1,1}, refine = 0, layers = 0, three;
3369b7f5c055SJed Brown         PetscReal      thickness = 0.;
3370b7f5c055SJed Brown         DMBoundaryType periodic[3] = {DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE};
3371b7f5c055SJed Brown         DMPlexTPSType  tps_type = shape == DM_SHAPE_SCHWARZ_P ? DMPLEX_TPS_SCHWARZ_P : DMPLEX_TPS_GYROID;
33721436d7faSJed Brown         PetscBool      tps_distribute;
33739566063dSJacob Faibussowitsch         PetscCall(PetscOptionsIntArray("-dm_plex_tps_extent", "Number of replicas for each of three dimensions", NULL, extent, (three=3, &three), NULL));
33749566063dSJacob Faibussowitsch         PetscCall(PetscOptionsInt("-dm_plex_tps_refine", "Number of refinements", NULL, refine, &refine, NULL));
33759566063dSJacob Faibussowitsch         PetscCall(PetscOptionsEnumArray("-dm_plex_tps_periodic", "Periodicity in each of three dimensions", NULL, DMBoundaryTypes, (PetscEnum*)periodic, (three=3, &three), NULL));
33769566063dSJacob Faibussowitsch         PetscCall(PetscOptionsInt("-dm_plex_tps_layers", "Number of layers in volumetric extrusion (or zero to not extrude)", NULL, layers, &layers, NULL));
33779566063dSJacob Faibussowitsch         PetscCall(PetscOptionsReal("-dm_plex_tps_thickness", "Thickness of volumetric extrusion", NULL, thickness, &thickness, NULL));
33789566063dSJacob Faibussowitsch         PetscCall(DMPlexDistributeGetDefault(dm, &tps_distribute));
33799566063dSJacob Faibussowitsch         PetscCall(PetscOptionsBool("-dm_plex_tps_distribute", "Distribute the 2D mesh prior to refinement and extrusion", NULL, tps_distribute, &tps_distribute, NULL));
33809566063dSJacob Faibussowitsch         PetscCall(DMPlexCreateTPSMesh_Internal(dm, tps_type, extent, periodic, tps_distribute, refine, layers, thickness));
3381b7f5c055SJed Brown       }
3382b7f5c055SJed Brown       break;
338305bd46c0SStefano Zampini       case DM_SHAPE_DOUBLET:
338405bd46c0SStefano Zampini       {
338505bd46c0SStefano Zampini         DM        dmnew;
338605bd46c0SStefano Zampini         PetscReal rl = 0.0;
338705bd46c0SStefano Zampini 
338805bd46c0SStefano Zampini         PetscCall(PetscOptionsReal("-dm_plex_doublet_refinementlimit", "Refinement limit", NULL, rl, &rl, NULL));
338905bd46c0SStefano Zampini         PetscCall(DMPlexCreateDoublet(PetscObjectComm((PetscObject)dm), dim, simplex, interpolate, rl, &dmnew));
33905de52c6dSVaclav Hapla         PetscCall(DMPlexCopy_Internal(dm, PETSC_FALSE, PETSC_FALSE, dmnew));
339105bd46c0SStefano Zampini         PetscCall(DMPlexReplace_Static(dm, &dmnew));
339205bd46c0SStefano Zampini       }
339305bd46c0SStefano Zampini       break;
339498921bdaSJacob Faibussowitsch       default: SETERRQ(comm, PETSC_ERR_SUP, "Domain shape %s is unsupported", DMPlexShapes[shape]);
33959318fe57SMatthew G. Knepley     }
33969318fe57SMatthew G. Knepley   }
33979566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
3398ed5e4e85SVaclav Hapla   if (!((PetscObject)dm)->name && nameflg) {
33999566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)dm, plexname));
3400ed5e4e85SVaclav Hapla   }
34010a6ba040SMatthew G. Knepley   PetscFunctionReturn(0);
34020a6ba040SMatthew G. Knepley }
34030a6ba040SMatthew G. Knepley 
340447920aaeSMatthew G. Knepley PetscErrorCode DMSetFromOptions_NonRefinement_Plex(PetscOptionItems *PetscOptionsObject, DM dm)
34050a6ba040SMatthew G. Knepley {
34060a6ba040SMatthew G. Knepley   DM_Plex       *mesh = (DM_Plex*) dm->data;
34077f9d8d6cSVaclav Hapla   PetscBool      flg, flg2;
34089318fe57SMatthew G. Knepley   char           bdLabel[PETSC_MAX_PATH_LEN];
34090a6ba040SMatthew G. Knepley 
34100a6ba040SMatthew G. Knepley   PetscFunctionBegin;
34110a6ba040SMatthew G. Knepley   /* Handle viewing */
34129566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_print_set_values", "Output all set values info", "DMPlexMatSetClosure", PETSC_FALSE, &mesh->printSetValues, NULL));
34139566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_fem", "Debug output level all fem computations", "DMPlexSNESComputeResidualFEM", 0, &mesh->printFEM, NULL,0));
34149566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-dm_plex_print_tol", "Tolerance for FEM output", "DMPlexSNESComputeResidualFEM", mesh->printTol, &mesh->printTol, NULL));
34159566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_print_l2", "Debug output level all L2 diff computations", "DMComputeL2Diff", 0, &mesh->printL2, NULL,0));
34169566063dSJacob Faibussowitsch   PetscCall(DMMonitorSetFromOptions(dm, "-dm_plex_monitor_throughput", "Monitor the simulation throughput", "DMPlexMonitorThroughput", DMPlexMonitorThroughput, NULL, &flg));
34179566063dSJacob Faibussowitsch   if (flg) PetscCall(PetscLogDefaultBegin());
34189318fe57SMatthew G. Knepley   /* Labeling */
34199566063dSJacob Faibussowitsch   PetscCall(PetscOptionsString("-dm_plex_boundary_label", "Label to mark the mesh boundary", "", bdLabel, bdLabel, sizeof(bdLabel), &flg));
34209566063dSJacob Faibussowitsch   if (flg) PetscCall(DMPlexCreateBoundaryLabel_Private(dm, bdLabel));
3421953fc75cSMatthew G. Knepley   /* Point Location */
34229566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_hash_location", "Use grid hashing for point location", "DMInterpolate", PETSC_FALSE, &mesh->useHashLocation, NULL));
34230848f4b5SMatthew G. Knepley   /* Partitioning and distribution */
34249566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_partition_balance", "Attempt to evenly divide points on partition boundary between processes", "DMPlexSetPartitionBalance", PETSC_FALSE, &mesh->partitionBalance, NULL));
34252e62ab5aSMatthew G. Knepley   /* Generation and remeshing */
34269566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_remesh_bd", "Allow changes to the boundary on remeshing", "DMAdapt", PETSC_FALSE, &mesh->remeshBd, NULL));
3427b29cfa1cSToby Isaac   /* Projection behavior */
34289566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_plex_max_projection_height", "Maxmimum mesh point height used to project locally", "DMPlexSetMaxProjectionHeight", 0, &mesh->maxProjectionHeight, NULL,0));
34299566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_regular_refinement", "Use special nested projection algorithm for regular refinement", "DMPlexSetRegularRefinement", mesh->regularRefinement, &mesh->regularRefinement, NULL));
3430f12cf164SMatthew G. Knepley   /* Checking structure */
3431f12cf164SMatthew G. Knepley   {
34327f9d8d6cSVaclav Hapla     PetscBool   all = PETSC_FALSE;
3433f12cf164SMatthew G. Knepley 
34347f9d8d6cSVaclav Hapla     PetscCall(PetscOptionsBool("-dm_plex_check_all", "Perform all basic checks", "DMPlexCheck", PETSC_FALSE, &all, NULL));
34357f9d8d6cSVaclav Hapla     if (all) {
34367f9d8d6cSVaclav Hapla       PetscCall(DMPlexCheck(dm));
34377f9d8d6cSVaclav Hapla     } else {
34389566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_symmetry", "Check that the adjacency information in the mesh is symmetric", "DMPlexCheckSymmetry", PETSC_FALSE, &flg, &flg2));
34397f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckSymmetry(dm));
34409566063dSJacob 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));
34417f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckSkeleton(dm, 0));
34429566063dSJacob 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));
34437f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckFaces(dm, 0));
34449566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_geometry", "Check that cells have positive volume", "DMPlexCheckGeometry", PETSC_FALSE, &flg, &flg2));
34457f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckGeometry(dm));
34469566063dSJacob Faibussowitsch       PetscCall(PetscOptionsBool("-dm_plex_check_pointsf", "Check some necessary conditions for PointSF", "DMPlexCheckPointSF", PETSC_FALSE, &flg, &flg2));
34477f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckPointSF(dm, NULL));
34489566063dSJacob 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));
34497f9d8d6cSVaclav Hapla       if (flg && flg2) PetscCall(DMPlexCheckInterfaceCones(dm));
34507f9d8d6cSVaclav Hapla     }
34519566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-dm_plex_check_cell_shape", "Check cell shape", "DMPlexCheckCellShape", PETSC_FALSE, &flg, &flg2));
34529566063dSJacob Faibussowitsch     if (flg && flg2) PetscCall(DMPlexCheckCellShape(dm, PETSC_TRUE, PETSC_DETERMINE));
3453f12cf164SMatthew G. Knepley   }
34549318fe57SMatthew G. Knepley   {
34559318fe57SMatthew G. Knepley     PetscReal scale = 1.0;
34564f3833eaSMatthew G. Knepley 
34579566063dSJacob Faibussowitsch     PetscCall(PetscOptionsReal("-dm_plex_scale", "Scale factor for mesh coordinates", "DMPlexScale", scale, &scale, &flg));
34589318fe57SMatthew G. Knepley     if (flg) {
34599318fe57SMatthew G. Knepley       Vec coordinates, coordinatesLocal;
34609318fe57SMatthew G. Knepley 
34619566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinates(dm, &coordinates));
34629566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinatesLocal(dm, &coordinatesLocal));
34639566063dSJacob Faibussowitsch       PetscCall(VecScale(coordinates, scale));
34649566063dSJacob Faibussowitsch       PetscCall(VecScale(coordinatesLocal, scale));
34659318fe57SMatthew G. Knepley     }
34669318fe57SMatthew G. Knepley   }
34679566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerSetFromOptions(mesh->partitioner));
346868d4fef7SMatthew G. Knepley   PetscFunctionReturn(0);
346968d4fef7SMatthew G. Knepley }
347068d4fef7SMatthew G. Knepley 
3471c506a872SMatthew G. Knepley PetscErrorCode DMSetFromOptions_Overlap_Plex(PetscOptionItems *PetscOptionsObject, DM dm, PetscInt *overlap)
3472c506a872SMatthew G. Knepley {
3473c506a872SMatthew G. Knepley   PetscInt  numOvLabels = 16, numOvExLabels = 16;
3474c506a872SMatthew G. Knepley   char     *ovLabelNames[16], *ovExLabelNames[16];
3475c506a872SMatthew G. Knepley   PetscInt  numOvValues = 16, numOvExValues = 16, l;
3476c506a872SMatthew G. Knepley   PetscBool flg;
3477c506a872SMatthew G. Knepley 
3478c506a872SMatthew G. Knepley   PetscFunctionBegin;
3479c506a872SMatthew G. Knepley   PetscCall(PetscOptionsBoundedInt("-dm_distribute_overlap", "The size of the overlap halo", "DMPlexDistribute", *overlap, overlap, NULL, 0));
3480c506a872SMatthew G. Knepley   PetscCall(PetscOptionsStringArray("-dm_distribute_overlap_labels", "List of overlap label names", "DMPlexDistribute", ovLabelNames, &numOvLabels, &flg));
3481c506a872SMatthew G. Knepley   if (!flg) numOvLabels = 0;
3482c506a872SMatthew G. Knepley   if (numOvLabels) {
3483c506a872SMatthew G. Knepley     ((DM_Plex*) dm->data)->numOvLabels = numOvLabels;
3484c506a872SMatthew G. Knepley     for (l = 0; l < numOvLabels; ++l) {
3485c506a872SMatthew G. Knepley       PetscCall(DMGetLabel(dm, ovLabelNames[l], &((DM_Plex*) dm->data)->ovLabels[l]));
3486c506a872SMatthew G. Knepley       PetscCheck(((DM_Plex*) dm->data)->ovLabels[l], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid label name %s", ovLabelNames[l]);
3487c506a872SMatthew G. Knepley       PetscCall(PetscFree(ovLabelNames[l]));
3488c506a872SMatthew G. Knepley     }
3489c506a872SMatthew G. Knepley     PetscCall(PetscOptionsIntArray("-dm_distribute_overlap_values", "List of overlap label values", "DMPlexDistribute", ((DM_Plex*) dm->data)->ovValues, &numOvValues, &flg));
3490c506a872SMatthew G. Knepley     if (!flg) numOvValues = 0;
3491c506a872SMatthew 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);
3492c506a872SMatthew G. Knepley 
3493c506a872SMatthew G. Knepley     PetscCall(PetscOptionsStringArray("-dm_distribute_overlap_exclude_labels", "List of overlap exclude label names", "DMPlexDistribute", ovExLabelNames, &numOvExLabels, &flg));
3494c506a872SMatthew G. Knepley     if (!flg) numOvExLabels = 0;
3495c506a872SMatthew G. Knepley     ((DM_Plex*) dm->data)->numOvExLabels = numOvExLabels;
3496c506a872SMatthew G. Knepley     for (l = 0; l < numOvExLabels; ++l) {
3497c506a872SMatthew G. Knepley       PetscCall(DMGetLabel(dm, ovExLabelNames[l], &((DM_Plex*) dm->data)->ovExLabels[l]));
3498c506a872SMatthew G. Knepley       PetscCheck(((DM_Plex*) dm->data)->ovExLabels[l], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid label name %s", ovExLabelNames[l]);
3499c506a872SMatthew G. Knepley       PetscCall(PetscFree(ovExLabelNames[l]));
3500c506a872SMatthew G. Knepley     }
3501c506a872SMatthew G. Knepley     PetscCall(PetscOptionsIntArray("-dm_distribute_overlap_exclude_values", "List of overlap exclude label values", "DMPlexDistribute", ((DM_Plex*) dm->data)->ovExValues, &numOvExValues, &flg));
3502c506a872SMatthew G. Knepley     if (!flg) numOvExValues = 0;
3503c506a872SMatthew 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);
3504c506a872SMatthew G. Knepley   }
3505c506a872SMatthew G. Knepley   PetscFunctionReturn(0);
3506c506a872SMatthew G. Knepley }
3507c506a872SMatthew G. Knepley 
350846fa42a0SMatthew G. Knepley static PetscErrorCode DMSetFromOptions_Plex(PetscOptionItems *PetscOptionsObject,DM dm)
350968d4fef7SMatthew G. Knepley {
3510bdf63967SMatthew G. Knepley   PetscFunctionList ordlist;
3511bdf63967SMatthew G. Knepley   char                      oname[256];
3512d410b0cfSMatthew G. Knepley   PetscReal                 volume = -1.0;
35139318fe57SMatthew G. Knepley   PetscInt                  prerefine = 0, refine = 0, r, coarsen = 0, overlap = 0, extLayers = 0, dim;
3514e600fa54SMatthew 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;
35156bc1bd01Sksagiyam   DMPlexReorderDefaultFlag  reorder;
351668d4fef7SMatthew G. Knepley 
351768d4fef7SMatthew G. Knepley   PetscFunctionBegin;
3518064a246eSJacob Faibussowitsch   PetscValidHeaderSpecific(dm, DM_CLASSID, 2);
3519d0609cedSBarry Smith   PetscOptionsHeadBegin(PetscOptionsObject,"DMPlex Options");
35209318fe57SMatthew G. Knepley   /* Handle automatic creation */
35219566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
35226bc1bd01Sksagiyam   if (dim < 0) {
35236bc1bd01Sksagiyam     PetscCall(DMPlexCreateFromOptions_Internal(PetscOptionsObject, &coordSpace, dm));
35246bc1bd01Sksagiyam     created = PETSC_TRUE;
35256bc1bd01Sksagiyam   }
35266bc1bd01Sksagiyam   PetscCall(DMGetDimension(dm, &dim));
3527d89e6e46SMatthew G. Knepley   /* Handle interpolation before distribution */
35289566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_interpolate_pre", "Flag to interpolate mesh before distribution", "", interpolate, &interpolate, &flg));
3529d89e6e46SMatthew G. Knepley   if (flg) {
3530d89e6e46SMatthew G. Knepley     DMPlexInterpolatedFlag interpolated;
3531d89e6e46SMatthew G. Knepley 
35329566063dSJacob Faibussowitsch     PetscCall(DMPlexIsInterpolated(dm, &interpolated));
3533d89e6e46SMatthew G. Knepley     if (interpolated == DMPLEX_INTERPOLATED_FULL && !interpolate) {
3534d89e6e46SMatthew G. Knepley       DM udm;
3535d89e6e46SMatthew G. Knepley 
35369566063dSJacob Faibussowitsch       PetscCall(DMPlexUninterpolate(dm, &udm));
35379566063dSJacob Faibussowitsch       PetscCall(DMPlexReplace_Static(dm, &udm));
3538d89e6e46SMatthew G. Knepley     } else if (interpolated != DMPLEX_INTERPOLATED_FULL && interpolate) {
3539d89e6e46SMatthew G. Knepley       DM idm;
3540d89e6e46SMatthew G. Knepley 
35419566063dSJacob Faibussowitsch       PetscCall(DMPlexInterpolate(dm, &idm));
35429566063dSJacob Faibussowitsch       PetscCall(DMPlexReplace_Static(dm, &idm));
3543d89e6e46SMatthew G. Knepley     }
3544d89e6e46SMatthew G. Knepley   }
35459b44eab4SMatthew G. Knepley   /* Handle DMPlex refinement before distribution */
35469566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_ignore_model", "Flag to ignore the geometry model when refining", "DMCreate", ignoreModel, &ignoreModel, &flg));
3547c1cad2e7SMatthew G. Knepley   if (flg) {((DM_Plex *) dm->data)->ignoreModel = ignoreModel;}
35489566063dSJacob Faibussowitsch   PetscCall(DMPlexGetRefinementUniform(dm, &uniformOrig));
35499566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine_pre", "The number of refinements before distribution", "DMCreate", prerefine, &prerefine, NULL,0));
35509566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_remap_pre", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL));
35519566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_uniform_pre", "Flag for uniform refinement before distribution", "DMCreate", uniform, &uniform, &flg));
35529566063dSJacob Faibussowitsch   if (flg) PetscCall(DMPlexSetRefinementUniform(dm, uniform));
35539566063dSJacob Faibussowitsch   PetscCall(PetscOptionsReal("-dm_refine_volume_limit_pre", "The maximum cell volume after refinement before distribution", "DMCreate", volume, &volume, &flg));
35549318fe57SMatthew G. Knepley   if (flg) {
35559566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementUniform(dm, PETSC_FALSE));
35569566063dSJacob Faibussowitsch     PetscCall(DMPlexSetRefinementLimit(dm, volume));
35579318fe57SMatthew G. Knepley     prerefine = PetscMax(prerefine, 1);
35589318fe57SMatthew G. Knepley   }
35599b44eab4SMatthew G. Knepley   for (r = 0; r < prerefine; ++r) {
35609b44eab4SMatthew G. Knepley     DM             rdm;
35619b44eab4SMatthew G. Knepley     PetscPointFunc coordFunc = ((DM_Plex*) dm->data)->coordFunc;
35629b44eab4SMatthew G. Knepley 
35639566063dSJacob Faibussowitsch     PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm));
35649566063dSJacob Faibussowitsch     PetscCall(DMRefine(dm, PetscObjectComm((PetscObject) dm), &rdm));
35659566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(dm, &rdm));
35669566063dSJacob Faibussowitsch     PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm));
356761a622f3SMatthew G. Knepley     if (coordFunc && remap) {
35689566063dSJacob Faibussowitsch       PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
35699b44eab4SMatthew G. Knepley       ((DM_Plex*) dm->data)->coordFunc = coordFunc;
35709b44eab4SMatthew G. Knepley     }
35719b44eab4SMatthew G. Knepley   }
35729566063dSJacob Faibussowitsch   PetscCall(DMPlexSetRefinementUniform(dm, uniformOrig));
35739318fe57SMatthew G. Knepley   /* Handle DMPlex extrusion before distribution */
35749566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_extrude", "The number of layers to extrude", "", extLayers, &extLayers, NULL, 0));
35759318fe57SMatthew G. Knepley   if (extLayers) {
35769318fe57SMatthew G. Knepley     DM edm;
35779318fe57SMatthew G. Knepley 
35789566063dSJacob Faibussowitsch     PetscCall(DMExtrude(dm, extLayers, &edm));
35799566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(dm, &edm));
358048d16a33SMatthew G. Knepley     ((DM_Plex *) dm->data)->coordFunc = NULL;
35819566063dSJacob Faibussowitsch     PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm));
3582d410b0cfSMatthew G. Knepley     extLayers = 0;
35839318fe57SMatthew G. Knepley   }
3584bdf63967SMatthew G. Knepley   /* Handle DMPlex reordering before distribution */
35856bc1bd01Sksagiyam   PetscCall(DMPlexReorderGetDefault(dm, &reorder));
35869566063dSJacob Faibussowitsch   PetscCall(MatGetOrderingList(&ordlist));
35876bc1bd01Sksagiyam   PetscCall(PetscStrncpy(oname, MATORDERINGNATURAL, sizeof(oname)));
35889566063dSJacob Faibussowitsch   PetscCall(PetscOptionsFList("-dm_plex_reorder", "Set mesh reordering type", "DMPlexGetOrdering", ordlist, MATORDERINGNATURAL, oname, sizeof(oname), &flg));
35896bc1bd01Sksagiyam   if (reorder == DMPLEX_REORDER_DEFAULT_TRUE || flg) {
3590bdf63967SMatthew G. Knepley     DM pdm;
3591bdf63967SMatthew G. Knepley     IS perm;
3592bdf63967SMatthew G. Knepley 
35939566063dSJacob Faibussowitsch     PetscCall(DMPlexGetOrdering(dm, oname, NULL, &perm));
35949566063dSJacob Faibussowitsch     PetscCall(DMPlexPermute(dm, perm, &pdm));
35959566063dSJacob Faibussowitsch     PetscCall(ISDestroy(&perm));
35969566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(dm, &pdm));
35979566063dSJacob Faibussowitsch     PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm));
3598bdf63967SMatthew G. Knepley   }
35999b44eab4SMatthew G. Knepley   /* Handle DMPlex distribution */
36009566063dSJacob Faibussowitsch   PetscCall(DMPlexDistributeGetDefault(dm, &distribute));
3601c506a872SMatthew G. Knepley   PetscCall(PetscOptionsBool("-dm_distribute", "Flag to redistribute a mesh among processes", "DMPlexDistribute", distribute, &distribute, NULL));
3602c506a872SMatthew G. Knepley   PetscCall(DMSetFromOptions_Overlap_Plex(PetscOptionsObject, dm, &overlap));
36039b44eab4SMatthew G. Knepley   if (distribute) {
36049b44eab4SMatthew G. Knepley     DM               pdm = NULL;
36059b44eab4SMatthew G. Knepley     PetscPartitioner part;
36069b44eab4SMatthew G. Knepley 
36079566063dSJacob Faibussowitsch     PetscCall(DMPlexGetPartitioner(dm, &part));
36089566063dSJacob Faibussowitsch     PetscCall(PetscPartitionerSetFromOptions(part));
36099566063dSJacob Faibussowitsch     PetscCall(DMPlexDistribute(dm, overlap, NULL, &pdm));
36109b44eab4SMatthew G. Knepley     if (pdm) {
36119566063dSJacob Faibussowitsch       PetscCall(DMPlexReplace_Static(dm, &pdm));
36129b44eab4SMatthew G. Knepley     }
36139b44eab4SMatthew G. Knepley   }
36149318fe57SMatthew G. Knepley   /* Create coordinate space */
36159318fe57SMatthew G. Knepley   if (created) {
361661a622f3SMatthew G. Knepley     DM_Plex  *mesh = (DM_Plex *) dm->data;
36179318fe57SMatthew G. Knepley     PetscInt  degree = 1;
36186858538eSMatthew G. Knepley     PetscBool flg;
36199318fe57SMatthew G. Knepley 
36209566063dSJacob Faibussowitsch     PetscCall(PetscOptionsBool("-dm_coord_space", "Use an FEM space for coordinates", "", coordSpace, &coordSpace, &flg));
36219566063dSJacob Faibussowitsch     PetscCall(PetscOptionsInt("-dm_coord_petscspace_degree", "FEM degree for coordinate space", "", degree, &degree, NULL));
36229566063dSJacob Faibussowitsch     if (coordSpace) PetscCall(DMPlexCreateCoordinateSpace(dm, degree, mesh->coordFunc));
362361a622f3SMatthew G. Knepley     if (flg && !coordSpace) {
362461a622f3SMatthew G. Knepley       DM           cdm;
362561a622f3SMatthew G. Knepley       PetscDS      cds;
362661a622f3SMatthew G. Knepley       PetscObject  obj;
362761a622f3SMatthew G. Knepley       PetscClassId id;
362861a622f3SMatthew G. Knepley 
36299566063dSJacob Faibussowitsch       PetscCall(DMGetCoordinateDM(dm, &cdm));
36309566063dSJacob Faibussowitsch       PetscCall(DMGetDS(cdm, &cds));
36319566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
36329566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
363361a622f3SMatthew G. Knepley       if (id == PETSCFE_CLASSID) {
363461a622f3SMatthew G. Knepley         PetscContainer dummy;
363561a622f3SMatthew G. Knepley 
36369566063dSJacob Faibussowitsch         PetscCall(PetscContainerCreate(PETSC_COMM_SELF, &dummy));
36379566063dSJacob Faibussowitsch         PetscCall(PetscObjectSetName((PetscObject) dummy, "coordinates"));
36389566063dSJacob Faibussowitsch         PetscCall(DMSetField(cdm, 0, NULL, (PetscObject) dummy));
36399566063dSJacob Faibussowitsch         PetscCall(PetscContainerDestroy(&dummy));
36409566063dSJacob Faibussowitsch         PetscCall(DMClearDS(cdm));
364161a622f3SMatthew G. Knepley       }
364261a622f3SMatthew G. Knepley       mesh->coordFunc = NULL;
364361a622f3SMatthew G. Knepley     }
36446858538eSMatthew G. Knepley     PetscCall(PetscOptionsBool("-dm_sparse_localize", "Localize only necessary cells", "", dm->sparseLocalize, &dm->sparseLocalize, &flg));
36459566063dSJacob Faibussowitsch     PetscCall(DMLocalizeCoordinates(dm));
36469318fe57SMatthew G. Knepley   }
364768d4fef7SMatthew G. Knepley   /* Handle DMPlex refinement */
364861a622f3SMatthew G. Knepley   remap = PETSC_TRUE;
36499566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine", "The number of uniform refinements", "DMCreate", refine, &refine, NULL,0));
36509566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_refine_remap", "Flag to control coordinate remapping", "DMCreate", remap, &remap, NULL));
36519566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_refine_hierarchy", "The number of uniform refinements", "DMCreate", refine, &refine, &isHierarchy,0));
36529566063dSJacob Faibussowitsch   if (refine) PetscCall(DMPlexSetRefinementUniform(dm, PETSC_TRUE));
365368d4fef7SMatthew G. Knepley   if (refine && isHierarchy) {
3654acdc6f61SToby Isaac     DM *dms, coarseDM;
365568d4fef7SMatthew G. Knepley 
36569566063dSJacob Faibussowitsch     PetscCall(DMGetCoarseDM(dm, &coarseDM));
36579566063dSJacob Faibussowitsch     PetscCall(PetscObjectReference((PetscObject)coarseDM));
36589566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(refine,&dms));
36599566063dSJacob Faibussowitsch     PetscCall(DMRefineHierarchy(dm, refine, dms));
366068d4fef7SMatthew G. Knepley     /* Total hack since we do not pass in a pointer */
36619566063dSJacob Faibussowitsch     PetscCall(DMPlexSwap_Static(dm, dms[refine-1]));
366268d4fef7SMatthew G. Knepley     if (refine == 1) {
36639566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dm, dms[0]));
36649566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dm, PETSC_TRUE));
366568d4fef7SMatthew G. Knepley     } else {
36669566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dm, dms[refine-2]));
36679566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dm, PETSC_TRUE));
36689566063dSJacob Faibussowitsch       PetscCall(DMSetCoarseDM(dms[0], dms[refine-1]));
36699566063dSJacob Faibussowitsch       PetscCall(DMPlexSetRegularRefinement(dms[0], PETSC_TRUE));
367068d4fef7SMatthew G. Knepley     }
36719566063dSJacob Faibussowitsch     PetscCall(DMSetCoarseDM(dms[refine-1], coarseDM));
36729566063dSJacob Faibussowitsch     PetscCall(PetscObjectDereference((PetscObject)coarseDM));
367368d4fef7SMatthew G. Knepley     /* Free DMs */
367468d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
36759566063dSJacob Faibussowitsch       PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]));
36769566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dms[r]));
367768d4fef7SMatthew G. Knepley     }
36789566063dSJacob Faibussowitsch     PetscCall(PetscFree(dms));
367968d4fef7SMatthew G. Knepley   } else {
368068d4fef7SMatthew G. Knepley     for (r = 0; r < refine; ++r) {
36819318fe57SMatthew G. Knepley       DM             rdm;
368251a74b61SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex*) dm->data)->coordFunc;
368368d4fef7SMatthew G. Knepley 
36849566063dSJacob Faibussowitsch       PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm));
36859566063dSJacob Faibussowitsch       PetscCall(DMRefine(dm, PetscObjectComm((PetscObject) dm), &rdm));
368668d4fef7SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
36879566063dSJacob Faibussowitsch       PetscCall(DMPlexReplace_Static(dm, &rdm));
36889566063dSJacob Faibussowitsch       PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm));
368961a622f3SMatthew G. Knepley       if (coordFunc && remap) {
36909566063dSJacob Faibussowitsch         PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
369151a74b61SMatthew G. Knepley         ((DM_Plex*) dm->data)->coordFunc = coordFunc;
369251a74b61SMatthew G. Knepley       }
369368d4fef7SMatthew G. Knepley     }
369468d4fef7SMatthew G. Knepley   }
36953cf6fe12SMatthew G. Knepley   /* Handle DMPlex coarsening */
36969566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_coarsen", "Coarsen the mesh", "DMCreate", coarsen, &coarsen, NULL,0));
36979566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBoundedInt("-dm_coarsen_hierarchy", "The number of coarsenings", "DMCreate", coarsen, &coarsen, &isHierarchy,0));
3698b653a561SMatthew G. Knepley   if (coarsen && isHierarchy) {
3699b653a561SMatthew G. Knepley     DM *dms;
3700b653a561SMatthew G. Knepley 
37019566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(coarsen, &dms));
37029566063dSJacob Faibussowitsch     PetscCall(DMCoarsenHierarchy(dm, coarsen, dms));
3703b653a561SMatthew G. Knepley     /* Free DMs */
3704b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
37059566063dSJacob Faibussowitsch       PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dms[r]));
37069566063dSJacob Faibussowitsch       PetscCall(DMDestroy(&dms[r]));
3707b653a561SMatthew G. Knepley     }
37089566063dSJacob Faibussowitsch     PetscCall(PetscFree(dms));
3709b653a561SMatthew G. Knepley   } else {
3710b653a561SMatthew G. Knepley     for (r = 0; r < coarsen; ++r) {
37119318fe57SMatthew G. Knepley       DM             cdm;
37129318fe57SMatthew G. Knepley       PetscPointFunc coordFunc = ((DM_Plex*) dm->data)->coordFunc;
37133cf6fe12SMatthew G. Knepley 
37149566063dSJacob Faibussowitsch       PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm));
37159566063dSJacob Faibussowitsch       PetscCall(DMCoarsen(dm, PetscObjectComm((PetscObject) dm), &cdm));
37163cf6fe12SMatthew G. Knepley       /* Total hack since we do not pass in a pointer */
37179566063dSJacob Faibussowitsch       PetscCall(DMPlexReplace_Static(dm, &cdm));
37189566063dSJacob Faibussowitsch       PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm));
37199318fe57SMatthew G. Knepley       if (coordFunc) {
37209566063dSJacob Faibussowitsch         PetscCall(DMPlexRemapGeometry(dm, 0.0, coordFunc));
37219318fe57SMatthew G. Knepley         ((DM_Plex*) dm->data)->coordFunc = coordFunc;
37229318fe57SMatthew G. Knepley       }
37233cf6fe12SMatthew G. Knepley     }
3724b653a561SMatthew G. Knepley   }
3725909dfd52SMatthew G. Knepley   /* Handle ghost cells */
37269566063dSJacob Faibussowitsch   PetscCall(PetscOptionsBool("-dm_plex_create_fv_ghost_cells", "Flag to create finite volume ghost cells on the boundary", "DMCreate", ghostCells, &ghostCells, NULL));
3727909dfd52SMatthew G. Knepley   if (ghostCells) {
3728909dfd52SMatthew G. Knepley     DM   gdm;
3729909dfd52SMatthew G. Knepley     char lname[PETSC_MAX_PATH_LEN];
3730909dfd52SMatthew G. Knepley 
3731909dfd52SMatthew G. Knepley     lname[0] = '\0';
37329566063dSJacob Faibussowitsch     PetscCall(PetscOptionsString("-dm_plex_fv_ghost_cells_label", "Label name for ghost cells boundary", "DMCreate", lname, lname, sizeof(lname), &flg));
37339566063dSJacob Faibussowitsch     PetscCall(DMPlexConstructGhostCells(dm, flg ? lname : NULL, NULL, &gdm));
37349566063dSJacob Faibussowitsch     PetscCall(DMPlexReplace_Static(dm, &gdm));
3735909dfd52SMatthew G. Knepley   }
37366913077dSMatthew G. Knepley   /* Handle 1D order */
37376bc1bd01Sksagiyam   if (reorder != DMPLEX_REORDER_DEFAULT_FALSE && dim == 1) {
37386913077dSMatthew G. Knepley     DM           cdm, rdm;
37396913077dSMatthew G. Knepley     PetscDS      cds;
37406913077dSMatthew G. Knepley     PetscObject  obj;
37416913077dSMatthew G. Knepley     PetscClassId id = PETSC_OBJECT_CLASSID;
37426913077dSMatthew G. Knepley     IS           perm;
37436bc1bd01Sksagiyam     PetscInt     Nf;
37446913077dSMatthew G. Knepley     PetscBool    distributed;
37456913077dSMatthew G. Knepley 
37469566063dSJacob Faibussowitsch     PetscCall(DMPlexIsDistributed(dm, &distributed));
37479566063dSJacob Faibussowitsch     PetscCall(DMGetCoordinateDM(dm, &cdm));
37489566063dSJacob Faibussowitsch     PetscCall(DMGetDS(cdm, &cds));
37499566063dSJacob Faibussowitsch     PetscCall(PetscDSGetNumFields(cds, &Nf));
37506913077dSMatthew G. Knepley     if (Nf) {
37519566063dSJacob Faibussowitsch       PetscCall(PetscDSGetDiscretization(cds, 0, &obj));
37529566063dSJacob Faibussowitsch       PetscCall(PetscObjectGetClassId(obj, &id));
37536913077dSMatthew G. Knepley     }
37546bc1bd01Sksagiyam     if (!distributed && id != PETSCFE_CLASSID) {
37559566063dSJacob Faibussowitsch       PetscCall(DMPlexGetOrdering1D(dm, &perm));
37569566063dSJacob Faibussowitsch       PetscCall(DMPlexPermute(dm, perm, &rdm));
37579566063dSJacob Faibussowitsch       PetscCall(DMPlexReplace_Static(dm, &rdm));
37589566063dSJacob Faibussowitsch       PetscCall(ISDestroy(&perm));
37596913077dSMatthew G. Knepley     }
37606913077dSMatthew G. Knepley   }
37613cf6fe12SMatthew G. Knepley   /* Handle */
37629566063dSJacob Faibussowitsch   PetscCall(DMSetFromOptions_NonRefinement_Plex(PetscOptionsObject, dm));
3763d0609cedSBarry Smith   PetscOptionsHeadEnd();
37640a6ba040SMatthew G. Knepley   PetscFunctionReturn(0);
37650a6ba040SMatthew G. Knepley }
37660a6ba040SMatthew G. Knepley 
3767552f7358SJed Brown static PetscErrorCode DMCreateGlobalVector_Plex(DM dm,Vec *vec)
3768552f7358SJed Brown {
3769552f7358SJed Brown   PetscFunctionBegin;
37709566063dSJacob Faibussowitsch   PetscCall(DMCreateGlobalVector_Section_Private(dm,vec));
37719566063dSJacob Faibussowitsch   /* PetscCall(VecSetOperation(*vec, VECOP_DUPLICATE, (void(*)(void)) VecDuplicate_MPI_DM)); */
37729566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex));
37739566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEWNATIVE, (void (*)(void)) VecView_Plex_Native));
37749566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex));
37759566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOADNATIVE, (void (*)(void)) VecLoad_Plex_Native));
3776552f7358SJed Brown   PetscFunctionReturn(0);
3777552f7358SJed Brown }
3778552f7358SJed Brown 
3779552f7358SJed Brown static PetscErrorCode DMCreateLocalVector_Plex(DM dm,Vec *vec)
3780552f7358SJed Brown {
3781552f7358SJed Brown   PetscFunctionBegin;
37829566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector_Section_Private(dm,vec));
37839566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_VIEW, (void (*)(void)) VecView_Plex_Local));
37849566063dSJacob Faibussowitsch   PetscCall(VecSetOperation(*vec, VECOP_LOAD, (void (*)(void)) VecLoad_Plex_Local));
3785552f7358SJed Brown   PetscFunctionReturn(0);
3786552f7358SJed Brown }
3787552f7358SJed Brown 
3788793f3fe5SMatthew G. Knepley static PetscErrorCode DMGetDimPoints_Plex(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
3789793f3fe5SMatthew G. Knepley {
3790793f3fe5SMatthew G. Knepley   PetscInt       depth, d;
3791793f3fe5SMatthew G. Knepley 
3792793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
37939566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepth(dm, &depth));
3794793f3fe5SMatthew G. Knepley   if (depth == 1) {
37959566063dSJacob Faibussowitsch     PetscCall(DMGetDimension(dm, &d));
37969566063dSJacob Faibussowitsch     if (dim == 0)      PetscCall(DMPlexGetDepthStratum(dm, dim, pStart, pEnd));
37979566063dSJacob Faibussowitsch     else if (dim == d) PetscCall(DMPlexGetDepthStratum(dm, 1, pStart, pEnd));
3798793f3fe5SMatthew G. Knepley     else               {*pStart = 0; *pEnd = 0;}
3799793f3fe5SMatthew G. Knepley   } else {
38009566063dSJacob Faibussowitsch     PetscCall(DMPlexGetDepthStratum(dm, dim, pStart, pEnd));
3801793f3fe5SMatthew G. Knepley   }
3802793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
3803793f3fe5SMatthew G. Knepley }
3804793f3fe5SMatthew G. Knepley 
380528d58a37SPierre Jolivet static PetscErrorCode DMGetNeighbors_Plex(DM dm, PetscInt *nranks, const PetscMPIInt *ranks[])
3806502a2867SDave May {
3807502a2867SDave May   PetscSF           sf;
38080a19bb7dSprj-   PetscInt          niranks, njranks, n;
38090a19bb7dSprj-   const PetscMPIInt *iranks, *jranks;
38100a19bb7dSprj-   DM_Plex           *data = (DM_Plex*) dm->data;
3811502a2867SDave May 
38122f356facSMatthew G. Knepley   PetscFunctionBegin;
38139566063dSJacob Faibussowitsch   PetscCall(DMGetPointSF(dm, &sf));
38140a19bb7dSprj-   if (!data->neighbors) {
38159566063dSJacob Faibussowitsch     PetscCall(PetscSFSetUp(sf));
38169566063dSJacob Faibussowitsch     PetscCall(PetscSFGetRootRanks(sf, &njranks, &jranks, NULL, NULL, NULL));
38179566063dSJacob Faibussowitsch     PetscCall(PetscSFGetLeafRanks(sf, &niranks, &iranks, NULL, NULL));
38189566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(njranks + niranks + 1, &data->neighbors));
38199566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(data->neighbors + 1, jranks, njranks));
38209566063dSJacob Faibussowitsch     PetscCall(PetscArraycpy(data->neighbors + njranks + 1, iranks, niranks));
38210a19bb7dSprj-     n = njranks + niranks;
38229566063dSJacob Faibussowitsch     PetscCall(PetscSortRemoveDupsMPIInt(&n, data->neighbors + 1));
38230a19bb7dSprj-     /* The following cast should never fail: can't have more neighbors than PETSC_MPI_INT_MAX */
38249566063dSJacob Faibussowitsch     PetscCall(PetscMPIIntCast(n, data->neighbors));
38250a19bb7dSprj-   }
38260a19bb7dSprj-   if (nranks) *nranks = data->neighbors[0];
38270a19bb7dSprj-   if (ranks) {
38280a19bb7dSprj-     if (data->neighbors[0]) *ranks = data->neighbors + 1;
38290a19bb7dSprj-     else                    *ranks = NULL;
38300a19bb7dSprj-   }
3831502a2867SDave May   PetscFunctionReturn(0);
3832502a2867SDave May }
3833502a2867SDave May 
38341eb70e55SToby Isaac PETSC_INTERN PetscErrorCode DMInterpolateSolution_Plex(DM, DM, Mat, Vec, Vec);
38351eb70e55SToby Isaac 
383646fa42a0SMatthew G. Knepley static PetscErrorCode DMInitialize_Plex(DM dm)
3837552f7358SJed Brown {
3838552f7358SJed Brown   PetscFunctionBegin;
3839552f7358SJed Brown   dm->ops->view                            = DMView_Plex;
38402c40f234SMatthew G. Knepley   dm->ops->load                            = DMLoad_Plex;
3841552f7358SJed Brown   dm->ops->setfromoptions                  = DMSetFromOptions_Plex;
384238221697SMatthew G. Knepley   dm->ops->clone                           = DMClone_Plex;
3843552f7358SJed Brown   dm->ops->setup                           = DMSetUp_Plex;
38441bb6d2a8SBarry Smith   dm->ops->createlocalsection              = DMCreateLocalSection_Plex;
384566ad2231SToby Isaac   dm->ops->createdefaultconstraints        = DMCreateDefaultConstraints_Plex;
3846552f7358SJed Brown   dm->ops->createglobalvector              = DMCreateGlobalVector_Plex;
3847552f7358SJed Brown   dm->ops->createlocalvector               = DMCreateLocalVector_Plex;
3848184d77edSJed Brown   dm->ops->getlocaltoglobalmapping         = NULL;
38490298fd71SBarry Smith   dm->ops->createfieldis                   = NULL;
3850552f7358SJed Brown   dm->ops->createcoordinatedm              = DMCreateCoordinateDM_Plex;
3851f19dbd58SToby Isaac   dm->ops->createcoordinatefield           = DMCreateCoordinateField_Plex;
38520a6ba040SMatthew G. Knepley   dm->ops->getcoloring                     = NULL;
3853552f7358SJed Brown   dm->ops->creatematrix                    = DMCreateMatrix_Plex;
3854bceba477SMatthew G. Knepley   dm->ops->createinterpolation             = DMCreateInterpolation_Plex;
3855bd041c0cSMatthew G. Knepley   dm->ops->createmassmatrix                = DMCreateMassMatrix_Plex;
3856b4937a87SMatthew G. Knepley   dm->ops->createmassmatrixlumped          = DMCreateMassMatrixLumped_Plex;
38575a84ad33SLisandro Dalcin   dm->ops->createinjection                 = DMCreateInjection_Plex;
3858552f7358SJed Brown   dm->ops->refine                          = DMRefine_Plex;
38590a6ba040SMatthew G. Knepley   dm->ops->coarsen                         = DMCoarsen_Plex;
38600a6ba040SMatthew G. Knepley   dm->ops->refinehierarchy                 = DMRefineHierarchy_Plex;
3861b653a561SMatthew G. Knepley   dm->ops->coarsenhierarchy                = DMCoarsenHierarchy_Plex;
3862d410b0cfSMatthew G. Knepley   dm->ops->extrude                         = DMExtrude_Plex;
38630298fd71SBarry Smith   dm->ops->globaltolocalbegin              = NULL;
38640298fd71SBarry Smith   dm->ops->globaltolocalend                = NULL;
38650298fd71SBarry Smith   dm->ops->localtoglobalbegin              = NULL;
38660298fd71SBarry Smith   dm->ops->localtoglobalend                = NULL;
3867552f7358SJed Brown   dm->ops->destroy                         = DMDestroy_Plex;
3868552f7358SJed Brown   dm->ops->createsubdm                     = DMCreateSubDM_Plex;
38692adcc780SMatthew G. Knepley   dm->ops->createsuperdm                   = DMCreateSuperDM_Plex;
3870793f3fe5SMatthew G. Knepley   dm->ops->getdimpoints                    = DMGetDimPoints_Plex;
3871552f7358SJed Brown   dm->ops->locatepoints                    = DMLocatePoints_Plex;
38720709b2feSToby Isaac   dm->ops->projectfunctionlocal            = DMProjectFunctionLocal_Plex;
38730709b2feSToby Isaac   dm->ops->projectfunctionlabellocal       = DMProjectFunctionLabelLocal_Plex;
3874bfc4295aSToby Isaac   dm->ops->projectfieldlocal               = DMProjectFieldLocal_Plex;
38758c6c5593SMatthew G. Knepley   dm->ops->projectfieldlabellocal          = DMProjectFieldLabelLocal_Plex;
3876ece3a9fcSMatthew G. Knepley   dm->ops->projectbdfieldlabellocal        = DMProjectBdFieldLabelLocal_Plex;
38770709b2feSToby Isaac   dm->ops->computel2diff                   = DMComputeL2Diff_Plex;
3878b698f381SToby Isaac   dm->ops->computel2gradientdiff           = DMComputeL2GradientDiff_Plex;
38792a16baeaSToby Isaac   dm->ops->computel2fielddiff              = DMComputeL2FieldDiff_Plex;
388028d58a37SPierre Jolivet   dm->ops->getneighbors                    = DMGetNeighbors_Plex;
38819566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertBoundaryValues_C",DMPlexInsertBoundaryValues_Plex));
38829566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMPlexInsertTimeDerviativeBoundaryValues_C",DMPlexInsertTimeDerivativeBoundaryValues_Plex));
38839566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMSetUpGLVisViewer_C",DMSetUpGLVisViewer_Plex));
38849566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMCreateNeumannOverlap_C",DMCreateNeumannOverlap_Plex));
38859566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMPlexGetOverlap_C",DMPlexGetOverlap_Plex));
38869566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMPlexDistributeGetDefault_C",DMPlexDistributeGetDefault_Plex));
38879566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMPlexDistributeSetDefault_C",DMPlexDistributeSetDefault_Plex));
38886bc1bd01Sksagiyam   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMPlexReorderGetDefault_C",DMPlexReorderGetDefault_Plex));
38896bc1bd01Sksagiyam   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMPlexReorderSetDefault_C",DMPlexReorderSetDefault_Plex));
38909566063dSJacob Faibussowitsch   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMInterpolateSolution_C",DMInterpolateSolution_Plex));
3891c506a872SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMPlexGetOverlap_C",DMPlexGetOverlap_Plex));
3892c506a872SMatthew G. Knepley   PetscCall(PetscObjectComposeFunction((PetscObject)dm,"DMPlexSetOverlap_C",DMPlexSetOverlap_Plex));
3893552f7358SJed Brown   PetscFunctionReturn(0);
3894552f7358SJed Brown }
3895552f7358SJed Brown 
389646fa42a0SMatthew G. Knepley PETSC_INTERN PetscErrorCode DMClone_Plex(DM dm, DM *newdm)
389763a16f15SMatthew G. Knepley {
389863a16f15SMatthew G. Knepley   DM_Plex        *mesh = (DM_Plex *) dm->data;
389963a16f15SMatthew G. Knepley 
390063a16f15SMatthew G. Knepley   PetscFunctionBegin;
390163a16f15SMatthew G. Knepley   mesh->refct++;
390263a16f15SMatthew G. Knepley   (*newdm)->data = mesh;
39039566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject) *newdm, DMPLEX));
39049566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(*newdm));
390563a16f15SMatthew G. Knepley   PetscFunctionReturn(0);
390663a16f15SMatthew G. Knepley }
390763a16f15SMatthew G. Knepley 
39088818961aSMatthew G Knepley /*MC
39098818961aSMatthew G Knepley   DMPLEX = "plex" - A DM object that encapsulates an unstructured mesh, or CW Complex, which can be expressed using a Hasse Diagram.
39108818961aSMatthew G Knepley                     In the local representation, Vecs contain all unknowns in the interior and shared boundary. This is
39118818961aSMatthew G Knepley                     specified by a PetscSection object. Ownership in the global representation is determined by
39128818961aSMatthew G Knepley                     ownership of the underlying DMPlex points. This is specified by another PetscSection object.
39138818961aSMatthew G Knepley 
3914e5893cccSMatthew G. Knepley   Options Database Keys:
3915250712c9SMatthew G. Knepley + -dm_refine_pre                     - Refine mesh before distribution
3916250712c9SMatthew G. Knepley + -dm_refine_uniform_pre             - Choose uniform or generator-based refinement
3917250712c9SMatthew G. Knepley + -dm_refine_volume_limit_pre        - Cell volume limit after pre-refinement using generator
3918250712c9SMatthew G. Knepley . -dm_distribute                     - Distribute mesh across processes
3919250712c9SMatthew G. Knepley . -dm_distribute_overlap             - Number of cells to overlap for distribution
3920250712c9SMatthew G. Knepley . -dm_refine                         - Refine mesh after distribution
3921250712c9SMatthew G. Knepley . -dm_plex_hash_location             - Use grid hashing for point location
3922ddce0771SMatthew G. Knepley . -dm_plex_hash_box_faces <n,m,p>    - The number of divisions in each direction of the grid hash
3923f12cf164SMatthew G. Knepley . -dm_plex_partition_balance         - Attempt to evenly divide points on partition boundary between processes
3924f12cf164SMatthew G. Knepley . -dm_plex_remesh_bd                 - Allow changes to the boundary on remeshing
3925f12cf164SMatthew G. Knepley . -dm_plex_max_projection_height     - Maxmimum mesh point height used to project locally
3926f12cf164SMatthew G. Knepley . -dm_plex_regular_refinement        - Use special nested projection algorithm for regular refinement
3927250712c9SMatthew G. Knepley . -dm_plex_check_all                 - Perform all shecks below
3928f12cf164SMatthew G. Knepley . -dm_plex_check_symmetry            - Check that the adjacency information in the mesh is symmetric
3929f12cf164SMatthew G. Knepley . -dm_plex_check_skeleton <celltype> - Check that each cell has the correct number of vertices
3930f12cf164SMatthew 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
3931f12cf164SMatthew G. Knepley . -dm_plex_check_geometry            - Check that cells have positive volume
3932f12cf164SMatthew G. Knepley . -dm_view :mesh.tex:ascii_latex     - View the mesh in LaTeX/TikZ
3933e5893cccSMatthew G. Knepley . -dm_plex_view_scale <num>          - Scale the TikZ
3934e5893cccSMatthew G. Knepley - -dm_plex_print_fem <num>           - View FEM assembly information, such as element vectors and matrices
3935e5893cccSMatthew G. Knepley 
39368818961aSMatthew G Knepley   Level: intermediate
39378818961aSMatthew G Knepley 
3938db781477SPatrick Sanan .seealso: `DMType`, `DMPlexCreate()`, `DMCreate()`, `DMSetType()`
39398818961aSMatthew G Knepley M*/
39408818961aSMatthew G Knepley 
39418cc058d9SJed Brown PETSC_EXTERN PetscErrorCode DMCreate_Plex(DM dm)
3942552f7358SJed Brown {
3943552f7358SJed Brown   DM_Plex       *mesh;
3944412e9a14SMatthew G. Knepley   PetscInt       unit;
3945552f7358SJed Brown 
3946552f7358SJed Brown   PetscFunctionBegin;
3947552f7358SJed Brown   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39489566063dSJacob Faibussowitsch   PetscCall(PetscNewLog(dm,&mesh));
3949552f7358SJed Brown   dm->data = mesh;
3950552f7358SJed Brown 
3951552f7358SJed Brown   mesh->refct             = 1;
39529566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->coneSection));
39539566063dSJacob Faibussowitsch   PetscCall(PetscSectionCreate(PetscObjectComm((PetscObject)dm), &mesh->supportSection));
3954552f7358SJed Brown   mesh->refinementUniform = PETSC_TRUE;
3955552f7358SJed Brown   mesh->refinementLimit   = -1.0;
3956e600fa54SMatthew G. Knepley   mesh->distDefault       = PETSC_TRUE;
39576bc1bd01Sksagiyam   mesh->reorderDefault    = DMPLEX_REORDER_DEFAULT_NOTSET;
39587d0f5628SVaclav Hapla   mesh->interpolated      = DMPLEX_INTERPOLATED_INVALID;
39597d0f5628SVaclav Hapla   mesh->interpolatedCollective = DMPLEX_INTERPOLATED_INVALID;
3960552f7358SJed Brown 
39619566063dSJacob Faibussowitsch   PetscCall(PetscPartitionerCreate(PetscObjectComm((PetscObject)dm), &mesh->partitioner));
39622e62ab5aSMatthew G. Knepley   mesh->remeshBd     = PETSC_FALSE;
3963d9deefdfSMatthew G. Knepley 
39648865f1eaSKarl Rupp   for (unit = 0; unit < NUM_PETSC_UNITS; ++unit) mesh->scale[unit] = 1.0;
3965552f7358SJed Brown 
3966df0420ecSMatthew G. Knepley   mesh->depthState          = -1;
3967ba2698f1SMatthew G. Knepley   mesh->celltypeState       = -1;
39686113b454SMatthew G. Knepley   mesh->printTol            = 1.0e-10;
3969552f7358SJed Brown 
39709566063dSJacob Faibussowitsch   PetscCall(DMInitialize_Plex(dm));
3971552f7358SJed Brown   PetscFunctionReturn(0);
3972552f7358SJed Brown }
3973552f7358SJed Brown 
3974552f7358SJed Brown /*@
3975552f7358SJed Brown   DMPlexCreate - Creates a DMPlex object, which encapsulates an unstructured mesh, or CW complex, which can be expressed using a Hasse Diagram.
3976552f7358SJed Brown 
3977d083f849SBarry Smith   Collective
3978552f7358SJed Brown 
3979552f7358SJed Brown   Input Parameter:
3980552f7358SJed Brown . comm - The communicator for the DMPlex object
3981552f7358SJed Brown 
3982552f7358SJed Brown   Output Parameter:
3983552f7358SJed Brown . mesh  - The DMPlex object
3984552f7358SJed Brown 
3985552f7358SJed Brown   Level: beginner
3986552f7358SJed Brown 
3987552f7358SJed Brown @*/
3988552f7358SJed Brown PetscErrorCode DMPlexCreate(MPI_Comm comm, DM *mesh)
3989552f7358SJed Brown {
3990552f7358SJed Brown   PetscFunctionBegin;
3991552f7358SJed Brown   PetscValidPointer(mesh,2);
39929566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, mesh));
39939566063dSJacob Faibussowitsch   PetscCall(DMSetType(*mesh, DMPLEX));
3994552f7358SJed Brown   PetscFunctionReturn(0);
3995552f7358SJed Brown }
3996552f7358SJed Brown 
3997b09969d6SVaclav Hapla /*@C
3998b09969d6SVaclav Hapla   DMPlexBuildFromCellListParallel - Build distributed DMPLEX topology from a list of vertices for each cell (common mesh generator output)
3999b09969d6SVaclav Hapla 
4000b09969d6SVaclav Hapla   Input Parameters:
4001b09969d6SVaclav Hapla + dm - The DM
4002b09969d6SVaclav Hapla . numCells - The number of cells owned by this process
4003325d53feSBarry Smith . numVertices - The number of vertices to be owned by this process, or PETSC_DECIDE
4004325d53feSBarry Smith . NVertices - The global number of vertices, or PETSC_DETERMINE
4005b09969d6SVaclav Hapla . numCorners - The number of vertices for each cell
40065e488331SVaclav Hapla - cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
4007b09969d6SVaclav Hapla 
4008be8c289dSNicolas Barral   Output Parameters:
4009be8c289dSNicolas Barral + vertexSF - (Optional) SF describing complete vertex ownership
4010be8c289dSNicolas Barral - verticesAdjSaved - (Optional) vertex adjacency array
4011b09969d6SVaclav Hapla 
4012b09969d6SVaclav Hapla   Notes:
4013b09969d6SVaclav Hapla   Two triangles sharing a face
4014b09969d6SVaclav Hapla $
4015b09969d6SVaclav Hapla $        2
4016b09969d6SVaclav Hapla $      / | \
4017b09969d6SVaclav Hapla $     /  |  \
4018b09969d6SVaclav Hapla $    /   |   \
4019b09969d6SVaclav Hapla $   0  0 | 1  3
4020b09969d6SVaclav Hapla $    \   |   /
4021b09969d6SVaclav Hapla $     \  |  /
4022b09969d6SVaclav Hapla $      \ | /
4023b09969d6SVaclav Hapla $        1
4024b09969d6SVaclav Hapla would have input
4025b09969d6SVaclav Hapla $  numCells = 2, numVertices = 4
4026b09969d6SVaclav Hapla $  cells = [0 1 2  1 3 2]
4027b09969d6SVaclav Hapla $
4028b09969d6SVaclav Hapla which would result in the DMPlex
4029b09969d6SVaclav Hapla $
4030b09969d6SVaclav Hapla $        4
4031b09969d6SVaclav Hapla $      / | \
4032b09969d6SVaclav Hapla $     /  |  \
4033b09969d6SVaclav Hapla $    /   |   \
4034b09969d6SVaclav Hapla $   2  0 | 1  5
4035b09969d6SVaclav Hapla $    \   |   /
4036b09969d6SVaclav Hapla $     \  |  /
4037b09969d6SVaclav Hapla $      \ | /
4038b09969d6SVaclav Hapla $        3
4039b09969d6SVaclav Hapla 
404025b6865aSVaclav Hapla   Vertices are implicitly numbered consecutively 0,...,NVertices.
404125b6865aSVaclav Hapla   Each rank owns a chunk of numVertices consecutive vertices.
404225b6865aSVaclav Hapla   If numVertices is PETSC_DECIDE, PETSc will distribute them as evenly as possible using PetscLayout.
4043325d53feSBarry Smith   If NVertices is PETSC_DETERMINE and numVertices is PETSC_DECIDE, NVertices is computed by PETSc as the maximum vertex index in cells + 1.
4044325d53feSBarry Smith   If only NVertices is PETSC_DETERMINE, it is computed as the sum of numVertices over all ranks.
404525b6865aSVaclav Hapla 
4046b09969d6SVaclav Hapla   The cell distribution is arbitrary non-overlapping, independent of the vertex distribution.
4047b09969d6SVaclav Hapla 
4048b09969d6SVaclav Hapla   Not currently supported in Fortran.
4049b09969d6SVaclav Hapla 
4050b09969d6SVaclav Hapla   Level: advanced
4051b09969d6SVaclav Hapla 
4052db781477SPatrick Sanan .seealso: `DMPlexBuildFromCellList()`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildCoordinatesFromCellListParallel()`
4053b09969d6SVaclav Hapla @*/
4054be8c289dSNicolas Barral PetscErrorCode DMPlexBuildFromCellListParallel(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt NVertices, PetscInt numCorners, const PetscInt cells[], PetscSF *vertexSF, PetscInt **verticesAdjSaved)
4055a47d0d45SMatthew G. Knepley {
40562464107aSksagiyam   PetscSF         sfPoint;
40572464107aSksagiyam   PetscLayout     layout;
405882fb893eSVaclav Hapla   PetscInt        numVerticesAdj, *verticesAdj, *cones, c, p;
4059a47d0d45SMatthew G. Knepley 
4060a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
406125b6865aSVaclav Hapla   PetscValidLogicalCollectiveInt(dm,NVertices,4);
40629566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList,dm,0,0,0));
406325b6865aSVaclav Hapla   /* Get/check global number of vertices */
406425b6865aSVaclav Hapla   {
406525b6865aSVaclav Hapla     PetscInt NVerticesInCells, i;
406625b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
406725b6865aSVaclav Hapla 
406825b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
406925b6865aSVaclav Hapla     NVerticesInCells = PETSC_MIN_INT;
407025b6865aSVaclav Hapla     for (i=0; i<len; i++) if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
407125b6865aSVaclav Hapla     ++NVerticesInCells;
40729566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Allreduce(MPI_IN_PLACE, &NVerticesInCells, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject) dm)));
407325b6865aSVaclav Hapla 
407425b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE && NVertices == PETSC_DECIDE) NVertices = NVerticesInCells;
40751dca8a05SBarry Smith     else 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);
407625b6865aSVaclav Hapla   }
40779079aca8SVaclav Hapla   /* Count locally unique vertices */
40789079aca8SVaclav Hapla   {
40799079aca8SVaclav Hapla     PetscHSetI vhash;
40809079aca8SVaclav Hapla     PetscInt off = 0;
40819079aca8SVaclav Hapla 
40829566063dSJacob Faibussowitsch     PetscCall(PetscHSetICreate(&vhash));
4083a47d0d45SMatthew G. Knepley     for (c = 0; c < numCells; ++c) {
4084a47d0d45SMatthew G. Knepley       for (p = 0; p < numCorners; ++p) {
40859566063dSJacob Faibussowitsch         PetscCall(PetscHSetIAdd(vhash, cells[c*numCorners+p]));
4086a47d0d45SMatthew G. Knepley       }
4087a47d0d45SMatthew G. Knepley     }
40889566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetSize(vhash, &numVerticesAdj));
40899566063dSJacob Faibussowitsch     if (!verticesAdjSaved) PetscCall(PetscMalloc1(numVerticesAdj, &verticesAdj));
4090be8c289dSNicolas Barral     else { verticesAdj = *verticesAdjSaved; }
40919566063dSJacob Faibussowitsch     PetscCall(PetscHSetIGetElems(vhash, &off, verticesAdj));
40929566063dSJacob Faibussowitsch     PetscCall(PetscHSetIDestroy(&vhash));
409363a3b9bcSJacob Faibussowitsch     PetscCheck(off == numVerticesAdj,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid number of local vertices %" PetscInt_FMT " should be %" PetscInt_FMT, off, numVerticesAdj);
4094a47d0d45SMatthew G. Knepley   }
40959566063dSJacob Faibussowitsch   PetscCall(PetscSortInt(numVerticesAdj, verticesAdj));
4096a47d0d45SMatthew G. Knepley   /* Create cones */
40979566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, 0, numCells+numVerticesAdj));
40989566063dSJacob Faibussowitsch   for (c = 0; c < numCells; ++c) PetscCall(DMPlexSetConeSize(dm, c, numCorners));
40999566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm));
41009566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCones(dm,&cones));
4101a47d0d45SMatthew G. Knepley   for (c = 0; c < numCells; ++c) {
4102a47d0d45SMatthew G. Knepley     for (p = 0; p < numCorners; ++p) {
4103a47d0d45SMatthew G. Knepley       const PetscInt gv = cells[c*numCorners+p];
4104a47d0d45SMatthew G. Knepley       PetscInt       lv;
4105a47d0d45SMatthew G. Knepley 
41069079aca8SVaclav Hapla       /* Positions within verticesAdj form 0-based local vertex numbering;
41079079aca8SVaclav Hapla          we need to shift it by numCells to get correct DAG points (cells go first) */
41089566063dSJacob Faibussowitsch       PetscCall(PetscFindInt(gv, numVerticesAdj, verticesAdj, &lv));
410963a3b9bcSJacob Faibussowitsch       PetscCheck(lv >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Could not find global vertex %" PetscInt_FMT " in local connectivity", gv);
4110961cfab0SVaclav Hapla       cones[c*numCorners+p] = lv+numCells;
4111a47d0d45SMatthew G. Knepley     }
4112a47d0d45SMatthew G. Knepley   }
41132464107aSksagiyam   /* Build point sf */
41149566063dSJacob Faibussowitsch   PetscCall(PetscLayoutCreate(PetscObjectComm((PetscObject)dm), &layout));
41159566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetSize(layout, NVertices));
41169566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetLocalSize(layout, numVertices));
41179566063dSJacob Faibussowitsch   PetscCall(PetscLayoutSetBlockSize(layout, 1));
41189566063dSJacob Faibussowitsch   PetscCall(PetscSFCreateByMatchingIndices(layout, numVerticesAdj, verticesAdj, NULL, numCells, numVerticesAdj, verticesAdj, NULL, numCells, vertexSF, &sfPoint));
41199566063dSJacob Faibussowitsch   PetscCall(PetscLayoutDestroy(&layout));
41209566063dSJacob Faibussowitsch   if (!verticesAdjSaved) PetscCall(PetscFree(verticesAdj));
41219566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject) sfPoint, "point SF"));
41222464107aSksagiyam   if (dm->sf) {
41232464107aSksagiyam     const char *prefix;
41242464107aSksagiyam 
41259566063dSJacob Faibussowitsch     PetscCall(PetscObjectGetOptionsPrefix((PetscObject)dm->sf, &prefix));
41269566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetOptionsPrefix((PetscObject)sfPoint, prefix));
41272464107aSksagiyam   }
41289566063dSJacob Faibussowitsch   PetscCall(DMSetPointSF(dm, sfPoint));
41299566063dSJacob Faibussowitsch   PetscCall(PetscSFDestroy(&sfPoint));
41309566063dSJacob Faibussowitsch   if (vertexSF) PetscCall(PetscObjectSetName((PetscObject)(*vertexSF), "Vertex Ownership SF"));
4131a47d0d45SMatthew G. Knepley   /* Fill in the rest of the topology structure */
41329566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
41339566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
41349566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList,dm,0,0,0));
4135a47d0d45SMatthew G. Knepley   PetscFunctionReturn(0);
4136a47d0d45SMatthew G. Knepley }
4137a47d0d45SMatthew G. Knepley 
4138b09969d6SVaclav Hapla /*@C
4139b09969d6SVaclav Hapla   DMPlexBuildCoordinatesFromCellListParallel - Build DM coordinates from a list of coordinates for each owned vertex (common mesh generator output)
4140b09969d6SVaclav Hapla 
4141b09969d6SVaclav Hapla   Input Parameters:
4142b09969d6SVaclav Hapla + dm - The DM
4143b09969d6SVaclav Hapla . spaceDim - The spatial dimension used for coordinates
4144b09969d6SVaclav Hapla . sfVert - SF describing complete vertex ownership
4145b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
4146b09969d6SVaclav Hapla 
4147b09969d6SVaclav Hapla   Level: advanced
4148b09969d6SVaclav Hapla 
4149b09969d6SVaclav Hapla   Notes:
4150b09969d6SVaclav Hapla   Not currently supported in Fortran.
4151b09969d6SVaclav Hapla 
4152db781477SPatrick Sanan .seealso: `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildFromCellListParallel()`
4153b09969d6SVaclav Hapla @*/
41541edcf0b2SVaclav Hapla PetscErrorCode DMPlexBuildCoordinatesFromCellListParallel(DM dm, PetscInt spaceDim, PetscSF sfVert, const PetscReal vertexCoords[])
4155a47d0d45SMatthew G. Knepley {
4156a47d0d45SMatthew G. Knepley   PetscSection   coordSection;
4157a47d0d45SMatthew G. Knepley   Vec            coordinates;
4158a47d0d45SMatthew G. Knepley   PetscScalar   *coords;
41591edcf0b2SVaclav Hapla   PetscInt       numVertices, numVerticesAdj, coordSize, v, vStart, vEnd;
4160a47d0d45SMatthew G. Knepley 
4161a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
41629566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0));
41639566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
41641dca8a05SBarry Smith   PetscCheck(vStart >= 0 && vEnd >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
41659566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, spaceDim));
41669566063dSJacob Faibussowitsch   PetscCall(PetscSFGetGraph(sfVert, &numVertices, &numVerticesAdj, NULL, NULL));
41671dca8a05SBarry 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);
41689566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
41699566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
41709566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, spaceDim));
41719566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, vStart, vEnd));
41721edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
41739566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, spaceDim));
41749566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, spaceDim));
4175a47d0d45SMatthew G. Knepley   }
41769566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
41779566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
41789566063dSJacob Faibussowitsch   PetscCall(VecCreate(PetscObjectComm((PetscObject)dm), &coordinates));
41799566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, spaceDim));
41809566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
41819566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
41829566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates,VECSTANDARD));
41839566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
4184a47d0d45SMatthew G. Knepley   {
4185a47d0d45SMatthew G. Knepley     MPI_Datatype coordtype;
4186a47d0d45SMatthew G. Knepley 
4187a47d0d45SMatthew G. Knepley     /* Need a temp buffer for coords if we have complex/single */
41889566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_contiguous(spaceDim, MPIU_SCALAR, &coordtype));
41899566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_commit(&coordtype));
419021016a8bSBarry Smith #if defined(PETSC_USE_COMPLEX)
419121016a8bSBarry Smith     {
419221016a8bSBarry Smith     PetscScalar *svertexCoords;
419321016a8bSBarry Smith     PetscInt    i;
41949566063dSJacob Faibussowitsch     PetscCall(PetscMalloc1(numVertices*spaceDim,&svertexCoords));
41953612f820SVaclav Hapla     for (i=0; i<numVertices*spaceDim; i++) svertexCoords[i] = vertexCoords[i];
41969566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sfVert, coordtype, svertexCoords, coords,MPI_REPLACE));
41979566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sfVert, coordtype, svertexCoords, coords,MPI_REPLACE));
41989566063dSJacob Faibussowitsch     PetscCall(PetscFree(svertexCoords));
419921016a8bSBarry Smith     }
420021016a8bSBarry Smith #else
42019566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastBegin(sfVert, coordtype, vertexCoords, coords,MPI_REPLACE));
42029566063dSJacob Faibussowitsch     PetscCall(PetscSFBcastEnd(sfVert, coordtype, vertexCoords, coords,MPI_REPLACE));
420321016a8bSBarry Smith #endif
42049566063dSJacob Faibussowitsch     PetscCallMPI(MPI_Type_free(&coordtype));
4205a47d0d45SMatthew G. Knepley   }
42069566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
42079566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
42089566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
42099566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0));
4210a47d0d45SMatthew G. Knepley   PetscFunctionReturn(0);
4211a47d0d45SMatthew G. Knepley }
4212a47d0d45SMatthew G. Knepley 
4213c3edce3dSSatish Balay /*@
4214b09969d6SVaclav Hapla   DMPlexCreateFromCellListParallelPetsc - Create distributed DMPLEX from a list of vertices for each cell (common mesh generator output)
4215a47d0d45SMatthew G. Knepley 
4216a47d0d45SMatthew G. Knepley   Input Parameters:
4217a47d0d45SMatthew G. Knepley + comm - The communicator
4218a47d0d45SMatthew G. Knepley . dim - The topological dimension of the mesh
4219a47d0d45SMatthew G. Knepley . numCells - The number of cells owned by this process
422025b6865aSVaclav Hapla . numVertices - The number of vertices owned by this process, or PETSC_DECIDE
422125b6865aSVaclav Hapla . NVertices - The global number of vertices, or PETSC_DECIDE
4222a47d0d45SMatthew G. Knepley . numCorners - The number of vertices for each cell
4223a47d0d45SMatthew G. Knepley . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
4224a47d0d45SMatthew G. Knepley . cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
4225a47d0d45SMatthew G. Knepley . spaceDim - The spatial dimension used for coordinates
4226a47d0d45SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
4227a47d0d45SMatthew G. Knepley 
4228d8d19677SJose E. Roman   Output Parameters:
422918d54ad4SMichael Lange + dm - The DM
4230be8c289dSNicolas Barral . vertexSF - (Optional) SF describing complete vertex ownership
4231be8c289dSNicolas Barral - verticesAdjSaved - (Optional) vertex adjacency array
4232a47d0d45SMatthew G. Knepley 
4233b09969d6SVaclav Hapla   Notes:
4234b09969d6SVaclav Hapla   This function is just a convenient sequence of DMCreate(), DMSetType(), DMSetDimension(),
4235b09969d6SVaclav Hapla   DMPlexBuildFromCellListParallel(), DMPlexInterpolate(), DMPlexBuildCoordinatesFromCellListParallel()
4236a47d0d45SMatthew G. Knepley 
423725b6865aSVaclav Hapla   See DMPlexBuildFromCellListParallel() for an example and details about the topology-related parameters.
423825b6865aSVaclav Hapla   See DMPlexBuildCoordinatesFromCellListParallel() for details about the geometry-related parameters.
423925b6865aSVaclav Hapla 
4240b09969d6SVaclav Hapla   Level: intermediate
4241a47d0d45SMatthew G. Knepley 
4242db781477SPatrick Sanan .seealso: `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()`
4243a47d0d45SMatthew G. Knepley @*/
4244be8c289dSNicolas Barral 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)
4245a47d0d45SMatthew G. Knepley {
4246a47d0d45SMatthew G. Knepley   PetscSF        sfVert;
4247a47d0d45SMatthew G. Knepley 
4248a47d0d45SMatthew G. Knepley   PetscFunctionBegin;
42499566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
42509566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
4251a47d0d45SMatthew G. Knepley   PetscValidLogicalCollectiveInt(*dm, dim, 2);
4252064a246eSJacob Faibussowitsch   PetscValidLogicalCollectiveInt(*dm, spaceDim, 9);
42539566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
42549566063dSJacob Faibussowitsch   PetscCall(DMPlexBuildFromCellListParallel(*dm, numCells, numVertices, NVertices, numCorners, cells, &sfVert, verticesAdj));
4255a47d0d45SMatthew G. Knepley   if (interpolate) {
42565fd9971aSMatthew G. Knepley     DM idm;
4257a47d0d45SMatthew G. Knepley 
42589566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
42599566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
4260a47d0d45SMatthew G. Knepley     *dm  = idm;
4261a47d0d45SMatthew G. Knepley   }
42629566063dSJacob Faibussowitsch   PetscCall(DMPlexBuildCoordinatesFromCellListParallel(*dm, spaceDim, sfVert, vertexCoords));
426318d54ad4SMichael Lange   if (vertexSF) *vertexSF = sfVert;
42649566063dSJacob Faibussowitsch   else PetscCall(PetscSFDestroy(&sfVert));
4265a47d0d45SMatthew G. Knepley   PetscFunctionReturn(0);
4266a47d0d45SMatthew G. Knepley }
4267a47d0d45SMatthew G. Knepley 
4268b09969d6SVaclav Hapla /*@C
4269b09969d6SVaclav Hapla   DMPlexBuildFromCellList - Build DMPLEX topology from a list of vertices for each cell (common mesh generator output)
42709298eaa6SMatthew G Knepley 
42719298eaa6SMatthew G Knepley   Input Parameters:
4272b09969d6SVaclav Hapla + dm - The DM
4273b09969d6SVaclav Hapla . numCells - The number of cells owned by this process
4274325d53feSBarry Smith . numVertices - The number of vertices owned by this process, or PETSC_DETERMINE
42759298eaa6SMatthew G Knepley . numCorners - The number of vertices for each cell
42765e488331SVaclav Hapla - cells - An array of numCells*numCorners numbers, the global vertex numbers for each cell
42779298eaa6SMatthew G Knepley 
4278b09969d6SVaclav Hapla   Level: advanced
42799298eaa6SMatthew G Knepley 
4280b09969d6SVaclav Hapla   Notes:
4281b09969d6SVaclav Hapla   Two triangles sharing a face
42829298eaa6SMatthew G Knepley $
42839298eaa6SMatthew G Knepley $        2
42849298eaa6SMatthew G Knepley $      / | \
42859298eaa6SMatthew G Knepley $     /  |  \
42869298eaa6SMatthew G Knepley $    /   |   \
42879298eaa6SMatthew G Knepley $   0  0 | 1  3
42889298eaa6SMatthew G Knepley $    \   |   /
42899298eaa6SMatthew G Knepley $     \  |  /
42909298eaa6SMatthew G Knepley $      \ | /
42919298eaa6SMatthew G Knepley $        1
42929298eaa6SMatthew G Knepley would have input
42939298eaa6SMatthew G Knepley $  numCells = 2, numVertices = 4
42949298eaa6SMatthew G Knepley $  cells = [0 1 2  1 3 2]
42959298eaa6SMatthew G Knepley $
42969298eaa6SMatthew G Knepley which would result in the DMPlex
42979298eaa6SMatthew G Knepley $
42989298eaa6SMatthew G Knepley $        4
42999298eaa6SMatthew G Knepley $      / | \
43009298eaa6SMatthew G Knepley $     /  |  \
43019298eaa6SMatthew G Knepley $    /   |   \
43029298eaa6SMatthew G Knepley $   2  0 | 1  5
43039298eaa6SMatthew G Knepley $    \   |   /
43049298eaa6SMatthew G Knepley $     \  |  /
43059298eaa6SMatthew G Knepley $      \ | /
43069298eaa6SMatthew G Knepley $        3
43079298eaa6SMatthew G Knepley 
4308325d53feSBarry Smith   If numVertices is PETSC_DETERMINE, it is computed by PETSc as the maximum vertex index in cells + 1.
430925b6865aSVaclav Hapla 
4310b09969d6SVaclav Hapla   Not currently supported in Fortran.
43119298eaa6SMatthew G Knepley 
4312db781477SPatrick Sanan .seealso: `DMPlexBuildFromCellListParallel()`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromCellListPetsc()`
4313b09969d6SVaclav Hapla @*/
43145e488331SVaclav Hapla PetscErrorCode DMPlexBuildFromCellList(DM dm, PetscInt numCells, PetscInt numVertices, PetscInt numCorners, const PetscInt cells[])
4315b09969d6SVaclav Hapla {
4316961cfab0SVaclav Hapla   PetscInt      *cones, c, p, dim;
4317b09969d6SVaclav Hapla 
4318b09969d6SVaclav Hapla   PetscFunctionBegin;
43199566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildFromCellList,dm,0,0,0));
43209566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
432125b6865aSVaclav Hapla   /* Get/check global number of vertices */
432225b6865aSVaclav Hapla   {
432325b6865aSVaclav Hapla     PetscInt NVerticesInCells, i;
432425b6865aSVaclav Hapla     const PetscInt len = numCells * numCorners;
432525b6865aSVaclav Hapla 
432625b6865aSVaclav Hapla     /* NVerticesInCells = max(cells) + 1 */
432725b6865aSVaclav Hapla     NVerticesInCells = PETSC_MIN_INT;
432825b6865aSVaclav Hapla     for (i=0; i<len; i++) if (cells[i] > NVerticesInCells) NVerticesInCells = cells[i];
432925b6865aSVaclav Hapla     ++NVerticesInCells;
433025b6865aSVaclav Hapla 
433125b6865aSVaclav Hapla     if (numVertices == PETSC_DECIDE) numVertices = NVerticesInCells;
433263a3b9bcSJacob Faibussowitsch     else 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);
433325b6865aSVaclav Hapla   }
43349566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, 0, numCells+numVertices));
4335b09969d6SVaclav Hapla   for (c = 0; c < numCells; ++c) {
43369566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeSize(dm, c, numCorners));
4337b09969d6SVaclav Hapla   }
43389566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm));
43399566063dSJacob Faibussowitsch   PetscCall(DMPlexGetCones(dm,&cones));
4340b09969d6SVaclav Hapla   for (c = 0; c < numCells; ++c) {
4341b09969d6SVaclav Hapla     for (p = 0; p < numCorners; ++p) {
4342961cfab0SVaclav Hapla       cones[c*numCorners+p] = cells[c*numCorners+p]+numCells;
4343b09969d6SVaclav Hapla     }
4344b09969d6SVaclav Hapla   }
43459566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
43469566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
43479566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildFromCellList,dm,0,0,0));
4348b09969d6SVaclav Hapla   PetscFunctionReturn(0);
4349b09969d6SVaclav Hapla }
4350b09969d6SVaclav Hapla 
4351b09969d6SVaclav Hapla /*@C
4352b09969d6SVaclav Hapla   DMPlexBuildCoordinatesFromCellList - Build DM coordinates from a list of coordinates for each owned vertex (common mesh generator output)
4353b09969d6SVaclav Hapla 
4354b09969d6SVaclav Hapla   Input Parameters:
4355b09969d6SVaclav Hapla + dm - The DM
4356b09969d6SVaclav Hapla . spaceDim - The spatial dimension used for coordinates
4357b09969d6SVaclav Hapla - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex
4358b09969d6SVaclav Hapla 
4359b09969d6SVaclav Hapla   Level: advanced
4360b09969d6SVaclav Hapla 
4361b09969d6SVaclav Hapla   Notes:
4362b09969d6SVaclav Hapla   Not currently supported in Fortran.
4363b09969d6SVaclav Hapla 
4364db781477SPatrick Sanan .seealso: `DMPlexBuildCoordinatesFromCellListParallel()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexBuildFromCellList()`
4365b09969d6SVaclav Hapla @*/
43661edcf0b2SVaclav Hapla PetscErrorCode DMPlexBuildCoordinatesFromCellList(DM dm, PetscInt spaceDim, const PetscReal vertexCoords[])
4367b09969d6SVaclav Hapla {
4368b09969d6SVaclav Hapla   PetscSection   coordSection;
4369b09969d6SVaclav Hapla   Vec            coordinates;
4370b09969d6SVaclav Hapla   DM             cdm;
4371b09969d6SVaclav Hapla   PetscScalar   *coords;
43721edcf0b2SVaclav Hapla   PetscInt       v, vStart, vEnd, d;
4373b09969d6SVaclav Hapla 
4374b09969d6SVaclav Hapla   PetscFunctionBegin;
43759566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0));
43769566063dSJacob Faibussowitsch   PetscCall(DMPlexGetDepthStratum(dm, 0, &vStart, &vEnd));
43771dca8a05SBarry Smith   PetscCheck(vStart >= 0 && vEnd >= 0,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "DM is not set up properly. DMPlexBuildFromCellList() should be called first.");
43789566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(dm, spaceDim));
43799566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
43809566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
43819566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, spaceDim));
43829566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, vStart, vEnd));
43831edcf0b2SVaclav Hapla   for (v = vStart; v < vEnd; ++v) {
43849566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, spaceDim));
43859566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, spaceDim));
4386b09969d6SVaclav Hapla   }
43879566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
4388b09969d6SVaclav Hapla 
43899566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDM(dm, &cdm));
43909566063dSJacob Faibussowitsch   PetscCall(DMCreateLocalVector(cdm, &coordinates));
43919566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, spaceDim));
43929566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
43939566063dSJacob Faibussowitsch   PetscCall(VecGetArrayWrite(coordinates, &coords));
43941edcf0b2SVaclav Hapla   for (v = 0; v < vEnd-vStart; ++v) {
4395b09969d6SVaclav Hapla     for (d = 0; d < spaceDim; ++d) {
4396b09969d6SVaclav Hapla       coords[v*spaceDim+d] = vertexCoords[v*spaceDim+d];
4397b09969d6SVaclav Hapla     }
4398b09969d6SVaclav Hapla   }
43999566063dSJacob Faibussowitsch   PetscCall(VecRestoreArrayWrite(coordinates, &coords));
44009566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
44019566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
44029566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_BuildCoordinatesFromCellList,dm,0,0,0));
4403b09969d6SVaclav Hapla   PetscFunctionReturn(0);
4404b09969d6SVaclav Hapla }
4405b09969d6SVaclav Hapla 
4406b09969d6SVaclav Hapla /*@
44073df08285SMatthew G. Knepley   DMPlexCreateFromCellListPetsc - Create DMPLEX from a list of vertices for each cell (common mesh generator output), but only process 0 takes in the input
44083df08285SMatthew G. Knepley 
44093df08285SMatthew G. Knepley   Collective on comm
4410b09969d6SVaclav Hapla 
4411b09969d6SVaclav Hapla   Input Parameters:
4412b09969d6SVaclav Hapla + comm - The communicator
4413b09969d6SVaclav Hapla . dim - The topological dimension of the mesh
44143df08285SMatthew G. Knepley . numCells - The number of cells, only on process 0
44153df08285SMatthew G. Knepley . numVertices - The number of vertices owned by this process, or PETSC_DECIDE, only on process 0
44163df08285SMatthew G. Knepley . numCorners - The number of vertices for each cell, only on process 0
4417b09969d6SVaclav Hapla . interpolate - Flag indicating that intermediate mesh entities (faces, edges) should be created automatically
44183df08285SMatthew G. Knepley . cells - An array of numCells*numCorners numbers, the vertices for each cell, only on process 0
4419b09969d6SVaclav Hapla . spaceDim - The spatial dimension used for coordinates
44203df08285SMatthew G. Knepley - vertexCoords - An array of numVertices*spaceDim numbers, the coordinates of each vertex, only on process 0
4421b09969d6SVaclav Hapla 
4422b09969d6SVaclav Hapla   Output Parameter:
44233df08285SMatthew G. Knepley . dm - The DM, which only has points on process 0
4424b09969d6SVaclav Hapla 
4425b09969d6SVaclav Hapla   Notes:
4426b09969d6SVaclav Hapla   This function is just a convenient sequence of DMCreate(), DMSetType(), DMSetDimension(), DMPlexBuildFromCellList(),
4427b09969d6SVaclav Hapla   DMPlexInterpolate(), DMPlexBuildCoordinatesFromCellList()
4428b09969d6SVaclav Hapla 
442925b6865aSVaclav Hapla   See DMPlexBuildFromCellList() for an example and details about the topology-related parameters.
443025b6865aSVaclav Hapla   See DMPlexBuildCoordinatesFromCellList() for details about the geometry-related parameters.
44313df08285SMatthew G. Knepley   See DMPlexCreateFromCellListParallelPetsc() for parallel input
443225b6865aSVaclav Hapla 
4433b09969d6SVaclav Hapla   Level: intermediate
4434b09969d6SVaclav Hapla 
4435db781477SPatrick Sanan .seealso: `DMPlexCreateFromCellListParallelPetsc()`, `DMPlexBuildFromCellList()`, `DMPlexBuildCoordinatesFromCellList()`, `DMPlexCreateFromDAG()`, `DMPlexCreate()`
44369298eaa6SMatthew G Knepley @*/
4437a4a685f2SJacob 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)
44389298eaa6SMatthew G Knepley {
44393df08285SMatthew G. Knepley   PetscMPIInt    rank;
44409298eaa6SMatthew G Knepley 
44419298eaa6SMatthew G Knepley   PetscFunctionBegin;
444228b400f6SJacob 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.");
44439566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
44449566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
44459566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
44469566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
4447c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildFromCellList(*dm, numCells, numVertices, numCorners, cells));
44489566063dSJacob Faibussowitsch   else           PetscCall(DMPlexBuildFromCellList(*dm, 0, 0, 0, NULL));
44499298eaa6SMatthew G Knepley   if (interpolate) {
44505fd9971aSMatthew G. Knepley     DM idm;
44519298eaa6SMatthew G Knepley 
44529566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
44539566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
44549298eaa6SMatthew G Knepley     *dm  = idm;
44559298eaa6SMatthew G Knepley   }
4456c5853193SPierre Jolivet   if (rank == 0) PetscCall(DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, vertexCoords));
44579566063dSJacob Faibussowitsch   else           PetscCall(DMPlexBuildCoordinatesFromCellList(*dm, spaceDim, NULL));
44589298eaa6SMatthew G Knepley   PetscFunctionReturn(0);
44599298eaa6SMatthew G Knepley }
44609298eaa6SMatthew G Knepley 
4461939f6067SMatthew G. Knepley /*@
4462939f6067SMatthew 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
4463939f6067SMatthew G. Knepley 
4464939f6067SMatthew G. Knepley   Input Parameters:
4465c73cfb54SMatthew G. Knepley + dm - The empty DM object, usually from DMCreate() and DMSetDimension()
4466939f6067SMatthew G. Knepley . depth - The depth of the DAG
4467367003a6SStefano Zampini . numPoints - Array of size depth + 1 containing the number of points at each depth
4468939f6067SMatthew G. Knepley . coneSize - The cone size of each point
4469939f6067SMatthew G. Knepley . cones - The concatenation of the cone points for each point, the cone list must be oriented correctly for each point
4470939f6067SMatthew G. Knepley . coneOrientations - The orientation of each cone point
4471367003a6SStefano Zampini - vertexCoords - An array of numPoints[0]*spacedim numbers representing the coordinates of each vertex, with spacedim the value set via DMSetCoordinateDim()
4472939f6067SMatthew G. Knepley 
4473939f6067SMatthew G. Knepley   Output Parameter:
4474939f6067SMatthew G. Knepley . dm - The DM
4475939f6067SMatthew G. Knepley 
4476939f6067SMatthew G. Knepley   Note: Two triangles sharing a face would have input
4477939f6067SMatthew G. Knepley $  depth = 1, numPoints = [4 2], coneSize = [3 3 0 0 0 0]
4478939f6067SMatthew G. Knepley $  cones = [2 3 4  3 5 4], coneOrientations = [0 0 0  0 0 0]
4479939f6067SMatthew G. Knepley $ vertexCoords = [-1.0 0.0  0.0 -1.0  0.0 1.0  1.0 0.0]
4480939f6067SMatthew G. Knepley $
4481939f6067SMatthew G. Knepley which would result in the DMPlex
4482939f6067SMatthew G. Knepley $
4483939f6067SMatthew G. Knepley $        4
4484939f6067SMatthew G. Knepley $      / | \
4485939f6067SMatthew G. Knepley $     /  |  \
4486939f6067SMatthew G. Knepley $    /   |   \
4487939f6067SMatthew G. Knepley $   2  0 | 1  5
4488939f6067SMatthew G. Knepley $    \   |   /
4489939f6067SMatthew G. Knepley $     \  |  /
4490939f6067SMatthew G. Knepley $      \ | /
4491939f6067SMatthew G. Knepley $        3
4492939f6067SMatthew G. Knepley $
4493a4a685f2SJacob Faibussowitsch $ Notice that all points are numbered consecutively, unlike DMPlexCreateFromCellListPetsc()
4494939f6067SMatthew G. Knepley 
4495939f6067SMatthew G. Knepley   Level: advanced
4496939f6067SMatthew G. Knepley 
4497db781477SPatrick Sanan .seealso: `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`
4498939f6067SMatthew G. Knepley @*/
44999298eaa6SMatthew G Knepley PetscErrorCode DMPlexCreateFromDAG(DM dm, PetscInt depth, const PetscInt numPoints[], const PetscInt coneSize[], const PetscInt cones[], const PetscInt coneOrientations[], const PetscScalar vertexCoords[])
45009298eaa6SMatthew G Knepley {
45019298eaa6SMatthew G Knepley   Vec            coordinates;
45029298eaa6SMatthew G Knepley   PetscSection   coordSection;
45039298eaa6SMatthew G Knepley   PetscScalar    *coords;
4504811e8653SToby Isaac   PetscInt       coordSize, firstVertex = -1, pStart = 0, pEnd = 0, p, v, dim, dimEmbed, d, off;
45059298eaa6SMatthew G Knepley 
45069298eaa6SMatthew G Knepley   PetscFunctionBegin;
45079566063dSJacob Faibussowitsch   PetscCall(DMGetDimension(dm, &dim));
45089566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateDim(dm, &dimEmbed));
450963a3b9bcSJacob Faibussowitsch   PetscCheck(dimEmbed >= dim,PETSC_COMM_SELF,PETSC_ERR_PLIB,"Embedding dimension %" PetscInt_FMT " cannot be less than intrinsic dimension %" PetscInt_FMT,dimEmbed,dim);
45109298eaa6SMatthew G Knepley   for (d = 0; d <= depth; ++d) pEnd += numPoints[d];
45119566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(dm, pStart, pEnd));
45129298eaa6SMatthew G Knepley   for (p = pStart; p < pEnd; ++p) {
45139566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeSize(dm, p, coneSize[p-pStart]));
451497e052ccSToby Isaac     if (firstVertex < 0 && !coneSize[p - pStart]) {
451597e052ccSToby Isaac       firstVertex = p - pStart;
45169298eaa6SMatthew G Knepley     }
451797e052ccSToby Isaac   }
45181dca8a05SBarry Smith   PetscCheck(firstVertex >= 0 || !numPoints[0],PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Expected %" PetscInt_FMT " vertices but could not find any", numPoints[0]);
45199566063dSJacob Faibussowitsch   PetscCall(DMSetUp(dm)); /* Allocate space for cones */
45209298eaa6SMatthew G Knepley   for (p = pStart, off = 0; p < pEnd; off += coneSize[p-pStart], ++p) {
45219566063dSJacob Faibussowitsch     PetscCall(DMPlexSetCone(dm, p, &cones[off]));
45229566063dSJacob Faibussowitsch     PetscCall(DMPlexSetConeOrientation(dm, p, &coneOrientations[off]));
45239298eaa6SMatthew G Knepley   }
45249566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(dm));
45259566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(dm));
45269298eaa6SMatthew G Knepley   /* Build coordinates */
45279566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(dm, &coordSection));
45289566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
45299566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, dimEmbed));
45309566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, firstVertex, firstVertex+numPoints[0]));
45319298eaa6SMatthew G Knepley   for (v = firstVertex; v < firstVertex+numPoints[0]; ++v) {
45329566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, dimEmbed));
45339566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, dimEmbed));
45349298eaa6SMatthew G Knepley   }
45359566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
45369566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
45379566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
45389566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
45399566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
45409566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, dimEmbed));
45419566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates,VECSTANDARD));
45429318fe57SMatthew G. Knepley   if (vertexCoords) {
45439566063dSJacob Faibussowitsch     PetscCall(VecGetArray(coordinates, &coords));
45449298eaa6SMatthew G Knepley     for (v = 0; v < numPoints[0]; ++v) {
45459298eaa6SMatthew G Knepley       PetscInt off;
45469298eaa6SMatthew G Knepley 
45479566063dSJacob Faibussowitsch       PetscCall(PetscSectionGetOffset(coordSection, v+firstVertex, &off));
4548811e8653SToby Isaac       for (d = 0; d < dimEmbed; ++d) {
4549811e8653SToby Isaac         coords[off+d] = vertexCoords[v*dimEmbed+d];
45509298eaa6SMatthew G Knepley       }
45519298eaa6SMatthew G Knepley     }
45529318fe57SMatthew G. Knepley   }
45539566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
45549566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(dm, coordinates));
45559566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
45569298eaa6SMatthew G Knepley   PetscFunctionReturn(0);
45579298eaa6SMatthew G Knepley }
45588415267dSToby Isaac 
4559ca522641SMatthew G. Knepley /*@C
45608ca92349SMatthew G. Knepley   DMPlexCreateCellVertexFromFile - Create a DMPlex mesh from a simple cell-vertex file.
45618ca92349SMatthew G. Knepley 
45628ca92349SMatthew G. Knepley + comm        - The MPI communicator
45638ca92349SMatthew G. Knepley . filename    - Name of the .dat file
45648ca92349SMatthew G. Knepley - interpolate - Create faces and edges in the mesh
45658ca92349SMatthew G. Knepley 
45668ca92349SMatthew G. Knepley   Output Parameter:
45678ca92349SMatthew G. Knepley . dm  - The DM object representing the mesh
45688ca92349SMatthew G. Knepley 
45698ca92349SMatthew G. Knepley   Note: The format is the simplest possible:
45708ca92349SMatthew G. Knepley $ Ne
45718ca92349SMatthew G. Knepley $ v0 v1 ... vk
45728ca92349SMatthew G. Knepley $ Nv
45738ca92349SMatthew G. Knepley $ x y z marker
45748ca92349SMatthew G. Knepley 
45758ca92349SMatthew G. Knepley   Level: beginner
45768ca92349SMatthew G. Knepley 
4577db781477SPatrick Sanan .seealso: `DMPlexCreateFromFile()`, `DMPlexCreateMedFromFile()`, `DMPlexCreateGmsh()`, `DMPlexCreate()`
45788ca92349SMatthew G. Knepley @*/
45798ca92349SMatthew G. Knepley PetscErrorCode DMPlexCreateCellVertexFromFile(MPI_Comm comm, const char filename[], PetscBool interpolate, DM *dm)
45808ca92349SMatthew G. Knepley {
45818ca92349SMatthew G. Knepley   DMLabel         marker;
45828ca92349SMatthew G. Knepley   PetscViewer     viewer;
45838ca92349SMatthew G. Knepley   Vec             coordinates;
45848ca92349SMatthew G. Knepley   PetscSection    coordSection;
45858ca92349SMatthew G. Knepley   PetscScalar    *coords;
45868ca92349SMatthew G. Knepley   char            line[PETSC_MAX_PATH_LEN];
45878ca92349SMatthew G. Knepley   PetscInt        dim = 3, cdim = 3, coordSize, v, c, d;
45888ca92349SMatthew G. Knepley   PetscMPIInt     rank;
4589f8d5e320SMatthew G. Knepley   int             snum, Nv, Nc, Ncn, Nl;
45908ca92349SMatthew G. Knepley 
45918ca92349SMatthew G. Knepley   PetscFunctionBegin;
45929566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
45939566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, &viewer));
45949566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(viewer, PETSCVIEWERASCII));
45959566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetMode(viewer, FILE_MODE_READ));
45969566063dSJacob Faibussowitsch   PetscCall(PetscViewerFileSetName(viewer, filename));
4597dd400576SPatrick Sanan   if (rank == 0) {
45989566063dSJacob Faibussowitsch     PetscCall(PetscViewerRead(viewer, line, 4, NULL, PETSC_STRING));
4599f8d5e320SMatthew G. Knepley     snum = sscanf(line, "%d %d %d %d", &Nc, &Nv, &Ncn, &Nl);
460008401ef6SPierre Jolivet     PetscCheck(snum == 4,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
460125ce1634SJed Brown   } else {
4602f8d5e320SMatthew G. Knepley     Nc = Nv = Ncn = Nl = 0;
46038ca92349SMatthew G. Knepley   }
46049566063dSJacob Faibussowitsch   PetscCall(DMCreate(comm, dm));
46059566063dSJacob Faibussowitsch   PetscCall(DMSetType(*dm, DMPLEX));
46069566063dSJacob Faibussowitsch   PetscCall(DMPlexSetChart(*dm, 0, Nc+Nv));
46079566063dSJacob Faibussowitsch   PetscCall(DMSetDimension(*dm, dim));
46089566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinateDim(*dm, cdim));
46098ca92349SMatthew G. Knepley   /* Read topology */
4610dd400576SPatrick Sanan   if (rank == 0) {
4611f8d5e320SMatthew G. Knepley     char     format[PETSC_MAX_PATH_LEN];
4612f8d5e320SMatthew G. Knepley     PetscInt cone[8];
46138ca92349SMatthew G. Knepley     int      vbuf[8], v;
46148ca92349SMatthew G. Knepley 
4615f8d5e320SMatthew G. Knepley     for (c = 0; c < Ncn; ++c) {format[c*3+0] = '%'; format[c*3+1] = 'd'; format[c*3+2] = ' ';}
4616f8d5e320SMatthew G. Knepley     format[Ncn*3-1] = '\0';
46179566063dSJacob Faibussowitsch     for (c = 0; c < Nc; ++c) PetscCall(DMPlexSetConeSize(*dm, c, Ncn));
46189566063dSJacob Faibussowitsch     PetscCall(DMSetUp(*dm));
46198ca92349SMatthew G. Knepley     for (c = 0; c < Nc; ++c) {
46209566063dSJacob Faibussowitsch       PetscCall(PetscViewerRead(viewer, line, Ncn, NULL, PETSC_STRING));
4621f8d5e320SMatthew G. Knepley       switch (Ncn) {
4622f8d5e320SMatthew G. Knepley         case 2: snum = sscanf(line, format, &vbuf[0], &vbuf[1]);break;
4623f8d5e320SMatthew G. Knepley         case 3: snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2]);break;
4624f8d5e320SMatthew G. Knepley         case 4: snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3]);break;
4625f8d5e320SMatthew G. Knepley         case 6: snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5]);break;
4626f8d5e320SMatthew G. Knepley         case 8: snum = sscanf(line, format, &vbuf[0], &vbuf[1], &vbuf[2], &vbuf[3], &vbuf[4], &vbuf[5], &vbuf[6], &vbuf[7]);break;
462763a3b9bcSJacob Faibussowitsch         default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No cell shape with %d vertices", Ncn);
4628f8d5e320SMatthew G. Knepley       }
462908401ef6SPierre Jolivet       PetscCheck(snum == Ncn,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
4630f8d5e320SMatthew G. Knepley       for (v = 0; v < Ncn; ++v) cone[v] = vbuf[v] + Nc;
46318ca92349SMatthew G. Knepley       /* Hexahedra are inverted */
4632f8d5e320SMatthew G. Knepley       if (Ncn == 8) {
46338ca92349SMatthew G. Knepley         PetscInt tmp = cone[1];
46348ca92349SMatthew G. Knepley         cone[1] = cone[3];
46358ca92349SMatthew G. Knepley         cone[3] = tmp;
46368ca92349SMatthew G. Knepley       }
46379566063dSJacob Faibussowitsch       PetscCall(DMPlexSetCone(*dm, c, cone));
46388ca92349SMatthew G. Knepley     }
46398ca92349SMatthew G. Knepley   }
46409566063dSJacob Faibussowitsch   PetscCall(DMPlexSymmetrize(*dm));
46419566063dSJacob Faibussowitsch   PetscCall(DMPlexStratify(*dm));
46428ca92349SMatthew G. Knepley   /* Read coordinates */
46439566063dSJacob Faibussowitsch   PetscCall(DMGetCoordinateSection(*dm, &coordSection));
46449566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetNumFields(coordSection, 1));
46459566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetFieldComponents(coordSection, 0, cdim));
46469566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetChart(coordSection, Nc, Nc + Nv));
46478ca92349SMatthew G. Knepley   for (v = Nc; v < Nc+Nv; ++v) {
46489566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetDof(coordSection, v, cdim));
46499566063dSJacob Faibussowitsch     PetscCall(PetscSectionSetFieldDof(coordSection, v, 0, cdim));
46508ca92349SMatthew G. Knepley   }
46519566063dSJacob Faibussowitsch   PetscCall(PetscSectionSetUp(coordSection));
46529566063dSJacob Faibussowitsch   PetscCall(PetscSectionGetStorageSize(coordSection, &coordSize));
46539566063dSJacob Faibussowitsch   PetscCall(VecCreate(PETSC_COMM_SELF, &coordinates));
46549566063dSJacob Faibussowitsch   PetscCall(PetscObjectSetName((PetscObject) coordinates, "coordinates"));
46559566063dSJacob Faibussowitsch   PetscCall(VecSetSizes(coordinates, coordSize, PETSC_DETERMINE));
46569566063dSJacob Faibussowitsch   PetscCall(VecSetBlockSize(coordinates, cdim));
46579566063dSJacob Faibussowitsch   PetscCall(VecSetType(coordinates, VECSTANDARD));
46589566063dSJacob Faibussowitsch   PetscCall(VecGetArray(coordinates, &coords));
4659dd400576SPatrick Sanan   if (rank == 0) {
4660f8d5e320SMatthew G. Knepley     char   format[PETSC_MAX_PATH_LEN];
46618ca92349SMatthew G. Knepley     double x[3];
4662f8d5e320SMatthew G. Knepley     int    l, val[3];
46638ca92349SMatthew G. Knepley 
4664f8d5e320SMatthew G. Knepley     if (Nl) {
4665f8d5e320SMatthew G. Knepley       for (l = 0; l < Nl; ++l) {format[l*3+0] = '%'; format[l*3+1] = 'd'; format[l*3+2] = ' ';}
4666f8d5e320SMatthew G. Knepley       format[Nl*3-1] = '\0';
46679566063dSJacob Faibussowitsch       PetscCall(DMCreateLabel(*dm, "marker"));
46689566063dSJacob Faibussowitsch       PetscCall(DMGetLabel(*dm, "marker", &marker));
4669f8d5e320SMatthew G. Knepley     }
46708ca92349SMatthew G. Knepley     for (v = 0; v < Nv; ++v) {
46719566063dSJacob Faibussowitsch       PetscCall(PetscViewerRead(viewer, line, 3+Nl, NULL, PETSC_STRING));
4672f8d5e320SMatthew G. Knepley       snum = sscanf(line, "%lg %lg %lg", &x[0], &x[1], &x[2]);
467308401ef6SPierre Jolivet       PetscCheck(snum == 3,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
4674f8d5e320SMatthew G. Knepley       switch (Nl) {
4675f8d5e320SMatthew G. Knepley         case 0: snum = 0;break;
4676f8d5e320SMatthew G. Knepley         case 1: snum = sscanf(line, format, &val[0]);break;
4677f8d5e320SMatthew G. Knepley         case 2: snum = sscanf(line, format, &val[0], &val[1]);break;
4678f8d5e320SMatthew G. Knepley         case 3: snum = sscanf(line, format, &val[0], &val[1], &val[2]);break;
467963a3b9bcSJacob Faibussowitsch         default: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Request support for %d labels", Nl);
4680f8d5e320SMatthew G. Knepley       }
468108401ef6SPierre Jolivet       PetscCheck(snum == Nl,PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unable to parse cell-vertex file: %s", line);
46828ca92349SMatthew G. Knepley       for (d = 0; d < cdim; ++d) coords[v*cdim+d] = x[d];
46839566063dSJacob Faibussowitsch       for (l = 0; l < Nl; ++l) PetscCall(DMLabelSetValue(marker, v+Nc, val[l]));
46848ca92349SMatthew G. Knepley     }
46858ca92349SMatthew G. Knepley   }
46869566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(coordinates, &coords));
46879566063dSJacob Faibussowitsch   PetscCall(DMSetCoordinatesLocal(*dm, coordinates));
46889566063dSJacob Faibussowitsch   PetscCall(VecDestroy(&coordinates));
46899566063dSJacob Faibussowitsch   PetscCall(PetscViewerDestroy(&viewer));
46908ca92349SMatthew G. Knepley   if (interpolate) {
46918ca92349SMatthew G. Knepley     DM      idm;
46928ca92349SMatthew G. Knepley     DMLabel bdlabel;
46938ca92349SMatthew G. Knepley 
46949566063dSJacob Faibussowitsch     PetscCall(DMPlexInterpolate(*dm, &idm));
46959566063dSJacob Faibussowitsch     PetscCall(DMDestroy(dm));
46968ca92349SMatthew G. Knepley     *dm  = idm;
46978ca92349SMatthew G. Knepley 
4698f8d5e320SMatthew G. Knepley     if (!Nl) {
46999566063dSJacob Faibussowitsch       PetscCall(DMCreateLabel(*dm, "marker"));
47009566063dSJacob Faibussowitsch       PetscCall(DMGetLabel(*dm, "marker", &bdlabel));
47019566063dSJacob Faibussowitsch       PetscCall(DMPlexMarkBoundaryFaces(*dm, PETSC_DETERMINE, bdlabel));
47029566063dSJacob Faibussowitsch       PetscCall(DMPlexLabelComplete(*dm, bdlabel));
47038ca92349SMatthew G. Knepley     }
4704f8d5e320SMatthew G. Knepley   }
47058ca92349SMatthew G. Knepley   PetscFunctionReturn(0);
47068ca92349SMatthew G. Knepley }
47078ca92349SMatthew G. Knepley 
47088ca92349SMatthew G. Knepley /*@C
4709ca522641SMatthew G. Knepley   DMPlexCreateFromFile - This takes a filename and produces a DM
4710ca522641SMatthew G. Knepley 
4711ca522641SMatthew G. Knepley   Input Parameters:
4712ca522641SMatthew G. Knepley + comm - The communicator
4713ca522641SMatthew G. Knepley . filename - A file name
4714cd7e8a5eSksagiyam . plexname - The object name of the resulting DM, also used for intra-datafile lookup by some formats
4715ca522641SMatthew G. Knepley - interpolate - Flag to create intermediate mesh pieces (edges, faces)
4716ca522641SMatthew G. Knepley 
4717ca522641SMatthew G. Knepley   Output Parameter:
4718ca522641SMatthew G. Knepley . dm - The DM
4719ca522641SMatthew G. Knepley 
472002ef0d99SVaclav Hapla   Options Database Keys:
472102ef0d99SVaclav Hapla . -dm_plex_create_from_hdf5_xdmf - use the PETSC_VIEWER_HDF5_XDMF format for reading HDF5
472202ef0d99SVaclav Hapla 
4723bca97951SVaclav Hapla   Use -dm_plex_create_ prefix to pass options to the internal PetscViewer, e.g.
4724bca97951SVaclav Hapla $ -dm_plex_create_viewer_hdf5_collective
4725bca97951SVaclav Hapla 
4726cd7e8a5eSksagiyam   Notes:
4727cd7e8a5eSksagiyam   Using PETSCVIEWERHDF5 type with PETSC_VIEWER_HDF5_PETSC format, one can save multiple DMPlex
4728cd7e8a5eSksagiyam   meshes in a single HDF5 file. This in turn requires one to name the DMPlex object with PetscObjectSetName()
4729cd7e8a5eSksagiyam   before saving it with DMView() and before loading it with DMLoad() for identification of the mesh object.
4730cd7e8a5eSksagiyam   The input parameter name is thus used to name the DMPlex object when DMPlexCreateFromFile() internally
4731cd7e8a5eSksagiyam   calls DMLoad(). Currently, name is ignored for other viewer types and/or formats.
4732cd7e8a5eSksagiyam 
4733ca522641SMatthew G. Knepley   Level: beginner
4734ca522641SMatthew G. Knepley 
4735db781477SPatrick Sanan .seealso: `DMPlexCreateFromDAG()`, `DMPlexCreateFromCellListPetsc()`, `DMPlexCreate()`, `PetscObjectSetName()`, `DMView()`, `DMLoad()`
4736ca522641SMatthew G. Knepley @*/
4737cd7e8a5eSksagiyam PetscErrorCode DMPlexCreateFromFile(MPI_Comm comm, const char filename[], const char plexname[], PetscBool interpolate, DM *dm)
4738ca522641SMatthew G. Knepley {
4739ef3a5affSJacob Faibussowitsch   const char  extGmsh[]      = ".msh";
4740ef3a5affSJacob Faibussowitsch   const char  extGmsh2[]     = ".msh2";
4741ef3a5affSJacob Faibussowitsch   const char  extGmsh4[]     = ".msh4";
4742ef3a5affSJacob Faibussowitsch   const char  extCGNS[]      = ".cgns";
4743ef3a5affSJacob Faibussowitsch   const char  extExodus[]    = ".exo";
4744ef3a5affSJacob Faibussowitsch   const char  extExodus_e[]  = ".e";
4745ef3a5affSJacob Faibussowitsch   const char  extGenesis[]   = ".gen";
4746ef3a5affSJacob Faibussowitsch   const char  extFluent[]    = ".cas";
4747ef3a5affSJacob Faibussowitsch   const char  extHDF5[]      = ".h5";
4748ef3a5affSJacob Faibussowitsch   const char  extMed[]       = ".med";
4749ef3a5affSJacob Faibussowitsch   const char  extPLY[]       = ".ply";
4750ef3a5affSJacob Faibussowitsch   const char  extEGADSLite[] = ".egadslite";
4751ef3a5affSJacob Faibussowitsch   const char  extEGADS[]     = ".egads";
4752ef3a5affSJacob Faibussowitsch   const char  extIGES[]      = ".igs";
4753ef3a5affSJacob Faibussowitsch   const char  extSTEP[]      = ".stp";
4754ef3a5affSJacob Faibussowitsch   const char  extCV[]        = ".dat";
4755ca522641SMatthew G. Knepley   size_t      len;
4756c1cad2e7SMatthew G. Knepley   PetscBool   isGmsh, isGmsh2, isGmsh4, isCGNS, isExodus, isGenesis, isFluent, isHDF5, isMed, isPLY, isEGADSLite, isEGADS, isIGES, isSTEP, isCV;
4757ca522641SMatthew G. Knepley   PetscMPIInt rank;
4758ca522641SMatthew G. Knepley 
4759ca522641SMatthew G. Knepley   PetscFunctionBegin;
47605d80c0bfSVaclav Hapla   PetscValidCharPointer(filename, 2);
47610d862eaeSPierre Jolivet   if (plexname) PetscValidCharPointer(plexname, 3);
4762cd7e8a5eSksagiyam   PetscValidPointer(dm, 5);
47639566063dSJacob Faibussowitsch   PetscCall(DMInitializePackage());
47649566063dSJacob Faibussowitsch   PetscCall(PetscLogEventBegin(DMPLEX_CreateFromFile,0,0,0,0));
47659566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(comm, &rank));
47669566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(filename, &len));
476728b400f6SJacob Faibussowitsch   PetscCheck(len,comm, PETSC_ERR_ARG_WRONG, "Filename must be a valid path");
4768ef3a5affSJacob Faibussowitsch 
4769ef3a5affSJacob Faibussowitsch #define CheckExtension(extension__,is_extension__) do {                                        \
4770274aaeaaSJacob Faibussowitsch     PetscAssert(sizeof(extension__), comm, PETSC_ERR_PLIB, "Zero-size extension: %s", extension__); \
4771274aaeaaSJacob Faibussowitsch     /* don't count the null-terminator at the end */                                           \
4772274aaeaaSJacob Faibussowitsch     const size_t ext_len = sizeof(extension__)-1;                                              \
4773274aaeaaSJacob Faibussowitsch     if (len < ext_len) {                                                                       \
4774ef3a5affSJacob Faibussowitsch       is_extension__ = PETSC_FALSE;                                                            \
4775ef3a5affSJacob Faibussowitsch     } else {                                                                                   \
4776274aaeaaSJacob Faibussowitsch       PetscCall(PetscStrncmp(filename+len-ext_len, extension__, ext_len, &is_extension__));    \
4777ef3a5affSJacob Faibussowitsch     }                                                                                          \
4778ef3a5affSJacob Faibussowitsch   } while (0)
4779ef3a5affSJacob Faibussowitsch 
4780ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh, isGmsh);
4781ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh2, isGmsh2);
4782ef3a5affSJacob Faibussowitsch   CheckExtension(extGmsh4, isGmsh4);
4783ef3a5affSJacob Faibussowitsch   CheckExtension(extCGNS, isCGNS);
4784ef3a5affSJacob Faibussowitsch   CheckExtension(extExodus, isExodus);
4785ef3a5affSJacob Faibussowitsch   if (!isExodus) CheckExtension(extExodus_e, isExodus);
4786ef3a5affSJacob Faibussowitsch   CheckExtension(extGenesis, isGenesis);
4787ef3a5affSJacob Faibussowitsch   CheckExtension(extFluent, isFluent);
4788ef3a5affSJacob Faibussowitsch   CheckExtension(extHDF5, isHDF5);
4789ef3a5affSJacob Faibussowitsch   CheckExtension(extMed, isMed);
4790ef3a5affSJacob Faibussowitsch   CheckExtension(extPLY, isPLY);
4791ef3a5affSJacob Faibussowitsch   CheckExtension(extEGADSLite, isEGADSLite);
4792ef3a5affSJacob Faibussowitsch   CheckExtension(extEGADS, isEGADS);
4793ef3a5affSJacob Faibussowitsch   CheckExtension(extIGES, isIGES);
4794ef3a5affSJacob Faibussowitsch   CheckExtension(extSTEP, isSTEP);
4795ef3a5affSJacob Faibussowitsch   CheckExtension(extCV, isCV);
4796ef3a5affSJacob Faibussowitsch 
4797ef3a5affSJacob Faibussowitsch #undef CheckExtension
4798ef3a5affSJacob Faibussowitsch 
4799de78e4feSLisandro Dalcin   if (isGmsh || isGmsh2 || isGmsh4) {
48009566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateGmshFromFile(comm, filename, interpolate, dm));
4801ca522641SMatthew G. Knepley   } else if (isCGNS) {
48029566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCGNSFromFile(comm, filename, interpolate, dm));
480390c68965SMatthew G. Knepley   } else if (isExodus || isGenesis) {
48049566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateExodusFromFile(comm, filename, interpolate, dm));
48052f0bd6dcSMichael Lange   } else if (isFluent) {
48069566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateFluentFromFile(comm, filename, interpolate, dm));
4807cc2f8f65SMatthew G. Knepley   } else if (isHDF5) {
48089c48423bSVaclav Hapla     PetscBool      load_hdf5_xdmf = PETSC_FALSE;
4809cc2f8f65SMatthew G. Knepley     PetscViewer viewer;
4810cc2f8f65SMatthew G. Knepley 
481143b242b4SVaclav 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 */
48129566063dSJacob Faibussowitsch     PetscCall(PetscStrncmp(&filename[PetscMax(0,len-8)], ".xdmf",  5, &load_hdf5_xdmf));
48139566063dSJacob Faibussowitsch     PetscCall(PetscOptionsGetBool(NULL, NULL, "-dm_plex_create_from_hdf5_xdmf", &load_hdf5_xdmf, NULL));
48149566063dSJacob Faibussowitsch     PetscCall(PetscViewerCreate(comm, &viewer));
48159566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetType(viewer, PETSCVIEWERHDF5));
48169566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetOptionsPrefix(viewer, "dm_plex_create_"));
48179566063dSJacob Faibussowitsch     PetscCall(PetscViewerSetFromOptions(viewer));
48189566063dSJacob Faibussowitsch     PetscCall(PetscViewerFileSetMode(viewer, FILE_MODE_READ));
48199566063dSJacob Faibussowitsch     PetscCall(PetscViewerFileSetName(viewer, filename));
4820cd7e8a5eSksagiyam 
48219566063dSJacob Faibussowitsch     PetscCall(DMCreate(comm, dm));
48229566063dSJacob Faibussowitsch     PetscCall(PetscObjectSetName((PetscObject)(*dm), plexname));
48239566063dSJacob Faibussowitsch     PetscCall(DMSetType(*dm, DMPLEX));
48249566063dSJacob Faibussowitsch     if (load_hdf5_xdmf) PetscCall(PetscViewerPushFormat(viewer, PETSC_VIEWER_HDF5_XDMF));
48259566063dSJacob Faibussowitsch     PetscCall(DMLoad(*dm, viewer));
48269566063dSJacob Faibussowitsch     if (load_hdf5_xdmf) PetscCall(PetscViewerPopFormat(viewer));
48279566063dSJacob Faibussowitsch     PetscCall(PetscViewerDestroy(&viewer));
48285fd9971aSMatthew G. Knepley 
48295fd9971aSMatthew G. Knepley     if (interpolate) {
48305fd9971aSMatthew G. Knepley       DM idm;
48315fd9971aSMatthew G. Knepley 
48329566063dSJacob Faibussowitsch       PetscCall(DMPlexInterpolate(*dm, &idm));
48339566063dSJacob Faibussowitsch       PetscCall(DMDestroy(dm));
48345fd9971aSMatthew G. Knepley       *dm  = idm;
48355fd9971aSMatthew G. Knepley     }
4836707dd687SMichael Lange   } else if (isMed) {
48379566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateMedFromFile(comm, filename, interpolate, dm));
4838f2801cd6SMatthew G. Knepley   } else if (isPLY) {
48399566063dSJacob Faibussowitsch     PetscCall(DMPlexCreatePLYFromFile(comm, filename, interpolate, dm));
4840c1cad2e7SMatthew G. Knepley   } else if (isEGADSLite || isEGADS || isIGES || isSTEP) {
48419566063dSJacob Faibussowitsch     if (isEGADSLite) PetscCall(DMPlexCreateEGADSLiteFromFile(comm, filename, dm));
48429566063dSJacob Faibussowitsch     else             PetscCall(DMPlexCreateEGADSFromFile(comm, filename, dm));
48437bee2925SMatthew Knepley     if (!interpolate) {
48447bee2925SMatthew Knepley       DM udm;
48457bee2925SMatthew Knepley 
48469566063dSJacob Faibussowitsch       PetscCall(DMPlexUninterpolate(*dm, &udm));
48479566063dSJacob Faibussowitsch       PetscCall(DMDestroy(dm));
48487bee2925SMatthew Knepley       *dm  = udm;
48497bee2925SMatthew Knepley     }
48508ca92349SMatthew G. Knepley   } else if (isCV) {
48519566063dSJacob Faibussowitsch     PetscCall(DMPlexCreateCellVertexFromFile(comm, filename, interpolate, dm));
485298921bdaSJacob Faibussowitsch   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot load file %s: unrecognized extension", filename);
48539566063dSJacob Faibussowitsch   PetscCall(PetscStrlen(plexname, &len));
48549566063dSJacob Faibussowitsch   if (len) PetscCall(PetscObjectSetName((PetscObject)(*dm), plexname));
48559566063dSJacob Faibussowitsch   PetscCall(PetscLogEventEnd(DMPLEX_CreateFromFile,0,0,0,0));
4856ca522641SMatthew G. Knepley   PetscFunctionReturn(0);
4857ca522641SMatthew G. Knepley }
4858